]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-new-message-dialog.c
c38a1c75b7ec491ac4caf38ee72e30593cb97629
[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 <glib/gi18n-lib.h>
24
25 #include <libempathy/empathy-request-util.h>
26
27 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
28 #include <libempathy/empathy-debug.h>
29
30 #include <libempathy-gtk/empathy-contact-chooser.h>
31 #include <libempathy-gtk/empathy-ui-utils.h>
32 #include <libempathy-gtk/empathy-images.h>
33
34 #include "empathy-new-message-dialog.h"
35
36 static EmpathyNewMessageDialog *dialog_singleton = NULL;
37
38 G_DEFINE_TYPE(EmpathyNewMessageDialog, empathy_new_message_dialog,
39     GTK_TYPE_DIALOG)
40
41 struct _EmpathyNewMessageDialogPriv {
42   GtkWidget *chooser;
43   GtkWidget *button_chat;
44   GtkWidget *button_sms;
45 };
46
47 /**
48  * SECTION:empathy-new-message-dialog
49  * @title: EmpathyNewMessageDialog
50  * @short_description: A dialog to show a new message
51  * @include: libempathy-gtk/empathy-new-message-dialog.h
52  *
53  * #EmpathyNewMessageDialog is a dialog which allows a text chat
54  * to be started with any contact on any enabled account.
55  */
56
57 enum
58 {
59   EMP_NEW_MESSAGE_TEXT,
60   EMP_NEW_MESSAGE_SMS,
61 };
62
63 static const gchar *
64 get_error_display_message (GError *error)
65 {
66   if (error->domain != TP_ERROR)
67     goto out;
68
69   switch (error->code)
70     {
71       case TP_ERROR_NETWORK_ERROR:
72         return _("Network error");
73       case TP_ERROR_OFFLINE:
74         return _("The contact is offline");
75       case TP_ERROR_INVALID_HANDLE:
76         return _("The specified contact is either invalid or unknown");
77       case TP_ERROR_NOT_CAPABLE:
78         return _("The contact does not support this kind of conversation");
79       case TP_ERROR_NOT_IMPLEMENTED:
80         return _("The requested functionality is not implemented "
81                  "for this protocol");
82       case TP_ERROR_INVALID_ARGUMENT:
83         /* Not very user friendly to say 'invalid arguments' */
84         break;
85       case TP_ERROR_NOT_AVAILABLE:
86         return _("Could not start a conversation with the given contact");
87       case TP_ERROR_CHANNEL_BANNED:
88         return _("You are banned from this channel");
89       case TP_ERROR_CHANNEL_FULL:
90         return _("This channel is full");
91       case TP_ERROR_CHANNEL_INVITE_ONLY:
92         return _("You must be invited to join this channel");
93       case TP_ERROR_DISCONNECTED:
94         return _("Can't proceed while disconnected");
95       case TP_ERROR_PERMISSION_DENIED:
96         return _("Permission denied");
97       default:
98         DEBUG ("Unhandled error code: %d", error->code);
99     }
100
101 out:
102   return _("There was an error starting the conversation");
103 }
104
105 static void
106 show_chat_error (GError *error,
107     GtkWindow *parent)
108 {
109   GtkWidget *dialog;
110
111   dialog = gtk_message_dialog_new (parent, GTK_DIALOG_MODAL,
112       GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
113       "%s",
114       get_error_display_message (error));
115
116   g_signal_connect_swapped (dialog, "response",
117       G_CALLBACK (gtk_widget_destroy),
118       dialog);
119
120   gtk_widget_show (dialog);
121 }
122
123 static void
124 ensure_text_channel_cb (GObject *source,
125     GAsyncResult *result,
126     gpointer user_data)
127 {
128   GError *error = NULL;
129
130   if (!tp_account_channel_request_ensure_channel_finish (
131         TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error))
132     {
133       DEBUG ("Failed to ensure text channel: %s", error->message);
134       show_chat_error (error, user_data);
135       g_error_free (error);
136     }
137 }
138
139 static void
140 empathy_new_message_dialog_response (GtkDialog *dialog,
141     int response_id)
142 {
143   EmpathyNewMessageDialog *self = (EmpathyNewMessageDialog *) dialog;
144   FolksIndividual *individual = NULL;
145   EmpathyContact *contact = NULL;
146
147   if (response_id < EMP_NEW_MESSAGE_TEXT)
148     goto out;
149
150   individual = empathy_contact_chooser_dup_selected (
151       EMPATHY_CONTACT_CHOOSER (self->priv->chooser));
152   if (individual == NULL)
153     goto out;
154
155   switch (response_id)
156     {
157       case EMP_NEW_MESSAGE_TEXT:
158         contact = empathy_contact_dup_best_for_action (individual,
159             EMPATHY_ACTION_CHAT);
160         g_return_if_fail (contact != NULL);
161
162         empathy_chat_with_contact_id (empathy_contact_get_account (contact),
163             empathy_contact_get_id (contact),
164             empathy_get_current_action_time (),
165             ensure_text_channel_cb,
166             gtk_widget_get_parent_window (GTK_WIDGET (dialog)));
167         break;
168
169       case EMP_NEW_MESSAGE_SMS:
170         contact = empathy_contact_dup_best_for_action (individual,
171             EMPATHY_ACTION_SMS);
172         g_return_if_fail (contact != NULL);
173
174         empathy_sms_contact_id (empathy_contact_get_account (contact),
175             empathy_contact_get_id (contact),
176             empathy_get_current_action_time (),
177             ensure_text_channel_cb,
178             gtk_widget_get_parent_window (GTK_WIDGET (dialog)));
179         break;
180
181       default:
182         g_warn_if_reached ();
183     }
184
185 out:
186   tp_clear_object (&individual);
187   tp_clear_object (&contact);
188   gtk_widget_destroy (GTK_WIDGET (dialog));
189 }
190
191 static GObject *
192 empathy_new_message_dialog_constructor (GType type,
193     guint n_props,
194     GObjectConstructParam *props)
195 {
196   GObject *retval;
197
198   if (dialog_singleton)
199     {
200       retval = G_OBJECT (dialog_singleton);
201       g_object_ref (retval);
202     }
203   else
204     {
205       retval = G_OBJECT_CLASS (
206       empathy_new_message_dialog_parent_class)->constructor (type,
207         n_props, props);
208
209       dialog_singleton = EMPATHY_NEW_MESSAGE_DIALOG (retval);
210       g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
211     }
212
213   return retval;
214 }
215
216 static gboolean
217 individual_supports_action (FolksIndividual *individual,
218     EmpathyActionType action)
219 {
220   EmpathyContact *contact;
221
222   contact = empathy_contact_dup_best_for_action (individual, action);
223   if (contact == NULL)
224     return FALSE;
225
226   g_object_unref (contact);
227   return TRUE;
228 }
229
230 static gboolean
231 filter_individual (EmpathyContactChooser *chooser,
232     FolksIndividual *individual,
233     gboolean is_online,
234     gboolean searching,
235     gpointer user_data)
236 {
237   return individual_supports_action (individual, EMPATHY_ACTION_CHAT) ||
238     individual_supports_action (individual, EMPATHY_ACTION_SMS);
239 }
240
241 static void
242 selection_changed_cb (GtkWidget *chooser,
243     FolksIndividual *selected,
244     EmpathyNewMessageDialog *self)
245 {
246   gboolean can_chat, can_sms;
247
248   if (selected == NULL)
249     {
250       can_chat = can_sms = FALSE;
251     }
252   else
253     {
254       can_chat = individual_supports_action (selected, EMPATHY_ACTION_CHAT);
255       can_sms = individual_supports_action (selected, EMPATHY_ACTION_SMS);
256     }
257
258   gtk_widget_set_sensitive (self->priv->button_chat, can_chat);
259   gtk_widget_set_sensitive (self->priv->button_sms, can_sms);
260 }
261
262 static void
263 selection_activate_cb (GtkWidget *chooser,
264     EmpathyNewMessageDialog *self)
265 {
266   gtk_dialog_response (GTK_DIALOG (self), EMP_NEW_MESSAGE_TEXT);
267 }
268
269 static void
270 empathy_new_message_dialog_init (EmpathyNewMessageDialog *self)
271 {
272   GtkWidget *label;
273   GtkWidget *image;
274   GtkWidget *content;
275
276   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
277       EMPATHY_TYPE_NEW_MESSAGE_DIALOG, EmpathyNewMessageDialogPriv);
278
279   content = gtk_dialog_get_content_area (GTK_DIALOG (self));
280
281   label = gtk_label_new (_("Enter a contact identifier or phone number:"));
282   gtk_box_pack_start (GTK_BOX (content), label, FALSE, FALSE, 0);
283   gtk_widget_show (label);
284
285   /* contact chooser */
286   self->priv->chooser = empathy_contact_chooser_new ();
287
288   empathy_contact_chooser_set_filter_func (
289       EMPATHY_CONTACT_CHOOSER (self->priv->chooser), filter_individual, self);
290
291   gtk_box_pack_start (GTK_BOX (content), self->priv->chooser, TRUE, TRUE, 6);
292   gtk_widget_show (self->priv->chooser);
293
294   g_signal_connect (self->priv->chooser, "selection-changed",
295       G_CALLBACK (selection_changed_cb), self);
296   g_signal_connect (self->priv->chooser, "activate",
297       G_CALLBACK (selection_activate_cb), self);
298
299   /* close button */
300   gtk_dialog_add_button (GTK_DIALOG (self),
301       GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
302
303   /* add SMS button */
304   self->priv->button_sms = gtk_button_new_with_mnemonic (_("_SMS"));
305   image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_SMS,
306       GTK_ICON_SIZE_BUTTON);
307   gtk_button_set_image (GTK_BUTTON (self->priv->button_sms), image);
308
309   /* add chat button */
310   self->priv->button_chat = gtk_button_new_with_mnemonic (_("_Chat"));
311   image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_NEW_MESSAGE,
312       GTK_ICON_SIZE_BUTTON);
313   gtk_button_set_image (GTK_BUTTON (self->priv->button_chat), image);
314
315   gtk_dialog_add_action_widget (GTK_DIALOG (self), self->priv->button_sms,
316       EMP_NEW_MESSAGE_SMS);
317   gtk_widget_show (self->priv->button_sms);
318
319   gtk_dialog_add_action_widget (GTK_DIALOG (self), self->priv->button_chat,
320       EMP_NEW_MESSAGE_TEXT);
321   gtk_widget_show (self->priv->button_chat);
322
323   /* Tweak the dialog */
324   gtk_window_set_title (GTK_WINDOW (self), _("New Conversation"));
325   gtk_window_set_role (GTK_WINDOW (self), "new_message");
326
327   /* Set a default height so a few contacts are displayed */
328   gtk_window_set_default_size (GTK_WINDOW (self), -1, 400);
329
330   gtk_widget_set_sensitive (self->priv->button_chat, FALSE);
331   gtk_widget_set_sensitive (self->priv->button_sms, FALSE);
332 }
333
334 static void
335 empathy_new_message_dialog_class_init (
336   EmpathyNewMessageDialogClass *class)
337 {
338   GObjectClass *object_class = G_OBJECT_CLASS (class);
339   GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (class);
340
341   object_class->constructor = empathy_new_message_dialog_constructor;
342
343   dialog_class->response = empathy_new_message_dialog_response;
344
345   g_type_class_add_private (class, sizeof (EmpathyNewMessageDialogPriv));
346 }
347
348 /**
349  * empathy_new_message_dialog_new:
350  * @parent: parent #GtkWindow of the dialog
351  *
352  * Create a new #EmpathyNewMessageDialog it.
353  *
354  * Return value: the new #EmpathyNewMessageDialog
355  */
356 GtkWidget *
357 empathy_new_message_dialog_show (GtkWindow *parent)
358 {
359   GtkWidget *dialog;
360
361   dialog = g_object_new (EMPATHY_TYPE_NEW_MESSAGE_DIALOG, NULL);
362
363   if (parent)
364     {
365       gtk_window_set_transient_for (GTK_WINDOW (dialog),
366           GTK_WINDOW (parent));
367     }
368
369   gtk_widget_show (dialog);
370   return dialog;
371 }