27
21
FILE_DIR = os.path.dirname(__file__)
28
22
GLADE_DIR = os.path.join(FILE_DIR, "glade")
34
def __init__(self, debug=False, start_frontend=False, frontend=None,
24
def __init__(self, quit_callback, toggle_interface_visibility_callback):
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
29
self.quit_callback = quit_callback
30
self.toggle_interface_visibility_callback = \
31
toggle_interface_visibility_callback
45
32
self.config = Configuration()
47
if not (frontend == None):
48
self.frontend = frontend
49
self.frontend_visible = True
52
self.frontend_visible = False
55
34
# Path to the tray icon when using a branch
56
35
self.tray_icon_url = os.path.join(self.FILE_DIR, "..", "..", "icons",
87
57
self.menu_widgets.signal_autoconnect(callback_dic)
88
58
self.popup = self.menu_widgets.get_widget("SystemTrayIconMenu")
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)
64
# Must be running from a package, therefore available by icon name
65
self.icon_widget.set_from_icon_name("entertainer")
89
67
self.icon_widget.connect('activate', self.systray_icon_activated)
90
68
self.icon_widget.connect('popup-menu', self.open_popup_menu)
92
self.set_frontend_visible(True)
94
def show_message(self, message,
95
title=_("Entertainer Media Center"),
96
length=5, icon="dialog-information",
99
Show libnotify message
100
Thanks to http://roscidus.com/desktop/node/336 for info on this
101
@author Joshua Scotton
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)
111
notifier.set_urgency(pynotify.URGENCY_LOW)
112
notifier.set_timeout(length*1000)
113
notifier.attach_to_status_icon(self.icon_widget)
116
raise Exception("Problem with pynotify")
121
"""Show icon in system tray"""
122
self.icon_widget.set_visible(True)
125
"""Hide icon from system tray"""
126
self.icon_widget.set_visible(False)
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()
132
74
def open_popup_menu(self, widget, button, time, data = None):
133
75
"""Display pop-up menu when system tray icon is clicked"""
141
83
def on_menuitem_preferences_activate(self, widget):
142
84
"""Executes preferences-tool."""
144
self.show_message("Starting preferences...")
145
85
PreferencesDialog(False)
147
87
def on_menuitem_content_management_activate(self, widget):
148
88
"""Executes content-management-tool"""
150
self.show_message("Starting content management...")
151
89
ContentManagementDialog(False)
153
91
def on_menuitem_log_viewer_activate(self, widget):
154
92
"""Display log viewer dialog"""
156
self.show_message("Starting log viewer...")
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()
164
self.show_message("Quitting...")
166
if(not self.frontend == None):
167
self.frontend.quit_frontend()
168
#gtk.gdk.threads_enter()
170
#gtk.gdk.threads_leave()
173
def set_message_bus_proxy(self, proxy):
175
This sets the MessageBusProxy object for the SystemTrayMessageHandler
176
@author Joshua Scotton
178
self.get_message_handler().set_message_bus_proxy(proxy)
180
def get_message_bus_proxy(self):
182
This gets the MessageBusProxy object from the SystemTrayMessageHandler
183
@author Joshua Scotton
185
return self.get_message_handler().get_message_bus_proxy()
187
def set_message_handler(self, message_handler):
189
This sets the associated SystemTrayMessageHandler
190
@author Joshua Scotton
192
self.message_handler = message_handler
194
def get_message_handler(self):
196
This gets the associated SystemTrayMessageHandler
197
@author Joshua Scotton
199
return self.message_handler
201
def set_frontend_visible(self, show=True):
203
Set the frontend to visible
204
@author Joshua Scotton
207
if(self.frontend == None):
209
self.show_message("Starting Frontend...")
210
self.frontend = FrontendClient(True)
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):
218
self.show_message("Hiding Frontend...")
219
self.frontend.ui.get_stage().hide()
220
self.frontend_visible = False
222
def get_frontend_visible(self):
224
Return the visibility of the frontend
225
@author Joshua Scotton
227
return self.frontend_visible
229
def switch_frontend_visibility(self):
231
Switches the visibility of the frontend
232
@author Joshua Scotton
234
if(self.get_frontend_visible()==False):
235
self.set_frontend_visible(True)
237
self.set_frontend_visible(False)
239
class SystemTrayMessageHandler(MessageHandler):
241
This handles the messages which are intended for the system tray icon
242
@author Joshua Scotton
245
def __init__(self, tray_icon, debug=False):
247
This sets up the SystemTrayMessageHandler object
249
MessageHandler.__init__(self)
251
self.tray_icon = tray_icon
252
self.message_bus_proxy = None
254
def set_tray_icon(self, tray_icon):
256
This sets the SystemTrayIcon object associated with this SystemTrayMessageHandler
258
self.tray_icon = tray_icon
259
def get_tray_icon(self):
261
Gets the SystemTrayIcon for this object
263
return self.tray_icon
264
def set_message_bus_proxy(self, proxy):
266
This sets the MessageBusProxy for this object
268
self.message_bus_proxy = proxy
269
def get_message_bus_proxy(self):
271
This gets the MessageBusProxy for this object
273
return self.message_bus_proxy
274
def set_debug(self, debug):
276
This sets debugging messages to True or False
281
Returns whether debugging messages should be shown
285
# Implement MessageHandler interface
287
def handleMessage(self, message):
288
"""Handles messages for the SystemTrayIcon"""
292
'''Start the systray tasks.'''
294
config = Configuration()
297
gobject.threads_init()
298
gtk.gdk.threads_init()
301
gtk.gdk.threads_enter()
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()
309
# Open connection to backend
310
proxy = MessageBusProxy(message_dict, tray_icon.get_message_handler(),
312
proxy.connectToMessageBus()
314
tray_icon.get_message_handler().set_message_bus_proxy(proxy)
317
print "Backend is not running. Start backend and try again."
320
gtk.gdk.threads_enter()
322
gtk.gdk.threads_leave()
96
'''Close the application by calling the quit callback.'''