Welcome to mirror list, hosted at ThFree Co, Russian Federation.

profile_window.py « src - dev.gajim.org/gajim/gajim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 78bbfb56167b8ac27762bad12414df23d41b57a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
##	profile_window.py
##
## Copyright (C) 2003-2007 Yann Leboulanger <asterix@lagaule.org>
## Copyright (C) 2005-2006 Nikos Kouremenos <kourem@gmail.com>
##
## This file is part of Gajim.
##
## Gajim is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation; version 3 only.
##
## Gajim is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Gajim.  If not, see <http://www.gnu.org/licenses/>.
##

# THIS FILE IS FOR **OUR** PROFILE (when we edit our INFO)

import gtk
import gobject
import base64
import mimetypes
import os

import gtkgui_helpers
import dialogs
import vcard

from common import gajim
from common.i18n import Q_


class ProfileWindow:
	'''Class for our information window'''

	def __init__(self, account):
		self.xml = gtkgui_helpers.get_glade('profile_window.glade')
		self.window = self.xml.get_widget('profile_window')
		self.progressbar = self.xml.get_widget('progressbar')
		self.statusbar = self.xml.get_widget('statusbar')
		self.context_id = self.statusbar.get_context_id('profile')

		self.account = account
		self.jid = gajim.get_jid_from_account(account)

		self.dialog = None
		self.avatar_mime_type = None
		self.avatar_encoded = None
		self.message_id = self.statusbar.push(self.context_id,
			_('Retrieving profile...'))
		self.update_progressbar_timeout_id = gobject.timeout_add(100,
			self.update_progressbar)
		self.remove_statusbar_timeout_id = None

		# Create Image for avatar button
		image = gtk.Image()
		self.xml.get_widget('PHOTO_button').set_image(image)
		self.xml.signal_autoconnect(self)
		self.window.show_all()

	def update_progressbar(self):
		self.progressbar.pulse()
		return True # loop forever

	def remove_statusbar(self, message_id):
		self.statusbar.remove(self.context_id, message_id)
		self.remove_statusbar_timeout_id = None

	def on_profile_window_destroy(self, widget):
		if self.update_progressbar_timeout_id is not None:
			gobject.source_remove(self.update_progressbar_timeout_id)
		if self.remove_statusbar_timeout_id is not None:
			gobject.source_remove(self.remove_statusbar_timeout_id)
		del gajim.interface.instances[self.account]['profile']
		if self.dialog: # Image chooser dialog
			self.dialog.destroy()

	def on_profile_window_key_press_event(self, widget, event):
		if event.keyval == gtk.keysyms.Escape:
			self.window.destroy()

	def on_clear_button_clicked(self, widget):
		# empty the image
		button = self.xml.get_widget('PHOTO_button')
		image = button.get_image()
		image.set_from_pixbuf(None)
		button.hide()
		text_button = self.xml.get_widget('NOPHOTO_button')
		text_button.show()
		self.avatar_encoded = None
		self.avatar_mime_type = None

	def on_set_avatar_button_clicked(self, widget):
		def on_ok(widget, path_to_file):
			must_delete = False
			filesize = os.path.getsize(path_to_file) # in bytes
			invalid_file = False
			msg = ''
			if os.path.isfile(path_to_file):
				stat = os.stat(path_to_file)
				if stat[6] == 0:
					invalid_file = True
					msg = _('File is empty')
			else:
				invalid_file = True
				msg = _('File does not exist')
			if not invalid_file and filesize > 16384: # 16 kb
				try:
					pixbuf = gtk.gdk.pixbuf_new_from_file(path_to_file)
					# get the image at 'notification size'
					# and hope that user did not specify in ACE crazy size
					scaled_pixbuf = gtkgui_helpers.get_scaled_pixbuf(pixbuf,
						'tooltip')
				except gobject.GError, msg: # unknown format
					# msg should be string, not object instance
					msg = str(msg)
					invalid_file = True
			if invalid_file:
				if True: # keep identation
					dialogs.ErrorDialog(_('Could not load image'), msg)
					return
			if filesize > 16384:
					if scaled_pixbuf:
						path_to_file = os.path.join(gajim.TMP,
							'avatar_scaled.png')
						scaled_pixbuf.save(path_to_file, 'png')
						must_delete = True
			
			fd = open(path_to_file, 'rb')
			data = fd.read()
			pixbuf = gtkgui_helpers.get_pixbuf_from_data(data)
			try:			
				# rescale it
				pixbuf = gtkgui_helpers.get_scaled_pixbuf(pixbuf, 'vcard')
			except AttributeError: # unknown format
				dialogs.ErrorDialog(_('Could not load image'))
				return
			self.dialog.destroy()
			self.dialog = None
			button = self.xml.get_widget('PHOTO_button')
			image = button.get_image()
			image.set_from_pixbuf(pixbuf)
			button.show()
			text_button = self.xml.get_widget('NOPHOTO_button')
			text_button.hide()
			self.avatar_encoded = base64.encodestring(data)
			# returns None if unknown type
			self.avatar_mime_type = mimetypes.guess_type(path_to_file)[0]
			if must_delete: 
				try: 
					os.remove(path_to_file) 
				except OSError: 
					gajim.log.debug('Cannot remove %s' % path_to_file)

		def on_clear(widget):
			self.dialog.destroy()
			self.dialog = None
			self.on_clear_button_clicked(widget)

		def on_cancel(widget):
			self.dialog.destroy()
			self.dialog = None

		if self.dialog:
			self.dialog.present()
		else:
			self.dialog = dialogs.AvatarChooserDialog(on_response_ok = on_ok,
				on_response_cancel = on_cancel, on_response_clear = on_clear)

	def on_PHOTO_button_press_event(self, widget, event):
		'''If right-clicked, show popup'''
		if event.button == 3 and self.avatar_encoded: # right click
			menu = gtk.Menu()
			
			# Try to get pixbuf
			pixbuf = gtkgui_helpers.get_avatar_pixbuf_from_cache(self.jid,
				use_local = False)

			if pixbuf:
				nick = gajim.config.get_per('accounts', self.account, 'name')
				menuitem = gtk.ImageMenuItem(gtk.STOCK_SAVE_AS)
				menuitem.connect('activate',
					gtkgui_helpers.on_avatar_save_as_menuitem_activate,
					self.jid, None, nick + '.jpeg')
				menu.append(menuitem)
			# show clear
			menuitem = gtk.ImageMenuItem(gtk.STOCK_CLEAR)
			menuitem.connect('activate', self.on_clear_button_clicked)
			menu.append(menuitem)
			menu.connect('selection-done', lambda w:w.destroy())	
			# show the menu
			menu.show_all()
			menu.popup(None, None, None, event.button, event.time)
		elif event.button == 1: # left click
			self.on_set_avatar_button_clicked(widget)

	def set_value(self, entry_name, value):
		try:
			self.xml.get_widget(entry_name).set_text(value)
		except AttributeError:
			pass

	def set_values(self, vcard_):
		button = self.xml.get_widget('PHOTO_button')
		image = button.get_image()
		text_button = self.xml.get_widget('NOPHOTO_button')
		if not 'PHOTO' in vcard_:
			# set default image
			image.set_from_pixbuf(None)
			button.hide()
			text_button.show()
		for i in vcard_.keys():
			if i == 'PHOTO':
				pixbuf, self.avatar_encoded, self.avatar_mime_type = \
					vcard.get_avatar_pixbuf_encoded_mime(vcard_[i])
				if not pixbuf:
					image.set_from_pixbuf(None)
					button.hide()
					text_button.show()
					continue
				pixbuf = gtkgui_helpers.get_scaled_pixbuf(pixbuf, 'vcard')
				image.set_from_pixbuf(pixbuf)
				button.show()
				text_button.hide()
				continue
			if i == 'ADR' or i == 'TEL' or i == 'EMAIL':
				for entry in vcard_[i]:
					add_on = '_HOME'
					if 'WORK' in entry:
						add_on = '_WORK'
					for j in entry.keys():
						self.set_value(i + add_on + '_' + j + '_entry', entry[j])
			if isinstance(vcard_[i], dict):
				for j in vcard_[i].keys():
					self.set_value(i + '_' + j + '_entry', vcard_[i][j])
			else:
				if i == 'DESC':
					self.xml.get_widget('DESC_textview').get_buffer().set_text(
						vcard_[i], 0)
				else:
					self.set_value(i + '_entry', vcard_[i])
		if self.update_progressbar_timeout_id is not None:
			if self.message_id:
				self.statusbar.remove(self.context_id, self.message_id)
			self.message_id = self.statusbar.push(self.context_id,
				_('Information received'))
			self.remove_statusbar_timeout_id = gobject.timeout_add_seconds(3,
				self.remove_statusbar, self.message_id)
			gobject.source_remove(self.update_progressbar_timeout_id)
			self.progressbar.hide()
			self.progressbar.set_fraction(0)
			self.update_progressbar_timeout_id = None

	def add_to_vcard(self, vcard_, entry, txt):
		'''Add an information to the vCard dictionary'''
		entries = entry.split('_')
		loc = vcard_
		if len(entries) == 3: # We need to use lists
			if not loc.has_key(entries[0]):
				loc[entries[0]] = []
			found = False
			for e in loc[entries[0]]:
				if entries[1] in e:
					found = True
					break
			if found:
				e[entries[2]] = txt
			else:
				loc[entries[0]].append({entries[1]: '', entries[2]: txt})
			return vcard_
		while len(entries) > 1:
			if not loc.has_key(entries[0]):
				loc[entries[0]] = {}
			loc = loc[entries[0]]
			del entries[0]
		loc[entries[0]] = txt
		return vcard_

	def make_vcard(self):
		'''make the vCard dictionary'''
		entries = ['FN', 'NICKNAME', 'BDAY', 'EMAIL_HOME_USERID', 'URL',
			'TEL_HOME_NUMBER', 'N_FAMILY', 'N_GIVEN', 'N_MIDDLE', 'N_PREFIX',
			'N_SUFFIX', 'ADR_HOME_STREET', 'ADR_HOME_EXTADR', 'ADR_HOME_LOCALITY',
			'ADR_HOME_REGION', 'ADR_HOME_PCODE', 'ADR_HOME_CTRY', 'ORG_ORGNAME',
			'ORG_ORGUNIT', 'TITLE', 'ROLE', 'TEL_WORK_NUMBER', 'EMAIL_WORK_USERID',
			'ADR_WORK_STREET', 'ADR_WORK_EXTADR', 'ADR_WORK_LOCALITY',
			'ADR_WORK_REGION', 'ADR_WORK_PCODE', 'ADR_WORK_CTRY']
		vcard_ = {}
		for e in entries: 
			txt = self.xml.get_widget(e + '_entry').get_text().decode('utf-8')
			if txt != '':
				vcard_ = self.add_to_vcard(vcard_, e, txt)

		# DESC textview
		buff = self.xml.get_widget('DESC_textview').get_buffer()
		start_iter = buff.get_start_iter()
		end_iter = buff.get_end_iter()
		txt = buff.get_text(start_iter, end_iter, 0)
		if txt != '':
			vcard_['DESC'] = txt.decode('utf-8')

		# Avatar
		if self.avatar_encoded:
			vcard_['PHOTO'] = {'BINVAL': self.avatar_encoded}
			if self.avatar_mime_type:
				vcard_['PHOTO']['TYPE'] = self.avatar_mime_type
		return vcard_

	def on_ok_button_clicked(self, widget):
		if self.update_progressbar_timeout_id:
			# Operation in progress
			return
		if gajim.connections[self.account].connected < 2:
			dialogs.ErrorDialog(_('You are not connected to the server'),
				_('Without a connection you can not publish your contact '
				'information.'))
			return
		vcard_ = self.make_vcard()
		nick = ''
		if vcard_.has_key('NICKNAME'):
			nick = vcard_['NICKNAME']
			from common import pep
			pep.user_send_nickname(self.account, nick)
		if nick == '':
			nick = gajim.config.get_per('accounts', self.account, 'name')
		gajim.nicks[self.account] = nick
		gajim.connections[self.account].send_vcard(vcard_)
		self.message_id = self.statusbar.push(self.context_id,
			_('Sending profile...'))
		self.progressbar.show()
		self.update_progressbar_timeout_id = gobject.timeout_add(100,
			self.update_progressbar)

	def vcard_published(self):
		if self.update_progressbar_timeout_id is not None:
			gobject.source_remove(self.update_progressbar_timeout_id)
			self.update_progressbar_timeout_id = None
		self.window.destroy()

	def vcard_not_published(self):
		if self.message_id:
			self.statusbar.remove(self.context_id, self.message_id)
		self.message_id = self.statusbar.push(self.context_id,
			_('Information NOT published'))
		self.remove_statusbar_timeout_id = gobject.timeout_add_seconds(3,
			self.remove_statusbar, self.message_id)
		if self.update_progressbar_timeout_id is not None:
			gobject.source_remove(self.update_progressbar_timeout_id)
			self.progressbar.set_fraction(0)
			self.update_progressbar_timeout_id = None
		dialogs.InformationDialog(_('vCard publication failed'),
			_('There was an error while publishing your personal information, '
			'try again later.'))

	def on_cancel_button_clicked(self, widget):
		self.window.destroy()

# vim: se ts=3: