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>
30 #include <libtelepathy/tp-conn.h>
31 #include <libtelepathy/tp-chan.h>
32 #include <libmissioncontrol/mc-account.h>
34 #include <libempathy/gossip-contact.h>
35 #include <libempathy/gossip-debug.h>
36 #include <libempathy/gossip-utils.h>
37 #include <libempathy/empathy-chandler.h>
38 #include <libempathy/empathy-contact-manager.h>
39 #include <libempathy/empathy-contact-list.h>
40 #include <libempathy/empathy-tp-chat.h>
41 #include <libempathy-gtk/gossip-private-chat.h>
43 #define DEBUG_DOMAIN "ChatMain"
45 #define BUS_NAME "org.gnome.Empathy.Chat"
46 #define OBJECT_PATH "/org/freedesktop/Telepathy/ChannelHandler"
48 /* Time to wait before exit, in seconds */
49 #define EXIT_TIMEOUT 5
52 static guint chat_count = 0;
53 static guint exit_timeout = 0;
57 exit_timeout_cb (gpointer user_data)
59 gossip_debug (DEBUG_DOMAIN, "Timeout, exiting");
67 exit_timeout_start (void)
73 exit_timeout = g_timeout_add (EXIT_TIMEOUT * 1000,
74 (GSourceFunc) exit_timeout_cb,
79 exit_timeout_stop (void)
82 gossip_debug (DEBUG_DOMAIN, "Exit timeout canceled");
83 g_source_remove (exit_timeout);
89 chat_finalized_cb (gpointer user_data,
93 if (chat_count == 0) {
94 gossip_debug (DEBUG_DOMAIN, "No more chat, start exit timeout");
95 exit_timeout_start ();
100 new_channel_cb (EmpathyChandler *chandler,
110 mc = gossip_mission_control_new ();
111 account = mission_control_get_account_for_connection (mc, tp_conn, NULL);
112 id = empathy_tp_chat_build_id (account, tp_chan);
114 chat = gossip_chat_window_find_chat_by_id (id);
116 /* The chat already exists */
117 if (!gossip_chat_is_connected (chat)) {
118 EmpathyTpChat *tp_chat;
120 /* The chat died, give him the new text channel */
121 tp_chat = empathy_tp_chat_new (account, tp_chan);
122 gossip_chat_set_tp_chat (chat, tp_chat);
123 g_object_unref (tp_chat);
125 gossip_chat_present (chat);
130 if (tp_chan->handle_type == TP_HANDLE_TYPE_CONTACT) {
131 EmpathyContactManager *manager;
132 EmpathyContactList *list;
133 GossipContact *contact;
134 GossipPrivateChat *chat;
136 /* We have a new private chat channel */
137 manager = empathy_contact_manager_new ();
138 list = empathy_contact_manager_get_list (manager, account);
139 contact = empathy_contact_list_get_from_handle (list, tp_chan->handle);
141 chat = gossip_private_chat_new_with_channel (contact, tp_chan);
142 g_object_weak_ref (G_OBJECT (chat),
143 (GWeakNotify) chat_finalized_cb,
146 exit_timeout_stop ();
149 gossip_chat_present (GOSSIP_CHAT (chat));
151 g_object_unref (contact);
152 g_object_unref (chat);
153 g_object_unref (manager);
158 g_object_unref (account);
163 main (int argc, char *argv[])
165 EmpathyChandler *chandler;
167 gtk_init (&argc, &argv);
169 exit_timeout_start ();
170 chandler = empathy_chandler_new (BUS_NAME, OBJECT_PATH);
172 g_signal_connect (chandler, "new-channel",
173 G_CALLBACK (new_channel_cb),