-
Notifications
You must be signed in to change notification settings - Fork 0
/
gajim.py
259 lines (225 loc) · 9.22 KB
/
gajim.py
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#
# gajim.py : A module for the deskbar applet, that allows you to search and
# message users from your Gajim roster.
#
# Copyright (C) 2008 by Matthew Gregg
# Copyright (C) 2007 by Sergey Nazarov
# Copyright (C) 2007 by Jason Chu
#
# 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; either version 2 of the License, or
# (at your option) any later version.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Authors: Matthew Gregg <mcg at no_spam_me butterfat.net>
# Sergey Nazarov <phearnot at no_spam_me gmail.com>
# Incorporates some ideas from:
# http://cixar.com/~segphault/programming/code/samples/aim_deskbar.py.html
#
# 1.0 : Initial release.
# 1.1 : Updated to work with latest dbus and gajim.
# 1.2 : Added escaping
# 1.3 : Rewrite. Can handle transports. '%' character in transport jid was an issue
# 1.4 : +Status icon support
# : +Transport icon support
# : +Ability to change visibility of the offline contacts
# : +Iconset guessed from Gajim's config file
# 1.5 : Fixed issue with Gajim not running
# 1.6 : Works with Deskbar 2.19.6's new module API
# 1.7 : Added caching from Jason Chu <jchu at no_spam_me xentac.net>
# 1.8 : Fix case when gajim is installed in prefix other than /usr
# (Oleg Andreev <[email protected]>).
import sys
import os
def get_gajim_prefix():
for prefix in ['/usr', '/usr/local']:
if os.access(prefix + '/share/gajim', os.X_OK):
return prefix + '/share/gajim'
return None
GAJIM_PREFIX = get_gajim_prefix()
if GAJIM_PREFIX == None:
raise ImportError('Unable to find gajim')
sys.path.append(GAJIM_PREFIX + '/src')
from common import exceptions
from gettext import gettext as _
import cgi, gtk
import deskbar, deskbar.core.Indexer, deskbar.interfaces.Module, deskbar.core.Utils, deskbar.interfaces.Match
from deskbar.core.GconfStore import GconfStore
import datetime
GCONF_GAJIM_SHOWOFFLINE = GconfStore.GCONF_DIR+'/gajim/show_offline'
HANDLERS = [ 'GajimHandler']
VERSION = '1.8'
STATUSES = [
'online', 'offline', 'away', 'chat',
'dnd', 'invisible', 'xa'
]
TRANSPORTS = [
'icq', 'aim', 'gadu-gadu', 'irc', 'msn', 'sms', 'tlen', 'weather', 'yahoo'
]
try:
import dbus
import dbus.service
import dbus.glib
except:
raise exceptions.DbusNotSuported
try:
PREFERRED_ENCODING = locale.getpreferredencoding()
except:
sys.exc_clear()
PREFERRED_ENCODING = 'UTF-8'
OBJ_PATH = '/org/gajim/dbus/RemoteObject'
INTERFACE = 'org.gajim.dbus.RemoteInterface'
SERVICE = 'org.gajim.dbus'
BASENAME = 'gajim-deskbar'
try:
sbus = dbus.SessionBus()
except:
raise exceptions.SessionBusNotPresent
iconset = 'dcraven'
HANDLER_ICON = GAJIM_PREFIX + '/data/iconsets/dcraven/16x16/online.png'
ICON_PATH = GAJIM_PREFIX + '/data/iconsets/'
ICON_TRANSPORTS = GAJIM_PREFIX + '/data/iconsets/transports/'
class GajimAction(deskbar.interfaces.Action):
def __init__(self, jid=None, name=None, interface=None):
deskbar.interfaces.Action.__init__(self, name)
self.jid = jid
self.name = name
self.interface = interface
def get_verb(self):
if self.jid:
return _('Open chat with <b>%(name)s</b> (%(jid)s)')
else:
return _('Open chat with <b>%s</b>') % self.name
def get_name(self, text=None):
return {
'name': cgi.escape(self.name),
'jid': self.jid,
}
def activate(self, text=None):
self.interface.open_chat(dbus.String(self.jid),dbus.String(''))
class GajimMatch(deskbar.interfaces.Match):
def __init__(self, name=None, jid=None, icon=None, interface=None):
deskbar.interfaces.Match.__init__(self, name=name, category='people', icon=icon)
self.name = name
self.jid = jid
self.interface = interface
self.add_action(GajimAction(self.jid, self.name, self.interface))
def get_category(self):
return 'people'
def get_hash(self, text=None):
return 'gajim ' + self.name
class GajimHandler(deskbar.interfaces.Module):
INFOS = {'icon': deskbar.core.Utils.load_icon(HANDLER_ICON),
'name': _('Gajim Roster'),
'description': _('See who is online and start a conversation'),
'version': VERSION,
}
def __init__ (self):
deskbar.interfaces.Module.__init__(self)
def initialize(self):
self.obj = None
self.interface = None
confpath = os.path.expanduser('~') + '/.gajim/config'
conffile = open(confpath, 'r')
for line in conffile.readlines():
if line.startswith('iconset = '):
iconset = line[10:].strip()
break
conffile.close()
self.icons = {}
self.transport_icons = {}
for transport in TRANSPORTS:
self.transport_icons[transport] = {}
for status in STATUSES:
for transport in TRANSPORTS:
self.transport_icons[transport][status] = '%s%s%s' % (ICON_TRANSPORTS, transport, '/16x16/' + status + '.png')
self.icons[status] = '%s%s%s' % (ICON_PATH, iconset, '/16x16/' + status + '.png')
self.lastrequest = None
self.lastroster = None
def get_cache(self, query):
return self.lastroster
def list_contacts(self, query):
self.lastroster = self.interface.list_contacts(query)
self.lastrequest = datetime.datetime.now()
return self.lastroster
def query(self, query):
try:
self.obj = sbus.get_object(SERVICE, OBJ_PATH)
self.interface = dbus.Interface(self.obj, INTERFACE)
except:
print sys.exc_info()
print 'Unable to connect to Gajim - probably it isn\'t running'
return []
t = GconfStore.get_instance().get_client().get_bool(GCONF_GAJIM_SHOWOFFLINE)
if t != None:
self.show_offline = t
else:
self.show_offline = False
GconfStore.get_instance().get_client().set_bool(GCONF_GAJIM_SHOWOFFLINE, False)
message = ''
if self.lastrequest != None and datetime.datetime.now() - self.lastrequest < datetime.timedelta(seconds=20):
self.method = self.get_cache
message = 'asking Cache for roster'
else:
self.method = self.list_contacts
message = 'asking Gajim for roster'
try:
print message
res = self.method(dbus.String(''))
results = []
for buddy in res:
name = buddy['name']
jid = buddy['jid']
status = buddy['show']
if self.show_offline or (status != 'offline'):
if query in name.lower() or query in jid.lower():
if '@' in jid.lower():
transport = jid.split('@')[1].split('.')[0]
else:
transport = jid.split('.')[0]
if transport in TRANSPORTS:
icon = self.transport_icons[transport][status]
else:
icon = self.icons[status]
if buddy['name'] != '':
results += [GajimMatch(name=name, jid=jid, icon=icon, interface=self.interface)]
else:
results += [GajimMatch(name=jid.split('@')[0], jid=jid, icon=icon, interface=self.interface)]
self._emit_query_ready(query, results)
except:
print sys.exc_info()
return []
def has_config(self):
return True
def show_config(self, parent):
dialog = gtk.Dialog(_('Gajim Configuration'), parent,
gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
(gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
dialog.set_geometry_hints(min_width=250, min_height=100)
table = gtk.Table(rows=1, columns=1)
show_offline = gtk.CheckButton('Show offline contacts')
show_offline.set_mode(True)
t = GconfStore.get_instance().get_client().get_bool(GCONF_GAJIM_SHOWOFFLINE)
if t != None:
show_offline.set_active(t)
table.attach(show_offline, 1, 2, 1, 2)
table.show_all()
dialog.vbox.add(table)
response = dialog.run()
if response == gtk.RESPONSE_ACCEPT:
GconfStore.get_instance().get_client().set_bool(GCONF_GAJIM_SHOWOFFLINE, show_offline.get_active())
dialog.destroy()
@staticmethod
def has_requirements():
GajimHandler.INSTRUCTIONS = _('You can configure the handler to show or hide offline contacts.')
return True