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