avatar.c
changeset 27 c641ef370d18
parent 26 fec515d07dd2
child 28 bcee0de24664
equal deleted inserted replaced
26:fec515d07dd2 27:c641ef370d18
    40 #include <mcabber/screen.h>
    40 #include <mcabber/screen.h>
    41 #include <mcabber/hbuf.h>
    41 #include <mcabber/hbuf.h>
    42 #include <mcabber/hooks.h>
    42 #include <mcabber/hooks.h>
    43 #include <mcabber/settings.h>
    43 #include <mcabber/settings.h>
    44 #include <mcabber/modules.h>
    44 #include <mcabber/modules.h>
       
    45 #include <mcabber/xmpp_helper.h>
    45 
    46 
    46 #include <mcabber/pep.h>
    47 #include <mcabber/pep.h>
    47 
    48 
    48 #include "config.h"
    49 #include "config.h"
    49 
    50 
    84 
    85 
    85 #define NS_AVATAR_DATA            ( "urn:xmpp:avatar:data"            )
    86 #define NS_AVATAR_DATA            ( "urn:xmpp:avatar:data"            )
    86 #define NS_AVATAR_METADATA        ( "urn:xmpp:avatar:metadata"        )
    87 #define NS_AVATAR_METADATA        ( "urn:xmpp:avatar:metadata"        )
    87 #define NS_AVATAR_METADATA_NOTIFY ( "urn:xmpp:avatar:metadata+notify" )
    88 #define NS_AVATAR_METADATA_NOTIFY ( "urn:xmpp:avatar:metadata+notify" )
    88 
    89 
    89 static guint avatar_cid            = 0;
       
    90 static guint avatar_hid_connect    = 0;
    90 static guint avatar_hid_connect    = 0;
    91 static guint avatar_hid_disconnect = 0;
    91 static guint avatar_hid_disconnect = 0;
    92 
    92 
    93 static GSList *reply_handlers = NULL;
    93 static GSList *reply_handlers = NULL;
    94 
    94 
    95 static LmMessageHandler *avatar_metadata_reply_handler = NULL;
    95 static LmMessageHandler *avatar_metadata_reply_handler = NULL;
    96 
    96 
    97 static gboolean  publish_delayed = FALSE;
    97 static gboolean  publish_delayed = FALSE;
    98 static gsize     publish_len     = 0;
    98 static gsize     publish_len     = 0;
    99 static gchar    *publish_data    = NULL;
    99 static guchar   *publish_data    = NULL;
   100 
   100 
   101 // predeclarations
   101 // predeclarations
   102 
   102 
   103 static LmHandlerResult avatar_retrieve_data_reply_handler (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, gpointer userdata);
   103 static LmHandlerResult avatar_retrieve_data_reply_handler (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, gpointer userdata);
   104 
   104 
   129 
   129 
   130 	if (!fd)
   130 	if (!fd)
   131 		return NULL;
   131 		return NULL;
   132 
   132 
   133 	{ // check signature
   133 	{ // check signature
   134 		char header[8];
   134 		png_byte header[8];
   135 
   135 
   136 		fread (header, 1, 8, fd);
   136 		fread (header, 1, 8, fd);
   137 
   137 
   138 		if (png_sig_cmp (header, 0, 8)) {
   138 		if (png_sig_cmp (header, 0, 8)) {
   139 			fclose (fd);
   139 			fclose (fd);
   267 	  // I hope, compiler will optimize out all these variables...
   267 	  // I hope, compiler will optimize out all these variables...
   268 
   268 
   269 		// font size in font pixels (real?)
   269 		// font size in font pixels (real?)
   270 		//int fonth = 16;
   270 		//int fonth = 16;
   271 		//int fontw = 8;
   271 		//int fontw = 8;
   272 		float fontaspect = (float) fontw / fonth;
   272 		//float fontaspect = (float) fontw / fonth;
   273 		// aa pixels per character
   273 		// aa pixels per character
   274 		const int aappch = 2;
   274 		const int aappch = 2;
   275 		const int aappcw = 2;
   275 		const int aappcw = 2;
   276 		const float aaaspect = (float) aappcw / aappch;
   276 		//const float aaaspect = (float) aappcw / aappch;
   277 		// font pixels per aa pixel
   277 		// font pixels per aa pixel
   278 		int aappph = (float) fonth / aappch;
   278 		int aappph = (float) fonth / aappch;
   279 		int aapppw = (float) fontw / aappcw;
   279 		int aapppw = (float) fontw / aappcw;
   280 		float aapixelaspect = (float) aapppw / aappph;
   280 		float aapixelaspect = (float) aapppw / aappph;
   281 		// original image size in real pixels
   281 		// original image size in real pixels
   331 		} else {
   331 		} else {
   332 			resh = (float) resh_rp * aapixelaspect;
   332 			resh = (float) resh_rp * aapixelaspect;
   333 			resw = resw_rp;
   333 			resw = resw_rp;
   334 		}
   334 		}
   335 		// number of original image pixels per one resulting image pixel
   335 		// number of original image pixels per one resulting image pixel
   336 		int ratioh = (float) originh / resh;
   336 		//int ratioh = (float) originh / resh;
   337 		int ratiow = (float) originw / resw;
   337 		//int ratiow = (float) originw / resw;
   338 		// resulting image size in chars
   338 		// resulting image size in chars
   339 		int rescharh = (float) resh / aappch;
   339 		int rescharh = (float) resh / aappch;
   340 		int rescharw = (float) resw / aappcw;
   340 		int rescharw = (float) resw / aappcw;
   341 
   341 
   342 		// re-calculate parameters, as they may change due to rounding (?)
   342 		// re-calculate parameters, as they may change due to rounding (?)
   413 	// render ascii image
   413 	// render ascii image
   414 	aa_render (context, &aa_defrenderparams, 0, 0, aa_scrwidth (context), aa_scrheight (context));
   414 	aa_render (context, &aa_defrenderparams, 0, 0, aa_scrwidth (context), aa_scrheight (context));
   415 	aa_flush (context);
   415 	aa_flush (context);
   416 
   416 
   417 	{ // format the result
   417 	{ // format the result
   418 		char    *text   = aa_text (context);
   418 		char    *text   = (char *) aa_text (context);
   419 		int      width  = aa_scrwidth (context);
   419 		int      width  = aa_scrwidth (context);
   420 		int      height = aa_scrheight (context);
   420 		int      height = aa_scrheight (context);
   421 		GString *avatar = g_string_new (NULL);
   421 		GString *avatar = g_string_new (NULL);
   422 		char    *end    = text + width * height;
   422 		char    *end    = text + width * height;
   423 
   423 
   746 						if (!data)
   746 						if (!data)
   747 							continue;
   747 							continue;
   748 
   748 
   749 						{ // save to file and display in ascii
   749 						{ // save to file and display in ascii
   750 							const gchar *base64 = lm_message_node_get_value (data);
   750 							const gchar *base64 = lm_message_node_get_value (data);
   751 							gchar       *png;
   751 							guchar      *png;
   752 							gsize        len;
   752 							gsize        len;
   753 							gchar       *file;
   753 							gchar       *file;
   754 
   754 
   755 							if (!base64)
   755 							if (!base64)
   756 								continue;
   756 								continue;
   825 	return LM_HANDLER_RESULT_REMOVE_MESSAGE;
   825 	return LM_HANDLER_RESULT_REMOVE_MESSAGE;
   826 }
   826 }
   827 
   827 
   828 // data is image/png data of length len
   828 // data is image/png data of length len
   829 // call with len = 0 to publish empty avatar
   829 // call with len = 0 to publish empty avatar
   830 static void avatar_publish (const gchar *data, gsize len)
   830 static void avatar_publish (const guchar *data, gsize len)
   831 {
   831 {
   832 	LmMessage        *datarequest;
   832 	LmMessage     *datarequest;
   833 	LmMessageHandler *handler;
   833 	gchar         *id;
   834 	gchar            *id;
   834 	LmMessage     *request;
   835 	LmMessage        *request;
   835 	LmMessageNode *node;
   836 	LmMessageNode    *node;
   836 	GError        *error       = NULL;
   837 	GError           *error       = NULL;
       
   838 
   837 
   839 	if (!xmpp_is_online ()) {
   838 	if (!xmpp_is_online ()) {
   840 		scr_log_print (LPRINT_DEBUG, "avatar: Not connected, delaying publish.");
   839 		scr_log_print (LPRINT_DEBUG, "avatar: Not connected, delaying publish.");
   841 
   840 
   842 		g_free (publish_data);
   841 		g_free (publish_data);
   843 
   842 
   844 		publish_data    = g_strndup (data, len);
   843 		publish_data    = (guchar *) g_strndup ((gchar *) data, len);
   845 		publish_len     = len;
   844 		publish_len     = len;
   846 		publish_delayed = TRUE;
   845 		publish_delayed = TRUE;
   847 
   846 
   848 		return;
   847 		return;
   849 	}
   848 	}
  1040 }
  1039 }
  1041 
  1040 
  1042 // /AVATAR [filename|-]
  1041 // /AVATAR [filename|-]
  1043 static void do_avatar (char *arg)
  1042 static void do_avatar (char *arg)
  1044 {
  1043 {
  1045 	gchar *fname;
  1044 	gchar  *fname;
  1046 	gchar *data;
  1045 	guchar *data;
  1047 	gsize  len;
  1046 	gsize   len;
  1048 
  1047 
  1049 	if (!*arg) { // print current byddy's avatar
  1048 	if (!*arg) { // print current byddy's avatar
  1050 		const gchar *jid = CURRENT_JID;
  1049 		const gchar *jid = CURRENT_JID;
  1051 
  1050 
  1052 		if (!jid) {
  1051 		if (!jid) {
  1091 		}
  1090 		}
  1092 
  1091 
  1093 		close (fd);
  1092 		close (fd);
  1094 
  1093 
  1095 		len  = datastring->len;
  1094 		len  = datastring->len;
  1096 		data = g_string_free (datastring, FALSE);
  1095 		data = (guchar *) g_string_free (datastring, FALSE);
  1097 	}
  1096 	}
  1098 
  1097 
  1099 	g_free (fname);
  1098 	g_free (fname);
  1100 
  1099 
  1101 	avatar_publish (data, len);
  1100 	avatar_publish (data, len);
  1133 }
  1132 }
  1134 
  1133 
  1135 static guint avatar_hch (const gchar *hid, hk_arg_t *args, gpointer userdata)
  1134 static guint avatar_hch (const gchar *hid, hk_arg_t *args, gpointer userdata)
  1136 {
  1135 {
  1137 	if (publish_data) {
  1136 	if (publish_data) {
  1138 		char *tmp_data = publish_data;
  1137 		guchar *tmp_data = publish_data;
  1139 
  1138 
  1140 		scr_log_print (LPRINT_DEBUG, "avatar: Publishing delayed data.");
  1139 		scr_log_print (LPRINT_DEBUG, "avatar: Publishing delayed data.");
  1141 
  1140 
  1142 		publish_data    = NULL;
  1141 		publish_data    = NULL;
  1143 		publish_delayed = FALSE;
  1142 		publish_delayed = FALSE;