]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-new-call-dialog.c
[EmpathyContactSelectorDialog] switch from table_contents to VBox
[empathy.git] / libempathy-gtk / empathy-new-call-dialog.c
1 /*
2  * Copyright (C) 2009 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: Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
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-call-factory.h>
34 #include <libempathy/empathy-dispatcher.h>
35 #include <libempathy/empathy-utils.h>
36
37 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
38 #include <libempathy/empathy-debug.h>
39
40 #include <libempathy-gtk/empathy-ui-utils.h>
41 #include <libempathy-gtk/empathy-images.h>
42
43 #include "empathy-new-call-dialog.h"
44 #include "empathy-account-chooser.h"
45
46 static EmpathyNewCallDialog *dialog_singleton = NULL;
47
48 G_DEFINE_TYPE(EmpathyNewCallDialog, empathy_new_call_dialog,
49                EMPATHY_TYPE_CONTACT_SELECTOR_DIALOG)
50
51 typedef struct _EmpathyNewCallDialogPriv EmpathyNewCallDialogPriv;
52
53 struct _EmpathyNewCallDialogPriv {
54   GtkWidget *check_video;
55 };
56
57 #define GET_PRIV(o) \
58   (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_NEW_CALL_DIALOG, \
59     EmpathyNewCallDialogPriv))
60
61 /**
62  * SECTION:empathy-new-call-dialog
63  * @title: EmpathyNewCallDialog
64  * @short_description: A dialog to show a new call
65  * @include: libempathy-gtk/empathy-new-call-dialog.h
66  *
67  * #EmpathyNewCallDialog is a dialog which allows a call
68  * to be started with any contact on any enabled account.
69  */
70
71 static void
72 got_contact_cb (EmpathyTpContactFactory *factory,
73     EmpathyContact *contact,
74     const GError *error,
75     gpointer user_data,
76     GObject *object)
77 {
78   EmpathyCallFactory *call_factory;
79   gboolean video = GPOINTER_TO_UINT (user_data);
80
81   if (error != NULL)
82     {
83       DEBUG ("Failed: %s", error->message);
84       return;
85     }
86
87   call_factory = empathy_call_factory_get ();
88   empathy_call_factory_new_call_with_streams (call_factory, contact, TRUE,
89       video);
90 }
91
92 static void
93 empathy_new_call_dialog_got_response (EmpathyContactSelectorDialog *dialog,
94     TpConnection *connection,
95     const gchar *contact_id)
96 {
97   EmpathyNewCallDialogPriv *priv = GET_PRIV (dialog);
98   EmpathyTpContactFactory *factory;
99   gboolean video;
100
101   /* check if video is enabled now because the dialog will be destroyed once
102    * we return from this function. */
103   video = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->check_video));
104
105   factory = empathy_tp_contact_factory_dup_singleton (connection);
106   empathy_tp_contact_factory_get_from_id (factory, contact_id,
107       got_contact_cb, GUINT_TO_POINTER (video), NULL, NULL);
108
109   g_object_unref (factory);
110 }
111
112 static gboolean
113 empathy_new_call_dialog_account_filter (EmpathyContactSelectorDialog *dialog,
114     TpAccount *account)
115 {
116   TpConnection *connection;
117   EmpathyDispatcher *dispatcher;
118   GList *classes;
119
120   if (tp_account_get_connection_status (account, NULL) !=
121       TP_CONNECTION_STATUS_CONNECTED)
122     return FALSE;
123
124   /* check if CM supports calls */
125   connection = tp_account_get_connection (account);
126   if (connection == NULL)
127     return FALSE;
128
129   dispatcher = empathy_dispatcher_dup_singleton ();
130
131   classes = empathy_dispatcher_find_requestable_channel_classes
132     (dispatcher, connection, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
133      TP_HANDLE_TYPE_CONTACT, NULL);
134
135   g_object_unref (dispatcher);
136
137   if (classes == NULL)
138     return FALSE;
139
140   g_list_free (classes);
141   return TRUE;
142 }
143
144 static GObject *
145 empathy_new_call_dialog_constructor (GType type,
146     guint n_props,
147     GObjectConstructParam *props)
148 {
149   GObject *retval;
150
151   if (dialog_singleton)
152     {
153       retval = G_OBJECT (dialog_singleton);
154       g_object_ref (retval);
155     }
156   else
157     {
158       retval = G_OBJECT_CLASS (
159       empathy_new_call_dialog_parent_class)->constructor (type,
160         n_props, props);
161
162       dialog_singleton = EMPATHY_NEW_CALL_DIALOG (retval);
163       g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
164     }
165
166   return retval;
167 }
168
169 static void
170 empathy_new_call_dialog_init (EmpathyNewCallDialog *dialog)
171 {
172   EmpathyContactSelectorDialog *parent = EMPATHY_CONTACT_SELECTOR_DIALOG (
173         dialog);
174   EmpathyNewCallDialogPriv *priv = GET_PRIV (dialog);
175   GtkWidget *image;
176
177   /* add video toggle */
178   priv->check_video = gtk_check_button_new_with_mnemonic (_("Send _Video"));
179
180   gtk_box_pack_end (GTK_BOX (parent->vbox), priv->check_video,
181       FALSE, TRUE, 0);
182
183   gtk_widget_show (priv->check_video);
184
185   /* add chat button */
186   parent->button_action = gtk_button_new_with_mnemonic (_("_Call"));
187   image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
188       GTK_ICON_SIZE_BUTTON);
189   gtk_button_set_image (GTK_BUTTON (parent->button_action), image);
190
191   gtk_dialog_add_action_widget (GTK_DIALOG (dialog), parent->button_action,
192       GTK_RESPONSE_ACCEPT);
193   gtk_widget_show (parent->button_action);
194
195   /* Tweak the dialog */
196   gtk_window_set_title (GTK_WINDOW (dialog), _("New Call"));
197   gtk_window_set_role (GTK_WINDOW (dialog), "new_call");
198
199   gtk_widget_set_sensitive (parent->button_action, FALSE);
200 }
201
202 static void
203 empathy_new_call_dialog_class_init (
204   EmpathyNewCallDialogClass *class)
205 {
206   GObjectClass *object_class = G_OBJECT_CLASS (class);
207   EmpathyContactSelectorDialogClass *dialog_class = \
208     EMPATHY_CONTACT_SELECTOR_DIALOG_CLASS (class);
209
210   g_type_class_add_private (class, sizeof (EmpathyNewCallDialogPriv));
211
212   object_class->constructor = empathy_new_call_dialog_constructor;
213
214   dialog_class->got_response = empathy_new_call_dialog_got_response;
215   dialog_class->account_filter = empathy_new_call_dialog_account_filter;
216 }
217
218 /**
219  * empathy_new_call_dialog_new:
220  * @parent: parent #GtkWindow of the dialog
221  *
222  * Create a new #EmpathyNewCallDialog it.
223  *
224  * Return value: the new #EmpathyNewCallDialog
225  */
226 GtkWidget *
227 empathy_new_call_dialog_show (GtkWindow *parent)
228 {
229   GtkWidget *dialog;
230
231   dialog = g_object_new (EMPATHY_TYPE_NEW_CALL_DIALOG, NULL);
232
233   if (parent)
234     {
235       gtk_window_set_transient_for (GTK_WINDOW (dialog),
236                   GTK_WINDOW (parent));
237     }
238
239   gtk_widget_show (dialog);
240   return dialog;
241 }