~rockstar/entertainer/kill-fixmes

« back to all changes in this revision

Viewing changes to entertainerlib/utils/system_tray_icon.py

  • Committer: Paul Hummer
  • Date: 2009-02-07 20:20:19 UTC
  • mfrom: (340.2.6 frontend-cleanup)
  • Revision ID: paul@eventuallyanyway.com-20090207202019-s13v5owgxwwcbaz8
Frontend refactored to include the system tray handler. (Matt Layman)

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
__author__ = "Lauri Taimila <lauri@taimila.com>"
6
6
 
7
7
import os
8
 
import sys
9
8
 
10
 
import gobject
11
9
import gtk
12
10
import gtk.glade
13
 
import pynotify
14
11
 
15
 
from entertainerlib.backend.core.message_bus_proxy import MessageBusProxy
16
 
from entertainerlib.backend.core.message_handler import MessageHandler
17
12
from entertainerlib.utils.log_viewer import LogViewer
18
13
from entertainerlib.utils.preferences_dialog import PreferencesDialog
19
14
from entertainerlib.utils.content_management_dialog import (
20
15
    ContentManagementDialog)
21
16
from entertainerlib.utils.configuration import Configuration
22
 
from entertainerlib.frontend.frontend_client import FrontendClient
23
17
 
24
18
class SystemTrayIcon:
25
19
    """Implements system tray icon for entertainer."""
27
21
    FILE_DIR = os.path.dirname(__file__)
28
22
    GLADE_DIR = os.path.join(FILE_DIR, "glade")
29
23
 
30
 
    # Icon states
31
 
    NORMAL_ICON = 0
32
 
    RECORD_ICON = 1
33
 
 
34
 
    def __init__(self, debug=False, start_frontend=False, frontend=None,
35
 
        notify=True):
 
24
    def __init__(self, quit_callback, toggle_interface_visibility_callback):
36
25
        """
37
26
        Create system tray icon and pop-up menu for it.
38
27
        @author ["Joshua Scotton", "Lauri Taimila"]
39
 
        @param debug If True than debugging inotify messages will be shown
40
 
        @param start_frontend should the frontend be started
41
 
        @param frontend FrontendClient to be associated with this class
42
 
        @param notify Should messages be shown? defaults to True
43
28
        """
44
 
        self.debug = debug
 
29
        self.quit_callback = quit_callback
 
30
        self.toggle_interface_visibility_callback = \
 
31
            toggle_interface_visibility_callback
45
32
        self.config = Configuration()
46
 
        self.popup = None
47
 
        if not (frontend == None):
48
 
            self.frontend = frontend
49
 
            self.frontend_visible = True
50
 
        else:
51
 
            self.frontend = None
52
 
            self.frontend_visible = False
53
 
        self.notify = notify
54
33
 
55
34
        # Path to the tray icon when using a branch
56
35
        self.tray_icon_url = os.path.join(self.FILE_DIR, "..", "..", "icons",
59
38
        self.icon_widget = gtk.StatusIcon()
60
39
        self.icon_widget.set_tooltip(_("Entertainer Server"))
61
40
 
62
 
        # Check if running from a branch to set the tray icon
63
 
        if (os.path.exists(self.tray_icon_url)):
64
 
            self.icon_widget.set_from_file(self.tray_icon_url)
65
 
        else:
66
 
            # Must be running from a package, therefore available by icon name
67
 
            self.icon_widget.set_from_icon_name("entertainer")
68
 
 
69
 
        self.message_handler = SystemTrayMessageHandler(self, debug)
70
 
 
71
41
        # Load glade files
72
42
        self.menu_widgets = gtk.glade.XML(
73
43
            os.path.join(self.GLADE_DIR, "system_tray_icon_menu.glade"))
86
56
                        }
87
57
        self.menu_widgets.signal_autoconnect(callback_dic)
88
58
        self.popup = self.menu_widgets.get_widget("SystemTrayIconMenu")
 
59
 
 
60
        # Check if running from a branch to set the tray icon
 
61
        if (os.path.exists(self.tray_icon_url)):
 
62
            self.icon_widget.set_from_file(self.tray_icon_url)
 
63
        else:
 
64
            # Must be running from a package, therefore available by icon name
 
65
            self.icon_widget.set_from_icon_name("entertainer")
 
66
 
89
67
        self.icon_widget.connect('activate', self.systray_icon_activated)
90
68
        self.icon_widget.connect('popup-menu', self.open_popup_menu)
91
 
        if(start_frontend):
92
 
            self.set_frontend_visible(True)
93
 
 
94
 
    def show_message(self, message,
95
 
                    title=_("Entertainer Media Center"),
96
 
                    length=5, icon="dialog-information",
97
 
                    urgency="LOW"):
98
 
        """
99
 
        Show libnotify message
100
 
        Thanks to http://roscidus.com/desktop/node/336 for info on this
101
 
        @author Joshua Scotton
102
 
        """
103
 
        if self.notify:
104
 
            if pynotify.init("Entertainer Media Center"):
105
 
                notifier = pynotify.Notification(title, message, icon)
106
 
                if urgency == "CRITICAL":
107
 
                    notifier.set_urgency(pynotify.URGENCY_CRITICAL)
108
 
                elif urgency == "NORMAL":
109
 
                    notifier.set_urgency(pynotify.URGENCY_NORMAL)
110
 
                else:
111
 
                    notifier.set_urgency(pynotify.URGENCY_LOW)
112
 
                notifier.set_timeout(length*1000)
113
 
                notifier.attach_to_status_icon(self.icon_widget)
114
 
                notifier.show()
115
 
            else:
116
 
                raise Exception("Problem with pynotify")
117
 
        else:
118
 
            print message
119
 
 
120
 
    def show_icon(self):
121
 
        """Show icon in system tray"""
122
 
        self.icon_widget.set_visible(True)
123
 
 
124
 
    def hide_icon(self):
125
 
        """Hide icon from system tray"""
126
 
        self.icon_widget.set_visible(False)
127
69
 
128
70
    def systray_icon_activated(self, widget, data= None):
129
71
        """Switch visibility of frontend when system tray icon is clicked"""
130
 
        self.switch_frontend_visibility()
 
72
        self.toggle_interface_visibility_callback()
131
73
 
132
74
    def open_popup_menu(self, widget, button, time, data = None):
133
75
        """Display pop-up menu when system tray icon is clicked"""
140
82
 
141
83
    def on_menuitem_preferences_activate(self, widget):
142
84
        """Executes preferences-tool."""
143
 
        if self.debug:
144
 
            self.show_message("Starting preferences...")
145
85
        PreferencesDialog(False)
146
86
 
147
87
    def on_menuitem_content_management_activate(self, widget):
148
88
        """Executes content-management-tool"""
149
 
        if self.debug:
150
 
            self.show_message("Starting content management...")
151
89
        ContentManagementDialog(False)
152
90
 
153
91
    def on_menuitem_log_viewer_activate(self, widget):
154
92
        """Display log viewer dialog"""
155
 
        if self.debug:
156
 
            self.show_message("Starting log viewer...")
157
93
        LogViewer(False)
158
94
 
159
95
    def on_menuitem_quit_activate(self, widget):
160
 
        """Show confirmation dialog and quit backend"""
161
 
        #FIXME Doesn't work yet hangs on threads_enter so commented out
162
 
        self.get_message_bus_proxy().disconnectFromMessageBus()
163
 
        if self.debug:
164
 
            self.show_message("Quitting...")
165
 
 
166
 
        if(not self.frontend == None):
167
 
            self.frontend.quit_frontend()
168
 
        #gtk.gdk.threads_enter()
169
 
        gtk.main_quit()
170
 
        #gtk.gdk.threads_leave()
171
 
        sys.exit()
172
 
 
173
 
    def set_message_bus_proxy(self, proxy):
174
 
        """
175
 
        This sets the MessageBusProxy object for the SystemTrayMessageHandler
176
 
        @author Joshua Scotton
177
 
        """
178
 
        self.get_message_handler().set_message_bus_proxy(proxy)
179
 
 
180
 
    def get_message_bus_proxy(self):
181
 
        """
182
 
        This gets the MessageBusProxy object from the SystemTrayMessageHandler
183
 
        @author Joshua Scotton
184
 
        """
185
 
        return self.get_message_handler().get_message_bus_proxy()
186
 
 
187
 
    def set_message_handler(self, message_handler):
188
 
        """
189
 
        This sets the associated SystemTrayMessageHandler
190
 
        @author Joshua Scotton
191
 
        """
192
 
        self.message_handler = message_handler
193
 
 
194
 
    def get_message_handler(self):
195
 
        """
196
 
        This gets the associated SystemTrayMessageHandler
197
 
        @author Joshua Scotton
198
 
        """
199
 
        return self.message_handler
200
 
 
201
 
    def set_frontend_visible(self, show=True):
202
 
        """
203
 
        Set the frontend to visible
204
 
        @author Joshua Scotton
205
 
        """
206
 
        if(show):
207
 
            if(self.frontend == None):
208
 
                if self.debug:
209
 
                    self.show_message("Starting Frontend...")
210
 
                self.frontend = FrontendClient(True)
211
 
            else:
212
 
                if self.debug:
213
 
                    self.show_message("Showing Frontend...")
214
 
                self.frontend.ui.get_stage().show()
215
 
            self.frontend_visible = True
216
 
        elif(not show and not self.frontend == None):
217
 
            if self.debug:
218
 
                self.show_message("Hiding Frontend...")
219
 
            self.frontend.ui.get_stage().hide()
220
 
            self.frontend_visible = False
221
 
 
222
 
    def get_frontend_visible(self):
223
 
        """
224
 
        Return the visibility of the frontend
225
 
        @author Joshua Scotton
226
 
        """
227
 
        return self.frontend_visible
228
 
 
229
 
    def switch_frontend_visibility(self):
230
 
        """
231
 
        Switches the visibility of the frontend
232
 
        @author Joshua Scotton
233
 
        """
234
 
        if(self.get_frontend_visible()==False):
235
 
            self.set_frontend_visible(True)
236
 
        else:
237
 
            self.set_frontend_visible(False)
238
 
 
239
 
class SystemTrayMessageHandler(MessageHandler):
240
 
    """
241
 
    This handles the messages which are intended for the system tray icon
242
 
    @author Joshua Scotton
243
 
    """
244
 
 
245
 
    def __init__(self, tray_icon, debug=False):
246
 
        """
247
 
        This sets up the SystemTrayMessageHandler object
248
 
        """
249
 
        MessageHandler.__init__(self)
250
 
        self.debug = debug
251
 
        self.tray_icon = tray_icon
252
 
        self.message_bus_proxy = None
253
 
 
254
 
    def set_tray_icon(self, tray_icon):
255
 
        """
256
 
        This sets the SystemTrayIcon object associated with this SystemTrayMessageHandler
257
 
        """
258
 
        self.tray_icon = tray_icon
259
 
    def get_tray_icon(self):
260
 
        """
261
 
        Gets the SystemTrayIcon for this object
262
 
        """
263
 
        return self.tray_icon
264
 
    def set_message_bus_proxy(self, proxy):
265
 
        """
266
 
        This sets the MessageBusProxy for this object
267
 
        """
268
 
        self.message_bus_proxy = proxy
269
 
    def get_message_bus_proxy(self):
270
 
        """
271
 
        This gets the MessageBusProxy for this object
272
 
        """
273
 
        return self.message_bus_proxy
274
 
    def set_debug(self, debug):
275
 
        """
276
 
        This sets debugging messages to True or False
277
 
        """
278
 
        self.debug = debug
279
 
    def get_debug(self):
280
 
        """
281
 
        Returns whether debugging messages should be shown
282
 
        """
283
 
        return self.debug
284
 
 
285
 
    # Implement MessageHandler interface
286
 
 
287
 
    def handleMessage(self, message):
288
 
        """Handles messages for the SystemTrayIcon"""
289
 
        pass
290
 
 
291
 
def init_systray():
292
 
    '''Start the systray tasks.'''
293
 
 
294
 
    config = Configuration()
295
 
    message_dict = {}
296
 
 
297
 
    gobject.threads_init()
298
 
    gtk.gdk.threads_init()
299
 
 
300
 
    # Create tray icon
301
 
    gtk.gdk.threads_enter()
302
 
 
303
 
    #Start SystemTrayIcon with show on start True
304
 
    notify = config.is_notifications_enabled()
305
 
    tray_icon = SystemTrayIcon(start_frontend=True, notify=notify)
306
 
    gtk.gdk.threads_leave()
307
 
 
308
 
    try:
309
 
        # Open connection to backend
310
 
        proxy = MessageBusProxy(message_dict, tray_icon.get_message_handler(),
311
 
            "system-tray-icon")
312
 
        proxy.connectToMessageBus()
313
 
        proxy.start()
314
 
        tray_icon.get_message_handler().set_message_bus_proxy(proxy)
315
 
 
316
 
    except:
317
 
        print "Backend is not running. Start backend and try again."
318
 
        sys.exit(1)
319
 
 
320
 
    gtk.gdk.threads_enter()
321
 
    gtk.main()
322
 
    gtk.gdk.threads_leave()
 
96
        '''Close the application by calling the quit callback.'''
 
97
        self.quit_callback()
323
98