]> git.0d.be Git - empathy.git/blob - src/empathy-chat-main.c
[darcs-to-svn @ gossip_mission_control_new() returns a MissionControl sigleton object...
[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-gtk/gossip-private-chat.h>
41
42 #define DEBUG_DOMAIN "ChatMain"
43
44 #define BUS_NAME "org.gnome.Empathy.Chat"
45 #define OBJECT_PATH "/org/freedesktop/Telepathy/ChannelHandler"
46
47 /* Time to wait before exit, in seconds */
48 #define EXIT_TIMEOUT 5
49
50
51 static guint chat_count = 0;
52 static guint exit_timeout = 0;
53
54
55 static gboolean
56 exit_timeout_cb (gpointer user_data)
57 {
58         gossip_debug (DEBUG_DOMAIN, "Timeout, exiting");
59
60         gtk_main_quit ();
61
62         return FALSE;
63 }
64
65 static void
66 exit_timeout_start (void)
67 {
68         if (exit_timeout) {
69                 return;
70         }
71
72         exit_timeout = g_timeout_add (EXIT_TIMEOUT * 1000,
73                                       (GSourceFunc) exit_timeout_cb,
74                                       NULL);
75 }
76
77 static void
78 exit_timeout_stop (void)
79 {
80         if (exit_timeout) {
81                 gossip_debug (DEBUG_DOMAIN, "Exit timeout canceled");
82                 g_source_remove (exit_timeout);
83                 exit_timeout = 0;
84         }
85 }
86
87 static void
88 chat_finalized_cb (gpointer    user_data,
89                    GossipChat *chat)
90 {
91         chat_count--;
92         if (chat_count == 0) {
93                 gossip_debug (DEBUG_DOMAIN, "No more chat, start exit timeout");
94                 exit_timeout_start ();
95         }
96 }
97
98 static void
99 new_channel_cb (EmpathyChandler *chandler,
100                 TpConn          *tp_conn,
101                 TpChan          *tp_chan,
102                 gpointer         user_data)
103 {
104         if (tp_chan->handle_type == TP_HANDLE_TYPE_CONTACT) {
105                 MissionControl        *mc;
106                 McAccount             *account;
107                 EmpathyContactManager *manager;
108                 EmpathyContactList    *list;
109                 GossipContact         *contact;
110                 GossipPrivateChat     *chat;
111
112                 /* We have a private chat channel */
113                 mc = gossip_mission_control_new ();
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);
118
119                 chat = gossip_private_chat_new_with_channel (contact, tp_chan);
120                 g_object_weak_ref (G_OBJECT (chat),
121                                    (GWeakNotify) chat_finalized_cb,
122                                    NULL);
123
124                 exit_timeout_stop ();
125                 chat_count++;
126
127                 gossip_chat_present (GOSSIP_CHAT (chat));
128
129                 g_object_unref (mc);
130                 g_object_unref (account);
131                 g_object_unref (contact);
132                 g_object_unref (chat);
133                 g_object_unref (manager);
134         }
135 }
136
137 int
138 main (int argc, char *argv[])
139 {
140         EmpathyChandler *chandler;
141
142         gtk_init (&argc, &argv);
143
144         exit_timeout_start ();
145         chandler = empathy_chandler_new (BUS_NAME, OBJECT_PATH);
146
147         g_signal_connect (chandler, "new-channel",
148                           G_CALLBACK (new_channel_cb),
149                           NULL);
150
151         gtk_main ();
152
153         return EXIT_SUCCESS;
154 }
155