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-tp-chat.h>
43 #include <libempathy-gtk/gossip-private-chat.h>
44 #include <libempathy-gtk/gossip-group-chat.h>
46 #define DEBUG_DOMAIN "ChatMain"
48 #define BUS_NAME "org.gnome.Empathy.Chat"
49 #define OBJECT_PATH "/org/freedesktop/Telepathy/ChannelHandler"
51 /* Time to wait before exit, in seconds */
52 #define EXIT_TIMEOUT 5
55 static guint chat_count = 0;
56 static guint exit_timeout = 0;
57 static gboolean debug_mode = FALSE;
60 exit_timeout_cb (gpointer user_data)
62 gossip_debug (DEBUG_DOMAIN, "Timeout, exiting");
70 exit_timeout_start (void)
72 if (exit_timeout || debug_mode) {
76 exit_timeout = g_timeout_add (EXIT_TIMEOUT * 1000,
77 (GSourceFunc) exit_timeout_cb,
82 exit_timeout_stop (void)
85 gossip_debug (DEBUG_DOMAIN, "Exit timeout canceled");
86 g_source_remove (exit_timeout);
92 chat_finalized_cb (gpointer user_data,
96 if (chat_count == 0) {
97 gossip_debug (DEBUG_DOMAIN, "No more chat, start exit timeout");
98 exit_timeout_start ();
103 new_channel_cb (EmpathyChandler *chandler,
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);
122 /* The chat already exists */
123 if (!gossip_chat_is_connected (chat)) {
124 EmpathyTpChat *tp_chat;
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);
131 gossip_chat_present (chat);
133 g_object_unref (account);
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));
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));
146 g_object_weak_ref (G_OBJECT (chat),
147 (GWeakNotify) chat_finalized_cb,
150 exit_timeout_stop ();
153 gossip_chat_present (GOSSIP_CHAT (chat));
155 g_object_unref (chat);
156 g_object_unref (account);
161 main (int argc, char *argv[])
163 EmpathyChandler *chandler;
164 GnomeProgram *program;
166 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
167 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
168 textdomain (GETTEXT_PACKAGE);
170 program = gnome_program_init ("empathy-chat",
174 GNOME_PROGRAM_STANDARD_PROPERTIES,
175 GNOME_PARAM_HUMAN_READABLE_NAME, PACKAGE_NAME,
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");
182 if (g_getenv ("EMPATHY_DEBUG")) {
186 //sexit_timeout_start ();
187 chandler = empathy_chandler_new (BUS_NAME, OBJECT_PATH);
189 g_signal_connect (chandler, "new-channel",
190 G_CALLBACK (new_channel_cb),
195 g_object_unref (program);
196 g_object_unref (chandler);