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