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