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