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>
27 #include <glib/gi18n-lib.h>
29 #include <telepathy-glib/interfaces.h>
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>
36 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
37 #include <libempathy/empathy-debug.h>
39 #include <libempathy-gtk/empathy-contact-chooser.h>
40 #include <libempathy-gtk/empathy-ui-utils.h>
41 #include <libempathy-gtk/empathy-images.h>
43 #include "empathy-new-message-dialog.h"
44 #include "empathy-account-chooser.h"
46 static EmpathyNewMessageDialog *dialog_singleton = NULL;
48 G_DEFINE_TYPE(EmpathyNewMessageDialog, empathy_new_message_dialog,
51 struct _EmpathyNewMessageDialogPriv {
53 GtkWidget *button_chat;
54 GtkWidget *button_sms;
58 * SECTION:empathy-new-message-dialog
59 * @title: EmpathyNewMessageDialog
60 * @short_description: A dialog to show a new message
61 * @include: libempathy-gtk/empathy-new-message-dialog.h
63 * #EmpathyNewMessageDialog is a dialog which allows a text chat
64 * to be started with any contact on any enabled account.
74 get_error_display_message (GError *error)
76 if (error->domain != TP_ERROR)
81 case TP_ERROR_NETWORK_ERROR:
82 return _("Network error");
83 case TP_ERROR_OFFLINE:
84 return _("The contact is offline");
85 case TP_ERROR_INVALID_HANDLE:
86 return _("The specified contact is either invalid or unknown");
87 case TP_ERROR_NOT_CAPABLE:
88 return _("The contact does not support this kind of conversation");
89 case TP_ERROR_NOT_IMPLEMENTED:
90 return _("The requested functionality is not implemented "
92 case TP_ERROR_INVALID_ARGUMENT:
93 /* Not very user friendly to say 'invalid arguments' */
95 case TP_ERROR_NOT_AVAILABLE:
96 return _("Could not start a conversation with the given contact");
97 case TP_ERROR_CHANNEL_BANNED:
98 return _("You are banned from this channel");
99 case TP_ERROR_CHANNEL_FULL:
100 return _("This channel is full");
101 case TP_ERROR_CHANNEL_INVITE_ONLY:
102 return _("You must be invited to join this channel");
103 case TP_ERROR_DISCONNECTED:
104 return _("Can't proceed while disconnected");
105 case TP_ERROR_PERMISSION_DENIED:
106 return _("Permission denied");
108 DEBUG ("Unhandled error code: %d", error->code);
112 return _("There was an error starting the conversation");
116 show_chat_error (GError *error,
121 dialog = gtk_message_dialog_new (parent, GTK_DIALOG_MODAL,
122 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
124 get_error_display_message (error));
126 g_signal_connect_swapped (dialog, "response",
127 G_CALLBACK (gtk_widget_destroy),
130 gtk_widget_show (dialog);
134 ensure_text_channel_cb (GObject *source,
135 GAsyncResult *result,
138 GError *error = NULL;
140 if (!tp_account_channel_request_ensure_channel_finish (
141 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error))
143 DEBUG ("Failed to ensure text channel: %s", error->message);
144 show_chat_error (error, user_data);
145 g_error_free (error);
150 empathy_new_message_dialog_response (GtkDialog *dialog,
153 EmpathyNewMessageDialog *self = (EmpathyNewMessageDialog *) dialog;
154 FolksIndividual *individual = NULL;
155 EmpathyContact *contact = NULL;
157 if (response_id < EMP_NEW_MESSAGE_TEXT)
160 individual = empathy_contact_chooser_dup_selected (
161 EMPATHY_CONTACT_CHOOSER (self->priv->chooser));
162 if (individual == NULL)
167 case EMP_NEW_MESSAGE_TEXT:
168 contact = empathy_contact_dup_best_for_action (individual,
169 EMPATHY_ACTION_CHAT);
170 g_return_if_fail (contact != NULL);
172 empathy_chat_with_contact_id (empathy_contact_get_account (contact),
173 empathy_contact_get_id (contact),
174 empathy_get_current_action_time (),
175 ensure_text_channel_cb,
176 gtk_widget_get_parent_window (GTK_WIDGET (dialog)));
179 case EMP_NEW_MESSAGE_SMS:
180 contact = empathy_contact_dup_best_for_action (individual,
182 g_return_if_fail (contact != NULL);
184 empathy_sms_contact_id (empathy_contact_get_account (contact),
185 empathy_contact_get_id (contact),
186 empathy_get_current_action_time (),
187 ensure_text_channel_cb,
188 gtk_widget_get_parent_window (GTK_WIDGET (dialog)));
192 g_warn_if_reached ();
196 tp_clear_object (&individual);
197 tp_clear_object (&contact);
198 gtk_widget_destroy (GTK_WIDGET (dialog));
202 empathy_new_message_dialog_constructor (GType type,
204 GObjectConstructParam *props)
208 if (dialog_singleton)
210 retval = G_OBJECT (dialog_singleton);
211 g_object_ref (retval);
215 retval = G_OBJECT_CLASS (
216 empathy_new_message_dialog_parent_class)->constructor (type,
219 dialog_singleton = EMPATHY_NEW_MESSAGE_DIALOG (retval);
220 g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
227 individual_supports_action (FolksIndividual *individual,
228 EmpathyActionType action)
230 EmpathyContact *contact;
232 contact = empathy_contact_dup_best_for_action (individual, action);
236 g_object_unref (contact);
241 filter_individual (EmpathyContactChooser *chooser,
242 FolksIndividual *individual,
247 return individual_supports_action (individual, EMPATHY_ACTION_CHAT) ||
248 individual_supports_action (individual, EMPATHY_ACTION_SMS);
252 selection_changed_cb (GtkWidget *chooser,
253 FolksIndividual *selected,
254 EmpathyNewMessageDialog *self)
256 gboolean can_chat, can_sms;
258 if (selected == NULL)
260 can_chat = can_sms = FALSE;
264 can_chat = individual_supports_action (selected, EMPATHY_ACTION_CHAT);
265 can_sms = individual_supports_action (selected, EMPATHY_ACTION_SMS);
268 gtk_widget_set_sensitive (self->priv->button_chat, can_chat);
269 gtk_widget_set_sensitive (self->priv->button_sms, can_sms);
273 selection_activate_cb (GtkWidget *chooser,
274 EmpathyNewMessageDialog *self)
276 gtk_dialog_response (GTK_DIALOG (self), EMP_NEW_MESSAGE_TEXT);
280 empathy_new_message_dialog_init (EmpathyNewMessageDialog *self)
286 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
287 EMPATHY_TYPE_NEW_MESSAGE_DIALOG, EmpathyNewMessageDialogPriv);
289 content = gtk_dialog_get_content_area (GTK_DIALOG (self));
291 label = gtk_label_new (_("Enter a contact identifier or phone number:"));
292 gtk_box_pack_start (GTK_BOX (content), label, FALSE, FALSE, 0);
293 gtk_widget_show (label);
295 /* contact chooser */
296 self->priv->chooser = empathy_contact_chooser_new ();
298 empathy_contact_chooser_set_filter_func (
299 EMPATHY_CONTACT_CHOOSER (self->priv->chooser), filter_individual, self);
301 gtk_box_pack_start (GTK_BOX (content), self->priv->chooser, TRUE, TRUE, 6);
302 gtk_widget_show (self->priv->chooser);
304 g_signal_connect (self->priv->chooser, "selection-changed",
305 G_CALLBACK (selection_changed_cb), self);
306 g_signal_connect (self->priv->chooser, "activate",
307 G_CALLBACK (selection_activate_cb), self);
310 gtk_dialog_add_button (GTK_DIALOG (self),
311 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
314 self->priv->button_sms = gtk_button_new_with_mnemonic (_("_SMS"));
315 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_SMS,
316 GTK_ICON_SIZE_BUTTON);
317 gtk_button_set_image (GTK_BUTTON (self->priv->button_sms), image);
319 /* add chat button */
320 self->priv->button_chat = gtk_button_new_with_mnemonic (_("_Chat"));
321 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_NEW_MESSAGE,
322 GTK_ICON_SIZE_BUTTON);
323 gtk_button_set_image (GTK_BUTTON (self->priv->button_chat), image);
325 gtk_dialog_add_action_widget (GTK_DIALOG (self), self->priv->button_sms,
326 EMP_NEW_MESSAGE_SMS);
327 gtk_widget_show (self->priv->button_sms);
329 gtk_dialog_add_action_widget (GTK_DIALOG (self), self->priv->button_chat,
330 EMP_NEW_MESSAGE_TEXT);
331 gtk_widget_show (self->priv->button_chat);
333 /* Tweak the dialog */
334 gtk_window_set_title (GTK_WINDOW (self), _("New Conversation"));
335 gtk_window_set_role (GTK_WINDOW (self), "new_message");
337 /* Set a default height so a few contacts are displayed */
338 gtk_window_set_default_size (GTK_WINDOW (self), -1, 400);
340 gtk_widget_set_sensitive (self->priv->button_chat, FALSE);
341 gtk_widget_set_sensitive (self->priv->button_sms, FALSE);
345 empathy_new_message_dialog_class_init (
346 EmpathyNewMessageDialogClass *class)
348 GObjectClass *object_class = G_OBJECT_CLASS (class);
349 GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (class);
351 object_class->constructor = empathy_new_message_dialog_constructor;
353 dialog_class->response = empathy_new_message_dialog_response;
355 g_type_class_add_private (class, sizeof (EmpathyNewMessageDialogPriv));
359 * empathy_new_message_dialog_new:
360 * @parent: parent #GtkWindow of the dialog
362 * Create a new #EmpathyNewMessageDialog it.
364 * Return value: the new #EmpathyNewMessageDialog
367 empathy_new_message_dialog_show (GtkWindow *parent)
371 dialog = g_object_new (EMPATHY_TYPE_NEW_MESSAGE_DIALOG, NULL);
375 gtk_window_set_transient_for (GTK_WINDOW (dialog),
376 GTK_WINDOW (parent));
379 gtk_widget_show (dialog);