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