isolationserver/isoserver/src/isoavtar.c
changeset 10 59927b2d3b75
parent 0 d0f3a028347a
equal deleted inserted replaced
0:d0f3a028347a 10:59927b2d3b75
     1 /*
       
     2 * ============================================================================
       
     3 *  Name        : isoconnectionmanager.c
       
     4 *  Part of     : isolation server.
       
     5 *  Version     : %version: 6 %
       
     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 
       
    32 #include "tp-conn.h"
       
    33 #include "isoavtar.h"
       
    34 #include "stdio.h"
       
    35 #include "isoservermain.h"
       
    36 #include "msgliterals.h"
       
    37 #include "msgqlib.h"
       
    38 #include "msg_enums.h"
       
    39 #include "isoutils.h"
       
    40 
       
    41 
       
    42 #include <string.h>
       
    43 #include <stdlib.h>
       
    44 
       
    45 /*
       
    46  * !	\file 
       
    47  *	Implements the functions in isoavtar.h
       
    48  */
       
    49 
       
    50 
       
    51 /*! \brief callback for updating own avtar. This function 
       
    52  *  sends response to client
       
    53  *
       
    54  *  \param proxy : unused
       
    55  *  \param avatar_sha1: token string 
       
    56  *  \param error : error if any
       
    57  *  \param message : message header  
       
    58  *  \return : void
       
    59  */
       
    60 void update_avtar_cb(DBusGProxy *proxy, char* avatar_sha1, GError* error, gpointer userdata)
       
    61     {
       
    62 
       
    63     int err = 0;
       
    64     //user data is of type message_hdr_req
       
    65     message_hdr_req* msg_hdr = ( message_hdr_req* ) userdata;
       
    66     // create the msg queue
       
    67 
       
    68     iso_logger( "%s", "In - update_avtar_cb\n" );
       
    69     UNUSED_FORMAL_PARAM(proxy);
       
    70 
       
    71     //msg_hdr should never be NULL
       
    72 
       
    73     if ( NULL != error ) 
       
    74         {
       
    75         //There was some error
       
    76         //send the response for the msg_hdr request to client
       
    77 
       
    78         iso_logger( "%s", "error in update_avtar_cb" );
       
    79         err = send_response_to_client( msg_hdr, error->code, 0 );
       
    80         g_error_free(error);
       
    81         }
       
    82     else 
       
    83         {
       
    84         //If the set avatar was successful then send the msg_hdr alongwith the avatar sha1 to client
       
    85         //<todo> : sending the avatar sha1
       
    86 
       
    87         iso_logger( "%s %s", "avatar sha1 is : ", avatar_sha1 );
       
    88         err = send_response_to_client( msg_hdr, 0, 1 );
       
    89         free ( avatar_sha1 );
       
    90         }
       
    91 
       
    92     if ( err < 0 )
       
    93         {
       
    94         // failed to deliver
       
    95         iso_logger( "%s", "failed to deliver\n" );
       
    96         }
       
    97     free( msg_hdr );
       
    98 
       
    99     iso_logger( "%s", "Out - update_avtar_cb\n" );
       
   100 
       
   101     }
       
   102 
       
   103 /*! \brief sends avtar of the client to n/w server thru' gabble
       
   104  *  calls tp_conn_iface_avatars_set_avatar_async to set avatar
       
   105  *  
       
   106  *  \param avtarcontent : avatar image content
       
   107  *  \param mimetype : mime type
       
   108  *  \param msghdr request header that will be passed back to client
       
   109  *
       
   110  *  \return : error code on failure, 0 on success
       
   111  */
       
   112 gint send_avtar( GArray* avtarcontent , char * mimetype , message_hdr_req *msg_hdr ) 
       
   113     {
       
   114 
       
   115     gint error = 0;
       
   116     DBusGProxy* avatar_face = NULL;
       
   117     message_hdr_req *hdr_req = NULL;
       
   118     iso_logger( "%s", "In - send_avtar\n" );	
       
   119 
       
   120     //Allocate the memory for header req and initialize that to 0 
       
   121     hdr_req = ( message_hdr_req* ) calloc ( sizeof( message_hdr_req ), 1 );
       
   122     if ( NULL == hdr_req )
       
   123         {
       
   124         return MEM_ALLOCATION_ERROR;
       
   125         }
       
   126     //msg_hdr can never be NULL.. handled in the case
       
   127     memcpy( hdr_req, msg_hdr,  sizeof( message_hdr_req ) );
       
   128 
       
   129     //Registering for signal to be done at different loation
       
   130     /*dbus_g_proxy_connect_signal (DBUS_G_PROXY(globalCon.conn), "AvatarUpdated",
       
   131                                  G_CALLBACK (avatar_updated_signal),
       
   132                                  NULL, NULL);       */
       
   133     //get the interface
       
   134     avatar_face = tp_conn_get_interface (
       
   135             globalCon.conn, TELEPATHY_CONN_IFACE_AVATAR_QUARK);
       
   136 
       
   137     if ( avatar_face )
       
   138         {
       
   139         //make call to the lib telepathy to set the avatar. update_avtar_cb is registered as the callback
       
   140         tp_conn_iface_avatars_set_avatar_async (avatar_face, avtarcontent, mimetype, update_avtar_cb, (gpointer)hdr_req);
       
   141         }
       
   142     else {
       
   143     error = TP_AVATAR_IFACE_ERROR; 
       
   144     }
       
   145 
       
   146     iso_logger( "%s", "Out - send_avtar\n" );
       
   147     return error;
       
   148     }
       
   149 
       
   150 /*! \brief callback for clearing own avtar. This function 
       
   151  *  sends response to client
       
   152  *
       
   153  *  \param proxy : unused
       
   154  *  \param error : error if any
       
   155  *  \param userdata : message header  
       
   156  *  \return : void
       
   157  */
       
   158 void clear_avtar_cb( DBusGProxy *proxy, GError* error, gpointer userdata )
       
   159     {
       
   160 
       
   161     int err = 0;
       
   162     //user data is of type message_hdr_req
       
   163     message_hdr_req* msg_hdr = ( message_hdr_req* ) userdata;
       
   164 
       
   165     iso_logger( "%s", "In - clear_avtar_cb\n" );
       
   166     UNUSED_FORMAL_PARAM(proxy);
       
   167 
       
   168     //msg_hdr should never be NULL
       
   169 
       
   170     if ( NULL != error ) 
       
   171         {
       
   172         //There was some error
       
   173         //send the response for the msg_hdr request to client
       
   174 
       
   175         iso_logger( "%s", "error in clear_avtar_cb" );
       
   176         err = send_response_to_client( msg_hdr, error->code, 0 );
       
   177         g_error_free(error);
       
   178         }
       
   179     else 
       
   180         {
       
   181         err = send_response_to_client( msg_hdr, 0, 1 );
       
   182         }
       
   183 
       
   184     if ( err < 0 )
       
   185         {
       
   186         // failed to deliver
       
   187         iso_logger( "%s", "failed to deliver\n" );
       
   188         }
       
   189     free( msg_hdr );
       
   190     iso_logger( "%s", "Out - clear_avtar_cb\n" );
       
   191 
       
   192     }
       
   193 /*! \brief clears the self avtar 
       
   194  *
       
   195  *  \param msghdr request header that will be passed back to client
       
   196  *
       
   197  *  \return : error code on failure, 0 on success
       
   198  */
       
   199 gint clear_avatar( message_hdr_req *msg_hdr )
       
   200     {
       
   201     gint error = 0;
       
   202     DBusGProxy* avatar_face = NULL;
       
   203     message_hdr_req *hdr_req = NULL;
       
   204     iso_logger( "%s", "In - clear_avtar\n" );    
       
   205 
       
   206     //Allocate the memory for header req and initialize that to 0 
       
   207     hdr_req = ( message_hdr_req* ) calloc ( sizeof( message_hdr_req ), 1 );
       
   208     if ( NULL == hdr_req ) 
       
   209         {
       
   210         return MEM_ALLOCATION_ERROR;    
       
   211         }
       
   212     //msg_hdr can never be NULL.. handled in the case
       
   213     memcpy( hdr_req, msg_hdr,  sizeof( message_hdr_req ) );
       
   214 
       
   215     //get the interface
       
   216     avatar_face = tp_conn_get_interface (
       
   217             globalCon.conn, TELEPATHY_CONN_IFACE_AVATAR_QUARK);
       
   218 
       
   219     if ( avatar_face )
       
   220         {
       
   221         tp_conn_iface_avatars_clear_avatar_async(avatar_face, clear_avtar_cb, (gpointer)hdr_req);
       
   222         }
       
   223     else {
       
   224     error = TP_AVATAR_IFACE_ERROR; 
       
   225     }
       
   226 
       
   227     iso_logger( "%s", "Out - clear_avtar\n" );
       
   228 
       
   229     return error;
       
   230 
       
   231     }
       
   232 /*! \brief clears the self avtar 
       
   233  *  \param rmsg : message buffer to be parsed
       
   234  *  \param msg_struct : request header 
       
   235  *  \param result : message buffer length
       
   236  *  \param avtarcontent : pointer to the avatar image data
       
   237  * 
       
   238  *  \return : error code on failure, 0 on success
       
   239  */
       
   240 gint update_own_avatar(char* rmsg,message_hdr_resp* msg_struct,gint result, GArray** avtarcontent)
       
   241     {
       
   242     int len = 0;
       
   243     int err = 0;
       
   244     static gboolean fetch_avatar_data = 0;
       
   245     static gchar *mimetype = NULL;
       
   246 
       
   247 
       
   248     iso_logger( "%s", "In - EUpdateOwnAvtar\n" );
       
   249 
       
   250     if(!fetch_avatar_data)
       
   251         {
       
   252         //parse mime type
       
   253         //calculating len for request header
       
   254         //skip the msg_hdr part
       
   255 
       
   256         len += sizeof( message_hdr_req );
       
   257 
       
   258         *avtarcontent = g_array_new ( FALSE, FALSE, sizeof ( gchar ) );
       
   259         if ( NULL == *avtarcontent )
       
   260             {
       
   261             return MEM_ALLOCATION_ERROR;
       
   262             }
       
   263 
       
   264         // reading a mimetype from buffer
       
   265         err = parse_a_string( rmsg, &mimetype, &len, result );
       
   266 
       
   267         if ( err < 0 )
       
   268             {
       
   269             goto oom;
       
   270             }
       
   271 
       
   272         fetch_avatar_data = 1;     
       
   273         }
       
   274     else
       
   275         {
       
   276         len = sizeof( message_hdr_req );
       
   277         }
       
   278 
       
   279 
       
   280     g_array_append_vals( *avtarcontent, rmsg + len, result - len );
       
   281 
       
   282 
       
   283     if(! msg_struct->hdr_req.continue_flag )
       
   284         {
       
   285         err = send_avtar( *avtarcontent, mimetype, &(msg_struct->hdr_req) );  
       
   286 
       
   287         fetch_avatar_data = 0; //reset the value so that if set avatar requested for
       
   288         //falling of to goto oom
       
   289 
       
   290         }
       
   291     else {
       
   292     //Continue flag is set and read the data again return 0
       
   293     return 0;
       
   294     }
       
   295 
       
   296     oom:
       
   297     //Free the memory if err or operation complete ( sent to libeteleapthy)
       
   298     if ( *avtarcontent )
       
   299         {
       
   300         g_array_free( *avtarcontent, (*avtarcontent)->len );
       
   301         *avtarcontent = NULL;
       
   302         }
       
   303     if( mimetype )
       
   304         {
       
   305         free ( mimetype );
       
   306         }
       
   307     return err;
       
   308     }