summaryrefslogtreecommitdiffstats
path: root/deluge/plugins/Scheduler/deluge_scheduler/gtkui.py
blob: 3b768eb488df73def7ca2f0570b8337dd123b2f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# -*- coding: utf-8 -*-
#
# Copyright (C) 2009 Andrew Resch <andrewresch@gmail.com>
#
# Basic plugin template created by:
# Copyright (C) 2008 Martijn Voncken <mvoncken@gmail.com>
# Copyright (C) 2007-2009 Andrew Resch <andrewresch@gmail.com>
#
# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with
# the additional special exception to link portions of this program with the OpenSSL library.
# See LICENSE for more details.
#

import logging

from gi.repository import Gdk, Gtk

import deluge.component as component
from deluge.plugins.pluginbase import Gtk3PluginBase
from deluge.ui.client import client

from .common import get_resource

log = logging.getLogger(__name__)

DAYS = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']


class SchedulerSelectWidget(Gtk.DrawingArea):
    def __init__(self, hover):
        super(SchedulerSelectWidget, self).__init__()
        self.set_events(
            Gdk.EventMask.BUTTON_PRESS_MASK
            | Gdk.EventMask.BUTTON_RELEASE_MASK
            | Gdk.EventMask.POINTER_MOTION_MASK
            | Gdk.EventMask.LEAVE_NOTIFY_MASK
        )

        self.connect('draw', self.draw)
        self.connect('button_press_event', self.mouse_down)
        self.connect('button_release_event', self.mouse_up)
        self.connect('motion_notify_event', self.mouse_hover)
        self.connect('leave_notify_event', self.mouse_leave)

        self.colors = [
            [115 / 255, 210 / 255, 22 / 255],
            [237 / 255, 212 / 255, 0 / 255],
            [204 / 255, 0 / 255, 0 / 255],
        ]
        self.button_state = [[0] * 7 for dummy in range(24)]

        self.start_point = [0, 0]
        self.hover_point = [-1, -1]
        self.hover_label = hover
        self.hover_days = DAYS
        self.mouse_press = False
        self.set_size_request(350, 150)

    def set_button_state(self, state):
        self.button_state = []
        for s in state:
            self.button_state.append(list(s))
        log.debug(self.button_state)

    # redraw the whole thing
    def draw(self, widget, context):
        width = widget.get_allocated_width()
        height = widget.get_allocated_height()
        context.rectangle(0, 0, width, height)
        context.clip()

        for y in range(7):
            for x in range(24):
                context.set_source_rgba(
                    self.colors[self.button_state[x][y]][0],
                    self.colors[self.button_state[x][y]][1],
                    self.colors[self.button_state[x][y]][2],
                    0.5,
                )
                context.rectangle(
                    width * (6 * x / 145 + 1 / 145),
                    height * (6 * y / 43 + 1 / 43),
                    6 * width / 145,
                    5 * height / 43,
                )
                context.fill_preserve()
                context.set_source_rgba(0, 0, 0, 0.7)
                context.set_line_width(1)
                context.stroke()

    # coordinates --> which box
    def get_point(self, event):
        width = self.get_allocated_width()
        height = self.get_allocated_height()
        x = int((event.x - width * 0.5 / 145) / (6 * width / 145))
        y = int((event.y - height * 0.5 / 43) / (6 * height / 43))

        if x > 23:
            x = 23
        elif x < 0:
            x = 0
        if y > 6:
            y = 6
        elif y < 0:
            y = 0

        return [x, y]

    # mouse down
    def mouse_down(self, widget, event):
        self.mouse_press = True
        self.start_point = self.get_point(event)

    # if the same box -> change it
    def mouse_up(self, widget, event):
        self.mouse_press = False
        end_point = self.get_point(event)

        # change color on mouseclick depending on the button
        if end_point[0] is self.start_point[0] and end_point[1] is self.start_point[1]:
            if event.button == 1:
                self.button_state[end_point[0]][end_point[1]] += 1
                if self.button_state[end_point[0]][end_point[1]] > 2:
                    self.button_state[end_point[0]][end_point[1]] = 0
            elif event.button == 3:
                self.button_state[end_point[0]][end_point[1]] -= 1
                if self.button_state[end_point[0]][end_point[1]] < 0:
                    self.button_state[end_point[0]][end_point[1]] = 2
            self.queue_draw()

    # if box changed and mouse is pressed draw all boxes from start point to end point
    # set hover text etc..
    def mouse_hover(self, widget, event):
        if self.get_point(event) != self.hover_point:
            self.hover_point = self.get_point(event)

            self.hover_label.set_text(
                self.hover_days[self.hover_point[1]]
                + ' '
                + str(self.hover_point[0])
                + ':00 - '
                + str(self.hover_point[0])
                + ':59'
            )

            if self.mouse_press:
                points = [
                    [self.hover_point[0], self.start_point[0]],
                    [self.hover_point[1], self.start_point[1]],
                ]

                for x in range(min(points[0]), max(points[0]) + 1):
                    for y in range(min(points[1]), max(points[1]) + 1):
                        self.button_state[x][y] = self.button_state[
                            self.start_point[0]
                        ][self.start_point[1]]

                self.queue_draw()

    # clear hover text on mouse leave
    def mouse_leave(self, widget, event):
        self.hover_label.set_text('')
        self.hover_point = [-1, -1]


class GtkUI(Gtk3PluginBase):
    def enable(self):
        self.create_prefs_page()

        component.get('PluginManager').register_hook(
            'on_apply_prefs', self.on_apply_prefs
        )
        component.get('PluginManager').register_hook(
            'on_show_prefs', self.on_show_prefs
        )
        self.statusbar = component.get('StatusBar')
        self.status_item = self.statusbar.add_item(
            image=get_resource('green.svg'),
            text='',
            callback=self.on_status_item_clicked,
            tooltip='Scheduler',
        )

        def on_state_deferred(state):
            self.state = state
            self.on_scheduler_event(state)

        self.on_show_prefs()

        client.scheduler.get_state().addCallback(on_state_deferred)
        client.register_event_handler('SchedulerEvent', self.on_scheduler_event)

    def disable(self):
        component.get('Preferences').remove_page(_('Scheduler'))
        # Reset statusbar dict.
        self.statusbar.config_value_changed_dict[
            'max_download_speed'
        ] = self.statusbar._on_max_download_speed
        self.statusbar.config_value_changed_dict[
            'max_upload_speed'
        ] = self.statusbar._on_max_upload_speed
        # Remove statusbar item.
        self.statusbar.remove_item(self.status_item)
        del self.status_item

        component.get('PluginManager').deregister_hook(
            'on_apply_prefs', self.on_apply_prefs
        )
        component.get('PluginManager').deregister_hook(
            'on_show_prefs', self.on_show_prefs
        )

    def on_apply_prefs(self):
        log.debug('applying prefs for Scheduler')
        config = {}
        config['low_down'] = self.spin_download.get_value()
        config['low_up'] = self.spin_upload.get_value()
        config['low_active'] = self.spin_active.get_value_as_int()
        config['low_active_down'] = self.spin_active_down.get_value_as_int()
        config['low_active_up'] = self.spin_active_up.get_value_as_int()
        config['button_state'] = self.scheduler_select.button_state
        client.scheduler.set_config(config)

    def on_show_prefs(self):
        def on_get_config(config):
            log.debug('config: %s', config)
            self.scheduler_select.set_button_state(config['button_state'])
            self.spin_download.set_value(config['low_down'])
            self.spin_upload.set_value(config['low_up'])
            self.spin_active.set_value(config['low_active'])
            self.spin_active_down.set_value(config['low_active_down'])
            self.spin_active_up.set_value(config['low_active_up'])

        client.scheduler.get_config().addCallback(on_get_config)

    def on_scheduler_event(self, state):
        self.state = state
        self.status_item.set_image_from_file(get_resource(self.state.lower() + '.svg'))
        if self.state == 'Yellow':
            # Prevent func calls in Statusbar if the config changes.
            self.statusbar.config_value_changed_dict.pop('max_download_speed', None)
            self.statusbar.config_value_changed_dict.pop('max_upload_speed', None)
            try:
                self.statusbar._on_max_download_speed(self.spin_download.get_value())
                self.statusbar._on_max_upload_speed(self.spin_upload.get_value())
            except AttributeError:
                # Skip error due to Plugin being enabled before statusbar items created on startup.
                pass
        else:
            self.statusbar.config_value_changed_dict[
                'max_download_speed'
            ] = self.statusbar._on_max_download_speed
            self.statusbar.config_value_changed_dict[
                'max_upload_speed'
            ] = self.statusbar._on_max_upload_speed

            def update_config_values(config):
                try:
                    self.statusbar._on_max_download_speed(config['max_download_speed'])
                    self.statusbar._on_max_upload_speed(config['max_upload_speed'])
                except AttributeError:
                    # Skip error due to Plugin being enabled before statusbar items created on startup.
                    pass

            client.core.get_config_values(
                ['max_download_speed', 'max_upload_speed']
            ).addCallback(update_config_values)

    def on_status_item_clicked(self, widget, event):
        component.get('Preferences').show('Scheduler')

    # Configuration dialog
    def create_prefs_page(self):
        # Select Widget
        hover = Gtk.Label()
        self.scheduler_select = SchedulerSelectWidget(hover)

        vbox = Gtk.Box.new(Gtk.Orientation.VERTICAL, spacing=5)
        hbox = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, spacing=5)
        vbox_days = Gtk.Box.new(Gtk.Orientation.VERTICAL, spacing=0)
        for day in DAYS:
            vbox_days.pack_start(Gtk.Label(day, xalign=0), True, False, 0)
        hbox.pack_start(vbox_days, False, False, 15)
        hbox.pack_start(self.scheduler_select, True, True, 0)
        frame = Gtk.Frame()
        label = Gtk.Label()
        label.set_markup(_('<b>Schedule</b>'))
        frame.set_label_widget(label)
        frame.set_shadow_type(Gtk.ShadowType.NONE)
        frame.set_margin_left(15)
        frame.add(hbox)

        vbox.pack_start(frame, False, False, 0)
        vbox.pack_start(hover, False, False, 0)

        table = Gtk.Table(5, 2)
        table.set_margin_left(15)

        label = Gtk.Label(_('Download Limit:'))
        label.set_alignment(0.0, 0.6)
        table.attach_defaults(label, 0, 1, 0, 1)
        self.spin_download = Gtk.SpinButton()
        self.spin_download.set_numeric(True)
        self.spin_download.set_range(-1.0, 99999.0)
        self.spin_download.set_increments(1, 10)
        table.attach_defaults(self.spin_download, 1, 2, 0, 1)

        label = Gtk.Label(_('Upload Limit:'))
        label.set_alignment(0.0, 0.6)
        table.attach_defaults(label, 0, 1, 1, 2)
        self.spin_upload = Gtk.SpinButton()
        self.spin_upload.set_numeric(True)
        self.spin_upload.set_range(-1.0, 99999.0)
        self.spin_upload.set_increments(1, 10)
        table.attach_defaults(self.spin_upload, 1, 2, 1, 2)

        label = Gtk.Label(_('Active Torrents:'))
        label.set_alignment(0.0, 0.6)
        table.attach_defaults(label, 0, 1, 2, 3)
        self.spin_active = Gtk.SpinButton()
        self.spin_active.set_numeric(True)
        self.spin_active.set_range(-1, 9999)
        self.spin_active.set_increments(1, 10)
        table.attach_defaults(self.spin_active, 1, 2, 2, 3)

        label = Gtk.Label(_('Active Downloading:'))
        label.set_alignment(0.0, 0.6)
        table.attach_defaults(label, 0, 1, 3, 4)
        self.spin_active_down = Gtk.SpinButton()
        self.spin_active_down.set_numeric(True)
        self.spin_active_down.set_range(-1, 9999)
        self.spin_active_down.set_increments(1, 10)
        table.attach_defaults(self.spin_active_down, 1, 2, 3, 4)

        label = Gtk.Label(_('Active Seeding:'))
        label.set_alignment(0.0, 0.6)
        table.attach_defaults(label, 0, 1, 4, 5)
        self.spin_active_up = Gtk.SpinButton()
        self.spin_active_up.set_numeric(True)
        self.spin_active_up.set_range(-1, 9999)
        self.spin_active_up.set_increments(1, 10)
        table.attach_defaults(self.spin_active_up, 1, 2, 4, 5)

        eventbox = Gtk.EventBox()
        eventbox.add(table)
        frame = Gtk.Frame()
        label = Gtk.Label()
        label.set_markup(_('<b>Slow Settings</b>'))
        label.modify_bg(Gtk.StateFlags.NORMAL, Gdk.color_parse('#EDD400'))
        frame.set_label_widget(label)
        frame.set_margin_left(15)
        frame.set_border_width(2)
        frame.add(eventbox)
        vbox.pack_start(frame, False, False, 0)

        vbox.show_all()
        component.get('Preferences').add_page(_('Scheduler'), vbox)