From 9e58a5b68cd5e4c24010b0621a49456add8d9e5e Mon Sep 17 00:00:00 2001
From: Yann Leboulanger <asterix@lagaule.org>
Date: Mon, 6 Jun 2005 22:58:06 +0000
Subject: [PATCH] we can now add an avatar in our vcard

---
 src/common/connection.py |  1 +
 src/gtkgui.glade         | 67 ++++++++++++++++++++++++++++++++----
 src/vcard.py             | 74 +++++++++++++++++++++++++++++++++++++++-
 3 files changed, 134 insertions(+), 8 deletions(-)

diff --git a/src/common/connection.py b/src/common/connection.py
index b650bb2850..9a727de0df 100644
--- a/src/common/connection.py
+++ b/src/common/connection.py
@@ -887,6 +887,7 @@ class Connection:
 	def send_vcard(self, vcard):
 		if not self.connection:
 			return
+		print vcard
 		iq = common.xmpp.Iq(typ = 'set')
 		iq2 = iq.setTag(common.xmpp.NS_VCARD + ' vCard')
 		for i in vcard.keys():
diff --git a/src/gtkgui.glade b/src/gtkgui.glade
index 48ab745411..80ef376ae3 100644
--- a/src/gtkgui.glade
+++ b/src/gtkgui.glade
@@ -8058,23 +8058,76 @@ Custom</property>
 		<widget class="GtkExpander" id="expander6">
 		  <property name="visible">True</property>
 		  <property name="can_focus">True</property>
-		  <property name="expanded">True</property>
+		  <property name="expanded">False</property>
 		  <property name="spacing">0</property>
 
 		  <child>
-		    <widget class="GtkImage" id="PHOTO_image">
+		    <widget class="GtkHBox" id="hbox2963">
 		      <property name="visible">True</property>
-		      <property name="xalign">0.5</property>
-		      <property name="yalign">0.5</property>
-		      <property name="xpad">0</property>
-		      <property name="ypad">0</property>
+		      <property name="homogeneous">False</property>
+		      <property name="spacing">0</property>
+
+		      <child>
+			<widget class="GtkImage" id="PHOTO_image">
+			  <property name="visible">True</property>
+			  <property name="xalign">0.5</property>
+			  <property name="yalign">0.5</property>
+			  <property name="xpad">0</property>
+			  <property name="ypad">0</property>
+			</widget>
+			<packing>
+			  <property name="padding">0</property>
+			  <property name="expand">True</property>
+			  <property name="fill">True</property>
+			</packing>
+		      </child>
+
+		      <child>
+			<widget class="GtkVButtonBox" id="photo_vbuttonbox">
+			  <property name="border_width">6</property>
+			  <property name="visible">True</property>
+			  <property name="layout_style">GTK_BUTTONBOX_SPREAD</property>
+			  <property name="spacing">0</property>
+
+			  <child>
+			    <widget class="GtkButton" id="set_avatar_button">
+			      <property name="visible">True</property>
+			      <property name="can_default">True</property>
+			      <property name="can_focus">True</property>
+			      <property name="label" translatable="yes">Set Avatar</property>
+			      <property name="use_underline">True</property>
+			      <property name="relief">GTK_RELIEF_NORMAL</property>
+			      <property name="focus_on_click">True</property>
+			      <signal name="clicked" handler="on_set_avatar_button_clicked" last_modification_time="Mon, 06 Jun 2005 20:29:04 GMT"/>
+			    </widget>
+			  </child>
+
+			  <child>
+			    <widget class="GtkButton" id="clear_button">
+			      <property name="visible">True</property>
+			      <property name="can_default">True</property>
+			      <property name="can_focus">True</property>
+			      <property name="label">gtk-clear</property>
+			      <property name="use_stock">True</property>
+			      <property name="relief">GTK_RELIEF_NORMAL</property>
+			      <property name="focus_on_click">True</property>
+			      <signal name="clicked" handler="on_clear_button_clicked" last_modification_time="Mon, 06 Jun 2005 20:28:59 GMT"/>
+			    </widget>
+			  </child>
+			</widget>
+			<packing>
+			  <property name="padding">0</property>
+			  <property name="expand">False</property>
+			  <property name="fill">True</property>
+			</packing>
+		      </child>
 		    </widget>
 		  </child>
 
 		  <child>
 		    <widget class="GtkLabel" id="label310">
 		      <property name="visible">True</property>
-		      <property name="label" translatable="yes">Photo</property>
+		      <property name="label" translatable="yes">Avatar</property>
 		      <property name="use_underline">False</property>
 		      <property name="use_markup">False</property>
 		      <property name="justify">GTK_JUSTIFY_LEFT</property>
diff --git a/src/vcard.py b/src/vcard.py
index a5d7a52a1f..9d3be6d347 100644
--- a/src/vcard.py
+++ b/src/vcard.py
@@ -21,6 +21,8 @@ import gtk
 import gtk.glade
 import urllib
 import base64
+import mimetypes
+import os
 from common import gajim
 from common import i18n
 _ = i18n._
@@ -69,6 +71,62 @@ class Vcard_window:
 				' '.join(no_log_for))
 		self.window.destroy()
 
+	def on_clear_button_clicked(self, widget):
+		# empty the image
+		self.xml.get_widget('PHOTO_image').set_from_pixbuf(None)
+
+	def image_is_ok(self, image):
+		if not os.path.exists(image):
+			return False
+		return True
+
+	def on_set_avatar_button_clicked(self, widget):
+		dialog = gtk.FileChooserDialog('Choose avatar', None,
+			gtk.FILE_CHOOSER_ACTION_OPEN,
+			(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
+			gtk.STOCK_OPEN, gtk.RESPONSE_OK))
+		dialog.set_default_response(gtk.RESPONSE_OK)
+		filter = gtk.FileFilter()
+		filter.set_name('All files')
+		filter.add_pattern('*')
+		dialog.add_filter(filter)
+
+		filter = gtk.FileFilter()
+		filter.set_name('Images')
+		filter.add_mime_type('image/png')
+		filter.add_mime_type('image/jpeg')
+		filter.add_mime_type('image/gif')
+		filter.add_pattern('*.png')
+		filter.add_pattern('*.jpg')
+		filter.add_pattern('*.gif')
+		filter.add_pattern('*.tif')
+		filter.add_pattern('*.xpm')
+		dialog.add_filter(filter)
+		dialog.set_filter(filter)
+
+		ok = False
+		while not ok:
+			response = dialog.run()
+			if response == gtk.RESPONSE_OK:
+				file = dialog.get_filename()
+				if self.image_is_ok(file):
+					ok = True
+			else:
+				ok = True
+		dialog.destroy()
+
+		if file:
+			fd = open(file)
+			data = fd.read()
+			pixbufloader = gtk.gdk.PixbufLoader()
+			pixbufloader.write(data)
+			pixbufloader.close()
+			pixbuf = pixbufloader.get_pixbuf()
+			image = self.xml.get_widget('PHOTO_image')
+			image.set_from_pixbuf(pixbuf)
+			self.avatar_encoded = base64.encodestring(data)
+			self.avatar_mime_type = mimetypes.guess_type(file)[0]
+
 	def set_value(self, entry_name, value):
 		try:
 			self.xml.get_widget(entry_name).set_text(value)
@@ -81,8 +139,10 @@ class Vcard_window:
 				if i == 'PHOTO':
 					img_decoded = None
 					if vcard[i].has_key('BINVAL'):
+						img_encoded = vcard[i]['BINVAL']
+						self.avatar_encoded = img_encoded
+						self.avatar_mime_type = vcard[i]['TYPE']
 						try:
-							img_encoded = vcard[i]['BINVAL']
 							img_decoded = base64.decodestring(img_encoded)
 						except:
 							pass
@@ -204,12 +264,19 @@ class Vcard_window:
 			txt = self.xml.get_widget(e + '_entry').get_text()
 			if txt != '':
 				vcard = self.add_to_vcard(vcard, e, txt)
+
+		# DESC textview
 		buffer = self.xml.get_widget('DESC_textview').get_buffer()
 		start_iter = buffer.get_start_iter()
 		end_iter = buffer.get_end_iter()
 		txt = buffer.get_text(start_iter, end_iter, 0)
 		if txt != '':
 			vcard['DESC'] = txt
+
+		# Avatar
+		if self.avatar_encoded:
+			vcard['PHOTO'] = {'TYPE': self.avatar_mime_type,
+				'BINVAL': self.avatar_encoded}
 		return vcard
 
 	def on_publish_button_clicked(self, widget):
@@ -263,6 +330,8 @@ class Vcard_window:
 		#close button at the end
 		button = self.xml.get_widget('close_button')
 		information_hbuttonbox.reorder_child(button, 2)
+		#photo_vbuttonbox visible
+		self.xml.get_widget('photo_vbuttonbox').show()
 		
 		#make all entries editable
 		entries = ['FN', 'NICKNAME', 'BDAY', 'EMAIL_HOME_USERID', 'URL',
@@ -283,10 +352,13 @@ class Vcard_window:
 	def __init__(self, user, plugin, account, vcard = False):
 		self.xml = gtk.glade.XML(GTKGUI_GLADE, 'vcard_information_window', APP)
 		self.window = self.xml.get_widget('vcard_information_window')
+		self.xml.get_widget('photo_vbuttonbox').set_no_show_all(True)
 		self.plugin = plugin
 		self.user = user #don't use it if vcard is true
 		self.account = account
 		self.vcard = vcard
+		self.avatar_mime_type = None
+		self.avatar_encoded = None
 
 		if vcard:
 			self.jid = user
-- 
GitLab