]> git.0d.be Git - empathy.git/blob - src/empathy-av.c
Don't destroy the call factory right away (#633816)
[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 activated = FALSE;
48 static gboolean use_timer = TRUE;
49
50 static EmpathyCallFactory *call_factory = NULL;
51
52 static void
53 new_call_handler_cb (EmpathyCallFactory *factory,
54     EmpathyCallHandler *handler,
55     gboolean outgoing,
56     gpointer user_data)
57 {
58   EmpathyCallWindow *window;
59
60   DEBUG ("Create a new call window");
61
62   window = empathy_call_window_new (handler);
63
64   g_application_hold (G_APPLICATION (app));
65
66   g_signal_connect_swapped (window, "destroy",
67       G_CALLBACK (g_application_release), app);
68
69   gtk_widget_show (GTK_WIDGET (window));
70 }
71
72 static void
73 activate_cb (GApplication *application)
74 {
75   if (!use_timer && !activated)
76     {
77       GError *error = NULL;
78
79       /* keep a 'ref' to the application */
80       g_application_hold (G_APPLICATION (app));
81
82       g_assert (call_factory == NULL);
83       call_factory = empathy_call_factory_initialise ();
84
85       g_signal_connect (G_OBJECT (call_factory), "new-call-handler",
86           G_CALLBACK (new_call_handler_cb), NULL);
87
88       if (!empathy_call_factory_register (call_factory, &error))
89         {
90           g_critical ("Failed to register Handler: %s", error->message);
91           g_error_free (error);
92         }
93
94       activated = TRUE;
95     }
96 }
97
98 int
99 main (int argc,
100     char *argv[])
101 {
102   GOptionContext *optcontext;
103   GOptionEntry options[] = {
104       { NULL }
105   };
106 #ifdef ENABLE_DEBUG
107   TpDebugSender *debug_sender;
108 #endif
109   GError *error = NULL;
110   gint retval;
111
112   /* Init */
113   g_thread_init (NULL);
114
115   optcontext = g_option_context_new (N_("- Empathy Audio/Video Client"));
116   g_option_context_add_group (optcontext, gst_init_get_option_group ());
117   g_option_context_add_group (optcontext, gtk_get_option_group (TRUE));
118   g_option_context_add_main_entries (optcontext, options, GETTEXT_PACKAGE);
119
120   if (!g_option_context_parse (optcontext, &argc, &argv, &error)) {
121     g_print ("%s\nRun '%s --help' to see a full list of available command "
122         "line options.\n",
123         error->message, argv[0]);
124     g_warning ("Error in empathy-av init: %s", error->message);
125     return EXIT_FAILURE;
126   }
127
128   g_option_context_free (optcontext);
129
130   empathy_gtk_init ();
131   g_set_application_name (_("Empathy Audio/Video Client"));
132   g_setenv ("PULSE_PROP_media.role", "phone", TRUE);
133
134   gtk_window_set_default_icon_name ("empathy");
135   textdomain (GETTEXT_PACKAGE);
136
137   app = gtk_application_new (EMPATHY_AV_DBUS_NAME, G_APPLICATION_FLAGS_NONE);
138   g_signal_connect (app, "activate", G_CALLBACK (activate_cb), NULL);
139
140 #ifdef ENABLE_DEBUG
141   /* Set up debug sender */
142   debug_sender = tp_debug_sender_dup ();
143   g_log_set_default_handler (tp_debug_sender_log_handler, G_LOG_DOMAIN);
144 #endif
145
146   if (g_getenv ("EMPATHY_PERSIST") != NULL)
147     {
148       DEBUG ("Disable timer");
149
150       use_timer = FALSE;
151     }
152
153   /* the inactivity timeout can only be set while the application is held */
154   g_application_hold (G_APPLICATION (app));
155   g_application_set_inactivity_timeout (G_APPLICATION (app), TIMEOUT * 1000);
156   g_application_release (G_APPLICATION (app));
157
158   retval = g_application_run (G_APPLICATION (app), argc, argv);
159
160   g_object_unref (app);
161   tp_clear_object (&call_factory);
162
163 #ifdef ENABLE_DEBUG
164   g_object_unref (debug_sender);
165 #endif
166
167   return retval;
168 }