]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-new-message-dialog.c
Merge branch 'balance-rebase'
[empathy.git] / libempathy-gtk / empathy-new-message-dialog.c
1 /*
2  * Copyright (C) 2007-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  */
20
21 #include <config.h>
22
23 #include <string.h>
24 #include <stdlib.h>
25
26 #include <gtk/gtk.h>
27 #include <glib/gi18n-lib.h>
28
29 #include <telepathy-glib/interfaces.h>
30
31 #include <libempathy/empathy-tp-contact-factory.h>
32 #include <libempathy/empathy-contact-manager.h>
33 #include <libempathy/empathy-request-util.h>
34 #include <libempathy/empathy-utils.h>
35
36 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
37 #include <libempathy/empathy-debug.h>
38
39 #include <libempathy-gtk/empathy-ui-utils.h>
40 #include <libempathy-gtk/empathy-images.h>
41
42 #include "empathy-new-message-dialog.h"
43 #include "empathy-account-chooser.h"
44
45 typedef struct {
46   EmpathyAccountChooserFilterResultCallback callback;
47   gpointer                                  user_data;
48 } FilterCallbackData;
49
50 static EmpathyNewMessageDialog *dialog_singleton = NULL;
51
52 G_DEFINE_TYPE(EmpathyNewMessageDialog, empathy_new_message_dialog,
53                EMPATHY_TYPE_CONTACT_SELECTOR_DIALOG)
54
55 /**
56  * SECTION:empathy-new-message-dialog
57  * @title: EmpathyNewMessageDialog
58  * @short_description: A dialog to show a new message
59  * @include: libempathy-gtk/empathy-new-message-dialog.h
60  *
61  * #EmpathyNewMessageDialog is a dialog which allows a text chat
62  * to be started with any contact on any enabled account.
63  */
64
65 enum
66 {
67   EMP_NEW_MESSAGE_TEXT,
68   EMP_NEW_MESSAGE_SMS,
69 };
70
71 static void
72 empathy_new_message_dialog_response (GtkDialog *dialog, int response_id)
73 {
74   TpAccount *account;
75   const gchar *contact_id;
76
77   if (response_id < EMP_NEW_MESSAGE_TEXT) goto out;
78
79   contact_id = empathy_contact_selector_dialog_get_selected (
80       EMPATHY_CONTACT_SELECTOR_DIALOG (dialog), NULL, &account);
81
82   if (EMP_STR_EMPTY (contact_id) || account == NULL) goto out;
83
84   switch (response_id)
85     {
86       case EMP_NEW_MESSAGE_TEXT:
87         empathy_chat_with_contact_id (account, contact_id,
88             gtk_get_current_event_time ());
89         break;
90
91       case EMP_NEW_MESSAGE_SMS:
92         empathy_sms_contact_id (account, contact_id,
93             gtk_get_current_event_time ());
94         break;
95
96       default:
97         g_warn_if_reached ();
98     }
99
100 out:
101   gtk_widget_destroy (GTK_WIDGET (dialog));
102 }
103
104 static void
105 conn_prepared_cb (GObject *conn,
106     GAsyncResult *result,
107     gpointer user_data)
108 {
109   FilterCallbackData *data = user_data;
110   GError *myerr = NULL;
111   TpCapabilities *caps;
112
113   if (!tp_proxy_prepare_finish (conn, result, &myerr))
114     {
115       data->callback (FALSE, data->user_data);
116       g_slice_free (FilterCallbackData, data);
117     }
118
119   caps = tp_connection_get_capabilities (TP_CONNECTION (conn));
120   data->callback (tp_capabilities_supports_text_chats (caps),
121       data->user_data);
122
123   g_slice_free (FilterCallbackData, data);
124 }
125
126 static void
127 empathy_new_message_account_filter (EmpathyContactSelectorDialog *dialog,
128     EmpathyAccountChooserFilterResultCallback callback,
129     gpointer callback_data,
130     TpAccount *account)
131 {
132   TpConnection *connection;
133   FilterCallbackData *cb_data;
134   GQuark features[] = { TP_CONNECTION_FEATURE_CAPABILITIES, 0 };
135
136   if (tp_account_get_connection_status (account, NULL) !=
137       TP_CONNECTION_STATUS_CONNECTED)
138     {
139       callback (FALSE, callback_data);
140       return;
141     }
142
143   /* check if CM supports 1-1 text chat */
144   connection = tp_account_get_connection (account);
145   if (connection == NULL)
146     {
147       callback (FALSE, callback_data);
148       return;
149     }
150
151   cb_data = g_slice_new0 (FilterCallbackData);
152   cb_data->callback = callback;
153   cb_data->user_data = callback_data;
154   tp_proxy_prepare_async (connection, features, conn_prepared_cb, cb_data);
155 }
156
157 static void
158 empathy_new_message_dialog_update_sms_button_sensitivity (GtkWidget *widget,
159     GParamSpec *pspec,
160     GtkWidget *button)
161 {
162   GtkWidget *self = gtk_widget_get_toplevel (widget);
163   EmpathyContactSelectorDialog *dialog;
164   TpConnection *conn;
165   GPtrArray *rccs;
166   gboolean sensitive = FALSE;
167   guint i;
168
169   g_return_if_fail (EMPATHY_IS_NEW_MESSAGE_DIALOG (self));
170
171   dialog = EMPATHY_CONTACT_SELECTOR_DIALOG (self);
172
173   /* if the Text widget isn't sensitive, don't bother checking the caps */
174   if (!gtk_widget_get_sensitive (dialog->button_action))
175     goto finally;
176
177   empathy_contact_selector_dialog_get_selected (dialog, &conn, NULL);
178
179   if (conn == NULL)
180     goto finally;
181
182   /* iterate the rccs to find if SMS channels are supported, this should
183    * be in tp-glib */
184   rccs = tp_capabilities_get_channel_classes (
185       tp_connection_get_capabilities (conn));
186
187   for (i = 0; i < rccs->len; i++)
188     {
189       GHashTable *fixed;
190       GStrv allowed;
191       const char *type;
192       gboolean sms_channel;
193
194       tp_value_array_unpack (g_ptr_array_index (rccs, i), 2,
195           &fixed,
196           &allowed);
197
198       /* SMS channels are type:Text and sms-channel:True */
199       type = tp_asv_get_string (fixed, TP_PROP_CHANNEL_CHANNEL_TYPE);
200       sms_channel = tp_asv_get_boolean (fixed,
201           TP_PROP_CHANNEL_INTERFACE_SMS_SMS_CHANNEL, NULL);
202
203       sensitive = sms_channel &&
204         !tp_strdiff (type, TP_IFACE_CHANNEL_TYPE_TEXT);
205
206       if (sensitive)
207         break;
208     }
209
210 finally:
211   gtk_widget_set_sensitive (button, sensitive);
212 }
213
214 static GObject *
215 empathy_new_message_dialog_constructor (GType type,
216     guint n_props,
217     GObjectConstructParam *props)
218 {
219   GObject *retval;
220
221   if (dialog_singleton)
222     {
223       retval = G_OBJECT (dialog_singleton);
224       g_object_ref (retval);
225     }
226   else
227     {
228       retval = G_OBJECT_CLASS (
229       empathy_new_message_dialog_parent_class)->constructor (type,
230         n_props, props);
231
232       dialog_singleton = EMPATHY_NEW_MESSAGE_DIALOG (retval);
233       g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
234     }
235
236   return retval;
237 }
238
239 static void
240 empathy_new_message_dialog_init (EmpathyNewMessageDialog *dialog)
241 {
242   EmpathyContactSelectorDialog *parent = EMPATHY_CONTACT_SELECTOR_DIALOG (
243         dialog);
244   GtkWidget *button;
245   GtkWidget *image;
246
247   /* add an SMS button */
248   button = gtk_button_new_with_mnemonic (_("_SMS"));
249   image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_SMS,
250       GTK_ICON_SIZE_BUTTON);
251   gtk_button_set_image (GTK_BUTTON (button), image);
252
253   gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button,
254       EMP_NEW_MESSAGE_SMS);
255   gtk_widget_show (button);
256
257   /* add chat button */
258   parent->button_action = gtk_button_new_with_mnemonic (_("C_hat"));
259   image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_NEW_MESSAGE,
260       GTK_ICON_SIZE_BUTTON);
261   gtk_button_set_image (GTK_BUTTON (parent->button_action), image);
262
263   gtk_dialog_add_action_widget (GTK_DIALOG (dialog), parent->button_action,
264       EMP_NEW_MESSAGE_TEXT);
265   gtk_widget_show (parent->button_action);
266
267   /* the parent class will update the sensitivity of button_action, propagate
268    * it */
269   g_signal_connect (parent->button_action, "notify::sensitive",
270       G_CALLBACK (empathy_new_message_dialog_update_sms_button_sensitivity),
271       button);
272   g_signal_connect (dialog, "notify::selected-account",
273       G_CALLBACK (empathy_new_message_dialog_update_sms_button_sensitivity),
274       button);
275
276   /* Tweak the dialog */
277   gtk_window_set_title (GTK_WINDOW (dialog), _("New Conversation"));
278   gtk_window_set_role (GTK_WINDOW (dialog), "new_message");
279
280   gtk_widget_set_sensitive (parent->button_action, FALSE);
281 }
282
283 static void
284 empathy_new_message_dialog_class_init (
285   EmpathyNewMessageDialogClass *class)
286 {
287   GObjectClass *object_class = G_OBJECT_CLASS (class);
288   GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (class);
289   EmpathyContactSelectorDialogClass *selector_dialog_class = \
290     EMPATHY_CONTACT_SELECTOR_DIALOG_CLASS (class);
291
292   object_class->constructor = empathy_new_message_dialog_constructor;
293
294   dialog_class->response = empathy_new_message_dialog_response;
295
296   selector_dialog_class->account_filter = empathy_new_message_account_filter;
297 }
298
299 /**
300  * empathy_new_message_dialog_new:
301  * @parent: parent #GtkWindow of the dialog
302  *
303  * Create a new #EmpathyNewMessageDialog it.
304  *
305  * Return value: the new #EmpathyNewMessageDialog
306  */
307 GtkWidget *
308 empathy_new_message_dialog_show (GtkWindow *parent)
309 {
310   GtkWidget *dialog;
311
312   dialog = g_object_new (EMPATHY_TYPE_NEW_MESSAGE_DIALOG, NULL);
313
314   if (parent)
315     {
316       gtk_window_set_transient_for (GTK_WINDOW (dialog),
317           GTK_WINDOW (parent));
318     }
319
320   gtk_widget_show (dialog);
321   return dialog;
322 }