]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-new-message-dialog.c
027ef872101074d02aa69ff8105db1cb4e835a26
[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     GtkWindow *parent)
111 {
112   GtkWidget *dialog;
113
114   dialog = gtk_message_dialog_new (parent, GTK_DIALOG_MODAL,
115       GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
116       "%s",
117       get_error_display_message (error));
118
119   g_signal_connect_swapped (dialog, "response",
120       G_CALLBACK (gtk_widget_destroy),
121       dialog);
122
123   gtk_widget_show (dialog);
124 }
125
126 static void
127 ensure_text_channel_cb (GObject *source,
128     GAsyncResult *result,
129     gpointer user_data)
130 {
131   GError *error = NULL;
132
133   if (!tp_account_channel_request_ensure_channel_finish (
134         TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error))
135     {
136       DEBUG ("Failed to ensure text channel: %s", error->message);
137       show_chat_error (error, user_data);
138       g_error_free (error);
139     }
140 }
141
142 static void
143 empathy_new_message_dialog_response (GtkDialog *dialog, int response_id)
144 {
145   TpAccount *account;
146   const gchar *contact_id;
147
148   if (response_id < EMP_NEW_MESSAGE_TEXT) goto out;
149
150   contact_id = empathy_contact_selector_dialog_get_selected (
151       EMPATHY_CONTACT_SELECTOR_DIALOG (dialog), NULL, &account);
152
153   if (EMP_STR_EMPTY (contact_id) || account == NULL) goto out;
154
155   switch (response_id)
156     {
157       case EMP_NEW_MESSAGE_TEXT:
158         empathy_chat_with_contact_id (account, contact_id,
159             empathy_get_current_action_time (),
160             ensure_text_channel_cb,
161             gtk_widget_get_parent_window (GTK_WIDGET (dialog)));
162         break;
163
164       case EMP_NEW_MESSAGE_SMS:
165         empathy_sms_contact_id (account, contact_id,
166             empathy_get_current_action_time (),
167             ensure_text_channel_cb,
168             gtk_widget_get_parent_window (GTK_WIDGET (dialog)));
169         break;
170
171       default:
172         g_warn_if_reached ();
173     }
174
175 out:
176   gtk_widget_destroy (GTK_WIDGET (dialog));
177 }
178
179 static void
180 empathy_new_message_account_filter (EmpathyContactSelectorDialog *dialog,
181     EmpathyAccountChooserFilterResultCallback callback,
182     gpointer callback_data,
183     TpAccount *account)
184 {
185   TpConnection *connection;
186   gboolean supported = FALSE;
187   TpCapabilities *caps;
188
189   /* check if CM supports 1-1 text chat */
190   connection = tp_account_get_connection (account);
191   if (connection == NULL)
192       goto out;
193
194   caps = tp_connection_get_capabilities (connection);
195   if (caps == NULL)
196       goto out;
197
198   supported = tp_capabilities_supports_text_chats (caps);
199
200 out:
201   callback (supported, callback_data);
202 }
203
204 static void
205 empathy_new_message_dialog_update_sms_button_sensitivity (GtkWidget *widget,
206     GParamSpec *pspec,
207     GtkWidget *button)
208 {
209   GtkWidget *self = gtk_widget_get_toplevel (widget);
210   EmpathyContactSelectorDialog *dialog;
211   TpConnection *conn;
212   GPtrArray *rccs;
213   gboolean sensitive = FALSE;
214   guint i;
215
216   g_return_if_fail (EMPATHY_IS_NEW_MESSAGE_DIALOG (self));
217
218   dialog = EMPATHY_CONTACT_SELECTOR_DIALOG (self);
219
220   /* if the Text widget isn't sensitive, don't bother checking the caps */
221   if (!gtk_widget_get_sensitive (dialog->button_action))
222     goto finally;
223
224   empathy_contact_selector_dialog_get_selected (dialog, &conn, NULL);
225
226   if (conn == NULL)
227     goto finally;
228
229   /* iterate the rccs to find if SMS channels are supported, this should
230    * be in tp-glib */
231   rccs = tp_capabilities_get_channel_classes (
232       tp_connection_get_capabilities (conn));
233
234   for (i = 0; i < rccs->len; i++)
235     {
236       GHashTable *fixed;
237       GStrv allowed;
238       const char *type;
239       gboolean sms_channel;
240
241       tp_value_array_unpack (g_ptr_array_index (rccs, i), 2,
242           &fixed,
243           &allowed);
244
245       /* SMS channels are type:Text and sms-channel:True */
246       type = tp_asv_get_string (fixed, TP_PROP_CHANNEL_CHANNEL_TYPE);
247       sms_channel = tp_asv_get_boolean (fixed,
248           TP_PROP_CHANNEL_INTERFACE_SMS_SMS_CHANNEL, NULL);
249
250       sensitive = sms_channel &&
251         !tp_strdiff (type, TP_IFACE_CHANNEL_TYPE_TEXT);
252
253       if (sensitive)
254         break;
255     }
256
257 finally:
258   gtk_widget_set_sensitive (button, sensitive);
259 }
260
261 static GObject *
262 empathy_new_message_dialog_constructor (GType type,
263     guint n_props,
264     GObjectConstructParam *props)
265 {
266   GObject *retval;
267
268   if (dialog_singleton)
269     {
270       retval = G_OBJECT (dialog_singleton);
271       g_object_ref (retval);
272     }
273   else
274     {
275       retval = G_OBJECT_CLASS (
276       empathy_new_message_dialog_parent_class)->constructor (type,
277         n_props, props);
278
279       dialog_singleton = EMPATHY_NEW_MESSAGE_DIALOG (retval);
280       g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
281     }
282
283   return retval;
284 }
285
286 static void
287 empathy_new_message_dialog_init (EmpathyNewMessageDialog *dialog)
288 {
289   EmpathyContactSelectorDialog *parent = EMPATHY_CONTACT_SELECTOR_DIALOG (
290         dialog);
291   GtkWidget *button;
292   GtkWidget *image;
293
294   /* add an SMS button */
295   button = gtk_button_new_with_mnemonic (_("_SMS"));
296   image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_SMS,
297       GTK_ICON_SIZE_BUTTON);
298   gtk_button_set_image (GTK_BUTTON (button), image);
299
300   gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button,
301       EMP_NEW_MESSAGE_SMS);
302   gtk_widget_show (button);
303
304   /* add chat button */
305   parent->button_action = gtk_button_new_with_mnemonic (_("C_hat"));
306   image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_NEW_MESSAGE,
307       GTK_ICON_SIZE_BUTTON);
308   gtk_button_set_image (GTK_BUTTON (parent->button_action), image);
309
310   gtk_dialog_add_action_widget (GTK_DIALOG (dialog), parent->button_action,
311       EMP_NEW_MESSAGE_TEXT);
312   gtk_widget_show (parent->button_action);
313
314   /* the parent class will update the sensitivity of button_action, propagate
315    * it */
316   g_signal_connect (parent->button_action, "notify::sensitive",
317       G_CALLBACK (empathy_new_message_dialog_update_sms_button_sensitivity),
318       button);
319   g_signal_connect (dialog, "notify::selected-account",
320       G_CALLBACK (empathy_new_message_dialog_update_sms_button_sensitivity),
321       button);
322
323   /* Tweak the dialog */
324   gtk_window_set_title (GTK_WINDOW (dialog), _("New Conversation"));
325   gtk_window_set_role (GTK_WINDOW (dialog), "new_message");
326
327   gtk_widget_set_sensitive (parent->button_action, FALSE);
328 }
329
330 static void
331 empathy_new_message_dialog_class_init (
332   EmpathyNewMessageDialogClass *class)
333 {
334   GObjectClass *object_class = G_OBJECT_CLASS (class);
335   GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (class);
336   EmpathyContactSelectorDialogClass *selector_dialog_class = \
337     EMPATHY_CONTACT_SELECTOR_DIALOG_CLASS (class);
338
339   object_class->constructor = empathy_new_message_dialog_constructor;
340
341   dialog_class->response = empathy_new_message_dialog_response;
342
343   selector_dialog_class->account_filter = empathy_new_message_account_filter;
344 }
345
346 /**
347  * empathy_new_message_dialog_new:
348  * @parent: parent #GtkWindow of the dialog
349  *
350  * Create a new #EmpathyNewMessageDialog it.
351  *
352  * Return value: the new #EmpathyNewMessageDialog
353  */
354 GtkWidget *
355 empathy_new_message_dialog_show (GtkWindow *parent)
356 {
357   GtkWidget *dialog;
358
359   dialog = g_object_new (EMPATHY_TYPE_NEW_MESSAGE_DIALOG, NULL);
360
361   if (parent)
362     {
363       gtk_window_set_transient_for (GTK_WINDOW (dialog),
364           GTK_WINDOW (parent));
365     }
366
367   gtk_widget_show (dialog);
368   return dialog;
369 }