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/>.
37 from common
import gajim
38 from common
import helpers
39 from common
import ged
41 from common
import dbus_support
42 if dbus_support
.supported
:
47 USER_HAS_PYNOTIFY
= True # user has pynotify module
50 pynotify
.init('Gajim Notification')
52 USER_HAS_PYNOTIFY
= False
54 def get_show_in_roster(event
, account
, contact
, session
=None):
56 Return True if this event must be shown in roster, else False
58 if event
== 'gc_message_received':
60 num
= get_advanced_notification(event
, account
, contact
)
62 if gajim
.config
.get_per('notifications', str(num
), 'roster') == 'yes':
64 if gajim
.config
.get_per('notifications', str(num
), 'roster') == 'no':
66 if event
== 'message_received':
67 if session
and session
.control
:
71 def get_show_in_systray(event
, account
, contact
, type_
=None):
73 Return True if this event must be shown in systray, else False
75 num
= get_advanced_notification(event
, account
, contact
)
77 if gajim
.config
.get_per('notifications', str(num
), 'systray') == 'yes':
79 if gajim
.config
.get_per('notifications', str(num
), 'systray') == 'no':
81 if type_
== 'printed_gc_msg' and not gajim
.config
.get(
82 'notify_on_all_muc_messages'):
83 # it's not an highlighted message, don't show in systray
85 return gajim
.config
.get('trayicon_notification_on_events')
87 def get_advanced_notification(event
, account
, contact
):
89 Returns the number of the first (top most) advanced notification else None
92 notif
= gajim
.config
.get_per('notifications', str(num
))
98 if gajim
.config
.get_per('notifications', str(num
), 'event') == event
:
100 recipient_type
= gajim
.config
.get_per('notifications', str(num
),
102 recipients
= gajim
.config
.get_per('notifications', str(num
),
103 'recipients').split()
104 if recipient_type
== 'all':
106 elif recipient_type
== 'contact' and contact
.jid
in recipients
:
108 elif recipient_type
== 'group':
109 for group
in contact
.groups
:
110 if group
in contact
.groups
:
115 our_status
= gajim
.SHOW_LIST
[gajim
.connections
[account
].connected
]
116 status
= gajim
.config
.get_per('notifications', str(num
), 'status')
117 if status
== 'all' or our_status
in status
.split():
121 tab_opened
= gajim
.config
.get_per('notifications', str(num
),
123 if tab_opened
== 'both':
126 chat_control
= helpers
.get_chat_control(account
, contact
)
127 if (chat_control
and tab_opened
== 'yes') or (not chat_control \
128 and tab_opened
== 'no'):
134 notif
= gajim
.config
.get_per('notifications', str(num
))
136 def notify(event
, jid
, account
, parameters
, advanced_notif_num
=None):
138 Check what type of notifications we want, depending on basic and the
139 advanced configuration of notifications and do these notifications;
140 advanced_notif_num holds the number of the first (top most) advanced
143 # First, find what notifications we want
147 if event
== 'status_change':
148 new_show
= parameters
[0]
149 status_message
= parameters
[1]
150 # Default: No popup for status change
151 elif event
== 'contact_connected':
152 status_message
= parameters
153 j
= gajim
.get_jid_without_resource(jid
)
154 server
= gajim
.get_server_from_jid(j
)
155 account_server
= account
+ '/' + server
156 block_transport
= False
157 if account_server
in gajim
.block_signed_in_notifications
and \
158 gajim
.block_signed_in_notifications
[account_server
]:
159 block_transport
= True
160 if helpers
.allow_showing_notification(account
, 'notify_on_signin') and \
161 not gajim
.block_signed_in_notifications
[account
] and \
164 if gajim
.config
.get_per('soundevents', 'contact_connected',
165 'enabled') and not gajim
.block_signed_in_notifications
[account
] and \
166 not block_transport
and helpers
.allow_sound_notification(account
,
167 event
, advanced_notif_num
):
169 elif event
== 'contact_disconnected':
170 status_message
= parameters
171 if helpers
.allow_showing_notification(account
, 'notify_on_signout'):
173 if gajim
.config
.get_per('soundevents', 'contact_disconnected',
174 'enabled') and helpers
.allow_sound_notification(account
,
175 event
, advanced_notif_num
):
177 elif event
== 'new_message':
178 message_type
= parameters
[0]
179 is_first_message
= parameters
[1]
180 nickname
= parameters
[2]
181 if gajim
.config
.get('notification_preview_message'):
182 message
= parameters
[3]
183 if message
.startswith('/me ') or message
.startswith('/me\n'):
184 message
= '* ' + nickname
+ message
[3:]
186 # We don't want message preview, do_preview = False
188 focused
= parameters
[4]
189 if helpers
.allow_showing_notification(account
, 'notify_on_new_message',
190 advanced_notif_num
, is_first_message
):
192 if is_first_message
and helpers
.allow_sound_notification(account
,
193 'first_message_received', advanced_notif_num
):
195 elif not is_first_message
and focused
and \
196 helpers
.allow_sound_notification(account
,
197 'next_message_received_focused', advanced_notif_num
):
199 elif not is_first_message
and not focused
and \
200 helpers
.allow_sound_notification(account
,
201 'next_message_received_unfocused', advanced_notif_num
):
204 print '*Event not implemeted yet*'
206 if advanced_notif_num
is not None and gajim
.config
.get_per('notifications',
207 str(advanced_notif_num
), 'run_command'):
210 # Do the wanted notifications
212 if event
in ('contact_connected', 'contact_disconnected',
213 'status_change'): # Common code for popup for these three events
214 if event
== 'contact_disconnected':
215 show_image
= 'offline.png'
216 suffix
= '_notif_size_bw'
217 else: # Status Change or Connected
218 # FIXME: for status change,
219 # we don't always 'online.png', but we
220 # first need 48x48 for all status
221 show_image
= 'online.png'
222 suffix
= '_notif_size_colored'
223 transport_name
= gajim
.get_transport_name_from_jid(jid
)
226 img_path
= os
.path
.join(helpers
.get_transport_path(
227 transport_name
), '48x48', show_image
)
228 if not img_path
or not os
.path
.isfile(img_path
):
229 iconset
= gajim
.config
.get('iconset')
230 img_path
= os
.path
.join(helpers
.get_iconset_path(iconset
),
232 path
= gtkgui_helpers
.get_path_to_generic_or_avatar(img_path
,
233 jid
=jid
, suffix
=suffix
)
234 if event
== 'status_change':
235 title
= _('%(nick)s Changed Status') % \
236 {'nick': gajim
.get_name_from_jid(account
, jid
)}
237 text
= _('%(nick)s is now %(status)s') % \
238 {'nick': gajim
.get_name_from_jid(account
, jid
),\
239 'status': helpers
.get_uf_show(gajim
.SHOW_LIST
[new_show
])}
241 text
= text
+ " : " + status_message
242 popup(_('Contact Changed Status'), jid
, account
,
243 path_to_image
=path
, title
=title
, text
=text
)
244 elif event
== 'contact_connected':
245 title
= _('%(nickname)s Signed In') % \
246 {'nickname': gajim
.get_name_from_jid(account
, jid
)}
249 text
= status_message
250 popup(_('Contact Signed In'), jid
, account
,
251 path_to_image
=path
, title
=title
, text
=text
)
252 elif event
== 'contact_disconnected':
253 title
= _('%(nickname)s Signed Out') % \
254 {'nickname': gajim
.get_name_from_jid(account
, jid
)}
257 text
= status_message
258 popup(_('Contact Signed Out'), jid
, account
,
259 path_to_image
=path
, title
=title
, text
=text
)
260 elif event
== 'new_message':
261 if message_type
== 'normal': # single message
262 event_type
= _('New Single Message')
263 img_name
= 'gajim-single_msg_recv'
264 title
= _('New Single Message from %(nickname)s') % \
265 {'nickname': nickname
}
267 elif message_type
== 'pm': # private message
268 event_type
= _('New Private Message')
269 room_name
= gajim
.get_nick_from_jid(jid
)
270 img_name
= 'gajim-priv_msg_recv'
271 title
= _('New Private Message from group chat %s') % room_name
273 text
= _('%(nickname)s: %(message)s') % \
274 {'nickname': nickname
, 'message': message
}
276 text
= _('Messaged by %(nickname)s') % \
277 {'nickname': nickname
}
280 event_type
= _('New Message')
281 img_name
= 'gajim-chat_msg_recv'
282 title
= _('New Message from %(nickname)s') % \
283 {'nickname': nickname
}
285 img_path
= gtkgui_helpers
.get_icon_path(img_name
, 48)
286 popup(event_type
, jid
, account
, message_type
,
287 path_to_image
=img_path
, title
=title
, text
=text
)
291 snd_event
= None # If not snd_file, play the event
292 if event
== 'new_message':
293 if advanced_notif_num
is not None and gajim
.config
.get_per(
294 'notifications', str(advanced_notif_num
), 'sound') == 'yes':
295 snd_file
= gajim
.config
.get_per('notifications',
296 str(advanced_notif_num
), 'sound_file')
297 elif advanced_notif_num
is not None and gajim
.config
.get_per(
298 'notifications', str(advanced_notif_num
), 'sound') == 'no':
299 pass # do not set snd_event
300 elif is_first_message
:
301 snd_event
= 'first_message_received'
303 snd_event
= 'next_message_received_focused'
305 snd_event
= 'next_message_received_unfocused'
306 elif event
in ('contact_connected', 'contact_disconnected'):
309 helpers
.play_sound_file(snd_file
)
311 helpers
.play_sound(snd_event
)
314 command
= gajim
.config
.get_per('notifications', str(advanced_notif_num
),
317 helpers
.exec_command(command
)
321 def popup(event_type
, jid
, account
, msg_type
='', path_to_image
=None, title
=None,
324 Notify a user of an event. It first tries to a valid implementation of
325 the Desktop Notification Specification. If that fails, then we fall back to
326 the older style PopupNotificationWindow method
329 if not path_to_image
:
330 path_to_image
= gtkgui_helpers
.get_icon_path('gajim-chat_msg_recv', 48)
332 # Try to show our popup via D-Bus and notification daemon
333 if gajim
.config
.get('use_notif_daemon') and dbus_support
.supported
:
335 DesktopNotification(event_type
, jid
, account
, msg_type
,
336 path_to_image
, title
, gobject
.markup_escape_text(text
))
337 return # sucessfully did D-Bus Notification procedure!
338 except dbus
.DBusException
, e
:
339 # Connection to D-Bus failed
340 gajim
.log
.debug(str(e
))
342 # This means that we sent the message incorrectly
343 gajim
.log
.debug(str(e
))
345 # Ok, that failed. Let's try pynotify, which also uses notification daemon
346 if gajim
.config
.get('use_notif_daemon') and USER_HAS_PYNOTIFY
:
347 if not text
and event_type
== 'new_message':
348 # empty text for new_message means do_preview = False
349 # -> default value for text
350 _text
= gobject
.markup_escape_text(
351 gajim
.get_name_from_jid(account
, jid
))
353 _text
= gobject
.markup_escape_text(text
)
360 notification
= pynotify
.Notification(_title
, _text
)
361 timeout
= gajim
.config
.get('notification_timeout') * 1000 # make it ms
362 notification
.set_timeout(timeout
)
364 notification
.set_category(event_type
)
365 notification
.set_data('event_type', event_type
)
366 notification
.set_data('jid', jid
)
367 notification
.set_data('account', account
)
368 notification
.set_data('msg_type', msg_type
)
369 notification
.set_property('icon-name', path_to_image
)
370 if 'actions' in pynotify
.get_server_caps():
371 notification
.add_action('default', 'Default Action',
372 on_pynotify_notification_clicked
)
377 except gobject
.GError
, e
:
378 # Connection to notification-daemon failed, see #2893
379 gajim
.log
.debug(str(e
))
381 # Either nothing succeeded or the user wants old-style notifications
382 instance
= dialogs
.PopupNotificationWindow(event_type
, jid
, account
,
383 msg_type
, path_to_image
, title
, text
)
384 gajim
.interface
.roster
.popup_notification_windows
.append(instance
)
386 def on_pynotify_notification_clicked(notification
, action
):
387 jid
= notification
.get_data('jid')
388 account
= notification
.get_data('account')
389 msg_type
= notification
.get_data('msg_type')
392 gajim
.interface
.handle_event(account
, jid
, msg_type
)
399 gajim
.ged
.register_event_handler('notification', ged
.GUI2
,
400 self
._nec
_notification
)
402 def _nec_notification(self
, obj
):
404 popup(obj
.popup_event_type
, obj
.jid
, obj
.conn
.name
,
405 obj
.popup_msg_type
, path_to_image
=obj
.popup_image
,
406 title
=obj
.popup_title
, text
=obj
.popup_text
)
410 helpers
.play_sound_file(obj
.sound_file
)
411 elif obj
.sound_event
:
412 helpers
.play_sound(obj
.sound_event
)
416 helpers
.exec_command(obj
.command
)
420 class NotificationResponseManager
:
422 Collect references to pending DesktopNotifications and manages there
423 signalling. This is necessary due to a bug in DBus where you can't remove a
424 signal from an interface once it's connected
430 self
.interface
= None
432 def attach_to_interface(self
):
433 if self
.interface
is not None:
435 self
.interface
= dbus_support
.get_notifications_interface()
436 self
.interface
.connect_to_signal('ActionInvoked',
437 self
.on_action_invoked
)
438 self
.interface
.connect_to_signal('NotificationClosed', self
.on_closed
)
440 def on_action_invoked(self
, id_
, reason
):
441 self
.received
.append((id_
, time
.time(), reason
))
442 if id_
in self
.pending
:
443 notification
= self
.pending
[id_
]
444 notification
.on_action_invoked(id_
, reason
)
445 del self
.pending
[id_
]
446 if len(self
.received
) > 20:
448 for rec
in self
.received
:
451 self
.received
.remove(rec
)
453 def on_closed(self
, id_
, reason
=None):
454 if id_
in self
.pending
:
455 del self
.pending
[id_
]
457 def add_pending(self
, id_
, object_
):
458 # Check to make sure that we handle an event immediately if we're adding
459 # an id that's already been triggered
460 for rec
in self
.received
:
462 object_
.on_action_invoked(id_
, rec
[2])
463 self
.received
.remove(rec
)
465 if id_
not in self
.pending
:
467 self
.pending
[id_
] = object_
469 # We've triggered an event that has a duplicate ID!
470 gajim
.log
.debug('Duplicate ID of notification. Can\'t handle this.')
472 notification_response_manager
= NotificationResponseManager()
474 class DesktopNotification
:
476 A DesktopNotification that interfaces with D-Bus via the Desktop
477 Notification Specification
480 def __init__(self
, event_type
, jid
, account
, msg_type
='',
481 path_to_image
=None, title
=None, text
=None):
482 self
.path_to_image
= path_to_image
483 self
.event_type
= event_type
486 # 0.3.1 is the only version of notification daemon that has no way
487 # to determine which version it is. If no method exists, it means
488 # they're using that one.
489 self
.default_version
= [0, 3, 1]
490 self
.account
= account
492 self
.msg_type
= msg_type
494 # default value of text
495 if not text
and event_type
== 'new_message':
496 # empty text for new_message means do_preview = False
497 self
.text
= gajim
.get_name_from_jid(account
, jid
)
500 self
.title
= event_type
# default value
502 if event_type
== _('Contact Signed In'):
503 ntype
= 'presence.online'
504 elif event_type
== _('Contact Signed Out'):
505 ntype
= 'presence.offline'
506 elif event_type
in (_('New Message'), _('New Single Message'),
507 _('New Private Message')):
508 ntype
= 'im.received'
509 elif event_type
== _('File Transfer Request'):
511 elif event_type
== _('File Transfer Error'):
512 ntype
= 'transfer.error'
513 elif event_type
in (_('File Transfer Completed'),
514 _('File Transfer Stopped')):
515 ntype
= 'transfer.complete'
516 elif event_type
== _('New E-mail'):
517 ntype
= 'email.arrived'
518 elif event_type
== _('Groupchat Invitation'):
519 ntype
= 'im.invitation'
520 elif event_type
== _('Contact Changed Status'):
521 ntype
= 'presence.status'
522 elif event_type
== _('Connection Failed'):
523 ntype
= 'connection.failed'
524 elif event_type
== _('Subscription request'):
525 ntype
= 'subscription.request'
526 elif event_type
== _('Unsubscribed'):
527 ntype
= 'unsubscribed'
529 # default failsafe values
530 self
.path_to_image
= gtkgui_helpers
.get_icon_path(
531 'gajim-chat_msg_recv', 48)
532 ntype
= 'im' # Notification Type
534 self
.notif
= dbus_support
.get_notifications_interface(self
)
535 if self
.notif
is None:
536 raise dbus
.DBusException('unable to get notifications interface')
539 if self
.kde_notifications
:
540 self
.attempt_notify()
542 self
.capabilities
= self
.notif
.GetCapabilities()
543 if self
.capabilities
is None:
544 self
.capabilities
= ['actions']
547 def attempt_notify(self
):
548 timeout
= gajim
.config
.get('notification_timeout') # in seconds
550 if self
.kde_notifications
:
551 notification_text
= ('<html><img src="%(image)s" align=left />' \
552 '%(title)s<br/>%(text)s</html>') % {'title': self
.title
,
553 'text': self
.text
, 'image': self
.path_to_image
}
554 gajim_icon
= gtkgui_helpers
.get_icon_path('gajim', 48)
557 dbus
.String(_('Gajim')), # app_name (string)
558 dbus
.UInt32(0), # replaces_id (uint)
559 ntype
, # event_id (string)
560 dbus
.String(gajim_icon
), # app_icon (string)
561 dbus
.String(''), # summary (string)
562 dbus
.String(notification_text
), # body (string)
563 # actions (stringlist)
564 (dbus
.String('default'), dbus
.String(self
.event_type
),
565 dbus
.String('ignore'), dbus
.String(_('Ignore'))),
566 [], # hints (not used in KDE yet)
567 dbus
.UInt32(timeout
*1000), # timeout (int), in ms
568 reply_handler
=self
.attach_by_id
,
569 error_handler
=self
.notify_another_way
)
573 version
= self
.version
574 if version
[:2] == [0, 2]:
576 if 'actions' in self
.capabilities
:
577 actions
= {'default': 0}
580 dbus
.String(_('Gajim')),
581 dbus
.String(self
.path_to_image
),
585 dbus
.String(self
.title
),
586 dbus
.String(self
.text
),
587 [dbus
.String(self
.path_to_image
)],
591 dbus
.UInt32(timeout
),
592 reply_handler
=self
.attach_by_id
,
593 error_handler
=self
.notify_another_way
)
594 except AttributeError:
595 # we're actually dealing with the newer version
598 if gajim
.interface
.systray_enabled
and \
599 gajim
.config
.get('attach_notifications_to_systray'):
600 status_icon
= gajim
.interface
.systray
.status_icon
601 x
, y
, width
, height
= status_icon
.get_geometry()[1]
602 pos_x
= x
+ (width
/ 2)
603 pos_y
= y
+ (height
/ 2)
604 hints
= {'x': pos_x
, 'y': pos_y
}
607 if version
>= [0, 3, 2]:
608 hints
['urgency'] = dbus
.Byte(0) # Low Urgency
609 hints
['category'] = dbus
.String(ntype
)
610 # it seems notification-daemon doesn't like empty text
615 if os
.environ
.get('KDE_FULL_SESSION') == 'true':
616 self
.path_to_image
= os
.path
.abspath(self
.path_to_image
)
617 text
= '<table style=\'padding: 3px\'><tr><td>' \
618 '<img src=\"%s\"></td><td width=20> </td>' \
619 '<td>%s</td></tr></table>' % (self
.path_to_image
,
621 self
.path_to_image
= os
.path
.abspath(
622 gtkgui_helpers
.get_icon_path('gajim', 48))
624 if 'actions' in self
.capabilities
:
625 actions
= (dbus
.String('default'), dbus
.String(
629 dbus
.String(_('Gajim')),
630 # this notification does not replace other
632 dbus
.String(self
.path_to_image
),
633 dbus
.String(self
.title
),
637 dbus
.UInt32(timeout
*1000),
638 reply_handler
=self
.attach_by_id
,
639 error_handler
=self
.notify_another_way
)
641 self
.notify_another_way(e
)
645 dbus
.String(_('Gajim')),
646 dbus
.String(self
.path_to_image
),
648 dbus
.String(self
.title
),
649 dbus
.String(self
.text
),
652 dbus
.UInt32(timeout
*1000),
653 reply_handler
=self
.attach_by_id
,
654 error_handler
=self
.notify_another_way
)
656 self
.notify_another_way(e
)
658 def attach_by_id(self
, id_
):
660 notification_response_manager
.attach_to_interface()
661 notification_response_manager
.add_pending(self
.id, self
)
663 def notify_another_way(self
, e
):
664 gajim
.log
.debug('Error when trying to use notification daemon: %s' % \
666 instance
= dialogs
.PopupNotificationWindow(self
.event_type
, self
.jid
,
667 self
.account
, self
.msg_type
, self
.path_to_image
, self
.title
,
669 gajim
.interface
.roster
.popup_notification_windows
.append(instance
)
671 def on_action_invoked(self
, id_
, reason
):
672 if self
.notif
is None:
674 self
.notif
.CloseNotification(dbus
.UInt32(id_
))
677 if reason
== 'ignore':
680 gajim
.interface
.handle_event(self
.account
, self
.jid
, self
.msg_type
)
682 def version_reply_handler(self
, name
, vendor
, version
, spec_version
=None):
684 version
= spec_version
685 elif vendor
== 'Xfce' and version
.startswith('0.1.0'):
687 version_list
= version
.split('.')
690 while len(version_list
):
691 self
.version
.append(int(version_list
.pop(0)))
693 self
.version_error_handler_3_x_try(None)
694 self
.attempt_notify()
696 def get_version(self
):
697 self
.notif
.GetServerInfo(
698 reply_handler
=self
.version_reply_handler
,
699 error_handler
=self
.version_error_handler_2_x_try
)
701 def version_error_handler_2_x_try(self
, e
):
702 self
.notif
.GetServerInformation(
703 reply_handler
=self
.version_reply_handler
,
704 error_handler
=self
.version_error_handler_3_x_try
)
706 def version_error_handler_3_x_try(self
, e
):
707 self
.version
= self
.default_version
708 self
.attempt_notify()