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