]> git.0d.be Git - empathy.git/blob - src/empathy-chat.c
Create 'empathy-chat' handling text channels (#631946)
[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 <telepathy-glib/debug-sender.h>
30
31 #include <libempathy/empathy-idle.h>
32 #include <libempathy-gtk/empathy-ui-utils.h>
33
34 #include "empathy-chat-manager.h"
35
36 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
37 #include <libempathy/empathy-debug.h>
38
39 /* Exit after $TIMEOUT seconds if not displaying any call window */
40 #define TIMEOUT 60
41
42 #define EMPATHY_CHAT_DBUS_NAME "org.gnome.Empathy.Chat"
43
44 static GtkApplication *app = NULL;
45 static gboolean app_held = FALSE;
46 static guint timeout_id = 0;
47 static gboolean use_timer = TRUE;
48
49 static void
50 start_timer (void)
51 {
52   if (!use_timer)
53     return;
54
55   DEBUG ("Start timer");
56
57   if (app_held)
58     g_application_release (G_APPLICATION (app));
59 }
60
61 static void
62 stop_timer (void)
63 {
64   if (timeout_id == 0)
65     return;
66
67   DEBUG ("Stop timer");
68
69   g_application_hold (G_APPLICATION (app));
70   app_held = TRUE;
71 }
72
73 static void
74 handled_chats_changed_cb (EmpathyChatManager *mgr,
75     guint nb_chats,
76     gpointer user_data)
77 {
78   if (nb_chats > 0)
79     {
80       stop_timer ();
81     }
82   else
83     {
84       start_timer ();
85     }
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   EmpathyChatManager *chat_mgr;
100   GError *error = NULL;
101   EmpathyIdle *idle;
102
103   /* Init */
104   g_thread_init (NULL);
105
106   optcontext = g_option_context_new (N_("- Empathy Chat Client"));
107   g_option_context_add_group (optcontext, gtk_get_option_group (TRUE));
108   g_option_context_add_main_entries (optcontext, options, GETTEXT_PACKAGE);
109
110   if (!g_option_context_parse (optcontext, &argc, &argv, &error)) {
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   gtk_window_set_default_icon_name ("empathy");
123   textdomain (GETTEXT_PACKAGE);
124
125   app = gtk_application_new (EMPATHY_CHAT_DBUS_NAME, G_APPLICATION_IS_SERVICE);
126
127 #ifdef ENABLE_DEBUG
128   /* Set up debug sender */
129   debug_sender = tp_debug_sender_dup ();
130   g_log_set_default_handler (tp_debug_sender_log_handler, G_LOG_DOMAIN);
131 #endif
132
133   /* Setting up Idle */
134   idle = empathy_idle_dup_singleton ();
135
136   chat_mgr = empathy_chat_manager_dup_singleton ();
137
138   g_signal_connect (chat_mgr, "handled-chats-changed",
139       G_CALLBACK (handled_chats_changed_cb), NULL);
140
141   if (g_getenv ("EMPATHY_PERSIST") != NULL)
142     {
143       DEBUG ("Disable timer");
144
145       use_timer = FALSE;
146     }
147
148   /* the inactivity timeout can only be set while the application is held */
149   g_application_hold (G_APPLICATION (app));
150   g_application_set_inactivity_timeout (G_APPLICATION (app), TIMEOUT * 1000);
151   if (use_timer)
152     {
153       g_application_release (G_APPLICATION (app));
154       app_held = FALSE;
155     }
156   else
157     app_held = TRUE;
158
159   start_timer ();
160
161   DEBUG ("Waiting for text channels to handle");
162
163   g_application_run (G_APPLICATION (app), argc, argv);
164
165   g_object_unref (app);
166   g_object_unref (idle);
167   g_object_unref (chat_mgr);
168
169 #ifdef ENABLE_DEBUG
170   g_object_unref (debug_sender);
171 #endif
172
173   return EXIT_SUCCESS;
174 }