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 <libtelepathy/tp-helpers.h>
33 #include <libmissioncontrol/mc-account.h>
35 #include <libempathy/gossip-contact.h>
36 #include <libempathy/gossip-debug.h>
37 #include <libempathy/empathy-chandler.h>
38 #include <libempathy/empathy-contact-manager.h>
39 #include <libempathy/empathy-contact-list.h>
40 #include <libempathy-gtk/gossip-private-chat.h>
42 #define DEBUG_DOMAIN "ChatMain"
44 #define BUS_NAME "org.gnome.Empathy.Chat"
45 #define OBJECT_PATH "/org/freedesktop/Telepathy/ChannelHandler"
47 /* Time to wait before exit, in seconds */
48 #define EXIT_TIMEOUT 5
51 static guint chat_count = 0;
52 static guint exit_timeout = 0;
56 exit_timeout_cb (gpointer user_data)
58 gossip_debug (DEBUG_DOMAIN, "Timeout, exiting");
66 exit_timeout_start (void)
72 exit_timeout = g_timeout_add (EXIT_TIMEOUT * 1000,
73 (GSourceFunc) exit_timeout_cb,
78 exit_timeout_stop (void)
81 gossip_debug (DEBUG_DOMAIN, "Exit timeout canceled");
82 g_source_remove (exit_timeout);
88 chat_finalized_cb (gpointer user_data,
92 if (chat_count == 0) {
93 gossip_debug (DEBUG_DOMAIN, "No more chat, start exit timeout");
94 exit_timeout_start ();
99 new_channel_cb (EmpathyChandler *chandler,
104 if (tp_chan->handle_type == TP_HANDLE_TYPE_CONTACT) {
107 EmpathyContactManager *manager;
108 EmpathyContactList *list;
109 GossipContact *contact;
110 GossipPrivateChat *chat;
112 /* We have a private chat channel */
113 mc = mission_control_new (tp_get_bus ());
114 account = mission_control_get_account_for_connection (mc, tp_conn, NULL);
115 manager = empathy_contact_manager_new ();
116 list = empathy_contact_manager_get_list (manager, account);
117 contact = empathy_contact_list_get_from_handle (list, tp_chan->handle);
119 chat = gossip_private_chat_new_with_channel (contact, tp_chan);
120 g_object_weak_ref (G_OBJECT (chat),
121 (GWeakNotify) chat_finalized_cb,
124 exit_timeout_stop ();
127 gossip_chat_present (GOSSIP_CHAT (chat));
130 g_object_unref (account);
131 g_object_unref (contact);
132 g_object_unref (chat);
133 g_object_unref (manager);
138 main (int argc, char *argv[])
140 EmpathyChandler *chandler;
142 gtk_init (&argc, &argv);
144 exit_timeout_start ();
145 chandler = empathy_chandler_new (BUS_NAME, OBJECT_PATH);
147 g_signal_connect (chandler, "new-channel",
148 G_CALLBACK (new_channel_cb),