telepathygabble/src/telepathy-helpers.c
changeset 10 59927b2d3b75
parent 0 d0f3a028347a
equal deleted inserted replaced
0:d0f3a028347a 10:59927b2d3b75
     1 /*
       
     2  * telepathy-helpers.c - Source for some Telepathy D-Bus helper functions
       
     3  * Copyright (C) 2005 Collabora Ltd.
       
     4  * 
       
     5  *
       
     6  * This library is free software; you can redistribute it and/or
       
     7  * modify it under the terms of the GNU Lesser General Public
       
     8  * License as published by the Free Software Foundation; either
       
     9  * version 2.1 of the License, or (at your option) any later version.
       
    10  *
       
    11  * This library is distributed in the hope that it will be useful,
       
    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    14  * Lesser General Public License for more details.
       
    15  *
       
    16  * You should have received a copy of the GNU Lesser General Public
       
    17  * License along with this library; if not, write to the Free Software
       
    18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
       
    19  */
       
    20 
       
    21 #include <stdlib.h>
       
    22 #include <dbus/dbus-glib.h>
       
    23 #include "telepathy-helpers.h"
       
    24 
       
    25 #ifdef EMULATOR
       
    26 #include "libgabble_wsd_solution.h"
       
    27 
       
    28 	GET_STATIC_VAR_FROM_TLS(busCon,gabble_helpers,DBusGConnection*)
       
    29 	#define busCon (*GET_WSD_VAR_NAME(busCon,gabble_helpers, s)())
       
    30 	
       
    31 	GET_STATIC_VAR_FROM_TLS(bus_proxy,gabble_helpers,DBusGProxy*)
       
    32 	#define bus_proxy (*GET_WSD_VAR_NAME(bus_proxy,gabble_helpers, s)())	
       
    33 	
       
    34 #endif
       
    35 	
       
    36 
       
    37 DBusGConnection *
       
    38 tp_get_bus ()
       
    39 {
       
    40 #ifndef EMULATOR
       
    41   static DBusGConnection *busCon = NULL;
       
    42 #endif
       
    43 
       
    44   if (busCon == NULL)
       
    45     {
       
    46       GError *error = NULL;
       
    47 
       
    48       busCon = dbus_g_bus_get (DBUS_BUS_SESSION/*DBUS_BUS_STARTER*/, &error);
       
    49 
       
    50       if (busCon == NULL)
       
    51         {
       
    52           g_warning ("Failed to connect to starter bus: %s", error->message);
       
    53           exit (1);
       
    54         }
       
    55     }
       
    56 
       
    57   return busCon;
       
    58 }
       
    59 
       
    60 DBusGProxy *
       
    61 tp_get_bus_proxy ()
       
    62 {
       
    63 #ifndef EMULATOR
       
    64   static DBusGProxy *bus_proxy = NULL;
       
    65 #endif
       
    66 
       
    67   if (bus_proxy == NULL)
       
    68     {
       
    69       DBusGConnection *bus = tp_get_bus ();
       
    70 
       
    71       bus_proxy = dbus_g_proxy_new_for_name (bus,
       
    72                                             "org.freedesktop.DBus",
       
    73                                             "/org/freedesktop/DBus",
       
    74                                             "org.freedesktop.DBus");
       
    75 
       
    76       if (bus_proxy == NULL)
       
    77         g_error ("Failed to get proxy object for bus.");
       
    78     }
       
    79 
       
    80   return bus_proxy;
       
    81 }
       
    82 
       
    83 static void _list_builder (gpointer key, gpointer value, gpointer data);
       
    84 
       
    85 GSList *
       
    86 tp_hash_to_key_value_list (GHashTable *hash)
       
    87 {
       
    88   GSList *ret = NULL;
       
    89 
       
    90   g_hash_table_foreach (hash, _list_builder, &ret);
       
    91 
       
    92   return ret;
       
    93 }
       
    94 
       
    95 void
       
    96 tp_key_value_list_free (GSList *list)
       
    97 {
       
    98   GSList *iter;
       
    99 
       
   100   for (iter = list; iter; iter = g_slist_next(iter))
       
   101   {
       
   102     g_free (iter->data);
       
   103   }
       
   104 
       
   105   g_slist_free (list);
       
   106 }
       
   107 
       
   108 static void _list_builder (gpointer key, gpointer value, gpointer data)
       
   109 {
       
   110   GSList **list = (GSList **) data;
       
   111   TpKeyValue *kv = g_new0 (TpKeyValue, 1);
       
   112 
       
   113   kv->key = key;
       
   114   kv->value = value;
       
   115 
       
   116   *list = g_slist_prepend (*list, kv);
       
   117 }
       
   118