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

Revision 2994, 14.1 kB (checked in by nk, 3 years ago)

using comments and Q_() for make disctioning of None and Unknown strings. this breaks strings freeze but I hope it is for the good and I hope it is the last time [sorry ppl]

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