]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tube-handler.c
Rename EmpathyTube to EmpathyTpTube and remove EmpathyTubes.
[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 <config.h>
23
24 #include <dbus/dbus-glib.h>
25
26 #include <telepathy-glib/dbus.h>
27 #include <telepathy-glib/connection.h>
28 #include <telepathy-glib/channel.h>
29 #include <telepathy-glib/interfaces.h>
30
31 #include <extensions/extensions.h>
32
33 #include "empathy-debug.h"
34 #include "empathy-tp-tube.h"
35 #include "empathy-tube-handler.h"
36
37 #define DEBUG_DOMAIN "TubeHandler"
38
39 static void empathy_tube_handler_iface_init (EmpSvcTubeHandlerClass *klass);
40
41 enum
42 {
43   NEW_TUBE,
44   LAST_SIGNAL
45 };
46
47 static guint signals[LAST_SIGNAL];
48
49 G_DEFINE_TYPE_WITH_CODE (EmpathyTubeHandler, empathy_tube_handler,
50     G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE (EMP_TYPE_SVC_TUBE_HANDLER,
51     empathy_tube_handler_iface_init))
52
53 typedef struct
54 {
55   EmpathyTubeHandler *thandler;
56   gchar *bus_name;
57   gchar *connection;
58   gchar *channel;
59   guint handle_type;
60   guint handle;
61   guint id;
62 } IdleData;
63
64 static gboolean
65 tube_handler_handle_tube_idle_cb (gpointer data)
66 {
67   IdleData *idle_data = data;
68   TpConnection *connection;
69   TpChannel *channel;
70   EmpathyTpTube *tube;
71   static TpDBusDaemon *daemon = NULL;
72
73   empathy_debug (DEBUG_DOMAIN, "New tube to be handled");
74
75   if (!daemon)
76     daemon = tp_dbus_daemon_new (tp_get_bus ());
77
78   connection = tp_connection_new (daemon, idle_data->bus_name,
79       idle_data->connection, NULL);
80   channel = tp_channel_new (connection, idle_data->channel,
81       TP_IFACE_CHANNEL_TYPE_TUBES, idle_data->handle_type,
82       idle_data->handle, NULL);
83   tp_channel_run_until_ready (channel, NULL, NULL);
84
85   tube = empathy_tp_tube_new (channel, idle_data->id);
86   g_signal_emit (idle_data->thandler, signals[NEW_TUBE], 0, tube);
87
88   g_object_unref (tube);
89   g_object_unref (channel);
90   g_object_unref (connection);
91   g_free (idle_data->bus_name);
92   g_free (idle_data->connection);
93   g_free (idle_data->channel);
94   g_slice_free (IdleData, idle_data);
95
96   return FALSE;
97 }
98
99 static void
100 tube_handler_handle_tube (EmpSvcTubeHandler *self,
101                           const gchar *bus_name,
102                           const gchar *connection,
103                           const gchar *channel,
104                           guint handle_type,
105                           guint handle,
106                           guint id,
107                           DBusGMethodInvocation *context)
108 {
109   EmpathyTubeHandler *thandler = EMPATHY_TUBE_HANDLER (self);
110   IdleData *data;
111
112   data = g_slice_new (IdleData);
113   data->thandler = thandler;
114   data->bus_name = g_strdup (bus_name);
115   data->connection = g_strdup (connection);
116   data->channel = g_strdup (channel);
117   data->handle_type = handle_type;
118   data->handle = handle;
119   data->id = id;
120
121   g_idle_add_full (G_PRIORITY_HIGH, tube_handler_handle_tube_idle_cb,
122       data, NULL);
123
124   emp_svc_tube_handler_return_from_handle_tube (context);
125 }
126
127 static void
128 empathy_tube_handler_class_init (EmpathyTubeHandlerClass *klass)
129 {
130   signals[NEW_TUBE] =
131       g_signal_new ("new-tube", G_OBJECT_CLASS_TYPE (klass),
132       G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
133       G_TYPE_NONE, 1, EMPATHY_TYPE_TP_TUBE);
134 }
135
136 static void
137 empathy_tube_handler_iface_init (EmpSvcTubeHandlerClass *klass)
138 {
139   emp_svc_tube_handler_implement_handle_tube (klass,
140       tube_handler_handle_tube);
141 }
142
143 static void
144 empathy_tube_handler_init (EmpathyTubeHandler *thandler)
145 {
146 }
147
148 EmpathyTubeHandler *
149 empathy_tube_handler_new (const gchar *bus_name,
150                           const gchar *object_path)
151 {
152   EmpathyTubeHandler *thandler;
153   DBusGProxy *proxy;
154   guint result;
155   GError *error = NULL;
156
157   proxy = dbus_g_proxy_new_for_name (tp_get_bus (), DBUS_SERVICE_DBUS,
158       DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);
159
160   if (!dbus_g_proxy_call (proxy, "RequestName", &error,
161       G_TYPE_STRING, bus_name, G_TYPE_UINT, DBUS_NAME_FLAG_DO_NOT_QUEUE,
162       G_TYPE_INVALID, G_TYPE_UINT, &result, G_TYPE_INVALID))
163     {
164       empathy_debug (DEBUG_DOMAIN, "Failed to request name: %s",
165           error ? error->message : "No error given");
166       g_clear_error (&error);
167       return NULL;
168     }
169
170   g_object_unref (proxy);
171
172   thandler = g_object_new (EMPATHY_TYPE_TUBE_HANDLER, NULL);
173   dbus_g_connection_register_g_object (tp_get_bus (), object_path,
174       G_OBJECT (thandler));
175
176   return thandler;
177 }
178