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