]> git.0d.be Git - empathy.git/blob - src/empathy-chat-main.c
7750b0b3f1aab40aec6fb7bf25e82641f17e1909
[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 <glib/gi18n.h>
29 #include <gtk/gtk.h>
30
31 #include <libgnome/gnome-program.h>
32 #include <libgnomeui/gnome-ui-init.h>
33
34 #include <libtelepathy/tp-conn.h>
35 #include <libtelepathy/tp-chan.h>
36 #include <libmissioncontrol/mc-account.h>
37
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-contact-manager.h>
43 #include <libempathy/empathy-contact-list.h>
44 #include <libempathy/empathy-tp-chat.h>
45 #include <libempathy/gossip-paths.h>
46 #include <libempathy-gtk/gossip-private-chat.h>
47
48 #define DEBUG_DOMAIN "ChatMain"
49
50 #define BUS_NAME "org.gnome.Empathy.Chat"
51 #define OBJECT_PATH "/org/freedesktop/Telepathy/ChannelHandler"
52
53 /* Time to wait before exit, in seconds */
54 #define EXIT_TIMEOUT 5
55
56
57 static guint    chat_count = 0;
58 static guint    exit_timeout = 0;
59 static gboolean debug_mode = FALSE;
60
61 static gboolean
62 exit_timeout_cb (gpointer user_data)
63 {
64         gossip_debug (DEBUG_DOMAIN, "Timeout, exiting");
65
66         gtk_main_quit ();
67
68         return FALSE;
69 }
70
71 static void
72 exit_timeout_start (void)
73 {
74         if (exit_timeout || debug_mode) {
75                 return;
76         }
77
78         exit_timeout = g_timeout_add (EXIT_TIMEOUT * 1000,
79                                       (GSourceFunc) exit_timeout_cb,
80                                       NULL);
81 }
82
83 static void
84 exit_timeout_stop (void)
85 {
86         if (exit_timeout) {
87                 gossip_debug (DEBUG_DOMAIN, "Exit timeout canceled");
88                 g_source_remove (exit_timeout);
89                 exit_timeout = 0;
90         }
91 }
92
93 static void
94 chat_finalized_cb (gpointer    user_data,
95                    GossipChat *chat)
96 {
97         chat_count--;
98         if (chat_count == 0) {
99                 gossip_debug (DEBUG_DOMAIN, "No more chat, start exit timeout");
100                 exit_timeout_start ();
101         }
102 }
103
104 static void
105 new_channel_cb (EmpathyChandler *chandler,
106                 TpConn          *tp_conn,
107                 TpChan          *tp_chan,
108                 gpointer         user_data)
109 {
110         MissionControl *mc;
111         McAccount      *account;
112         GossipChat     *chat;
113         gchar          *id;
114
115         mc = gossip_mission_control_new ();
116         account = mission_control_get_account_for_connection (mc, tp_conn, NULL);
117         id = empathy_tp_chat_build_id (account, tp_chan);
118
119         chat = gossip_chat_window_find_chat_by_id (id);
120         if (chat) {
121                 /* The chat already exists */
122                 if (!gossip_chat_is_connected (chat)) {
123                         EmpathyTpChat *tp_chat;
124
125                         /* The chat died, give him the new text channel */
126                         tp_chat = empathy_tp_chat_new (account, tp_chan);
127                         gossip_chat_set_tp_chat (chat, tp_chat);
128                         g_object_unref (tp_chat);
129                 }
130                 gossip_chat_present (chat);
131
132                 goto OUT;
133         }
134
135         if (tp_chan->handle_type == TP_HANDLE_TYPE_CONTACT) {
136                 EmpathyContactManager *manager;
137                 EmpathyContactList    *list;
138                 GossipContact         *contact;
139                 GossipPrivateChat     *chat;
140
141                 /* We have a new private chat channel */
142                 manager = empathy_contact_manager_new ();
143                 list = empathy_contact_manager_get_list (manager, account);
144                 contact = empathy_contact_list_get_from_handle (list, tp_chan->handle);
145
146                 chat = gossip_private_chat_new_with_channel (contact, tp_chan);
147                 g_object_weak_ref (G_OBJECT (chat),
148                                    (GWeakNotify) chat_finalized_cb,
149                                    NULL);
150
151                 exit_timeout_stop ();
152                 chat_count++;
153
154                 gossip_chat_present (GOSSIP_CHAT (chat));
155
156                 g_object_unref (contact);
157                 g_object_unref (chat);
158                 g_object_unref (manager);
159         }
160
161 OUT:
162         g_free (id);
163         g_object_unref (account);
164         g_object_unref (mc);
165 }
166
167 int
168 main (int argc, char *argv[])
169 {
170         EmpathyChandler *chandler;
171         GnomeProgram    *program;
172         gchar           *localedir;
173
174         localedir = gossip_paths_get_locale_path ();
175         bindtextdomain (GETTEXT_PACKAGE, localedir);
176         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
177         textdomain (GETTEXT_PACKAGE);
178         g_free (localedir);
179
180         program = gnome_program_init ("empathy-chat",
181                                       PACKAGE_VERSION,
182                                       LIBGNOMEUI_MODULE,
183                                       argc, argv,
184                                       GNOME_PROGRAM_STANDARD_PROPERTIES,
185                                       GNOME_PARAM_HUMAN_READABLE_NAME, PACKAGE_NAME,
186                                       NULL);
187
188         gtk_window_set_default_icon_name ("empathy");
189
190         if (g_getenv ("EMPATHY_DEBUG")) {
191                 debug_mode = TRUE;
192         }
193
194         exit_timeout_start ();
195         chandler = empathy_chandler_new (BUS_NAME, OBJECT_PATH);
196
197         g_signal_connect (chandler, "new-channel",
198                           G_CALLBACK (new_channel_cb),
199                           NULL);
200
201         gtk_main ();
202
203         g_object_unref (program);
204         g_object_unref (chandler);
205
206         return EXIT_SUCCESS;
207 }
208