]> git.0d.be Git - empathy.git/blob - src/empathy-chat-main.c
Fix warning when selecting all accounts.
[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/gossip-paths.h>
44 #include <libempathy-gtk/gossip-private-chat.h>
45 #include <libempathy-gtk/gossip-group-chat.h>
46
47 #define DEBUG_DOMAIN "ChatMain"
48
49 #define BUS_NAME "org.gnome.Empathy.Chat"
50 #define OBJECT_PATH "/org/freedesktop/Telepathy/ChannelHandler"
51
52 /* Time to wait before exit, in seconds */
53 #define EXIT_TIMEOUT 5
54
55
56 static guint    chat_count = 0;
57 static guint    exit_timeout = 0;
58 static gboolean debug_mode = FALSE;
59
60 static gboolean
61 exit_timeout_cb (gpointer user_data)
62 {
63         gossip_debug (DEBUG_DOMAIN, "Timeout, exiting");
64
65         gtk_main_quit ();
66
67         return FALSE;
68 }
69
70 static void
71 exit_timeout_start (void)
72 {
73         if (exit_timeout || debug_mode) {
74                 return;
75         }
76
77         exit_timeout = g_timeout_add (EXIT_TIMEOUT * 1000,
78                                       (GSourceFunc) exit_timeout_cb,
79                                       NULL);
80 }
81
82 static void
83 exit_timeout_stop (void)
84 {
85         if (exit_timeout) {
86                 gossip_debug (DEBUG_DOMAIN, "Exit timeout canceled");
87                 g_source_remove (exit_timeout);
88                 exit_timeout = 0;
89         }
90 }
91
92 static void
93 chat_finalized_cb (gpointer    user_data,
94                    GossipChat *chat)
95 {
96         chat_count--;
97         if (chat_count == 0) {
98                 gossip_debug (DEBUG_DOMAIN, "No more chat, start exit timeout");
99                 exit_timeout_start ();
100         }
101 }
102
103 static void
104 new_channel_cb (EmpathyChandler *chandler,
105                 TpConn          *tp_conn,
106                 TpChan          *tp_chan,
107                 gpointer         user_data)
108 {
109         MissionControl *mc;
110         McAccount      *account;
111         GossipChat     *chat;
112         gchar          *id;
113
114         mc = gossip_mission_control_new ();
115         account = mission_control_get_account_for_connection (mc, tp_conn, NULL);
116         id = gossip_get_channel_id (account, tp_chan);
117         chat = gossip_chat_window_find_chat (account, id);
118
119         g_free (id);
120         g_object_unref (mc);
121
122         if (chat) {
123                 /* The chat already exists */
124                 if (!gossip_chat_is_connected (chat)) {
125                         EmpathyTpChat *tp_chat;
126
127                         /* The chat died, give him the new text channel */
128                         tp_chat = empathy_tp_chat_new (account, tp_chan);
129                         gossip_chat_set_tp_chat (chat, tp_chat);
130                         g_object_unref (tp_chat);
131                 }
132                 gossip_chat_present (chat);
133
134                 g_object_unref (account);
135                 return;
136         }
137
138         if (tp_chan->handle_type == TP_HANDLE_TYPE_CONTACT) {
139                 /* We have a new private chat channel */
140                 chat = GOSSIP_CHAT (gossip_private_chat_new (account, tp_chan));
141         }
142         else if (tp_chan->handle_type == TP_HANDLE_TYPE_ROOM) {
143                 /* We have a new group chat channel */
144                 chat = GOSSIP_CHAT (gossip_group_chat_new (account, tp_chan));
145         }
146
147         g_object_weak_ref (G_OBJECT (chat),
148                            (GWeakNotify) chat_finalized_cb,
149                            NULL);
150
151         exit_timeout_stop ();
152         chat_count++;
153
154         gossip_chat_present (GOSSIP_CHAT (chat));
155
156         g_object_unref (chat);
157         g_object_unref (account);
158
159 }
160
161 int
162 main (int argc, char *argv[])
163 {
164         EmpathyChandler *chandler;
165         GnomeProgram    *program;
166         gchar           *localedir;
167
168         localedir = gossip_paths_get_locale_path ();
169         bindtextdomain (GETTEXT_PACKAGE, localedir);
170         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
171         textdomain (GETTEXT_PACKAGE);
172         g_free (localedir);
173
174         program = gnome_program_init ("empathy-chat",
175                                       PACKAGE_VERSION,
176                                       LIBGNOMEUI_MODULE,
177                                       argc, argv,
178                                       GNOME_PROGRAM_STANDARD_PROPERTIES,
179                                       GNOME_PARAM_HUMAN_READABLE_NAME, PACKAGE_NAME,
180                                       NULL);
181
182         gtk_window_set_default_icon_name ("empathy");
183
184         if (g_getenv ("EMPATHY_DEBUG")) {
185                 debug_mode = TRUE;
186         }
187
188         //sexit_timeout_start ();
189         chandler = empathy_chandler_new (BUS_NAME, OBJECT_PATH);
190
191         g_signal_connect (chandler, "new-channel",
192                           G_CALLBACK (new_channel_cb),
193                           NULL);
194
195         gtk_main ();
196
197         g_object_unref (program);
198         g_object_unref (chandler);
199
200         return EXIT_SUCCESS;
201 }
202