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   DBusPendingCall *pending = val;
01432 
01433   dbus_pending_call_cancel (pending);
01434 }
01435 
01436 static void
01437 dbus_g_proxy_dispose (GObject *object)
01438 {
01439   DBusGProxy *proxy = DBUS_G_PROXY (object);
01440   DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
01441 
01442   if (priv->pending_calls == NULL) 
01443     {
01444       return;
01445     }
01446 
01447   /* Cancel outgoing pending calls */
01448   g_hash_table_foreach_remove (priv->pending_calls, cancel_pending_call, NULL);
01449   g_hash_table_destroy (priv->pending_calls);
01450   priv->pending_calls = NULL;
01451 
01452   if (priv->manager && proxy != priv->manager->bus_proxy)
01453     {
01454       dbus_g_proxy_manager_unregister (priv->manager, proxy);
01455       dbus_g_proxy_manager_unref (priv->manager);
01456     }
01457   priv->manager = NULL;
01458   
01459   g_datalist_clear (&priv->signal_signatures);
01460   
01461   g_signal_emit (object, signals[DESTROY], 0);
01462   
01463   G_OBJECT_CLASS (parent_class)->dispose (object);
01464 }
01465 
01466 static void
01467 dbus_g_proxy_finalize (GObject *object)
01468 {
01469   DBusGProxy *proxy = DBUS_G_PROXY (object);
01470   DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
01471   
01472   g_return_if_fail (DBUS_G_PROXY_DESTROYED (proxy));
01473   
01474   g_free (priv->name);
01475   g_free (priv->path);
01476   g_free (priv->interface);
01477   
01478   G_OBJECT_CLASS (parent_class)->finalize (object);
01479 }
01480 
01481 static void
01482 dbus_g_proxy_destroy (DBusGProxy *proxy)
01483 {
01484   /* FIXME do we need the GTK_IN_DESTRUCTION style flag
01485    * from GtkObject?
01486    */
01487   g_object_run_dispose (G_OBJECT (proxy));
01488 }
01489 
01490 static void
01491 dbus_g_proxy_set_property (GObject *object,
01492                            guint prop_id,
01493                            const GValue *value,
01494                            GParamSpec *pspec)
01495 {
01496   DBusGProxy *proxy = DBUS_G_PROXY (object);
01497   DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
01498   DBusGConnection *connection;
01499 
01500   switch (prop_id)
01501     {
01502     case PROP_NAME:
01503       priv->name = g_strdup (g_value_get_string (value));
01504       if (priv->name)
01505         priv->for_owner = (priv->name[0] == ':');
01506       else
01507         priv->for_owner = TRUE;
01508       break;
01509     case PROP_PATH:
01510       priv->path = g_strdup (g_value_get_string (value));
01511       break;
01512     case PROP_INTERFACE:
01513       priv->interface = g_strdup (g_value_get_string (value));
01514       break;
01515     case PROP_CONNECTION:
01516       connection = g_value_get_boxed (value);
01517       if (connection != NULL)
01518         {
01519           priv->manager = dbus_g_proxy_manager_get (DBUS_CONNECTION_FROM_G_CONNECTION (connection));
01520         }
01521       break;
01522     default:
01523       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
01524       break;
01525     }
01526 }
01527 
01528 static void 
01529 dbus_g_proxy_get_property (GObject *object,
01530                            guint prop_id,
01531                            GValue *value,
01532                            GParamSpec *pspec)
01533 {
01534   DBusGProxy *proxy = DBUS_G_PROXY (object);
01535   DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
01536 
01537   switch (prop_id)
01538     {
01539     case PROP_NAME:
01540       g_value_set_string (value, priv->name);
01541       break;
01542     case PROP_PATH:
01543       g_value_set_string (value, priv->path);
01544       break;
01545     case PROP_INTERFACE:
01546       g_value_set_string (value, priv->interface);
01547       break;
01548     case PROP_CONNECTION:
01549       g_value_set_boxed (value, DBUS_G_CONNECTION_FROM_CONNECTION(priv->manager->connection));
01550       break;
01551     default:
01552       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
01553       break;
01554     }
01555 }
01556 
01557 /* this is to avoid people using g_signal_connect() directly,
01558  * to avoid confusion with local signal names, and because
01559  * of the horribly broken current setup (signals are added
01560  * globally to all proxies)
01561  */
01562 static char*
01563 create_signal_name (const char *interface,
01564                     const char *signal)
01565 {
01566   GString *str;
01567   char *p;
01568 
01569   str = g_string_new (interface);
01570 
01571   g_string_append (str, "-");
01572   
01573   g_string_append (str, signal);
01574 
01575   /* GLib will silently barf on '.' in signal names */
01576   p = str->str;
01577   while (*p)
01578     {
01579       if (*p == '.')
01580         *p = '-';
01581       ++p;
01582     }
01583   
01584   return g_string_free (str, FALSE);
01585 }
01586 
01587 static void
01588 marshal_dbus_message_to_g_marshaller (GClosure     *closure,
01589                                       GValue       *return_value,
01590                                       guint         n_param_values,
01591                                       const GValue *param_values,
01592                                       gpointer      invocation_hint,
01593                                       gpointer      marshal_data)
01594 {
01595   /* Incoming here we have three params, the instance (Proxy), the
01596    * DBusMessage, the signature. We want to convert that to an
01597    * expanded GValue array, then call an appropriate normal GLib
01598    * marshaller.
01599    */
01600 #define MAX_SIGNATURE_ARGS 20
01601   GValueArray *value_array;
01602   GSignalCMarshaller c_marshaller;
01603   DBusGProxy *proxy;
01604   DBusMessage *message;
01605   GArray *gsignature;
01606   const GType *types;
01607   DBusGProxyPrivate *priv;
01608 
01609   g_assert (n_param_values == 3);
01610 
01611   proxy = g_value_get_object (&param_values[0]);
01612   message = g_value_get_boxed (&param_values[1]);
01613   gsignature = g_value_get_pointer (&param_values[2]);
01614 
01615   g_return_if_fail (DBUS_IS_G_PROXY (proxy));
01616   g_return_if_fail (message != NULL);
01617   g_return_if_fail (gsignature != NULL);
01618 
01619   priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
01620 
01621   c_marshaller = _dbus_gobject_lookup_marshaller (G_TYPE_NONE, gsignature->len,
01622                                                   (GType*) gsignature->data);
01623 
01624   g_return_if_fail (c_marshaller != NULL);
01625   
01626   {
01627     DBusGValueMarshalCtx context;
01628     context.gconnection = DBUS_G_CONNECTION_FROM_CONNECTION (priv->manager->connection);
01629     context.proxy = proxy;
01630 
01631     types = (const GType*) gsignature->data;
01632     value_array = _dbus_gvalue_demarshal_message (&context, message,
01633                                                  gsignature->len, types, NULL);
01634   }
01635 
01636   if (value_array == NULL)
01637     return;
01638   
01639   g_value_array_prepend (value_array, NULL);
01640   g_value_init (g_value_array_get_nth (value_array, 0), G_TYPE_FROM_INSTANCE (proxy));
01641   g_value_set_instance (g_value_array_get_nth (value_array, 0), proxy);
01642 
01643   (* c_marshaller) (closure, return_value, value_array->n_values,
01644                     value_array->values, invocation_hint, marshal_data);
01645   
01646   g_value_array_free (value_array);
01647 }
01648 
01649 static void
01650 dbus_g_proxy_emit_remote_signal (DBusGProxy  *proxy,
01651                                  DBusMessage *message)
01652 {
01653   const char *interface;
01654   const char *signal;
01655   char *name;
01656   GQuark q;
01657   DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
01658   GArray *msg_gsignature = NULL;
01659 
01660   g_return_if_fail (!DBUS_G_PROXY_DESTROYED (proxy));
01661 
01662   interface = dbus_message_get_interface (message);
01663   signal = dbus_message_get_member (message);
01664 
01665   g_assert (interface != NULL);
01666   g_assert (signal != NULL);
01667 
01668   name = create_signal_name (interface, signal);
01669 
01670   /* If the quark isn't preexisting, there's no way there
01671    * are any handlers connected. We don't want to create
01672    * extra quarks for every possible signal.
01673    */
01674   q = g_quark_try_string (name);
01675 
01676   if (q != 0)
01677     {
01678       GArray *gsignature;
01679       guint i;
01680       
01681       gsignature = g_datalist_id_get_data (&priv->signal_signatures, q);
01682       if (gsignature == NULL)
01683         goto out;
01684       
01685       msg_gsignature = _dbus_gtypes_from_arg_signature (dbus_message_get_signature (message),
01686                                                        TRUE);
01687       for (i = 0; i < gsignature->len; i++)
01688         {
01689           if (msg_gsignature->len == i
01690               || g_array_index (gsignature, GType, i) != g_array_index (msg_gsignature, GType, i))
01691             goto mismatch;
01692         }
01693       if (msg_gsignature->len != i)
01694         goto mismatch;
01695       
01696       g_signal_emit (proxy,
01697                      signals[RECEIVED],
01698                      q,
01699                      message,
01700                      msg_gsignature);
01701     }
01702 
01703  out:
01704   g_free (name);
01705   if (msg_gsignature)
01706     g_array_free (msg_gsignature, TRUE);
01707   return;
01708  mismatch:
01709 #if 0
01710   /* Don't spew on remote errors */
01711   g_warning ("Unexpected message signature '%s' for signal '%s'\n",
01712              dbus_message_get_signature (message),
01713              name);
01714 #endif
01715   goto out;
01716 }
01717 
01718 typedef struct
01719 {
01720   DBusGProxy *proxy;
01721   guint call_id;
01722   DBusGProxyCallNotify func;
01723   void *data;
01724   GDestroyNotify free_data_func;
01725 } GPendingNotifyClosure;
01726 
01727 static void
01728 d_pending_call_notify (DBusPendingCall *dcall,
01729                        void            *data)
01730 {
01731   GPendingNotifyClosure *closure = data;
01732 
01733   (* closure->func) (closure->proxy, DBUS_G_PROXY_ID_TO_CALL (closure->call_id), closure->data);
01734 }
01735 
01736 static void
01737 d_pending_call_free (void *data)
01738 {
01739   GPendingNotifyClosure *closure = data;
01740   
01741   if (closure->free_data_func)
01742     (* closure->free_data_func) (closure->data);
01743 
01744   g_free (closure);
01745 }
01746   
01747 #define DBUS_G_VALUE_ARRAY_COLLECT_ALL(VALARRAY, FIRST_ARG_TYPE, ARGS) \
01748 do { \
01749   GType valtype; \
01750   int i = 0; \
01751   VALARRAY = g_value_array_new (6); \
01752   valtype = FIRST_ARG_TYPE; \
01753   while (valtype != G_TYPE_INVALID) \
01754     { \
01755       const char *collect_err; \
01756       GValue *val; \
01757       g_value_array_append (VALARRAY, NULL); \
01758       val = g_value_array_get_nth (VALARRAY, i); \
01759       g_value_init (val, valtype); \
01760       collect_err = NULL; \
01761       G_VALUE_COLLECT (val, ARGS, G_VALUE_NOCOPY_CONTENTS, &collect_err); \
01762       valtype = va_arg (ARGS, GType); \
01763       i++; \
01764     } \
01765 } while (0)
01766 
01767 DBusGProxyCall *
01768 manager_begin_bus_call (DBusGProxyManager    *manager,
01769                         const char           *method,
01770                         DBusGProxyCallNotify  notify,
01771                         gpointer              user_data,
01772                         GDestroyNotify        destroy,
01773                         GType                 first_arg_type,
01774                         ...)
01775 {
01776   DBusGProxyCall *call;
01777   DBusGProxyPrivate *priv;
01778   va_list args;
01779   GValueArray *arg_values;
01780   
01781   va_start (args, first_arg_type);
01782 
01783   if (!manager->bus_proxy)
01784     {
01785       manager->bus_proxy = g_object_new (DBUS_TYPE_G_PROXY,
01786                                          "name", DBUS_SERVICE_DBUS,
01787                                          "path", DBUS_PATH_DBUS,
01788                                          "interface", DBUS_INTERFACE_DBUS,
01789                                          NULL);
01790       priv = DBUS_G_PROXY_GET_PRIVATE(manager->bus_proxy);
01791       priv->manager = manager;
01792     }
01793 
01794   DBUS_G_VALUE_ARRAY_COLLECT_ALL (arg_values, first_arg_type, args);
01795   
01796   call = DBUS_G_PROXY_ID_TO_CALL (dbus_g_proxy_begin_call_internal (manager->bus_proxy, method, notify, user_data, destroy, arg_values,-1));
01797 
01798   g_value_array_free (arg_values);
01799 
01800   va_end (args);
01801 
01802   return call;
01803 }
01804 
01826 GType
01827 dbus_g_proxy_get_type (void)
01828 {
01829   static GType object_type = 0;
01830 
01831   if (!object_type)
01832     {
01833       static const GTypeInfo object_info =
01834         {
01835           sizeof (DBusGProxyClass),
01836           (GBaseInitFunc) NULL,
01837           (GBaseFinalizeFunc) NULL,
01838           (GClassInitFunc) dbus_g_proxy_class_init,
01839           NULL,           /* class_finalize */
01840           NULL,           /* class_data */
01841           sizeof (DBusGProxy),
01842           0,              /* n_preallocs */
01843           (GInstanceInitFunc) dbus_g_proxy_init,
01844         };
01845       
01846       object_type = g_type_register_static (G_TYPE_OBJECT,
01847                                             "DBusGProxy",
01848                                             &object_info, 0);
01849     }
01850   
01851   return object_type;
01852 }
01853 
01854 static DBusGProxy*
01855 dbus_g_proxy_new (DBusGConnection *connection,
01856                   const char      *name,
01857                   const char      *path_name,
01858                   const char      *interface_name)
01859 {
01860   DBusGProxy *proxy;
01861 
01862   g_assert (connection != NULL);
01863   
01864   proxy = g_object_new (DBUS_TYPE_G_PROXY, 
01865                         "name", name, 
01866                         "path", path_name, 
01867                         "interface", interface_name, 
01868                         "connection", connection, NULL);
01869 
01870   return proxy;
01871 }
01872 
01901 DBusGProxy*
01902 dbus_g_proxy_new_for_name (DBusGConnection *connection,
01903                            const char      *name,
01904                            const char      *path_name,
01905                            const char      *interface_name)
01906 {
01907   g_return_val_if_fail (connection != NULL, NULL);
01908   g_return_val_if_fail (name != NULL, NULL);
01909   g_return_val_if_fail (path_name != NULL, NULL);
01910   g_return_val_if_fail (interface_name != NULL, NULL);
01911 
01912   return dbus_g_proxy_new (connection, name,
01913                            path_name, interface_name);
01914 }
01915 
01941 DBusGProxy*
01942 dbus_g_proxy_new_for_name_owner (DBusGConnection          *connection,
01943                                  const char               *name,
01944                                  const char               *path_name,
01945                                  const char               *interface_name,
01946                                  GError                  **error)
01947 {
01948   DBusGProxy *proxy;
01949   char *unique_name;
01950 
01951   g_return_val_if_fail (connection != NULL, NULL);
01952   g_return_val_if_fail (name != NULL, NULL);
01953   g_return_val_if_fail (path_name != NULL, NULL);
01954   g_return_val_if_fail (interface_name != NULL, NULL);
01955 
01956   if (!(unique_name = get_name_owner (DBUS_CONNECTION_FROM_G_CONNECTION (connection), name, error)))
01957     return NULL;
01958 
01959   proxy = dbus_g_proxy_new (connection, unique_name,
01960                             path_name, interface_name);
01961   g_free (unique_name);
01962   return proxy;
01963 }
01964 
01976 DBusGProxy*
01977 dbus_g_proxy_new_from_proxy (DBusGProxy        *proxy,
01978                              const char        *interface,
01979                              const char        *path)
01980 {
01981   DBusGProxyPrivate *priv;
01982 
01983   g_return_val_if_fail (proxy != NULL, NULL);
01984 
01985   priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
01986   
01987   if (interface == NULL)
01988     interface = priv->interface;
01989   if (path == NULL)
01990     path = priv->path;
01991 
01992   return dbus_g_proxy_new (DBUS_G_CONNECTION_FROM_CONNECTION (priv->manager->connection),
01993                            priv->name,
01994                            path, interface);
01995 }
01996 
02011 DBusGProxy*
02012 dbus_g_proxy_new_for_peer (DBusGConnection          *connection,
02013                            const char               *path_name,
02014                            const char               *interface_name)
02015 {
02016   DBusGProxy *proxy;
02017   
02018   g_return_val_if_fail (connection != NULL, NULL);
02019   g_return_val_if_fail (path_name != NULL, NULL);
02020   g_return_val_if_fail (interface_name != NULL, NULL);
02021 
02022   proxy = dbus_g_proxy_new (connection, NULL,
02023                             path_name, interface_name);
02024 
02025   return proxy;
02026 }
02027 
02041 const char*
02042 dbus_g_proxy_get_bus_name (DBusGProxy        *proxy)
02043 {
02044   DBusGProxyPrivate *priv;
02045 
02046   g_return_val_if_fail (DBUS_IS_G_PROXY (proxy), NULL);
02047   g_return_val_if_fail (!DBUS_G_PROXY_DESTROYED (proxy), NULL);
02048 
02049   priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
02050 
02051   return priv->name;
02052 }
02053 
02062 const char*
02063 dbus_g_proxy_get_interface (DBusGProxy        *proxy)
02064 {
02065   DBusGProxyPrivate *priv;
02066   
02067   g_return_val_if_fail (DBUS_IS_G_PROXY (proxy), NULL);
02068   g_return_val_if_fail (!DBUS_G_PROXY_DESTROYED (proxy), NULL);
02069 
02070   priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
02071 
02072   return priv->interface;
02073 }
02074 
02082 void
02083 dbus_g_proxy_set_interface (DBusGProxy        *proxy,
02084                             const char        *interface_name)
02085 {
02086   DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
02087   /* FIXME - need to unregister when we switch interface for now
02088    * later should support idea of unset interface
02089    */
02090   dbus_g_proxy_manager_unregister (priv->manager, proxy);
02091   g_free (priv->interface);
02092   priv->interface = g_strdup (interface_name);
02093   dbus_g_proxy_manager_register (priv->manager, proxy);
02094 }
02095 
02103 const char*
02104 dbus_g_proxy_get_path (DBusGProxy        *proxy)
02105 {
02106   DBusGProxyPrivate *priv;
02107   
02108   g_return_val_if_fail (DBUS_IS_G_PROXY (proxy), NULL);
02109   g_return_val_if_fail (!DBUS_G_PROXY_DESTROYED (proxy), NULL);
02110 
02111   priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
02112 
02113   return priv->path;
02114 }
02115 
02116 static DBusMessage *
02117 dbus_g_proxy_marshal_args_to_message (DBusGProxy  *proxy,
02118                                       const char  *method,
02119                                       GValueArray *args)
02120 {
02121   DBusMessage *message;
02122   DBusMessageIter msgiter;
02123   guint i;
02124   DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
02125 
02126   message = dbus_message_new_method_call (priv->name,
02127                                           priv->path,
02128                                           priv->interface,
02129                                           method);
02130   if (message == NULL)
02131     goto oom;
02132 
02133   dbus_message_iter_init_append (message, &msgiter);
02134   for (i = 0; i < args->n_values; i++)
02135     {
02136       GValue *gvalue;
02137 
02138       gvalue = g_value_array_get_nth (args, i);
02139 
02140       if (!_dbus_gvalue_marshal (&msgiter, gvalue))
02141         g_assert_not_reached ();
02142     }
02143   return message;
02144  oom:
02145   return NULL;
02146 }
02147 
02148 static guint
02149 dbus_g_proxy_begin_call_internal (DBusGProxy          *proxy,
02150                                   const char          *method,
02151                                   DBusGProxyCallNotify notify,
02152                                   gpointer             user_data,
02153                                   GDestroyNotify       destroy,
02154                                   GValueArray         *args,
02155                                   int timeout)
02156 {
02157   DBusMessage *message;
02158   DBusPendingCall *pending;
02159   GPendingNotifyClosure *closure;
02160   guint call_id;
02161   DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
02162 
02163   pending = NULL;
02164 
02165   message = dbus_g_proxy_marshal_args_to_message (proxy, method, args);
02166   if (!message)
02167     goto oom;
02168 
02169   if (!dbus_connection_send_with_reply (priv->manager->connection,
02170                                         message,
02171                                         &pending,
02172                                         timeout))
02173     goto oom;
02174   dbus_message_unref (message);
02175   g_assert (pending != NULL);
02176 
02177   call_id = ++priv->call_id_counter;
02178 
02179   if (notify != NULL)
02180     {
02181       closure = g_new (GPendingNotifyClosure, 1);
02182       closure->proxy = proxy; /* No need to ref as the lifecycle is tied to proxy */
02183       closure->call_id = call_id;
02184       closure->func = notify;
02185       closure->data = user_data;
02186       closure->free_data_func = destroy;
02187       dbus_pending_call_set_notify (pending, d_pending_call_notify,
02188                                     closure,
02189                                     d_pending_call_free);
02190     }
02191 
02192   g_hash_table_insert (priv->pending_calls, GUINT_TO_POINTER (call_id), pending);
02193 
02194   return call_id;
02195  oom:
02196   g_error ("Out of memory");
02197   return 0;
02198 }
02199 
02200 static gboolean
02201 dbus_g_proxy_end_call_internal (DBusGProxy        *proxy,
02202                                 guint              call_id,
02203                                 GError           **error,
02204                                 GType              first_arg_type,
02205                                 va_list            args)
02206 {
02207   DBusMessage *reply;
02208   DBusMessageIter msgiter;
02209   DBusError derror;
02210   va_list args_unwind;
02211   guint over;
02212   int n_retvals_processed;
02213   gboolean ret;
02214   GType valtype;
02215   DBusPendingCall *pending;
02216   DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
02217 
02218   reply = NULL;
02219   ret = FALSE;
02220   n_retvals_processed = 0;
02221   over = 0;
02222 
02223   pending = g_hash_table_lookup (priv->pending_calls, GUINT_TO_POINTER (call_id));
02224   
02225   dbus_pending_call_block (pending);
02226   reply = dbus_pending_call_steal_reply (pending);
02227 
02228   g_assert (reply != NULL);
02229 
02230   dbus_error_init (&derror);
02231 
02232   switch (dbus_message_get_type (reply))
02233     {
02234     case DBUS_MESSAGE_TYPE_METHOD_RETURN:
02235       dbus_message_iter_init (reply, &msgiter);
02236       valtype = first_arg_type;
02237       while (valtype != G_TYPE_INVALID)
02238         {
02239           int arg_type;
02240           gpointer return_storage;
02241           GValue gvalue = { 0, };
02242           DBusGValueMarshalCtx context;
02243 
02244           context.gconnection = DBUS_G_CONNECTION_FROM_CONNECTION (priv->manager->connection);
02245           context.proxy = proxy;
02246 
02247           arg_type = dbus_message_iter_get_arg_type (&msgiter);
02248           if (arg_type == DBUS_TYPE_INVALID)
02249             {
02250               g_set_error (error, DBUS_GERROR,
02251                            DBUS_GERROR_INVALID_ARGS,
02252                            _("Too few arguments in reply"));
02253               goto out;
02254             }
02255 
02256           return_storage = va_arg (args, gpointer);
02257           if (return_storage == NULL)
02258             goto next;
02259 
02260           /* We handle variants specially; the caller is expected
02261            * to have already allocated storage for them.
02262            */
02263           if (arg_type == DBUS_TYPE_VARIANT
02264               && g_type_is_a (valtype, G_TYPE_VALUE))
02265             {
02266               if (!_dbus_gvalue_demarshal_variant (&context, &msgiter, (GValue*) return_storage, NULL))
02267                 {
02268                   g_set_error (error,
02269                                DBUS_GERROR,
02270                                DBUS_GERROR_INVALID_ARGS,
02271                                _("Couldn't convert argument, expected \"%s\""),
02272                                g_type_name (valtype));
02273                   goto out;
02274                 }
02275             }
02276           else
02277             {
02278               g_value_init (&gvalue, valtype);
02279 
02280               if (!_dbus_gvalue_demarshal (&context, &msgiter, &gvalue, error))
02281                 goto out;
02282 
02283               /* Anything that can be demarshaled must be storable */
02284               if (!_dbus_gvalue_store (&gvalue, (gpointer*) return_storage))
02285                 g_assert_not_reached ();
02286               /* Ownership of the value passes to the client, don't unset */
02287             }
02288           
02289         next:
02290           n_retvals_processed++;
02291           dbus_message_iter_next (&msgiter);
02292           valtype = va_arg (args, GType);
02293         }
02294       
02295       while (dbus_message_iter_get_arg_type (&msgiter) != DBUS_TYPE_INVALID)
02296         {
02297           over++;
02298           dbus_message_iter_next (&msgiter);
02299         }
02300 
02301       if (over > 0)
02302         {
02303           g_set_error (error, DBUS_GERROR,
02304                        DBUS_GERROR_INVALID_ARGS,
02305                        _("Too many arguments in reply; expected %d, got %d"),
02306                        n_retvals_processed, over);
02307           goto out;
02308         }
02309       break;
02310     case DBUS_MESSAGE_TYPE_ERROR:
02311       dbus_set_error_from_message (&derror, reply);
02312       dbus_set_g_error (error, &derror);
02313       dbus_error_free (&derror);
02314       goto out;
02315       break;
02316     default:
02317       dbus_set_error (&derror, DBUS_ERROR_FAILED,
02318                       "Reply was neither a method return nor an exception");
02319       dbus_set_g_error (error, &derror);
02320       dbus_error_free (&derror);
02321       goto out;
02322       break;
02323     }
02324 
02325   ret = TRUE;
02326  out:
02327   va_end (args);
02328 
02329   if (ret == FALSE)
02330     {
02331       int i;
02332       for (i = 0; i < n_retvals_processed; i++)
02333         {
02334           gpointer retval;
02335 
02336           retval = va_arg (args_unwind, gpointer);
02337 
02338           g_free (retval);
02339         }
02340     }
02341   va_end (args_unwind);
02342 
02343   g_hash_table_remove (priv->pending_calls, GUINT_TO_POINTER (call_id));
02344 
02345   if (reply)
02346     dbus_message_unref (reply);
02347   return ret;
02348 }
02349 
02372 DBusGProxyCall *
02373 dbus_g_proxy_begin_call (DBusGProxy          *proxy,
02374                          const char          *method,
02375                          DBusGProxyCallNotify notify,
02376                          gpointer             user_data,
02377                          GDestroyNotify       destroy,
02378                          GType                first_arg_type,
02379                          ...)
02380 {
02381   guint call_id;
02382   va_list args;
02383   GValueArray *arg_values;
02384   
02385   g_return_val_if_fail (DBUS_IS_G_PROXY (proxy), NULL);
02386   g_return_val_if_fail (!DBUS_G_PROXY_DESTROYED (proxy), NULL);
02387 
02388   va_start (args, first_arg_type);
02389 
02390   DBUS_G_VALUE_ARRAY_COLLECT_ALL (arg_values, first_arg_type, args);
02391   
02392   call_id = dbus_g_proxy_begin_call_internal (proxy, method, notify, user_data, destroy, arg_values,-1);
02393 
02394   g_value_array_free (arg_values);
02395 
02396   va_end (args);
02397 
02398   return DBUS_G_PROXY_ID_TO_CALL (call_id);
02399 }
02400 
02424 DBusGProxyCall *
02425 dbus_g_proxy_begin_call_with_timeout (DBusGProxy          *proxy,
02426                          const char          *method,
02427                          DBusGProxyCallNotify notify,
02428                          gpointer             user_data,
02429                          GDestroyNotify       destroy,
02430                          int timeout,
02431                          GType                first_arg_type,
02432                          ...)
02433 {
02434   guint call_id;
02435   va_list args;
02436   GValueArray *arg_values;
02437 
02438   g_return_val_if_fail (DBUS_IS_G_PROXY (proxy), NULL);
02439   g_return_val_if_fail (!DBUS_G_PROXY_DESTROYED (proxy), NULL);
02440 
02441   va_start (args, first_arg_type);
02442 
02443   DBUS_G_VALUE_ARRAY_COLLECT_ALL (arg_values, first_arg_type, args);
02444 
02445   call_id = dbus_g_proxy_begin_call_internal (proxy, method, notify, user_data, destroy, arg_values,timeout);
02446 
02447   g_value_array_free (arg_values);
02448 
02449   va_end (args);
02450 
02451   return DBUS_G_PROXY_ID_TO_CALL (call_id);
02452 }
02453 
02476 gboolean
02477 dbus_g_proxy_end_call (DBusGProxy          *proxy,
02478                        DBusGProxyCall      *call,
02479                        GError             **error,
02480                        GType                first_arg_type,
02481                        ...)
02482 {
02483   gboolean ret;
02484   va_list args;
02485 
02486   va_start (args, first_arg_type);
02487 
02488   ret = dbus_g_proxy_end_call_internal (proxy, GPOINTER_TO_UINT (call), error, first_arg_type, args);
02489 
02490   va_end (args);
02491   
02492   return ret;
02493 }
02494 
02511 gboolean
02512 dbus_g_proxy_call (DBusGProxy        *proxy,
02513                    const char        *method,
02514                    GError           **error,
02515                    GType              first_arg_type,
02516                    ...)
02517 {
02518   gboolean ret;
02519   guint call_id;
02520   va_list args;
02521   GValueArray *in_args;
02522 
02523   g_return_val_if_fail (DBUS_IS_G_PROXY (proxy), FALSE);
02524   g_return_val_if_fail (!DBUS_G_PROXY_DESTROYED (proxy), FALSE);
02525 
02526   va_start (args, first_arg_type);
02527 
02528   DBUS_G_VALUE_ARRAY_COLLECT_ALL (in_args, first_arg_type, args);
02529 
02530   call_id = dbus_g_proxy_begin_call_internal (proxy, method, NULL, NULL, NULL, in_args,-1);
02531 
02532   g_value_array_free (in_args);
02533 
02534   first_arg_type = va_arg (args, GType);
02535   ret = dbus_g_proxy_end_call_internal (proxy, call_id, error, first_arg_type, args);
02536 
02537   va_end (args);
02538 
02539   return ret;
02540 }
02541 
02559 gboolean
02560 dbus_g_proxy_call_with_timeout (DBusGProxy        *proxy,
02561                    const char        *method,
02562                    int timeout,
02563                    GError           **error,
02564                    GType              first_arg_type,
02565                    ...)
02566 {
02567   gboolean ret;
02568   guint call_id;
02569   va_list args;
02570   GValueArray *in_args;
02571 
02572   g_return_val_if_fail (DBUS_IS_G_PROXY (proxy), FALSE);
02573   g_return_val_if_fail (!DBUS_G_PROXY_DESTROYED (proxy), FALSE);
02574 
02575   va_start (args, first_arg_type);
02576 
02577   DBUS_G_VALUE_ARRAY_COLLECT_ALL (in_args, first_arg_type, args);
02578 
02579   call_id = dbus_g_proxy_begin_call_internal (proxy, method, NULL, NULL, NULL, in_args,timeout);
02580 
02581   g_value_array_free (in_args);
02582 
02583   first_arg_type = va_arg (args, GType);
02584   ret = dbus_g_proxy_end_call_internal (proxy, call_id, error, first_arg_type, args);
02585 
02586   va_end (args);
02587 
02588   return ret;
02589 }
02590 
02603 void
02604 dbus_g_proxy_call_no_reply (DBusGProxy               *proxy,
02605                             const char               *method,
02606                             GType                     first_arg_type,
02607                             ...)
02608 {
02609   DBusMessage *message;
02610   va_list args;
02611   GValueArray *in_args;
02612   DBusGProxyPrivate *priv;
02613   
02614   g_return_if_fail (DBUS_IS_G_PROXY (proxy));
02615   g_return_if_fail (!DBUS_G_PROXY_DESTROYED (proxy));
02616 
02617   priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
02618 
02619   va_start (args, first_arg_type);
02620   DBUS_G_VALUE_ARRAY_COLLECT_ALL (in_args, first_arg_type, args);
02621 
02622   message = dbus_g_proxy_marshal_args_to_message (proxy, method, in_args);
02623 
02624   g_value_array_free (in_args);
02625   va_end (args);
02626 
02627   if (!message)
02628     goto oom;
02629 
02630   dbus_message_set_no_reply (message, TRUE);
02631 
02632   if (!dbus_connection_send (priv->manager->connection,
02633                              message,
02634                              NULL))
02635     goto oom;
02636   dbus_message_unref (message);
02637   return;
02638   
02639  oom:
02640   g_error ("Out of memory");
02641 }
02642 
02653 void
02654 dbus_g_proxy_cancel_call (DBusGProxy        *proxy,
02655                           DBusGProxyCall    *call)
02656 {
02657   guint call_id;
02658   DBusPendingCall *pending;
02659   DBusGProxyPrivate *priv;
02660   
02661   g_return_if_fail (DBUS_IS_G_PROXY (proxy));
02662   g_return_if_fail (!DBUS_G_PROXY_DESTROYED (proxy));
02663 
02664   priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
02665 
02666   call_id = DBUS_G_PROXY_CALL_TO_ID (call);
02667 
02668   pending = g_hash_table_lookup (priv->pending_calls, GUINT_TO_POINTER (call_id));
02669   g_return_if_fail (pending != NULL);
02670 
02671   dbus_pending_call_cancel (pending);
02672 
02673   g_hash_table_remove (priv->pending_calls, GUINT_TO_POINTER (call_id));
02674 }
02675 
02696 void
02697 dbus_g_proxy_send (DBusGProxy          *proxy,
02698                    DBusMessage         *message,
02699                    dbus_uint32_t       *client_serial)
02700 {
02701   DBusGProxyPrivate *priv;
02702   
02703   g_return_if_fail (DBUS_IS_G_PROXY (proxy));
02704   g_return_if_fail (!DBUS_G_PROXY_DESTROYED (proxy));
02705   
02706   priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
02707   
02708   if (priv->name)
02709     {
02710       if (!dbus_message_set_destination (message, priv->name))
02711         g_error ("Out of memory");
02712     }
02713   if (priv->path)
02714     {
02715       if (!dbus_message_set_path (message, priv->path))
02716         g_error ("Out of memory");
02717     }
02718   if (priv->interface)
02719     {
02720       if (!dbus_message_set_interface (message, priv->interface))
02721         g_error ("Out of memory");
02722     }
02723   
02724   if (!dbus_connection_send (priv->manager->connection, message, client_serial))
02725     g_error ("Out of memory\n");
02726 }
02727 
02728 static void
02729 array_free_all (gpointer array)
02730 {
02731   g_array_free (array, TRUE);
02732 }
02733 
02744 void
02745 dbus_g_proxy_add_signal  (DBusGProxy        *proxy,
02746                           const char        *signal_name,
02747                           GType              first_type,
02748                           ...)
02749 {
02750   GQuark q;
02751   char *name;
02752   GArray *gtypesig;
02753   GType gtype;
02754   va_list args;
02755   DBusGProxyPrivate *priv;
02756 
02757   g_return_if_fail (DBUS_IS_G_PROXY (proxy));
02758   g_return_if_fail (!DBUS_G_PROXY_DESTROYED (proxy));
02759   g_return_if_fail (signal_name != NULL);
02760   
02761   priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
02762 
02763   name = create_signal_name (priv->interface, signal_name);
02764   
02765   q = g_quark_from_string (name);
02766   
02767   g_return_if_fail (g_datalist_id_get_data (&priv->signal_signatures, q) == NULL);
02768 
02769   gtypesig = g_array_new (FALSE, TRUE, sizeof (GType));
02770 
02771   va_start (args, first_type);
02772   gtype = first_type;
02773   while (gtype != G_TYPE_INVALID)
02774     {
02775       g_array_append_val (gtypesig, gtype);
02776       gtype = va_arg (args, GType);
02777     }
02778   va_end (args);
02779 
02780 #ifndef G_DISABLE_CHECKS
02781   if (_dbus_gobject_lookup_marshaller (G_TYPE_NONE, gtypesig->len, (const GType*) gtypesig->data) == NULL)
02782     g_warning ("No marshaller for signature of signal '%s'", signal_name);
02783 #endif
02784 
02785   
02786   g_datalist_id_set_data_full (&priv->signal_signatures,
02787                                q, gtypesig,
02788                                array_free_all);
02789 
02790   g_free (name);
02791 }
02792 
02805 void
02806 dbus_g_proxy_connect_signal (DBusGProxy             *proxy,
02807                              const char             *signal_name,
02808                              GCallback               handler,
02809                              void                   *data,
02810                              GClosureNotify          free_data_func)
02811 {
02812   char *name;
02813   GClosure *closure;
02814   GQuark q;
02815   DBusGProxyPrivate *priv;
02816 
02817   g_return_if_fail (DBUS_IS_G_PROXY (proxy));
02818   g_return_if_fail (!DBUS_G_PROXY_DESTROYED (proxy));
02819   g_return_if_fail (signal_name != NULL);
02820   g_return_if_fail (handler != NULL);
02821   
02822   priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
02823   name = create_signal_name (priv->interface, signal_name);
02824 
02825   q = g_quark_try_string (name);
02826 
02827 #ifndef G_DISABLE_CHECKS
02828   if (q == 0 || g_datalist_id_get_data (&priv->signal_signatures, q) == NULL)
02829     {
02830       g_warning ("Must add the signal '%s' with dbus_g_proxy_add_signal() prior to connecting to it\n", name);
02831       g_free (name);
02832       return;
02833     }
02834 #endif
02835   
02836   closure = g_cclosure_new (G_CALLBACK (handler), data, free_data_func);
02837   
02838   g_signal_connect_closure_by_id (G_OBJECT (proxy),
02839                                   signals[RECEIVED],
02840                                   q,
02841                                   closure, FALSE);
02842   
02843   g_free (name);
02844 }
02845 
02856 void
02857 dbus_g_proxy_disconnect_signal (DBusGProxy             *proxy,
02858                                 const char             *signal_name,
02859                                 GCallback               handler,
02860                                 void                   *data)
02861 {
02862   char *name;
02863   GQuark q;
02864   DBusGProxyPrivate *priv;
02865   
02866   g_return_if_fail (DBUS_IS_G_PROXY (proxy));
02867   g_return_if_fail (!DBUS_G_PROXY_DESTROYED (proxy));
02868   g_return_if_fail (signal_name != NULL);
02869   g_return_if_fail (handler != NULL);
02870 
02871   priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
02872   name = create_signal_name (priv->interface, signal_name);
02873 
02874   q = g_quark_try_string (name);
02875   
02876   if (q != 0)
02877     {
02878       g_signal_handlers_disconnect_matched (G_OBJECT (proxy),
02879                                             G_SIGNAL_MATCH_DETAIL |
02880                                             G_SIGNAL_MATCH_FUNC   |
02881                                             G_SIGNAL_MATCH_DATA,
02882                                             signals[RECEIVED],
02883                                             q,
02884                                             NULL,
02885                                             G_CALLBACK (handler), data);
02886     }
02887   else
02888     {
02889       g_warning ("Attempt to disconnect from signal '%s' which is not registered\n",
02890                  name);
02891     }
02892 
02893   g_free (name);
02894 }
02895 
02898 #ifdef DBUS_BUILD_TESTS
02899 
02905 gboolean
02906 _dbus_g_proxy_test (void)
02907 {
02908   
02909   
02910   return TRUE;
02911 }
02912 
02913 #endif /* DBUS_BUILD_TESTS */

Generated on Wed Oct 3 10:04:23 2007 for D-BUSGLibBindings by  doxygen 1.5.1