root/branches/gajim_0.6.1/plugins/gtkgui/gtkgui.py

Revision 975, 32.2 kB (checked in by nk, 4 years ago)

icons --> iconsets {folder} and new invisible for gossip

  • Property svn:keywords set to LastChangedDate LastChangedRevision LastChangedBy HeadURL Id
Line 
1##      plugins/gtkgui.py
2##
3## Gajim Team:
4## - Yann Le Boulanger <asterix@lagaule.org>
5## - Vincent Hanquez <tab@snarc.org>
6##      - Nikos Kouremenos <kourem@gmail.com>
7##
8##      Copyright (C) 2003-2005 Gajim Team
9##
10## This program is free software; you can redistribute it and/or modify
11## it under the terms of the GNU General Public License as published
12## by the Free Software Foundation; version 2 only.
13##
14## This program is distributed in the hope that it will be useful,
15## but WITHOUT ANY WARRANTY; without even the implied warranty of
16## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17## GNU General Public License for more details.
18##
19
20if __name__ == "__main__":
21        import getopt, pickle, sys, socket
22
23        try:    # Import Psyco if available
24                import psyco
25                psyco.full()
26        except ImportError:
27                pass
28               
29        try:
30                opts, args = getopt.getopt(sys.argv[1:], "p:h", ["help"])
31        except getopt.GetoptError:
32                # print help information and exit:
33                usage()
34                sys.exit(2)
35        port = 8255
36        for o, a in opts:
37                if o == '-p':
38                        port = a
39                if o in ("-h", "--help"):
40                        usage()
41                        sys.exit()
42        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
43        try:
44                sock.connect(('', 8255))
45        except:
46                #TODO: use i18n
47                print "unable to connect to localhost on port ", port
48        else:
49                evp = pickle.dumps(('EXEC_PLUGIN', '', 'gtkgui'))
50                sock.send('<'+evp+'>')
51                sock.close()
52        sys.exit()
53
54import pygtk
55pygtk.require('2.0')
56import gtk
57import gtk.glade
58import pango
59import gobject
60import os
61import time
62import sys
63import Queue
64import sre
65import common.sleepy
66
67class CellRendererImage(gtk.GenericCellRenderer):
68
69        __gproperties__ = {
70                "image": (gobject.TYPE_OBJECT, "Image", 
71                "Image", gobject.PARAM_READWRITE),
72        }
73
74        def __init__(self):
75                self.__gobject_init__()
76                self.image = None
77
78        def do_set_property(self, pspec, value):
79                setattr(self, pspec.name, value)
80
81        def do_get_property(self, pspec):
82                return getattr(self, pspec.name)
83
84        def func(self, model, path, iter, (image, tree)):
85                if model.get_value(iter, 0) == image:
86                        self.redraw = 1
87                        cell_area = tree.get_cell_area(path, tree.get_column(0))
88                        tree.queue_draw_area(cell_area.x, cell_area.y, cell_area.width, \
89                                cell_area.height)
90
91        def animation_timeout(self, tree, image):
92                if image.get_storage_type() == gtk.IMAGE_ANIMATION:
93                        self.redraw = 0
94                        image.get_data('iter').advance()
95                        model = tree.get_model()
96                        model.foreach(self.func, (image, tree))
97                        if self.redraw:
98                                gobject.timeout_add(image.get_data('iter').get_delay_time(), \
99                                        self.animation_timeout, tree, image)
100                        else:
101                                image.set_data('iter', None)
102                               
103        def on_render(self, window, widget, background_area,cell_area, \
104                expose_area, flags):
105                if not self.image:
106                        return
107                pix_rect = gtk.gdk.Rectangle()
108                pix_rect.x, pix_rect.y, pix_rect.width, pix_rect.height = \
109                        self.on_get_size(widget, cell_area)
110
111                pix_rect.x += cell_area.x
112                pix_rect.y += cell_area.y
113                pix_rect.width  -= 2 * self.get_property("xpad")
114                pix_rect.height -= 2 * self.get_property("ypad")
115
116                draw_rect = cell_area.intersect(pix_rect)
117                draw_rect = expose_area.intersect(draw_rect)
118
119                if self.image.get_storage_type() == gtk.IMAGE_ANIMATION:
120                        if not self.image.get_data('iter'):
121                                animation = self.image.get_animation()
122                                self.image.set_data('iter', animation.get_iter())
123                                gobject.timeout_add(self.image.get_data('iter').get_delay_time(), \
124                                        self.animation_timeout, widget, self.image)
125
126                        pix = self.image.get_data('iter').get_pixbuf()
127                elif self.image.get_storage_type() == gtk.IMAGE_PIXBUF:
128                        pix = self.image.get_pixbuf()
129                else:
130                        return
131                window.draw_pixbuf(widget.style.black_gc, pix, \
132                        draw_rect.x-pix_rect.x, draw_rect.y-pix_rect.y, draw_rect.x, \
133                        draw_rect.y+2, draw_rect.width, draw_rect.height, \
134                        gtk.gdk.RGB_DITHER_NONE, 0, 0)
135
136        def on_get_size(self, widget, cell_area):
137                if not self.image:
138                        return 0, 0, 0, 0
139                if self.image.get_storage_type() == gtk.IMAGE_ANIMATION:
140                        animation = self.image.get_animation()
141                        pix = animation.get_iter().get_pixbuf()
142                elif self.image.get_storage_type() == gtk.IMAGE_PIXBUF:
143                        pix = self.image.get_pixbuf()
144                else:
145                        return 0, 0, 0, 0
146                pixbuf_width  = pix.get_width()
147                pixbuf_height = pix.get_height()
148                calc_width  = self.get_property("xpad") * 2 + pixbuf_width
149                calc_height = self.get_property("ypad") * 2 + pixbuf_height
150                x_offset = 0
151                y_offset = 0
152                if cell_area and pixbuf_width > 0 and pixbuf_height > 0:
153                        x_offset = self.get_property("xalign") * (cell_area.width - \
154                                calc_width -  self.get_property("xpad"))
155                        y_offset = self.get_property("yalign") * (cell_area.height - \
156                                calc_height -  self.get_property("ypad"))
157                return x_offset, y_offset, calc_width, calc_height
158
159gobject.type_register(CellRendererImage)
160
161class User:
162        """Information concerning each users"""
163        def __init__(self, *args):
164                if len(args) == 0:
165                        self.jid = ''
166                        self.name = ''
167                        self.groups = []
168                        self.show = ''
169                        self.status = ''
170                        self.sub = ''
171                        self.ask = ''
172                        self.resource = ''
173                        self.priority = 1
174                        self.keyID = ''
175                elif len(args) == 10:
176                        self.jid = args[0]
177                        self.name = args[1]
178                        self.groups = args[2]
179                        self.show = args[3]
180                        self.status = args[4]
181                        self.sub = args[5]
182                        self.ask = args[6]
183                        self.resource = args[7]
184                        self.priority = args[8]
185                        self.keyID = args[9]
186                else: raise TypeError, _('bad arguments')
187
188from tabbed_chat_window import *
189from groupchat_window import *
190from history_window import *
191from roster_window import *
192from systray import *
193from dialogs import *
194from config import *
195
196from common import i18n
197
198_ = i18n._
199APP = i18n.APP
200gtk.glade.bindtextdomain(APP, i18n.DIR)
201gtk.glade.textdomain(APP)
202
203def usage():
204        #TODO: use i18n
205        print 'usage :', sys.argv[0], ' [OPTION]'
206        print '  -p\tport on which the sock plugin listen'
207        print '  -h, --help\tdisplay this help and exit'
208
209
210GTKGUI_GLADE='plugins/gtkgui/gtkgui.glade'
211
212
213class plugin:
214        """Class called by the core in a new thread"""
215
216        class accounts:
217                """Class where are stored the accounts and users in them"""
218                def __init__(self):
219                        self.__accounts = {}
220
221                def add_account(self, account, users=()):
222                        #users must be like (user1, user2)
223                        self.__accounts[account] = users
224
225                def add_user_to_account(self, account, user):
226                        if self.__accounts.has_key(account):
227                                self.__accounts[account].append(user)
228                        else :
229                                return 1
230
231                def get_accounts(self):
232                        return self.__accounts.keys();
233
234                def get_users(self, account):
235                        if self.__accounts.has_key(account):
236                                return self.__accounts[account]
237                        else :
238                                return None
239
240                def which_account(self, user):
241                        for a in self.__accounts.keys():
242                                if user in self.__accounts[a]:
243                                        return a
244                        return None
245
246        def launch_browser_mailer(self, kind, url):
247                #kind = 'url' or 'mail'
248                if self.config['openwith'] == 'gnome-open':
249                        app = 'gnome-open'
250                        args = ['gnome-open']
251                        args.append(url)
252                elif self.config['openwith'] == 'kfmclient exec':
253                        app = 'kfmclient'
254                        args = ['kfmclient', 'exec']
255                elif self.config['openwith'] == 'custom':
256                        if kind == 'url':
257                                conf = self.config['custombrowser']
258                        if kind == 'mail':
259                                conf = self.config['custommailapp']
260                        if conf == '': # if no app is configured
261                                return
262                        args = conf.split()
263                        app = args[0]
264                args.append(url)
265                try:
266                        if os.name == 'posix':
267                                os.spawnvp(os.P_NOWAIT, app, args)
268                        else:
269                                os.spawnv(os.P_NOWAIT, app, args)
270                except:
271                        pass
272
273        def play_timeout(self, pid):
274                pidp, r = os.waitpid(pid, os.WNOHANG)
275                return 0
276                       
277
278        def play_sound(self, event):
279                if os.name != 'posix':
280                        return
281                if self.config['soundplayer'] == '':
282                        return
283                if not self.config[event]:
284                        return
285                file = self.config[event + '_file']
286                if not os.path.exists(file):
287                        return
288                argv = self.config['soundplayer'].split()
289                argv.append(file)
290                pid = os.spawnvp(os.P_NOWAIT, argv[0], argv)
291                pidp, r = os.waitpid(pid, os.WNOHANG)
292                if pidp == 0:
293                        gobject.timeout_add(10000, self.play_timeout, pid)
294
295        def send(self, event, account, data):
296                self.queueOUT.put((event, account, data))
297
298        def wait(self, what):
299                """Wait for a message from Core"""
300                #TODO: timeout
301                temp_q = Queue.Queue(50)
302                while 1:
303                        if not self.queueIN.empty():
304                                ev = self.queueIN.get()
305                                if ev[0] == what and ev[2][0] == 'GtkGui':
306                                        #Restore messages
307                                        while not temp_q.empty():
308                                                ev2 = temp_q.get()
309                                                self.queueIN.put(ev2)
310                                        return ev[2][1]
311                                else:
312                                        #Save messages
313                                        temp_q.put(ev)
314
315        def handle_event_roster(self, account, data):
316                #('ROSTER', account, (state, array))
317                statuss = ['offline', 'online', 'away', 'xa', 'dnd', 'invisible']
318                self.roster.on_status_changed(account, statuss[data[0]])
319                self.roster.mklists(data[1], account)
320                self.roster.draw_roster()
321       
322        def handle_event_warning(self, unused, msg):
323                Warning_dialog(msg)
324       
325        def handle_event_error(self, unused, msg):
326                Error_dialog(msg)
327       
328        def handle_event_status(self, account, status):
329                #('STATUS', account, status)
330                self.roster.on_status_changed(account, status)
331       
332        def handle_event_notify(self, account, array):
333                #('NOTIFY', account, (jid, status, message, resource, priority, keyID,
334                # role, affiliation, real_jid, reason, actor, statusCode))
335                statuss = ['offline', 'error', 'online', 'chat', 'away', 'xa', 'dnd', 'invisible']
336                old_show = 0
337                jid = array[0].split('/')[0]
338                keyID = array[5]
339                resource = array[3]
340                if not resource:
341                        resource = ''
342                priority = array[4]
343                if jid.find("@") <= 0:
344                        #It must be an agent
345                        ji = jid.replace('@', '')
346                else:
347                        ji = jid
348                #Update user
349                if self.roster.contacts[account].has_key(ji):
350                        luser = self.roster.contacts[account][ji]
351                        user1 = None
352                        resources = []
353                        for u in luser:
354                                resources.append(u.resource)
355                                if u.resource == resource:
356                                        user1 = u
357                                        break
358                        if user1:
359                                if user1.show in statuss:
360                                        old_show = statuss.index(user1.show)
361                        else:
362                                user1 = self.roster.contacts[account][ji][0]
363                                if user1.show in statuss:
364                                        old_show = statuss.index(user1.show)
365                                if (resources != [''] and (len(luser) != 1 or 
366                                        luser[0].show != 'offline')) and not jid.find("@") <= 0:
367                                        old_show = 0
368                                        user1 = User(user1.jid, user1.name, user1.groups, user1.show, \
369                                        user1.status, user1.sub, user1.ask, user1.resource, \
370                                                user1.priority, user1.keyID)
371                                        luser.append(user1)
372                                user1.resource = resource
373                        user1.show = array[1]
374                        user1.status = array[2]
375                        user1.priority = priority
376                        user1.keyID = keyID
377                if jid.find("@") <= 0:
378                        #It must be an agent
379                        if self.roster.contacts[account].has_key(ji):
380                                #Update existing iter
381                                self.roster.redraw_jid(ji, account)
382                elif self.roster.contacts[account].has_key(ji):
383                        #It isn't an agent
384                        self.roster.chg_user_status(user1, array[1], array[2], account)
385                        #play sound
386                        if old_show < 2 and statuss.index(user1.show) > 1 and \
387                                self.config['sound_contact_connected']:
388                                self.play_sound('sound_contact_connected')
389                        elif old_show > 1 and statuss.index(user1.show) < 2 and \
390                                self.config['sound_contact_disconnected']:
391                                self.play_sound('sound_contact_disconnected')
392                               
393                elif self.windows[account]['gc'].has_key(ji):
394                        #it is a groupchat presence
395                        self.windows[account]['gc'][ji].chg_user_status(ji, resource, \
396                                array[1], array[2], array[6], array[7], array[8], array[9], \
397                                array[10], array[11], account)
398
399        def handle_event_msg(self, account, array):
400                #('MSG', account, (user, msg, time))
401                jid = array[0].split('/')[0]
402                if jid.find("@") <= 0:
403                        jid = jid.replace('@', '')
404                if self.config['ignore_unknown_contacts'] and \
405                        not self.roster.contacts[account].has_key(jid):
406                        return
407                first = 0
408                if not self.windows[account]['chats'].has_key(jid) and \
409                        not self.queues[account].has_key(jid):
410                        first = 1
411                self.roster.on_message(jid, array[1], array[2], account)
412                if self.config['sound_first_message_received'] and first:
413                        self.play_sound('sound_first_message_received')
414                if self.config['sound_next_message_received'] and not first:
415                        self.play_sound('sound_next_message_received')
416               
417        def handle_event_msgerror(self, account, array):
418                #('MSGERROR', account, (user, error_code, error_msg, msg, time))
419                jid = array[0].split('/')[0]
420                if jid.find("@") <= 0:
421                        jid = jid.replace('@', '')
422                self.roster.on_message(jid, _("error while sending") + \
423                        ' \"%s\" ( %s )' % (array[3], array[2]), array[4], account)
424               
425        def handle_event_msgsent(self, account, array):
426                #('MSG', account, (jid, msg, keyID))
427                self.play_sound('sound_message_sent')
428               
429        def handle_event_subscribe(self, account, array):
430                #('SUBSCRIBE', account, (jid, text))
431                subscription_request_window(self, array[0], array[1], account)
432
433        def handle_event_subscribed(self, account, array):
434                #('SUBSCRIBED', account, (jid, resource))
435                jid = array[0]
436                if self.roster.contacts[account].has_key(jid):
437                        u = self.roster.contacts[account][jid][0]
438                        u.resource = array[1]
439                        self.roster.remove_user(u, account)
440                        if 'not in the roster' in u.groups:
441                                u.groups.remove('not in the roster')
442                        if len(u.groups) == 0:
443                                u.groups = ['General']
444                        self.roster.add_user_to_roster(u.jid, account)
445                        self.send('UPDUSER', account, (u.jid, u.name, u.groups))
446                else:
447                        user1 = User(jid, jid, ['General'], 'online', \
448                                'online', 'to', '', array[1], 0, '')
449                        self.roster.contacts[account][jid] = [user1]
450                        self.roster.add_user_to_roster(jid, account)
451                Information_dialog(_("You are now authorized by %s") % jid)
452
453        def handle_event_unsubscribed(self, account, jid):
454                Information_dialog(_("You are now unsubscribed by %s") % jid)
455
456        def handle_event_agents(self, account, agents):
457                #('AGENTS', account, agents)
458                if self.windows[account].has_key('browser'):
459                        self.windows[account]['browser'].agents(agents)
460
461        def handle_event_agent_info(self, account, array):
462                #('AGENT_INFO', account, (agent, identities, features, items))
463                if self.windows[account].has_key('browser'):
464                        self.windows[account]['browser'].agent_info(array[0], array[1], \
465                                array[2], array[3])
466
467        def handle_event_agent_info_items(self, account, array):
468                #('AGENT_INFO_ITEMS', account, (agent, items))
469                if self.windows[account].has_key('browser'):
470                        self.windows[account]['browser'].agent_info_items(array[0], array[1])
471
472        def handle_event_agent_info_info(self, account, array):
473                #('AGENT_INFO_INFO', account, (agent, identities, features))
474                if self.windows[account].has_key('browser'):
475                        self.windows[account]['browser'].agent_info_info(array[0], array[1], \
476                                array[2])
477
478        def handle_event_reg_agent_info(self, account, array):
479                #('REG_AGENTS_INFO', account, (agent, infos))
480                if not array[1].has_key('instructions'):
481                        Error_dialog(_("error contacting %s") % array[0])
482                else:
483                        Service_registration_window(array[0], array[1], self, account)
484
485        def handle_event_acc_ok(self, account, array):
486                #('ACC_OK', account, (hostname, login, pasword, name, resource, prio,
487                #use_proxy, proxyhost, proxyport))
488                name = array[3]
489                if self.windows['account_modification_window']:
490                        self.windows['account_modification_window'].account_is_ok(array[1])
491                else:
492                        self.accounts[name] = {'name': array[1], \
493                                'hostname': array[0],\
494                                'password': array[2],\
495                                'resource': array[4],\
496                                'priority': array[5],\
497                                'use_proxy': array[6],\
498                                'proxyhost': array[7], \
499                                'proxyport': array[8]}
500                        self.send('CONFIG', None, ('accounts', self.accounts, 'GtkGui'))
501                self.windows[name] = {'infos': {}, 'chats': {}, 'gc': {}}
502                self.queues[name] = {}
503                self.connected[name] = 0
504                self.nicks[name] = array[1]
505                self.roster.groups[name] = {}
506                self.roster.contacts[name] = {}
507                self.sleeper_state[name] = 0
508                if self.windows.has_key('accounts_window'):
509                        self.windows['accounts_window'].init_accounts()
510                self.roster.draw_roster()
511
512        def handle_event_quit(self, p1, p2):
513                self.roster.on_quit()
514
515        def handle_event_myvcard(self, account, array):
516                nick = ''
517                if array.has_key('NICKNAME'):
518                        nick = array['NICKNAME']
519                if nick == '':
520                        nick = self.accounts[account]['name']
521                self.nicks[account] = nick
522
523        def handle_event_vcard(self, account, array):
524                if self.windows[account]['infos'].has_key(array['jid']):
525                        self.windows[account]['infos'][array['jid']].set_values(array)
526
527        def handle_event_log_nb_line(self, account, array):
528                #('LOG_NB_LINE', account, (jid, nb_line))
529                if self.windows['logs'].has_key(array[0]):
530                        self.windows['logs'][array[0]].set_nb_line(array[1])
531                        begin = 0
532                        if array[1] > 50:
533                                begin = array[1] - 50
534                        self.send('LOG_GET_RANGE', None, (array[0], begin, array[1]))
535
536        def handle_event_log_line(self, account, array):
537                #('LOG_LINE', account, (jid, num_line, date, type, data))
538                # if type = 'recv' or 'sent' data = [msg]
539                # else type = jid and data = [status, away_msg]
540                if self.windows['logs'].has_key(array[0]):
541                        self.windows['logs'][array[0]].new_line(array[1:])
542
543        def handle_event_gc_msg(self, account, array):
544                #('GC_MSG', account, (jid, msg, time))
545                jids = array[0].split('/')
546                jid = jids[0]
547                if not self.windows[account]['gc'].has_key(jid):
548                        return
549                if len(jids) == 1:
550                        #message from server
551                        self.windows[account]['gc'][jid].print_conversation(array[1], jid, \
552                                tim = array[