]> git.0d.be Git - empathy.git/blob - src/empathy-av.c
empathy.c: call g_application_hold() on the app
[empathy.git] / src / empathy-av.c
1 /*
2  * Copyright (C) 2007-2010 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 <telepathy-glib/debug-sender.h>
30
31 #include <libempathy/empathy-call-factory.h>
32 #include <libempathy-gtk/empathy-ui-utils.h>
33
34 #include "empathy-call-window.h"
35
36 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
37 #include <libempathy/empathy-debug.h>
38
39 #include <gst/gst.h>
40
41 /* Exit after $TIMEOUT seconds if not displaying any call window */
42 #define TIMEOUT 60
43
44 #define EMPATHY_AV_DBUS_NAME "org.gnome.Empathy.AudioVideo"
45
46 static GtkApplication *app = NULL;
47 static gboolean app_held = FALSE;
48 static guint nb_windows = 0;
49 static gboolean use_timer = TRUE;
50
51 static void
52 start_timer (void)
53 {
54   if (!use_timer)
55     return;
56
57   DEBUG ("Start timer");
58
59   if (app_held)
60     g_application_release (G_APPLICATION (app));
61 }
62
63 static void
64 stop_timer (void)
65 {
66   DEBUG ("Stop timer");
67
68   g_application_hold (G_APPLICATION (app));
69   app_held = TRUE;
70 }
71
72 static void
73 call_window_destroy_cb (EmpathyCallWindow *window,
74     gpointer user_data)
75 {
76   nb_windows--;
77
78   if (nb_windows > 0)
79     return;
80
81   start_timer ();
82 }
83
84 static void
85 new_call_handler_cb (EmpathyCallFactory *factory,
86     EmpathyCallHandler *handler,
87     gboolean outgoing,
88     gpointer user_data)
89 {
90   EmpathyCallWindow *window;
91
92   DEBUG ("Create a new call window");
93
94   window = empathy_call_window_new (handler);
95
96   nb_windows++;
97   stop_timer ();
98
99   g_signal_connect (window, "destroy",
100       G_CALLBACK (call_window_destroy_cb), NULL);
101
102   gtk_widget_show (GTK_WIDGET (window));
103 }
104
105 int
106 main (int argc,
107     char *argv[])
108 {
109   GOptionContext *optcontext;
110   GOptionEntry options[] = {
111       { NULL }
112   };
113 #ifdef ENABLE_DEBUG
114   TpDebugSender *debug_sender;
115 #endif
116   EmpathyCallFactory *call_factory;
117   GError *error = NULL;
118
119   /* Init */
120   g_thread_init (NULL);
121
122   optcontext = g_option_context_new (N_("- Empathy Audio/Video Client"));
123   g_option_context_add_group (optcontext, gst_init_get_option_group ());
124   g_option_context_add_group (optcontext, gtk_get_option_group (TRUE));
125   g_option_context_add_main_entries (optcontext, options, GETTEXT_PACKAGE);
126
127   if (!g_option_context_parse (optcontext, &argc, &argv, &error)) {
128     g_print ("%s\nRun '%s --help' to see a full list of available command "
129         "line options.\n",
130         error->message, argv[0]);
131     g_warning ("Error in empathy-av init: %s", error->message);
132     return EXIT_FAILURE;
133   }
134
135   g_option_context_free (optcontext);
136
137   empathy_gtk_init ();
138   g_set_application_name (_("Empathy Audio/Video Client"));
139   g_setenv ("PULSE_PROP_media.role", "phone", TRUE);
140
141   gtk_window_set_default_icon_name ("empathy");
142   textdomain (GETTEXT_PACKAGE);
143
144   app = gtk_application_new (EMPATHY_AV_DBUS_NAME, G_APPLICATION_IS_SERVICE);
145
146 #ifdef ENABLE_DEBUG
147   /* Set up debug sender */
148   debug_sender = tp_debug_sender_dup ();
149   g_log_set_default_handler (tp_debug_sender_log_handler, G_LOG_DOMAIN);
150 #endif
151
152   call_factory = empathy_call_factory_initialise ();
153
154   g_signal_connect (G_OBJECT (call_factory), "new-call-handler",
155       G_CALLBACK (new_call_handler_cb), NULL);
156
157   if (!empathy_call_factory_register (call_factory, &error))
158     {
159       g_critical ("Failed to register Handler: %s", error->message);
160       g_error_free (error);
161       return EXIT_FAILURE;
162     }
163
164   if (g_getenv ("EMPATHY_PERSIST") != NULL)
165     {
166       DEBUG ("Disable timer");
167
168       use_timer = FALSE;
169     }
170
171   /* the inactivity timeout can only be set while the application is held */
172   g_application_hold (G_APPLICATION (app));
173   g_application_set_inactivity_timeout (G_APPLICATION (app), TIMEOUT * 1000);
174   if (use_timer)
175     {
176       g_application_release (G_APPLICATION (app));
177       app_held = FALSE;
178     }
179   else
180     app_held = TRUE;
181
182   start_timer ();
183
184   g_application_run (G_APPLICATION (app), argc, argv);
185
186   g_object_unref (app);
187   g_object_unref (call_factory);
188
189 #ifdef ENABLE_DEBUG
190   g_object_unref (debug_sender);
191 #endif
192
193   return EXIT_SUCCESS;
194 }