| 1 | ## vcard.py (has Vcard_window 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 | |
|---|
| 20 | import gtk |
|---|
| 21 | import gtk.glade |
|---|
| 22 | from common import gajim |
|---|
| 23 | from common import i18n |
|---|
| 24 | _ = i18n._ |
|---|
| 25 | APP = i18n.APP |
|---|
| 26 | gtk.glade.bindtextdomain (APP, i18n.DIR) |
|---|
| 27 | gtk.glade.textdomain (APP) |
|---|
| 28 | |
|---|
| 29 | GTKGUI_GLADE = 'gtkgui.glade' |
|---|
| 30 | |
|---|
| 31 | class Vcard_window: |
|---|
| 32 | '''Class for contact's information window''' |
|---|
| 33 | def on_user_information_window_destroy(self, widget = None): |
|---|
| 34 | del self.plugin.windows[self.account]['infos'][self.jid] |
|---|
| 35 | |
|---|
| 36 | def on_vcard_information_window_key_press_event(self, widget, event): |
|---|
| 37 | if event.keyval == gtk.keysyms.Escape: # ESCAPE |
|---|
| 38 | self.window.destroy() |
|---|
| 39 | |
|---|
| 40 | def on_close_button_clicked(self, widget): |
|---|
| 41 | '''Save user's informations and update the roster on the Jabber server''' |
|---|
| 42 | if self.vcard: |
|---|
| 43 | self.window.destroy() |
|---|
| 44 | return |
|---|
| 45 | #update user.name if it's not '' |
|---|
| 46 | name_entry = self.xml.get_widget('nickname_entry') |
|---|
| 47 | new_name = name_entry.get_text() |
|---|
| 48 | if new_name != self.user.name and new_name != '': |
|---|
| 49 | self.user.name = new_name |
|---|
| 50 | for i in self.plugin.roster.get_user_iter(self.user.jid, self.account): |
|---|
| 51 | self.plugin.roster.tree.get_model().set_value(i, 1, new_name) |
|---|
| 52 | gajim.connections[self.account].update_user(self.user.jid, |
|---|
| 53 | self.user.name, self.user.groups) |
|---|
| 54 | #log history ? |
|---|
| 55 | oldlog = 1 |
|---|
| 56 | no_log_for = gajim.config.get_per('accounts', self.account, |
|---|
| 57 | 'no_log_for').split() |
|---|
| 58 | if self.user.jid in no_log_for: |
|---|
| 59 | oldlog = 0 |
|---|
| 60 | log = self.xml.get_widget('log_checkbutton').get_active() |
|---|
| 61 | if not log and not self.user.jid in no_log_for: |
|---|
| 62 | no_log_for.append(self.user.jid) |
|---|
| 63 | if log and self.user.jid in no_log_for: |
|---|
| 64 | no_log_for.remove(self.user.jid) |
|---|
| 65 | if oldlog != log: |
|---|
| 66 | gajim.config.set_per('accounts', self.account, 'no_log_for', |
|---|
| 67 | ' '.join(no_log_for)) |
|---|
| 68 | self.window.destroy() |
|---|
| 69 | |
|---|
| 70 | def set_value(self, entry_name, value): |
|---|
| 71 | try: |
|---|
| 72 | self.xml.get_widget(entry_name).set_text(value) |
|---|
| 73 | except AttributeError, e: |
|---|
| 74 | pass |
|---|
| 75 | |
|---|
| 76 | def set_values(self, vcard): |
|---|
| 77 | for i in vcard.keys(): |
|---|
| 78 | if type(vcard[i]) == type({}): |
|---|
| 79 | for j in vcard[i].keys(): |
|---|
| 80 | self.set_value(i + '_' + j + |
|---|
| 81 | '_entry', vcard[i][j]) |
|---|
| 82 | else: |
|---|
| 83 | if i == 'DESC': |
|---|
| 84 | self.xml.get_widget('DESC_textview').get_buffer().set_text( |
|---|
| 85 | vcard[i], 0) |
|---|
| 86 | else: |
|---|
| 87 | self.set_value(i + '_entry', vcard[i]) |
|---|
| 88 | |
|---|
| 89 | def set_os_info(self, resource, client_info, os_info): |
|---|
| 90 | i = 0 |
|---|
| 91 | client = '' |
|---|
| 92 | os = '' |
|---|
| 93 | while self.os_info.has_key(i): |
|---|
| 94 | if self.os_info[i]['resource'] == resource: |
|---|
| 95 | self.os_info[i]['client'] = client_info |
|---|
| 96 | self.os_info[i]['os'] = os_info |
|---|
| 97 | if i > 0: |
|---|
| 98 | client += '\n' |
|---|
| 99 | os += '\n' |
|---|
| 100 | client += self.os_info[i]['client'] |
|---|
| 101 | os += self.os_info[i]['os'] |
|---|
| 102 | i += 1 |
|---|
| 103 | |
|---|
| 104 | if client == '': |
|---|
| 105 | client = 'N/A' |
|---|
| 106 | if os == '': |
|---|
| 107 | os = 'N/A' |
|---|
| 108 | self.xml.get_widget('client_name_version_label').set_text(client) |
|---|
| 109 | self.xml.get_widget('os_label').set_text(os) |
|---|
| 110 | |
|---|
| 111 | def fill_jabber_page(self): |
|---|
| 112 | self.xml.get_widget('nickname_label').set_text(self.user.name) |
|---|
| 113 | self.xml.get_widget('jid_label').set_text(self.user.jid) |
|---|
| 114 | self.xml.get_widget('subscription_label').set_text(self.user.sub) |
|---|
| 115 | label = self.xml.get_widget('ask_label') |
|---|
| 116 | if self.user.ask: |
|---|
| 117 | label.set_text(self.user.ask) |
|---|
| 118 | else: |
|---|
| 119 | label.set_text('None') |
|---|
| 120 | self.xml.get_widget('nickname_entry').set_text(self.user.name) |
|---|
| 121 | log = 1 |
|---|
| 122 | if self.user.jid in gajim.config.get_per('accounts', self.account, |
|---|
| 123 | 'no_log_for').split(' '): |
|---|
| 124 | log = 0 |
|---|
| 125 | self.xml.get_widget('log_checkbutton').set_active(log) |
|---|
| 126 | resources = self.user.resource + ' (' + str(self.user.priority) + ')' |
|---|
| 127 | if not self.user.status: |
|---|
| 128 | self.user.status = '' |
|---|
| 129 | stats = self.user.show |
|---|
| 130 | if self.user.status: |
|---|
| 131 | stats += ': ' + self.user.status |
|---|
| 132 | gajim.connections[self.account].request_os_info(self.user.jid, |
|---|
| 133 | self.user.resource) |
|---|
| 134 | self.os_info = {0: {'resource': self.user.resource, 'client': '', |
|---|
| 135 | 'os': ''}} |
|---|
| 136 | i = 1 |
|---|
| 137 | for u in self.plugin.roster.contacts[self.account][self.user.jid]: |
|---|
| 138 | if u.resource != self.user.resource: |
|---|
| 139 | resources += '\n' + u.resource + ' (' + str(u.priority) + ')' |
|---|
| 140 | if not u.status: |
|---|
| 141 | u.status = '' |
|---|
| 142 | stats += '\n' + u.show + ': ' + u.status |
|---|
| 143 | gajim.connections[self.account].request_os_info(self.user.jid, |
|---|
| 144 | u.resource) |
|---|
| 145 | self.os_info[i] = {'resource': u.resource, 'client': '', |
|---|
| 146 | 'os': ''} |
|---|
| 147 | i += 1 |
|---|
| 148 | self.xml.get_widget('resource_label').set_text(resources) |
|---|
| 149 | self.xml.get_widget('status_label').set_text(stats) |
|---|
| 150 | gajim.connections[self.account].request_vcard(self.user.jid) |
|---|
| 151 | |
|---|
| 152 | def add_to_vcard(self, vcard, entry, txt): |
|---|
| 153 | '''Add an information to the vCard dictionary''' |
|---|
| 154 | entries = entry.split('_') |
|---|
| 155 | loc = vcard |
|---|
| 156 | while len(entries) > 1: |
|---|
| 157 | if not loc.has_key(entries[0]): |
|---|
| 158 | loc[entries[0]] = {} |
|---|
| 159 | loc = loc[entries[0]] |
|---|
| 160 | del entries[0] |
|---|
| 161 | loc[entries[0]] = txt |
|---|
| 162 | return vcard |
|---|
| 163 | |
|---|
| 164 | def make_vcard(self): |
|---|
| 165 | '''make the vCard dictionary''' |
|---|
| 166 | entries = ['FN', 'NICKNAME', 'BDAY', 'EMAIL_USERID', 'URL', 'TEL_NUMBER', |
|---|
| 167 | 'ADR_STREET', 'ADR_EXTADR', 'ADR_LOCALITY', 'ADR_REGION', 'ADR_PCODE', |
|---|
| 168 | 'ADR_CTRY', 'ORG_ORGNAME', 'ORG_ORGUNIT', 'TITLE', 'ROLE'] |
|---|
| 169 | vcard = {} |
|---|
| 170 | for e in entries: |
|---|
| 171 | txt = self.xml.get_widget(e + '_entry').get_text() |
|---|
| 172 | if txt != '': |
|---|
| 173 | vcard = self.add_to_vcard(vcard, e, txt) |
|---|
| 174 | buffer = self.xml.get_widget('DESC_textview').get_buffer() |
|---|
| 175 | start_iter = buffer.get_start_iter() |
|---|
| 176 | end_iter = buffer.get_end_iter() |
|---|
| 177 | txt = buffer.get_text(start_iter, end_iter, 0) |
|---|
| 178 | if txt != '': |
|---|
| 179 | vcard['DESC'] = txt |
|---|
| 180 | return vcard |
|---|
| 181 | |
|---|
| 182 | def on_publish_button_clicked(self, widget): |
|---|
| 183 | if gajim.connections[self.account].connected < 2: |
|---|
| 184 | Error_dialog(_('You must be connected to publish your contact information')) |
|---|
| 185 | return |
|---|
| 186 | vcard = self.make_vcard() |
|---|
| 187 | nick = '' |
|---|
| 188 | if vcard.has_key('NICKNAME'): |
|---|
| 189 | nick = vcard['NICKNAME'] |
|---|
| 190 | if nick == '': |
|---|
| 191 | nick = gajim.config.get_per('accounts', self.account, 'name') |
|---|
| 192 | self.plugin.nicks[self.account] = nick |
|---|
| 193 | gajim.connections[self.account].send_vcard(vcard) |
|---|
| 194 | |
|---|
| 195 | def on_retrieve_button_clicked(self, widget): |
|---|
| 196 | entries = ['FN', 'NICKNAME', 'BDAY', 'EMAIL_USERID', 'URL', 'TEL_NUMBER', |
|---|
| 197 | 'ADR_STREET', 'ADR_EXTADR', 'ADR_LOCALITY', 'ADR_REGION', 'ADR_PCODE', |
|---|
| 198 | 'ADR_CTRY', 'ORG_ORGNAME', 'ORG_ORGUNIT', 'TITLE', 'ROLE'] |
|---|
| 199 | if gajim.connections[self.account].connected > 1: |
|---|
| 200 | # clear all entries |
|---|
| 201 | for e in entries: |
|---|
| 202 | self.xml.get_widget(e + '_entry').set_text('') |
|---|
| 203 | self.xml.get_widget('DESC_textview').get_buffer().set_text('') |
|---|
| 204 | gajim.connections[self.account].request_vcard(self.jid) |
|---|
| 205 | else: |
|---|
| 206 | Error_dialog(_('You must be connected to get your contact information')) |
|---|
| 207 | |
|---|
| 208 | def change_to_vcard(self): |
|---|
| 209 | self.xml.get_widget('information_notebook').remove_page(0) |
|---|
| 210 | self.xml.get_widget('nickname_label').set_text('Personal details') |
|---|
| 211 | information_hbuttonbox = self.xml.get_widget('information_hbuttonbox') |
|---|
| 212 | #publish button |
|---|
| 213 | button = gtk.Button(stock = gtk.STOCK_GOTO_TOP) |
|---|
| 214 | button.get_children()[0].get_children()[0].get_children()[1].set_text( |
|---|
| 215 | 'Publish') |
|---|
| 216 | button.connect('clicked', self.on_publish_button_clicked) |
|---|
| 217 | button.show_all() |
|---|
| 218 | information_hbuttonbox.pack_start(button) |
|---|
| 219 | #retrieve button |
|---|
| 220 | button = gtk.Button(stock = gtk.STOCK_GOTO_BOTTOM) |
|---|
| 221 | button.get_children()[0].get_children()[0].get_children()[1].set_text( |
|---|
| 222 | 'Retrieve') |
|---|
| 223 | button.connect('clicked', self.on_retrieve_button_clicked) |
|---|
| 224 | button.show_all() |
|---|
| 225 | information_hbuttonbox.pack_start(button) |
|---|
| 226 | #close button at the end |
|---|
| 227 | button = self.xml.get_widget('close_button') |
|---|
| 228 | information_hbuttonbox.reorder_child(button, 2) |
|---|
| 229 | |
|---|
| 230 | #make all entries editable |
|---|
| 231 | entries = ['FN', 'NICKNAME', 'BDAY', 'EMAIL_USERID', 'URL', 'TEL_NUMBER', |
|---|
| 232 | 'ADR_STREET', 'ADR_EXTADR', 'ADR_LOCALITY', 'ADR_REGION', 'ADR_PCODE', |
|---|
| 233 | 'ADR_CTRY', 'ORG_ORGNAME', 'ORG_ORGUNIT', 'TITLE', 'ROLE'] |
|---|
| 234 | for e in entries: |
|---|
| 235 | self.xml.get_widget(e + '_entry').set_property('editable', True) |
|---|
| 236 | |
|---|
| 237 | description_textview = self.xml.get_widget('DESC_textview') |
|---|
| 238 | description_textview.set_editable(True) |
|---|
| 239 | description_textview.set_cursor_visible(True) |
|---|
| 240 | |
|---|
| 241 | #the user variable is the jid if vcard is true |
|---|
| 242 | def __init__(self, user, plugin, account, vcard = False): |
|---|
| 243 | self.xml = gtk.glade.XML(GTKGUI_GLADE, 'vcard_information_window', APP) |
|---|
| 244 | self.window = self.xml.get_widget('vcard_information_window') |
|---|
| 245 | self.plugin = plugin |
|---|
| 246 | self.user = user #don't use it if vcard is true |
|---|
| 247 | self.account = account |
|---|
| 248 | self.vcard = vcard |
|---|
| 249 | |
|---|
| 250 | if vcard: |
|---|
| 251 | self.jid = user |
|---|
| 252 | self.change_to_vcard() |
|---|
| 253 | else: |
|---|
| 254 | self.jid = user.jid |
|---|
| 255 | self.fill_jabber_page() |
|---|
| 256 | |
|---|
| 257 | self.xml.signal_autoconnect(self) |
|---|
| 258 | self.window.show_all() |
|---|