summaryrefslogtreecommitdiffstats
path: root/deluge/plugins/Stats/deluge/plugins/stats/gtkui.py
blob: d3bec7353ac50447c493af4267480ab20a51ec12 (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
#
# gtkui.py
#
# Copyright (C) 2008 Martijn Voncken <mvoncken@gmail.com>
#
# Basic plugin template created by:
# Copyright (C) 2008 Martijn Voncken <mvoncken@gmail.com>
# Copyright (C) 2007, 2008 Andrew Resch <andrewresch@gmail.com>
#
# Deluge is free software.
#
# You may redistribute it and/or modify it under the terms of the
# GNU General Public License, as published by the Free Software
# Foundation; either version 3 of the License, or (at your option)
# any later version.
#
# deluge is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with deluge.    If not, write to:
# 	The Free Software Foundation, Inc.,
# 	51 Franklin Street, Fifth Floor
# 	Boston, MA  02110-1301, USA.
#
#    In addition, as a special exception, the copyright holders give
#    permission to link the code of portions of this program with the OpenSSL
#    library.
#    You must obey the GNU General Public License in all respects for all of
#    the code used other than OpenSSL. If you modify file(s) with this
#    exception, you may extend this exception to your version of the file(s),
#    but you are not obligated to do so. If you do not wish to do so, delete
#    this exception statement from your version. If you delete this exception
#    statement from all source files in the program, then also delete it here.
#
#
#    In addition, as a special exception, the copyright holders give
#    permission to link the code of portions of this program with the OpenSSL
#    library.
#    You must obey the GNU General Public License in all respects for all of
#    the code used other than OpenSSL. If you modify file(s) with this
#    exception, you may extend this exception to your version of the file(s),
#    but you are not obligated to do so. If you do not wish to do so, delete
#    this exception statement from your version. If you delete this exception

import os
import gtk
import logging
from gtk.glade import XML

from twisted.internet import defer

# Relative imports
from . import common
from . import graph

from deluge import component
from deluge.common import fspeed
from deluge.ui.client import client
from deluge.ui.gtkui.torrentdetails import Tab
from deluge.plugins.pluginbase import GtkPluginBase

log = logging.getLogger(__name__)

class GraphsTab(Tab):
    def __init__(self, glade):
        Tab.__init__(self)
        self._name = 'Graphs'
        self.glade = glade
        self.window = self.glade.get_widget('graph_tab')
        self._child_widget = self.window
        self.notebook = self.glade.get_widget('graph_notebook')
        self.label = self.glade.get_widget('graph_label')
        self._tab_label = self.label
        self.bandwidth_graph = self.glade.get_widget('bandwidth_graph')
        self.bandwidth_graph.connect('expose_event', self.expose)
        self.window.unparent()
        self.label.unparent()

        self.graph_widget = self.bandwidth_graph
        self.graph = graph.Graph()
        self.graph.add_stat('payload_download_rate', label='Download Rate', color=graph.green)
        self.graph.add_stat('payload_upload_rate', label='Upload Rate', color=graph.blue)
        self.graph.set_left_axis(formatter=fspeed, min=10240)

    def expose(self, widget, event):
        """Redraw"""
        context = self.graph_widget.window.cairo_create()
        # set a clip region
        context.rectangle(event.area.x, event.area.y,
                           event.area.width, event.area.height)
        context.clip()

        width, height = self.graph_widget.allocation.width, self.graph_widget.allocation.height
        self.graph.draw_to_context(context, width, height)
        #Do not propagate the event
        return False

    def update(self):
        log.debug("getstat keys: %s", self.graph.stat_info.keys())
        d1 = client.stats.get_stats(self.graph.stat_info.keys())
        d1.addCallback(self.graph.set_stats)
        d2 = client.stats.get_config()
        d2.addCallback(self.graph.set_config)
        dl = defer.DeferredList([d1, d2])

        def _on_update(result):
            width, height = self.graph_widget.allocation.width, self.graph_widget.allocation.height
            rect = gtk.gdk.Rectangle(0, 0, width, height)
            self.graph_widget.window.invalidate_rect(rect, True)

        dl.addCallback(_on_update)

    def clear(self):
        pass



class GtkUI(GtkPluginBase):
    def enable(self):
        log.debug("Stats plugin enable called")
        self.glade = XML(common.get_resource("config.glade"))
        component.get("Preferences").add_page("Stats", self.glade.get_widget("prefs_box"))
        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.on_show_prefs()

        self.graphs_tab = GraphsTab(XML(common.get_resource("tabs.glade")))
        self.torrent_details = component.get('TorrentDetails')
        self.torrent_details.add_tab(self.graphs_tab)

    def disable(self):
        component.get("Preferences").remove_page("Stats")
        component.get("PluginManager").deregister_hook("on_apply_prefs", self.on_apply_prefs)
        component.get("PluginManager").deregister_hook("on_show_prefs", self.on_show_prefs)
        self.torrent_details.remove_tab(self.graphs_tab.get_name())

    def on_apply_prefs(self):
        log.debug("applying prefs for Stats")
        config = {
            "test":self.glade.get_widget("txt_test").get_text()
        }
        client.stats.set_config(config)

    def on_show_prefs(self):
        client.stats.get_config().addCallback(self.cb_get_config)

    def cb_get_config(self, config):
        "callback for on show_prefs"
        self.glade.get_widget("txt_test").set_text(config["test"])