]> git.0d.be Git - empathy.git/blob - src/empathy-call.c
9d5ab8a83f64b6b51141ac3b11dd874f1a68f2ea
[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/gi18n.h>
26
27 #include <clutter-gtk/clutter-gtk.h>
28 #include <clutter-gst/clutter-gst.h>
29
30 #ifdef CLUTTER_WINDOWING_X11
31 #include <X11/Xlib.h>
32 #endif
33
34 #include <libempathy-gtk/empathy-ui-utils.h>
35
36 #include "empathy-call-window.h"
37 #include "empathy-call-factory.h"
38
39 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
40 #include <libempathy/empathy-debug.h>
41
42 /* Exit after $TIMEOUT seconds if not displaying any call window */
43 #define TIMEOUT 60
44
45 #define EMPATHY_CALL_DBUS_NAME "org.gnome.Empathy.Call"
46
47 static GtkApplication *app = NULL;
48 static gboolean activated = FALSE;
49 static gboolean use_timer = TRUE;
50 static guint inhibit_id = 0;
51
52 static EmpathyCallFactory *call_factory = NULL;
53
54 /* An EmpathyContact -> EmpathyCallWindow hash table for all existing
55  * Call windows. We own a ref on the EmpathyContacts. */
56 static GHashTable *call_windows;
57
58 static void
59 call_window_destroyed_cb (GtkWidget *window,
60     EmpathyContact *contact)
61 {
62   g_hash_table_remove (call_windows, contact);
63
64   g_application_release (G_APPLICATION (app));
65 }
66
67 static gboolean
68 find_window_for_handle (gpointer key,
69     gpointer value,
70     gpointer user_data)
71 {
72   EmpathyContact *contact = key;
73   guint handle = GPOINTER_TO_UINT (user_data);
74
75   if (handle == empathy_contact_get_handle (contact))
76     return TRUE;
77
78   return FALSE;
79 }
80
81 static gboolean
82 incoming_call_cb (EmpathyCallFactory *factory,
83     guint handle,
84     TpCallChannel *channel,
85     TpChannelDispatchOperation *dispatch_operation,
86     TpAddDispatchOperationContext *context,
87     gpointer user_data)
88 {
89   EmpathyCallWindow *window = g_hash_table_find (call_windows,
90       find_window_for_handle, GUINT_TO_POINTER (handle));
91
92   if (window != NULL)
93     {
94       /* The window takes care of accepting or rejecting the context. */
95       empathy_call_window_start_ringing (window,
96           channel, dispatch_operation, context);
97       return TRUE;
98     }
99
100   return FALSE;
101 }
102
103 static void
104 call_window_inhibit_cb (EmpathyCallWindow *window,
105     gboolean inhibit,
106     gpointer user_data)
107 {
108   if (inhibit)
109     {
110       if (inhibit_id != 0)
111         return;
112
113       inhibit_id = gtk_application_inhibit (GTK_APPLICATION (app),
114           GTK_WINDOW (window),
115           GTK_APPLICATION_INHIBIT_LOGOUT | GTK_APPLICATION_INHIBIT_SWITCH |
116           GTK_APPLICATION_INHIBIT_SUSPEND | GTK_APPLICATION_INHIBIT_IDLE,
117           _("In a call"));
118     }
119   else
120     {
121       if (inhibit_id == 0)
122         return;
123
124       gtk_application_uninhibit (GTK_APPLICATION (app), inhibit_id);
125       inhibit_id = 0;
126     }
127 }
128
129 static void
130 new_call_handler_cb (EmpathyCallFactory *factory,
131     EmpathyCallHandler *handler,
132     gboolean outgoing,
133     gpointer user_data)
134 {
135   EmpathyCallWindow *window;
136   EmpathyContact *contact;
137
138   DEBUG ("Show the call window");
139
140   contact = empathy_call_handler_get_contact (handler);
141
142   window = g_hash_table_lookup (call_windows, contact);
143
144   if (window != NULL)
145     {
146       empathy_call_window_present (window, handler);
147     }
148   else
149     {
150       window = empathy_call_window_new (handler);
151
152       g_hash_table_insert (call_windows, g_object_ref (contact), window);
153       g_application_hold (G_APPLICATION (app));
154       g_signal_connect (window, "destroy",
155           G_CALLBACK (call_window_destroyed_cb), contact);
156       g_signal_connect (window, "inhibit",
157           G_CALLBACK (call_window_inhibit_cb), NULL);
158
159       gtk_widget_show (GTK_WIDGET (window));
160     }
161 }
162
163 static void
164 activate_cb (GApplication *application)
165 {
166   GError *error = NULL;
167
168   if (activated)
169     return;
170
171   activated = TRUE;
172
173   if (!use_timer)
174     {
175       /* keep a 'ref' to the application */
176       g_application_hold (G_APPLICATION (app));
177     }
178
179   g_assert (call_factory == NULL);
180   call_factory = empathy_call_factory_initialise ();
181
182   g_signal_connect (G_OBJECT (call_factory), "new-call-handler",
183       G_CALLBACK (new_call_handler_cb), NULL);
184   g_signal_connect (G_OBJECT (call_factory), "incoming-call",
185       G_CALLBACK (incoming_call_cb), NULL);
186
187   if (!empathy_call_factory_register (call_factory, &error))
188     {
189       g_critical ("Failed to register Handler: %s", error->message);
190       g_error_free (error);
191     }
192 }
193
194 int
195 main (int argc,
196     char *argv[])
197 {
198   GOptionContext *optcontext;
199   GOptionEntry options[] = {
200       { NULL }
201   };
202 #ifdef ENABLE_DEBUG
203   TpDebugSender *debug_sender;
204 #endif
205   GError *error = NULL;
206   gint retval;
207   GtkSettings *gtk_settings;
208
209   g_setenv ("GST_DEBUG_DUMP_DOT_DIR", g_get_tmp_dir (), FALSE);
210
211 #ifdef GDK_WINDOWING_X11
212   /* We can't call clutter_gst_init() before gtk_clutter_init(), so no choice
213    * but to intiialise X11 threading ourself */
214   XInitThreads ();
215 #endif
216
217   optcontext = g_option_context_new (N_("- Empathy Audio/Video Client"));
218   g_option_context_add_group (optcontext, gst_init_get_option_group ());
219   g_option_context_add_group (optcontext, gtk_get_option_group (TRUE));
220   g_option_context_add_group (optcontext, cogl_get_option_group ());
221   g_option_context_add_group (optcontext,
222       clutter_get_option_group_without_init ());
223   g_option_context_add_group (optcontext, gtk_clutter_get_option_group ());
224   g_option_context_add_main_entries (optcontext, options, GETTEXT_PACKAGE);
225   g_option_context_set_translation_domain (optcontext, GETTEXT_PACKAGE);
226
227   if (!g_option_context_parse (optcontext, &argc, &argv, &error)) {
228     g_print ("%s\nRun '%s --help' to see a full list of available command "
229         "line options.\n",
230         error->message, argv[0]);
231     g_warning ("Error in empathy-call init: %s", error->message);
232     return EXIT_FAILURE;
233   }
234
235   g_option_context_free (optcontext);
236
237   clutter_gst_init (&argc, &argv);
238
239   empathy_gtk_init ();
240   textdomain (GETTEXT_PACKAGE);
241   g_set_application_name (_("Empathy Audio/Video Client"));
242
243   /* Make empathy and empathy-call appear as the same app in gnome-shell */
244   gdk_set_program_class ("Empathy");
245   gtk_window_set_default_icon_name ("empathy");
246
247   gtk_settings = gtk_settings_get_default ();
248   g_object_set (G_OBJECT (gtk_settings), "gtk-application-prefer-dark-theme",
249       TRUE, NULL);
250
251   app = gtk_application_new (EMPATHY_CALL_DBUS_NAME, G_APPLICATION_FLAGS_NONE);
252   g_signal_connect (app, "activate", G_CALLBACK (activate_cb), NULL);
253
254 #ifdef ENABLE_DEBUG
255   /* Set up debug sender */
256   debug_sender = tp_debug_sender_dup ();
257   g_log_set_default_handler (tp_debug_sender_log_handler, G_LOG_DOMAIN);
258 #endif
259
260   if (g_getenv ("EMPATHY_PERSIST") != NULL)
261     {
262       DEBUG ("Disable timer");
263
264       use_timer = FALSE;
265     }
266
267   call_windows = g_hash_table_new_full (g_direct_hash, g_direct_equal,
268       g_object_unref, NULL);
269
270   /* the inactivity timeout can only be set while the application is held */
271   g_application_hold (G_APPLICATION (app));
272   g_application_set_inactivity_timeout (G_APPLICATION (app), TIMEOUT * 1000);
273   g_application_release (G_APPLICATION (app));
274
275   retval = g_application_run (G_APPLICATION (app), argc, argv);
276
277   g_hash_table_unref (call_windows);
278   g_object_unref (app);
279   tp_clear_object (&call_factory);
280
281 #ifdef ENABLE_DEBUG
282   g_object_unref (debug_sender);
283 #endif
284
285   return retval;
286 }