2 * Copyright (C) 2007-2008 Collabora Ltd.
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.
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.
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
18 * Authors: Xavier Claessens <xclaesse@gmail.com>
23 #include <glib/gi18n-lib.h>
25 #include <libempathy/empathy-request-util.h>
27 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
28 #include <libempathy/empathy-debug.h>
30 #include <libempathy-gtk/empathy-contact-chooser.h>
31 #include <libempathy-gtk/empathy-ui-utils.h>
32 #include <libempathy-gtk/empathy-images.h>
34 #include "empathy-new-message-dialog.h"
36 static EmpathyNewMessageDialog *dialog_singleton = NULL;
38 G_DEFINE_TYPE(EmpathyNewMessageDialog, empathy_new_message_dialog,
41 struct _EmpathyNewMessageDialogPriv {
43 GtkWidget *button_chat;
44 GtkWidget *button_sms;
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
53 * #EmpathyNewMessageDialog is a dialog which allows a text chat
54 * to be started with any contact on any enabled account.
64 get_error_display_message (GError *error)
66 if (error->domain != TP_ERROR)
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 "
82 case TP_ERROR_INVALID_ARGUMENT:
83 /* Not very user friendly to say 'invalid arguments' */
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");
98 DEBUG ("Unhandled error code: %d", error->code);
102 return _("There was an error starting the conversation");
106 show_chat_error (GError *error,
111 dialog = gtk_message_dialog_new (parent, GTK_DIALOG_MODAL,
112 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
114 get_error_display_message (error));
116 g_signal_connect_swapped (dialog, "response",
117 G_CALLBACK (gtk_widget_destroy),
120 gtk_widget_show (dialog);
124 ensure_text_channel_cb (GObject *source,
125 GAsyncResult *result,
128 GError *error = NULL;
130 if (!tp_account_channel_request_ensure_channel_finish (
131 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error))
133 DEBUG ("Failed to ensure text channel: %s", error->message);
134 show_chat_error (error, user_data);
135 g_error_free (error);
140 empathy_new_message_dialog_response (GtkDialog *dialog,
143 EmpathyNewMessageDialog *self = (EmpathyNewMessageDialog *) dialog;
144 FolksIndividual *individual = NULL;
145 EmpathyContact *contact = NULL;
147 if (response_id < EMP_NEW_MESSAGE_TEXT)
150 individual = empathy_contact_chooser_dup_selected (
151 EMPATHY_CONTACT_CHOOSER (self->priv->chooser));
152 if (individual == NULL)
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);
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)));
169 case EMP_NEW_MESSAGE_SMS:
170 contact = empathy_contact_dup_best_for_action (individual,
172 g_return_if_fail (contact != NULL);
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)));
182 g_warn_if_reached ();
186 tp_clear_object (&individual);
187 tp_clear_object (&contact);
188 gtk_widget_destroy (GTK_WIDGET (dialog));
192 empathy_new_message_dialog_constructor (GType type,
194 GObjectConstructParam *props)
198 if (dialog_singleton)
200 retval = G_OBJECT (dialog_singleton);
201 g_object_ref (retval);
205 retval = G_OBJECT_CLASS (
206 empathy_new_message_dialog_parent_class)->constructor (type,
209 dialog_singleton = EMPATHY_NEW_MESSAGE_DIALOG (retval);
210 g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
217 individual_supports_action (FolksIndividual *individual,
218 EmpathyActionType action)
220 EmpathyContact *contact;
222 contact = empathy_contact_dup_best_for_action (individual, action);
226 g_object_unref (contact);
231 filter_individual (EmpathyContactChooser *chooser,
232 FolksIndividual *individual,
237 return individual_supports_action (individual, EMPATHY_ACTION_CHAT) ||
238 individual_supports_action (individual, EMPATHY_ACTION_SMS);
242 selection_changed_cb (GtkWidget *chooser,
243 FolksIndividual *selected,
244 EmpathyNewMessageDialog *self)
246 gboolean can_chat, can_sms;
248 if (selected == NULL)
250 can_chat = can_sms = FALSE;
254 can_chat = individual_supports_action (selected, EMPATHY_ACTION_CHAT);
255 can_sms = individual_supports_action (selected, EMPATHY_ACTION_SMS);
258 gtk_widget_set_sensitive (self->priv->button_chat, can_chat);
259 gtk_widget_set_sensitive (self->priv->button_sms, can_sms);
263 selection_activate_cb (GtkWidget *chooser,
264 EmpathyNewMessageDialog *self)
266 gtk_dialog_response (GTK_DIALOG (self), EMP_NEW_MESSAGE_TEXT);
270 empathy_new_message_dialog_init (EmpathyNewMessageDialog *self)
276 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
277 EMPATHY_TYPE_NEW_MESSAGE_DIALOG, EmpathyNewMessageDialogPriv);
279 content = gtk_dialog_get_content_area (GTK_DIALOG (self));
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);
285 /* contact chooser */
286 self->priv->chooser = empathy_contact_chooser_new ();
288 empathy_contact_chooser_set_filter_func (
289 EMPATHY_CONTACT_CHOOSER (self->priv->chooser), filter_individual, self);
291 gtk_box_pack_start (GTK_BOX (content), self->priv->chooser, TRUE, TRUE, 6);
292 gtk_widget_show (self->priv->chooser);
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);
300 gtk_dialog_add_button (GTK_DIALOG (self),
301 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
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);
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);
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);
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);
323 /* Tweak the dialog */
324 gtk_window_set_title (GTK_WINDOW (self), _("New Conversation"));
325 gtk_window_set_role (GTK_WINDOW (self), "new_message");
327 /* Set a default height so a few contacts are displayed */
328 gtk_window_set_default_size (GTK_WINDOW (self), -1, 400);
330 gtk_widget_set_sensitive (self->priv->button_chat, FALSE);
331 gtk_widget_set_sensitive (self->priv->button_sms, FALSE);
335 empathy_new_message_dialog_class_init (
336 EmpathyNewMessageDialogClass *class)
338 GObjectClass *object_class = G_OBJECT_CLASS (class);
339 GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (class);
341 object_class->constructor = empathy_new_message_dialog_constructor;
343 dialog_class->response = empathy_new_message_dialog_response;
345 g_type_class_add_private (class, sizeof (EmpathyNewMessageDialogPriv));
349 * empathy_new_message_dialog_new:
350 * @parent: parent #GtkWindow of the dialog
352 * Create a new #EmpathyNewMessageDialog it.
354 * Return value: the new #EmpathyNewMessageDialog
357 empathy_new_message_dialog_show (GtkWindow *parent)
361 dialog = g_object_new (EMPATHY_TYPE_NEW_MESSAGE_DIALOG, NULL);
365 gtk_window_set_transient_for (GTK_WINDOW (dialog),
366 GTK_WINDOW (parent));
369 gtk_widget_show (dialog);