]> git.0d.be Git - empathy.git/blob - src/empathy-chat-main.c
When a new text channel arrives, check if there is no existing GossipChat
[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 <gtk/gtk.h>
29
30 #include <libtelepathy/tp-conn.h>
31 #include <libtelepathy/tp-chan.h>
32 #include <libmissioncontrol/mc-account.h>
33
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>
42
43 #define DEBUG_DOMAIN "ChatMain"
44
45 #define BUS_NAME "org.gnome.Empathy.Chat"
46 #define OBJECT_PATH "/org/freedesktop/Telepathy/ChannelHandler"
47
48 /* Time to wait before exit, in seconds */
49 #define EXIT_TIMEOUT 5
50
51
52 static guint chat_count = 0;
53 static guint exit_timeout = 0;
54
55
56 static gboolean
57 exit_timeout_cb (gpointer user_data)
58 {
59         gossip_debug (DEBUG_DOMAIN, "Timeout, exiting");
60
61         gtk_main_quit ();
62
63         return FALSE;
64 }
65
66 static void
67 exit_timeout_start (void)
68 {
69         if (exit_timeout) {
70                 return;
71         }
72
73         exit_timeout = g_timeout_add (EXIT_TIMEOUT * 1000,
74                                       (GSourceFunc) exit_timeout_cb,
75                                       NULL);
76 }
77
78 static void
79 exit_timeout_stop (void)
80 {
81         if (exit_timeout) {
82                 gossip_debug (DEBUG_DOMAIN, "Exit timeout canceled");
83                 g_source_remove (exit_timeout);
84                 exit_timeout = 0;
85         }
86 }
87
88 static void
89 chat_finalized_cb (gpointer    user_data,
90                    GossipChat *chat)
91 {
92         chat_count--;
93         if (chat_count == 0) {
94                 gossip_debug (DEBUG_DOMAIN, "No more chat, start exit timeout");
95                 exit_timeout_start ();
96         }
97 }
98
99 static void
100 new_channel_cb (EmpathyChandler *chandler,
101                 TpConn          *tp_conn,
102                 TpChan          *tp_chan,
103                 gpointer         user_data)
104 {
105         MissionControl *mc;
106         McAccount      *account;
107         GossipChat     *chat;
108         gchar          *id;
109
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);
113
114         chat = gossip_chat_window_find_chat_by_id (id);
115         if (chat) {
116                 /* The chat already exists */
117                 if (!gossip_chat_is_connected (chat)) {
118                         EmpathyTpChat *tp_chat;
119
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);
124                 }
125                 gossip_chat_present (chat);
126
127                 goto OUT;
128         }
129
130         if (tp_chan->handle_type == TP_HANDLE_TYPE_CONTACT) {
131                 EmpathyContactManager *manager;
132                 EmpathyContactList    *list;
133                 GossipContact         *contact;
134                 GossipPrivateChat     *chat;
135
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);
140
141                 chat = gossip_private_chat_new_with_channel (contact, tp_chan);
142                 g_object_weak_ref (G_OBJECT (chat),
143                                    (GWeakNotify) chat_finalized_cb,
144                                    NULL);
145
146                 exit_timeout_stop ();
147                 chat_count++;
148
149                 gossip_chat_present (GOSSIP_CHAT (chat));
150
151                 g_object_unref (contact);
152                 g_object_unref (chat);
153                 g_object_unref (manager);
154         }
155
156 OUT:
157         g_free (id);
158         g_object_unref (account);
159         g_object_unref (mc);
160 }
161
162 int
163 main (int argc, char *argv[])
164 {
165         EmpathyChandler *chandler;
166
167         gtk_init (&argc, &argv);
168
169         exit_timeout_start ();
170         chandler = empathy_chandler_new (BUS_NAME, OBJECT_PATH);
171
172         g_signal_connect (chandler, "new-channel",
173                           G_CALLBACK (new_channel_cb),
174                           NULL);
175
176         gtk_main ();
177
178         return EXIT_SUCCESS;
179 }
180