]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-new-message-dialog.c
new-message-dialog: filter out CM which doesn't support 1-1 chat
[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-contact-manager.h>
33 #include <libempathy/empathy-dispatcher.h>
34 #include <libempathy/empathy-utils.h>
35
36 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
37 #include <libempathy/empathy-debug.h>
38
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                EMPATHY_TYPE_CONTACT_SELECTOR_DIALOG)
49
50 /**
51  * SECTION:empathy-new-message-dialog
52  * @title: EmpathyNewMessageDialog
53  * @short_description: A dialog to show a new message
54  * @include: libempathy-gtk/empathy-new-message-dialog.h
55  *
56  * #EmpathyNewMessageDialog is a dialog which allows a text chat
57  * to be started with any contact on any enabled account.
58  */
59
60 static void
61 empathy_new_message_dialog_got_response (EmpathyContactSelectorDialog *dialog,
62     TpConnection *connection,
63     const gchar *contact_id)
64 {
65   empathy_dispatcher_chat_with_contact_id (connection, contact_id, NULL, NULL);
66 }
67
68 static gboolean
69 empathy_new_message_account_filter (EmpathyContactSelectorDialog *dialog,
70     TpAccount *account)
71 {
72   TpConnection *connection;
73   EmpathyDispatcher *dispatcher;
74   GList *classes;
75
76   if (tp_account_get_connection_status (account, NULL) !=
77       TP_CONNECTION_STATUS_CONNECTED)
78     return FALSE;
79
80   /* check if CM supports 1-1 text chat */
81   connection = tp_account_get_connection (account);
82   if (connection == NULL)
83     return FALSE;
84
85   dispatcher = empathy_dispatcher_dup_singleton ();
86
87   classes = empathy_dispatcher_find_requestable_channel_classes
88     (dispatcher, connection, TP_IFACE_CHANNEL_TYPE_TEXT,
89      TP_HANDLE_TYPE_CONTACT, NULL);
90
91   g_object_unref (dispatcher);
92
93   if (classes == NULL)
94     return FALSE;
95
96   g_list_free (classes);
97   return TRUE;
98 }
99
100 static GObject *
101 empathy_new_message_dialog_constructor (GType type,
102     guint n_props,
103     GObjectConstructParam *props)
104 {
105   GObject *retval;
106
107   if (dialog_singleton)
108     {
109       retval = G_OBJECT (dialog_singleton);
110       g_object_ref (retval);
111     }
112   else
113     {
114       retval = G_OBJECT_CLASS (
115       empathy_new_message_dialog_parent_class)->constructor (type,
116         n_props, props);
117
118       dialog_singleton = EMPATHY_NEW_MESSAGE_DIALOG (retval);
119       g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
120     }
121
122   return retval;
123 }
124
125 static void
126 empathy_new_message_dialog_init (EmpathyNewMessageDialog *dialog)
127 {
128   EmpathyContactSelectorDialog *parent = EMPATHY_CONTACT_SELECTOR_DIALOG (
129         dialog);
130   GtkWidget *image;
131
132   /* add chat button */
133   parent->button_action = gtk_button_new_with_mnemonic (_("C_hat"));
134   image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_NEW_MESSAGE,
135       GTK_ICON_SIZE_BUTTON);
136   gtk_button_set_image (GTK_BUTTON (parent->button_action), image);
137
138   gtk_dialog_add_action_widget (GTK_DIALOG (dialog), parent->button_action,
139       GTK_RESPONSE_ACCEPT);
140   gtk_widget_show (parent->button_action);
141
142   /* Tweak the dialog */
143   gtk_window_set_title (GTK_WINDOW (dialog), _("New Conversation"));
144   gtk_window_set_role (GTK_WINDOW (dialog), "new_message");
145
146   gtk_widget_set_sensitive (parent->button_action, FALSE);
147 }
148
149 static void
150 empathy_new_message_dialog_class_init (
151   EmpathyNewMessageDialogClass *class)
152 {
153   GObjectClass *object_class = G_OBJECT_CLASS (class);
154   EmpathyContactSelectorDialogClass *dialog_class = \
155     EMPATHY_CONTACT_SELECTOR_DIALOG_CLASS (class);
156
157   object_class->constructor = empathy_new_message_dialog_constructor;
158
159   dialog_class->got_response = empathy_new_message_dialog_got_response;
160   dialog_class->account_filter = empathy_new_message_account_filter;
161 }
162
163 /**
164  * empathy_new_message_dialog_new:
165  * @parent: parent #GtkWindow of the dialog
166  *
167  * Create a new #EmpathyNewMessageDialog it.
168  *
169  * Return value: the new #EmpathyNewMessageDialog
170  */
171 GtkWidget *
172 empathy_new_message_dialog_show (GtkWindow *parent)
173 {
174   GtkWidget *dialog;
175
176   dialog = g_object_new (EMPATHY_TYPE_NEW_MESSAGE_DIALOG, NULL);
177
178   if (parent)
179     {
180       gtk_window_set_transient_for (GTK_WINDOW (dialog),
181           GTK_WINDOW (parent));
182     }
183
184   gtk_widget_show (dialog);
185   return dialog;
186 }