]> git.0d.be Git - empathy.git/blob - src/empathy-chat.c
Merge remote-tracking branch 'origin/call1' into master+call
[empathy.git] / src / empathy-chat.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 <libnotify/notify.h>
30
31 #include <telepathy-glib/debug-sender.h>
32
33 #include <libempathy/empathy-presence-manager.h>
34 #include <libempathy/empathy-individual-manager.h>
35
36 #include <libempathy-gtk/empathy-theme-manager.h>
37 #include <libempathy-gtk/empathy-ui-utils.h>
38
39 #include "empathy-chat-manager.h"
40
41 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
42 #include <libempathy/empathy-debug.h>
43
44 /* Exit after $TIMEOUT seconds if not displaying any call window */
45 #define TIMEOUT 60
46
47 #define EMPATHY_CHAT_DBUS_NAME "org.gnome.Empathy.Chat"
48
49 static GtkApplication *app = NULL;
50 static gboolean activated = FALSE;
51 static gboolean use_timer = TRUE;
52
53 static EmpathyChatManager *chat_mgr = NULL;
54
55 static void
56 displayed_chats_changed_cb (EmpathyChatManager *mgr,
57     guint nb_chats,
58     gpointer user_data)
59 {
60   DEBUG ("New chat count: %u", nb_chats);
61
62   if (nb_chats == 0)
63     g_application_release (G_APPLICATION (app));
64   else
65     g_application_hold (G_APPLICATION (app));
66 }
67
68 static void
69 activate_cb (GApplication *application)
70 {
71   if (activated)
72     return;
73
74   activated = TRUE;
75
76   if (!use_timer)
77     {
78       /* keep a 'ref' to the application */
79       g_application_hold (G_APPLICATION (application));
80     }
81
82   g_assert (chat_mgr == NULL);
83   chat_mgr = empathy_chat_manager_dup_singleton ();
84
85   g_signal_connect (chat_mgr, "displayed-chats-changed",
86       G_CALLBACK (displayed_chats_changed_cb), GUINT_TO_POINTER (1));
87 }
88
89 int
90 main (int argc,
91     char *argv[])
92 {
93   GOptionContext *optcontext;
94   GOptionEntry options[] = {
95       { NULL }
96   };
97 #ifdef ENABLE_DEBUG
98   TpDebugSender *debug_sender;
99 #endif
100   GError *error = NULL;
101   EmpathyPresenceManager *presence_mgr;
102   EmpathyThemeManager *theme_mgr;
103   gint retval;
104   EmpathyIndividualManager *individual_mgr;
105
106   /* Init */
107   g_thread_init (NULL);
108
109   optcontext = g_option_context_new (N_("- Empathy Chat Client"));
110   g_option_context_add_group (optcontext, gtk_get_option_group (TRUE));
111   g_option_context_add_main_entries (optcontext, options, GETTEXT_PACKAGE);
112
113   if (!g_option_context_parse (optcontext, &argc, &argv, &error))
114     {
115       g_print ("%s\nRun '%s --help' to see a full list of available command "
116           "line options.\n",
117           error->message, argv[0]);
118       g_warning ("Error in empathy-av init: %s", error->message);
119       return EXIT_FAILURE;
120     }
121
122   g_option_context_free (optcontext);
123
124   empathy_gtk_init ();
125
126   /* Make empathy and empathy-chat appear as the same app in gnome-shell */
127   gdk_set_program_class ("Empathy");
128   gtk_window_set_default_icon_name ("empathy");
129   textdomain (GETTEXT_PACKAGE);
130
131   notify_init (_(PACKAGE_NAME));
132
133   app = gtk_application_new (EMPATHY_CHAT_DBUS_NAME, G_APPLICATION_FLAGS_NONE);
134   g_signal_connect (app, "activate", G_CALLBACK (activate_cb), NULL);
135
136 #ifdef ENABLE_DEBUG
137   /* Set up debug sender */
138   debug_sender = tp_debug_sender_dup ();
139   g_log_set_default_handler (tp_debug_sender_log_handler, G_LOG_DOMAIN);
140 #endif
141
142   /* Setting up Idle */
143   presence_mgr = empathy_presence_manager_dup_singleton ();
144
145   /* Keep the theme manager alive as it does some caching */
146   theme_mgr = empathy_theme_manager_dup_singleton ();
147
148   /* Keep the individual manager alive so we won't fetch everything from Folks
149    * each time we need to use it. */
150   individual_mgr = empathy_individual_manager_dup_singleton ();
151
152   if (g_getenv ("EMPATHY_PERSIST") != NULL)
153     {
154       DEBUG ("Disable timer");
155
156       use_timer = FALSE;
157     }
158
159   /* the inactivity timeout can only be set while the application is held */
160   g_application_hold (G_APPLICATION (app));
161   g_application_set_inactivity_timeout (G_APPLICATION (app), TIMEOUT * 1000);
162   g_application_release (G_APPLICATION (app));
163
164   DEBUG ("Waiting for text channels to handle");
165
166   retval = g_application_run (G_APPLICATION (app), argc, argv);
167
168   g_object_unref (app);
169   g_object_unref (presence_mgr);
170   g_object_unref (theme_mgr);
171   tp_clear_object (&chat_mgr);
172   g_object_unref (individual_mgr);
173
174 #ifdef ENABLE_DEBUG
175   g_object_unref (debug_sender);
176 #endif
177
178   notify_uninit ();
179
180   return retval;
181 }