dbus-gproxy.c

00001 /* -*- mode: C; c-file-style: "gnu" -*- */
00002 /* dbus-gproxy.c Proxy for remote objects
00003  *
00004  * Copyright (C) 2003, 2004, 2005 Red Hat, Inc.
00005  * Copyright (C) 2005 Nokia
00006  *
00007  * Licensed under the Academic Free License version 2.1
00008  * 
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  * 
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  *
00023  */
00024 #include <dbus/dbus-glib.h>
00025 #include <dbus/dbus-glib-lowlevel.h>
00026 #include <dbus/dbus-signature.h>
00027 #include "dbus-gutils.h"
00028 #include "dbus-gsignature.h"
00029 #include "dbus-gvalue.h"
00030 #include "dbus-gvalue-utils.h"
00031 #include "dbus-gobject.h"
00032 #include <string.h>
00033 #include <glib/gi18n.h>
00034 #include <gobject/gvaluecollector.h>
00035 
00036 #define DBUS_G_PROXY_CALL_TO_ID(x) (GPOINTER_TO_UINT(x))
00037 #define DBUS_G_PROXY_ID_TO_CALL(x) (GUINT_TO_POINTER(x))
00038 #define DBUS_G_PROXY_GET_PRIVATE(o)  \
00039        (G_TYPE_INSTANCE_GET_PRIVATE ((o), DBUS_TYPE_G_PROXY, DBusGProxyPrivate))
00040 
00041 
00052 typedef struct _DBusGProxyManager DBusGProxyManager;
00053 
00054 typedef struct _DBusGProxyPrivate DBusGProxyPrivate;
00055 
00059 struct _DBusGProxyPrivate
00060 {
00061   DBusGProxyManager *manager; 
00062   char *name;                 
00063   char *path;                 
00064   char *interface;            
00066   DBusGProxyCall *name_call;  
00067   guint for_owner : 1;        
00068   guint associated : 1;       
00070   /* FIXME: make threadsafe? */
00071   guint call_id_counter;      
00073   GData *signal_signatures;   
00075   GHashTable *pending_calls;  
00076 };
00077 
00078 static void dbus_g_proxy_init               (DBusGProxy      *proxy);
00079 static void dbus_g_proxy_class_init         (DBusGProxyClass *klass);
00080 static GObject *dbus_g_proxy_constructor    (GType                  type,
00081                                              guint                  n_construct_properties,
00082                                              GObjectConstructParam *construct_properties);
00083 static void     dbus_g_proxy_set_property       (GObject               *object,
00084                                                  guint                  prop_id,
00085                                                  const GValue          *value,
00086                                                  GParamSpec            *pspec);
00087 static void     dbus_g_proxy_get_property       (GObject               *object,
00088                                                  guint                  prop_id,
00089                                                  GValue                *value,
00090                                                  GParamSpec            *pspec);
00091 
00092 static void dbus_g_proxy_finalize           (GObject         *object);
00093 static void dbus_g_proxy_dispose            (GObject         *object);
00094 static void dbus_g_proxy_destroy            (DBusGProxy      *proxy);
00095 static void dbus_g_proxy_emit_remote_signal (DBusGProxy      *proxy,
00096                                              DBusMessage     *message);
00097 
00098 static DBusGProxyCall *manager_begin_bus_call (DBusGProxyManager    *manager,
00099                                                const char          *method,
00100                                                DBusGProxyCallNotify notify,
00101                                                gpointer             data,
00102                                                GDestroyNotify       destroy,
00103                                                GType                first_arg_type,
00104                                                ...);
00105 static guint dbus_g_proxy_begin_call_internal (DBusGProxy          *proxy,
00106                                                const char          *method,
00107                                                DBusGProxyCallNotify notify,
00108                                                gpointer             data,
00109                                                GDestroyNotify       destroy,
00110                                                GValueArray         *args,
00111                                                int timeout );
00112 static gboolean dbus_g_proxy_end_call_internal (DBusGProxy        *proxy,
00113                                                 guint              call_id,
00114                                                 GError           **error,
00115                                                 GType              first_arg_type,
00116                                                 va_list            args);
00117 
00122 typedef struct
00123 {
00124   GSList *proxies; 
00126   char name[4]; 
00131 } DBusGProxyList;
00132 
00138 struct _DBusGProxyManager
00139 {
00140   GStaticMutex lock; 
00141   int refcount;      
00142   DBusConnection *connection; 
00144   DBusGProxy *bus_proxy; 
00146   GHashTable *proxy_lists; 
00149   GHashTable *owner_names; 
00153   GSList *unassociated_proxies;     
00157 };
00158 
00159 static DBusGProxyManager *dbus_g_proxy_manager_ref    (DBusGProxyManager *manager);
00160 static DBusHandlerResult  dbus_g_proxy_manager_filter (DBusConnection    *connection,
00161                                                        DBusMessage       *message,
00162                                                        void              *user_data);
00163 
00164 
00166 #define LOCK_MANAGER(mgr)   (g_static_mutex_lock (&(mgr)->lock))
00167 
00168 #define UNLOCK_MANAGER(mgr) (g_static_mutex_unlock (&(mgr)->lock))
00169 
00170 static int g_proxy_manager_slot = -1;
00171 
00172 /* Lock controlling get/set manager as data on each connection */
00173 static GStaticMutex connection_g_proxy_lock = G_STATIC_MUTEX_INIT;
00174 
00175 static DBusGProxyManager*
00176 dbus_g_proxy_manager_get (DBusConnection *connection)
00177 {
00178   DBusGProxyManager *manager;
00179 
00180   dbus_connection_allocate_data_slot (&g_proxy_manager_slot);
00181   if (g_proxy_manager_slot < 0)
00182     g_error ("out of memory");
00183   
00184   g_static_mutex_lock (&connection_g_proxy_lock);
00185   
00186   manager = dbus_connection_get_data (connection, g_proxy_manager_slot);
00187   if (manager != NULL)
00188     {
00189       dbus_connection_free_data_slot (&g_proxy_manager_slot);
00190       dbus_g_proxy_manager_ref (manager);
00191       g_static_mutex_unlock (&connection_g_proxy_lock);
00192       return manager;
00193     }
00194   
00195   manager = g_new0 (DBusGProxyManager, 1);
00196 
00197   manager->refcount = 1;
00198   manager->connection = connection;
00199 
00200   g_static_mutex_init (&manager->lock);
00201 
00202   /* Proxy managers keep the connection alive, which means that
00203    * DBusGProxy indirectly does. To free a connection you have to free
00204    * all the proxies referring to it.
00205    */
00206   dbus_connection_ref (manager->connection);
00207 
00208   dbus_connection_set_data (connection, g_proxy_manager_slot,
00209                             manager, NULL);
00210 
00211   dbus_connection_add_filter (connection, dbus_g_proxy_manager_filter,
00212                               manager, NULL);
00213   
00214   g_static_mutex_unlock (&connection_g_proxy_lock);
00215   
00216   return manager;
00217 }
00218 
00219 static DBusGProxyManager * 
00220 dbus_g_proxy_manager_ref (DBusGProxyManager *manager)
00221 {
00222   g_assert (manager != NULL);
00223   g_assert (manager->refcount > 0);
00224 
00225   LOCK_MANAGER (manager);
00226   
00227   manager->refcount += 1;
00228 
00229   UNLOCK_MANAGER (manager);
00230 
00231   return manager;
00232 }
00233 
00234 static void
00235 dbus_g_proxy_manager_unref (DBusGProxyManager *manager)
00236 {
00237   g_assert (manager != NULL);
00238   g_assert (manager->refcount > 0);
00239 
00240   LOCK_MANAGER (manager);
00241   manager->refcount -= 1;
00242   if (manager->refcount == 0)
00243     {
00244       UNLOCK_MANAGER (manager);
00245 
00246       if (manager->bus_proxy)
00247         g_object_unref (manager->bus_proxy);
00248 
00249       if (manager->proxy_lists)
00250         {
00251           /* can't have any proxies left since they hold
00252            * a reference to the proxy manager.
00253            */
00254           g_assert (g_hash_table_size (manager->proxy_lists) == 0);
00255           
00256           g_hash_table_destroy (manager->proxy_lists);
00257           manager->proxy_lists = NULL;
00258 
00259         }
00260 
00261       if (manager->owner_names)
00262         {
00263           /* Since we destroyed all proxies, none can be tracking
00264            * name owners
00265            */
00266           g_assert (g_hash_table_size (manager->owner_names) == 0);
00267 
00268           g_hash_table_destroy (manager->owner_names);
00269           manager->owner_names = NULL;
00270         }
00271 
00272       g_assert (manager->unassociated_proxies == NULL);
00273       
00274       g_static_mutex_free (&manager->lock);
00275 
00276       g_static_mutex_lock (&connection_g_proxy_lock);
00277 
00278       dbus_connection_remove_filter (manager->connection, dbus_g_proxy_manager_filter,
00279                                      manager);
00280       
00281       dbus_connection_set_data (manager->connection,
00282                                 g_proxy_manager_slot,
00283                                 NULL, NULL);
00284 
00285       g_static_mutex_unlock (&connection_g_proxy_lock);
00286       
00287       dbus_connection_unref (manager->connection);
00288       g_free (manager);
00289 
00290       dbus_connection_free_data_slot (&g_proxy_manager_slot);
00291     }
00292   else
00293     {
00294       UNLOCK_MANAGER (manager);
00295     }
00296 }
00297 
00298 static guint
00299 tristring_hash (gconstpointer key)
00300 {
00301   const char *p = key;
00302   guint h = *p;
00303 
00304   if (h)
00305     {
00306       for (p += 1; *p != '\0'; p++)
00307         h = (h << 5) - h + *p;
00308     }
00309 
00310   /* skip nul and do the next substring */
00311   for (p += 1; *p != '\0'; p++)
00312     h = (h << 5) - h + *p;
00313 
00314   /* skip nul again and another substring */
00315   for (p += 1; *p != '\0'; p++)
00316     h = (h << 5) - h + *p;
00317   
00318   return h;
00319 }
00320 
00321 static gboolean
00322 strequal_len (const char *a,
00323               const char *b,
00324               size_t     *lenp)
00325 {
00326   size_t a_len;
00327   size_t b_len;
00328 
00329   a_len = strlen (a);
00330   b_len = strlen (b);
00331 
00332   if (a_len != b_len)
00333     return FALSE;
00334 
00335   if (memcmp (a, b, a_len) != 0)
00336     return FALSE;
00337   
00338   *lenp = a_len;
00339 
00340   return TRUE;
00341 }
00342 
00343 static gboolean
00344 tristring_equal (gconstpointer  a,
00345                  gconstpointer  b)
00346 {
00347   const char *ap = a;
00348   const char *bp = b;
00349   size_t len;
00350 
00351   if (!strequal_len (ap, bp, &len))
00352     return FALSE;
00353 
00354   ap += len + 1;
00355   bp += len + 1;
00356 
00357   if (!strequal_len (ap, bp, &len))
00358     return FALSE;
00359 
00360   ap += len + 1;
00361   bp += len + 1;
00362 
00363   if (strcmp (ap, bp) != 0)
00364     return FALSE;
00365   
00366   return TRUE;
00367 }
00368 
00369 static char*
00370 tristring_alloc_from_strings (size_t      padding_before,
00371                               const char *name,
00372                               const char *path,
00373                               const char *interface)
00374 {
00375   size_t name_len, iface_len, path_len, len;
00376   char *tri;
00377   
00378   if (name)
00379     name_len = strlen (name);
00380   else
00381     name_len = 0;
00382 
00383   path_len = strlen (path);
00384   
00385   iface_len = strlen (interface);
00386 
00387   tri = g_malloc (padding_before + name_len + path_len + iface_len + 3);
00388 
00389   len = padding_before;
00390   
00391   if (name)
00392     memcpy (&tri[len], name, name_len);
00393 
00394   len += name_len;
00395   tri[len] = '\0';
00396   len += 1;
00397 
00398   g_assert (len == (padding_before + name_len + 1));
00399   
00400   memcpy (&tri[len], path, path_len);
00401   len += path_len;
00402   tri[len] = '\0';
00403   len += 1;
00404 
00405   g_assert (len == (padding_before + name_len + path_len + 2));
00406   
00407   memcpy (&tri[len], interface, iface_len);
00408   len += iface_len;
00409   tri[len] = '\0';
00410   len += 1;
00411 
00412   g_assert (len == (padding_before + name_len + path_len + iface_len + 3));
00413 
00414   return tri;
00415 }
00416 
00417 static char*
00418 tristring_from_proxy (DBusGProxy *proxy)
00419 {
00420   DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
00421 
00422   return tristring_alloc_from_strings (0,
00423                                        priv->name,
00424                                        priv->path,
00425                                        priv->interface);
00426 }
00427 
00428 static char*
00429 tristring_from_message (DBusMessage *message)
00430 {
00431   const char *path;
00432   const char *interface;
00433 
00434   path = dbus_message_get_path (message);
00435   interface = dbus_message_get_interface (message);
00436 
00437   g_assert (path);
00438   g_assert (interface);
00439   
00440   return tristring_alloc_from_strings (0,
00441                                        dbus_message_get_sender (message),
00442                                        path, interface);
00443 }
00444 
00445 static DBusGProxyList*
00446 g_proxy_list_new (DBusGProxy *first_proxy)
00447 {
00448   DBusGProxyList *list;
00449   DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(first_proxy);
00450   
00451   list = (void*) tristring_alloc_from_strings (G_STRUCT_OFFSET (DBusGProxyList, name),
00452                                                priv->name,
00453                                                priv->path,
00454                                                priv->interface);
00455   list->proxies = NULL;
00456 
00457   return list;
00458 }
00459 
00460 static void
00461 g_proxy_list_free (DBusGProxyList *list)
00462 {
00463   /* we don't hold a reference to the proxies in the list,
00464    * as they ref the GProxyManager
00465    */
00466   g_slist_free (list->proxies);  
00467 
00468   g_free (list);
00469 }
00470 
00471 static char*
00472 g_proxy_get_match_rule (DBusGProxy *proxy)
00473 {
00474   DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
00475   /* FIXME Escaping is required here */
00476   
00477   if (priv->name)
00478     return g_strdup_printf ("type='signal',sender='%s',path='%s',interface='%s'",
00479                             priv->name, priv->path, priv->interface);
00480   else
00481     return g_strdup_printf ("type='signal',path='%s',interface='%s'",
00482                             priv->path, priv->interface);
00483 }
00484 
00485 typedef struct
00486 {
00487   char *name;
00488   guint refcount;
00489 } DBusGProxyNameOwnerInfo;
00490 
00491 static gint
00492 find_name_in_info (gconstpointer a, gconstpointer b)
00493 {
00494   const DBusGProxyNameOwnerInfo *info = a;
00495   const char *name = b;
00496 
00497   if (info == NULL || info->name == NULL)
00498     return 1;
00499   return strcmp (info->name, name);
00500 }
00501 
00502 typedef struct
00503 {
00504   const char *name;
00505   const char *owner;
00506   DBusGProxyNameOwnerInfo *info;
00507 } DBusGProxyNameOwnerForeachData;
00508 
00509 static void
00510 name_owner_foreach (gpointer key, gpointer val, gpointer data)
00511 {
00512   const char *owner;
00513   DBusGProxyNameOwnerForeachData *foreach_data;
00514   GSList *names;
00515   GSList *link;
00516 
00517   owner = key;
00518   names = val;
00519   foreach_data = data;
00520 
00521   if (foreach_data->owner != NULL)
00522     return;
00523 
00524   g_assert (foreach_data->info == NULL);
00525 
00526   link = g_slist_find_custom (names, foreach_data->name, find_name_in_info);
00527   if (link)
00528     {
00529       foreach_data->owner = owner;
00530       foreach_data->info = link->data;
00531     }
00532 }
00533 
00534 static gboolean
00535 dbus_g_proxy_manager_lookup_name_owner (DBusGProxyManager        *manager,
00536                                         const char               *name,
00537                                         DBusGProxyNameOwnerInfo **info,
00538                                         const char              **owner)
00539 {
00540   DBusGProxyNameOwnerForeachData foreach_data;
00541 
00542   foreach_data.name = name;
00543   foreach_data.owner = NULL;
00544   foreach_data.info = NULL;
00545   
00546   g_hash_table_foreach (manager->owner_names, name_owner_foreach, &foreach_data);
00547 
00548   *info = foreach_data.info;
00549   *owner = foreach_data.owner;
00550   return *info != NULL;
00551 }
00552 
00553 static void
00554 insert_nameinfo (DBusGProxyManager       *manager,
00555                  const char              *owner,
00556                  DBusGProxyNameOwnerInfo *info)
00557 {
00558   GSList *names;
00559   gboolean insert;
00560 
00561   names = g_hash_table_lookup (manager->owner_names, owner);
00562 
00563   /* Only need to g_hash_table_insert the first time */
00564   insert = (names == NULL);
00565 
00566   names = g_slist_append (names, info); 
00567 
00568   if (insert)
00569     g_hash_table_insert (manager->owner_names, g_strdup (owner), names);
00570 }
00571 
00572 static void
00573 dbus_g_proxy_manager_monitor_name_owner (DBusGProxyManager  *manager,
00574                                          const char         *owner,
00575                                          const char         *name)
00576 {
00577   GSList *names;
00578   GSList *link;
00579   DBusGProxyNameOwnerInfo *nameinfo;
00580 
00581   names = g_hash_table_lookup (manager->owner_names, owner);
00582   link = g_slist_find_custom (names, name, find_name_in_info);
00583   
00584   if (!link)
00585     {
00586       nameinfo = g_new0 (DBusGProxyNameOwnerInfo, 1);
00587       nameinfo->name = g_strdup (name);
00588       nameinfo->refcount = 1;
00589 
00590       insert_nameinfo (manager, owner, nameinfo);
00591     }
00592   else
00593     {
00594       nameinfo = link->data;
00595       nameinfo->refcount++;
00596     }
00597 }
00598 
00599 static void
00600 dbus_g_proxy_manager_unmonitor_name_owner (DBusGProxyManager  *manager,
00601                                            const char         *name)
00602 {
00603   DBusGProxyNameOwnerInfo *info;
00604   const char *owner;
00605   gboolean ret;
00606 
00607   ret = dbus_g_proxy_manager_lookup_name_owner (manager, name, &info, &owner);
00608   g_assert (ret);
00609   g_assert (info != NULL);
00610   g_assert (owner != NULL);
00611 
00612   info->refcount--;
00613   if (info->refcount == 0)
00614     {
00615       GSList *names;
00616       GSList *link;
00617 
00618       names = g_hash_table_lookup (manager->owner_names, owner);
00619       link = g_slist_find_custom (names, name, find_name_in_info);
00620       names = g_slist_delete_link (names, link);
00621       if (names != NULL)
00622         g_hash_table_insert (manager->owner_names, g_strdup (owner), names);
00623       else
00624         g_hash_table_remove (manager->owner_names, owner);
00625 
00626       g_free (info->name);
00627       g_free (info);
00628     }
00629 }
00630 
00631 typedef struct
00632 {
00633   const char *name;
00634   GSList *destroyed;
00635 } DBusGProxyUnassociateData;
00636 
00637 static void
00638 unassociate_proxies (gpointer key, gpointer val, gpointer user_data)
00639 {
00640   DBusGProxyList *list;
00641   const char *name;
00642   GSList *tmp;
00643   DBusGProxyUnassociateData *data;
00644 
00645   list = val;
00646   data = user_data;
00647   name = data->name;
00648   
00649   for (tmp = list->proxies; tmp; tmp = tmp->next)
00650     {
00651       DBusGProxy *proxy = DBUS_G_PROXY (tmp->data);
00652       DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
00653       DBusGProxyManager *manager;
00654 
00655       manager = priv->manager;
00656 
00657       if (!strcmp (priv->name, name))
00658         {
00659           if (!priv->for_owner)
00660             {
00661               if (priv->name_call != NULL)
00662                 {
00663                   dbus_g_proxy_cancel_call (manager->bus_proxy,
00664                                             priv->name_call);
00665                   priv->name_call = NULL;
00666                 }
00667 
00668               priv->associated = FALSE;
00669               manager->unassociated_proxies = g_slist_prepend (manager->unassociated_proxies, proxy);
00670             }
00671           else
00672             {
00673               data->destroyed = g_slist_prepend (data->destroyed, proxy);
00674               /* make contents of list into weak pointers in case the objects
00675                * unref each other when disposing */
00676               g_object_add_weak_pointer (G_OBJECT (proxy),
00677                   &(data->destroyed->data));
00678             }
00679         }
00680     }
00681 }
00682 
00683 static void
00684 dbus_g_proxy_manager_replace_name_owner (DBusGProxyManager  *manager,
00685                                          const char         *name,
00686                                          const char         *prev_owner,
00687                                          const char         *new_owner)
00688 {
00689   GSList *names;
00690           
00691   if (prev_owner[0] == '\0')
00692     {
00693       GSList *tmp;
00694       GSList *removed;
00695 
00696       /* We have a new service, look at unassociated proxies */
00697 
00698       removed = NULL;
00699 
00700       for (tmp = manager->unassociated_proxies; tmp ; tmp = tmp->next)
00701         {
00702           DBusGProxy *proxy = tmp->data;
00703           DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
00704 
00705           if (!strcmp (priv->name, name))
00706             {
00707               removed = g_slist_prepend (removed, tmp);
00708               
00709               dbus_g_proxy_manager_monitor_name_owner (manager, new_owner, name);
00710               priv->associated = TRUE;
00711             }
00712         }
00713 
00714       for (tmp = removed; tmp; tmp = tmp->next)
00715         manager->unassociated_proxies = g_slist_delete_link (manager->unassociated_proxies, tmp->data);
00716       g_slist_free (removed);
00717     }
00718   else
00719     {
00720       DBusGProxyNameOwnerInfo *info;
00721       GSList *link;
00722 
00723       /* Name owner changed or deleted */ 
00724 
00725       names = g_hash_table_lookup (manager->owner_names, prev_owner);
00726 
00727       info = NULL;
00728       if (names != NULL)
00729         {
00730           link = g_slist_find_custom (names, name, find_name_in_info);
00731 
00732           if (link != NULL)
00733             {
00734               info = link->data;
00735           
00736               names = g_slist_delete_link (names, link);
00737 
00738               if (names == NULL)
00739                 g_hash_table_remove (manager->owner_names, prev_owner);
00740             }
00741         }
00742 
00743       if (new_owner[0] == '\0')
00744         {
00745           DBusGProxyUnassociateData data;
00746           GSList *tmp;
00747 
00748           data.name = name;
00749           data.destroyed = NULL;
00750 
00751           /* A service went away, we need to unassociate proxies */
00752           g_hash_table_foreach (manager->proxy_lists,
00753                                 unassociate_proxies, &data);
00754 
00755           UNLOCK_MANAGER (manager);
00756 
00757           /* the destroyed list's data pointers are weak pointers, so that we
00758            * don't end up calling destroy on proxies which have already been
00759            * freed up as a result of other ones being destroyed */
00760           for (tmp = data.destroyed; tmp; tmp = tmp->next)
00761             if (tmp->data != NULL)
00762               {
00763                 g_object_remove_weak_pointer (G_OBJECT (tmp->data),
00764                     &(tmp->data));
00765                 dbus_g_proxy_destroy (tmp->data);
00766               }
00767           g_slist_free (data.destroyed);
00768 
00769           LOCK_MANAGER (manager);
00770 
00771           if (info)
00772             {
00773               g_free (info->name);
00774               g_free (info);
00775             }
00776         }
00777       else if (info)
00778         {
00779           insert_nameinfo (manager, new_owner, info);
00780         }
00781     }
00782 }
00783 
00784 static void
00785 got_name_owner_cb (DBusGProxy       *bus_proxy,
00786                    DBusGProxyCall   *call,
00787                    void             *user_data)
00788 {
00789   DBusGProxy *proxy = user_data;
00790   DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
00791   GError *error;
00792   char *owner;
00793 
00794   error = NULL;
00795   owner = NULL;
00796 
00797   LOCK_MANAGER (priv->manager);
00798 
00799   if (!dbus_g_proxy_end_call (bus_proxy, call, &error,
00800                               G_TYPE_STRING, &owner,
00801                               G_TYPE_INVALID))
00802     {
00803       if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_NAME_HAS_NO_OWNER)
00804         {
00805           priv->manager->unassociated_proxies = g_slist_prepend (priv->manager->unassociated_proxies, proxy);
00806         }
00807       else if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_REMOTE_EXCEPTION)
00808         g_warning ("Couldn't get name owner (%s): %s",
00809                    dbus_g_error_get_name (error),
00810                    error->message);
00811       else
00812         g_warning ("Couldn't get name owner (code %d): %s",
00813                    error->code, error->message);
00814       g_clear_error (&error);
00815       goto out;
00816     }
00817   else
00818     {
00819       dbus_g_proxy_manager_monitor_name_owner (priv->manager, owner, priv->name);
00820       priv->associated = TRUE;
00821     }
00822 
00823  out:
00824   priv->name_call = NULL;
00825   UNLOCK_MANAGER (priv->manager);
00826   g_free (owner);
00827 }
00828 
00829 static char *
00830 get_name_owner (DBusConnection     *connection,
00831                 const char         *name,
00832                 GError            **error)
00833 {
00834   DBusError derror;
00835   DBusMessage *request, *reply;
00836   char *base_name;
00837   
00838   dbus_error_init (&derror);
00839 
00840   base_name = NULL;
00841   reply = NULL;
00842 
00843   request = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
00844                                           DBUS_PATH_DBUS,
00845                                           DBUS_INTERFACE_DBUS,
00846                                           "GetNameOwner");
00847   if (request == NULL)
00848     g_error ("Out of memory");
00849   
00850   if (!dbus_message_append_args (request, 
00851                                  DBUS_TYPE_STRING, &name, 
00852                                  DBUS_TYPE_INVALID))
00853     g_error ("Out of memory");
00854 
00855   reply =
00856     dbus_connection_send_with_reply_and_block (connection,
00857                                                request,
00858                                                2000, &derror);
00859   if (reply == NULL)
00860     goto error;
00861 
00862   if (dbus_set_error_from_message (&derror, reply))
00863     goto error;
00864 
00865   if (!dbus_message_get_args (reply, &derror, 
00866                               DBUS_TYPE_STRING, &base_name, 
00867                               DBUS_TYPE_INVALID))
00868     goto error;
00869 
00870   base_name = g_strdup (base_name);
00871   goto out;
00872 
00873  error:
00874   g_assert (dbus_error_is_set (&derror));
00875   dbus_set_g_error (error, &derror);
00876   dbus_error_free (&derror);
00877 
00878  out:
00879   if (request)
00880     dbus_message_unref (request);
00881   if (reply)
00882     dbus_message_unref (reply);
00883 
00884   return base_name;
00885 }
00886 
00887 
00888 static void
00889 dbus_g_proxy_manager_register (DBusGProxyManager *manager,
00890                                DBusGProxy        *proxy)
00891 {
00892   DBusGProxyList *list;
00893   DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
00894 
00895   LOCK_MANAGER (manager);
00896 
00897   if (manager->proxy_lists == NULL)
00898     {
00899       g_assert (manager->owner_names == NULL);
00900 
00901       list = NULL;
00902       manager->proxy_lists = g_hash_table_new_full (tristring_hash,
00903                                                     tristring_equal,
00904                                                     NULL,
00905                                                     (GFreeFunc) g_proxy_list_free);
00906       manager->owner_names = g_hash_table_new_full (g_str_hash,
00907                                                     g_str_equal,
00908                                                     g_free,
00909                                                     NULL);
00910       /* FIXME - for now we listen for all NameOwnerChanged; once
00911        * Anders' detail patch lands we should add individual rules
00912        */
00913       dbus_bus_add_match (manager->connection,
00914                           "type='signal',sender='" DBUS_SERVICE_DBUS
00915                           "',path='" DBUS_PATH_DBUS
00916                           "',interface='" DBUS_INTERFACE_DBUS
00917                           "',member='NameOwnerChanged'",
00918                           NULL);
00919     }
00920   else
00921     {
00922       char *tri;
00923 
00924       tri = tristring_from_proxy (proxy);
00925       
00926       list = g_hash_table_lookup (manager->proxy_lists, tri);
00927 
00928       g_free (tri);
00929     }
00930       
00931   if (list == NULL)
00932     {
00933       list = g_proxy_list_new (proxy);
00934       
00935       g_hash_table_replace (manager->proxy_lists,
00936                             list->name, list);
00937     }
00938 
00939   if (list->proxies == NULL && priv->name)
00940     {
00941       /* We have to add the match rule to the server,
00942        * but only if the server is a message bus,
00943        * not if it's a peer.
00944        */
00945        char *rule;
00946        
00947        rule = g_proxy_get_match_rule (proxy);
00948        
00949        /* We don't check for errors; it's not like anyone would handle them, and
00950         * we don't want a round trip here.
00951         */
00952        dbus_bus_add_match (manager->connection,
00953                            rule, NULL);
00954        
00955        g_free (rule);
00956     }
00957 
00958   g_assert (g_slist_find (list->proxies, proxy) == NULL);
00959   
00960   list->proxies = g_slist_prepend (list->proxies, proxy);
00961 
00962   if (!priv->for_owner)
00963     {
00964       const char *owner;
00965       DBusGProxyNameOwnerInfo *info;
00966 
00967       if (!dbus_g_proxy_manager_lookup_name_owner (manager, priv->name, &info, &owner))
00968         {
00969           priv->name_call = manager_begin_bus_call (manager, "GetNameOwner",
00970                                                      got_name_owner_cb,
00971                                                      proxy, NULL,
00972                                                      G_TYPE_STRING,
00973                                                      priv->name, 
00974                                                      G_TYPE_INVALID);
00975           
00976           priv->associated = FALSE;
00977         }
00978       else
00979         {
00980           info->refcount++;
00981           priv->associated = TRUE;
00982         }
00983     }
00984   
00985   UNLOCK_MANAGER (manager);
00986 }
00987 
00988 static void
00989 dbus_g_proxy_manager_unregister (DBusGProxyManager *manager,
00990                                 DBusGProxy        *proxy)
00991 {
00992   DBusGProxyList *list;
00993   DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
00994   char *tri;
00995   
00996   LOCK_MANAGER (manager);
00997 
00998 #ifndef G_DISABLE_CHECKS
00999   if (manager->proxy_lists == NULL)
01000     {
01001       g_warning ("Trying to unregister a proxy but there aren't any registered");
01002       return;
01003     }
01004 #endif
01005 
01006   tri = tristring_from_proxy (proxy);
01007   
01008   list = g_hash_table_lookup (manager->proxy_lists, tri);
01009 
01010 #ifndef G_DISABLE_CHECKS
01011   if (list == NULL)
01012     {
01013       g_warning ("Trying to unregister a proxy but it isn't registered");
01014       return;
01015     }
01016 #endif
01017 
01018   g_assert (g_slist_find (list->proxies, proxy) != NULL);
01019   
01020   list->proxies = g_slist_remove (list->proxies, proxy);
01021 
01022   g_assert (g_slist_find (list->proxies, proxy) == NULL);
01023 
01024   if (!priv->for_owner)
01025     {
01026       if (!priv->associated)
01027         {
01028           GSList *link;
01029 
01030           if (priv->name_call != 0)
01031             {
01032               dbus_g_proxy_cancel_call (manager->bus_proxy, priv->name_call);
01033               priv->name_call = 0;
01034             }
01035           else
01036             {
01037               link = g_slist_find (manager->unassociated_proxies, proxy);
01038               g_assert (link != NULL);
01039 
01040               manager->unassociated_proxies = g_slist_delete_link (manager->unassociated_proxies, link);
01041             }
01042         }
01043       else
01044         {
01045           g_assert (priv->name_call == 0);
01046           
01047           dbus_g_proxy_manager_unmonitor_name_owner (manager, priv->name);
01048         }
01049     }
01050 
01051   if (list->proxies == NULL)
01052     {
01053       char *rule;
01054       g_hash_table_remove (manager->proxy_lists,
01055                            tri);
01056       list = NULL;
01057 
01058       rule = g_proxy_get_match_rule (proxy);
01059       dbus_bus_remove_match (manager->connection,
01060                              rule, NULL);
01061       g_free (rule);
01062     }
01063   
01064   if (g_hash_table_size (manager->proxy_lists) == 0)
01065     {
01066       g_hash_table_destroy (manager->proxy_lists);
01067       manager->proxy_lists = NULL;
01068     }
01069 
01070   g_free (tri);
01071       
01072   UNLOCK_MANAGER (manager);
01073 }
01074 
01075 static void
01076 list_proxies_foreach (gpointer key,
01077                       gpointer value,
01078                       gpointer user_data)
01079 {
01080   DBusGProxyList *list;
01081   GSList **ret;
01082   GSList *tmp;
01083   
01084   list = value;
01085   ret = user_data;
01086 
01087   tmp = list->proxies;
01088   while (tmp != NULL)
01089     {
01090       DBusGProxy *proxy = DBUS_G_PROXY (tmp->data);
01091 
01092       g_object_ref (proxy);
01093       *ret = g_slist_prepend (*ret, proxy);
01094       
01095       tmp = tmp->next;
01096     }
01097 }
01098 
01099 static GSList*
01100 dbus_g_proxy_manager_list_all (DBusGProxyManager *manager)
01101 {
01102   GSList *ret;
01103 
01104   ret = NULL;
01105 
01106   if (manager->proxy_lists)
01107     {
01108       g_hash_table_foreach (manager->proxy_lists,
01109                             list_proxies_foreach,
01110                             &ret);
01111     }
01112 
01113   return ret;
01114 }
01115 
01116 static DBusHandlerResult
01117 dbus_g_proxy_manager_filter (DBusConnection    *connection,
01118                              DBusMessage       *message,
01119                              void              *user_data)
01120 {
01121   DBusGProxyManager *manager;
01122   
01123   if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
01124     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
01125 
01126   manager = user_data;
01127 
01128   dbus_g_proxy_manager_ref (manager);
01129   
01130   LOCK_MANAGER (manager);
01131   
01132   if (dbus_message_is_signal (message,
01133                               DBUS_INTERFACE_LOCAL,
01134                               "Disconnected"))
01135     {
01136       /* Destroy all the proxies, quite possibly resulting in unreferencing
01137        * the proxy manager and the connection as well.
01138        */
01139       GSList *all;
01140       GSList *tmp;
01141 
01142       all = dbus_g_proxy_manager_list_all (manager);
01143 
01144       tmp = all;
01145       while (tmp != NULL)
01146         {
01147           DBusGProxy *proxy;
01148 
01149           proxy = DBUS_G_PROXY (tmp->data);
01150 
01151           UNLOCK_MANAGER (manager);
01152           dbus_g_proxy_destroy (proxy);
01153           g_object_unref (G_OBJECT (proxy));
01154           LOCK_MANAGER (manager);
01155           
01156           tmp = tmp->next;
01157         }
01158 
01159       g_slist_free (all);
01160 
01161 #ifndef G_DISABLE_CHECKS
01162       if (manager->proxy_lists != NULL)
01163         g_warning ("Disconnection emitted \"destroy\" on all DBusGProxy, but somehow new proxies were created in response to one of those destroy signals. This will cause a memory leak.");
01164 #endif
01165     }
01166   else
01167     {
01168       char *tri;
01169       GSList *full_list;
01170       GSList *owned_names;
01171       GSList *tmp;
01172       const char *sender;
01173 
01174       /* First we handle NameOwnerChanged internally */
01175       if (dbus_message_is_signal (message,
01176                                   DBUS_INTERFACE_DBUS,
01177                                   "NameOwnerChanged"))
01178         {
01179           const char *name;
01180           const char *prev_owner;
01181           const char *new_owner;
01182           DBusError derr;
01183 
01184           dbus_error_init (&derr);
01185           if (!dbus_message_get_args (message,
01186                                       &derr,
01187                                       DBUS_TYPE_STRING,
01188                                       &name,
01189                                       DBUS_TYPE_STRING,
01190                                       &prev_owner,
01191                                       DBUS_TYPE_STRING,
01192                                       &new_owner,
01193                                       DBUS_TYPE_INVALID))
01194             {
01195               /* Ignore this error */
01196               dbus_error_free (&derr);
01197             }
01198           else if (manager->owner_names != NULL)
01199             {
01200               dbus_g_proxy_manager_replace_name_owner (manager, name, prev_owner, new_owner);
01201             }
01202         }
01203 
01204       sender = dbus_message_get_sender (message);
01205 
01206       /* dbus spec requires these, libdbus validates */
01207       g_assert (dbus_message_get_path (message) != NULL);
01208       g_assert (dbus_message_get_interface (message) != NULL);
01209       g_assert (dbus_message_get_member (message) != NULL);
01210       
01211       tri = tristring_from_message (message);
01212 
01213       if (manager->proxy_lists)
01214         {
01215           DBusGProxyList *owner_list;
01216           owner_list = g_hash_table_lookup (manager->proxy_lists, tri);
01217           if (owner_list)
01218             full_list = g_slist_copy (owner_list->proxies);
01219           else
01220             full_list = NULL;
01221         }
01222       else
01223         full_list = NULL;
01224 
01225       g_free (tri);
01226 
01227       if (manager->owner_names && sender)
01228         {
01229           owned_names = g_hash_table_lookup (manager->owner_names, sender);
01230           for (tmp = owned_names; tmp; tmp = tmp->next)
01231             {
01232               DBusGProxyList *owner_list;
01233               DBusGProxyNameOwnerInfo *nameinfo;
01234 
01235               nameinfo = tmp->data;
01236               g_assert (nameinfo->refcount > 0);
01237               tri = tristring_alloc_from_strings (0, nameinfo->name,
01238                                                   dbus_message_get_path (message),
01239                                                   dbus_message_get_interface (message));
01240 
01241               owner_list = g_hash_table_lookup (manager->proxy_lists, tri);
01242               if (owner_list != NULL)
01243                 full_list = g_slist_concat (full_list, g_slist_copy (owner_list->proxies));
01244               g_free (tri);
01245             }
01246         }
01247 
01248 #if 0
01249       g_print ("proxy got %s,%s,%s = list %p\n",
01250                tri,
01251                tri + strlen (tri) + 1,
01252                tri + strlen (tri) + 1 + strlen (tri + strlen (tri) + 1) + 1,
01253                list);
01254 #endif
01255       
01256       /* Emit the signal */
01257       
01258       g_slist_foreach (full_list, (GFunc) g_object_ref, NULL);
01259       
01260       for (tmp = full_list; tmp; tmp = tmp->next)
01261         {
01262           DBusGProxy *proxy;
01263           
01264           proxy = DBUS_G_PROXY (tmp->data);
01265           
01266           UNLOCK_MANAGER (manager);
01267           dbus_g_proxy_emit_remote_signal (proxy, message);
01268           g_object_unref (G_OBJECT (proxy));
01269           LOCK_MANAGER (manager);
01270         }
01271       g_slist_free (full_list);
01272     }
01273 
01274   UNLOCK_MANAGER (manager);
01275   dbus_g_proxy_manager_unref (manager);
01276   
01277   /* "Handling" signals doesn't make sense, they are for everyone
01278    * who cares
01279    */
01280   return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
01281 }
01282 
01283 
01284 
01285 /*      ---------- DBusGProxy --------------   */
01286 #define DBUS_G_PROXY_DESTROYED(proxy)  (DBUS_G_PROXY_GET_PRIVATE(proxy)->manager == NULL)
01287 
01288 static void
01289 marshal_dbus_message_to_g_marshaller (GClosure     *closure,
01290                                       GValue       *return_value,
01291                                       guint         n_param_values,
01292                                       const GValue *param_values,
01293                                       gpointer      invocation_hint,
01294                                       gpointer      marshal_data);
01295 enum
01296 {
01297   PROP_0,
01298   PROP_NAME,
01299   PROP_PATH,
01300   PROP_INTERFACE,
01301   PROP_CONNECTION
01302 };
01303 
01304 enum
01305 {
01306   DESTROY,
01307   RECEIVED,
01308   LAST_SIGNAL
01309 };
01310 
01311 static void *parent_class;
01312 static guint signals[LAST_SIGNAL] = { 0 };
01313 
01314 static void
01315 dbus_g_proxy_init (DBusGProxy *proxy)
01316 {
01317   DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
01318   
01319   g_datalist_init (&priv->signal_signatures);
01320   priv->pending_calls = g_hash_table_new_full (NULL, NULL, NULL,
01321                                 (GDestroyNotify) dbus_pending_call_unref);
01322   priv->name_call = 0;
01323   priv->associated = FALSE;
01324 }
01325 
01326 static GObject *
01327 dbus_g_proxy_constructor (GType                  type,
01328                           guint                  n_construct_properties,
01329                           GObjectConstructParam *construct_properties)
01330 {
01331   DBusGProxy *proxy;
01332   DBusGProxyClass *klass;
01333   GObjectClass *parent_class;
01334   DBusGProxyPrivate *priv;
01335 
01336   klass = DBUS_G_PROXY_CLASS (g_type_class_peek (DBUS_TYPE_G_PROXY));
01337 
01338   parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
01339 
01340   proxy = DBUS_G_PROXY (parent_class->constructor (type, n_construct_properties,
01341                                                     construct_properties));
01342 
01343   priv = DBUS_G_PROXY_GET_PRIVATE (proxy);
01344 
01345   /* if these assertions fail, a deriving class has not set our required
01346    * parameters - our own public constructors do return_if_fail checks
01347    * on these parameters being provided. unfortunately we can't assert
01348    * for manager because it's allowed to be NULL when tha mangager is
01349    * setting up a bus proxy for its own calls */
01350   g_assert (priv->path != NULL);
01351   g_assert (priv->interface != NULL);
01352 
01353   if (priv->manager != NULL)
01354     {
01355       dbus_g_proxy_manager_register (priv->manager, proxy);
01356     }
01357 
01358   return G_OBJECT (proxy);
01359 }
01360 
01361 static void
01362 dbus_g_proxy_class_init (DBusGProxyClass *klass)
01363 {
01364   GObjectClass *object_class = G_OBJECT_CLASS (klass);
01365   
01366   parent_class = g_type_class_peek_parent (klass);
01367 
01368   g_type_class_add_private (klass, sizeof (DBusGProxyPrivate));
01369 
01370   object_class->set_property = dbus_g_proxy_set_property;
01371   object_class->get_property = dbus_g_proxy_get_property;
01372 
01373   g_object_class_install_property (object_class,
01374                                    PROP_NAME,
01375                                    g_param_spec_string ("name",
01376                                                         "name",
01377                                                         "name",
01378                                                         NULL,
01379                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
01380 
01381   g_object_class_install_property (object_class,
01382                                    PROP_PATH,
01383                                    g_param_spec_string ("path",
01384                                                         "path",
01385                                                         "path",
01386                                                         NULL,
01387                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
01388 
01389   g_object_class_install_property (object_class,
01390                                    PROP_INTERFACE,
01391                                    g_param_spec_string ("interface",
01392                                                         "interface",
01393                                                         "interface",
01394                                                         NULL,
01395                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
01396   
01397   g_object_class_install_property (object_class,
01398                                    PROP_CONNECTION,
01399                                    g_param_spec_boxed ("connection",
01400                                                         "connection",
01401                                                         "connection",
01402                                                         DBUS_TYPE_G_CONNECTION,
01403                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
01404   
01405   object_class->finalize = dbus_g_proxy_finalize;
01406   object_class->dispose = dbus_g_proxy_dispose;
01407   object_class->constructor = dbus_g_proxy_constructor;
01408   
01409   signals[DESTROY] =
01410     g_signal_new ("destroy",
01411                   G_OBJECT_CLASS_TYPE (object_class),
01412                   G_SIGNAL_RUN_CLEANUP | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS,
01413                   0,
01414                   NULL, NULL,
01415                   g_cclosure_marshal_VOID__VOID,
01416                   G_TYPE_NONE, 0);
01417 
01418   signals[RECEIVED] =
01419     g_signal_new ("received",
01420                   G_OBJECT_CLASS_TYPE (object_class),
01421                   G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
01422                   0,
01423                   NULL, NULL,
01424                   marshal_dbus_message_to_g_marshaller,
01425                   G_TYPE_NONE, 2, DBUS_TYPE_MESSAGE, G_TYPE_POINTER);
01426 }
01427 
01428 static void
01429 cancel_pending_call (gpointer key, gpointer val, gpointer data)
01430 {
01431   DBusGProxyCall *call = key;
01432   DBusGProxy *proxy = data;
01433 
01434   dbus_g_proxy_cancel_call (proxy, call);
01435 }
01436 
01437 static void
01438 dbus_g_proxy_dispose (GObject *object)
01439 {
01440   DBusGProxy *proxy = DBUS_G_PROXY (object);
01441   DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
01442 
01443   if (priv->pending_calls == NULL) 
01444     {
01445       return;
01446     }
01447 
01448   /* Cancel outgoing pending calls */
01449   g_hash_table_foreach (priv->pending_calls, cancel_pending_call, proxy);
01450   g_hash_table_destroy (priv->pending_calls);
01451   priv->pending_calls = NULL;
01452 
01453   if (priv->manager && proxy != priv->manager->bus_proxy)
01454     {
01455       dbus_g_proxy_manager_unregister (priv->manager, proxy);
01456       dbus_g_proxy_manager_unref (priv->manager);
01457     }
01458   priv->manager = NULL;
01459   
01460   g_datalist_clear (&priv->signal_signatures);
01461   
01462   g_signal_emit (object, signals[DESTROY], 0);
01463   
01464   G_OBJECT_CLASS (parent_class)->dispose (object);
01465 }
01466 
01467 static void
01468 dbus_g_proxy_finalize (GObject *object)
01469 {
01470   DBusGProxy *proxy = DBUS_G_PROXY (object);
01471   DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
01472   
01473   g_return_if_fail (DBUS_G_PROXY_DESTROYED (proxy));
01474   
01475   g_free (priv->name);
01476   g_free (priv->path);
01477   g_free (priv->interface);
01478   
01479   G_OBJECT_CLASS (parent_class)->finalize (object);
01480 }
01481 
01482 static void
01483 dbus_g_proxy_destroy (DBusGProxy *proxy)
01484 {
01485   /* FIXME do we need the GTK_IN_DESTRUCTION style flag
01486    * from GtkObject?
01487    */
01488   g_object_run_dispose (G_OBJECT (proxy));
01489 }
01490 
01491 static void
01492 dbus_g_proxy_set_property (GObject *object,
01493                            guint prop_id,
01494                            const GValue *value,
01495                            GParamSpec *pspec)
01496 {
01497   DBusGProxy *proxy = DBUS_G_PROXY (object);
01498   DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
01499   DBusGConnection *connection;
01500 
01501   switch (prop_id)
01502     {
01503     case PROP_NAME:
01504       priv->name = g_strdup (g_value_get_string (value));
01505       if (priv->name)
01506         priv->for_owner = (priv->name[0] == ':');
01507       else
01508         priv->for_owner = TRUE;
01509       break;
01510     case PROP_PATH:
01511       priv->path = g_strdup (g_value_get_string (value));
01512       break;
01513     case PROP_INTERFACE:
01514       priv->interface = g_strdup (g_value_get_string (value));
01515       break;
01516     case PROP_CONNECTION:
01517       connection = g_value_get_boxed (value);
01518       if (connection != NULL)
01519         {
01520           priv->manager = dbus_g_proxy_manager_get (DBUS_CONNECTION_FROM_G_CONNECTION (connection));
01521         }
01522       break;
01523     default:
01524       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
01525       break;
01526     }
01527 }
01528 
01529 static void 
01530 dbus_g_proxy_get_property (GObject *object,
01531                            guint prop_id,
01532                            GValue *value,
01533                            GParamSpec *pspec)
01534 {
01535   DBusGProxy *proxy = DBUS_G_PROXY (object);
01536   DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
01537 
01538   switch (prop_id)
01539     {
01540     case PROP_NAME:
01541       g_value_set_string (value, priv->name);
01542       break;
01543     case PROP_PATH:
01544       g_value_set_string (value, priv->path);
01545       break;
01546     case PROP_INTERFACE:
01547       g_value_set_string (value, priv->interface);
01548       break;
01549     case PROP_CONNECTION:
01550       g_value_set_boxed (value, DBUS_G_CONNECTION_FROM_CONNECTION(priv->manager->connection));
01551       break;
01552     default:
01553       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
01554       break;
01555     }
01556 }
01557 
01558 /* this is to avoid people using g_signal_connect() directly,
01559  * to avoid confusion with local signal names, and because
01560  * of the horribly broken current setup (signals are added
01561  * globally to all proxies)
01562  */
01563 static char*
01564 create_signal_name (const char *interface,
01565                     const char *signal)
01566 {
01567   GString *str;
01568   char *p;
01569 
01570   str = g_string_new (interface);
01571 
01572   g_string_append (str, "-");
01573   
01574   g_string_append (str, signal);
01575 
01576   /* GLib will silently barf on '.' in signal names */
01577   p = str->str;
01578   while (*p)
01579     {
01580       if (*p == '.')
01581         *p = '-';
01582       ++p;
01583     }
01584   
01585   return g_string_free (str, FALSE);
01586 }
01587 
01588 static void
01589 marshal_dbus_message_to_g_marshaller (GClosure     *closure,
01590                                       GValue       *return_value,
01591                                       guint         n_param_values,
01592                                       const GValue *param_values,
01593                                       gpointer      invocation_hint,
01594                                       gpointer      marshal_data)
01595 {
01596   /* Incoming here we have three params, the instance (Proxy), the
01597    * DBusMessage, the signature. We want to convert that to an
01598    * expanded GValue array, then call an appropriate normal GLib
01599    * marshaller.
01600    */
01601 #define MAX_SIGNATURE_ARGS 20
01602   GValueArray *value_array;
01603   GSignalCMarshaller c_marshaller;
01604   DBusGProxy *proxy;
01605   DBusMessage *message;
01606   GArray *gsignature;
01607   const GType *types;
01608   DBusGProxyPrivate *priv;
01609 
01610   g_assert (n_param_values == 3);
01611 
01612   proxy = g_value_get_object (&param_values[0]);
01613   message = g_value_get_boxed (&param_values[1]);
01614   gsignature = g_value_get_pointer (&param_values[2]);
01615 
01616   g_return_if_fail (DBUS_IS_G_PROXY (proxy));
01617   g_return_if_fail (message != NULL);
01618   g_return_if_fail (gsignature != NULL);
01619 
01620   priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
01621 
01622   c_marshaller = _dbus_gobject_lookup_marshaller (G_TYPE_NONE, gsignature->len,
01623                                                   (GType*) gsignature->data);
01624 
01625   g_return_if_fail (c_marshaller != NULL);
01626   
01627   {
01628     DBusGValueMarshalCtx context;
01629     context.gconnection = DBUS_G_CONNECTION_FROM_CONNECTION (priv->manager->connection);
01630     context.proxy = proxy;
01631 
01632     types = (const GType*) gsignature->data;
01633     value_array = _dbus_gvalue_demarshal_message (&context, message,
01634                                                  gsignature->len, types, NULL);
01635   }
01636 
01637   if (value_array == NULL)
01638     return;
01639   
01640   g_value_array_prepend (value_array, NULL);
01641   g_value_init (g_value_array_get_nth (value_array, 0), G_TYPE_FROM_INSTANCE (proxy));
01642   g_value_set_instance (g_value_array_get_nth (value_array, 0), proxy);
01643 
01644   (* c_marshaller) (closure, return_value, value_array->n_values,
01645                     value_array->values, invocation_hint, marshal_data);
01646   
01647   g_value_array_free (value_array);
01648 }
01649 
01650 static void
01651 dbus_g_proxy_emit_remote_signal (DBusGProxy  *proxy,
01652                                  DBusMessage *message)
01653 {
01654   const char *interface;
01655   const char *signal;
01656   char *name;
01657   GQuark q;
01658   DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
01659   GArray *msg_gsignature = NULL;
01660 
01661   g_return_if_fail (!DBUS_G_PROXY_DESTROYED (proxy));
01662 
01663   interface = dbus_message_get_interface (message);
01664   signal = dbus_message_get_member (message);
01665 
01666   g_assert (interface != NULL);
01667   g_assert (signal != NULL);
01668 
01669   name = create_signal_name (interface, signal);
01670 
01671   /* If the quark isn't preexisting, there's no way there
01672    * are any handlers connected. We don't want to create
01673    * extra quarks for every possible signal.
01674    */
01675   q = g_quark_try_string (name);
01676 
01677   if (q != 0)
01678     {
01679       GArray *gsignature;
01680       guint i;
01681       
01682       gsignature = g_datalist_id_get_data (&priv->signal_signatures, q);
01683       if (gsignature == NULL)
01684         goto out;
01685       
01686       msg_gsignature = _dbus_gtypes_from_arg_signature (dbus_message_get_signature (message),
01687                                                        TRUE);
01688       for (i = 0; i < gsignature->len; i++)
01689         {
01690           if (msg_gsignature->len == i
01691               || g_array_index (gsignature, GType, i) != g_array_index (msg_gsignature, GType, i))
01692             goto mismatch;
01693         }
01694       if (msg_gsignature->len != i)
01695         goto mismatch;
01696       
01697       g_signal_emit (proxy,
01698                      signals[RECEIVED],
01699                      q,
01700                      message,
01701                      msg_gsignature);
01702     }
01703 
01704  out:
01705   g_free (name);
01706   if (msg_gsignature)
01707     g_array_free (msg_gsignature, TRUE);
01708   return;
01709  mismatch:
01710 #if 0
01711   /* Don't spew on remote errors */
01712   g_warning ("Unexpected message signature '%s' for signal '%s'\n",
01713              dbus_message_get_signature (message),
01714              name);
01715 #endif
01716   goto out;
01717 }
01718 
01719 typedef struct
01720 {
01721   DBusGProxy *proxy;
01722   guint call_id;
01723   DBusGProxyCallNotify func;
01724   void *data;
01725   GDestroyNotify free_data_func;
01726 } GPendingNotifyClosure;
01727 
01728 static void
01729 d_pending_call_notify (DBusPendingCall *dcall,
01730                        void            *data)
01731 {
01732   GPendingNotifyClosure *closure = data;
01733 
01734   (* closure->func) (closure->proxy, DBUS_G_PROXY_ID_TO_CALL (closure->call_id), closure->data);
01735 }
01736 
01737 static void
01738 d_pending_call_free (void *data)
01739 {
01740   GPendingNotifyClosure *closure = data;
01741   
01742   if (closure->free_data_func)
01743     (* closure->free_data_func) (closure->data);
01744 
01745   g_free (closure);
01746 }
01747   
01748 #define DBUS_G_VALUE_ARRAY_COLLECT_ALL(VALARRAY, FIRST_ARG_TYPE, ARGS) \
01749 do { \
01750   GType valtype; \
01751   int i = 0; \
01752   VALARRAY = g_value_array_new (6); \
01753   valtype = FIRST_ARG_TYPE; \
01754   while (valtype != G_TYPE_INVALID) \
01755     { \
01756       const char *collect_err; \
01757       GValue *val; \
01758       g_value_array_append (VALARRAY, NULL); \
01759       val = g_value_array_get_nth (VALARRAY, i); \
01760       g_value_init (val, valtype); \
01761       collect_err = NULL; \
01762       G_VALUE_COLLECT (val, ARGS, G_VALUE_NOCOPY_CONTENTS, &collect_err); \
01763       valtype = va_arg (ARGS, GType); \
01764       i++; \
01765     } \
01766 } while (0)
01767 
01768 DBusGProxyCall *
01769 manager_begin_bus_call (DBusGProxyManager    *manager,
01770                         const char           *method,
01771                         DBusGProxyCallNotify  notify,
01772                         gpointer              user_data,
01773                         GDestroyNotify        destroy,
01774                         GType                 first_arg_type,
01775                         ...)
01776 {
01777   DBusGProxyCall *call;
01778   DBusGProxyPrivate *priv;
01779   va_list args;
01780   GValueArray *arg_values;
01781   
01782   va_start (args, first_arg_type);
01783 
01784   if (!manager->bus_proxy)
01785     {
01786       manager->bus_proxy = g_object_new (DBUS_TYPE_G_PROXY,
01787                                          "name", DBUS_SERVICE_DBUS,
01788                                          "path", DBUS_PATH_DBUS,
01789                                          "interface", DBUS_INTERFACE_DBUS,
01790                                          NULL);
01791       priv = DBUS_G_PROXY_GET_PRIVATE(manager->bus_proxy);
01792       priv->manager = manager;
01793     }
01794 
01795   DBUS_G_VALUE_ARRAY_COLLECT_ALL (arg_values, first_arg_type, args);
01796   
01797   call = DBUS_G_PROXY_ID_TO_CALL (dbus_g_proxy_begin_call_internal (manager->bus_proxy, method, notify, user_data, destroy, arg_values,-1));
01798 
01799   g_value_array_free (arg_values);
01800 
01801   va_end (args);
01802 
01803   return call;
01804 }
01805 
01827 GType
01828 dbus_g_proxy_get_type (void)
01829 {
01830   static GType object_type = 0;
01831 
01832   if (!object_type)
01833     {
01834       static const GTypeInfo object_info =
01835         {
01836           sizeof (DBusGProxyClass),
01837           (GBaseInitFunc) NULL,
01838           (GBaseFinalizeFunc) NULL,
01839           (GClassInitFunc) dbus_g_proxy_class_init,
01840           NULL,           /* class_finalize */
01841           NULL,           /* class_data */
01842           sizeof (DBusGProxy),
01843           0,              /* n_preallocs */
01844           (GInstanceInitFunc) dbus_g_proxy_init,
01845         };
01846       
01847       object_type = g_type_register_static (G_TYPE_OBJECT,
01848                                             "DBusGProxy",
01849                                             &object_info, 0);
01850     }
01851   
01852   return object_type;
01853 }
01854 
01855 static DBusGProxy*
01856 dbus_g_proxy_new (DBusGConnection *connection,
01857                   const char      *name,
01858                   const char      *path_name,
01859                   const char      *interface_name)
01860 {
01861   DBusGProxy *proxy;
01862 
01863   g_assert (connection != NULL);
01864   
01865   proxy = g_object_new (DBUS_TYPE_G_PROXY, 
01866                         "name", name, 
01867                         "path", path_name, 
01868                         "interface", interface_name, 
01869                         "connection", connection, NULL);
01870 
01871   return proxy;
01872 }
01873 
01902 DBusGProxy*
01903 dbus_g_proxy_new_for_name (DBusGConnection *connection,
01904                            const char      *name,
01905                            const char      *path_name,
01906                            const char      *interface_name)
01907 {
01908   g_return_val_if_fail (connection != NULL, NULL);
01909   g_return_val_if_fail (name != NULL, NULL);
01910   g_return_val_if_fail (path_name != NULL, NULL);
01911   g_return_val_if_fail (interface_name != NULL, NULL);
01912 
01913   return dbus_g_proxy_new (connection, name,
01914                            path_name, interface_name);
01915 }
01916 
01942 DBusGProxy*
01943 dbus_g_proxy_new_for_name_owner (DBusGConnection          *connection,
01944                                  const char               *name,
01945                                  const char               *path_name,
01946                                  const char               *interface_name,
01947                                  GError                  **error)
01948 {
01949   DBusGProxy *proxy;
01950   char *unique_name;
01951 
01952   g_return_val_if_fail (connection != NULL, NULL);
01953   g_return_val_if_fail (name != NULL, NULL);
01954   g_return_val_if_fail (path_name != NULL, NULL);
01955   g_return_val_if_fail (interface_name != NULL, NULL);
01956 
01957   if (!(unique_name = get_name_owner (DBUS_CONNECTION_FROM_G_CONNECTION (connection), name, error)))
01958     return NULL;
01959 
01960   proxy = dbus_g_proxy_new (connection, unique_name,
01961                             path_name, interface_name);
01962   g_free (unique_name);
01963   return proxy;
01964 }
01965 
01977 DBusGProxy*
01978 dbus_g_proxy_new_from_proxy (DBusGProxy        *proxy,
01979                              const char        *interface,
01980                              const char        *path)
01981 {
01982   DBusGProxyPrivate *priv;
01983 
01984   g_return_val_if_fail (proxy != NULL, NULL);
01985 
01986   priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
01987   
01988   if (interface == NULL)
01989     interface = priv->interface;
01990   if (path == NULL)
01991     path = priv->path;
01992 
01993   return dbus_g_proxy_new (DBUS_G_CONNECTION_FROM_CONNECTION (priv->manager->connection),
01994                            priv->name,
01995                            path, interface);
01996 }
01997 
02012 DBusGProxy*
02013 dbus_g_proxy_new_for_peer (DBusGConnection          *connection,
02014                            const char               *path_name,
02015                            const char               *interface_name)
02016 {
02017   DBusGProxy *proxy;
02018   
02019   g_return_val_if_fail (connection != NULL, NULL);
02020   g_return_val_if_fail (path_name != NULL, NULL);
02021   g_return_val_if_fail (interface_name != NULL, NULL);
02022 
02023   proxy = dbus_g_proxy_new (connection, NULL,
02024                             path_name, interface_name);
02025 
02026   return proxy;
02027 }
02028 
02042 const char*
02043 dbus_g_proxy_get_bus_name (DBusGProxy        *proxy)
02044 {
02045   DBusGProxyPrivate *priv;
02046 
02047   g_return_val_if_fail (DBUS_IS_G_PROXY (proxy), NULL);
02048   g_return_val_if_fail (!DBUS_G_PROXY_DESTROYED (proxy), NULL);
02049 
02050   priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
02051 
02052   return priv->name;
02053 }
02054 
02063 const char*
02064 dbus_g_proxy_get_interface (DBusGProxy        *proxy)
02065 {
02066   DBusGProxyPrivate *priv;
02067   
02068   g_return_val_if_fail (DBUS_IS_G_PROXY (proxy), NULL);
02069   g_return_val_if_fail (!DBUS_G_PROXY_DESTROYED (proxy), NULL);
02070 
02071   priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
02072 
02073   return priv->interface;
02074 }
02075 
02083 void
02084 dbus_g_proxy_set_interface (DBusGProxy        *proxy,
02085                             const char        *interface_name)
02086 {
02087   DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
02088   /* FIXME - need to unregister when we switch interface for now
02089    * later should support idea of unset interface
02090    */
02091   dbus_g_proxy_manager_unregister (priv->manager, proxy);
02092   g_free (priv->interface);
02093   priv->interface = g_strdup (interface_name);
02094   dbus_g_proxy_manager_register (priv->manager, proxy);
02095 }
02096 
02104 const char*
02105 dbus_g_proxy_get_path (DBusGProxy        *proxy)
02106 {
02107   DBusGProxyPrivate *priv;
02108   
02109   g_return_val_if_fail (DBUS_IS_G_PROXY (proxy), NULL);
02110   g_return_val_if_fail (!DBUS_G_PROXY_DESTROYED (proxy), NULL);
02111 
02112   priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
02113 
02114   return priv->path;
02115 }
02116 
02117 static DBusMessage *
02118 dbus_g_proxy_marshal_args_to_message (DBusGProxy  *proxy,
02119                                       const char  *method,
02120                                       GValueArray *args)
02121 {
02122   DBusMessage *message;
02123   DBusMessageIter msgiter;
02124   guint i;
02125   DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
02126 
02127   message = dbus_message_new_method_call (priv->name,
02128                                           priv->path,
02129                                           priv->interface,
02130                                           method);
02131   if (message == NULL)
02132     goto oom;
02133 
02134   dbus_message_iter_init_append (message, &msgiter);
02135   for (i = 0; i < args->n_values; i++)
02136     {
02137       GValue *gvalue;
02138 
02139       gvalue = g_value_array_get_nth (args, i);
02140 
02141       if (!_dbus_gvalue_marshal (&msgiter, gvalue))
02142         g_assert_not_reached ();
02143     }
02144   return message;
02145  oom:
02146   return NULL;
02147 }
02148 
02149 static guint
02150 dbus_g_proxy_begin_call_internal (DBusGProxy          *proxy,
02151                                   const char          *method,
02152                                   DBusGProxyCallNotify notify,
02153                                   gpointer             user_data,
02154                                   GDestroyNotify       destroy,
02155                                   GValueArray         *args,
02156                                   int timeout)
02157 {
02158   DBusMessage *message;
02159   DBusPendingCall *pending;
02160   GPendingNotifyClosure *closure;
02161   guint call_id;
02162   DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
02163 
02164   pending = NULL;
02165 
02166   message = dbus_g_proxy_marshal_args_to_message (proxy, method, args);
02167   if (!message)
02168     goto oom;
02169 
02170   if (!dbus_connection_send_with_reply (priv->manager->connection,
02171                                         message,
02172                                         &pending,
02173                                         timeout))
02174     goto oom;
02175   dbus_message_unref (message);
02176   g_assert (pending != NULL);
02177 
02178   call_id = ++priv->call_id_counter;
02179 
02180   if (notify != NULL)
02181     {
02182       closure = g_new (GPendingNotifyClosure, 1);
02183       closure->proxy = proxy; /* No need to ref as the lifecycle is tied to proxy */
02184       closure->call_id = call_id;
02185       closure->func = notify;
02186       closure->data = user_data;
02187       closure->free_data_func = destroy;
02188       dbus_pending_call_set_notify (pending, d_pending_call_notify,
02189                                     closure,
02190                                     d_pending_call_free);
02191     }
02192 
02193   g_hash_table_insert (priv->pending_calls, GUINT_TO_POINTER (call_id), pending);
02194 
02195   return call_id;
02196  oom:
02197   g_error ("Out of memory");
02198   return 0;
02199 }
02200 
02201 static gboolean
02202 dbus_g_proxy_end_call_internal (DBusGProxy        *proxy,
02203                                 guint              call_id,
02204                                 GError           **error,
02205                                 GType              first_arg_type,
02206                                 va_list            args)
02207 {
02208   DBusMessage *reply;
02209   DBusMessageIter msgiter;
02210   DBusError derror;
02211   va_list args_unwind;
02212   guint over;
02213   int n_retvals_processed;
02214   gboolean ret;
02215   GType valtype;
02216   DBusPendingCall *pending;
02217   DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
02218 
02219   reply = NULL;
02220   ret = FALSE;
02221   n_retvals_processed = 0;
02222   over = 0;
02223 
02224   pending = g_hash_table_lookup (priv->pending_calls, GUINT_TO_POINTER (call_id));
02225   
02226   dbus_pending_call_block (pending);
02227   reply = dbus_pending_call_steal_reply (pending);
02228 
02229   g_assert (reply != NULL);
02230 
02231   dbus_error_init (&derror);
02232 
02233   switch (dbus_message_get_type (reply))
02234     {
02235     case DBUS_MESSAGE_TYPE_METHOD_RETURN:
02236       dbus_message_iter_init (reply, &msgiter);
02237       valtype = first_arg_type;
02238       while (valtype != G_TYPE_INVALID)
02239         {
02240           int arg_type;
02241           gpointer return_storage;
02242           GValue gvalue = { 0, };
02243           DBusGValueMarshalCtx context;
02244 
02245           context.gconnection = DBUS_G_CONNECTION_FROM_CONNECTION (priv->manager->connection);
02246           context.proxy = proxy;
02247 
02248           arg_type = dbus_message_iter_get_arg_type (&msgiter);
02249           if (arg_type == DBUS_TYPE_INVALID)
02250             {
02251               g_set_error (error, DBUS_GERROR,
02252                            DBUS_GERROR_INVALID_ARGS,
02253                            _("Too few arguments in reply"));
02254               goto out;
02255             }
02256 
02257           return_storage = va_arg (args, gpointer);
02258           if (return_storage == NULL)
02259             goto next;
02260 
02261           /* We handle variants specially; the caller is expected
02262            * to have already allocated storage for them.
02263            */
02264           if (arg_type == DBUS_TYPE_VARIANT
02265               && g_type_is_a (valtype, G_TYPE_VALUE))
02266             {
02267               if (!_dbus_gvalue_demarshal_variant (&context, &msgiter, (GValue*) return_storage, NULL))
02268                 {
02269                   g_set_error (error,
02270                                DBUS_GERROR,
02271                                DBUS_GERROR_INVALID_ARGS,
02272                                _("Couldn't convert argument, expected \"%s\""),
02273                                g_type_name (valtype));
02274                   goto out;
02275                 }
02276             }
02277           else
02278             {
02279               g_value_init (&gvalue, valtype);
02280 
02281               if (!_dbus_gvalue_demarshal (&context, &msgiter, &gvalue, error))
02282                 goto out;
02283 
02284               /* Anything that can be demarshaled must be storable */
02285               if (!_dbus_gvalue_store (&gvalue, (gpointer*) return_storage))
02286                 g_assert_not_reached ();
02287               /* Ownership of the value passes to the client, don't unset */
02288             }
02289           
02290         next:
02291           n_retvals_processed++;
02292           dbus_message_iter_next (&msgiter);
02293           valtype = va_arg (args, GType);
02294         }
02295       
02296       while (dbus_message_iter_get_arg_type (&msgiter) != DBUS_TYPE_INVALID)
02297         {
02298           over++;
02299           dbus_message_iter_next (&msgiter);
02300         }
02301 
02302       if (over > 0)
02303         {
02304           g_set_error (error, DBUS_GERROR,
02305                        DBUS_GERROR_INVALID_ARGS,
02306                        _("Too many arguments in reply; expected %d, got %d"),
02307                        n_retvals_processed, over);
02308           goto out;
02309         }
02310       break;
02311     case DBUS_MESSAGE_TYPE_ERROR:
02312       dbus_set_error_from_message (&derror, reply);
02313       dbus_set_g_error (error, &derror);
02314       dbus_error_free (&derror);
02315       goto out;
02316       break;
02317     default:
02318       dbus_set_error (&derror, DBUS_ERROR_FAILED,
02319                       "Reply was neither a method return nor an exception");
02320       dbus_set_g_error (error, &derror);
02321       dbus_error_free (&derror);
02322       goto out;
02323       break;
02324     }
02325 
02326   ret = TRUE;
02327  out:
02328   va_end (args);
02329 
02330   if (ret == FALSE)
02331     {
02332       int i;
02333       for (i = 0; i < n_retvals_processed; i++)
02334         {
02335           gpointer retval;
02336 
02337           retval = va_arg (args_unwind, gpointer);
02338 
02339           g_free (retval);
02340         }
02341     }
02342   va_end (args_unwind);
02343 
02344   g_hash_table_remove (priv->pending_calls, GUINT_TO_POINTER (call_id));
02345 
02346   if (reply)
02347     dbus_message_unref (reply);
02348   return ret;
02349 }
02350 
02373 DBusGProxyCall *
02374 dbus_g_proxy_begin_call (DBusGProxy          *proxy,
02375                          const char          *method,
02376                          DBusGProxyCallNotify notify,
02377                          gpointer             user_data,
02378                          GDestroyNotify       destroy,
02379                          GType                first_arg_type,
02380                          ...)
02381 {
02382   guint call_id;
02383   va_list args;
02384   GValueArray *arg_values;
02385   
02386   g_return_val_if_fail (DBUS_IS_G_PROXY (proxy), NULL);
02387   g_return_val_if_fail (!DBUS_G_PROXY_DESTROYED (proxy), NULL);
02388 
02389   va_start (args, first_arg_type);
02390 
02391   DBUS_G_VALUE_ARRAY_COLLECT_ALL (arg_values, first_arg_type, args);
02392   
02393   call_id = dbus_g_proxy_begin_call_internal (proxy, method, notify, user_data, destroy, arg_values,-1);
02394 
02395   g_value_array_free (arg_values);
02396 
02397   va_end (args);
02398 
02399   return DBUS_G_PROXY_ID_TO_CALL (call_id);
02400 }
02401 
02425 DBusGProxyCall *
02426 dbus_g_proxy_begin_call_with_timeout (DBusGProxy          *proxy,
02427                          const char          *method,
02428                          DBusGProxyCallNotify notify,
02429                          gpointer             user_data,
02430                          GDestroyNotify       destroy,
02431                          int timeout,
02432                          GType                first_arg_type,
02433                          ...)
02434 {
02435   guint call_id;
02436   va_list args;
02437   GValueArray *arg_values;
02438 
02439   g_return_val_if_fail (DBUS_IS_G_PROXY (proxy), NULL);
02440   g_return_val_if_fail (!DBUS_G_PROXY_DESTROYED (proxy), NULL);
02441 
02442   va_start (args, first_arg_type);
02443 
02444   DBUS_G_VALUE_ARRAY_COLLECT_ALL (arg_values, first_arg_type, args);
02445 
02446   call_id = dbus_g_proxy_begin_call_internal (proxy, method, notify, user_data, destroy, arg_values,timeout);
02447 
02448   g_value_array_free (arg_values);
02449 
02450   va_end (args);
02451 
02452   return DBUS_G_PROXY_ID_TO_CALL (call_id);
02453 }
02454 
02477 gboolean
02478 dbus_g_proxy_end_call (DBusGProxy          *proxy,
02479                        DBusGProxyCall      *call,
02480                        GError             **error,
02481                        GType                first_arg_type,
02482                        ...)
02483 {
02484   gboolean ret;
02485   va_list args;
02486 
02487   va_start (args, first_arg_type);
02488 
02489   ret = dbus_g_proxy_end_call_internal (proxy, GPOINTER_TO_UINT (call), error, first_arg_type, args);
02490 
02491   va_end (args);
02492   
02493   return ret;
02494 }
02495 
02512 gboolean
02513 dbus_g_proxy_call (DBusGProxy        *proxy,
02514                    const char        *method,
02515                    GError           **error,
02516                    GType              first_arg_type,
02517                    ...)
02518 {
02519   gboolean ret;
02520   guint call_id;
02521   va_list args;
02522   GValueArray *in_args;
02523 
02524   g_return_val_if_fail (DBUS_IS_G_PROXY (proxy), FALSE);
02525   g_return_val_if_fail (!DBUS_G_PROXY_DESTROYED (proxy), FALSE);
02526 
02527   va_start (args, first_arg_type);
02528 
02529   DBUS_G_VALUE_ARRAY_COLLECT_ALL (in_args, first_arg_type, args);
02530 
02531   call_id = dbus_g_proxy_begin_call_internal (proxy, method, NULL, NULL, NULL, in_args,-1);
02532 
02533   g_value_array_free (in_args);
02534 
02535   first_arg_type = va_arg (args, GType);
02536   ret = dbus_g_proxy_end_call_internal (proxy, call_id, error, first_arg_type, args);
02537 
02538   va_end (args);
02539 
02540   return ret;
02541 }
02542 
02560 gboolean
02561 dbus_g_proxy_call_with_timeout (DBusGProxy        *proxy,
02562                    const char        *method,
02563                    int timeout,
02564                    GError           **error,
02565                    GType              first_arg_type,
02566                    ...)
02567 {
02568   gboolean ret;
02569   guint call_id;
02570   va_list args;
02571   GValueArray *in_args;
02572 
02573   g_return_val_if_fail (DBUS_IS_G_PROXY (proxy), FALSE);
02574   g_return_val_if_fail (!DBUS_G_PROXY_DESTROYED (proxy), FALSE);
02575 
02576   va_start (args, first_arg_type);
02577 
02578   DBUS_G_VALUE_ARRAY_COLLECT_ALL (in_args, first_arg_type, args);
02579 
02580   call_id = dbus_g_proxy_begin_call_internal (proxy, method, NULL, NULL, NULL, in_args,timeout);
02581 
02582   g_value_array_free (in_args);
02583 
02584   first_arg_type = va_arg (args, GType);
02585   ret = dbus_g_proxy_end_call_internal (proxy, call_id, error, first_arg_type, args);
02586 
02587   va_end (args);
02588 
02589   return ret;
02590 }
02591 
02604 void
02605 dbus_g_proxy_call_no_reply (DBusGProxy               *proxy,
02606                             const char               *method,
02607                             GType                     first_arg_type,
02608                             ...)
02609 {
02610   DBusMessage *message;
02611   va_list args;
02612   GValueArray *in_args;
02613   DBusGProxyPrivate *priv;
02614   
02615   g_return_if_fail (DBUS_IS_G_PROXY (proxy));
02616   g_return_if_fail (!DBUS_G_PROXY_DESTROYED (proxy));
02617 
02618   priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
02619 
02620   va_start (args, first_arg_type);
02621   DBUS_G_VALUE_ARRAY_COLLECT_ALL (in_args, first_arg_type, args);
02622 
02623   message = dbus_g_proxy_marshal_args_to_message (proxy, method, in_args);
02624 
02625   g_value_array_free (in_args);
02626   va_end (args);
02627 
02628   if (!message)
02629     goto oom;
02630 
02631   dbus_message_set_no_reply (message, TRUE);
02632 
02633   if (!dbus_connection_send (priv->manager->connection,
02634                              message,
02635                              NULL))
02636     goto oom;
02637   dbus_message_unref (message);
02638   return;
02639   
02640  oom:
02641   g_error ("Out of memory");
02642 }
02643 
02654 void
02655 dbus_g_proxy_cancel_call (DBusGProxy        *proxy,
02656                           DBusGProxyCall    *call)
02657 {
02658   guint call_id;
02659   DBusPendingCall *pending;
02660   DBusGProxyPrivate *priv;
02661   
02662   g_return_if_fail (DBUS_IS_G_PROXY (proxy));
02663   g_return_if_fail (!DBUS_G_PROXY_DESTROYED (proxy));
02664 
02665   priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
02666 
02667   call_id = DBUS_G_PROXY_CALL_TO_ID (call);
02668 
02669   pending = g_hash_table_lookup (priv->pending_calls, GUINT_TO_POINTER (call_id));
02670   g_return_if_fail (pending != NULL);
02671 
02672   dbus_pending_call_cancel (pending);
02673 
02674   g_hash_table_remove (priv->pending_calls, GUINT_TO_POINTER (call_id));
02675 }
02676 
02697 void
02698 dbus_g_proxy_send (DBusGProxy          *proxy,
02699                    DBusMessage         *message,
02700                    dbus_uint32_t       *client_serial)
02701 {
02702   DBusGProxyPrivate *priv;
02703   
02704   g_return_if_fail (DBUS_IS_G_PROXY (proxy));
02705   g_return_if_fail (!DBUS_G_PROXY_DESTROYED (proxy));
02706   
02707   priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
02708   
02709   if (priv->name)
02710     {
02711       if (!dbus_message_set_destination (message, priv->name))
02712         g_error ("Out of memory");
02713     }
02714   if (priv->path)
02715     {
02716       if (!dbus_message_set_path (message, priv->path))
02717         g_error ("Out of memory");
02718     }
02719   if (priv->interface)
02720     {
02721       if (!dbus_message_set_interface (message, priv->interface))
02722         g_error ("Out of memory");
02723     }
02724   
02725   if (!dbus_connection_send (priv->manager->connection, message, client_serial))
02726     g_error ("Out of memory\n");
02727 }
02728 
02729 static void
02730 array_free_all (gpointer array)
02731 {
02732   g_array_free (array, TRUE);
02733 }
02734 
02745 void
02746 dbus_g_proxy_add_signal  (DBusGProxy        *proxy,
02747                           const char        *signal_name,
02748                           GType              first_type,
02749                           ...)
02750 {
02751   GQuark q;
02752   char *name;
02753   GArray *gtypesig;
02754   GType gtype;
02755   va_list args;
02756   DBusGProxyPrivate *priv;
02757 
02758   g_return_if_fail (DBUS_IS_G_PROXY (proxy));
02759   g_return_if_fail (!DBUS_G_PROXY_DESTROYED (proxy));
02760   g_return_if_fail (signal_name != NULL);
02761   
02762   priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
02763 
02764   name = create_signal_name (priv->interface, signal_name);
02765   
02766   q = g_quark_from_string (name);
02767   
02768   g_return_if_fail (g_datalist_id_get_data (&priv->signal_signatures, q) == NULL);
02769 
02770   gtypesig = g_array_new (FALSE, TRUE, sizeof (GType));
02771 
02772   va_start (args, first_type);
02773   gtype = first_type;
02774   while (gtype != G_TYPE_INVALID)
02775     {
02776       g_array_append_val (gtypesig, gtype);
02777       gtype = va_arg (args, GType);
02778     }
02779   va_end (args);
02780 
02781 #ifndef G_DISABLE_CHECKS
02782   if (_dbus_gobject_lookup_marshaller (G_TYPE_NONE, gtypesig->len, (const GType*) gtypesig->data) == NULL)
02783     g_warning ("No marshaller for signature of signal '%s'", signal_name);
02784 #endif
02785 
02786   
02787   g_datalist_id_set_data_full (&priv->signal_signatures,
02788                                q, gtypesig,
02789                                array_free_all);
02790 
02791   g_free (name);
02792 }
02793 
02806 void
02807 dbus_g_proxy_connect_signal (DBusGProxy             *proxy,
02808                              const char             *signal_name,
02809                              GCallback               handler,
02810                              void                   *data,
02811                              GClosureNotify          free_data_func)
02812 {
02813   char *name;
02814   GClosure *closure;
02815   GQuark q;
02816   DBusGProxyPrivate *priv;
02817 
02818   g_return_if_fail (DBUS_IS_G_PROXY (proxy));
02819   g_return_if_fail (!DBUS_G_PROXY_DESTROYED (proxy));
02820   g_return_if_fail (signal_name != NULL);
02821   g_return_if_fail (handler != NULL);
02822   
02823   priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
02824   name = create_signal_name (priv->interface, signal_name);
02825 
02826   q = g_quark_try_string (name);
02827 
02828 #ifndef G_DISABLE_CHECKS
02829   if (q == 0 || g_datalist_id_get_data (&priv->signal_signatures, q) == NULL)
02830     {
02831       g_warning ("Must add the signal '%s' with dbus_g_proxy_add_signal() prior to connecting to it\n", name);
02832       g_free (name);
02833       return;
02834     }
02835 #endif
02836   
02837   closure = g_cclosure_new (G_CALLBACK (handler), data, free_data_func);
02838   
02839   g_signal_connect_closure_by_id (G_OBJECT (proxy),
02840                                   signals[RECEIVED],
02841                                   q,
02842                                   closure, FALSE);
02843   
02844   g_free (name);
02845 }
02846 
02857 void
02858 dbus_g_proxy_disconnect_signal (DBusGProxy             *proxy,
02859                                 const char             *signal_name,
02860                                 GCallback               handler,
02861                                 void                   *data)
02862 {
02863   char *name;
02864   GQuark q;
02865   DBusGProxyPrivate *priv;
02866   
02867   g_return_if_fail (DBUS_IS_G_PROXY (proxy));
02868   g_return_if_fail (!DBUS_G_PROXY_DESTROYED (proxy));
02869   g_return_if_fail (signal_name != NULL);
02870   g_return_if_fail (handler != NULL);
02871 
02872   priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
02873   name = create_signal_name (priv->interface, signal_name);
02874 
02875   q = g_quark_try_string (name);
02876   
02877   if (q != 0)
02878     {
02879       g_signal_handlers_disconnect_matched (G_OBJECT (proxy),
02880                                             G_SIGNAL_MATCH_DETAIL |
02881                                             G_SIGNAL_MATCH_FUNC   |
02882                                             G_SIGNAL_MATCH_DATA,
02883                                             signals[RECEIVED],
02884                                             q,
02885                                             NULL,
02886                                             G_CALLBACK (handler), data);
02887     }
02888   else
02889     {
02890       g_warning ("Attempt to disconnect from signal '%s' which is not registered\n",
02891                  name);
02892     }
02893 
02894   g_free (name);
02895 }
02896 
02899 #ifdef DBUS_BUILD_TESTS
02900 
02906 gboolean
02907 _dbus_g_proxy_test (void)
02908 {
02909   
02910   
02911   return TRUE;
02912 }
02913 
02914 #endif /* DBUS_BUILD_TESTS */

Generated on Mon Jul 23 15:27:25 2007 for D-BUSGLibBindings by  doxygen 1.4.6