]> git.0d.be Git - empathy.git/blob - libempathy/empathy-request-util.c
Merge branch 'gnome-3-8'
[empathy.git] / libempathy / empathy-request-util.c
1 /* * Copyright (C) 2007-2010 Collabora Ltd.
2  *
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16  *
17  * Authors: Xavier Claessens <xclaesse@gmail.com>
18  *          Sjoerd Simons <sjoerd.simons@collabora.co.uk>
19  *          Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
20  */
21
22 #include "config.h"
23 #include "empathy-request-util.h"
24
25 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
26 #include "empathy-debug.h"
27
28 void
29 empathy_chat_with_contact (EmpathyContact *contact,
30     gint64 timestamp)
31 {
32   empathy_chat_with_contact_id (
33       empathy_contact_get_account (contact), empathy_contact_get_id (contact),
34       timestamp, NULL, NULL);
35 }
36
37 static void
38 ensure_text_channel_cb (GObject *source,
39     GAsyncResult *result,
40     gpointer user_data)
41 {
42   GError *error = NULL;
43
44   if (!tp_account_channel_request_ensure_channel_finish (
45         TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error))
46     {
47       DEBUG ("Failed to ensure text channel: %s", error->message);
48       g_error_free (error);
49     }
50 }
51
52 static void
53 create_text_channel (TpAccount *account,
54     TpHandleType target_handle_type,
55     const gchar *target_id,
56     gboolean sms_channel,
57     gint64 timestamp,
58     GAsyncReadyCallback callback,
59     gpointer user_data)
60 {
61   GHashTable *request;
62   TpAccountChannelRequest *req;
63
64   request = tp_asv_new (
65       TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
66         TP_IFACE_CHANNEL_TYPE_TEXT,
67       TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, target_handle_type,
68       TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING, target_id,
69       NULL);
70
71   if (sms_channel)
72     tp_asv_set_boolean (request,
73         TP_PROP_CHANNEL_INTERFACE_SMS_SMS_CHANNEL, TRUE);
74
75   req = tp_account_channel_request_new (account, request, timestamp);
76   tp_account_channel_request_set_delegate_to_preferred_handler (req, TRUE);
77
78   tp_account_channel_request_ensure_channel_async (req, EMPATHY_CHAT_BUS_NAME,
79       NULL, callback ? callback : ensure_text_channel_cb, user_data);
80
81   g_hash_table_unref (request);
82   g_object_unref (req);
83 }
84
85 /* @callback is optional, but if it's provided, it should call the right
86  * _finish() func that we call in ensure_text_channel_cb() */
87 void
88 empathy_chat_with_contact_id (TpAccount *account,
89     const gchar *contact_id,
90     gint64 timestamp,
91     GAsyncReadyCallback callback,
92     gpointer user_data)
93 {
94   create_text_channel (account, TP_HANDLE_TYPE_CONTACT,
95       contact_id, FALSE, timestamp, callback, user_data);
96 }
97
98 void
99 empathy_join_muc (TpAccount *account,
100     const gchar *room_name,
101     gint64 timestamp)
102 {
103   create_text_channel (account, TP_HANDLE_TYPE_ROOM,
104       room_name, FALSE, timestamp, NULL, NULL);
105 }
106
107 /* @callback is optional, but if it's provided, it should call the right
108  * _finish() func that we call in ensure_text_channel_cb() */
109 void
110 empathy_sms_contact_id (TpAccount *account,
111     const gchar *contact_id,
112     gint64 timestamp,
113     GAsyncReadyCallback callback,
114     gpointer user_data)
115 {
116   create_text_channel (account, TP_HANDLE_TYPE_CONTACT,
117       contact_id, TRUE, timestamp, callback, user_data);
118 }