]> git.0d.be Git - empathy.git/blob - libempathy/empathy-utils.c
Various whitespace and comment fixes. (Jonny Lamb)
[empathy.git] / libempathy / empathy-utils.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2003-2007 Imendio AB
4  * Copyright (C) 2007-2008 Collabora Ltd.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  * Authors: Richard Hult <richard@imendio.com>
22  *          Martyn Russell <martyn@imendio.com>
23  *          Xavier Claessens <xclaesse@gmail.com>
24  */
25
26 #include "config.h"
27
28 #include <string.h>
29 #include <time.h>
30 #include <sys/types.h>
31 #include <regex.h>
32
33 #include <gio/gio.h>
34 #include <glib/gi18n.h>
35
36 #include <libxml/uri.h>
37 #include <telepathy-glib/connection.h>
38 #include <telepathy-glib/channel.h>
39 #include <telepathy-glib/dbus.h>
40
41 #include <extensions/extensions.h>
42
43 #include "empathy-utils.h"
44 #include "empathy-contact-factory.h"
45 #include "empathy-contact-manager.h"
46
47 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
48 #include "empathy-debug.h"
49
50 static void regex_init (void);
51
52 gchar *
53 empathy_substring (const gchar *str,
54                   gint         start,
55                   gint         end)
56 {
57         return g_strndup (str + start, end - start);
58 }
59
60 /*
61  * Regular Expression code to match urls.
62  */
63 #define APTCHARS  "-A-Za-z0-9,-."
64 #define USERCHARS "-A-Za-z0-9"
65 #define PASSCHARS "-A-Za-z0-9,?;.:/!%$^*&~\"#'"
66 #define HOSTCHARS "-A-Za-z0-9_"
67 #define PATHCHARS "-A-Za-z0-9_$.+!*(),;:@&=?/~#%"
68 #define SCHEME    "(news:|telnet:|nntp:|file:/|https?:|ftps?:|webcal:)"
69 #define USER      "[" USERCHARS "]+(:["PASSCHARS "]+)?"
70 #define URLPATH   "/[" PATHCHARS "]*[^]'.}>) \t\r\n,\\\"]"
71
72 static regex_t dingus[EMPATHY_REGEX_ALL];
73
74 static void
75 regex_init (void)
76 {
77         static gboolean  inited = FALSE;
78         const gchar     *expression;
79         gint             i;
80
81         if (inited) {
82                 return;
83         }
84
85         for (i = 0; i < EMPATHY_REGEX_ALL; i++) {
86                 switch (i) {
87                 case EMPATHY_REGEX_AS_IS:
88                         expression =
89                                 SCHEME "//(" USER "@)?[" HOSTCHARS ".]+"
90                                 "(:[0-9]+)?(" URLPATH ")?";
91                         break;
92                 case EMPATHY_REGEX_BROWSER:
93                         expression =
94                                 "(www|ftp)[" HOSTCHARS "]*\\.[" HOSTCHARS ".]+"
95                                 "(:[0-9]+)?(" URLPATH ")?";
96                         break;
97                 case EMPATHY_REGEX_APT:
98                         expression =
99                                 "apt://[" APTCHARS "]*";
100                         break;
101                 case EMPATHY_REGEX_EMAIL:
102                         expression =
103                                 "(mailto:)?[a-z0-9][a-z0-9._-]*@[a-z0-9]"
104                                 "[a-z0-9-]*(\\.[a-z0-9][a-z0-9-]*)+";
105                         break;
106                 case EMPATHY_REGEX_OTHER:
107                         expression =
108                                 "news:[-A-Z\\^_a-z{|}~!\"#$%&'()*+,./0-9;:=?`]+"
109                                 "@[" HOSTCHARS ".]+(:[0-9]+)?";
110                         break;
111                 default:
112                         /* Silence the compiler. */
113                         expression = NULL;
114                         continue;
115                 }
116
117                 memset (&dingus[i], 0, sizeof (regex_t));
118                 regcomp (&dingus[i], expression, REG_EXTENDED | REG_ICASE);
119         }
120
121         inited = TRUE;
122 }
123
124 gint
125 empathy_regex_match (EmpathyRegExType  type,
126                     const gchar     *msg,
127                     GArray          *start,
128                     GArray          *end)
129 {
130         regmatch_t matches[1];
131         gint       ret = 0;
132         gint       num_matches = 0;
133         gint       offset = 0;
134         gint       i;
135
136         g_return_val_if_fail (type >= 0 || type <= EMPATHY_REGEX_ALL, 0);
137
138         regex_init ();
139
140         while (!ret && type != EMPATHY_REGEX_ALL) {
141                 ret = regexec (&dingus[type], msg + offset, 1, matches, 0);
142                 if (ret == 0) {
143                         gint s;
144
145                         num_matches++;
146
147                         s = matches[0].rm_so + offset;
148                         offset = matches[0].rm_eo + offset;
149
150                         g_array_append_val (start, s);
151                         g_array_append_val (end, offset);
152                 }
153         }
154
155         if (type != EMPATHY_REGEX_ALL) {
156                 DEBUG ("Found %d matches for regex type:%d", num_matches, type);
157                 return num_matches;
158         }
159
160         /* If EMPATHY_REGEX_ALL then we run ALL regex's on the string. */
161         for (i = 0; i < EMPATHY_REGEX_ALL; i++, ret = 0) {
162                 while (!ret) {
163                         ret = regexec (&dingus[i], msg + offset, 1, matches, 0);
164                         if (ret == 0) {
165                                 gint s;
166
167                                 num_matches++;
168
169                                 s = matches[0].rm_so + offset;
170                                 offset = matches[0].rm_eo + offset;
171
172                                 g_array_append_val (start, s);
173                                 g_array_append_val (end, offset);
174                         }
175                 }
176         }
177
178         DEBUG ("Found %d matches for ALL regex types", num_matches);
179
180         return num_matches;
181 }
182
183 gint
184 empathy_strcasecmp (const gchar *s1,
185                    const gchar *s2)
186 {
187         return empathy_strncasecmp (s1, s2, -1);
188 }
189
190 gint
191 empathy_strncasecmp (const gchar *s1,
192                     const gchar *s2,
193                     gsize        n)
194 {
195         gchar *u1, *u2;
196         gint   ret_val;
197
198         u1 = g_utf8_casefold (s1, n);
199         u2 = g_utf8_casefold (s2, n);
200
201         ret_val = g_utf8_collate (u1, u2);
202         g_free (u1);
203         g_free (u2);
204
205         return ret_val;
206 }
207
208 gboolean
209 empathy_xml_validate (xmlDoc      *doc,
210                      const gchar *dtd_filename)
211 {
212         gchar        *path, *escaped;
213         xmlValidCtxt  cvp;
214         xmlDtd       *dtd;
215         gboolean      ret;
216
217         path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "libempathy",
218                                  dtd_filename, NULL);
219         if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
220                 g_free (path);
221                 path = g_build_filename (DATADIR, "empathy", dtd_filename, NULL);
222         }
223         DEBUG ("Loading dtd file %s", path);
224
225         /* The list of valid chars is taken from libxml. */
226         escaped = xmlURIEscapeStr (path, ":@&=+$,/?;");
227         g_free (path);
228
229         memset (&cvp, 0, sizeof (cvp));
230         dtd = xmlParseDTD (NULL, escaped);
231         ret = xmlValidateDtd (&cvp, doc, dtd);
232
233         xmlFree (escaped);
234         xmlFreeDtd (dtd);
235
236         return ret;
237 }
238
239 xmlNodePtr
240 empathy_xml_node_get_child (xmlNodePtr   node, 
241                            const gchar *child_name)
242 {
243         xmlNodePtr l;
244
245         g_return_val_if_fail (node != NULL, NULL);
246         g_return_val_if_fail (child_name != NULL, NULL);
247
248         for (l = node->children; l; l = l->next) {
249                 if (l->name && strcmp (l->name, child_name) == 0) {
250                         return l;
251                 }
252         }
253
254         return NULL;
255 }
256
257 xmlChar *
258 empathy_xml_node_get_child_content (xmlNodePtr   node, 
259                                    const gchar *child_name)
260 {
261         xmlNodePtr l;
262
263         g_return_val_if_fail (node != NULL, NULL);
264         g_return_val_if_fail (child_name != NULL, NULL);
265
266         l = empathy_xml_node_get_child (node, child_name);
267         if (l) {
268                 return xmlNodeGetContent (l);
269         }
270                 
271         return NULL;
272 }
273
274 xmlNodePtr
275 empathy_xml_node_find_child_prop_value (xmlNodePtr   node, 
276                                        const gchar *prop_name,
277                                        const gchar *prop_value)
278 {
279         xmlNodePtr l;
280         xmlNodePtr found = NULL;
281
282         g_return_val_if_fail (node != NULL, NULL);
283         g_return_val_if_fail (prop_name != NULL, NULL);
284         g_return_val_if_fail (prop_value != NULL, NULL);
285
286         for (l = node->children; l && !found; l = l->next) {
287                 xmlChar *prop;
288
289                 if (!xmlHasProp (l, prop_name)) {
290                         continue;
291                 }
292
293                 prop = xmlGetProp (l, prop_name);
294                 if (prop && strcmp (prop, prop_value) == 0) {
295                         found = l;
296                 }
297                 
298                 xmlFree (prop);
299         }
300                 
301         return found;
302 }
303
304 guint
305 empathy_account_hash (gconstpointer key)
306 {
307         g_return_val_if_fail (MC_IS_ACCOUNT (key), 0);
308
309         return g_str_hash (mc_account_get_unique_name (MC_ACCOUNT (key)));
310 }
311
312 gboolean
313 empathy_account_equal (gconstpointer a,
314                        gconstpointer b)
315 {
316         const gchar *name_a;
317         const gchar *name_b;
318
319         g_return_val_if_fail (MC_IS_ACCOUNT (a), FALSE);
320         g_return_val_if_fail (MC_IS_ACCOUNT (b), FALSE);
321
322         name_a = mc_account_get_unique_name (MC_ACCOUNT (a));
323         name_b = mc_account_get_unique_name (MC_ACCOUNT (b));
324
325         return g_str_equal (name_a, name_b);
326 }
327
328 MissionControl *
329 empathy_mission_control_new (void)
330 {
331         static MissionControl *mc = NULL;
332
333         if (!mc) {
334                 mc = mission_control_new (tp_get_bus ());
335                 g_object_add_weak_pointer (G_OBJECT (mc), (gpointer) &mc);
336         } else {
337                 g_object_ref (mc);
338         }
339
340         return mc;
341 }
342
343 const gchar *
344 empathy_presence_get_default_message (McPresence presence)
345 {
346         switch (presence) {
347         case MC_PRESENCE_AVAILABLE:
348                 return _("Available");
349         case MC_PRESENCE_DO_NOT_DISTURB:
350                 return _("Busy");
351         case MC_PRESENCE_AWAY:
352         case MC_PRESENCE_EXTENDED_AWAY:
353                 return _("Away");
354         case MC_PRESENCE_HIDDEN:
355                 return _("Hidden");
356         case MC_PRESENCE_OFFLINE:
357         case MC_PRESENCE_UNSET:
358                 return _("Offline");
359         default:
360                 g_assert_not_reached ();
361         }
362
363         return NULL;
364 }
365
366 const gchar *
367 empathy_presence_to_str (McPresence presence)
368 {
369         switch (presence) {
370         case MC_PRESENCE_AVAILABLE:
371                 return "available";
372         case MC_PRESENCE_DO_NOT_DISTURB:
373                 return "busy";
374         case MC_PRESENCE_AWAY:
375                 return "away";
376         case MC_PRESENCE_EXTENDED_AWAY:
377                 return "ext_away";
378         case MC_PRESENCE_HIDDEN:
379                 return "hidden";
380         case MC_PRESENCE_OFFLINE:
381                 return "offline";
382         case MC_PRESENCE_UNSET:
383                 return "unset";
384         default:
385                 g_assert_not_reached ();
386         }
387
388         return NULL;
389 }
390
391 McPresence
392 empathy_presence_from_str (const gchar *str)
393 {
394         if (strcmp (str, "available") == 0) {
395                 return MC_PRESENCE_AVAILABLE;
396         } else if ((strcmp (str, "dnd") == 0) || (strcmp (str, "busy") == 0)) {
397                 return MC_PRESENCE_DO_NOT_DISTURB;
398         } else if ((strcmp (str, "away") == 0) || (strcmp (str, "brb") == 0)) {
399                 return MC_PRESENCE_AWAY;
400         } else if ((strcmp (str, "xa") == 0) || (strcmp (str, "ext_away") == 0)) {
401                 return MC_PRESENCE_EXTENDED_AWAY;
402         } else if (strcmp (str, "hidden") == 0) {
403                 return MC_PRESENCE_HIDDEN;
404         } else if (strcmp (str, "offline") == 0) {
405                 return MC_PRESENCE_OFFLINE;
406         } else if (strcmp (str, "unset") == 0) {
407                 return MC_PRESENCE_UNSET;
408         }
409
410         return MC_PRESENCE_UNSET;
411 }
412
413 gchar *
414 empathy_file_lookup (const gchar *filename, const gchar *subdir)
415 {
416         gchar *path;
417
418         if (!subdir) {
419                 subdir = ".";
420         }
421
422         path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), subdir, filename, NULL);
423         if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
424                 g_free (path);
425                 path = g_build_filename (DATADIR, "empathy", filename, NULL);
426         }
427
428         return path;
429 }
430
431 typedef struct {
432         EmpathyRunUntilReadyFunc  func;
433         gpointer                  user_data;
434         GObject                  *object;
435         GMainLoop                *loop;
436 } RunUntilReadyData;
437
438 static void
439 run_until_ready_cb (RunUntilReadyData *data)
440 {
441         if (!data->func || data->func (data->object, data->user_data)) {
442                 DEBUG ("Object %p is ready", data->object);
443                 g_main_loop_quit (data->loop);
444         }
445 }
446
447 static gboolean
448 object_is_ready (GObject *object,
449                  gpointer user_data)
450 {
451         gboolean ready;
452
453         g_object_get (object, "ready", &ready, NULL);
454
455         return ready;
456 }
457
458 void
459 empathy_run_until_ready_full (gpointer                  object,
460                               const gchar              *signal,
461                               EmpathyRunUntilReadyFunc  func,
462                               gpointer                  user_data,
463                               GMainLoop               **loop)
464 {
465         RunUntilReadyData  data;
466         gulong             signal_id;
467
468         g_return_if_fail (G_IS_OBJECT (object));
469         g_return_if_fail (signal != NULL);
470
471         if (func && func (object, user_data)) {
472                 return;
473         }
474
475         DEBUG ("Starting run until ready for object %p", object);
476
477         data.func = func;
478         data.user_data = user_data;
479         data.object = object;
480         data.loop = g_main_loop_new (NULL, FALSE);
481
482         signal_id = g_signal_connect_swapped (object, signal,
483                                               G_CALLBACK (run_until_ready_cb),
484                                               &data);
485         if (loop != NULL) {
486                 *loop = data.loop;
487         }
488
489         g_main_loop_run (data.loop);
490
491         if (loop != NULL) {
492                 *loop = NULL;
493         }
494
495         g_signal_handler_disconnect (object, signal_id);
496         g_main_loop_unref (data.loop);
497 }
498
499 void
500 empathy_run_until_ready (gpointer object)
501 {
502         empathy_run_until_ready_full (object, "notify::ready", object_is_ready,
503                                       NULL, NULL);
504 }
505
506 McAccount *
507 empathy_channel_get_account (TpChannel *channel)
508 {
509         TpConnection   *connection;
510         McAccount      *account;
511         MissionControl *mc;
512
513         g_object_get (channel, "connection", &connection, NULL);
514         mc = empathy_mission_control_new ();
515         account = mission_control_get_account_for_tpconnection (mc, connection, NULL);
516         g_object_unref (connection);
517         g_object_unref (mc);
518
519         return account;
520 }
521
522 typedef void (*AccountStatusChangedFunc) (MissionControl           *mc,
523                                           TpConnectionStatus        status,
524                                           McPresence                presence,
525                                           TpConnectionStatusReason  reason,
526                                           const gchar              *unique_name,
527                                           gpointer                  user_data);
528
529 typedef struct {
530         AccountStatusChangedFunc handler;
531         gpointer                 user_data;
532         GClosureNotify           free_func;
533         MissionControl          *mc;
534 } AccountStatusChangedData;
535
536 typedef struct {
537         TpConnectionStatus        status;
538         McPresence                presence;
539         TpConnectionStatusReason  reason;
540         gchar                    *unique_name;
541         AccountStatusChangedData *data;
542 } InvocationData;
543
544 static void
545 account_status_changed_data_free (gpointer ptr,
546                                   GClosure *closure)
547 {
548         AccountStatusChangedData *data = ptr;
549
550         if (data->free_func) {
551                 data->free_func (data->user_data, closure);
552         }
553         g_object_unref (data->mc);
554         g_slice_free (AccountStatusChangedData, data);
555 }
556
557 static gboolean
558 account_status_changed_invoke_callback (gpointer data)
559 {
560         InvocationData *invocation_data = data;
561
562         invocation_data->data->handler (invocation_data->data->mc,
563                                         invocation_data->status,
564                                         invocation_data->presence,
565                                         invocation_data->reason,
566                                         invocation_data->unique_name,
567                                         invocation_data->data->user_data);
568
569         g_free (invocation_data->unique_name);
570         g_slice_free (InvocationData, invocation_data);
571
572         return FALSE;
573 }
574
575 static void
576 account_status_changed_cb (MissionControl           *mc,
577                            TpConnectionStatus        status,
578                            McPresence                presence,
579                            TpConnectionStatusReason  reason,
580                            const gchar              *unique_name,
581                            AccountStatusChangedData *data)
582 {
583         InvocationData *invocation_data;
584
585         invocation_data = g_slice_new (InvocationData);
586         invocation_data->status = status;
587         invocation_data->presence = presence;
588         invocation_data->reason = reason;
589         invocation_data->unique_name = g_strdup (unique_name);
590         invocation_data->data = data;
591
592         g_idle_add_full (G_PRIORITY_HIGH,
593                          account_status_changed_invoke_callback,
594                          invocation_data, NULL);
595 }
596
597 gpointer
598 empathy_connect_to_account_status_changed (MissionControl *mc,
599                                            GCallback       handler,
600                                            gpointer        user_data,
601                                            GClosureNotify  free_func)
602 {
603         AccountStatusChangedData *data;
604
605         g_return_val_if_fail (IS_MISSIONCONTROL (mc), NULL);
606         g_return_val_if_fail (handler != NULL, NULL);
607         
608         data = g_slice_new (AccountStatusChangedData);
609         data->handler = (AccountStatusChangedFunc) handler;
610         data->user_data = user_data;
611         data->free_func = free_func;
612         data->mc = g_object_ref (mc);
613
614         dbus_g_proxy_connect_signal (DBUS_G_PROXY (mc), "AccountStatusChanged",
615                                      G_CALLBACK (account_status_changed_cb),
616                                      data, account_status_changed_data_free);
617
618         return data;
619 }
620
621 void
622 empathy_disconnect_account_status_changed (gpointer token)
623 {
624         AccountStatusChangedData *data = token;
625
626         dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (data->mc),
627                                         "AccountStatusChanged",
628                                         G_CALLBACK (account_status_changed_cb),
629                                         data);
630 }
631
632 guint
633 empathy_proxy_hash (gconstpointer key)
634 {
635         TpProxy      *proxy = TP_PROXY (key);
636         TpProxyClass *proxy_class = TP_PROXY_GET_CLASS (key);
637
638         g_return_val_if_fail (TP_IS_PROXY (proxy), 0);
639         g_return_val_if_fail (proxy_class->must_have_unique_name, 0);
640
641         return g_str_hash (proxy->object_path) ^ g_str_hash (proxy->bus_name);
642 }
643
644 gboolean
645 empathy_proxy_equal (gconstpointer a,
646                      gconstpointer b)
647 {
648         TpProxy *proxy_a = TP_PROXY (a);
649         TpProxy *proxy_b = TP_PROXY (b);
650         TpProxyClass *proxy_a_class = TP_PROXY_GET_CLASS (a);
651         TpProxyClass *proxy_b_class = TP_PROXY_GET_CLASS (b);
652
653         g_return_val_if_fail (TP_IS_PROXY (proxy_a), FALSE);
654         g_return_val_if_fail (TP_IS_PROXY (proxy_b), FALSE);
655         g_return_val_if_fail (proxy_a_class->must_have_unique_name, 0);
656         g_return_val_if_fail (proxy_b_class->must_have_unique_name, 0);
657
658         return g_str_equal (proxy_a->object_path, proxy_b->object_path) &&
659                g_str_equal (proxy_a->bus_name, proxy_b->bus_name);
660 }
661
662 typedef struct {
663         gint timeout_ms;
664         gchar *channel_type;
665         guint handle_type;
666         empathy_connection_callback_for_request_channel callback;
667         gpointer user_data;
668         GDestroyNotify destroy;
669         TpHandle handle;
670         gboolean suppress_handler;
671         guint ref_count;
672 } ConnectionRequestChannelData;
673
674 static void
675 connection_request_channel_data_unref (gpointer user_data)
676 {
677         ConnectionRequestChannelData *data = (ConnectionRequestChannelData*) user_data;
678
679         if (--data->ref_count == 0) {
680                 g_free (data->channel_type);
681                 if (data->destroy) {
682                         data->destroy (data->user_data);
683                 }
684                 g_slice_free (ConnectionRequestChannelData, data);
685         }
686 }
687
688 static void
689 connection_request_channel_cb (TpConnection *connection,
690                                const gchar *object_path,
691                                const GError *error,
692                                gpointer user_data,
693                                GObject *weak_object)
694 {
695         ConnectionRequestChannelData *data = (ConnectionRequestChannelData*) user_data;
696         TpChannel *channel;
697
698         if (!data->callback) {
699                 return;
700         }
701
702         if (error) {
703                 data->callback (connection, NULL, error, data->user_data, weak_object);
704                 return;
705         }
706
707         channel = tp_channel_new (connection, object_path,
708                                   data->channel_type,
709                                   data->handle_type,
710                                   data->handle, NULL);
711
712         data->callback (connection, channel, NULL, data->user_data, weak_object);
713         g_object_unref (channel);
714 }
715
716 static void
717 connection_request_handles_cb (TpConnection *connection,
718                                const GArray *handles,
719                                const GError *error,
720                                gpointer user_data,
721                                GObject *weak_object)
722 {
723         ConnectionRequestChannelData *data = (ConnectionRequestChannelData*) user_data;
724
725         if (error) {
726                 if (data->callback) {
727                         data->callback (connection, NULL, error, data->user_data, weak_object);
728                 }
729                 return;
730         }
731
732         data->handle = g_array_index (handles, guint, 0);
733         data->ref_count++;
734         tp_cli_connection_call_request_channel (connection, data->timeout_ms,
735                                                 data->channel_type,
736                                                 data->handle_type,
737                                                 data->handle,
738                                                 data->suppress_handler,
739                                                 connection_request_channel_cb,
740                                                 data,
741                                                 (GDestroyNotify) connection_request_channel_data_unref,
742                                                 weak_object);
743 }
744
745 void
746 empathy_connection_request_channel (TpConnection *connection,
747                                     gint timeout_ms,
748                                     const gchar *channel_type,
749                                     guint handle_type,
750                                     const gchar *name,
751                                     gboolean suppress_handler,
752                                     empathy_connection_callback_for_request_channel callback,
753                                     gpointer user_data,
754                                     GDestroyNotify destroy,
755                                     GObject *weak_object)
756 {
757         const gchar *names[] = {name, NULL};
758         ConnectionRequestChannelData *data;
759
760         data = g_slice_new (ConnectionRequestChannelData);
761         data->timeout_ms = timeout_ms;
762         data->channel_type = g_strdup (channel_type);
763         data->handle_type = handle_type;
764         data->callback = callback;
765         data->user_data = user_data;
766         data->destroy = destroy;
767         data->handle = 0;
768         data->suppress_handler = suppress_handler;
769         data->ref_count = 1;
770         tp_cli_connection_call_request_handles (connection,
771                                                 timeout_ms,
772                                                 handle_type,
773                                                 names,
774                                                 connection_request_handles_cb,
775                                                 data,
776                                                 (GDestroyNotify) connection_request_channel_data_unref,
777                                                 weak_object);
778 }
779
780 EmpathyTpFile *
781 empathy_send_file (EmpathyContact *contact,
782                    GFile          *gfile)
783 {
784         GFileInfo      *info;
785         guint64         size;
786         GInputStream   *in_stream = NULL;
787         MissionControl *mc;
788         McAccount      *account;
789         TpConnection   *connection;
790         guint           handle;
791         gchar          *object_path;
792         TpChannel      *channel;
793         EmpathyTpFile  *tp_file;
794         GError         *error = NULL;
795         GValue          value = { 0 };
796         gchar          *filename;
797
798         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
799         g_return_val_if_fail (G_IS_FILE (gfile), NULL);
800
801         info = g_file_query_info (gfile,
802                                   G_FILE_ATTRIBUTE_STANDARD_SIZE ","
803                                   G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
804                                   0, NULL, NULL);
805         size = info ? g_file_info_get_size (info) : EMPATHY_TP_FILE_UNKNOWN_SIZE;
806         filename = g_file_get_basename (gfile);
807         in_stream = G_INPUT_STREAM (g_file_read (gfile, NULL, NULL));
808         mc = empathy_mission_control_new ();
809         account = empathy_contact_get_account (contact);
810         connection = mission_control_get_tpconnection (mc, account, NULL);
811         tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
812         handle = empathy_contact_get_handle (contact);
813
814         DEBUG ("Sending %s from a stream to %s (size %llu, content-type %s)",
815                filename, empathy_contact_get_name (contact), size,
816                g_file_info_get_content_type (info));
817
818         if (!tp_cli_connection_run_request_channel (connection, -1,
819                                                     EMP_IFACE_CHANNEL_TYPE_FILE,
820                                                     TP_HANDLE_TYPE_CONTACT,
821                                                     handle,
822                                                     FALSE,
823                                                     &object_path,
824                                                     &error,
825                                                     NULL)) {
826                 DEBUG ("Couldn't request channel: %s",
827                        error ? error->message : "No error given");
828                 g_clear_error (&error);
829                 g_object_unref (mc);
830                 g_object_unref (connection);
831                 return NULL;
832         }
833
834         channel = tp_channel_new (connection,
835                                   object_path,
836                                   EMP_IFACE_CHANNEL_TYPE_FILE,
837                                   TP_HANDLE_TYPE_CONTACT,
838                                   handle,
839                                   NULL);
840
841         /* TODO: this should go in CreateChannel in the new requests API */
842
843         g_value_init (&value, G_TYPE_STRING);
844         g_value_set_string (&value, g_filename_display_basename (filename));
845         tp_cli_dbus_properties_run_set (TP_PROXY (channel),
846                 -1, EMP_IFACE_CHANNEL_TYPE_FILE, "Filename",
847                 &value, NULL, NULL);
848         g_value_reset (&value);
849
850         g_value_set_string (&value, g_file_info_get_content_type (info));
851         tp_cli_dbus_properties_run_set (TP_PROXY (channel),
852                 -1, EMP_IFACE_CHANNEL_TYPE_FILE, "ContentType",
853                 &value, NULL, NULL);
854
855         g_value_unset (&value);
856
857         g_value_init (&value, G_TYPE_UINT64);
858         g_value_set_uint64 (&value, size);
859         tp_cli_dbus_properties_run_set (TP_PROXY (channel),
860                 -1, EMP_IFACE_CHANNEL_TYPE_FILE, "Size",
861                 &value, NULL, NULL);
862         g_value_unset (&value);
863
864         tp_file = empathy_tp_file_new (account, channel);
865
866         if (tp_file) {
867                 empathy_tp_file_set_input_stream (tp_file, in_stream);
868         }
869
870         empathy_tp_file_offer (tp_file);
871
872         g_object_unref (mc);
873         g_object_unref (connection);
874         g_object_unref (channel);
875         g_free (object_path);
876
877         return tp_file;
878 }
879