Welcome to mirror list, hosted at ThFree Co, Russian Federation.

notify.py « src - dev.gajim.org/gajim/gajim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4b03ecbeb5a3241fbb376ad633d2a13eca487558 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
##	notify.py
##
## Contributors for this file:
## - Yann Le Boulanger <asterix@lagaule.org>
## - Nikos Kouremenos <kourem@gmail.com>
## - Dimitur Kirov <dkirov@gmail.com>
## - Andrew Sayman <lorien420@myrealbox.com>
##
##	Copyright (C) 2003-2005 Gajim Team
##
## DBUS/libnotify connection code:
## Copyright (C) 2005 by Sebastian Estienne
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation; version 2 only.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##

HAS_DBUS = True

try:
	import dbus
except ImportError:
	HAS_DBUS = False

import os
import sys
import gajim
import dialogs

from common import gajim
from common import i18n
i18n.init()
_ = i18n._


def dbus_get_interface():
	try:
		interface = 'org.freedesktop.Notifications'
		path = '/org/freedesktop/Notifications'
		bus = dbus.SessionBus()
		obj = bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus')
		dbus_iface = dbus.Interface(obj, 'org.freedesktop.DBus')
		running_services = dbus_iface.ListNames()
		started = True
		if interface not in running_services:
			# try to start the service (notif-daemon)
			if dbus_iface.StartServiceByName(interface, dbus.UInt32(0)) == 1:
				started = True
			else:
				started = False
		if not started:
			return None
		obj = bus.get_object(interface, path)
		return dbus.Interface(obj, interface)
	except Exception, e:
		return None
	except dbus.DBusException, e:
		# This exception could give useful info about why notification breaks
		print >> sys.stderr, e
		return None

def dbus_available():
	if not HAS_DBUS:
		return False
	if dbus_get_interface() is None:
		return False
	else:
		return True
		
def dbus_notify(event_type, jid, account, msg_type = '', file_props = None):
	if jid in gajim.contacts[account]:
		actor = gajim.get_first_contact_instance_from_jid(account, jid).name
	else:
		actor = jid
	
	# default value of txt
	txt = actor

	img = 'chat.png' # img to display
	ntype = 'im'     # Notification Type

	if event_type == _('Contact Signed In'):
		img = 'online.png'
		ntype = 'presence.online'
	elif event_type == _('Contact Signed Out'):
		img = 'offline.png'
		ntype = 'presence.offline'
	elif event_type in (_('New Message'), _('New Single Message'),
		_('New Private Message')):
		img = 'chat.png' # FIXME: better img and split events
		ntype = 'im.received'
		if event_type == _('New Private Message'):
			room_jid, nick = gajim.get_room_and_nick_from_fjid(jid)
			room_name,t = gajim.get_room_name_and_server_from_room_jid(room_jid)
			txt = _('%(nickname)s in room %(room_name)s has sent you a new message.')\
				% {'nickname': nick, 'room_name': room_name}
		else:
			#we talk about a name here
			txt = _('%s has sent you a new message.') % actor
	elif event_type == _('File Transfer Request'):
		img = 'requested.png' # FIXME: better img
		ntype = 'transfer'
		#we talk about a name here
		txt = _('%s wants to send you a file.') % actor
	elif event_type == _('File Transfer Error'):
		img = 'error.png' # FIXME: better img
		ntype = 'transfer.error'
	elif event_type in (_('File Transfer Completed'), _('File Transfer Stopped')):
		img = 'closed.png' # # FIXME: better img and split events
		ntype = 'transfer.complete'
		if file_props is not None:
			if file_props['type'] == 'r':
				# get the name of the sender, as it is in the roster
				sender = unicode(file_props['sender']).split('/')[0]
				name = gajim.get_first_contact_instance_from_jid( 
					account, sender).name
				filename = os.path.basename(file_props['file-name'])
				if event_type == _('File Transfer Completed'):
					txt = _('You successfully received %(filename)s from %(name)s.')\
						% {'filename': filename, 'name': name}
				else: # ft stopped
					txt = _('File transfer of %(filename)s from %(name)s stopped.')\
						% {'filename': filename, 'name': name}
			else:
				receiver = file_props['receiver']
				if hasattr(receiver, 'jid'):
					receiver = receiver.jid
				receiver = receiver.split('/')[0]
				# get the name of the contact, as it is in the roster
				name = gajim.get_first_contact_instance_from_jid( 
					account, receiver).name
				filename = os.path.basename(file_props['file-name'])
				if event_type == _('File Transfer Completed'):
					txt = _('You successfully sent %(filename)s to %(name)s.')\
						% {'filename': filename, 'name': name}
				else: # ft stopped
					txt = _('File transfer of %(filename)s to %(name)s stopped.')\
						% {'filename': filename, 'name': name}
		else:
			txt = ''

	iconset = gajim.config.get('iconset')
	if not iconset:
		iconset = 'sun'
	# FIXME: use 32x32 or 48x48 someday
	path = os.path.join(gajim.DATA_DIR, 'iconsets', iconset, '16x16', img)
	path = os.path.abspath(path)
	notif = dbus_get_interface()
	if notif is None:
		raise dbus.DBusException()
	notif.Notify(dbus.String(_('Gajim')), 
		dbus.String(path), dbus.UInt32(0), ntype, dbus.Byte(0),
		dbus.String(event_type), dbus.String(txt),
		[dbus.String(path)], [''], [''], True, dbus.UInt32(5))

def notify(event_type, jid, account, msg_type = '', file_props = None):
	if dbus_available() and gajim.config.get('use_notif_daemon'):
		try:
			dbus_notify(event_type, jid, account, msg_type, file_props)
			return
		except dbus.DBusException, e:
			# Connection to DBus failed, try popup
			pass
		except TypeError, e:
			# This means that we sent the message incorrectly
			print >> sys.stderr, e
	instance = dialogs.PopupNotificationWindow(event_type, jid, account,
		msg_type, file_props)
	gajim.interface.roster.popup_notification_windows.append(instance)