]> git.0d.be Git - empathy.git/blob - src/empathy-chat-main.c
Disconnect signals from chatroom_manager. Fixes bug #447178.
[empathy.git] / src / empathy-chat-main.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007 Collabora Ltd.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  * 
20  * Authors: Xavier Claessens <xclaesse@gmail.com>
21  */
22
23 #include <config.h>
24
25 #include <stdlib.h>
26
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <gtk/gtk.h>
30
31 #include <libgnome/gnome-program.h>
32 #include <libgnomeui/gnome-ui-init.h>
33
34 #include <libtelepathy/tp-conn.h>
35 #include <libtelepathy/tp-chan.h>
36 #include <libmissioncontrol/mc-account.h>
37
38 #include <libempathy/gossip-contact.h>
39 #include <libempathy/gossip-debug.h>
40 #include <libempathy/gossip-utils.h>
41 #include <libempathy/empathy-chandler.h>
42 #include <libempathy/empathy-tp-chat.h>
43 #include <libempathy-gtk/gossip-private-chat.h>
44 #include <libempathy-gtk/gossip-group-chat.h>
45
46 #define DEBUG_DOMAIN "ChatMain"
47
48 #define BUS_NAME "org.gnome.Empathy.Chat"
49 #define OBJECT_PATH "/org/freedesktop/Telepathy/ChannelHandler"
50
51 /* Time to wait before exit, in seconds */
52 #define EXIT_TIMEOUT 5
53
54
55 static guint    chat_count = 0;
56 static guint    exit_timeout = 0;
57 static gboolean debug_mode = FALSE;
58
59 static gboolean
60 exit_timeout_cb (gpointer user_data)
61 {
62         gossip_debug (DEBUG_DOMAIN, "Timeout, exiting");
63
64         gtk_main_quit ();
65
66         return FALSE;
67 }
68
69 static void
70 exit_timeout_start (void)
71 {
72         if (exit_timeout || debug_mode) {
73                 return;
74         }
75
76         exit_timeout = g_timeout_add (EXIT_TIMEOUT * 1000,
77                                       (GSourceFunc) exit_timeout_cb,
78                                       NULL);
79 }
80
81 static void
82 exit_timeout_stop (void)
83 {
84         if (exit_timeout) {
85                 gossip_debug (DEBUG_DOMAIN, "Exit timeout canceled");
86                 g_source_remove (exit_timeout);
87                 exit_timeout = 0;
88         }
89 }
90
91 static void
92 chat_finalized_cb (gpointer    user_data,
93                    GossipChat *chat)
94 {
95         chat_count--;
96         if (chat_count == 0) {
97                 gossip_debug (DEBUG_DOMAIN, "No more chat, start exit timeout");
98                 exit_timeout_start ();
99         }
100 }
101
102 static void
103 new_channel_cb (EmpathyChandler *chandler,
104                 TpConn          *tp_conn,
105                 TpChan          *tp_chan,
106                 gpointer         user_data)
107 {
108         MissionControl *mc;
109         McAccount      *account;
110         GossipChat     *chat;
111         gchar          *id;
112
113         mc = gossip_mission_control_new ();
114         account = mission_control_get_account_for_connection (mc, tp_conn, NULL);
115         id = gossip_get_channel_id (account, tp_chan);
116         chat = gossip_chat_window_find_chat (account, id);
117
118         g_free (id);
119         g_object_unref (mc);
120
121         if (chat) {
122                 /* The chat already exists */
123                 if (!gossip_chat_is_connected (chat)) {
124                         EmpathyTpChat *tp_chat;
125
126                         /* The chat died, give him the new text channel */
127                         tp_chat = empathy_tp_chat_new (account, tp_chan);
128                         gossip_chat_set_tp_chat (chat, tp_chat);
129                         g_object_unref (tp_chat);
130                 }
131                 gossip_chat_present (chat);
132
133                 g_object_unref (account);
134                 return;
135         }
136
137         if (tp_chan->handle_type == TP_HANDLE_TYPE_CONTACT) {
138                 /* We have a new private chat channel */
139                 chat = GOSSIP_CHAT (gossip_private_chat_new (account, tp_chan));
140         }
141         else if (tp_chan->handle_type == TP_HANDLE_TYPE_ROOM) {
142                 /* We have a new group chat channel */
143                 chat = GOSSIP_CHAT (gossip_group_chat_new (account, tp_chan));
144         }
145
146         g_object_weak_ref (G_OBJECT (chat),
147                            (GWeakNotify) chat_finalized_cb,
148                            NULL);
149
150         exit_timeout_stop ();
151         chat_count++;
152
153         gossip_chat_present (GOSSIP_CHAT (chat));
154
155         g_object_unref (chat);
156         g_object_unref (account);
157
158 }
159
160 int
161 main (int argc, char *argv[])
162 {
163         EmpathyChandler *chandler;
164         GnomeProgram    *program;
165
166         bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
167         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
168         textdomain (GETTEXT_PACKAGE);
169
170         program = gnome_program_init ("empathy-chat",
171                                       PACKAGE_VERSION,
172                                       LIBGNOMEUI_MODULE,
173                                       argc, argv,
174                                       GNOME_PROGRAM_STANDARD_PROPERTIES,
175                                       GNOME_PARAM_HUMAN_READABLE_NAME, PACKAGE_NAME,
176                                       NULL);
177
178         gtk_window_set_default_icon_name ("empathy");
179         gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
180                                            DATADIR G_DIR_SEPARATOR_S "empathy");
181
182         if (g_getenv ("EMPATHY_DEBUG")) {
183                 debug_mode = TRUE;
184         }
185
186         //sexit_timeout_start ();
187         chandler = empathy_chandler_new (BUS_NAME, OBJECT_PATH);
188
189         g_signal_connect (chandler, "new-channel",
190                           G_CALLBACK (new_channel_cb),
191                           NULL);
192
193         gtk_main ();
194
195         g_object_unref (program);
196         g_object_unref (chandler);
197
198         return EXIT_SUCCESS;
199 }
200