use webbrowser module to open uri instead of using popen. Fixes #5751
[gajim.git] / src / notify.py
blob888c2fbde94a60bce94553534208b7768dee2f92
1 # -*- coding:utf-8 -*-
2 ## src/notify.py
3 ##
4 ## Copyright (C) 2005 Sebastian Estienne
5 ## Copyright (C) 2005-2006 Andrew Sayman <lorien420 AT myrealbox.com>
6 ## Copyright (C) 2005-2007 Nikos Kouremenos <kourem AT gmail.com>
7 ## Copyright (C) 2005-2010 Yann Leboulanger <asterix AT lagaule.org>
8 ## Copyright (C) 2006 Travis Shirk <travis AT pobox.com>
9 ## Copyright (C) 2006-2008 Jean-Marie Traissard <jim AT lapin.org>
10 ## Copyright (C) 2007 Julien Pivotto <roidelapluie AT gmail.com>
11 ## Stephan Erb <steve-e AT h3c.de>
12 ## Copyright (C) 2008 Brendan Taylor <whateley AT gmail.com>
13 ## Jonathan Schleifer <js-gajim AT webkeks.org>
15 ## This file is part of Gajim.
17 ## Gajim is free software; you can redistribute it and/or modify
18 ## it under the terms of the GNU General Public License as published
19 ## by the Free Software Foundation; version 3 only.
21 ## Gajim is distributed in the hope that it will be useful,
22 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ## GNU General Public License for more details.
26 ## You should have received a copy of the GNU General Public License
27 ## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
30 import os
31 import time
32 import dialogs
33 import gobject
34 import gtkgui_helpers
35 import gtk
37 from common import gajim
38 from common import helpers
40 from common import dbus_support
41 if dbus_support.supported:
42 import dbus
43 import dbus.glib
46 USER_HAS_PYNOTIFY = True # user has pynotify module
47 try:
48 import pynotify
49 pynotify.init('Gajim Notification')
50 except ImportError:
51 USER_HAS_PYNOTIFY = False
53 def get_show_in_roster(event, account, contact, session=None):
54 """
55 Return True if this event must be shown in roster, else False
56 """
57 if event == 'gc_message_received':
58 return True
59 num = get_advanced_notification(event, account, contact)
60 if num is not None:
61 if gajim.config.get_per('notifications', str(num), 'roster') == 'yes':
62 return True
63 if gajim.config.get_per('notifications', str(num), 'roster') == 'no':
64 return False
65 if event == 'message_received':
66 if session and session.control:
67 return False
68 return True
70 def get_show_in_systray(event, account, contact, type_=None):
71 """
72 Return True if this event must be shown in systray, else False
73 """
74 num = get_advanced_notification(event, account, contact)
75 if num is not None:
76 if gajim.config.get_per('notifications', str(num), 'systray') == 'yes':
77 return True
78 if gajim.config.get_per('notifications', str(num), 'systray') == 'no':
79 return False
80 if type_ == 'printed_gc_msg' and not gajim.config.get(
81 'notify_on_all_muc_messages'):
82 # it's not an highlighted message, don't show in systray
83 return False
84 return gajim.config.get('trayicon_notification_on_events')
86 def get_advanced_notification(event, account, contact):
87 """
88 Returns the number of the first (top most) advanced notification else None
89 """
90 num = 0
91 notif = gajim.config.get_per('notifications', str(num))
92 while notif:
93 recipient_ok = False
94 status_ok = False
95 tab_opened_ok = False
96 # test event
97 if gajim.config.get_per('notifications', str(num), 'event') == event:
98 # test recipient
99 recipient_type = gajim.config.get_per('notifications', str(num),
100 'recipient_type')
101 recipients = gajim.config.get_per('notifications', str(num),
102 'recipients').split()
103 if recipient_type == 'all':
104 recipient_ok = True
105 elif recipient_type == 'contact' and contact.jid in recipients:
106 recipient_ok = True
107 elif recipient_type == 'group':
108 for group in contact.groups:
109 if group in contact.groups:
110 recipient_ok = True
111 break
112 if recipient_ok:
113 # test status
114 our_status = gajim.SHOW_LIST[gajim.connections[account].connected]
115 status = gajim.config.get_per('notifications', str(num), 'status')
116 if status == 'all' or our_status in status.split():
117 status_ok = True
118 if status_ok:
119 # test window_opened
120 tab_opened = gajim.config.get_per('notifications', str(num),
121 'tab_opened')
122 if tab_opened == 'both':
123 tab_opened_ok = True
124 else:
125 chat_control = helpers.get_chat_control(account, contact)
126 if (chat_control and tab_opened == 'yes') or (not chat_control \
127 and tab_opened == 'no'):
128 tab_opened_ok = True
129 if tab_opened_ok:
130 return num
132 num += 1
133 notif = gajim.config.get_per('notifications', str(num))
135 def notify(event, jid, account, parameters, advanced_notif_num=None):
137 Check what type of notifications we want, depending on basic and the
138 advanced configuration of notifications and do these notifications;
139 advanced_notif_num holds the number of the first (top most) advanced
140 notification
142 # First, find what notifications we want
143 do_popup = False
144 do_sound = False
145 do_cmd = False
146 if event == 'status_change':
147 new_show = parameters[0]
148 status_message = parameters[1]
149 # Default: No popup for status change
150 elif event == 'contact_connected':
151 status_message = parameters
152 j = gajim.get_jid_without_resource(jid)
153 server = gajim.get_server_from_jid(j)
154 account_server = account + '/' + server
155 block_transport = False
156 if account_server in gajim.block_signed_in_notifications and \
157 gajim.block_signed_in_notifications[account_server]:
158 block_transport = True
159 if helpers.allow_showing_notification(account, 'notify_on_signin') and \
160 not gajim.block_signed_in_notifications[account] and \
161 not block_transport:
162 do_popup = True
163 if gajim.config.get_per('soundevents', 'contact_connected',
164 'enabled') and not gajim.block_signed_in_notifications[account] and \
165 not block_transport and helpers.allow_sound_notification(account,
166 event, advanced_notif_num):
167 do_sound = True
168 elif event == 'contact_disconnected':
169 status_message = parameters
170 if helpers.allow_showing_notification(account, 'notify_on_signout'):
171 do_popup = True
172 if gajim.config.get_per('soundevents', 'contact_disconnected',
173 'enabled') and helpers.allow_sound_notification(account,
174 event, advanced_notif_num):
175 do_sound = True
176 elif event == 'new_message':
177 message_type = parameters[0]
178 is_first_message = parameters[1]
179 nickname = parameters[2]
180 if gajim.config.get('notification_preview_message'):
181 message = parameters[3]
182 if message.startswith('/me ') or message.startswith('/me\n'):
183 message = '* ' + nickname + message[3:]
184 else:
185 # We don't want message preview, do_preview = False
186 message = ''
187 focused = parameters[4]
188 if helpers.allow_showing_notification(account, 'notify_on_new_message',
189 advanced_notif_num, is_first_message):
190 do_popup = True
191 if is_first_message and helpers.allow_sound_notification(account,
192 'first_message_received', advanced_notif_num):
193 do_sound = True
194 elif not is_first_message and focused and \
195 helpers.allow_sound_notification(account,
196 'next_message_received_focused', advanced_notif_num):
197 do_sound = True
198 elif not is_first_message and not focused and \
199 helpers.allow_sound_notification(account,
200 'next_message_received_unfocused', advanced_notif_num):
201 do_sound = True
202 else:
203 print '*Event not implemeted yet*'
205 if advanced_notif_num is not None and gajim.config.get_per('notifications',
206 str(advanced_notif_num), 'run_command'):
207 do_cmd = True
209 # Do the wanted notifications
210 if do_popup:
211 if event in ('contact_connected', 'contact_disconnected',
212 'status_change'): # Common code for popup for these three events
213 if event == 'contact_disconnected':
214 show_image = 'offline.png'
215 suffix = '_notif_size_bw'
216 else: # Status Change or Connected
217 # FIXME: for status change,
218 # we don't always 'online.png', but we
219 # first need 48x48 for all status
220 show_image = 'online.png'
221 suffix = '_notif_size_colored'
222 transport_name = gajim.get_transport_name_from_jid(jid)
223 img_path = None
224 if transport_name:
225 img_path = os.path.join(helpers.get_transport_path(
226 transport_name), '48x48', show_image)
227 if not img_path or not os.path.isfile(img_path):
228 iconset = gajim.config.get('iconset')
229 img_path = os.path.join(helpers.get_iconset_path(iconset),
230 '48x48', show_image)
231 path = gtkgui_helpers.get_path_to_generic_or_avatar(img_path,
232 jid=jid, suffix=suffix)
233 if event == 'status_change':
234 title = _('%(nick)s Changed Status') % \
235 {'nick': gajim.get_name_from_jid(account, jid)}
236 text = _('%(nick)s is now %(status)s') % \
237 {'nick': gajim.get_name_from_jid(account, jid),\
238 'status': helpers.get_uf_show(gajim.SHOW_LIST[new_show])}
239 if status_message:
240 text = text + " : " + status_message
241 popup(_('Contact Changed Status'), jid, account,
242 path_to_image=path, title=title, text=text)
243 elif event == 'contact_connected':
244 title = _('%(nickname)s Signed In') % \
245 {'nickname': gajim.get_name_from_jid(account, jid)}
246 text = ''
247 if status_message:
248 text = status_message
249 popup(_('Contact Signed In'), jid, account,
250 path_to_image=path, title=title, text=text)
251 elif event == 'contact_disconnected':
252 title = _('%(nickname)s Signed Out') % \
253 {'nickname': gajim.get_name_from_jid(account, jid)}
254 text = ''
255 if status_message:
256 text = status_message
257 popup(_('Contact Signed Out'), jid, account,
258 path_to_image=path, title=title, text=text)
259 elif event == 'new_message':
260 if message_type == 'normal': # single message
261 event_type = _('New Single Message')
262 img_name = 'gajim-single_msg_recv'
263 title = _('New Single Message from %(nickname)s') % \
264 {'nickname': nickname}
265 text = message
266 elif message_type == 'pm': # private message
267 event_type = _('New Private Message')
268 room_name = gajim.get_nick_from_jid(jid)
269 img_name = 'gajim-priv_msg_recv'
270 title = _('New Private Message from group chat %s') % room_name
271 if message:
272 text = _('%(nickname)s: %(message)s') % \
273 {'nickname': nickname, 'message': message}
274 else:
275 text = _('Messaged by %(nickname)s') % \
276 {'nickname': nickname}
278 else: # chat message
279 event_type = _('New Message')
280 img_name = 'gajim-chat_msg_recv'
281 title = _('New Message from %(nickname)s') % \
282 {'nickname': nickname}
283 text = message
284 img_path = gtkgui_helpers.get_icon_path(img_name, 48)
285 popup(event_type, jid, account, message_type,
286 path_to_image=img_path, title=title, text=text)
288 if do_sound:
289 snd_file = None
290 snd_event = None # If not snd_file, play the event
291 if event == 'new_message':
292 if advanced_notif_num is not None and gajim.config.get_per(
293 'notifications', str(advanced_notif_num), 'sound') == 'yes':
294 snd_file = gajim.config.get_per('notifications',
295 str(advanced_notif_num), 'sound_file')
296 elif advanced_notif_num is not None and gajim.config.get_per(
297 'notifications', str(advanced_notif_num), 'sound') == 'no':
298 pass # do not set snd_event
299 elif is_first_message:
300 snd_event = 'first_message_received'
301 elif focused:
302 snd_event = 'next_message_received_focused'
303 else:
304 snd_event = 'next_message_received_unfocused'
305 elif event in ('contact_connected', 'contact_disconnected'):
306 snd_event = event
307 if snd_file:
308 helpers.play_sound_file(snd_file)
309 if snd_event:
310 helpers.play_sound(snd_event)
312 if do_cmd:
313 command = gajim.config.get_per('notifications', str(advanced_notif_num),
314 'command')
315 try:
316 helpers.exec_command(command)
317 except Exception:
318 pass
320 def popup(event_type, jid, account, msg_type='', path_to_image=None, title=None,
321 text=None):
323 Notify a user of an event. It first tries to a valid implementation of
324 the Desktop Notification Specification. If that fails, then we fall back to
325 the older style PopupNotificationWindow method
327 # default image
328 if not path_to_image:
329 path_to_image = gtkgui_helpers.get_icon_path('gajim-chat_msg_recv', 48)
331 # Try to show our popup via D-Bus and notification daemon
332 if gajim.config.get('use_notif_daemon') and dbus_support.supported:
333 try:
334 DesktopNotification(event_type, jid, account, msg_type,
335 path_to_image, title, gobject.markup_escape_text(text))
336 return # sucessfully did D-Bus Notification procedure!
337 except dbus.DBusException, e:
338 # Connection to D-Bus failed
339 gajim.log.debug(str(e))
340 except TypeError, e:
341 # This means that we sent the message incorrectly
342 gajim.log.debug(str(e))
344 # Ok, that failed. Let's try pynotify, which also uses notification daemon
345 if gajim.config.get('use_notif_daemon') and USER_HAS_PYNOTIFY:
346 if not text and event_type == 'new_message':
347 # empty text for new_message means do_preview = False
348 # -> default value for text
349 _text = gobject.markup_escape_text(
350 gajim.get_name_from_jid(account, jid))
351 else:
352 _text = gobject.markup_escape_text(text)
354 if not title:
355 _title = ''
356 else:
357 _title = title
359 notification = pynotify.Notification(_title, _text)
360 timeout = gajim.config.get('notification_timeout') * 1000 # make it ms
361 notification.set_timeout(timeout)
363 notification.set_category(event_type)
364 notification.set_data('event_type', event_type)
365 notification.set_data('jid', jid)
366 notification.set_data('account', account)
367 notification.set_data('msg_type', msg_type)
368 notification.set_property('icon-name', path_to_image)
369 if 'actions' in pynotify.get_server_caps():
370 notification.add_action('default', 'Default Action',
371 on_pynotify_notification_clicked)
373 try:
374 notification.show()
375 return
376 except gobject.GError, e:
377 # Connection to notification-daemon failed, see #2893
378 gajim.log.debug(str(e))
380 # Either nothing succeeded or the user wants old-style notifications
381 instance = dialogs.PopupNotificationWindow(event_type, jid, account,
382 msg_type, path_to_image, title, text)
383 gajim.interface.roster.popup_notification_windows.append(instance)
385 def on_pynotify_notification_clicked(notification, action):
386 jid = notification.get_data('jid')
387 account = notification.get_data('account')
388 msg_type = notification.get_data('msg_type')
390 notification.close()
391 gajim.interface.handle_event(account, jid, msg_type)
393 class NotificationResponseManager:
395 Collect references to pending DesktopNotifications and manages there
396 signalling. This is necessary due to a bug in DBus where you can't remove a
397 signal from an interface once it's connected
400 def __init__(self):
401 self.pending = {}
402 self.received = []
403 self.interface = None
405 def attach_to_interface(self):
406 if self.interface is not None:
407 return
408 self.interface = dbus_support.get_notifications_interface()
409 self.interface.connect_to_signal('ActionInvoked',
410 self.on_action_invoked)
411 self.interface.connect_to_signal('NotificationClosed', self.on_closed)
413 def on_action_invoked(self, id_, reason):
414 self.received.append((id_, time.time(), reason))
415 if id_ in self.pending:
416 notification = self.pending[id_]
417 notification.on_action_invoked(id_, reason)
418 del self.pending[id_]
419 if len(self.received) > 20:
420 curt = time.time()
421 for rec in self.received:
422 diff = curt - rec[1]
423 if diff > 10:
424 self.received.remove(rec)
426 def on_closed(self, id_, reason=None):
427 if id_ in self.pending:
428 del self.pending[id_]
430 def add_pending(self, id_, object_):
431 # Check to make sure that we handle an event immediately if we're adding
432 # an id that's already been triggered
433 for rec in self.received:
434 if rec[0] == id_:
435 object_.on_action_invoked(id_, rec[2])
436 self.received.remove(rec)
437 return
438 if id_ not in self.pending:
439 # Add it
440 self.pending[id_] = object_
441 else:
442 # We've triggered an event that has a duplicate ID!
443 gajim.log.debug('Duplicate ID of notification. Can\'t handle this.')
445 notification_response_manager = NotificationResponseManager()
447 class DesktopNotification:
449 A DesktopNotification that interfaces with D-Bus via the Desktop
450 Notification Specification
453 def __init__(self, event_type, jid, account, msg_type='',
454 path_to_image=None, title=None, text=None):
455 self.path_to_image = path_to_image
456 self.event_type = event_type
457 self.title = title
458 self.text = text
459 # 0.3.1 is the only version of notification daemon that has no way
460 # to determine which version it is. If no method exists, it means
461 # they're using that one.
462 self.default_version = [0, 3, 1]
463 self.account = account
464 self.jid = jid
465 self.msg_type = msg_type
467 # default value of text
468 if not text and event_type == 'new_message':
469 # empty text for new_message means do_preview = False
470 self.text = gajim.get_name_from_jid(account, jid)
472 if not title:
473 self.title = event_type # default value
475 if event_type == _('Contact Signed In'):
476 ntype = 'presence.online'
477 elif event_type == _('Contact Signed Out'):
478 ntype = 'presence.offline'
479 elif event_type in (_('New Message'), _('New Single Message'),
480 _('New Private Message')):
481 ntype = 'im.received'
482 elif event_type == _('File Transfer Request'):
483 ntype = 'transfer'
484 elif event_type == _('File Transfer Error'):
485 ntype = 'transfer.error'
486 elif event_type in (_('File Transfer Completed'),
487 _('File Transfer Stopped')):
488 ntype = 'transfer.complete'
489 elif event_type == _('New E-mail'):
490 ntype = 'email.arrived'
491 elif event_type == _('Groupchat Invitation'):
492 ntype = 'im.invitation'
493 elif event_type == _('Contact Changed Status'):
494 ntype = 'presence.status'
495 elif event_type == _('Connection Failed'):
496 ntype = 'connection.failed'
497 elif event_type == _('Subscription request'):
498 ntype = 'subscription.request'
499 elif event_type == _('Unsubscribed'):
500 ntype = 'unsubscribed'
501 else:
502 # default failsafe values
503 self.path_to_image = gtkgui_helpers.get_icon_path(
504 'gajim-chat_msg_recv', 48)
505 ntype = 'im' # Notification Type
507 self.notif = dbus_support.get_notifications_interface(self)
508 if self.notif is None:
509 raise dbus.DBusException('unable to get notifications interface')
510 self.ntype = ntype
512 if self.kde_notifications:
513 self.attempt_notify()
514 else:
515 self.capabilities = self.notif.GetCapabilities()
516 if self.capabilities is None:
517 self.capabilities = ['actions']
518 self.get_version()
520 def attempt_notify(self):
521 timeout = gajim.config.get('notification_timeout') # in seconds
522 ntype = self.ntype
523 if self.kde_notifications:
524 notification_text = ('<html><img src="%(image)s" align=left />' \
525 '%(title)s<br/>%(text)s</html>') % {'title': self.title,
526 'text': self.text, 'image': self.path_to_image}
527 gajim_icon = gtkgui_helpers.get_icon_path('gajim', 48)
528 try:
529 self.notif.Notify(
530 dbus.String(_('Gajim')), # app_name (string)
531 dbus.UInt32(0), # replaces_id (uint)
532 ntype, # event_id (string)
533 dbus.String(gajim_icon), # app_icon (string)
534 dbus.String(''), # summary (string)
535 dbus.String(notification_text), # body (string)
536 # actions (stringlist)
537 (dbus.String('default'), dbus.String(self.event_type),
538 dbus.String('ignore'), dbus.String(_('Ignore'))),
539 [], # hints (not used in KDE yet)
540 dbus.UInt32(timeout*1000), # timeout (int), in ms
541 reply_handler=self.attach_by_id,
542 error_handler=self.notify_another_way)
543 return
544 except Exception:
545 pass
546 version = self.version
547 if version[:2] == [0, 2]:
548 actions = {}
549 if 'actions' in self.capabilities:
550 actions = {'default': 0}
551 try:
552 self.notif.Notify(
553 dbus.String(_('Gajim')),
554 dbus.String(self.path_to_image),
555 dbus.UInt32(0),
556 ntype,
557 dbus.Byte(0),
558 dbus.String(self.title),
559 dbus.String(self.text),
560 [dbus.String(self.path_to_image)],
561 actions,
562 [''],
563 True,
564 dbus.UInt32(timeout),
565 reply_handler=self.attach_by_id,
566 error_handler=self.notify_another_way)
567 except AttributeError:
568 # we're actually dealing with the newer version
569 version = [0, 3, 1]
570 if version > [0, 3]:
571 if gajim.interface.systray_enabled and \
572 gajim.config.get('attach_notifications_to_systray'):
573 status_icon = gajim.interface.systray.status_icon
574 x, y, width, height = status_icon.get_geometry()[1]
575 pos_x = x + (width / 2)
576 pos_y = y + (height / 2)
577 hints = {'x': pos_x, 'y': pos_y}
578 else:
579 hints = {}
580 if version >= [0, 3, 2]:
581 hints['urgency'] = dbus.Byte(0) # Low Urgency
582 hints['category'] = dbus.String(ntype)
583 # it seems notification-daemon doesn't like empty text
584 if self.text:
585 text = self.text
586 else:
587 text = ' '
588 if os.environ.get('KDE_FULL_SESSION') == 'true':
589 self.path_to_image = os.path.abspath(self.path_to_image)
590 text = '<table style=\'padding: 3px\'><tr><td>' \
591 '<img src=\"%s\"></td><td width=20> </td>' \
592 '<td>%s</td></tr></table>' % (self.path_to_image,
593 text)
594 self.path_to_image = os.path.abspath(
595 gtkgui_helpers.get_icon_path('gajim', 48))
596 actions = ()
597 if 'actions' in self.capabilities:
598 actions = (dbus.String('default'), dbus.String(
599 self.event_type))
600 try:
601 self.notif.Notify(
602 dbus.String(_('Gajim')),
603 # this notification does not replace other
604 dbus.UInt32(0),
605 dbus.String(self.path_to_image),
606 dbus.String(self.title),
607 dbus.String(text),
608 actions,
609 hints,
610 dbus.UInt32(timeout*1000),
611 reply_handler=self.attach_by_id,
612 error_handler=self.notify_another_way)
613 except Exception, e:
614 self.notify_another_way(e)
615 else:
616 try:
617 self.notif.Notify(
618 dbus.String(_('Gajim')),
619 dbus.String(self.path_to_image),
620 dbus.UInt32(0),
621 dbus.String(self.title),
622 dbus.String(self.text),
623 dbus.String(''),
624 hints,
625 dbus.UInt32(timeout*1000),
626 reply_handler=self.attach_by_id,
627 error_handler=self.notify_another_way)
628 except Exception, e:
629 self.notify_another_way(e)
631 def attach_by_id(self, id_):
632 self.id = id_
633 notification_response_manager.attach_to_interface()
634 notification_response_manager.add_pending(self.id, self)
636 def notify_another_way(self, e):
637 gajim.log.debug('Error when trying to use notification daemon: %s' % \
638 str(e))
639 instance = dialogs.PopupNotificationWindow(self.event_type, self.jid,
640 self.account, self.msg_type, self.path_to_image, self.title,
641 self.text)
642 gajim.interface.roster.popup_notification_windows.append(instance)
644 def on_action_invoked(self, id_, reason):
645 if self.notif is None:
646 return
647 self.notif.CloseNotification(dbus.UInt32(id_))
648 self.notif = None
650 if reason == 'ignore':
651 return
653 gajim.interface.handle_event(self.account, self.jid, self.msg_type)
655 def version_reply_handler(self, name, vendor, version, spec_version=None):
656 if spec_version:
657 version = spec_version
658 elif vendor == 'Xfce' and version.startswith('0.1.0'):
659 version = '0.9'
660 version_list = version.split('.')
661 self.version = []
662 try:
663 while len(version_list):
664 self.version.append(int(version_list.pop(0)))
665 except ValueError:
666 self.version_error_handler_3_x_try(None)
667 self.attempt_notify()
669 def get_version(self):
670 self.notif.GetServerInfo(
671 reply_handler=self.version_reply_handler,
672 error_handler=self.version_error_handler_2_x_try)
674 def version_error_handler_2_x_try(self, e):
675 self.notif.GetServerInformation(
676 reply_handler=self.version_reply_handler,
677 error_handler=self.version_error_handler_3_x_try)
679 def version_error_handler_3_x_try(self, e):
680 self.version = self.default_version
681 self.attempt_notify()