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