]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tube-handler.c
Little coding stype changes
[empathy.git] / libempathy / empathy-tube-handler.c
1 /*
2  * Copyright (C) 2008 Collabora Ltd.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  * 
18  * Authors: Xavier Claessens <xclaesse@gmail.com>
19  *          Elliot Fairweather <elliot.fairweather@collabora.co.uk>
20  */
21
22 #include <dbus/dbus-glib.h>
23
24 #include <telepathy-glib/dbus.h>
25 #include <telepathy-glib/connection.h>
26 #include <telepathy-glib/channel.h>
27 #include <telepathy-glib/interfaces.h>
28
29 #include <extensions/extensions.h>
30
31 #include "empathy-debug.h"
32 #include "empathy-tube.h"
33 #include "empathy-tube-handler.h"
34 #include "empathy-tubes.h"
35
36 #define DEBUG_DOMAIN "TubeHandler"
37
38 static void empathy_tube_handler_iface_init (EmpSvcTubeHandlerClass *klass);
39
40 enum
41 {
42   NEW_TUBE,
43   LAST_SIGNAL
44 };
45
46 static guint signals[LAST_SIGNAL];
47
48 G_DEFINE_TYPE_WITH_CODE (EmpathyTubeHandler, empathy_tube_handler,
49     G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE (EMP_TYPE_SVC_TUBE_HANDLER,
50     empathy_tube_handler_iface_init))
51
52 typedef struct
53 {
54   EmpathyTubeHandler *thandler;
55   gchar *bus_name;
56   gchar *connection;
57   gchar *channel;
58   guint handle_type;
59   guint handle;
60   guint id;
61 } IdleData;
62
63 static gboolean
64 empathy_tube_handler_handle_tube_idle_cb (gpointer data)
65 {
66   IdleData *idle_data = data;
67   TpConnection *connection;
68   TpChannel *channel;
69   EmpathyTubes *tubes;
70   EmpathyTube *tube;
71   static TpDBusDaemon *daemon = NULL;
72
73   if (!daemon)
74     daemon = tp_dbus_daemon_new (tp_get_bus ());
75
76   connection = tp_connection_new (daemon, idle_data->bus_name,
77       idle_data->connection, NULL);
78   channel = tp_channel_new (connection, idle_data->channel,
79       TP_IFACE_CHANNEL_TYPE_TUBES, idle_data->handle_type,
80       idle_data->handle, NULL);
81   tp_channel_run_until_ready (channel, NULL, NULL);
82
83   tubes = empathy_tubes_new (channel);
84   tube = empathy_tubes_get_tube (tubes, idle_data->id);
85
86   empathy_debug (DEBUG_DOMAIN, "New tube to be handled");
87
88   g_signal_emit (idle_data->thandler, signals[NEW_TUBE], 0, tube);
89
90   g_object_unref (tube);
91   g_object_unref (tubes);
92   g_object_unref (channel);
93   g_object_unref (connection);
94   g_free (idle_data->bus_name);
95   g_free (idle_data->connection);
96   g_free (idle_data->channel);
97   g_slice_free (IdleData, idle_data);
98
99   return FALSE;
100 }
101
102 static void
103 empathy_tube_handler_handle_tube (EmpSvcTubeHandler *self,
104                                   const gchar *bus_name,
105                                   const gchar *connection,
106                                   const gchar *channel,
107                                   guint handle_type,
108                                   guint handle,
109                                   guint id,
110                                   DBusGMethodInvocation *context)
111 {
112   EmpathyTubeHandler *thandler = EMPATHY_TUBE_HANDLER (self);
113   IdleData *data;
114
115   data = g_slice_new (IdleData);
116   data->thandler = thandler;
117   data->bus_name = g_strdup (bus_name);
118   data->connection = g_strdup (connection);
119   data->channel = g_strdup (channel);
120   data->handle_type = handle_type;
121   data->handle = handle;
122   data->id = id;
123
124   g_idle_add_full (G_PRIORITY_HIGH, empathy_tube_handler_handle_tube_idle_cb,
125       data, NULL);
126
127   emp_svc_tube_handler_return_from_handle_tube (context);
128 }
129
130 static void
131 empathy_tube_handler_class_init (EmpathyTubeHandlerClass *klass)
132 {
133   signals[NEW_TUBE] =
134       g_signal_new ("new-tube", G_OBJECT_CLASS_TYPE (klass),
135       G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
136       G_TYPE_NONE, 1, EMPATHY_TYPE_TUBE);
137 }
138
139 static void
140 empathy_tube_handler_iface_init (EmpSvcTubeHandlerClass *klass)
141 {
142   emp_svc_tube_handler_implement_handle_tube (klass,
143       empathy_tube_handler_handle_tube);
144 }
145
146 static void
147 empathy_tube_handler_init (EmpathyTubeHandler *thandler)
148 {
149 }
150
151 EmpathyTubeHandler *
152 empathy_tube_handler_new (const gchar *bus_name,
153                           const gchar *object_path)
154 {
155   EmpathyTubeHandler *thandler;
156   DBusGProxy *proxy;
157   guint result;
158   GError *error = NULL;
159
160   proxy = dbus_g_proxy_new_for_name (tp_get_bus (), DBUS_SERVICE_DBUS,
161       DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);
162
163   if (!dbus_g_proxy_call (proxy, "RequestName", &error,
164       G_TYPE_STRING, bus_name, G_TYPE_UINT, DBUS_NAME_FLAG_DO_NOT_QUEUE,
165       G_TYPE_INVALID, G_TYPE_UINT, &result, G_TYPE_INVALID))
166     {
167       empathy_debug (DEBUG_DOMAIN, "Failed to request name: %s",
168           error ? error->message : "No error given");
169       g_clear_error (&error);
170       return NULL;
171     }
172
173   g_object_unref (proxy);
174
175   thandler = g_object_new (EMPATHY_TYPE_TUBE_HANDLER, NULL);
176   dbus_g_connection_register_g_object (tp_get_bus (), object_path,
177       G_OBJECT (thandler));
178
179   return thandler;
180 }
181