]> git.0d.be Git - empathy.git/blob - src/empathy-chat-chandler.c
Revert "merge git work"
[empathy.git] / src / empathy-chat-chandler.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 <libgnomevfs/gnome-vfs.h>
32
33 #include <libmissioncontrol/mission-control.h>
34
35 #include <libempathy/empathy-chandler.h>
36 #include <libempathy/empathy-utils.h>
37 #include <libempathy/empathy-tp-chat.h>
38 #include <libempathy/empathy-tp-chatroom.h>
39 #include <libempathy/empathy-debug.h>
40
41 #include <libempathy-gtk/empathy-chat.h>
42 #include <libempathy-gtk/empathy-private-chat.h>
43 #include <libempathy-gtk/empathy-group-chat.h>
44 #include <libempathy-gtk/empathy-chat-window.h>
45
46 #define DEBUG_DOMAIN "EmpathyChat"
47
48 #define BUS_NAME "org.gnome.Empathy.ChatChandler"
49 #define OBJECT_PATH "/org/gnome/Empathy/ChatChandler"
50
51 static guint nb_chats = 0;
52
53 static void
54 chat_chandler_weak_notify (gpointer  data,
55                            GObject  *where_the_object_was)
56 {
57         nb_chats--;
58         if (nb_chats == 0) {
59                 empathy_debug (DEBUG_DOMAIN, "No more chats, leaving...");
60                 gtk_main_quit ();
61         }
62 }
63
64 static void
65 chat_chandler_new_channel_cb (EmpathyChandler *chandler,
66                               TpConn          *tp_conn,
67                               TpChan          *tp_chan,
68                               MissionControl  *mc)
69 {
70         EmpathyTpChat *tp_chat;
71         McAccount     *account;
72         EmpathyChat   *chat;
73         gchar         *id;
74
75         account = mission_control_get_account_for_connection (mc, tp_conn, NULL);
76         id = empathy_inspect_channel (account, tp_chan);
77         chat = empathy_chat_window_find_chat (account, id);
78         g_free (id);
79
80         if (chat) {
81                 /* The chat already exists */
82                 if (!empathy_chat_is_connected (chat)) {
83                         /* The chat died, give him the new text channel */
84                         if (empathy_chat_is_group_chat (chat)) {
85                                 tp_chat = EMPATHY_TP_CHAT (empathy_tp_chatroom_new (account, tp_chan));
86                         } else {
87                                 tp_chat = empathy_tp_chat_new (account, tp_chan);
88                         }
89                         empathy_chat_set_tp_chat (chat, tp_chat);
90                         g_object_unref (tp_chat);
91                 }
92                 empathy_chat_present (chat);
93
94                 g_object_unref (account);
95                 return;
96         }
97
98         if (tp_chan->handle_type == TP_HANDLE_TYPE_CONTACT) {
99                 /* We have a new private chat channel */
100                 tp_chat = empathy_tp_chat_new (account, tp_chan);
101                 chat = EMPATHY_CHAT (empathy_private_chat_new (tp_chat));
102         }
103         else if (tp_chan->handle_type == TP_HANDLE_TYPE_ROOM) {
104                 /* We have a new group chat channel */
105                 tp_chat = EMPATHY_TP_CHAT (empathy_tp_chatroom_new (account, tp_chan));
106                 chat = EMPATHY_CHAT (empathy_group_chat_new (EMPATHY_TP_CHATROOM (tp_chat)));
107         } else {
108                 empathy_debug (DEBUG_DOMAIN,
109                                "Unknown handle type (%d) for Text channel",
110                                tp_chan->handle_type);
111                 g_object_unref (account);
112                 return;
113         }
114
115         nb_chats++;
116         g_object_weak_ref (G_OBJECT (chat), chat_chandler_weak_notify, NULL);
117         empathy_chat_present (chat);
118
119         g_object_unref (chat);
120         g_object_unref (account);
121         g_object_unref (tp_chat);
122 }
123
124 int
125 main (int argc, char *argv[])
126 {
127         EmpathyChandler *chandler;
128         MissionControl  *mc;
129
130         empathy_debug_set_log_file_from_env ();
131
132         bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
133         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
134         textdomain (GETTEXT_PACKAGE);
135
136         gtk_init (&argc, &argv);
137
138         gtk_window_set_default_icon_name ("empathy");
139         gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
140                                            PKGDATADIR G_DIR_SEPARATOR_S "icons");
141         gnome_vfs_init ();
142
143         mc = empathy_mission_control_new ();
144         chandler = empathy_chandler_new (BUS_NAME, OBJECT_PATH);
145         g_signal_connect (chandler, "new-channel",
146                           G_CALLBACK (chat_chandler_new_channel_cb),
147                           mc);
148
149         empathy_debug (DEBUG_DOMAIN, "Ready to handle new text channels");
150
151         gtk_main ();
152
153         g_object_unref (chandler);
154         g_object_unref (mc);
155
156         return EXIT_SUCCESS;
157 }
158