]> git.0d.be Git - empathy.git/blob - src/empathy-call.c
Merge remote-tracking branch 'origin/call1' into master+call
[empathy.git] / src / empathy-call.c
1 /*
2  * Copyright (C) 2007-2011 Collabora Ltd.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program 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  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA  02110-1301  USA
18  *
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  *          Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
21  */
22
23 #include <config.h>
24
25 #include <glib.h>
26 #include <glib/gi18n.h>
27 #include <gtk/gtk.h>
28
29 #include <clutter/clutter.h>
30 #include <clutter-gtk/clutter-gtk.h>
31 #include <clutter-gst/clutter-gst.h>
32
33 #ifdef CLUTTER_WINDOWING_X11
34 #include <X11/Xlib.h>
35 #endif
36
37 #include <telepathy-glib/debug-sender.h>
38
39 #include <libempathy/empathy-client-factory.h>
40
41 #include <libempathy-gtk/empathy-ui-utils.h>
42
43 #include "empathy-call-window.h"
44 #include "empathy-call-factory.h"
45
46 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
47 #include <libempathy/empathy-debug.h>
48
49 #include <gst/gst.h>
50
51 /* Exit after $TIMEOUT seconds if not displaying any call window */
52 #define TIMEOUT 60
53
54 #define EMPATHY_CALL_DBUS_NAME "org.gnome.Empathy.Call"
55
56 static GtkApplication *app = NULL;
57 static gboolean activated = FALSE;
58 static gboolean use_timer = TRUE;
59
60 static EmpathyCallFactory *call_factory = NULL;
61
62 /* An EmpathyContact -> EmpathyCallWindow hash table for all existing
63  * Call windows. We own a ref on the EmpathyContacts. */
64 static GHashTable *call_windows;
65
66 static void
67 call_window_destroyed_cb (GtkWidget *window,
68     EmpathyContact *contact)
69 {
70   g_hash_table_remove (call_windows, contact);
71
72   g_application_release (G_APPLICATION (app));
73 }
74
75 static gboolean
76 find_window_for_handle (gpointer key,
77     gpointer value,
78     gpointer user_data)
79 {
80   EmpathyContact *contact = key;
81   guint handle = GPOINTER_TO_UINT (user_data);
82
83   if (handle == empathy_contact_get_handle (contact))
84     return TRUE;
85
86   return FALSE;
87 }
88
89 static gboolean
90 incoming_call_cb (EmpathyCallFactory *factory,
91     guint handle,
92     TpCallChannel *channel,
93     TpChannelDispatchOperation *dispatch_operation,
94     TpAddDispatchOperationContext *context,
95     gpointer user_data)
96 {
97   EmpathyCallWindow *window = g_hash_table_find (call_windows,
98       find_window_for_handle, GUINT_TO_POINTER (handle));
99
100   if (window != NULL)
101     {
102       /* The window takes care of accepting or rejecting the context. */
103       empathy_call_window_start_ringing (window,
104           channel, dispatch_operation, context);
105       return TRUE;
106     }
107
108   return FALSE;
109 }
110
111 static void
112 new_call_handler_cb (EmpathyCallFactory *factory,
113     EmpathyCallHandler *handler,
114     gboolean outgoing,
115     gpointer user_data)
116 {
117   EmpathyCallWindow *window;
118   EmpathyContact *contact;
119
120   DEBUG ("Show the call window");
121
122   g_object_get (handler, "target-contact", &contact, NULL);
123
124   window = g_hash_table_lookup (call_windows, contact);
125
126   if (window != NULL)
127     {
128       empathy_call_window_present (window, handler);
129     }
130   else
131     {
132       window = empathy_call_window_new (handler);
133
134       g_hash_table_insert (call_windows, g_object_ref (contact), window);
135       g_application_hold (G_APPLICATION (app));
136       g_signal_connect (window, "destroy",
137           G_CALLBACK (call_window_destroyed_cb), contact);
138
139       gtk_widget_show (GTK_WIDGET (window));
140     }
141 }
142
143 static void
144 activate_cb (GApplication *application)
145 {
146   GError *error = NULL;
147
148   if (activated)
149     return;
150
151   activated = TRUE;
152
153   if (!use_timer)
154     {
155       /* keep a 'ref' to the application */
156       g_application_hold (G_APPLICATION (app));
157     }
158
159   g_assert (call_factory == NULL);
160   call_factory = empathy_call_factory_initialise ();
161
162   g_signal_connect (G_OBJECT (call_factory), "new-call-handler",
163       G_CALLBACK (new_call_handler_cb), NULL);
164   g_signal_connect (G_OBJECT (call_factory), "incoming-call",
165       G_CALLBACK (incoming_call_cb), NULL);
166
167   if (!empathy_call_factory_register (call_factory, &error))
168     {
169       g_critical ("Failed to register Handler: %s", error->message);
170       g_error_free (error);
171     }
172 }
173
174 int
175 main (int argc,
176     char *argv[])
177 {
178   GOptionContext *optcontext;
179   GOptionEntry options[] = {
180       { NULL }
181   };
182 #ifdef ENABLE_DEBUG
183   TpDebugSender *debug_sender;
184 #endif
185   GError *error = NULL;
186   gint retval;
187   GtkSettings *gtk_settings;
188
189   /* Init */
190   g_thread_init (NULL);
191
192 #ifdef GDK_WINDOWING_X11
193   /* We can't call clutter_gst_init() before gtk_clutter_init(), so no choice
194    * but to intiialise X11 threading ourself */
195   XInitThreads ();
196 #endif
197
198   /* Clutter needs this */
199   gdk_disable_multidevice ();
200
201   optcontext = g_option_context_new (N_("- Empathy Audio/Video Client"));
202   g_option_context_add_group (optcontext, gst_init_get_option_group ());
203   g_option_context_add_group (optcontext, gtk_get_option_group (TRUE));
204   g_option_context_add_group (optcontext, cogl_get_option_group ());
205   g_option_context_add_group (optcontext,
206       clutter_get_option_group_without_init ());
207   g_option_context_add_group (optcontext, gtk_clutter_get_option_group ());
208   g_option_context_add_main_entries (optcontext, options, GETTEXT_PACKAGE);
209
210   if (!g_option_context_parse (optcontext, &argc, &argv, &error)) {
211     g_print ("%s\nRun '%s --help' to see a full list of available command "
212         "line options.\n",
213         error->message, argv[0]);
214     g_warning ("Error in empathy-call init: %s", error->message);
215     return EXIT_FAILURE;
216   }
217
218   g_option_context_free (optcontext);
219
220   gtk_clutter_init (&argc, &argv);
221   clutter_gst_init (&argc, &argv);
222
223   empathy_gtk_init ();
224   g_set_application_name (_("Empathy Audio/Video Client"));
225
226   /* Make empathy and empathy-call appear as the same app in gnome-shell */
227   gdk_set_program_class ("Empathy");
228   gtk_window_set_default_icon_name ("empathy");
229   textdomain (GETTEXT_PACKAGE);
230
231   gtk_settings = gtk_settings_get_default ();
232   g_object_set (G_OBJECT (gtk_settings), "gtk-application-prefer-dark-theme",
233       TRUE, NULL);
234
235   app = gtk_application_new (EMPATHY_CALL_DBUS_NAME, G_APPLICATION_FLAGS_NONE);
236   g_signal_connect (app, "activate", G_CALLBACK (activate_cb), NULL);
237
238 #ifdef ENABLE_DEBUG
239   /* Set up debug sender */
240   debug_sender = tp_debug_sender_dup ();
241   g_log_set_default_handler (tp_debug_sender_log_handler, G_LOG_DOMAIN);
242 #endif
243
244   if (g_getenv ("EMPATHY_PERSIST") != NULL)
245     {
246       DEBUG ("Disable timer");
247
248       use_timer = FALSE;
249     }
250
251   call_windows = g_hash_table_new_full (g_direct_hash, g_direct_equal,
252       g_object_unref, NULL);
253
254   /* the inactivity timeout can only be set while the application is held */
255   g_application_hold (G_APPLICATION (app));
256   g_application_set_inactivity_timeout (G_APPLICATION (app), TIMEOUT * 1000);
257   g_application_release (G_APPLICATION (app));
258
259   retval = g_application_run (G_APPLICATION (app), argc, argv);
260
261   g_hash_table_unref (call_windows);
262   g_object_unref (app);
263   tp_clear_object (&call_factory);
264
265 #ifdef ENABLE_DEBUG
266   g_object_unref (debug_sender);
267 #endif
268
269   return retval;
270 }