2008-08-15 19:31:51 +02:00
|
|
|
# -*- coding:utf-8 -*-
|
2008-08-15 05:20:23 +02:00
|
|
|
## src/notify.py
|
2005-11-11 20:12:02 +01:00
|
|
|
##
|
2008-08-15 05:20:23 +02:00
|
|
|
## Copyright (C) 2005 Sebastian Estienne
|
|
|
|
## Copyright (C) 2005-2006 Andrew Sayman <lorien420 AT myrealbox.com>
|
|
|
|
## Copyright (C) 2005-2007 Nikos Kouremenos <kourem AT gmail.com>
|
|
|
|
## Copyright (C) 2005-2008 Yann Leboulanger <asterix AT lagaule.org>
|
|
|
|
## Copyright (C) 2006 Travis Shirk <travis AT pobox.com>
|
|
|
|
## Copyright (C) 2006-2008 Jean-Marie Traissard <jim AT lapin.org>
|
|
|
|
## Copyright (C) 2007 Julien Pivotto <roidelapluie AT gmail.com>
|
|
|
|
## Stephan Erb <steve-e AT h3c.de>
|
|
|
|
## Copyright (C) 2008 Brendan Taylor <whateley AT gmail.com>
|
|
|
|
## Jonathan Schleifer <js-gajim AT webkeks.org>
|
2005-11-11 20:12:02 +01:00
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## This file is part of Gajim.
|
|
|
|
##
|
|
|
|
## Gajim is free software; you can redistribute it and/or modify
|
2005-11-11 20:12:02 +01:00
|
|
|
## it under the terms of the GNU General Public License as published
|
2007-10-22 13:13:13 +02:00
|
|
|
## by the Free Software Foundation; version 3 only.
|
2005-11-11 20:12:02 +01:00
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## Gajim is distributed in the hope that it will be useful,
|
2005-11-11 20:12:02 +01:00
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2008-08-15 05:20:23 +02:00
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2005-11-11 20:12:02 +01:00
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## You should have received a copy of the GNU General Public License
|
2008-08-15 05:20:23 +02:00
|
|
|
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
2007-10-22 13:13:13 +02:00
|
|
|
##
|
2005-11-11 20:12:02 +01:00
|
|
|
|
|
|
|
import os
|
2006-03-25 01:59:09 +01:00
|
|
|
import time
|
2005-11-11 20:12:02 +01:00
|
|
|
import dialogs
|
2007-01-17 00:26:38 +01:00
|
|
|
import gobject
|
2006-04-05 16:57:09 +02:00
|
|
|
import gtkgui_helpers
|
2009-04-04 07:06:46 +02:00
|
|
|
import gtk
|
2005-11-11 20:12:02 +01:00
|
|
|
|
|
|
|
from common import gajim
|
2006-05-10 22:25:51 +02:00
|
|
|
from common import helpers
|
2005-11-11 20:12:02 +01:00
|
|
|
|
2006-09-29 22:49:15 +02:00
|
|
|
from common import dbus_support
|
2005-12-10 01:56:38 +01:00
|
|
|
if dbus_support.supported:
|
|
|
|
import dbus
|
2006-09-21 17:11:13 +02:00
|
|
|
import dbus.glib
|
2005-11-11 20:12:02 +01:00
|
|
|
|
2006-09-28 02:31:59 +02:00
|
|
|
|
|
|
|
USER_HAS_PYNOTIFY = True # user has pynotify module
|
|
|
|
try:
|
|
|
|
import pynotify
|
|
|
|
pynotify.init('Gajim Notification')
|
|
|
|
except ImportError:
|
|
|
|
USER_HAS_PYNOTIFY = False
|
|
|
|
|
2009-04-08 09:31:11 +02:00
|
|
|
if gajim.HAVE_INDICATOR:
|
|
|
|
import indicate
|
|
|
|
|
|
|
|
def setup_indicator_server():
|
|
|
|
server = indicate.indicate_server_ref_default()
|
|
|
|
server.set_type('message.im')
|
|
|
|
server.set_desktop_file('/usr/share/applications/gajim.desktop')
|
|
|
|
server.connect('server-display', server_display)
|
|
|
|
server.show()
|
|
|
|
|
|
|
|
def display(indicator, account, jid, msg_type):
|
|
|
|
gajim.interface.handle_event(account, jid, msg_type)
|
|
|
|
indicator.hide()
|
|
|
|
|
|
|
|
def server_display(server):
|
|
|
|
win = gajim.interface.roster.window
|
2009-04-04 07:06:46 +02:00
|
|
|
win.present()
|
|
|
|
|
2008-09-22 11:10:18 +02:00
|
|
|
def get_show_in_roster(event, account, contact, session=None):
|
2006-09-02 23:01:11 +02:00
|
|
|
'''Return True if this event must be shown in roster, else False'''
|
2007-05-07 23:02:48 +02:00
|
|
|
if event == 'gc_message_received':
|
|
|
|
return True
|
2006-09-02 23:01:11 +02:00
|
|
|
num = get_advanced_notification(event, account, contact)
|
2008-04-18 02:26:07 +02:00
|
|
|
if num is not None:
|
2006-09-02 23:01:11 +02:00
|
|
|
if gajim.config.get_per('notifications', str(num), 'roster') == 'yes':
|
|
|
|
return True
|
|
|
|
if gajim.config.get_per('notifications', str(num), 'roster') == 'no':
|
|
|
|
return False
|
|
|
|
if event == 'message_received':
|
2008-05-13 03:59:10 +02:00
|
|
|
if session and session.control:
|
2006-09-16 01:06:40 +02:00
|
|
|
return False
|
|
|
|
return True
|
2006-09-02 23:01:11 +02:00
|
|
|
|
2008-02-27 22:33:50 +01:00
|
|
|
def get_show_in_systray(event, account, contact, type_=None):
|
2006-11-03 20:52:39 +01:00
|
|
|
'''Return True if this event must be shown in systray, else False'''
|
2006-09-02 23:01:11 +02:00
|
|
|
num = get_advanced_notification(event, account, contact)
|
2008-04-18 02:26:07 +02:00
|
|
|
if num is not None:
|
2006-09-02 23:01:11 +02:00
|
|
|
if gajim.config.get_per('notifications', str(num), 'systray') == 'yes':
|
|
|
|
return True
|
|
|
|
if gajim.config.get_per('notifications', str(num), 'systray') == 'no':
|
|
|
|
return False
|
2008-02-27 22:33:50 +01:00
|
|
|
if type_ == 'printed_gc_msg' and not gajim.config.get(
|
|
|
|
'notify_on_all_muc_messages'):
|
|
|
|
# it's not an highlighted message, don't show in systray
|
|
|
|
return False
|
2006-11-03 20:52:39 +01:00
|
|
|
return gajim.config.get('trayicon_notification_on_events')
|
2006-09-02 23:01:11 +02:00
|
|
|
|
2006-08-27 00:51:33 +02:00
|
|
|
def get_advanced_notification(event, account, contact):
|
2006-09-28 15:35:53 +02:00
|
|
|
'''Returns the number of the first (top most)
|
|
|
|
advanced notification else None'''
|
2006-08-27 00:51:33 +02:00
|
|
|
num = 0
|
|
|
|
notif = gajim.config.get_per('notifications', str(num))
|
|
|
|
while notif:
|
|
|
|
recipient_ok = False
|
|
|
|
status_ok = False
|
|
|
|
tab_opened_ok = False
|
|
|
|
# test event
|
|
|
|
if gajim.config.get_per('notifications', str(num), 'event') == event:
|
|
|
|
# test recipient
|
|
|
|
recipient_type = gajim.config.get_per('notifications', str(num),
|
|
|
|
'recipient_type')
|
|
|
|
recipients = gajim.config.get_per('notifications', str(num),
|
|
|
|
'recipients').split()
|
|
|
|
if recipient_type == 'all':
|
|
|
|
recipient_ok = True
|
|
|
|
elif recipient_type == 'contact' and contact.jid in recipients:
|
|
|
|
recipient_ok = True
|
|
|
|
elif recipient_type == 'group':
|
|
|
|
for group in contact.groups:
|
|
|
|
if group in contact.groups:
|
|
|
|
recipient_ok = True
|
|
|
|
break
|
|
|
|
if recipient_ok:
|
|
|
|
# test status
|
|
|
|
our_status = gajim.SHOW_LIST[gajim.connections[account].connected]
|
|
|
|
status = gajim.config.get_per('notifications', str(num), 'status')
|
|
|
|
if status == 'all' or our_status in status.split():
|
|
|
|
status_ok = True
|
|
|
|
if status_ok:
|
|
|
|
# test window_opened
|
|
|
|
tab_opened = gajim.config.get_per('notifications', str(num),
|
|
|
|
'tab_opened')
|
|
|
|
if tab_opened == 'both':
|
|
|
|
tab_opened_ok = True
|
|
|
|
else:
|
2006-11-15 02:59:21 +01:00
|
|
|
chat_control = helpers.get_chat_control(account, contact)
|
2006-08-27 00:51:33 +02:00
|
|
|
if (chat_control and tab_opened == 'yes') or (not chat_control and \
|
|
|
|
tab_opened == 'no'):
|
|
|
|
tab_opened_ok = True
|
|
|
|
if tab_opened_ok:
|
|
|
|
return num
|
|
|
|
|
|
|
|
num += 1
|
|
|
|
notif = gajim.config.get_per('notifications', str(num))
|
|
|
|
|
2008-09-22 11:10:18 +02:00
|
|
|
def notify(event, jid, account, parameters, advanced_notif_num=None):
|
2008-05-18 23:17:53 +02:00
|
|
|
'''Check what type of notifications we want, depending on basic
|
2006-09-28 15:35:53 +02:00
|
|
|
and the advanced configuration of notifications and do these notifications;
|
2006-09-28 15:39:42 +02:00
|
|
|
advanced_notif_num holds the number of the first (top most) advanced
|
|
|
|
notification'''
|
2006-05-10 22:25:51 +02:00
|
|
|
# First, find what notifications we want
|
|
|
|
do_popup = False
|
|
|
|
do_sound = False
|
2006-08-27 00:51:33 +02:00
|
|
|
do_cmd = False
|
2006-09-28 00:37:41 +02:00
|
|
|
if event == 'status_change':
|
2006-05-10 22:25:51 +02:00
|
|
|
new_show = parameters[0]
|
|
|
|
status_message = parameters[1]
|
2006-09-28 00:37:41 +02:00
|
|
|
# Default: No popup for status change
|
|
|
|
elif event == 'contact_connected':
|
2006-05-10 22:25:51 +02:00
|
|
|
status_message = parameters
|
2006-06-12 17:24:58 +02:00
|
|
|
j = gajim.get_jid_without_resource(jid)
|
|
|
|
server = gajim.get_server_from_jid(j)
|
|
|
|
account_server = account + '/' + server
|
|
|
|
block_transport = False
|
|
|
|
if account_server in gajim.block_signed_in_notifications and \
|
|
|
|
gajim.block_signed_in_notifications[account_server]:
|
|
|
|
block_transport = True
|
2006-08-27 00:51:33 +02:00
|
|
|
if helpers.allow_showing_notification(account, 'notify_on_signin') and \
|
|
|
|
not gajim.block_signed_in_notifications[account] and not block_transport:
|
2006-05-10 22:25:51 +02:00
|
|
|
do_popup = True
|
|
|
|
if gajim.config.get_per('soundevents', 'contact_connected',
|
2006-06-12 17:24:58 +02:00
|
|
|
'enabled') and not gajim.block_signed_in_notifications[account] and \
|
|
|
|
not block_transport:
|
2006-05-10 22:25:51 +02:00
|
|
|
do_sound = True
|
2006-09-28 00:37:41 +02:00
|
|
|
elif event == 'contact_disconnected':
|
2006-05-10 22:25:51 +02:00
|
|
|
status_message = parameters
|
2006-08-27 00:51:33 +02:00
|
|
|
if helpers.allow_showing_notification(account, 'notify_on_signout'):
|
2006-05-10 22:25:51 +02:00
|
|
|
do_popup = True
|
|
|
|
if gajim.config.get_per('soundevents', 'contact_disconnected',
|
|
|
|
'enabled'):
|
|
|
|
do_sound = True
|
2006-09-28 00:37:41 +02:00
|
|
|
elif event == 'new_message':
|
2006-05-22 22:14:54 +02:00
|
|
|
message_type = parameters[0]
|
2006-09-28 15:35:53 +02:00
|
|
|
is_first_message = parameters[1]
|
2006-05-22 22:14:54 +02:00
|
|
|
nickname = parameters[2]
|
2007-07-10 23:57:56 +02:00
|
|
|
if gajim.config.get('notification_preview_message'):
|
|
|
|
message = parameters[3]
|
2007-12-31 01:24:59 +01:00
|
|
|
if message.startswith('/me ') or message.startswith('/me\n'):
|
|
|
|
message = '* ' + nickname + message[3:]
|
2007-07-10 23:57:56 +02:00
|
|
|
else:
|
2007-07-13 00:19:21 +02:00
|
|
|
# We don't want message preview, do_preview = False
|
2007-07-10 23:57:56 +02:00
|
|
|
message = ''
|
2008-01-27 21:15:17 +01:00
|
|
|
focused = parameters[4]
|
2006-08-27 00:51:33 +02:00
|
|
|
if helpers.allow_showing_notification(account, 'notify_on_new_message',
|
2006-09-28 15:35:53 +02:00
|
|
|
advanced_notif_num, is_first_message):
|
2006-05-22 22:14:54 +02:00
|
|
|
do_popup = True
|
2009-02-13 22:14:50 +01:00
|
|
|
if is_first_message and helpers.allow_sound_notification(account,
|
2006-09-28 16:47:18 +02:00
|
|
|
'first_message_received', advanced_notif_num):
|
2006-05-22 22:14:54 +02:00
|
|
|
do_sound = True
|
2008-01-27 21:15:17 +01:00
|
|
|
elif not is_first_message and focused and \
|
2009-02-13 22:14:50 +01:00
|
|
|
helpers.allow_sound_notification(account, 'next_message_received_focused',
|
2008-01-27 21:15:17 +01:00
|
|
|
advanced_notif_num):
|
|
|
|
do_sound = True
|
|
|
|
elif not is_first_message and not focused and \
|
2009-02-13 22:14:50 +01:00
|
|
|
helpers.allow_sound_notification(account,
|
|
|
|
'next_message_received_unfocused', advanced_notif_num):
|
2006-05-22 22:14:54 +02:00
|
|
|
do_sound = True
|
|
|
|
else:
|
|
|
|
print '*Event not implemeted yet*'
|
2006-08-27 00:51:33 +02:00
|
|
|
|
2006-09-28 00:37:41 +02:00
|
|
|
if advanced_notif_num is not None and gajim.config.get_per('notifications',
|
2006-08-27 00:51:33 +02:00
|
|
|
str(advanced_notif_num), 'run_command'):
|
|
|
|
do_cmd = True
|
2008-05-18 23:17:53 +02:00
|
|
|
|
|
|
|
# Do the wanted notifications
|
2006-09-28 00:37:41 +02:00
|
|
|
if do_popup:
|
|
|
|
if event in ('contact_connected', 'contact_disconnected',
|
|
|
|
'status_change'): # Common code for popup for these three events
|
|
|
|
if event == 'contact_disconnected':
|
2006-05-10 22:25:51 +02:00
|
|
|
show_image = 'offline.png'
|
2008-07-27 12:21:51 +02:00
|
|
|
suffix = '_notif_size_bw'
|
2006-05-10 22:25:51 +02:00
|
|
|
else: #Status Change or Connected
|
2006-09-28 00:37:41 +02:00
|
|
|
# FIXME: for status change,
|
2008-05-18 23:17:53 +02:00
|
|
|
# we don't always 'online.png', but we
|
2006-05-10 22:25:51 +02:00
|
|
|
# first need 48x48 for all status
|
|
|
|
show_image = 'online.png'
|
2008-07-27 12:21:51 +02:00
|
|
|
suffix = '_notif_size_colored'
|
2006-05-10 22:25:51 +02:00
|
|
|
transport_name = gajim.get_transport_name_from_jid(jid)
|
|
|
|
img = None
|
|
|
|
if transport_name:
|
2007-08-23 00:46:31 +02:00
|
|
|
img = os.path.join(helpers.get_transport_path(transport_name),
|
2008-05-18 23:17:53 +02:00
|
|
|
'48x48', show_image)
|
2006-05-10 22:25:51 +02:00
|
|
|
if not img or not os.path.isfile(img):
|
|
|
|
iconset = gajim.config.get('iconset')
|
2007-08-23 00:46:31 +02:00
|
|
|
img = os.path.join(helpers.get_iconset_path(iconset), '48x48',
|
|
|
|
show_image)
|
2006-05-10 22:25:51 +02:00
|
|
|
path = gtkgui_helpers.get_path_to_generic_or_avatar(img,
|
|
|
|
jid = jid, suffix = suffix)
|
2006-09-28 00:37:41 +02:00
|
|
|
if event == 'status_change':
|
2006-05-10 22:25:51 +02:00
|
|
|
title = _('%(nick)s Changed Status') % \
|
|
|
|
{'nick': gajim.get_name_from_jid(account, jid)}
|
|
|
|
text = _('%(nick)s is now %(status)s') % \
|
|
|
|
{'nick': gajim.get_name_from_jid(account, jid),\
|
|
|
|
'status': helpers.get_uf_show(gajim.SHOW_LIST[new_show])}
|
|
|
|
if status_message:
|
2007-08-23 00:46:31 +02:00
|
|
|
text = text + " : " + status_message
|
2006-05-10 22:29:17 +02:00
|
|
|
popup(_('Contact Changed Status'), jid, account,
|
2008-09-22 11:10:18 +02:00
|
|
|
path_to_image=path, title=title, text=text)
|
2006-09-28 00:37:41 +02:00
|
|
|
elif event == 'contact_connected':
|
2006-05-10 22:25:51 +02:00
|
|
|
title = _('%(nickname)s Signed In') % \
|
|
|
|
{'nickname': gajim.get_name_from_jid(account, jid)}
|
|
|
|
text = ''
|
|
|
|
if status_message:
|
|
|
|
text = status_message
|
|
|
|
popup(_('Contact Signed In'), jid, account,
|
2008-09-22 11:10:18 +02:00
|
|
|
path_to_image=path, title=title, text=text)
|
2006-09-28 00:37:41 +02:00
|
|
|
elif event == 'contact_disconnected':
|
2006-05-10 22:25:51 +02:00
|
|
|
title = _('%(nickname)s Signed Out') % \
|
|
|
|
{'nickname': gajim.get_name_from_jid(account, jid)}
|
|
|
|
text = ''
|
|
|
|
if status_message:
|
|
|
|
text = status_message
|
|
|
|
popup(_('Contact Signed Out'), jid, account,
|
2008-09-22 11:10:18 +02:00
|
|
|
path_to_image=path, title=title, text=text)
|
2006-09-28 00:37:41 +02:00
|
|
|
elif event == 'new_message':
|
2006-05-22 22:14:54 +02:00
|
|
|
if message_type == 'normal': # single message
|
|
|
|
event_type = _('New Single Message')
|
|
|
|
img = os.path.join(gajim.DATA_DIR, 'pixmaps', 'events',
|
|
|
|
'single_msg_recv.png')
|
|
|
|
title = _('New Single Message from %(nickname)s') % \
|
|
|
|
{'nickname': nickname}
|
|
|
|
text = message
|
|
|
|
elif message_type == 'pm': # private message
|
|
|
|
event_type = _('New Private Message')
|
2006-10-07 14:18:51 +02:00
|
|
|
room_name = gajim.get_nick_from_jid(jid)
|
2006-05-22 22:14:54 +02:00
|
|
|
img = os.path.join(gajim.DATA_DIR, 'pixmaps', 'events',
|
|
|
|
'priv_msg_recv.png')
|
2006-10-07 16:36:08 +02:00
|
|
|
title = _('New Private Message from group chat %s') % room_name
|
2007-07-13 00:19:21 +02:00
|
|
|
if message:
|
2007-07-10 23:57:56 +02:00
|
|
|
text = _('%(nickname)s: %(message)s') % {'nickname': nickname,
|
|
|
|
'message': message}
|
|
|
|
else:
|
|
|
|
text = _('Messaged by %(nickname)s') % {'nickname': nickname}
|
|
|
|
|
2006-05-22 22:14:54 +02:00
|
|
|
else: # chat message
|
|
|
|
event_type = _('New Message')
|
|
|
|
img = os.path.join(gajim.DATA_DIR, 'pixmaps', 'events',
|
|
|
|
'chat_msg_recv.png')
|
|
|
|
title = _('New Message from %(nickname)s') % \
|
|
|
|
{'nickname': nickname}
|
2006-07-17 12:06:04 +02:00
|
|
|
text = message
|
2006-05-22 22:14:54 +02:00
|
|
|
path = gtkgui_helpers.get_path_to_generic_or_avatar(img)
|
|
|
|
popup(event_type, jid, account, message_type,
|
2008-09-22 11:10:18 +02:00
|
|
|
path_to_image=path, title=title, text=text)
|
2006-05-22 22:14:54 +02:00
|
|
|
|
2006-09-28 00:37:41 +02:00
|
|
|
if do_sound:
|
2006-08-27 00:51:33 +02:00
|
|
|
snd_file = None
|
|
|
|
snd_event = None # If not snd_file, play the event
|
2006-09-28 00:37:41 +02:00
|
|
|
if event == 'new_message':
|
|
|
|
if advanced_notif_num is not None and gajim.config.get_per(
|
|
|
|
'notifications', str(advanced_notif_num), 'sound') == 'yes':
|
2006-08-27 00:51:33 +02:00
|
|
|
snd_file = gajim.config.get_per('notifications',
|
|
|
|
str(advanced_notif_num), 'sound_file')
|
2006-09-28 00:37:41 +02:00
|
|
|
elif advanced_notif_num is not None and gajim.config.get_per(
|
2006-08-27 00:51:33 +02:00
|
|
|
'notifications', str(advanced_notif_num), 'sound') == 'no':
|
|
|
|
pass # do not set snd_event
|
2006-09-28 15:35:53 +02:00
|
|
|
elif is_first_message:
|
2006-08-27 00:51:33 +02:00
|
|
|
snd_event = 'first_message_received'
|
2008-01-27 21:15:17 +01:00
|
|
|
elif focused:
|
|
|
|
snd_event = 'next_message_received_focused'
|
2006-05-22 22:14:54 +02:00
|
|
|
else:
|
2008-01-27 21:15:17 +01:00
|
|
|
snd_event = 'next_message_received_unfocused'
|
2006-07-17 12:06:04 +02:00
|
|
|
elif event in ('contact_connected', 'contact_disconnected'):
|
2006-08-27 00:51:33 +02:00
|
|
|
snd_event = event
|
|
|
|
if snd_file:
|
|
|
|
helpers.play_sound_file(snd_file)
|
|
|
|
if snd_event:
|
|
|
|
helpers.play_sound(snd_event)
|
|
|
|
|
|
|
|
if do_cmd:
|
|
|
|
command = gajim.config.get_per('notifications', str(advanced_notif_num),
|
|
|
|
'command')
|
|
|
|
try:
|
|
|
|
helpers.exec_command(command)
|
2008-10-11 11:37:13 +02:00
|
|
|
except Exception:
|
2006-08-27 00:51:33 +02:00
|
|
|
pass
|
2006-05-10 22:25:51 +02:00
|
|
|
|
2008-09-22 11:10:18 +02:00
|
|
|
def popup(event_type, jid, account, msg_type='', path_to_image=None,
|
|
|
|
title=None, text=None):
|
2005-12-10 01:56:38 +01:00
|
|
|
'''Notifies a user of an event. It first tries to a valid implementation of
|
|
|
|
the Desktop Notification Specification. If that fails, then we fall back to
|
|
|
|
the older style PopupNotificationWindow method.'''
|
2008-11-29 14:37:06 +01:00
|
|
|
|
|
|
|
# default image
|
|
|
|
if not path_to_image:
|
|
|
|
path_to_image = os.path.abspath(
|
|
|
|
os.path.join(gajim.DATA_DIR, 'pixmaps', 'events',
|
|
|
|
'chat_msg_recv.png')) # img to display
|
|
|
|
|
2009-04-04 07:06:46 +02:00
|
|
|
if gajim.HAVE_INDICATOR and event_type in (_('New Message'),
|
|
|
|
_('New Single Message'), _('New Private Message')):
|
|
|
|
indicator = indicate.IndicatorMessage()
|
|
|
|
indicator.set_property('subtype', 'im')
|
|
|
|
indicator.set_property('sender', jid)
|
|
|
|
indicator.set_property('body', text)
|
|
|
|
indicator.set_property_time('time', time.time())
|
|
|
|
pixbuf = gtk.gdk.pixbuf_new_from_file(path_to_image)
|
|
|
|
indicator.set_property_icon('icon', pixbuf)
|
|
|
|
indicator.connect('user-display', display, account, jid, msg_type)
|
|
|
|
indicator.show()
|
|
|
|
|
2008-11-29 14:37:06 +01:00
|
|
|
# Try to show our popup via D-Bus and notification daemon
|
2005-12-10 01:56:38 +01:00
|
|
|
if gajim.config.get('use_notif_daemon') and dbus_support.supported:
|
2005-11-11 20:12:02 +01:00
|
|
|
try:
|
2006-09-28 00:37:41 +02:00
|
|
|
DesktopNotification(event_type, jid, account, msg_type,
|
2008-12-06 13:43:15 +01:00
|
|
|
path_to_image, title, gobject.markup_escape_text(text))
|
2006-09-28 02:31:59 +02:00
|
|
|
return # sucessfully did D-Bus Notification procedure!
|
2006-12-17 12:43:34 +01:00
|
|
|
except dbus.DBusException, e:
|
2006-09-28 02:31:59 +02:00
|
|
|
# Connection to D-Bus failed
|
2005-12-26 17:41:22 +01:00
|
|
|
gajim.log.debug(str(e))
|
2005-11-11 20:12:02 +01:00
|
|
|
except TypeError, e:
|
|
|
|
# This means that we sent the message incorrectly
|
2005-12-26 17:41:22 +01:00
|
|
|
gajim.log.debug(str(e))
|
2008-11-29 14:37:06 +01:00
|
|
|
|
|
|
|
# Ok, that failed. Let's try pynotify, which also uses notification daemon
|
|
|
|
if gajim.config.get('use_notif_daemon') and USER_HAS_PYNOTIFY:
|
2007-07-13 00:19:21 +02:00
|
|
|
if not text and event_type == 'new_message':
|
|
|
|
# empty text for new_message means do_preview = False
|
2008-11-29 14:37:06 +01:00
|
|
|
# -> default value for text
|
|
|
|
_text = gobject.markup_escape_text(
|
|
|
|
gajim.get_name_from_jid(account, jid))
|
|
|
|
else:
|
|
|
|
_text = gobject.markup_escape_text(text)
|
2008-05-18 23:17:53 +02:00
|
|
|
|
2008-11-29 14:37:06 +01:00
|
|
|
if not title:
|
|
|
|
_title = ''
|
|
|
|
else:
|
2008-12-06 13:43:15 +01:00
|
|
|
_title = title
|
2008-05-18 23:17:53 +02:00
|
|
|
|
2008-11-29 14:37:06 +01:00
|
|
|
notification = pynotify.Notification(_title, _text)
|
2006-09-28 02:31:59 +02:00
|
|
|
timeout = gajim.config.get('notification_timeout') * 1000 # make it ms
|
|
|
|
notification.set_timeout(timeout)
|
2008-05-18 23:17:53 +02:00
|
|
|
|
2006-09-28 02:31:59 +02:00
|
|
|
notification.set_category(event_type)
|
|
|
|
notification.set_data('event_type', event_type)
|
|
|
|
notification.set_data('jid', jid)
|
|
|
|
notification.set_data('account', account)
|
2007-05-14 07:47:44 +02:00
|
|
|
notification.set_data('msg_type', msg_type)
|
2007-05-14 18:09:53 +02:00
|
|
|
notification.set_property('icon-name', path_to_image)
|
2009-03-13 22:51:49 +01:00
|
|
|
if 'actions' in pynotify.get_server_caps():
|
|
|
|
notification.add_action('default', 'Default Action',
|
|
|
|
on_pynotify_notification_clicked)
|
2006-09-28 02:31:59 +02:00
|
|
|
|
2007-01-13 01:52:47 +01:00
|
|
|
try:
|
|
|
|
notification.show()
|
|
|
|
return
|
|
|
|
except gobject.GError, e:
|
|
|
|
# Connection to notification-daemon failed, see #2893
|
|
|
|
gajim.log.debug(str(e))
|
|
|
|
|
2008-11-29 14:37:06 +01:00
|
|
|
# Either nothing succeeded or the user wants old-style notifications
|
2007-01-13 01:52:47 +01:00
|
|
|
instance = dialogs.PopupNotificationWindow(event_type, jid, account,
|
|
|
|
msg_type, path_to_image, title, text)
|
|
|
|
gajim.interface.roster.popup_notification_windows.append(instance)
|
2006-09-28 02:31:59 +02:00
|
|
|
|
|
|
|
def on_pynotify_notification_clicked(notification, action):
|
|
|
|
jid = notification.get_data('jid')
|
|
|
|
account = notification.get_data('account')
|
|
|
|
msg_type = notification.get_data('msg_type')
|
|
|
|
|
|
|
|
notification.close()
|
|
|
|
gajim.interface.handle_event(account, jid, msg_type)
|
2005-11-11 20:12:02 +01:00
|
|
|
|
2005-12-10 01:56:38 +01:00
|
|
|
class NotificationResponseManager:
|
|
|
|
'''Collects references to pending DesktopNotifications and manages there
|
|
|
|
signalling. This is necessary due to a bug in DBus where you can't remove
|
|
|
|
a signal from an interface once it's connected.'''
|
|
|
|
def __init__(self):
|
|
|
|
self.pending = {}
|
2006-03-25 01:59:09 +01:00
|
|
|
self.received = []
|
2005-12-10 01:56:38 +01:00
|
|
|
self.interface = None
|
|
|
|
|
|
|
|
def attach_to_interface(self):
|
|
|
|
if self.interface is not None:
|
|
|
|
return
|
|
|
|
self.interface = dbus_support.get_notifications_interface()
|
|
|
|
self.interface.connect_to_signal('ActionInvoked', self.on_action_invoked)
|
|
|
|
self.interface.connect_to_signal('NotificationClosed', self.on_closed)
|
2005-12-10 23:44:47 +01:00
|
|
|
|
2008-10-11 12:22:04 +02:00
|
|
|
def on_action_invoked(self, id_, reason):
|
|
|
|
self.received.append((id_, time.time(), reason))
|
|
|
|
if id_ in self.pending:
|
|
|
|
notification = self.pending[id_]
|
|
|
|
notification.on_action_invoked(id_, reason)
|
|
|
|
del self.pending[id_]
|
2006-03-25 01:59:09 +01:00
|
|
|
if len(self.received) > 20:
|
|
|
|
curt = time.time()
|
|
|
|
for rec in self.received:
|
|
|
|
diff = curt - rec[1]
|
|
|
|
if diff > 10:
|
|
|
|
self.received.remove(rec)
|
2005-12-10 01:56:38 +01:00
|
|
|
|
2008-10-11 12:22:04 +02:00
|
|
|
def on_closed(self, id_, reason=None):
|
|
|
|
if id_ in self.pending:
|
|
|
|
del self.pending[id_]
|
2005-12-10 23:44:47 +01:00
|
|
|
|
2008-10-11 12:22:04 +02:00
|
|
|
def add_pending(self, id_, object_):
|
2006-03-25 01:59:09 +01:00
|
|
|
# Check to make sure that we handle an event immediately if we're adding
|
|
|
|
# an id that's already been triggered
|
|
|
|
for rec in self.received:
|
2008-10-11 12:22:04 +02:00
|
|
|
if rec[0] == id_:
|
|
|
|
object_.on_action_invoked(id_, rec[2])
|
2006-03-25 01:59:09 +01:00
|
|
|
self.received.remove(rec)
|
|
|
|
return
|
2008-10-11 12:22:04 +02:00
|
|
|
if id_ not in self.pending:
|
2006-03-25 01:59:09 +01:00
|
|
|
# Add it
|
2008-10-11 12:22:04 +02:00
|
|
|
self.pending[id_] = object_
|
2006-03-25 01:59:09 +01:00
|
|
|
else:
|
|
|
|
# We've triggered an event that has a duplicate ID!
|
|
|
|
gajim.log.debug('Duplicate ID of notification. Can\'t handle this.')
|
|
|
|
|
2005-12-10 01:56:38 +01:00
|
|
|
notification_response_manager = NotificationResponseManager()
|
|
|
|
|
|
|
|
class DesktopNotification:
|
2006-09-28 02:31:59 +02:00
|
|
|
'''A DesktopNotification that interfaces with D-Bus via the Desktop
|
2005-12-10 01:56:38 +01:00
|
|
|
Notification specification'''
|
2008-09-22 11:10:18 +02:00
|
|
|
def __init__(self, event_type, jid, account, msg_type='',
|
|
|
|
path_to_image=None, title=None, text=None):
|
2006-03-25 01:59:09 +01:00
|
|
|
self.path_to_image = path_to_image
|
|
|
|
self.event_type = event_type
|
2006-04-03 09:40:15 +02:00
|
|
|
self.title = title
|
2006-03-25 01:59:09 +01:00
|
|
|
self.text = text
|
2008-12-02 15:09:18 +01:00
|
|
|
# 0.3.1 is the only version of notification daemon that has no way
|
|
|
|
# to determine which version it is. If no method exists, it means
|
|
|
|
# they're using that one.
|
2007-01-28 13:53:14 +01:00
|
|
|
self.default_version = [0, 3, 1]
|
2005-12-10 01:56:38 +01:00
|
|
|
self.account = account
|
|
|
|
self.jid = jid
|
|
|
|
self.msg_type = msg_type
|
2005-12-10 23:44:47 +01:00
|
|
|
|
2007-07-10 23:57:56 +02:00
|
|
|
# default value of text
|
|
|
|
if not text and event_type == 'new_message':
|
|
|
|
# empty text for new_message means do_preview = False
|
2006-04-03 09:40:15 +02:00
|
|
|
self.text = gajim.get_name_from_jid(account, jid)
|
|
|
|
|
|
|
|
if not title:
|
|
|
|
self.title = event_type # default value
|
|
|
|
|
2005-12-10 01:56:38 +01:00
|
|
|
if event_type == _('Contact Signed In'):
|
|
|
|
ntype = 'presence.online'
|
|
|
|
elif event_type == _('Contact Signed Out'):
|
|
|
|
ntype = 'presence.offline'
|
|
|
|
elif event_type in (_('New Message'), _('New Single Message'),
|
|
|
|
_('New Private Message')):
|
|
|
|
ntype = 'im.received'
|
|
|
|
elif event_type == _('File Transfer Request'):
|
|
|
|
ntype = 'transfer'
|
|
|
|
elif event_type == _('File Transfer Error'):
|
|
|
|
ntype = 'transfer.error'
|
2009-02-01 11:28:48 +01:00
|
|
|
elif event_type in (_('File Transfer Completed'),
|
|
|
|
_('File Transfer Stopped')):
|
2005-12-10 01:56:38 +01:00
|
|
|
ntype = 'transfer.complete'
|
2006-01-11 22:30:48 +01:00
|
|
|
elif event_type == _('New E-mail'):
|
2006-03-25 01:59:09 +01:00
|
|
|
ntype = 'email.arrived'
|
2006-03-24 21:01:52 +01:00
|
|
|
elif event_type == _('Groupchat Invitation'):
|
|
|
|
ntype = 'im.invitation'
|
2006-05-10 22:29:17 +02:00
|
|
|
elif event_type == _('Contact Changed Status'):
|
2006-05-10 22:25:51 +02:00
|
|
|
ntype = 'presence.status'
|
2006-09-10 12:22:16 +02:00
|
|
|
elif event_type == _('Connection Failed'):
|
|
|
|
ntype = 'connection.failed'
|
2009-02-13 20:35:30 +01:00
|
|
|
elif event_type == _('Subscription request'):
|
|
|
|
ntype = 'subscription.request'
|
|
|
|
elif event_type == _('Unsubscribed'):
|
|
|
|
ntype = 'unsubscribed'
|
2005-12-12 16:15:56 +01:00
|
|
|
else:
|
2006-01-20 18:40:45 +01:00
|
|
|
# default failsafe values
|
2006-03-25 01:59:09 +01:00
|
|
|
self.path_to_image = os.path.abspath(
|
2006-01-20 18:40:45 +01:00
|
|
|
os.path.join(gajim.DATA_DIR, 'pixmaps', 'events',
|
|
|
|
'chat_msg_recv.png')) # img to display
|
|
|
|
ntype = 'im' # Notification Type
|
2005-12-10 23:44:47 +01:00
|
|
|
|
2009-02-01 11:28:48 +01:00
|
|
|
self.notif = dbus_support.get_notifications_interface(self)
|
2005-12-10 01:56:38 +01:00
|
|
|
if self.notif is None:
|
2006-12-17 12:43:34 +01:00
|
|
|
raise dbus.DBusException('unable to get notifications interface')
|
2006-03-25 01:59:09 +01:00
|
|
|
self.ntype = ntype
|
|
|
|
|
2009-02-01 11:28:48 +01:00
|
|
|
if self.kde_notifications:
|
|
|
|
self.attempt_notify()
|
|
|
|
else:
|
2009-04-01 15:39:52 +02:00
|
|
|
self.capabilities = self.notif.GetCapabilities()
|
2009-05-18 16:17:14 +02:00
|
|
|
if self.capabilities is None:
|
|
|
|
self.capabilities = ['actions']
|
2009-02-01 11:28:48 +01:00
|
|
|
self.get_version()
|
2006-03-25 01:59:09 +01:00
|
|
|
|
|
|
|
def attempt_notify(self):
|
2005-12-30 22:37:36 +01:00
|
|
|
timeout = gajim.config.get('notification_timeout') # in seconds
|
2006-03-25 01:59:09 +01:00
|
|
|
ntype = self.ntype
|
2009-02-01 11:28:48 +01:00
|
|
|
if self.kde_notifications:
|
2009-02-01 22:12:11 +01:00
|
|
|
notification_text = ('<html><img src="%(image)s" align=left />' \
|
|
|
|
'%(title)s<br/>%(text)s</html>') % {'title': self.title,
|
2009-02-01 11:28:48 +01:00
|
|
|
'text': self.text, 'image': self.path_to_image}
|
|
|
|
gajim_icon = os.path.abspath(os.path.join(gajim.DATA_DIR, 'pixmaps',
|
|
|
|
'gajim.png'))
|
|
|
|
self.notif.Notify(
|
|
|
|
dbus.String(_('Gajim')), # app_name (string)
|
|
|
|
dbus.UInt32(0), # replaces_id (uint)
|
|
|
|
ntype, # event_id (string)
|
|
|
|
dbus.String(gajim_icon), # app_icon (string)
|
2009-05-18 22:33:09 +02:00
|
|
|
dbus.String(''), # summary (string)
|
2009-02-01 11:28:48 +01:00
|
|
|
dbus.String(notification_text), # body (string)
|
2009-04-01 15:39:52 +02:00
|
|
|
# actions (stringlist)
|
|
|
|
(dbus.String('default'), dbus.String(self.event_type),
|
|
|
|
dbus.String('ignore'), dbus.String(_('Ignore'))),
|
2009-02-01 11:28:48 +01:00
|
|
|
[], # hints (not used in KDE yet)
|
|
|
|
dbus.UInt32(timeout*1000), # timeout (int), in ms
|
|
|
|
reply_handler=self.attach_by_id,
|
|
|
|
error_handler=self.notify_another_way)
|
|
|
|
return
|
|
|
|
version = self.version
|
2007-01-28 13:53:14 +01:00
|
|
|
if version[:2] == [0, 2]:
|
2009-03-13 22:51:49 +01:00
|
|
|
actions = {}
|
|
|
|
if 'actions' in self.capabilities:
|
|
|
|
actions = {'default': 0}
|
2006-01-13 21:33:43 +01:00
|
|
|
try:
|
2006-03-25 01:59:09 +01:00
|
|
|
self.notif.Notify(
|
|
|
|
dbus.String(_('Gajim')),
|
|
|
|
dbus.String(self.path_to_image),
|
2008-05-18 23:17:53 +02:00
|
|
|
dbus.UInt32(0),
|
|
|
|
ntype,
|
2006-03-25 01:59:09 +01:00
|
|
|
dbus.Byte(0),
|
2006-04-03 09:40:15 +02:00
|
|
|
dbus.String(self.title),
|
2006-03-25 01:59:09 +01:00
|
|
|
dbus.String(self.text),
|
|
|
|
[dbus.String(self.path_to_image)],
|
2009-03-13 22:51:49 +01:00
|
|
|
actions,
|
2008-05-18 23:17:53 +02:00
|
|
|
[''],
|
2006-03-25 01:59:09 +01:00
|
|
|
True,
|
|
|
|
dbus.UInt32(timeout),
|
|
|
|
reply_handler=self.attach_by_id,
|
|
|
|
error_handler=self.notify_another_way)
|
2006-01-13 21:33:43 +01:00
|
|
|
except AttributeError:
|
2007-01-28 13:53:14 +01:00
|
|
|
version = [0, 3, 1] # we're actually dealing with the newer version
|
|
|
|
if version > [0, 3]:
|
2007-11-17 23:27:40 +01:00
|
|
|
if gajim.interface.systray_enabled and \
|
|
|
|
gajim.config.get('attach_notifications_to_systray'):
|
2008-11-30 22:37:50 +01:00
|
|
|
x, y = gajim.interface.systray.img_tray.window.get_origin()
|
2008-12-02 16:53:23 +01:00
|
|
|
width, height, = \
|
|
|
|
gajim.interface.systray.img_tray.window.get_geometry()[2:4]
|
2007-11-17 23:27:40 +01:00
|
|
|
pos_x = x + (width / 2)
|
|
|
|
pos_y = y + (height / 2)
|
|
|
|
hints = {'x': pos_x, 'y': pos_y}
|
|
|
|
else:
|
2006-03-25 01:59:09 +01:00
|
|
|
hints = {}
|
2007-11-17 23:27:40 +01:00
|
|
|
if version >= [0, 3, 2]:
|
2006-04-05 20:37:53 +02:00
|
|
|
hints['urgency'] = dbus.Byte(0) # Low Urgency
|
2006-03-25 01:59:09 +01:00
|
|
|
hints['category'] = dbus.String(ntype)
|
2009-04-08 09:31:11 +02:00
|
|
|
# it seems notification-daemon doesn't like empty text
|
2008-12-19 19:48:25 +01:00
|
|
|
if self.text:
|
|
|
|
text = self.text
|
|
|
|
else:
|
|
|
|
text = ' '
|
2009-03-13 22:51:49 +01:00
|
|
|
actions = ()
|
|
|
|
if 'actions' in self.capabilities:
|
2009-03-15 13:11:50 +01:00
|
|
|
actions = (dbus.String('default'), dbus.String(self.event_type))
|
2006-03-25 01:59:09 +01:00
|
|
|
self.notif.Notify(
|
|
|
|
dbus.String(_('Gajim')),
|
2006-04-05 20:37:53 +02:00
|
|
|
dbus.UInt32(0), # this notification does not replace other
|
|
|
|
dbus.String(self.path_to_image),
|
2006-04-03 09:40:15 +02:00
|
|
|
dbus.String(self.title),
|
2008-12-19 19:48:25 +01:00
|
|
|
dbus.String(text),
|
2009-03-13 22:51:49 +01:00
|
|
|
actions,
|
2008-05-18 23:17:53 +02:00
|
|
|
hints,
|
2006-03-25 01:59:09 +01:00
|
|
|
dbus.UInt32(timeout*1000),
|
|
|
|
reply_handler=self.attach_by_id,
|
|
|
|
error_handler=self.notify_another_way)
|
2006-01-26 16:26:20 +01:00
|
|
|
else:
|
2006-03-25 01:59:09 +01:00
|
|
|
self.notif.Notify(
|
|
|
|
dbus.String(_('Gajim')),
|
2008-05-18 23:17:53 +02:00
|
|
|
dbus.String(self.path_to_image),
|
2006-03-25 01:59:09 +01:00
|
|
|
dbus.UInt32(0),
|
2006-04-03 09:40:15 +02:00
|
|
|
dbus.String(self.title),
|
2006-03-25 01:59:09 +01:00
|
|
|
dbus.String(self.text),
|
|
|
|
dbus.String(''),
|
2007-11-17 23:27:40 +01:00
|
|
|
hints,
|
2006-03-25 01:59:09 +01:00
|
|
|
dbus.UInt32(timeout*1000),
|
|
|
|
reply_handler=self.attach_by_id,
|
|
|
|
error_handler=self.notify_another_way)
|
|
|
|
|
2008-10-11 12:22:04 +02:00
|
|
|
def attach_by_id(self, id_):
|
|
|
|
self.id = id_
|
2005-12-10 01:56:38 +01:00
|
|
|
notification_response_manager.attach_to_interface()
|
2006-03-25 01:59:09 +01:00
|
|
|
notification_response_manager.add_pending(self.id, self)
|
|
|
|
|
|
|
|
def notify_another_way(self,e):
|
|
|
|
gajim.log.debug(str(e))
|
|
|
|
gajim.log.debug('Need to implement a new way of falling back')
|
2005-12-10 23:44:47 +01:00
|
|
|
|
2008-10-11 12:22:04 +02:00
|
|
|
def on_action_invoked(self, id_, reason):
|
2005-12-10 01:56:38 +01:00
|
|
|
if self.notif is None:
|
|
|
|
return
|
2008-10-11 12:22:04 +02:00
|
|
|
self.notif.CloseNotification(dbus.UInt32(id_))
|
2005-12-10 01:56:38 +01:00
|
|
|
self.notif = None
|
2008-05-18 23:17:53 +02:00
|
|
|
|
2009-02-01 11:28:48 +01:00
|
|
|
if reason == 'ignore':
|
|
|
|
return
|
|
|
|
|
2005-12-11 11:32:11 +01:00
|
|
|
gajim.interface.handle_event(self.account, self.jid, self.msg_type)
|
2006-03-25 01:59:09 +01:00
|
|
|
|
2008-09-22 11:04:16 +02:00
|
|
|
def version_reply_handler(self, name, vendor, version, spec_version=None):
|
|
|
|
if spec_version:
|
|
|
|
version = spec_version
|
2009-02-07 10:11:40 +01:00
|
|
|
elif vendor == 'Xfce' and version == '0.1.0':
|
|
|
|
version = '0.9'
|
2007-01-28 13:53:14 +01:00
|
|
|
version_list = version.split('.')
|
|
|
|
self.version = []
|
2009-08-05 23:13:47 +02:00
|
|
|
try:
|
|
|
|
while len(version_list):
|
|
|
|
self.version.append(int(version_list.pop(0)))
|
|
|
|
except ValueError:
|
|
|
|
self.version_error_handler_3_x_try(None)
|
2006-03-25 01:59:09 +01:00
|
|
|
self.attempt_notify()
|
|
|
|
|
|
|
|
def get_version(self):
|
|
|
|
self.notif.GetServerInfo(
|
|
|
|
reply_handler=self.version_reply_handler,
|
|
|
|
error_handler=self.version_error_handler_2_x_try)
|
|
|
|
|
|
|
|
def version_error_handler_2_x_try(self, e):
|
2006-04-05 20:37:53 +02:00
|
|
|
self.notif.GetServerInformation(reply_handler=self.version_reply_handler,
|
|
|
|
error_handler=self.version_error_handler_3_x_try)
|
2006-03-25 01:59:09 +01:00
|
|
|
|
|
|
|
def version_error_handler_3_x_try(self, e):
|
|
|
|
self.version = self.default_version
|
|
|
|
self.attempt_notify()
|
2008-07-29 21:49:31 +02:00
|
|
|
|
2008-08-15 05:20:23 +02:00
|
|
|
# vim: se ts=3:
|