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