1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007 Collabora Ltd.
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.
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.
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.
20 * Authors: Xavier Claessens <xclaesse@gmail.com>
28 #include <glib/gi18n.h>
31 #include <libgnome/gnome-program.h>
32 #include <libgnomeui/gnome-ui-init.h>
34 #include <libtelepathy/tp-conn.h>
35 #include <libtelepathy/tp-chan.h>
36 #include <libmissioncontrol/mc-account.h>
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>
48 #define DEBUG_DOMAIN "ChatMain"
50 #define BUS_NAME "org.gnome.Empathy.Chat"
51 #define OBJECT_PATH "/org/freedesktop/Telepathy/ChannelHandler"
53 /* Time to wait before exit, in seconds */
54 #define EXIT_TIMEOUT 5
57 static guint chat_count = 0;
58 static guint exit_timeout = 0;
59 static gboolean debug_mode = FALSE;
62 exit_timeout_cb (gpointer user_data)
64 gossip_debug (DEBUG_DOMAIN, "Timeout, exiting");
72 exit_timeout_start (void)
74 if (exit_timeout || debug_mode) {
78 exit_timeout = g_timeout_add (EXIT_TIMEOUT * 1000,
79 (GSourceFunc) exit_timeout_cb,
84 exit_timeout_stop (void)
87 gossip_debug (DEBUG_DOMAIN, "Exit timeout canceled");
88 g_source_remove (exit_timeout);
94 chat_finalized_cb (gpointer user_data,
98 if (chat_count == 0) {
99 gossip_debug (DEBUG_DOMAIN, "No more chat, start exit timeout");
100 exit_timeout_start ();
105 new_channel_cb (EmpathyChandler *chandler,
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);
119 chat = gossip_chat_window_find_chat_by_id (id);
121 /* The chat already exists */
122 if (!gossip_chat_is_connected (chat)) {
123 EmpathyTpChat *tp_chat;
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);
130 gossip_chat_present (chat);
132 else if (tp_chan->handle_type == TP_HANDLE_TYPE_CONTACT) {
133 EmpathyContactManager *manager;
134 EmpathyTpContactList *list;
135 GossipContact *contact;
136 GossipPrivateChat *chat;
138 /* We have a new private chat channel */
139 manager = empathy_contact_manager_new ();
140 list = empathy_contact_manager_get_list (manager, account);
141 contact = empathy_tp_contact_list_get_from_handle (list, tp_chan->handle);
143 chat = gossip_private_chat_new_with_channel (contact, tp_chan);
144 g_object_weak_ref (G_OBJECT (chat),
145 (GWeakNotify) chat_finalized_cb,
148 exit_timeout_stop ();
151 gossip_chat_present (GOSSIP_CHAT (chat));
153 g_object_unref (contact);
154 g_object_unref (chat);
155 g_object_unref (manager);
157 else if (tp_chan->handle_type == TP_HANDLE_TYPE_ROOM) {
159 GossipGroupChat *chat;
161 /* We have a new group chat channel */
162 chat = gossip_group_chat_new (account, tp_chan);
163 g_object_weak_ref (G_OBJECT (chat),
164 (GWeakNotify) chat_finalized_cb,
167 exit_timeout_stop ();
170 gossip_chat_present (GOSSIP_CHAT (chat));
172 g_object_unref (chat);
177 g_object_unref (account);
182 main (int argc, char *argv[])
184 EmpathyChandler *chandler;
185 GnomeProgram *program;
188 localedir = gossip_paths_get_locale_path ();
189 bindtextdomain (GETTEXT_PACKAGE, localedir);
190 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
191 textdomain (GETTEXT_PACKAGE);
194 program = gnome_program_init ("empathy-chat",
198 GNOME_PROGRAM_STANDARD_PROPERTIES,
199 GNOME_PARAM_HUMAN_READABLE_NAME, PACKAGE_NAME,
202 gtk_window_set_default_icon_name ("empathy");
204 if (g_getenv ("EMPATHY_DEBUG")) {
208 exit_timeout_start ();
209 chandler = empathy_chandler_new (BUS_NAME, OBJECT_PATH);
211 g_signal_connect (chandler, "new-channel",
212 G_CALLBACK (new_channel_cb),
217 g_object_unref (program);
218 g_object_unref (chandler);