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>
22 #include "empathy-new-message-dialog.h"
24 #include <glib/gi18n-lib.h>
26 #include "empathy-request-util.h"
27 #include "empathy-contact-chooser.h"
28 #include "empathy-ui-utils.h"
29 #include "empathy-images.h"
31 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
32 #include "empathy-debug.h"
34 static EmpathyNewMessageDialog
*dialog_singleton
= NULL
;
36 G_DEFINE_TYPE(EmpathyNewMessageDialog
, empathy_new_message_dialog
,
39 struct _EmpathyNewMessageDialogPriv
{
41 GtkWidget
*button_chat
;
42 GtkWidget
*button_sms
;
46 * SECTION:empathy-new-message-dialog
47 * @title: EmpathyNewMessageDialog
48 * @short_description: A dialog to show a new message
49 * @include: libempathy-gtk/empathy-new-message-dialog.h
51 * #EmpathyNewMessageDialog is a dialog which allows a text chat
52 * to be started with any contact on any enabled account.
62 get_error_display_message (GError
*error
)
64 if (error
->domain
!= TP_ERROR
)
69 case TP_ERROR_NETWORK_ERROR
:
70 return _("Network error");
71 case TP_ERROR_OFFLINE
:
72 return _("The contact is offline");
73 case TP_ERROR_INVALID_HANDLE
:
74 return _("The specified contact is either invalid or unknown");
75 case TP_ERROR_NOT_CAPABLE
:
76 return _("The contact does not support this kind of conversation");
77 case TP_ERROR_NOT_IMPLEMENTED
:
78 return _("The requested functionality is not implemented "
80 case TP_ERROR_INVALID_ARGUMENT
:
81 /* Not very user friendly to say 'invalid arguments' */
83 case TP_ERROR_NOT_AVAILABLE
:
84 return _("Could not start a conversation with the given contact");
85 case TP_ERROR_CHANNEL_BANNED
:
86 return _("You are banned from this channel");
87 case TP_ERROR_CHANNEL_FULL
:
88 return _("This channel is full");
89 case TP_ERROR_CHANNEL_INVITE_ONLY
:
90 return _("You must be invited to join this channel");
91 case TP_ERROR_DISCONNECTED
:
92 return _("Can't proceed while disconnected");
93 case TP_ERROR_PERMISSION_DENIED
:
94 return _("Permission denied");
96 DEBUG ("Unhandled error code: %d", error
->code
);
100 return _("There was an error starting the conversation");
104 show_chat_error (GError
*error
,
109 dialog
= gtk_message_dialog_new (parent
, GTK_DIALOG_MODAL
,
110 GTK_MESSAGE_ERROR
, GTK_BUTTONS_CLOSE
,
112 get_error_display_message (error
));
114 g_signal_connect_swapped (dialog
, "response",
115 G_CALLBACK (gtk_widget_destroy
),
118 gtk_widget_show (dialog
);
122 ensure_text_channel_cb (GObject
*source
,
123 GAsyncResult
*result
,
126 GError
*error
= NULL
;
128 if (!tp_account_channel_request_ensure_channel_finish (
129 TP_ACCOUNT_CHANNEL_REQUEST (source
), result
, &error
))
131 DEBUG ("Failed to ensure text channel: %s", error
->message
);
132 show_chat_error (error
, user_data
);
133 g_error_free (error
);
138 empathy_new_message_dialog_response (GtkDialog
*dialog
,
141 EmpathyNewMessageDialog
*self
= (EmpathyNewMessageDialog
*) dialog
;
142 FolksIndividual
*individual
= NULL
;
143 EmpathyContact
*contact
= NULL
;
145 if (response_id
< EMP_NEW_MESSAGE_TEXT
)
148 individual
= empathy_contact_chooser_dup_selected (
149 EMPATHY_CONTACT_CHOOSER (self
->priv
->chooser
));
150 if (individual
== NULL
)
155 case EMP_NEW_MESSAGE_TEXT
:
156 contact
= empathy_contact_dup_best_for_action (individual
,
157 EMPATHY_ACTION_CHAT
);
158 g_return_if_fail (contact
!= NULL
);
160 empathy_chat_with_contact_id (empathy_contact_get_account (contact
),
161 empathy_contact_get_id (contact
),
162 empathy_get_current_action_time (),
163 ensure_text_channel_cb
,
164 gtk_widget_get_parent_window (GTK_WIDGET (dialog
)));
167 case EMP_NEW_MESSAGE_SMS
:
168 contact
= empathy_contact_dup_best_for_action (individual
,
170 g_return_if_fail (contact
!= NULL
);
172 empathy_sms_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
)));
180 g_warn_if_reached ();
184 tp_clear_object (&individual
);
185 tp_clear_object (&contact
);
186 gtk_widget_destroy (GTK_WIDGET (dialog
));
190 empathy_new_message_dialog_constructor (GType type
,
192 GObjectConstructParam
*props
)
196 if (dialog_singleton
)
198 retval
= G_OBJECT (dialog_singleton
);
199 g_object_ref (retval
);
203 retval
= G_OBJECT_CLASS (
204 empathy_new_message_dialog_parent_class
)->constructor (type
,
207 dialog_singleton
= EMPATHY_NEW_MESSAGE_DIALOG (retval
);
208 g_object_add_weak_pointer (retval
, (gpointer
) &dialog_singleton
);
215 individual_supports_action (FolksIndividual
*individual
,
216 EmpathyActionType action
)
218 EmpathyContact
*contact
;
220 contact
= empathy_contact_dup_best_for_action (individual
, action
);
224 g_object_unref (contact
);
229 filter_individual (EmpathyContactChooser
*chooser
,
230 FolksIndividual
*individual
,
235 return individual_supports_action (individual
, EMPATHY_ACTION_CHAT
) ||
236 individual_supports_action (individual
, EMPATHY_ACTION_SMS
);
240 selection_changed_cb (GtkWidget
*chooser
,
241 FolksIndividual
*selected
,
242 EmpathyNewMessageDialog
*self
)
244 gboolean can_chat
, can_sms
;
246 if (selected
== NULL
)
248 can_chat
= can_sms
= FALSE
;
252 can_chat
= individual_supports_action (selected
, EMPATHY_ACTION_CHAT
);
253 can_sms
= individual_supports_action (selected
, EMPATHY_ACTION_SMS
);
256 gtk_widget_set_sensitive (self
->priv
->button_chat
, can_chat
);
257 gtk_widget_set_sensitive (self
->priv
->button_sms
, can_sms
);
261 selection_activate_cb (GtkWidget
*chooser
,
262 EmpathyNewMessageDialog
*self
)
264 gtk_dialog_response (GTK_DIALOG (self
), EMP_NEW_MESSAGE_TEXT
);
268 empathy_new_message_dialog_init (EmpathyNewMessageDialog
*self
)
274 self
->priv
= G_TYPE_INSTANCE_GET_PRIVATE (self
,
275 EMPATHY_TYPE_NEW_MESSAGE_DIALOG
, EmpathyNewMessageDialogPriv
);
277 content
= gtk_dialog_get_content_area (GTK_DIALOG (self
));
279 label
= gtk_label_new (_("Enter a contact identifier or phone number:"));
280 gtk_box_pack_start (GTK_BOX (content
), label
, FALSE
, FALSE
, 0);
281 gtk_widget_show (label
);
283 /* contact chooser */
284 self
->priv
->chooser
= empathy_contact_chooser_new ();
286 empathy_contact_chooser_set_filter_func (
287 EMPATHY_CONTACT_CHOOSER (self
->priv
->chooser
), filter_individual
, self
);
289 gtk_box_pack_start (GTK_BOX (content
), self
->priv
->chooser
, TRUE
, TRUE
, 6);
290 gtk_widget_show (self
->priv
->chooser
);
292 g_signal_connect (self
->priv
->chooser
, "selection-changed",
293 G_CALLBACK (selection_changed_cb
), self
);
294 g_signal_connect (self
->priv
->chooser
, "activate",
295 G_CALLBACK (selection_activate_cb
), self
);
298 gtk_dialog_add_button (GTK_DIALOG (self
),
299 GTK_STOCK_CLOSE
, GTK_RESPONSE_CLOSE
);
302 self
->priv
->button_sms
= gtk_button_new_with_mnemonic (_("_SMS"));
303 image
= gtk_image_new_from_icon_name (EMPATHY_IMAGE_SMS
,
304 GTK_ICON_SIZE_BUTTON
);
305 gtk_button_set_image (GTK_BUTTON (self
->priv
->button_sms
), image
);
307 /* add chat button */
308 self
->priv
->button_chat
= gtk_button_new_with_mnemonic (_("_Chat"));
309 image
= gtk_image_new_from_icon_name (EMPATHY_IMAGE_NEW_MESSAGE
,
310 GTK_ICON_SIZE_BUTTON
);
311 gtk_button_set_image (GTK_BUTTON (self
->priv
->button_chat
), image
);
313 gtk_dialog_add_action_widget (GTK_DIALOG (self
), self
->priv
->button_sms
,
314 EMP_NEW_MESSAGE_SMS
);
315 gtk_widget_show (self
->priv
->button_sms
);
317 gtk_dialog_add_action_widget (GTK_DIALOG (self
), self
->priv
->button_chat
,
318 EMP_NEW_MESSAGE_TEXT
);
319 gtk_widget_show (self
->priv
->button_chat
);
321 /* Tweak the dialog */
322 gtk_window_set_title (GTK_WINDOW (self
), _("New Conversation"));
323 gtk_window_set_role (GTK_WINDOW (self
), "new_message");
325 /* Set a default height so a few contacts are displayed */
326 gtk_window_set_default_size (GTK_WINDOW (self
), -1, 400);
328 gtk_widget_set_sensitive (self
->priv
->button_chat
, FALSE
);
329 gtk_widget_set_sensitive (self
->priv
->button_sms
, FALSE
);
333 empathy_new_message_dialog_class_init (
334 EmpathyNewMessageDialogClass
*class)
336 GObjectClass
*object_class
= G_OBJECT_CLASS (class);
337 GtkDialogClass
*dialog_class
= GTK_DIALOG_CLASS (class);
339 object_class
->constructor
= empathy_new_message_dialog_constructor
;
341 dialog_class
->response
= empathy_new_message_dialog_response
;
343 g_type_class_add_private (class, sizeof (EmpathyNewMessageDialogPriv
));
347 * empathy_new_message_dialog_new:
348 * @parent: parent #GtkWindow of the dialog
350 * Create a new #EmpathyNewMessageDialog it.
352 * Return value: the new #EmpathyNewMessageDialog
355 empathy_new_message_dialog_show (GtkWindow
*parent
)
359 dialog
= g_object_new (EMPATHY_TYPE_NEW_MESSAGE_DIALOG
, NULL
);
363 gtk_window_set_transient_for (GTK_WINDOW (dialog
),
364 GTK_WINDOW (parent
));
367 gtk_widget_show (dialog
);