isolationserver/isoserver/src/isopresence.c
branchRCL_3
changeset 11 3404599e4dda
parent 9 46cc8e302e43
equal deleted inserted replaced
9:46cc8e302e43 11:3404599e4dda
     1 /*
       
     2 * ============================================================================
       
     3 *  Name        : isopresence.c
       
     4 *  Part of     : isolation server.
       
     5 *  Version     : %version: 18 %
       
     6 *
       
     7 *  Copyright © 2007-2008 Nokia.  All rights reserved.
       
     8 *  All rights reserved.
       
     9 *  Redistribution and use in source and binary forms, with or without modification, 
       
    10 *  are permitted provided that the following conditions are met:
       
    11 *  Redistributions of source code must retain the above copyright notice, this list 
       
    12 *  of conditions and the following disclaimer.Redistributions in binary form must 
       
    13 *  reproduce the above copyright notice, this list of conditions and the following 
       
    14 *  disclaimer in the documentation and/or other materials provided with the distribution.
       
    15 *  Neither the name of the Nokia Corporation nor the names of its contributors may be used 
       
    16 *  to endorse or promote products derived from this software without specific prior written 
       
    17 *  permission.
       
    18 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
       
    19 *  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
       
    20 *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
       
    21 *  SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
       
    22 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
       
    23 *  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
       
    24 *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
       
    25 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
       
    26 *  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    27 * ============================================================================
       
    28 * Template version: 1.0
       
    29 */
       
    30 
       
    31 #include "isopresence.h"
       
    32 #include "stdio.h"
       
    33 #include "isoservermain.h"
       
    34 #include "msgliterals.h"
       
    35 #include "msgqlib.h"
       
    36 #include "msg_enums.h"
       
    37 #include "isoutils.h"
       
    38 
       
    39 //#include "config.h"
       
    40 
       
    41 #include <string.h>
       
    42 #include <stdlib.h>
       
    43 
       
    44 #ifndef SYMBIAN
       
    45 #include <glib/gi18n.h>
       
    46 #include <libintl.h>
       
    47 #define _(x) dgettext (GETTEXT_PACKAGE, x)
       
    48 #define N_(x) x
       
    49 #else
       
    50 #define _(x) x
       
    51 #define N_(x) x
       
    52 #endif
       
    53 
       
    54 /*!	\file 
       
    55 *	Impliments the functions in isomodifycontactlist.h
       
    56 */
       
    57 
       
    58 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TELEPATHY_TYPE_PRESENCE, TelepathyPresencePriv))
       
    59 
       
    60 /*!	\var glob_handle_name accessed in multiple functions. Stores the 
       
    61 *	contact name
       
    62 *	\remark This global variable can be avoided
       
    63 */
       
    64 gchar *              glob_handle_name;
       
    65 
       
    66 /*!	\typedef long typedefed to TelepathyTime
       
    67 */
       
    68 typedef long TelepathyTime;         /* Note: Always in UTC. */
       
    69 
       
    70 
       
    71 /*!	\typedef long struct _TelepathyPresencePriv to TelepathyPresencePriv 
       
    72 */
       
    73 typedef struct _TelepathyPresencePriv TelepathyPresencePriv;
       
    74 
       
    75 /*!	\struct _TelepathyPresencePriv
       
    76 *	\brief telepathy presence object
       
    77 *
       
    78 *	\var state presence state
       
    79 *	\var status status text
       
    80 *	\var resource GObject resource
       
    81 *	\var priority GObject priority
       
    82 *	\var timestamp set with current time
       
    83 */
       
    84 struct _TelepathyPresencePriv {
       
    85 	TelepathyPresenceState  state;
       
    86 
       
    87 	gchar               *status;
       
    88 	gchar               *resource;
       
    89 
       
    90 	gint                 priority;
       
    91 	TelepathyTime           timestamp;
       
    92 };
       
    93 
       
    94 /*! \enum 
       
    95 *	\brief _TelepathyPresencePriv member variables
       
    96 */
       
    97 enum {
       
    98 	PROP_0,
       
    99 	PROP_STATE,
       
   100 	PROP_STATUS,
       
   101 	PROP_RESOURCE,
       
   102 	PROP_PRIORITY
       
   103 };
       
   104 
       
   105 G_DEFINE_TYPE (TelepathyPresence, telepathy_presence, G_TYPE_OBJECT);
       
   106 
       
   107 /*! \brief Documented in the header file
       
   108 */
       
   109 TelepathyTime
       
   110 telepathy_time_get_current (void)
       
   111 {
       
   112 	return time (NULL);
       
   113 }
       
   114 
       
   115 /*! \brief Documented in the header file
       
   116 */
       
   117 static void
       
   118 telepathy_presence_class_init (TelepathyPresenceClass *presclass)
       
   119 {
       
   120 	GObjectClass *object_class;
       
   121 
       
   122 	object_class = G_OBJECT_CLASS (presclass);
       
   123 
       
   124 	object_class->finalize     = presence_finalize;
       
   125 	object_class->get_property = presence_get_property;
       
   126 	object_class->set_property = presence_set_property;
       
   127 
       
   128 	g_object_class_install_property (object_class,
       
   129 					 PROP_STATE,
       
   130 					 g_param_spec_int ("state",
       
   131 							   "Presence State",
       
   132 							   "The current state of the presence",
       
   133 							   TELEPATHY_PRESENCE_STATE_AVAILABLE,
       
   134 							   TELEPATHY_PRESENCE_STATE_EXT_AWAY,
       
   135 							   TELEPATHY_PRESENCE_STATE_AVAILABLE,
       
   136 							   G_PARAM_READWRITE));
       
   137 	g_object_class_install_property (object_class,
       
   138 					 PROP_STATUS,
       
   139 					 g_param_spec_string ("status",
       
   140 							      "Presence Status",
       
   141 							      "Status string set on presence",
       
   142 							      NULL,
       
   143 							      G_PARAM_READWRITE));
       
   144 	g_object_class_install_property (object_class,
       
   145 					 PROP_RESOURCE,
       
   146 					 g_param_spec_string ("resource",
       
   147 							      "Presence Resource",
       
   148 							      "Resource that this presence is for",
       
   149 							      NULL,
       
   150 							      G_PARAM_READWRITE));
       
   151 	g_object_class_install_property (object_class,
       
   152 					 PROP_PRIORITY,
       
   153 					 g_param_spec_int ("priority",
       
   154 							   "Presence Priority",
       
   155 							   "Priority value of presence",
       
   156 							   G_MININT,
       
   157 							   G_MAXINT,
       
   158 							   0,
       
   159 							   G_PARAM_READWRITE));
       
   160 
       
   161 	g_type_class_add_private (object_class, sizeof (TelepathyPresencePriv));
       
   162 }
       
   163 
       
   164 /*! \brief Documented in the header file
       
   165 */
       
   166 static void
       
   167 telepathy_presence_init (TelepathyPresence *presence)
       
   168 {
       
   169 	TelepathyPresencePriv *priv;
       
   170 
       
   171 	priv = GET_PRIV (presence);
       
   172 
       
   173 	priv->state = TELEPATHY_PRESENCE_STATE_AVAILABLE;
       
   174 
       
   175 	priv->status = NULL;
       
   176 	priv->resource = NULL;
       
   177 
       
   178 	priv->priority = 0;
       
   179 
       
   180 	priv->timestamp = telepathy_time_get_current ();
       
   181 }
       
   182 
       
   183 /*! /brief GObject finalize funtion
       
   184 *
       
   185 *   /param object object to be finalized
       
   186 */
       
   187 static void
       
   188 presence_finalize (GObject *object)
       
   189 {
       
   190 	TelepathyPresencePriv *priv;
       
   191 
       
   192 	priv = GET_PRIV (object);
       
   193 
       
   194 	g_free (priv->status);
       
   195 	g_free (priv->resource);
       
   196 
       
   197 	(G_OBJECT_CLASS (telepathy_presence_parent_class)->finalize) (object);
       
   198 }
       
   199 
       
   200 /*! /brief GObject get property funtion
       
   201 *
       
   202 *   /param object Gobject
       
   203 *   /param param_id
       
   204 *   /param value
       
   205 *   /param pspec
       
   206 */
       
   207 static void
       
   208 presence_get_property (GObject    *object,
       
   209 		       guint       param_id,
       
   210 		       GValue     *value,
       
   211 		       GParamSpec *pspec)
       
   212 {
       
   213 	TelepathyPresencePriv *priv;
       
   214 
       
   215 	priv = GET_PRIV (object);
       
   216 
       
   217 	switch (param_id) {
       
   218 	case PROP_STATE:
       
   219 		g_value_set_int (value, priv->state);
       
   220 		break;
       
   221 	case PROP_STATUS:
       
   222 		g_value_set_string (value,
       
   223 				    telepathy_presence_get_status (TELEPATHY_PRESENCE (object)));
       
   224 		break;
       
   225 	default:
       
   226 		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
       
   227 		break;
       
   228 	}
       
   229 }
       
   230 
       
   231 /*! /brief GObject set property funtion
       
   232 *
       
   233 *   /param object Gobject
       
   234 *   /param param_id
       
   235 *   /param value
       
   236 *   /param pspec
       
   237 */  
       
   238 static void
       
   239 presence_set_property (GObject      *object,
       
   240 		       guint         param_id,
       
   241 		       const GValue *value,
       
   242 		       GParamSpec   *pspec)
       
   243 {
       
   244 	TelepathyPresencePriv *priv;
       
   245 
       
   246 	priv = GET_PRIV (object);
       
   247 
       
   248 	switch (param_id) {
       
   249 	case PROP_STATE:
       
   250 		priv->state = g_value_get_int (value);
       
   251 		break;
       
   252 	case PROP_STATUS:
       
   253 		telepathy_presence_set_status (TELEPATHY_PRESENCE (object),
       
   254 					    g_value_get_string (value));
       
   255 		break;
       
   256 	default:
       
   257 		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
       
   258 		break;
       
   259 	}
       
   260 }
       
   261 
       
   262 /*! /brief create a presence object instance
       
   263  *  /return created presence object instance
       
   264  */
       
   265 
       
   266 TelepathyPresence *
       
   267 telepathy_presence_new (void)
       
   268 {
       
   269 	return g_object_new (TELEPATHY_TYPE_PRESENCE, NULL);
       
   270 }
       
   271 
       
   272 /*! /brief get the presence status value
       
   273  *
       
   274  *  /param presence : presence object
       
   275  *  /return state : status as set in presence object
       
   276  */
       
   277 const gchar *
       
   278 telepathy_presence_get_status (TelepathyPresence *presence)
       
   279 {
       
   280 	TelepathyPresencePriv *priv;
       
   281 
       
   282 	g_return_val_if_fail (TELEPATHY_IS_PRESENCE (presence), NULL);
       
   283 	
       
   284 	priv = GET_PRIV (presence);
       
   285 
       
   286 	return priv->status;
       
   287 }
       
   288 
       
   289 /*! /brief get the presence state value
       
   290  *
       
   291  *  /param presence : presence object
       
   292  *  /return state : state as set in presence object
       
   293  */
       
   294 TelepathyPresenceState
       
   295 telepathy_presence_get_state (TelepathyPresence *presence)
       
   296 {
       
   297 	TelepathyPresencePriv *priv;
       
   298 
       
   299 	g_return_val_if_fail (TELEPATHY_IS_PRESENCE (presence),
       
   300 			      TELEPATHY_PRESENCE_STATE_AVAILABLE);
       
   301 
       
   302 	priv = GET_PRIV (presence);
       
   303 
       
   304 	return priv->state;
       
   305 }
       
   306 
       
   307 /*! /brief set the presence state value in presence gobject
       
   308  *
       
   309  *  /param presence : presence object
       
   310  *  /param state : state
       
   311  *  /return : void
       
   312  */
       
   313 void
       
   314 telepathy_presence_set_state (TelepathyPresence      *presence,
       
   315 			   TelepathyPresenceState  state)
       
   316 {
       
   317 	TelepathyPresencePriv *priv;
       
   318 
       
   319 	g_return_if_fail (TELEPATHY_IS_PRESENCE (presence));
       
   320 
       
   321 	priv = GET_PRIV (presence);
       
   322 
       
   323 	priv->state = state;
       
   324 
       
   325 	g_object_notify (G_OBJECT (presence), "state");
       
   326 }
       
   327 
       
   328 /*! /brief set the presence status text value in presence gobject
       
   329  *
       
   330  *  /param presence : presence object
       
   331  *  /param state : status
       
   332  *  /return : void
       
   333  */
       
   334 void
       
   335 telepathy_presence_set_status (TelepathyPresence *presence,
       
   336 			    const gchar    *status)
       
   337 {
       
   338 	TelepathyPresencePriv *priv;
       
   339 
       
   340 	g_return_if_fail (TELEPATHY_IS_PRESENCE (presence));
       
   341 
       
   342 	priv = GET_PRIV (presence);
       
   343 
       
   344 	g_free (priv->status);
       
   345 
       
   346 	if (status) {
       
   347 		priv->status = g_strdup (status);
       
   348 	} else {
       
   349 		priv->status = NULL;
       
   350 	}
       
   351 
       
   352 	g_object_notify (G_OBJECT (presence), "status");
       
   353 
       
   354 }
       
   355 
       
   356 /*! /brief converts presence state value from enum to string
       
   357  *  /param presence_state : presence state enum value
       
   358  *  /return : presence state string value
       
   359  */
       
   360 const gchar *
       
   361 telepathy_presence_state_to_str (TelepathyPresenceState presence_state)
       
   362 {
       
   363 
       
   364 	switch (presence_state) {
       
   365 	case TELEPATHY_PRESENCE_STATE_AVAILABLE:
       
   366 		return "available";
       
   367 	case TELEPATHY_PRESENCE_STATE_BUSY:
       
   368 		return "dnd";
       
   369 	case TELEPATHY_PRESENCE_STATE_AWAY:
       
   370 		return "away";
       
   371 	case TELEPATHY_PRESENCE_STATE_EXT_AWAY:
       
   372 		return "xa";
       
   373 	case TELEPATHY_PRESENCE_STATE_HIDDEN:
       
   374 		return "hidden";
       
   375 	case TELEPATHY_PRESENCE_STATE_UNAVAILABLE:
       
   376 		return "offline";
       
   377 	default:
       
   378 		return NULL;
       
   379 	}
       
   380 }
       
   381 
       
   382 /*! /brief converts presence state value from string to enum
       
   383  *
       
   384  *  /param str : presence state string value
       
   385  *  /return : presence state enum value
       
   386  */
       
   387 TelepathyPresenceState
       
   388 telepathy_telepathy_presence_state_from_str (const gchar *str)
       
   389 {
       
   390 
       
   391 	if (strcmp (str, "available") == 0) {
       
   392 		return TELEPATHY_PRESENCE_STATE_AVAILABLE;
       
   393 	} else if ((strcmp (str, "dnd") == 0) || (strcmp (str, "busy") == 0)) {
       
   394 		return TELEPATHY_PRESENCE_STATE_BUSY;
       
   395 	} else if ((strcmp (str, "away") == 0) || (strcmp (str, "brb") == 0)) {
       
   396 		return TELEPATHY_PRESENCE_STATE_AWAY;
       
   397 	} else if (strcmp (str, "xa") == 0) {
       
   398 		return TELEPATHY_PRESENCE_STATE_EXT_AWAY;
       
   399 	} else if (strcmp (str, "hidden") == 0) {
       
   400 		return TELEPATHY_PRESENCE_STATE_HIDDEN;
       
   401 	} else if (strcmp (str, "offline") == 0) {
       
   402 		return TELEPATHY_PRESENCE_STATE_UNAVAILABLE;
       
   403 	} 
       
   404 
       
   405 	return TELEPATHY_PRESENCE_STATE_AVAILABLE;
       
   406 }
       
   407 
       
   408 /*! /brief updates own presence to the n/w server. update_status_cb 
       
   409  *  is registered for the callback
       
   410  *
       
   411  *  /param pres_iface : presence interface
       
   412  *  /param presence : presence object 
       
   413  *  /param msg_hdr : presence info list to send presence_msg_struct
       
   414  *  has state, status etc., 
       
   415  *  /return : void
       
   416  */
       
   417 void
       
   418 telepathy_contacts_send_presence (DBusGProxy *pres_iface,
       
   419 					 TelepathyPresence          *presence,
       
   420 					 presence_msg_struct* msg_hdr)
       
   421 {
       
   422 	GHashTable          *status_ids;
       
   423 	GHashTable          *status_options;
       
   424 	const gchar         *status_id;
       
   425 	const gchar         *message;
       
   426 	TelepathyPresenceState  presence_state;
       
   427 	GValue               value_message = {0, };
       
   428 
       
   429 	if (!pres_iface) {
       
   430 		return;
       
   431 	}
       
   432   	iso_logger( "%s", "In - telepathy_contacts_send_presence\n" );
       
   433 
       
   434 	status_ids = g_hash_table_new_full (g_str_hash,
       
   435 					    g_str_equal,
       
   436 					    g_free,
       
   437 					    (GDestroyNotify) g_hash_table_destroy);
       
   438 	status_options = g_hash_table_new_full (g_str_hash,
       
   439 						g_str_equal,
       
   440 						NULL,
       
   441 						(GDestroyNotify) g_value_unset); //ToDo: get clarity on freeing this resource
       
   442 
       
   443 	presence_state = telepathy_presence_get_state (presence);
       
   444 	status_id = telepathy_presence_state_to_str (presence_state);
       
   445 	message = telepathy_presence_get_status (presence);
       
   446 
       
   447 	if (message) {
       
   448 		g_value_init (&value_message, G_TYPE_STRING);
       
   449 		g_value_set_string (&value_message, message);
       
   450 		g_hash_table_insert (status_options, "message", &value_message);
       
   451 	}
       
   452 
       
   453 	g_hash_table_insert (status_ids,
       
   454 			     g_strdup (status_id),
       
   455 			     status_options);
       
   456 
       
   457 	tp_conn_iface_presence_set_status_async(DBUS_G_PROXY(pres_iface),
       
   458 						status_ids,
       
   459 						update_status_cb,
       
   460 						(gpointer)msg_hdr->hdr_req );
       
   461 
       
   462 	g_hash_table_destroy (status_options); //ToDo: get clarity on freeing this resource
       
   463 	g_hash_table_destroy (status_ids);
       
   464   	iso_logger( "%s", "Out - telepathy_contacts_send_presence\n" );
       
   465 
       
   466 }
       
   467 
       
   468 /*! /brief This function will be called for presence of each contact
       
   469  *  key has got the state and value status
       
   470  *
       
   471  *  /param key : hash table key ie presence state
       
   472  *  /param value : hash table value ie presence information
       
   473  *  /param presence : user data
       
   474  *  /remark presence object is logically unused ?
       
   475  *
       
   476  *  /return : void
       
   477  */
       
   478 static void
       
   479 telepathy_presences_foreach (gpointer        *key,
       
   480 		      gpointer        *value,
       
   481 		      TelepathyPresence **presence)
       
   482 {
       
   483 
       
   484 	TelepathyPresenceState state;
       
   485 	const gchar        *message = NULL;
       
   486 
       
   487 	*presence = telepathy_presence_new();
       
   488   	iso_logger( "%s", "In - telepathy_presences_foreach\n" );
       
   489   	
       
   490   	iso_logger( "%s%s", "state : ", (gchar *)key );
       
   491 
       
   492 	state = telepathy_telepathy_presence_state_from_str((const gchar *)
       
   493 							 key);
       
   494 	telepathy_presence_set_state(*presence, state);
       
   495 
       
   496 	message = g_value_get_string((GValue *)
       
   497 				     g_hash_table_lookup((GHashTable *)
       
   498 							 value, "message"));
       
   499   	iso_logger( "%s%s", "message : ", (const gchar *)message );
       
   500 
       
   501 	send_presence_to_client((const gchar *)key, message );
       
   502 	
       
   503 	if (*presence) {
       
   504 		g_object_unref (*presence);
       
   505 	}
       
   506 	
       
   507   	iso_logger( "%s", "Out - telepathy_presences_foreach\n" );
       
   508 		
       
   509 }
       
   510 
       
   511 /*! /brief This function will be called for each contact. Contact name is 
       
   512  *  got from the contcat handle, telepathy_presences_foreach is called 
       
   513  *  to get the presence from value of the hash table
       
   514  *
       
   515  * @param key : hash table key ie contact handle id
       
   516  * @param value : hash table value ie presence
       
   517  * @return : void
       
   518  */
       
   519 static void
       
   520 telepathy_presence_handle_foreach (gpointer        *key,
       
   521 		      gpointer        *value)
       
   522 {
       
   523 	gint                 handle_id;
       
   524 	GHashTable          *presences_hash;
       
   525 	GValue              *gvalue;
       
   526 	GObject      		*presence;
       
   527 	gchar              **handle_name = NULL;
       
   528 	GArray              *handles;
       
   529 	GError         		*error = NULL;
       
   530 
       
   531 	handle_id = GPOINTER_TO_INT(key);
       
   532 
       
   533   	iso_logger( "%s", "In - telepathy_presence_handle_foreach\n" );
       
   534 	
       
   535 	handles = g_array_new (FALSE, FALSE, sizeof (gint));
       
   536 	g_array_append_val (handles, handle_id);
       
   537 	if(!tp_conn_inspect_handles(DBUS_G_PROXY(globalCon.conn),
       
   538 			       TP_CONN_HANDLE_TYPE_CONTACT, handles,
       
   539 			       &handle_name, &error))
       
   540 		{
       
   541 		iso_logger( "%s%s", "InspectHandle Error: ", error->message );
       
   542 		g_clear_error (&error);
       
   543 		g_array_free (handles, TRUE);
       
   544 		if( handle_name )
       
   545 		{
       
   546 		g_strfreev (handle_name);
       
   547 		}
       
   548 		return;
       
   549 		}
       
   550 
       
   551   	if (handle_name)
       
   552 	  	{
       
   553 	  	iso_logger( "%s%s", "handle_name : ", *handle_name );
       
   554 		glob_handle_name = *handle_name;
       
   555 	  	}
       
   556 	
       
   557 	gvalue = g_value_array_get_nth((GValueArray *) value, 1);
       
   558 	
       
   559 	presences_hash = (GHashTable *) g_value_get_boxed(gvalue);
       
   560 	
       
   561 	g_hash_table_foreach(presences_hash,
       
   562 			     (GHFunc) telepathy_presences_foreach, &presence );
       
   563 			     
       
   564 	g_clear_error (&error);
       
   565 	g_array_free (handles, TRUE);
       
   566 	g_strfreev (handle_name);
       
   567 			     
       
   568   	iso_logger( "%s", "Out - telepathy_presence_handle_foreach\n" );
       
   569 	
       
   570 }
       
   571 
       
   572 /*! /brief Registered as a signal for presence updates of 
       
   573  *  other users. This function will be called whenevr a contact
       
   574  *  to whom this user has subscribed for presence changes his 
       
   575  *  presence state or status message.
       
   576  * 
       
   577  *  /param proxy : unused
       
   578  *  /param handle_hash : hash table containing presence information
       
   579  *  as hash value, contact handle as key
       
   580  *  /return : void
       
   581  */
       
   582 void
       
   583 telepathy_presence_handler(DBusGProxy      *proxy,
       
   584 			   GHashTable      *handle_hash)
       
   585 {
       
   586 	GSList *data = NULL;
       
   587     UNUSED_FORMAL_PARAM(proxy);
       
   588   	iso_logger( "%s", "In - telepathy_presence_handler\n" );
       
   589 	g_hash_table_foreach(handle_hash, (GHFunc) telepathy_presence_handle_foreach, data);
       
   590   	iso_logger( "%s", "Out - telepathy_presence_handler\n" );
       
   591 
       
   592 }
       
   593 
       
   594 
       
   595 /*! /brief sends presence of the client to n/w server thru' gabble
       
   596  *  calls telepathy_contacts_send_presence to send presence
       
   597  *
       
   598  *  /param msghdr request header that will be passed back to client
       
   599  *  /param message_type_err unused
       
   600  *  /remark please remove this unused variable(why was this added?)
       
   601  *
       
   602  *  /return : error code on failure, 0 on success
       
   603  */
       
   604 void send_presence( presence_msg_struct* msg_hdr , gint message_type_err ) 
       
   605   {
       
   606   	TelepathyPresence        *presence;
       
   607 	TelepathyPresenceState state;
       
   608     UNUSED_FORMAL_PARAM(message_type_err);
       
   609   
       
   610   	iso_logger( "%s", "In - send_presence\n" );
       
   611 
       
   612     //changing own presence status
       
   613 	presence = telepathy_presence_new();
       
   614 	state = telepathy_telepathy_presence_state_from_str(msg_hdr->useravailability);   
       
   615 	telepathy_presence_set_state(presence, state);
       
   616 
       
   617 	//if (msg_hdr->statustext != NULL)
       
   618 		telepathy_presence_set_status (presence, msg_hdr->statustext);
       
   619 	
       
   620     telepathy_contacts_send_presence(DBUS_G_PROXY(globalCon.pres_iface), presence, msg_hdr);
       
   621    	
       
   622    	g_object_unref (presence);
       
   623   	iso_logger( "%s", "Out - send_presence\n" );
       
   624   }
       
   625 
       
   626 /*! /brief parses the message buffer. aPresenceStatus and aStatustext
       
   627  *  pointers updated
       
   628  *
       
   629  *  /param aMsgBuffer message buffer
       
   630  *  /param aPresenceStatus after call to this function it will
       
   631  *  have the presence status to be updated
       
   632  *  /param aStatustext after call to this function it will
       
   633  *  have the status text to be updated
       
   634  * 
       
   635  *  /param msg_len : msg_len no. of bytes in msg_buf
       
   636  *  /return returns error code on failure, or 0
       
   637  */
       
   638 int parse_for_presence( gchar* msgbuffer, gchar** presencestatus, 
       
   639 	gchar** statustext, gint msg_len ) 
       
   640 {
       
   641 
       
   642    gint len = 0;
       
   643    gint err=0;
       
   644    gchar *user_availability =NULL;
       
   645    gchar *status_text =NULL;
       
   646    
       
   647    iso_logger( "%s", "In - parse_for_presence\n" );
       
   648    //calculating len for request header
       
   649    //skip the msg_hdr part
       
   650    len += sizeof( message_hdr_req );
       
   651    
       
   652    // reading a availability from buffer
       
   653    err = parse_a_string( msgbuffer, &user_availability, &len, msg_len );
       
   654    *presencestatus=user_availability;
       
   655    if ( err < 0 ) 
       
   656 		{
       
   657 		return err;
       
   658 		}
       
   659    // reading a status text
       
   660    err = parse_a_string( msgbuffer, &status_text, &len, msg_len );
       
   661    *statustext=status_text;
       
   662   
       
   663 iso_logger( "%s", "Out - parse_for_presence\n" );
       
   664 // need to check.....
       
   665 return 1;	
       
   666 }
       
   667 
       
   668 /*! /brief Parses the message from client and sends the presence 
       
   669  *  to n/w server. A err is returned if there is any parse error
       
   670  *  or out of memory condition
       
   671  *
       
   672  *  /param buf : buffer to be parsed
       
   673  *  /param buf_len : buffer length
       
   674  *  /return : error code if any, 0 on success 
       
   675  */
       
   676 int action_parse_presence( gchar* buf, gint buf_len ) 
       
   677 
       
   678 {
       
   679 	gchar* presencestatus = NULL;
       
   680 	gchar* statustext = NULL;
       
   681 	message_hdr_req *msg_hdr = NULL;
       
   682 	gint message_type_err = 0;
       
   683 	presence_msg_struct* msg_struct = NULL;
       
   684 	//parse user availability and status text .. 	
       
   685 	iso_logger( "%s", "In - action_parse_presence\n" );
       
   686 	message_type_err = parse_for_presence( buf, 
       
   687 			&presencestatus,&statustext, buf_len );
       
   688 		//if <= 0 there is some error in the message formation
       
   689 	if ( message_type_err >  0 ) 
       
   690 		{
       
   691 		
       
   692 		//send message
       
   693 		msg_hdr = ( message_hdr_req* ) malloc( sizeof( message_hdr_req ) );
       
   694 		if ( NULL == msg_hdr ) 
       
   695 			{
       
   696 			return MEM_ALLOCATION_ERROR;
       
   697 			}
       
   698 	    memset( msg_hdr, '\0', sizeof( message_hdr_req ) );
       
   699 	    //read message header from buffer
       
   700 	    memcpy( msg_hdr, buf, sizeof( message_hdr_req ) );
       
   701 	    
       
   702 	    //fill the contacts and message into a struct
       
   703 	    msg_struct = ( presence_msg_struct* ) malloc ( sizeof ( presence_msg_struct ) );
       
   704 	    if ( NULL == msg_struct ) 
       
   705 			{
       
   706 			free ( msg_hdr );
       
   707 			return MEM_ALLOCATION_ERROR;
       
   708 			}
       
   709 		msg_struct->hdr_req = msg_hdr;
       
   710 		msg_struct->useravailability = presencestatus;
       
   711 		msg_struct->statustext = statustext;
       
   712 		//send presence should be called
       
   713 		 send_presence( msg_struct, message_type_err );		
       
   714 		}
       
   715 	
       
   716 	//free ( msg_hdr );
       
   717 	iso_logger( "%s", "Out - action_parse_send\n" );
       
   718 	//return error on failure, or no. of contacts message sent to on success
       
   719 	return message_type_err;
       
   720 		
       
   721 			
       
   722 			
       
   723 }
       
   724 
       
   725 
       
   726 /*! /brief callback for updating own presence. This function 
       
   727  *  sends response to client
       
   728  *
       
   729  *  /param proxy : unused
       
   730  *  /param error : error if any
       
   731  *  /param message : message header  
       
   732  *  /return : void
       
   733  */
       
   734 void update_status_cb(DBusGProxy *proxy, GError* error, gpointer userdata)
       
   735 {
       
   736 
       
   737 	int err = 0;
       
   738 
       
   739 	message_hdr_req* msg_hdr = ( message_hdr_req* ) userdata;
       
   740 	// create the msg queue
       
   741 	//user data is of type message_hdr_req
       
   742 	iso_logger( "%s", "In - update_status_cb\n" );
       
   743     UNUSED_FORMAL_PARAM(proxy);
       
   744 	if ( NULL != error ) 
       
   745 		{
       
   746 		//There was some error
       
   747 		//send the response for the msg_hdr request to client
       
   748 		err = send_response_to_client( msg_hdr, error->code, 0 );
       
   749 		}
       
   750 	else 
       
   751 		{
       
   752 		//send the response for the msg_hdr request to client
       
   753 		err = send_response_to_client( msg_hdr, 0, 1 );
       
   754 		}
       
   755 						
       
   756 	//free( msg_hdr );
       
   757 
       
   758 	if ( err < 0 )
       
   759 		{
       
   760 		// failed to deliver
       
   761 		iso_logger( "%s", "failed to deliver\n" );
       
   762 		}
       
   763 	iso_logger( "%s", "Out - update_status_cb\n" );
       
   764 		
       
   765 }
       
   766 
       
   767 
       
   768 /*! /brief Sends presence of conatct to the client. Forms 
       
   769  *  the proper presence message format and sends that to client
       
   770  *
       
   771  *  /param : availability user availability
       
   772  *  /param : text custom status message set by user
       
   773  *  /return : error code on failure, 0 on success
       
   774  */
       
   775 int send_presence_to_client( const gchar *availability , const gchar *text )
       
   776 	{
       
   777 		int len=0;
       
   778 		int pri = MSG_PRI_NORMAL;
       
   779 		char rmsg[MAX_MSG_SIZE];
       
   780 		int index = 0;
       
   781 		int err;
       
   782 		message_hdr_resp* msg_resp = NULL;
       
   783 		int result = 0;
       
   784 		int timeout = NO_WAIT;
       
   785 	   
       
   786 	   	iso_logger( "%s", "In - send_presence_to_client\n" );
       
   787 
       
   788 	   	msg_resp = ( message_hdr_resp* ) malloc( sizeof( message_hdr_resp ) );
       
   789 		if ( NULL == msg_resp ) 
       
   790 			{
       
   791 			return MEM_ALLOCATION_ERROR;	
       
   792 			}
       
   793 		memset( msg_resp, '\0', sizeof( message_hdr_resp ) );
       
   794 	    memset( msg_resp, '\0', sizeof( message_hdr_resp ) );
       
   795 		msg_resp->hdr_req.message_type = EPresenceNotification;
       
   796 		msg_resp->hdr_req.protocol_id = 1;
       
   797 		msg_resp->hdr_req.session_id = 1;
       
   798 		msg_resp->hdr_req.request_id =1;
       
   799 		msg_resp->response = 1;
       
   800 		msg_resp->error_type = 0;
       
   801 	    memset( rmsg, '\0', MAX_MSG_SIZE );
       
   802 		memcpy( rmsg, msg_resp, sizeof( message_hdr_resp ) );
       
   803 	    index += sizeof( message_hdr_resp );
       
   804 	    //putting sender in message queue
       
   805 	    len = strlen(glob_handle_name);
       
   806 	    strcpy( rmsg + index, glob_handle_name );
       
   807 	    index += len + 1;
       
   808 	    //putting user availability   
       
   809 	    len = strlen(availability);
       
   810 	    strcpy( rmsg + index, availability );
       
   811 	    index += len + 1;
       
   812 	    // puttin status text
       
   813 	    len = strlen(text);
       
   814 	    strcpy( rmsg + index, text );
       
   815 	    index += len + 1;
       
   816 	    result = MsgQSend( RESPONSE_QUEUE, rmsg, index, 
       
   817 							pri, timeout, &err);
       
   818 							
       
   819 		free ( msg_resp );
       
   820 	    if(result<0)
       
   821 	    {
       
   822 	    	return FALSE;
       
   823 	    }
       
   824 	   
       
   825 	    
       
   826 		iso_logger( "%s", "message is:\n");
       
   827 	   	iso_logger( "%s", "Out - send_presence_to_client\n" );
       
   828 
       
   829 		return TRUE;
       
   830    }