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

Revision 3319, 14.3 kB (checked in by nk, 3 years ago)

[hawke] fix 2 fields to be editable

Line 
1##      vcard.py (has VcardWindow class)
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
20import gtk
21import gtk.glade
22import urllib
23import base64
24import mimetypes
25import os
26import sys
27import dialogs
28
29from common import helpers
30from common import gajim
31from common import i18n
32_ = i18n._
33Q_ = i18n.Q_
34APP = i18n.APP
35gtk.glade.bindtextdomain (APP, i18n.DIR)
36gtk.glade.textdomain (APP)
37
38GTKGUI_GLADE = 'gtkgui.glade'
39
40class VcardWindow:
41        '''Class for contact's information window'''
42
43        def __init__(self, contact, plugin, account, vcard = False):
44                #the contact variable is the jid if vcard is true
45                self.xml = gtk.glade.XML(GTKGUI_GLADE, 'vcard_information_window', APP)
46                self.window = self.xml.get_widget('vcard_information_window')
47                self.xml.get_widget('photo_vbuttonbox').set_no_show_all(True)
48               
49                self.publish_button = self.xml.get_widget('publish_button')
50                self.retrieve_button = self.xml.get_widget('retrieve_button')
51                self.publish_button.set_no_show_all(True)
52                self.retrieve_button.set_no_show_all(True)
53               
54                self.plugin = plugin
55                self.contact = contact #don't use it if vcard is true
56                self.account = account
57                self.vcard = vcard
58                self.avatar_mime_type = None
59                self.avatar_encoded = None
60
61                if vcard:
62                        self.jid = contact
63                        # remove Jabber tab & show publish/retrieve/set_avatar buttons
64                        self.change_to_vcard()
65                else:
66                        self.jid = contact.jid
67                        self.publish_button.hide()
68                        self.retrieve_button.hide()
69                        self.fill_jabber_page()
70
71                self.xml.signal_autoconnect(self)
72                self.window.show_all()
73
74        def on_vcard_information_window_destroy(self, widget = None):
75                del self.plugin.windows[self.account]['infos'][self.jid]
76
77        def on_vcard_information_window_key_press_event(self, widget, event):
78                if event.keyval == gtk.keysyms.Escape:
79                        self.window.destroy()
80
81        def on_close_button_clicked(self, widget):
82                '''Save contact information and update the roster on the Jabber server'''
83                if self.vcard:
84                        self.window.destroy()
85                        return
86                #update contact.name if it's not ''
87                name_entry = self.xml.get_widget('nickname_entry')
88                new_name = name_entry.get_text().decode('utf-8')
89                if new_name != self.contact.name and new_name != '':
90                        self.contact.name = new_name
91                        for i in self.plugin.roster.get_contact_iter(self.contact.jid, self.account):
92                                self.plugin.roster.tree.get_model().set_value(i, 1, new_name)
93                        gajim.connections[self.account].update_contact(self.contact.jid,
94                                self.contact.name, self.contact.groups)
95                #log history ?
96                oldlog = True
97                no_log_for = gajim.config.get_per('accounts', self.account,
98                        'no_log_for').split()
99                if self.contact.jid in no_log_for:
100                        oldlog = False
101                log = self.xml.get_widget('log_checkbutton').get_active()
102                if not log and not self.contact.jid in no_log_for:
103                        no_log_for.append(self.contact.jid)
104                if log and self.contact.jid in no_log_for:
105                        no_log_for.remove(self.contact.jid)
106                if oldlog != log:
107                        gajim.config.set_per('accounts', self.account, 'no_log_for',
108                                ' '.join(no_log_for))
109                self.window.destroy()
110
111        def on_clear_button_clicked(self, widget):
112                # empty the image
113                self.xml.get_widget('PHOTO_image').set_from_pixbuf(None)
114                self.avatar_encoded = None
115
116        def image_is_ok(self, image):
117                if not os.path.exists(image):
118                        return False
119                return True
120
121        def update_preview(self, widget):
122                path_to_file = widget.get_preview_filename()
123                widget.get_preview_widget().set_from_file(path_to_file)
124
125        def on_set_avatar_button_clicked(self, widget):
126                f = None
127                dialog = gtk.FileChooserDialog(_('Choose Avatar'), None,
128                        gtk.FILE_CHOOSER_ACTION_OPEN,
129                        (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
130                        gtk.STOCK_OPEN, gtk.RESPONSE_OK))
131                dialog.set_default_response(gtk.RESPONSE_OK)
132                filtr = gtk.FileFilter()
133                filtr.set_name(_('All files'))
134                filtr.add_pattern('*')
135                dialog.add_filter(filtr)
136
137                filtr = gtk.FileFilter()
138                filtr.set_name(_('Images'))
139                filtr.add_mime_type('image/png')
140                filtr.add_mime_type('image/jpeg')
141                filtr.add_mime_type('image/gif')
142                filtr.add_pattern('*.png')
143                filtr.add_pattern('*.jpg')
144                filtr.add_pattern('*.gif')
145                filtr.add_pattern('*.tif')
146                filtr.add_pattern('*.xpm')
147                dialog.add_filter(filtr)
148                dialog.set_filter(filtr)
149                dialog.set_use_preview_label(False)
150                dialog.set_preview_widget(gtk.Image())
151                dialog.connect('selection-changed', self.update_preview)
152
153                ok = False
154                while not ok:
155                        response = dialog.run()
156                        if response == gtk.RESPONSE_OK:
157                                f = dialog.get_filename()
158                                f = f.decode(sys.getfilesystemencoding())
159                                if self.image_is_ok(f):
160                                        ok = True
161                        else:
162                                ok = True
163                dialog.destroy()
164
165                if f:
166                        filesize = os.path.getsize(f) # in bytes
167                        if filesize > 8192: # 8 kb
168                                dialogs.ErrorDialog(_('The filesize of image "%s" is too large')\
169                                        % f,
170                                        _('The file must not be more than 8 kilobytes.')).get_response()
171                                return
172                        fd = open(f, 'rb')
173                        data = fd.read()
174                        pixbufloader = gtk.gdk.PixbufLoader()
175                        pixbufloader.write(data)
176                        pixbufloader.close()
177                        pixbuf = pixbufloader.get_pixbuf()
178                        image = self.xml.get_widget('PHOTO_image')
179                        image.set_from_pixbuf(pixbuf)
180                        self.avatar_encoded = base64.encodestring(data)
181                        self.avatar_mime_type = mimetypes.guess_type(f)[0]
182
183        def set_value(self, entry_name, value):
184                try:
185                        self.xml.get_widget(entry_name).set_text(value)
186                except AttributeError:
187                        pass
188
189        def set_values(self, vcard):
190                for i in vcard.keys():
191                        if i == 'PHOTO':
192                                if not isinstance(vcard[i], dict):
193                                        continue
194                                img_decoded = None
195                                if vcard[i].has_key('BINVAL') and vcard[i].has_key('TYPE'):
196                                        img_encoded = vcard[i]['BINVAL']
197                                        self.avatar_encoded = img_encoded
198                                        self.avatar_mime_type = vcard[i]['TYPE']
199                                        try:
200                                                img_decoded = base64.decodestring(img_encoded)
201                                        except:
202                                                pass
203                                elif vcard[i].has_key('EXTVAL'):
204                                        url = vcard[i]['EXTVAL']
205                                        try:
206                                                fd = urllib.urlopen(url)
207                                                img_decoded = fd.read()
208                                        except:
209                                                pass
210                                if img_decoded:
211                                        pixbufloader = gtk.gdk.PixbufLoader()
212                                        pixbufloader.write(img_decoded)
213                                        pixbufloader.close()
214                                        pixbuf = pixbufloader.get_pixbuf()
215                                        image = self.xml.get_widget('PHOTO_image')
216                                        image.set_from_pixbuf(pixbuf)
217                                continue
218                        if i == 'ADR' or i == 'TEL' or i == 'EMAIL':
219                                for entry in vcard[i]:
220                                        add_on = '_HOME'
221                                        if 'WORK' in entry:
222                                                add_on = '_WORK'
223                                        for j in entry.keys():
224                                                self.set_value(i + add_on + '_' + j + '_entry', entry[j])
225                        if isinstance(vcard[i], dict):
226                                for j in vcard[i].keys():
227                                        self.set_value(i + '_' + j + '_entry', vcard[i][j])
228                        else:
229                                if i == 'DESC':
230                                        self.xml.get_widget('DESC_textview').get_buffer().set_text(
231                                                vcard[i], 0)
232                                else:
233                                        self.set_value(i + '_entry', vcard[i])
234       
235        def set_os_info(self, resource, client_info, os_info):
236                i = 0
237                client = ''
238                os = ''
239                while self.os_info.has_key(i):
240                        if not self.os_info[i]['resource'] or \
241                                        self.os_info[i]['resource'] == resource:
242                                self.os_info[i]['client'] = client_info
243                                self.os_info[i]['os'] = os_info
244                        if i > 0:
245                                client += '\n'
246                                os += '\n'
247                        client += self.os_info[i]['client']
248                        os += self.os_info[i]['os']
249                        i += 1
250
251                if client == '':
252                        client = Q_('?Client:Unknown')
253                if os == '':
254                        os = Q_('?OS:Unknown')
255                self.xml.get_widget('client_name_version_label').set_text(client)
256                self.xml.get_widget('os_label').set_text(os)
257
258        def fill_jabber_page(self):
259                self.xml.get_widget('nickname_label').set_text(self.contact.name)
260                self.xml.get_widget('jid_label').set_text(self.contact.jid)
261                uf_sub = helpers.get_uf_sub(self.contact.sub)
262                self.xml.get_widget('subscription_label').set_text(uf_sub)
263                label = self.xml.get_widget('ask_label')
264               
265                uf_ask = helpers.get_uf_ask(self.contact.ask)
266                label.set_text(uf_ask)
267                self.xml.get_widget('nickname_entry').set_text(self.contact.name)
268                log = 1
269                if self.contact.jid in gajim.config.get_per('accounts', self.account,
270                        'no_log_for').split(' '):
271                        log = 0
272                self.xml.get_widget('log_checkbutton').set_active(log)
273                resources = '%s (%s)' % (self.contact.resource, unicode(
274                        self.contact.priority))
275                uf_resources = self.contact.resource + _(' resource with priority ')\
276                        + unicode(self.contact.priority)
277                if not self.contact.status:
278                        self.contact.status = ''
279               
280                # stats holds show and status message
281                stats = helpers.get_uf_show(self.contact.show)
282                if self.contact.status:
283                        stats += ': ' + self.contact.status
284                gajim.connections[self.account].request_os_info(self.contact.jid,
285                        self.contact.resource)
286                self.os_info = {0: {'resource': self.contact.resource, 'client': '',
287                        'os': ''}}
288                i = 1
289                if gajim.contacts[self.account].has_key(self.contact.jid):
290                        for c in gajim.contacts[self.account][self.contact.jid]:
291                                if c.resource != self.contact.resource:
292                                        resources += '\n%s (%s)' % (c.resource,
293                                                unicode(c.priority))
294                                        uf_resources += '\n' + c.resource + _(' resource with priority ')\
295                                                + unicode(c.priority)
296                                        if not c.status:
297                                                c.status = ''
298                                        stats += '\n' + c.show + ': ' + c.status
299                                        gajim.connections[self.account].request_os_info(self.contact.jid,
300                                                c.resource)
301                                        self.os_info[i] = {'resource': c.resource, 'client': '',
302                                                'os': ''}
303                                        i += 1
304                self.xml.get_widget('resource_prio_label').set_text(resources)
305                tip = gtk.Tooltips()
306                resource_prio_label_eventbox = self.xml.get_widget(
307                        'resource_prio_label_eventbox')
308                tip.set_tip(resource_prio_label_eventbox, uf_resources)
309               
310                status_label = self.xml.get_widget('status_label')
311                #FIXME: when gtk2.4 is OOOOLD do it via glade2.10+
312                if gtk.pygtk_version >= (2, 6, 0) and gtk.gtk_version >= (2, 6, 0):
313                        tip = gtk.Tooltips()
314                        status_label_eventbox = self.xml.get_widget('status_label_eventbox')
315                        tip.set_tip(status_label_eventbox, stats)
316                        status_label.set_max_width_chars(15)
317               
318                status_label.set_text(stats)
319               
320                gajim.connections[self.account].request_vcard(self.contact.jid)
321
322        def add_to_vcard(self, vcard, entry, txt):
323                '''Add an information to the vCard dictionary'''
324                entries = entry.split('_')
325                loc = vcard
326                if len(entries) == 3: # We need to use lists
327                        if not loc.has_key(entries[0]):
328                                loc[entries[0]] = []
329                        found = False
330                        for e in loc[entries[0]]:
331                                if entries[1] in e:
332                                        found = True
333                                        break
334                        if found:
335                                e[entries[2]] = txt
336                        else:
337                                loc[entries[0]].append({entries[1]: '', entries[2]: txt})
338                        return vcard
339                while len(entries) > 1:
340                        if not loc.has_key(entries[0]):
341                                loc[entries[0]] = {}
342                        loc = loc[entries[0]]
343                        del entries[0]
344                loc[entries[0]] = txt
345                return vcard
346
347        def make_vcard(self):
348                '''make the vCard dictionary'''
349                entries = ['FN', 'NICKNAME', 'BDAY', 'EMAIL_HOME_USERID', 'URL',
350                        'TEL_HOME_NUMBER', 'N_FAMILY', 'N_GIVEN', 'N_MIDDLE', 'N_PREFIX',
351                        'N_SUFFIX', 'ADR_HOME_STREET', 'ADR_HOME_EXTADR', 'ADR_HOME_LOCALITY',
352                        'ADR_HOME_REGION', 'ADR_HOME_PCODE', 'ADR_HOME_CTRY', 'ORG_ORGNAME',
353                        'ORG_ORGUNIT', 'TITLE', 'ROLE', 'TEL_WORK_NUMBER', 'EMAIL_WORK_USERID',
354                        'ADR_WORK_STREET', 'ADR_WORK_EXTADR', 'ADR_WORK_LOCALITY',
355                        'ADR_WORK_REGION', 'ADR_WORK_PCODE', 'ADR_WORK_CTRY']
356                vcard = {}
357                for e in entries: 
358                        txt = self.xml.get_widget(e + '_entry').get_text().decode('utf-8')
359                        if txt != '':
360                                vcard = self.add_to_vcard(vcard, e, txt)
361
362                # DESC textview
363                buff = self.xml.get_widget('DESC_textview').get_buffer()
364                start_iter = buff.get_start_iter()
365                end_iter = buff.get_end_iter()
366                txt = buff.get_text(start_iter, end_iter, 0)
367                if txt != '':
368                        vcard['DESC'] = txt.decode('utf-8')
369
370                # Avatar
371                if self.avatar_encoded:
372                        vcard['PHOTO'] = {'TYPE': self.avatar_mime_type,
373                                'BINVAL': self.avatar_encoded}
374                return vcard
375
376        def on_publish_button_clicked(self, widget):
377                if gajim.connections[self.account].connected < 2:
378                        ErrorDialog(_('You are not connected to the server'),
379                    _('Without a connection you can not publish your contact information.')).get_response()
380                        return
381                vcard = self.make_vcard()
382                nick = ''
383                if vcard.has_key('NICKNAME'):
384                        nick = vcard['NICKNAME']
385                if nick == '':
386                        nick = gajim.config.get_per('accounts', self.account, 'name')
387                gajim.nicks[self.account] = nick
388                gajim.connections[self.account].send_vcard(vcard)
389
390        def on_retrieve_button_clicked(self, widget):
391                entries = ['FN', 'NICKNAME', 'BDAY', 'EMAIL_HOME_USERID', 'URL',
392                        'TEL_HOME_NUMBER', 'N_FAMILY', 'N_GIVEN', 'N_MIDDLE', 'N_PREFIX',
393                        'N_SUFFIX', 'ADR_HOME_STREET', 'ADR_HOME_EXTADR', 'ADR_HOME_LOCALITY',
394                        'ADR_HOME_REGION', 'ADR_HOME_PCODE', 'ADR_HOME_CTRY', 'ORG_ORGNAME',
395                        'ORG_ORGUNIT', 'TITLE', 'ROLE', 'ADR_WORK_STREET', 'ADR_WORK_EXTADR',
396                        'ADR_WORK_LOCALITY', 'ADR_WORK_REGION', 'ADR_WORK_PCODE',
397                        'ADR_WORK_CTRY']
398                if gajim.connections[self.account].connected > 1:
399                        # clear all entries
400                        for e in entries:
401                                self.xml.get_widget(e + '_entry').set_text('')
402                        self.xml.get_widget('DESC_textview').get_buffer().set_text('')
403                        self.xml.get_widget('PHOTO_image').set_from_pixbuf(None)
404                        gajim.connections[self.account].request_vcard(self.jid)
405                else:
406                        ErrorDialog(_('You are not connected to the server'),
407                                                _('Without a connection, you can not get your contact information.')).get_response()
408
409        def change_to_vcard(self):
410                self.xml.get_widget('information_notebook').remove_page(0)
411                self.xml.get_widget('nickname_label').set_text('Personal details')
412               
413                self.publish_button.show()
414                self.retrieve_button.show()
415               
416                #photo_vbuttonbox visible
417                self.xml.get_widget('photo_vbuttonbox').show()
418               
419                #make all entries editable
420                entries = ['FN', 'NICKNAME', 'BDAY', 'EMAIL_HOME_USERID', 'URL',
421                        'TEL_HOME_NUMBER', 'N_FAMILY', 'N_GIVEN', 'N_MIDDLE', 'N_PREFIX',
422                        'N_SUFFIX', 'ADR_HOME_STREET', 'ADR_HOME_EXTADR', 'ADR_HOME_LOCALITY',
423                        'ADR_HOME_REGION', 'ADR_HOME_PCODE', 'ADR_HOME_CTRY', 'ORG_ORGNAME',
424                        'ORG_ORGUNIT', 'TITLE', 'ROLE', 'TEL_WORK_NUMBER', 'EMAIL_WORK_USERID',
425                        'ADR_WORK_STREET', 'ADR_WORK_EXTADR', 'ADR_WORK_LOCALITY',
426                        'ADR_WORK_REGION', 'ADR_WORK_PCODE', 'ADR_WORK_CTRY']
427                for e in entries:
428                        self.xml.get_widget(e + '_entry').set_property('editable', True)
429
430                description_textview = self.xml.get_widget('DESC_textview')
431                description_textview.set_editable(True)
432                description_textview.set_cursor_visible(True)
Note: See TracBrowser for help on using the browser.