libtelepathy/src/tp-helpers.c
changeset 10 59927b2d3b75
parent 0 d0f3a028347a
equal deleted inserted replaced
0:d0f3a028347a 10:59927b2d3b75
     1 /*
       
     2  * tp-helpers.c - Source for various helper functions
       
     3  * for telepathy implementation
       
     4  *
       
     5  * Copyright (C) 2005 Collabora Ltd.
       
     6  * 
       
     7  *
       
     8  * This library is free software; you can redistribute it and/or
       
     9  * modify it under the terms of the GNU Lesser General Public
       
    10  * License as published by the Free Software Foundation; either
       
    11  * version 2.1 of the License, or (at your option) any later version.
       
    12  *
       
    13  * This library is distributed in the hope that it will be useful,
       
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    16  * Lesser General Public License for more details.
       
    17  *
       
    18  * You should have received a copy of the GNU Lesser General Public
       
    19  * License along with this library; if not, write to the Free Software
       
    20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
       
    21  */
       
    22 
       
    23 #include <dbus/dbus-glib.h>
       
    24 #include <stdlib.h>
       
    25 #include "tp-helpers.h"
       
    26 
       
    27 
       
    28 #define CM_CONFS_DIR "/usr/share/telepathy/managers/"
       
    29 #define CM_CONF_SUFFIX ".manager"
       
    30 #define CM_CONF_GROUP "ConnectionManager"
       
    31 #define FILE_SEPARATOR ','
       
    32 #define PROTO "Proto "
       
    33 
       
    34 
       
    35 #ifdef EMULATOR
       
    36 #include "libtelepathy_wsd_solution.h"
       
    37 #endif
       
    38 
       
    39 #ifdef EMULATOR
       
    40 	
       
    41 	GET_STATIC_VAR_FROM_TLS(bus_proxy,tp_helpers,DBusGProxy *)
       
    42 	#define bus_proxy (*GET_WSD_VAR_NAME(bus_proxy,tp_helpers,s)())
       
    43 	
       
    44 	GET_STATIC_VAR_FROM_TLS(bus1,tp_helpers,DBusGConnection *)
       
    45 	#define bus1 (*GET_WSD_VAR_NAME(bus1,tp_helpers,s)())	
       
    46 	
       
    47 #endif
       
    48 	
       
    49 static void _list_builder(gpointer key, gpointer value, gpointer data);
       
    50 
       
    51 DBusGConnection *
       
    52 tp_get_bus ()
       
    53 {
       
    54 #ifndef EMULATOR
       
    55   static DBusGConnection *bus1 = NULL;
       
    56 #endif
       
    57   
       
    58 
       
    59   if (bus1 == NULL)
       
    60     {
       
    61       GError *error = NULL;
       
    62 
       
    63       bus1 = dbus_g_bus_get (/*DBUS_BUS_STARTER*/DBUS_BUS_SESSION, &error);
       
    64 
       
    65       if (bus1 == NULL)
       
    66         g_error ("Failed to connect to starter bus: %s", error->message);
       
    67     }
       
    68 
       
    69   return bus1;
       
    70 }
       
    71 
       
    72 DBusGProxy *
       
    73 tp_get_bus_proxy ()
       
    74 {
       
    75 #ifndef EMULATOR
       
    76   static DBusGProxy *bus_proxy = NULL;
       
    77 #endif
       
    78 
       
    79   if (bus_proxy == NULL)
       
    80     {
       
    81       DBusGConnection *bus = tp_get_bus ();
       
    82 
       
    83       bus_proxy = dbus_g_proxy_new_for_name (bus,
       
    84                                             "org.freedesktop.DBus",
       
    85                                             "/org/freedesktop/DBus",
       
    86                                             "org.freedesktop.DBus");
       
    87 
       
    88       if (bus_proxy == NULL)
       
    89         g_error ("Failed to get proxy object for bus.");
       
    90     }
       
    91 
       
    92   return bus_proxy;
       
    93 }
       
    94 
       
    95 GSList *
       
    96 tp_hash_to_key_value_list(GHashTable *hash)
       
    97 {
       
    98   GSList *ret = NULL;
       
    99   g_hash_table_foreach(hash, _list_builder, &ret);
       
   100   return ret;
       
   101 }
       
   102 
       
   103 void
       
   104 tp_key_value_list_free(GSList *list)
       
   105 {
       
   106   GSList *iter;
       
   107 
       
   108   for (iter = list; iter; iter = g_slist_next(iter))
       
   109   {
       
   110     g_free(iter->data);
       
   111   }
       
   112   g_slist_free(list);
       
   113 }
       
   114 
       
   115 static void _list_builder(gpointer key, gpointer value, gpointer data)
       
   116 {
       
   117   GSList **list = (GSList **)data;
       
   118   TpKeyValue *kv = g_new0(TpKeyValue, 1);
       
   119   kv->key = key;
       
   120   kv->value = value;
       
   121   *list = g_slist_prepend(*list, kv);
       
   122 }
       
   123 
       
   124 GSList *tp_connmgr_list_cms(void)
       
   125 {
       
   126   GError *error = NULL;
       
   127   GDir *dir;
       
   128   const gchar *filename;
       
   129   gchar **name;
       
   130   gchar *absolute_filepath;
       
   131   GSList *cms = NULL;
       
   132 
       
   133 
       
   134   /* Read the configuration file directory */
       
   135   if ((dir = g_dir_open(CM_CONFS_DIR, 0, &error)) == NULL)
       
   136   {
       
   137     g_printerr("Error opening directory %s: %s", CM_CONFS_DIR,
       
   138         error->message);
       
   139     return NULL;
       
   140   }
       
   141 
       
   142   while ((filename = g_dir_read_name(dir)) != NULL)
       
   143     /* Skip the file if it doesn't contain the required file suffix */
       
   144     if (g_str_has_suffix(filename, CM_CONF_SUFFIX))
       
   145     {
       
   146       absolute_filepath = g_strconcat(CM_CONFS_DIR, filename, NULL);
       
   147       name = g_strsplit(filename, ".", 0);
       
   148       cms = g_slist_append(cms, *name);
       
   149     }
       
   150 
       
   151   return cms;
       
   152 }
       
   153 
       
   154 TpConnMgrInfo *tp_connmgr_get_info(gchar *cm)
       
   155 {
       
   156   GError *error = NULL;
       
   157   GKeyFile *file;
       
   158   gsize len;
       
   159   gchar *absolute_filepath;
       
   160   gchar **protocols;
       
   161   TpConnMgrInfo *cm_info = (TpConnMgrInfo *)malloc(sizeof(TpConnMgrInfo));
       
   162   cm_info->protocol_info = g_hash_table_new(g_str_hash, g_str_equal);
       
   163 
       
   164   absolute_filepath = g_strconcat(CM_CONFS_DIR, cm, CM_CONF_SUFFIX, NULL);
       
   165 
       
   166   file = g_key_file_new();
       
   167   if (!g_key_file_load_from_file
       
   168       (file, absolute_filepath, G_KEY_FILE_NONE, &error))
       
   169   {
       
   170     /* handle error */
       
   171     g_printerr("%s", error->message);
       
   172     g_error_free(error);
       
   173     return NULL;
       
   174   }
       
   175   g_key_file_set_list_separator(file, FILE_SEPARATOR);
       
   176 
       
   177   cm_info->name = g_key_file_get_string(file, CM_CONF_GROUP,
       
   178             "Name", &error);
       
   179   if (!(cm_info->name))
       
   180   {
       
   181     /* handle error and free dynamic memory */
       
   182     g_printerr("%s", error->message);
       
   183     g_error_free(error);
       
   184     g_key_file_free(file);
       
   185     g_free(absolute_filepath);
       
   186     free(cm_info);
       
   187     return NULL;
       
   188   }
       
   189   cm_info->bus_name = g_key_file_get_string(file, CM_CONF_GROUP,
       
   190             "BusName", &error);
       
   191   if (!(cm_info->bus_name))
       
   192   {
       
   193     /* handle error and free dynamic memory */
       
   194     g_printerr("%s", error->message);
       
   195     g_error_free(error);
       
   196     g_key_file_free(file);
       
   197     g_free(absolute_filepath);
       
   198     g_free(cm_info->name);
       
   199     free(cm_info);
       
   200     return NULL;
       
   201   }
       
   202   cm_info->object_path = g_key_file_get_string(file,CM_CONF_GROUP,
       
   203             "ObjectPath", &error);
       
   204   if (!(cm_info->object_path))
       
   205   {
       
   206     /* handle error and free dynamic memory */
       
   207     g_printerr("%s", error->message);
       
   208     g_error_free(error);
       
   209     g_key_file_free(file);
       
   210     g_free(absolute_filepath);
       
   211     g_free(cm_info->name);
       
   212     g_free(cm_info->bus_name);
       
   213     free(cm_info);
       
   214     return NULL;
       
   215   }
       
   216   cm_info->protocols = g_key_file_get_string_list(file, CM_CONF_GROUP, 
       
   217                               "Protos", &len, &error);
       
   218   if (!(cm_info->protocols))
       
   219   {
       
   220     /* handle error and free dynamic memory */
       
   221     g_printerr("%s", error->message);
       
   222     g_error_free(error);
       
   223     g_key_file_free(file);
       
   224     g_free(absolute_filepath);
       
   225     g_free(cm_info->name);
       
   226     g_free(cm_info->bus_name);
       
   227     g_free(cm_info->object_path);
       
   228     free(cm_info);
       
   229     return NULL;
       
   230   }
       
   231 
       
   232   for(protocols=cm_info->protocols; *protocols; protocols++)
       
   233   {
       
   234     gchar **keys;
       
   235     gchar *key_value;
       
   236     gchar *proto_group =  g_strconcat(PROTO, *protocols, NULL);
       
   237     TpConnMgrProtInfo *cm_prot_info = 
       
   238                        (TpConnMgrProtInfo *)malloc(sizeof(TpConnMgrProtInfo));
       
   239     cm_prot_info->default_params = g_hash_table_new(g_str_hash, g_str_equal);
       
   240     
       
   241     cm_prot_info->mandatory_params = g_key_file_get_string_list(file, 
       
   242                                    proto_group, "MandatoryParams", &len, &error);
       
   243     if (!(cm_prot_info->mandatory_params))
       
   244     {
       
   245       g_printerr("%s", error->message);
       
   246       g_error_free(error);
       
   247     }
       
   248     cm_prot_info->optional_params = g_key_file_get_string_list(file, 
       
   249                                    proto_group, "OptionalParams", &len, &error);
       
   250     if (!(cm_prot_info->optional_params))
       
   251     {
       
   252       g_printerr("%s", error->message);
       
   253       g_error_free(error);
       
   254     }
       
   255 
       
   256     keys = g_key_file_get_keys (file, proto_group, &len, &error);
       
   257     if (!(keys))
       
   258     {
       
   259       g_printerr("%s", error->message);
       
   260       g_error_free(error);
       
   261     }
       
   262     else
       
   263     {
       
   264       for(; *keys; keys++)
       
   265       {
       
   266         if(g_str_has_prefix(*keys, "default"))
       
   267         {
       
   268           key_value = g_key_file_get_string(file, proto_group, 
       
   269                                                    *keys, &error);
       
   270           if (!(key_value))
       
   271           {
       
   272             g_printerr("%s", error->message);
       
   273             g_error_free(error);
       
   274           }
       
   275           else
       
   276             g_hash_table_insert(cm_prot_info->default_params, 
       
   277                                 g_strdup(*keys), key_value);
       
   278         }
       
   279       }
       
   280    }
       
   281     
       
   282 
       
   283    g_hash_table_insert(cm_info->protocol_info, g_strdup(*protocols), 
       
   284                         cm_prot_info);
       
   285          
       
   286   }
       
   287 
       
   288   g_key_file_free(file);
       
   289   g_free(absolute_filepath);
       
   290   return cm_info;
       
   291 
       
   292 }