root/branches/gajim_0.9/src/vcard.py

Revision 4700, 16.2 kB (checked in by asterix, 3 years ago)

change copyright from "Gajim Team" to real people

  • Property svn:eol-style set to LF
Line 
1##      vcard.py (has VcardWindow class)
2##
3## Contributors for this file:
4##      - Yann Le Boulanger <asterix@lagaule.org>
5##      - Nikos Kouremenos <kourem@gmail.com>
6##
7## Copyright (C) 2003-2004 Yann Le Boulanger <asterix@lagaule.org>
8##                         Vincent Hanquez <tab@snarc.org>
9## Copyright (C) 2005 Yann Le Boulanger <asterix@lagaule.org>
10##                    Vincent Hanquez <tab@snarc.org>
11##                    Nikos Kouremenos <nkour@jabber.org>
12##                    Dimitur Kirov <dkirov@gmail.com>
13##                    Travis Shirk <travis@pobox.com>
14##                    Norman Rasmussen <norman@rasmussen.co.za>
15##
16## This program is free software; you can redistribute it and/or modify
17## it under the terms of the GNU General Public License as published
18## by the Free Software Foundation; version 2 only.
19##
20## This program is distributed in the hope that it will be useful,
21## but WITHOUT ANY WARRANTY; without even the implied warranty of
22## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23## GNU General Public License for more details.
24##
25
26import gtk
27import gtk.glade
28import gobject
29import urllib
30import base64
31import mimetypes
32import os
33import sys
34import gtkgui_helpers
35import dialogs
36
37from common import helpers
38from common import gajim
39from common import i18n
40_ = i18n._
41Q_ = i18n.Q_
42APP = i18n.APP
43gtk.glade.bindtextdomain (APP, i18n.DIR)
44gtk.glade.textdomain (APP)
45
46GTKGUI_GLADE = 'gtkgui.glade'
47
48def get_avatar_pixbuf_encoded_mime(photo):
49        '''return the pixbuf of the image
50        photo is a dictionary containing PHOTO information'''
51        if not isinstance(photo, dict):
52                return None, None, None
53        img_decoded = None
54        avatar_encoded = None
55        avatar_mime_type = None
56        if photo.has_key('BINVAL') and photo.has_key('TYPE'):
57                img_encoded = photo['BINVAL']
58                avatar_encoded = img_encoded
59                avatar_mime_type = photo['TYPE']
60                try:
61                        img_decoded = base64.decodestring(img_encoded)
62                except:
63                        pass
64        elif photo.has_key('EXTVAL'):
65                url = photo['EXTVAL']
66                try:
67                        fd = urllib.urlopen(url)
68                        img_decoded = fd.read()
69                except:
70                        pass
71        if img_decoded:
72                pixbuf = gtkgui_helpers.get_pixbuf_from_data(img_decoded)
73        else:
74                pixbuf = None
75        return pixbuf, avatar_encoded, avatar_mime_type
76
77class VcardWindow:
78        '''Class for contact's information window'''
79
80        def __init__(self, contact, account, vcard = False):
81                #the contact variable is the jid if vcard is true
82                self.xml = gtk.glade.XML(GTKGUI_GLADE, 'vcard_information_window', APP)
83                self.window = self.xml.get_widget('vcard_information_window')
84                self.xml.get_widget('photo_vbuttonbox').set_no_show_all(True)
85               
86                self.publish_button = self.xml.get_widget('publish_button')
87                self.retrieve_button = self.xml.get_widget('retrieve_button')
88                self.publish_button.set_no_show_all(True)
89                self.retrieve_button.set_no_show_all(True)
90               
91                self.contact = contact #don't use it if vcard is true
92                self.account = account
93                self.vcard = vcard
94                self.avatar_mime_type = None
95                self.avatar_encoded = None
96
97                if vcard:
98                        self.jid = contact
99                        # remove Jabber tab & show publish/retrieve/set_avatar buttons
100                        self.change_to_vcard()
101                else:
102                        self.jid = contact.jid
103                        self.publish_button.hide()
104                        self.retrieve_button.hide()
105                        self.fill_jabber_page()
106
107                self.xml.signal_autoconnect(self)
108                self.window.show_all()
109
110        def on_vcard_information_window_destroy(self, widget):
111                del gajim.interface.instances[self.account]['infos'][self.jid]
112
113        def on_vcard_information_window_key_press_event(self, widget, event):
114                if event.keyval == gtk.keysyms.Escape:
115                        self.window.destroy()
116
117        def on_log_history_checkbutton_toggled(self, widget):
118                #log conversation history?
119                oldlog = True
120                no_log_for = gajim.config.get_per('accounts', self.account,
121                        'no_log_for').split()
122                if self.contact.jid in no_log_for:
123                        oldlog = False
124                log = widget.get_active()
125                if not log and not self.contact.jid in no_log_for:
126                        no_log_for.append(self.contact.jid)
127                if log and self.contact.jid in no_log_for:
128                        no_log_for.remove(self.contact.jid)
129                if oldlog != log:
130                        gajim.config.set_per('accounts', self.account, 'no_log_for',
131                                ' '.join(no_log_for))
132       
133        def on_close_button_clicked(self, widget):
134                '''Save contact information and update the roster on the Jabber server'''
135                if self.vcard:
136                        self.window.destroy()
137                        return
138                #update contact.name if it's not ''
139                name_entry = self.xml.get_widget('nickname_entry')
140                new_name = name_entry.get_text().decode('utf-8')
141                if new_name != self.contact.name and new_name != '':
142                        self.contact.name = new_name
143                        for i in gajim.interface.roster.get_contact_iter(self.contact.jid,
144                                self.account):
145                                gajim.interface.roster.tree.get_model().set_value(i, 1, new_name)
146                        gajim.connections[self.account].update_contact(self.contact.jid,
147                                self.contact.name, self.contact.groups)
148                self.window.destroy()
149
150        def on_clear_button_clicked(self, widget):
151                # empty the image
152                self.xml.get_widget('PHOTO_image').set_from_pixbuf(None)
153                self.avatar_encoded = None
154
155        def image_is_ok(self, image):
156                if not os.path.exists(image):
157                        return False
158                return True
159
160        def update_preview(self, widget):
161                path_to_file = widget.get_preview_filename()
162                if path_to_file is None or os.path.isdir(path_to_file):
163                        # nothing to preview or directory
164                        # make sure you clean image do show nothing
165                        widget.get_preview_widget().set_from_file(None)
166                        return
167                try:
168                        pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(path_to_file, 100, 100)
169                except gobject.GError:
170                        return
171                widget.get_preview_widget().set_from_pixbuf(pixbuf)
172
173        def on_set_avatar_button_clicked(self, widget):
174                f = None
175                dialog = gtk.FileChooserDialog(_('Choose Avatar'), None,
176                        gtk.FILE_CHOOSER_ACTION_OPEN,
177                        (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
178                        gtk.STOCK_OPEN, gtk.RESPONSE_OK))
179                dialog.set_default_response(gtk.RESPONSE_OK)
180                filtr = gtk.FileFilter()
181                filtr.set_name(_('All files'))
182                filtr.add_pattern('*')
183                dialog.add_filter(filtr)
184
185                filtr = gtk.FileFilter()
186                filtr.set_name(_('Images'))
187                filtr.add_mime_type('image/png')
188                filtr.add_mime_type('image/jpeg')
189                filtr.add_mime_type('image/gif')
190                filtr.add_mime_type('image/tiff')
191                filtr.add_mime_type('image/x-xpixmap') # xpm
192                dialog.add_filter(filtr)
193                dialog.set_filter(filtr)
194                dialog.set_use_preview_label(False)
195                dialog.set_preview_widget(gtk.Image())
196                dialog.connect('selection-changed', self.update_preview)
197
198                done = False
199                while not done:
200                        response = dialog.run()
201                        if response == gtk.RESPONSE_OK:
202                                f = dialog.get_filename()
203                                try:
204                                        f = f.decode(sys.getfilesystemencoding())
205                                except UnicodeError:
206                                        pass
207                                else:
208                                        filesize = os.path.getsize(f) # in bytes
209                                        if filesize > 32768: # 32 kb
210                                                dialogs.ErrorDialog(_('The filesize of image "%s" is too large')\
211                                                        % os.path.basename(f),
212                                                _('The file must not be more than 32 kilobytes.')).get_response()
213                                                continue
214                                if self.image_is_ok(f):
215                                        done = True
216                        else: # Cancel or WM X button
217                                done = True
218                dialog.destroy()
219
220                if response == gtk.RESPONSE_OK:
221                        fd = open(f, 'rb')
222                        data = fd.read()
223                        pixbuf = gtkgui_helpers.get_pixbuf_from_data(data)
224                        image = self.xml.get_widget('PHOTO_image')
225                        image.set_from_pixbuf(pixbuf)
226                        self.avatar_encoded = base64.encodestring(data)
227                        # returns None if unknown type
228                        self.avatar_mime_type = mimetypes.guess_type(f)[0]
229
230        def set_value(self, entry_name, value):
231                try:
232                        self.xml.get_widget(entry_name).set_text(value)
233                except AttributeError:
234                        pass
235
236        def set_values(self, vcard):
237                for i in vcard.keys():
238                        if i == 'PHOTO':
239                                pixbuf, self.avatar_encoded, self.avatar_mime_type = \
240                                        get_avatar_pixbuf_encoded_mime(vcard[i])
241                                image = self.xml.get_widget('PHOTO_image')
242                                image.set_from_pixbuf(pixbuf)
243                                continue
244                        if i == 'ADR' or i == 'TEL' or i == 'EMAIL':
245                                for entry in vcard[i]:
246                                        add_on = '_HOME'
247                                        if 'WORK' in entry:
248                                                add_on = '_WORK'
249                                        for j in entry.keys():
250                                                self.set_value(i + add_on + '_' + j + '_entry', entry[j])
251                        if isinstance(vcard[i], dict):
252                                for j in vcard[i].keys():
253                                        self.set_value(i + '_' + j + '_entry', vcard[i][j])
254                        else:
255                                if i == 'DESC':
256                                        self.xml.get_widget('DESC_textview').get_buffer().set_text(
257                                                vcard[i], 0)
258                                else:
259                                        self.set_value(i + '_entry', vcard[i])
260       
261        def set_os_info(self, resource, client_info, os_info):
262                i = 0
263                client = ''
264                os = ''
265                while self.os_info.has_key(i):
266                        if not self.os_info[i]['resource'] or \
267                                        self.os_info[i]['resource'] == resource:
268                                self.os_info[i]['client'] = client_info
269                                self.os_info[i]['os'] = os_info
270                        if i > 0:
271                                client += '\n'
272                                os += '\n'
273                        client += self.os_info[i]['client']
274                        os += self.os_info[i]['os']
275                        i += 1
276
277                if client == '':
278                        client = Q_('?Client:Unknown')
279                if os == '':
280                        os = Q_('?OS:Unknown')
281                self.xml.get_widget('client_name_version_label').set_text(client)
282                self.xml.get_widget('os_label').set_text(os)
283
284        def fill_jabber_page(self):
285                tooltips = gtk.Tooltips()
286                self.xml.get_widget('nickname_label').set_text(self.contact.name)
287                self.xml.get_widget('jid_label').set_text(self.contact.jid)
288                uf_sub = helpers.get_uf_sub(self.contact.sub)
289                self.xml.get_widget('subscription_label').set_text(uf_sub)
290                eb = self.xml.get_widget('subscription_label_eventbox')
291                if self.contact.sub == 'from':
292                        tt_text = _("This contact is interested in your presence information, but you are not interested in his/her presence")
293                elif self.contact.sub == 'to':
294                        tt_text = _("You are interested in the contact's presence information, but he/she is not interested in yours")
295                elif self.contact.sub == 'both':
296                        tt_text = _("You and the contact are interested in each other's presence information")
297                else: # None
298                        tt_text = _("You are not interested in the contact's presence, and neither he/she is interested in yours")
299                tooltips.set_tip(eb, tt_text)
300
301                label = self.xml.get_widget('ask_label')
302                uf_ask = helpers.get_uf_ask(self.contact.ask)
303                label.set_text(uf_ask)
304                eb = self.xml.get_widget('ask_label_eventbox')
305                if self.contact.ask == 'subscribe':
306                        tooltips.set_tip(eb,
307                        _("You are waiting contact's answer about your subscription request"))
308                self.xml.get_widget('nickname_entry').set_text(self.contact.name)
309                log = True
310                if self.contact.jid in gajim.config.get_per('accounts', self.account,
311                        'no_log_for').split(' '):
312                        log = False
313                checkbutton = self.xml.get_widget('log_history_checkbutton')
314                checkbutton.set_active(log)
315                checkbutton.connect('toggled', self.on_log_history_checkbutton_toggled)
316               
317                resources = '%s (%s)' % (self.contact.resource, unicode(
318                        self.contact.priority))
319                uf_resources = self.contact.resource + _(' resource with priority ')\
320                        + unicode(self.contact.priority)
321                if not self.contact.status:
322                        self.contact.status = ''
323               
324                # stats holds show and status message
325                stats = helpers.get_uf_show(self.contact.show)
326                if self.contact.status:
327                        stats += ': ' + self.contact.status
328                gajim.connections[self.account].request_os_info(self.contact.jid,
329                        self.contact.resource)
330                self.os_info = {0: {'resource': self.contact.resource, 'client': '',
331                        'os': ''}}
332                i = 1
333                if gajim.contacts[self.account].has_key(self.contact.jid):
334                        for c in gajim.contacts[self.account][self.contact.jid]:
335                                if c.resource != self.contact.resource:
336                                        resources += '\n%s (%s)' % (c.resource,
337                                                unicode(c.priority))
338                                        uf_resources += '\n' + c.resource + _(' resource with priority ')\
339                                                + unicode(c.priority)
340                                        if not c.status:
341                                                c.status = ''
342                                        stats += '\n' + c.show + ': ' + c.status
343                                        gajim.connections[self.account].request_os_info(self.contact.jid,
344                                                c.resource)
345                                        self.os_info[i] = {'resource': c.resource, 'client': '',
346                                                'os': ''}
347                                        i += 1
348                self.xml.get_widget('resource_prio_label').set_text(resources)
349                tip = gtk.Tooltips()
350                resource_prio_label_eventbox = self.xml.get_widget(
351                        'resource_prio_label_eventbox')
352                tip.set_tip(resource_prio_label_eventbox, uf_resources)
353               
354                tip = gtk.Tooltips()
355                status_label_eventbox = self.xml.get_widget('status_label_eventbox')
356                tip.set_tip(status_label_eventbox, stats)
357                status_label = self.xml.get_widget('status_label')
358                status_label.set_max_width_chars(15)
359                status_label.set_text(stats)
360               
361                gajim.connections[self.account].request_vcard(self.contact.jid)
362
363        def add_to_vcard(self, vcard, entry, txt):
364                '''Add an information to the vCard dictionary'''
365                entries = entry.split('_')
366                loc = vcard
367                if len(entries) == 3: # We need to use lists
368                        if not loc.has_key(entries[0]):
369                                loc[entries[0]] = []
370                        found = False
371                        for e in loc[entries[0]]:
372                                if entries[1] in e:
373                                        found = True
374                                        break
375                        if found:
376                                e[entries[2]] = txt
377                        else:
378                                loc[entries[0]].append({entries[1]: '', entries[2]: txt})
379                        return vcard
380                while len(entries) > 1:
381                        if not loc.has_key(entries[0]):
382                                loc[entries[0]] = {}
383                        loc = loc[entries[0]]
384                        del entries[0]
385                loc[entries[0]] = txt
386                return vcard
387
388        def make_vcard(self):
389                '''make the vCard dictionary'''
390                entries = ['FN', 'NICKNAME', 'BDAY', 'EMAIL_HOME_USERID', 'URL',
391                        'TEL_HOME_NUMBER', 'N_FAMILY', 'N_GIVEN', 'N_MIDDLE', 'N_PREFIX',
392                        'N_SUFFIX', 'ADR_HOME_STREET', 'ADR_HOME_EXTADR', 'ADR_HOME_LOCALITY',
393                        'ADR_HOME_REGION', 'ADR_HOME_PCODE', 'ADR_HOME_CTRY', 'ORG_ORGNAME',
394                        'ORG_ORGUNIT', 'TITLE', 'ROLE', 'TEL_WORK_NUMBER', 'EMAIL_WORK_USERID',
395                        'ADR_WORK_STREET', 'ADR_WORK_EXTADR', 'ADR_WORK_LOCALITY',
396                        'ADR_WORK_REGION', 'ADR_WORK_PCODE', 'ADR_WORK_CTRY']
397                vcard = {}
398                for e in entries: 
399                        txt = self.xml.get_widget(e + '_entry').get_text().decode('utf-8')
400                        if txt != '':
401                                vcard = self.add_to_vcard(vcard, e, txt)
402
403                # DESC textview
404                buff = self.xml.get_widget('DESC_textview').get_buffer()
405                start_iter = buff.get_start_iter()
406                end_iter = buff.get_end_iter()
407                txt = buff.get_text(start_iter, end_iter, 0)
408                if txt != '':
409                        vcard['DESC'] = txt.decode('utf-8')
410
411                # Avatar
412                if self.avatar_encoded:
413                        vcard['PHOTO'] = {'BINVAL': self.avatar_encoded}
414                        if self.avatar_mime_type:
415                                vcard['PHOTO']['TYPE'] = self.avatar_mime_type
416                return vcard
417
418        def on_publish_button_clicked(self, widget):
419                if gajim.connections[self.account].connected < 2:
420                        dialogs.ErrorDialog(_('You are not connected to the server'),
421                        _('Without a connection you can not publish your contact '
422                                'information.')).get_response()
423                        return
424                vcard = self.make_vcard()
425                nick = ''
426                if vcard.has_key('NICKNAME'):
427                        nick = vcard['NICKNAME']
428                if nick == '':
429                        nick = gajim.config.get_per('accounts', self.account, 'name')
430                gajim.nicks[self.account] = nick
431                gajim.connections[self.account].send_vcard(vcard)
432
433        def on_retrieve_button_clicked(self, widget):
434                entries = ['FN', 'NICKNAME', 'BDAY', 'EMAIL_HOME_USERID', 'URL',
435                        'TEL_HOME_NUMBER', 'N_FAMILY', 'N_GIVEN', 'N_MIDDLE', 'N_PREFIX',
436                        'N_SUFFIX', 'ADR_HOME_STREET', 'ADR_HOME_EXTADR', 'ADR_HOME_LOCALITY',
437                        'ADR_HOME_REGION', 'ADR_HOME_PCODE', 'ADR_HOME_CTRY', 'ORG_ORGNAME',
438                        'ORG_ORGUNIT', 'TITLE', 'ROLE', 'ADR_WORK_STREET', 'ADR_WORK_EXTADR',
439                        'ADR_WORK_LOCALITY', 'ADR_WORK_REGION', 'ADR_WORK_PCODE',
440                        'ADR_WORK_CTRY']
441                if gajim.connections[self.account].connected > 1:
442                        # clear all entries
443                        for e in entries:
444                                self.xml.get_widget(e + '_entry').set_text('')
445                        self.xml.get_widget('DESC_textview').get_buffer().set_text('')
446                        self.xml.get_widget('PHOTO_image').set_from_pixbuf(None)
447                        gajim.connections[self.account].request_vcard(self.jid)
448                else:
449                        ErrorDialog(_('You are not connected to the server'),
450                                                _('Without a connection, you can not get your contact information.')).get_response()
451
452        def change_to_vcard(self):
453                self.xml.get_widget('information_notebook').remove_page(0)
454                #FIXME: make this string translatable for .10 [thanks Stian]
455                self.xml.get_widget('nickname_label').set_text('Personal details')
456               
457                self.publish_button.show()
458                self.retrieve_button.show()
459               
460                #photo_vbuttonbox visible
461                self.xml.get_widget('photo_vbuttonbox').show()
462               
463                #make all entries editable
464                entries = ['FN', 'NICKNAME', 'BDAY', 'EMAIL_HOME_USERID', 'URL',
465                        'TEL_HOME_NUMBER', 'N_FAMILY', 'N_GIVEN', 'N_MIDDLE', 'N_PREFIX',
466                        'N_SUFFIX', 'ADR_HOME_STREET', 'ADR_HOME_EXTADR', 'ADR_HOME_LOCALITY',
467                        'ADR_HOME_REGION', 'ADR_HOME_PCODE', 'ADR_HOME_CTRY', 'ORG_ORGNAME',
468                        'ORG_ORGUNIT', 'TITLE', 'ROLE', 'TEL_WORK_NUMBER', 'EMAIL_WORK_USERID',
469                        'ADR_WORK_STREET', 'ADR_WORK_EXTADR', 'ADR_WORK_LOCALITY',
470                        'ADR_WORK_REGION', 'ADR_WORK_PCODE', 'ADR_WORK_CTRY']
471                for e in entries:
472                        self.xml.get_widget(e + '_entry').set_property('editable', True)
473
474                description_textview = self.xml.get_widget('DESC_textview')
475                description_textview.set_editable(True)
476                description_textview.set_cursor_visible(True)
Note: See TracBrowser for help on using the browser.