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