]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-new-message-dialog.c
NewMessageDialog: show an error if starting the channel fails
[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 static EmpathyNewMessageDialog *dialog_singleton = NULL;
46
47 G_DEFINE_TYPE(EmpathyNewMessageDialog, empathy_new_message_dialog,
48                EMPATHY_TYPE_CONTACT_SELECTOR_DIALOG)
49
50 /**
51  * SECTION:empathy-new-message-dialog
52  * @title: EmpathyNewMessageDialog
53  * @short_description: A dialog to show a new message
54  * @include: libempathy-gtk/empathy-new-message-dialog.h
55  *
56  * #EmpathyNewMessageDialog is a dialog which allows a text chat
57  * to be started with any contact on any enabled account.
58  */
59
60 enum
61 {
62   EMP_NEW_MESSAGE_TEXT,
63   EMP_NEW_MESSAGE_SMS,
64 };
65
66 static const gchar *
67 get_error_display_message (GError *error)
68 {
69   if (error->domain != TP_ERROR)
70     goto out;
71
72   switch (error->code)
73     {
74       case TP_ERROR_NETWORK_ERROR:
75         return _("Network error");
76       case TP_ERROR_OFFLINE:
77         return _("The contact is offline");
78       case TP_ERROR_INVALID_HANDLE:
79         return _("The specified contact is either invalid or unknown");
80       case TP_ERROR_NOT_CAPABLE:
81         return _("The contact does not support this kind of conversation");
82       case TP_ERROR_NOT_IMPLEMENTED:
83         return _("The requested functionality is not implemented "
84                  "for this protocol");
85       case TP_ERROR_INVALID_ARGUMENT:
86         /* Not very user friendly to say 'invalid arguments' */
87         break;
88       case TP_ERROR_NOT_AVAILABLE:
89         return _("Could not start a conversation with the given contact");
90       case TP_ERROR_CHANNEL_BANNED:
91         return _("You are banned from this channel");
92       case TP_ERROR_CHANNEL_FULL:
93         return _("This channel is full");
94       case TP_ERROR_CHANNEL_INVITE_ONLY:
95         return _("You must be invited to join this channel");
96       case TP_ERROR_DISCONNECTED:
97         return _("Can't proceed while disconnected");
98       case TP_ERROR_PERMISSION_DENIED:
99         return _("Permission denied");
100       default:
101         DEBUG ("Unhandled error code: %d", error->code);
102     }
103
104 out:
105   return _("There was an error starting the conversation");
106 }
107
108 static void
109 show_chat_error (GError *error)
110 {
111   GtkWidget *dialog;
112
113   dialog = gtk_message_dialog_new (NULL, 0,
114       GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
115       "%s",
116       get_error_display_message (error));
117
118   g_signal_connect_swapped (dialog, "response",
119       G_CALLBACK (gtk_widget_destroy),
120       dialog);
121
122   gtk_widget_show (dialog);
123 }
124
125 static void
126 ensure_text_channel_cb (GObject *source,
127     GAsyncResult *result,
128     gpointer user_data)
129 {
130   GError *error = NULL;
131
132   if (!tp_account_channel_request_ensure_channel_finish (
133         TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error))
134     {
135       DEBUG ("Failed to ensure text channel: %s", error->message);
136       show_chat_error (error);
137       g_error_free (error);
138     }
139 }
140
141 static void
142 empathy_new_message_dialog_response (GtkDialog *dialog, int response_id)
143 {
144   TpAccount *account;
145   const gchar *contact_id;
146
147   if (response_id < EMP_NEW_MESSAGE_TEXT) goto out;
148
149   contact_id = empathy_contact_selector_dialog_get_selected (
150       EMPATHY_CONTACT_SELECTOR_DIALOG (dialog), NULL, &account);
151
152   if (EMP_STR_EMPTY (contact_id) || account == NULL) goto out;
153
154   switch (response_id)
155     {
156       case EMP_NEW_MESSAGE_TEXT:
157         empathy_chat_with_contact_id (account, contact_id,
158             empathy_get_current_action_time (),
159             ensure_text_channel_cb, NULL);
160         break;
161
162       case EMP_NEW_MESSAGE_SMS:
163         empathy_sms_contact_id (account, contact_id,
164             empathy_get_current_action_time (),
165             ensure_text_channel_cb, NULL);
166         break;
167
168       default:
169         g_warn_if_reached ();
170     }
171
172 out:
173   gtk_widget_destroy (GTK_WIDGET (dialog));
174 }
175
176 static void
177 empathy_new_message_account_filter (EmpathyContactSelectorDialog *dialog,
178     EmpathyAccountChooserFilterResultCallback callback,
179     gpointer callback_data,
180     TpAccount *account)
181 {
182   TpConnection *connection;
183   gboolean supported = FALSE;
184   TpCapabilities *caps;
185
186   /* check if CM supports 1-1 text chat */
187   connection = tp_account_get_connection (account);
188   if (connection == NULL)
189       goto out;
190
191   caps = tp_connection_get_capabilities (connection);
192   if (caps == NULL)
193       goto out;
194
195   supported = tp_capabilities_supports_text_chats (caps);
196
197 out:
198   callback (supported, callback_data);
199 }
200
201 static void
202 empathy_new_message_dialog_update_sms_button_sensitivity (GtkWidget *widget,
203     GParamSpec *pspec,
204     GtkWidget *button)
205 {
206   GtkWidget *self = gtk_widget_get_toplevel (widget);
207   EmpathyContactSelectorDialog *dialog;
208   TpConnection *conn;
209   GPtrArray *rccs;
210   gboolean sensitive = FALSE;
211   guint i;
212
213   g_return_if_fail (EMPATHY_IS_NEW_MESSAGE_DIALOG (self));
214
215   dialog = EMPATHY_CONTACT_SELECTOR_DIALOG (self);
216
217   /* if the Text widget isn't sensitive, don't bother checking the caps */
218   if (!gtk_widget_get_sensitive (dialog->button_action))
219     goto finally;
220
221   empathy_contact_selector_dialog_get_selected (dialog, &conn, NULL);
222
223   if (conn == NULL)
224     goto finally;
225
226   /* iterate the rccs to find if SMS channels are supported, this should
227    * be in tp-glib */
228   rccs = tp_capabilities_get_channel_classes (
229       tp_connection_get_capabilities (conn));
230
231   for (i = 0; i < rccs->len; i++)
232     {
233       GHashTable *fixed;
234       GStrv allowed;
235       const char *type;
236       gboolean sms_channel;
237
238       tp_value_array_unpack (g_ptr_array_index (rccs, i), 2,
239           &fixed,
240           &allowed);
241
242       /* SMS channels are type:Text and sms-channel:True */
243       type = tp_asv_get_string (fixed, TP_PROP_CHANNEL_CHANNEL_TYPE);
244       sms_channel = tp_asv_get_boolean (fixed,
245           TP_PROP_CHANNEL_INTERFACE_SMS_SMS_CHANNEL, NULL);
246
247       sensitive = sms_channel &&
248         !tp_strdiff (type, TP_IFACE_CHANNEL_TYPE_TEXT);
249
250       if (sensitive)
251         break;
252     }
253
254 finally:
255   gtk_widget_set_sensitive (button, sensitive);
256 }
257
258 static GObject *
259 empathy_new_message_dialog_constructor (GType type,
260     guint n_props,
261     GObjectConstructParam *props)
262 {
263   GObject *retval;
264
265   if (dialog_singleton)
266     {
267       retval = G_OBJECT (dialog_singleton);
268       g_object_ref (retval);
269     }
270   else
271     {
272       retval = G_OBJECT_CLASS (
273       empathy_new_message_dialog_parent_class)->constructor (type,
274         n_props, props);
275
276       dialog_singleton = EMPATHY_NEW_MESSAGE_DIALOG (retval);
277       g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
278     }
279
280   return retval;
281 }
282
283 static void
284 empathy_new_message_dialog_init (EmpathyNewMessageDialog *dialog)
285 {
286   EmpathyContactSelectorDialog *parent = EMPATHY_CONTACT_SELECTOR_DIALOG (
287         dialog);
288   GtkWidget *button;
289   GtkWidget *image;
290
291   /* add an SMS button */
292   button = gtk_button_new_with_mnemonic (_("_SMS"));
293   image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_SMS,
294       GTK_ICON_SIZE_BUTTON);
295   gtk_button_set_image (GTK_BUTTON (button), image);
296
297   gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button,
298       EMP_NEW_MESSAGE_SMS);
299   gtk_widget_show (button);
300
301   /* add chat button */
302   parent->button_action = gtk_button_new_with_mnemonic (_("C_hat"));
303   image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_NEW_MESSAGE,
304       GTK_ICON_SIZE_BUTTON);
305   gtk_button_set_image (GTK_BUTTON (parent->button_action), image);
306
307   gtk_dialog_add_action_widget (GTK_DIALOG (dialog), parent->button_action,
308       EMP_NEW_MESSAGE_TEXT);
309   gtk_widget_show (parent->button_action);
310
311   /* the parent class will update the sensitivity of button_action, propagate
312    * it */
313   g_signal_connect (parent->button_action, "notify::sensitive",
314       G_CALLBACK (empathy_new_message_dialog_update_sms_button_sensitivity),
315       button);
316   g_signal_connect (dialog, "notify::selected-account",
317       G_CALLBACK (empathy_new_message_dialog_update_sms_button_sensitivity),
318       button);
319
320   /* Tweak the dialog */
321   gtk_window_set_title (GTK_WINDOW (dialog), _("New Conversation"));
322   gtk_window_set_role (GTK_WINDOW (dialog), "new_message");
323
324   gtk_widget_set_sensitive (parent->button_action, FALSE);
325 }
326
327 static void
328 empathy_new_message_dialog_class_init (
329   EmpathyNewMessageDialogClass *class)
330 {
331   GObjectClass *object_class = G_OBJECT_CLASS (class);
332   GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (class);
333   EmpathyContactSelectorDialogClass *selector_dialog_class = \
334     EMPATHY_CONTACT_SELECTOR_DIALOG_CLASS (class);
335
336   object_class->constructor = empathy_new_message_dialog_constructor;
337
338   dialog_class->response = empathy_new_message_dialog_response;
339
340   selector_dialog_class->account_filter = empathy_new_message_account_filter;
341 }
342
343 /**
344  * empathy_new_message_dialog_new:
345  * @parent: parent #GtkWindow of the dialog
346  *
347  * Create a new #EmpathyNewMessageDialog it.
348  *
349  * Return value: the new #EmpathyNewMessageDialog
350  */
351 GtkWidget *
352 empathy_new_message_dialog_show (GtkWindow *parent)
353 {
354   GtkWidget *dialog;
355
356   dialog = g_object_new (EMPATHY_TYPE_NEW_MESSAGE_DIALOG, NULL);
357
358   if (parent)
359     {
360       gtk_window_set_transient_for (GTK_WINDOW (dialog),
361           GTK_WINDOW (parent));
362     }
363
364   gtk_widget_show (dialog);
365   return dialog;
366 }