[David Flatz] show transports even if show transports is false when they have unread...
[gajim.git] / src / profile_window.py
bloba417ede08bbd90da366d0097dbbdc4e58bc888de
1 # -*- coding:utf-8 -*-
2 ## src/profile_window.py
3 ##
4 ## Copyright (C) 2003-2008 Yann Leboulanger <asterix AT lagaule.org>
5 ## Copyright (C) 2005-2006 Nikos Kouremenos <kourem AT gmail.com>
6 ## Copyright (C) 2006-2008 Jean-Marie Traissard <jim AT lapin.org>
7 ##
8 ## This file is part of Gajim.
9 ##
10 ## Gajim 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 3 only.
14 ## Gajim 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.
19 ## You should have received a copy of the GNU General Public License
20 ## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
23 # THIS FILE IS FOR **OUR** PROFILE (when we edit our INFO)
25 import gtk
26 import gobject
27 import base64
28 import mimetypes
29 import os
31 import gtkgui_helpers
32 import dialogs
33 import vcard
35 from common import gajim
38 class ProfileWindow:
39 '''Class for our information window'''
41 def __init__(self, account):
42 self.xml = gtkgui_helpers.get_glade('profile_window.glade')
43 self.window = self.xml.get_widget('profile_window')
44 self.progressbar = self.xml.get_widget('progressbar')
45 self.statusbar = self.xml.get_widget('statusbar')
46 self.context_id = self.statusbar.get_context_id('profile')
48 self.account = account
49 self.jid = gajim.get_jid_from_account(account)
51 self.dialog = None
52 self.avatar_mime_type = None
53 self.avatar_encoded = None
54 self.message_id = self.statusbar.push(self.context_id,
55 _('Retrieving profile...'))
56 self.update_progressbar_timeout_id = gobject.timeout_add(100,
57 self.update_progressbar)
58 self.remove_statusbar_timeout_id = None
60 # Create Image for avatar button
61 image = gtk.Image()
62 self.xml.get_widget('PHOTO_button').set_image(image)
63 self.xml.signal_autoconnect(self)
64 self.window.show_all()
66 def update_progressbar(self):
67 self.progressbar.pulse()
68 return True # loop forever
70 def remove_statusbar(self, message_id):
71 self.statusbar.remove(self.context_id, message_id)
72 self.remove_statusbar_timeout_id = None
74 def on_profile_window_destroy(self, widget):
75 if self.update_progressbar_timeout_id is not None:
76 gobject.source_remove(self.update_progressbar_timeout_id)
77 if self.remove_statusbar_timeout_id is not None:
78 gobject.source_remove(self.remove_statusbar_timeout_id)
79 del gajim.interface.instances[self.account]['profile']
80 if self.dialog: # Image chooser dialog
81 self.dialog.destroy()
83 def on_profile_window_key_press_event(self, widget, event):
84 if event.keyval == gtk.keysyms.Escape:
85 self.window.destroy()
87 def on_clear_button_clicked(self, widget):
88 # empty the image
89 button = self.xml.get_widget('PHOTO_button')
90 image = button.get_image()
91 image.set_from_pixbuf(None)
92 button.hide()
93 text_button = self.xml.get_widget('NOPHOTO_button')
94 text_button.show()
95 self.avatar_encoded = None
96 self.avatar_mime_type = None
98 def on_set_avatar_button_clicked(self, widget):
99 def on_ok(widget, path_to_file):
100 must_delete = False
101 filesize = os.path.getsize(path_to_file) # in bytes
102 invalid_file = False
103 msg = ''
104 if os.path.isfile(path_to_file):
105 stat = os.stat(path_to_file)
106 if stat[6] == 0:
107 invalid_file = True
108 msg = _('File is empty')
109 else:
110 invalid_file = True
111 msg = _('File does not exist')
112 if not invalid_file and filesize > 16384: # 16 kb
113 try:
114 pixbuf = gtk.gdk.pixbuf_new_from_file(path_to_file)
115 # get the image at 'notification size'
116 # and hope that user did not specify in ACE crazy size
117 scaled_pixbuf = gtkgui_helpers.get_scaled_pixbuf(pixbuf,
118 'tooltip')
119 except gobject.GError, msg: # unknown format
120 # msg should be string, not object instance
121 msg = str(msg)
122 invalid_file = True
123 if invalid_file:
124 if True: # keep identation
125 dialogs.ErrorDialog(_('Could not load image'), msg)
126 return
127 if filesize > 16384:
128 if scaled_pixbuf:
129 path_to_file = os.path.join(gajim.TMP,
130 'avatar_scaled.png')
131 scaled_pixbuf.save(path_to_file, 'png')
132 must_delete = True
134 fd = open(path_to_file, 'rb')
135 data = fd.read()
136 pixbuf = gtkgui_helpers.get_pixbuf_from_data(data)
137 try:
138 # rescale it
139 pixbuf = gtkgui_helpers.get_scaled_pixbuf(pixbuf, 'vcard')
140 except AttributeError: # unknown format
141 dialogs.ErrorDialog(_('Could not load image'))
142 return
143 self.dialog.destroy()
144 self.dialog = None
145 button = self.xml.get_widget('PHOTO_button')
146 image = button.get_image()
147 image.set_from_pixbuf(pixbuf)
148 button.show()
149 text_button = self.xml.get_widget('NOPHOTO_button')
150 text_button.hide()
151 self.avatar_encoded = base64.encodestring(data)
152 # returns None if unknown type
153 self.avatar_mime_type = mimetypes.guess_type(path_to_file)[0]
154 if must_delete:
155 try:
156 os.remove(path_to_file)
157 except OSError:
158 gajim.log.debug('Cannot remove %s' % path_to_file)
160 def on_clear(widget):
161 self.dialog.destroy()
162 self.dialog = None
163 self.on_clear_button_clicked(widget)
165 def on_cancel(widget):
166 self.dialog.destroy()
167 self.dialog = None
169 if self.dialog:
170 self.dialog.present()
171 else:
172 self.dialog = dialogs.AvatarChooserDialog(on_response_ok = on_ok,
173 on_response_cancel = on_cancel, on_response_clear = on_clear)
175 def on_PHOTO_button_press_event(self, widget, event):
176 '''If right-clicked, show popup'''
177 if event.button == 3 and self.avatar_encoded: # right click
178 menu = gtk.Menu()
180 # Try to get pixbuf
181 pixbuf = gtkgui_helpers.get_avatar_pixbuf_from_cache(self.jid,
182 use_local = False)
184 if pixbuf:
185 nick = gajim.config.get_per('accounts', self.account, 'name')
186 menuitem = gtk.ImageMenuItem(gtk.STOCK_SAVE_AS)
187 menuitem.connect('activate',
188 gtkgui_helpers.on_avatar_save_as_menuitem_activate,
189 self.jid, None, nick + '.jpeg')
190 menu.append(menuitem)
191 # show clear
192 menuitem = gtk.ImageMenuItem(gtk.STOCK_CLEAR)
193 menuitem.connect('activate', self.on_clear_button_clicked)
194 menu.append(menuitem)
195 menu.connect('selection-done', lambda w:w.destroy())
196 # show the menu
197 menu.show_all()
198 menu.popup(None, None, None, event.button, event.time)
199 elif event.button == 1: # left click
200 self.on_set_avatar_button_clicked(widget)
202 def set_value(self, entry_name, value):
203 try:
204 self.xml.get_widget(entry_name).set_text(value)
205 except AttributeError:
206 pass
208 def set_values(self, vcard_):
209 button = self.xml.get_widget('PHOTO_button')
210 image = button.get_image()
211 text_button = self.xml.get_widget('NOPHOTO_button')
212 if not 'PHOTO' in vcard_:
213 # set default image
214 image.set_from_pixbuf(None)
215 button.hide()
216 text_button.show()
217 for i in vcard_.keys():
218 if i == 'PHOTO':
219 pixbuf, self.avatar_encoded, self.avatar_mime_type = \
220 vcard.get_avatar_pixbuf_encoded_mime(vcard_[i])
221 if not pixbuf:
222 image.set_from_pixbuf(None)
223 button.hide()
224 text_button.show()
225 continue
226 pixbuf = gtkgui_helpers.get_scaled_pixbuf(pixbuf, 'vcard')
227 image.set_from_pixbuf(pixbuf)
228 button.show()
229 text_button.hide()
230 continue
231 if i == 'ADR' or i == 'TEL' or i == 'EMAIL':
232 for entry in vcard_[i]:
233 add_on = '_HOME'
234 if 'WORK' in entry:
235 add_on = '_WORK'
236 for j in entry.keys():
237 self.set_value(i + add_on + '_' + j + '_entry', entry[j])
238 if isinstance(vcard_[i], dict):
239 for j in vcard_[i].keys():
240 self.set_value(i + '_' + j + '_entry', vcard_[i][j])
241 else:
242 if i == 'DESC':
243 self.xml.get_widget('DESC_textview').get_buffer().set_text(
244 vcard_[i], 0)
245 else:
246 self.set_value(i + '_entry', vcard_[i])
247 if self.update_progressbar_timeout_id is not None:
248 if self.message_id:
249 self.statusbar.remove(self.context_id, self.message_id)
250 self.message_id = self.statusbar.push(self.context_id,
251 _('Information received'))
252 self.remove_statusbar_timeout_id = gobject.timeout_add_seconds(3,
253 self.remove_statusbar, self.message_id)
254 gobject.source_remove(self.update_progressbar_timeout_id)
255 self.progressbar.hide()
256 self.progressbar.set_fraction(0)
257 self.update_progressbar_timeout_id = None
259 def add_to_vcard(self, vcard_, entry, txt):
260 '''Add an information to the vCard dictionary'''
261 entries = entry.split('_')
262 loc = vcard_
263 if len(entries) == 3: # We need to use lists
264 if entries[0] not in loc:
265 loc[entries[0]] = []
266 found = False
267 for e in loc[entries[0]]:
268 if entries[1] in e:
269 e[entries[2]] = txt
270 break
271 else:
272 loc[entries[0]].append({entries[1]: '', entries[2]: txt})
273 return vcard_
274 while len(entries) > 1:
275 if entries[0] not in loc:
276 loc[entries[0]] = {}
277 loc = loc[entries[0]]
278 del entries[0]
279 loc[entries[0]] = txt
280 return vcard_
282 def make_vcard(self):
283 '''make the vCard dictionary'''
284 entries = ['FN', 'NICKNAME', 'BDAY', 'EMAIL_HOME_USERID', 'URL',
285 'TEL_HOME_NUMBER', 'N_FAMILY', 'N_GIVEN', 'N_MIDDLE', 'N_PREFIX',
286 'N_SUFFIX', 'ADR_HOME_STREET', 'ADR_HOME_EXTADR', 'ADR_HOME_LOCALITY',
287 'ADR_HOME_REGION', 'ADR_HOME_PCODE', 'ADR_HOME_CTRY', 'ORG_ORGNAME',
288 'ORG_ORGUNIT', 'TITLE', 'ROLE', 'TEL_WORK_NUMBER', 'EMAIL_WORK_USERID',
289 'ADR_WORK_STREET', 'ADR_WORK_EXTADR', 'ADR_WORK_LOCALITY',
290 'ADR_WORK_REGION', 'ADR_WORK_PCODE', 'ADR_WORK_CTRY']
291 vcard_ = {}
292 for e in entries:
293 txt = self.xml.get_widget(e + '_entry').get_text().decode('utf-8')
294 if txt != '':
295 vcard_ = self.add_to_vcard(vcard_, e, txt)
297 # DESC textview
298 buff = self.xml.get_widget('DESC_textview').get_buffer()
299 start_iter = buff.get_start_iter()
300 end_iter = buff.get_end_iter()
301 txt = buff.get_text(start_iter, end_iter, 0)
302 if txt != '':
303 vcard_['DESC'] = txt.decode('utf-8')
305 # Avatar
306 if self.avatar_encoded:
307 vcard_['PHOTO'] = {'BINVAL': self.avatar_encoded}
308 if self.avatar_mime_type:
309 vcard_['PHOTO']['TYPE'] = self.avatar_mime_type
310 return vcard_
312 def on_ok_button_clicked(self, widget):
313 if self.update_progressbar_timeout_id:
314 # Operation in progress
315 return
316 if gajim.connections[self.account].connected < 2:
317 dialogs.ErrorDialog(_('You are not connected to the server'),
318 _('Without a connection you can not publish your contact '
319 'information.'))
320 return
321 vcard_ = self.make_vcard()
322 nick = ''
323 if 'NICKNAME' in vcard_:
324 nick = vcard_['NICKNAME']
325 from common import pep
326 pep.user_send_nickname(self.account, nick)
327 if nick == '':
328 nick = gajim.config.get_per('accounts', self.account, 'name')
329 gajim.nicks[self.account] = nick
330 gajim.connections[self.account].send_vcard(vcard_)
331 self.message_id = self.statusbar.push(self.context_id,
332 _('Sending profile...'))
333 self.progressbar.show()
334 self.update_progressbar_timeout_id = gobject.timeout_add(100,
335 self.update_progressbar)
337 def vcard_published(self):
338 if self.update_progressbar_timeout_id is not None:
339 gobject.source_remove(self.update_progressbar_timeout_id)
340 self.update_progressbar_timeout_id = None
341 self.window.destroy()
343 def vcard_not_published(self):
344 if self.message_id:
345 self.statusbar.remove(self.context_id, self.message_id)
346 self.message_id = self.statusbar.push(self.context_id,
347 _('Information NOT published'))
348 self.remove_statusbar_timeout_id = gobject.timeout_add_seconds(3,
349 self.remove_statusbar, self.message_id)
350 if self.update_progressbar_timeout_id is not None:
351 gobject.source_remove(self.update_progressbar_timeout_id)
352 self.progressbar.set_fraction(0)
353 self.update_progressbar_timeout_id = None
354 dialogs.InformationDialog(_('vCard publication failed'),
355 _('There was an error while publishing your personal information, '
356 'try again later.'))
358 def on_cancel_button_clicked(self, widget):
359 self.window.destroy()
361 # vim: se ts=3: