~cmiller/ubuntu/quantal/deluge/fix-parameter-move-storage

« back to all changes in this revision

Viewing changes to deluge/ui/gtkui/systemtray.py

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Starr-Bochicchio
  • Date: 2011-03-13 14:36:24 UTC
  • Revision ID: james.westby@ubuntu.com-20110313143624-v2ce1oa59858qiy1
Tags: 1.3.1-1ubuntu2
Backport indicator support from upstream 1.3.x
stable branch (LP: #584669).

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
#
34
34
#
35
35
 
 
36
try:
 
37
    import appindicator
 
38
except ImportError:
 
39
    appindicator = None
36
40
 
37
41
import gtk
38
42
import pkg_resources
63
67
            "separatormenuitem4"
64
68
        ]
65
69
        self.config.register_set_function("enable_system_tray",
66
 
            self.on_enable_system_tray_set)
 
70
                                          self.on_enable_system_tray_set)
67
71
 
68
72
        self.max_download_speed = -1.0
69
73
        self.download_rate = 0.0
77
81
 
78
82
    def enable(self):
79
83
        """Enables the system tray icon."""
80
 
        log.debug("Enabling the system tray icon..")
81
84
        self.tray_glade = gtk.glade.XML(
82
85
            pkg_resources.resource_filename("deluge.ui.gtkui",
83
86
                                            "glade/tray_menu.glade"))
84
87
 
85
 
        if deluge.common.windows_check() or deluge.common.osx_check():
86
 
            self.tray = gtk.status_icon_new_from_pixbuf(
87
 
                common.get_logo(32))
88
 
        else:
89
 
            try:
90
 
                self.tray = gtk.status_icon_new_from_icon_name("deluge")
91
 
            except:
92
 
                log.warning("Update PyGTK to 2.10 or greater for SystemTray..")
93
 
                return
94
 
 
95
 
        self.tray.connect("activate", self.on_tray_clicked)
96
 
        self.tray.connect("popup-menu", self.on_tray_popup)
97
 
 
98
 
 
99
88
        self.tray_glade.signal_autoconnect({
100
89
            "on_menuitem_show_deluge_activate": \
101
90
                self.on_menuitem_show_deluge_activate,
112
101
 
113
102
        self.tray_menu = self.tray_glade.get_widget("tray_menu")
114
103
 
115
 
        self.tray_glade.get_widget("download-limit-image").set_from_file(
116
 
            deluge.common.get_pixmap("downloading16.png"))
117
 
        self.tray_glade.get_widget("upload-limit-image").set_from_file(
118
 
            deluge.common.get_pixmap("seeding16.png"))
 
104
        if appindicator:
 
105
            log.debug("Enabling the Application Indicator..")
 
106
            self.indicator = appindicator.Indicator (
 
107
                "deluge", "deluge", appindicator.CATEGORY_APPLICATION_STATUS)
 
108
            # Pass the menu to the Application Indicator
 
109
            self.indicator.set_menu(self.tray_menu)
 
110
 
 
111
            # Make sure the status of the Show Window MenuItem is correct
 
112
            self._sig_win_hide = self.window.window.connect("hide", self._on_window_hide)
 
113
            self._sig_win_show = self.window.window.connect("show", self._on_window_show)
 
114
            if self.window.visible():
 
115
                self.tray_glade.get_widget("menuitem_show_deluge").set_active(True)
 
116
            else:
 
117
                self.tray_glade.get_widget("menuitem_show_deluge").set_active(False)
 
118
 
 
119
            # Show the Application Indicator
 
120
            self.indicator.set_status(appindicator.STATUS_ACTIVE)
 
121
 
 
122
        else:
 
123
            log.debug("Enabling the system tray icon..")
 
124
            if deluge.common.windows_check() or deluge.common.osx_check():
 
125
                self.tray = gtk.status_icon_new_from_pixbuf(
 
126
                    common.get_logo(32))
 
127
            else:
 
128
                try:
 
129
                    self.tray = gtk.status_icon_new_from_icon_name("deluge")
 
130
                except:
 
131
                    log.warning("Update PyGTK to 2.10 or greater for SystemTray..")
 
132
                    return
 
133
 
 
134
            self.tray.connect("activate", self.on_tray_clicked)
 
135
            self.tray.connect("popup-menu", self.on_tray_popup)
 
136
 
 
137
            # For some reason these icons do not display in appindicator
 
138
            self.tray_glade.get_widget("download-limit-image").set_from_file(
 
139
                deluge.common.get_pixmap("downloading16.png"))
 
140
            self.tray_glade.get_widget("upload-limit-image").set_from_file(
 
141
                deluge.common.get_pixmap("seeding16.png"))
119
142
 
120
143
        client.register_event_handler("ConfigValueChangedEvent", self.config_value_changed)
121
 
        if not client.connected():
122
 
        # Hide menu widgets because we're not connected to a host.
123
 
            for widget in self.hide_widget_list:
124
 
                self.tray_glade.get_widget(widget).hide()
125
 
 
126
144
        if client.connected():
127
145
            # We're connected so we need to get some values from the core
128
146
            self.__start()
 
147
        else:
 
148
            # Hide menu widgets because we're not connected to a host.
 
149
            for widget in self.hide_widget_list:
 
150
                self.tray_glade.get_widget(widget).hide()
129
151
 
130
152
    def __start(self):
131
153
        if self.config["enable_system_tray"]:
136
158
                self.tray_glade.get_widget("menuitem_quitdaemon").hide()
137
159
                self.tray_glade.get_widget("separatormenuitem4").hide()
138
160
 
 
161
            # These do not work with appindicator currently and can crash Deluge.
 
162
            # Related to Launchpad bug #608219
 
163
            if appindicator:
 
164
                self.hide_widget_list.remove("menuitem_download_limit")
 
165
                self.hide_widget_list.remove("menuitem_upload_limit")
 
166
                self.hide_widget_list.remove("separatormenuitem3")
 
167
                self.tray_glade.get_widget("menuitem_download_limit").hide()
 
168
                self.tray_glade.get_widget("menuitem_upload_limit").hide()
 
169
                self.tray_glade.get_widget("separatormenuitem3").hide()
 
170
 
139
171
            # Show widgets in the hide list because we've connected to a host
140
172
            for widget in self.hide_widget_list:
141
173
                self.tray_glade.get_widget(widget).show()
166
198
 
167
199
    def shutdown(self):
168
200
        if self.config["enable_system_tray"]:
169
 
            self.tray.set_visible(False)
 
201
            if appindicator:
 
202
                self.indicator.set_status(appindicator.STATUS_PASSIVE)
 
203
            else:
 
204
                self.tray.set_visible(False)
170
205
 
171
206
    def send_status_request(self):
172
207
        client.core.get_session_status([
198
233
        if not self.config["enable_system_tray"]:
199
234
            return
200
235
 
 
236
        # Tool tip text not available for appindicator
 
237
        if appindicator:
 
238
            return
 
239
 
201
240
        # Set the tool tip text
202
241
        max_download_speed = self.max_download_speed
203
242
        max_upload_speed = self.max_upload_speed
243
282
        submenu_bwdownset.show_all()
244
283
        submenu_bwupset.show_all()
245
284
 
 
285
        # Re-set the menu to partly work around Launchpad bug #608219
 
286
        if appindicator:
 
287
            self.indicator.set_menu(self.tray_menu)
 
288
 
246
289
    def disable(self):
247
 
        """Disables the system tray icon."""
248
 
        log.debug("Disabling the system tray icon..")
 
290
        """Disables the system tray icon or appindicator."""
249
291
        try:
250
 
            self.tray.set_visible(False)
251
 
            del self.tray
 
292
            if appindicator:
 
293
                if hasattr(self, "_sig_win_hide"):
 
294
                    self.window.window.disconnect(self._sig_win_hide)
 
295
                    self.window.window.disconnect(self._sig_win_show)
 
296
                    log.debug("Disabling the application indicator..")
 
297
 
 
298
                self.indicator.set_status(appindicator.STATUS_PASSIVE)
 
299
                del self.indicator
 
300
            else:
 
301
                log.debug("Disabling the system tray icon..")
 
302
                self.tray.set_visible(False)
 
303
                del self.tray
252
304
            del self.tray_glade
253
305
            del self.tray_menu
254
306
        except Exception, e:
336
388
        self.setbwlimit(widget, _("Set Maximum Download Speed"), "max_download_speed",
337
389
            "tray_download_speed_list", self.max_download_speed, "downloading.svg")
338
390
 
 
391
    def _on_window_hide(self, widget, data=None):
 
392
        """_on_window_hide - update the menuitem's status"""
 
393
        log.debug("_on_window_hide")
 
394
        self.tray_glade.get_widget("menuitem_show_deluge").set_active(False)
 
395
 
 
396
    def _on_window_show(self, widget, data=None):
 
397
        """_on_window_show - update the menuitem's status"""
 
398
        log.debug("_on_window_show")
 
399
        self.tray_glade.get_widget("menuitem_show_deluge").set_active(True)
 
400
 
339
401
    def tray_setbwup(self, widget, data=None):
340
402
        self.setbwlimit(widget, _("Set Maximum Upload Speed"), "max_upload_speed",
341
403
            "tray_upload_speed_list", self.max_upload_speed, "seeding.svg")