]> git.0d.be Git - empathy.git/blob - src/empathy.c
Empathy is no more splitted into 2 processes. It makes debugging more
[empathy.git] / src / empathy.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 #include <libmissioncontrol/mc-account-monitor.h>
38 #include <libmissioncontrol/mission-control.h>
39
40 #include <libempathy/gossip-debug.h>
41 #include <libempathy/gossip-utils.h>
42 #include <libempathy/gossip-presence.h>
43 #include <libempathy/gossip-contact.h>
44 #include <libempathy/empathy-chandler.h>
45 #include <libempathy/empathy-tp-chat.h>
46 #include <libempathy-gtk/empathy-main-window.h>
47 #include <libempathy-gtk/empathy-status-icon.h>
48 #include <libempathy-gtk/gossip-private-chat.h>
49 #include <libempathy-gtk/gossip-group-chat.h>
50
51 #define DEBUG_DOMAIN "EmpathyMain"
52
53 #define BUS_NAME "org.gnome.Empathy.Chat"
54 #define OBJECT_PATH "/org/freedesktop/Telepathy/ChannelHandler"
55
56 static void
57 error_cb (MissionControl *mc,
58           GError         *error,
59           gpointer        data)
60 {
61         if (error) {
62                 gossip_debug (DEBUG_DOMAIN, "Error: %s", error->message);
63         }
64 }
65
66 static void
67 service_ended_cb (MissionControl *mc,
68                   gpointer        user_data)
69 {
70         gossip_debug (DEBUG_DOMAIN, "Mission Control stopped");
71 }
72
73 static void
74 operation_error_cb (MissionControl *mc,
75                     guint           operation_id,
76                     guint           error_code,
77                     gpointer        user_data)
78 {
79         gossip_debug (DEBUG_DOMAIN, "Error code %d during operation %d",
80                       error_code,
81                       operation_id);
82 }
83
84 static void
85 start_mission_control (MissionControl *mc)
86 {
87         McPresence presence;
88
89         presence = mission_control_get_presence_actual (mc, NULL);
90
91         if (presence > MC_PRESENCE_OFFLINE) {
92                 /* MC is already running and online, nothing to do */
93                 return;
94         }
95
96         gossip_debug (DEBUG_DOMAIN, "Starting Mission Control...");
97
98         mission_control_set_presence (mc,
99                                       MC_PRESENCE_AVAILABLE,
100                                       NULL,
101                                       (McCallback) error_cb,
102                                       NULL);
103 }
104
105 static void
106 account_enabled_cb (McAccountMonitor *monitor,
107                     gchar            *unique_name,
108                     MissionControl   *mc)
109 {
110         gossip_debug (DEBUG_DOMAIN, "Account enabled: %s", unique_name);
111         start_mission_control (mc);
112 }
113
114 static void
115 new_channel_cb (EmpathyChandler *chandler,
116                 TpConn          *tp_conn,
117                 TpChan          *tp_chan,
118                 MissionControl  *mc)
119 {
120         McAccount  *account;
121         GossipChat *chat;
122         gchar      *id;
123
124         account = mission_control_get_account_for_connection (mc, tp_conn, NULL);
125         id = gossip_get_channel_id (account, tp_chan);
126         chat = gossip_chat_window_find_chat (account, id);
127         g_free (id);
128
129         if (chat) {
130                 /* The chat already exists */
131                 if (!gossip_chat_is_connected (chat)) {
132                         EmpathyTpChat *tp_chat;
133
134                         /* The chat died, give him the new text channel */
135                         tp_chat = empathy_tp_chat_new (account, tp_chan);
136                         gossip_chat_set_tp_chat (chat, tp_chat);
137                         g_object_unref (tp_chat);
138                 }
139                 gossip_chat_present (chat);
140
141                 g_object_unref (account);
142                 return;
143         }
144
145         if (tp_chan->handle_type == TP_HANDLE_TYPE_CONTACT) {
146                 /* We have a new private chat channel */
147                 chat = GOSSIP_CHAT (gossip_private_chat_new (account, tp_chan));
148         }
149         else if (tp_chan->handle_type == TP_HANDLE_TYPE_ROOM) {
150                 /* We have a new group chat channel */
151                 chat = GOSSIP_CHAT (gossip_group_chat_new (account, tp_chan));
152         }
153
154         gossip_chat_present (GOSSIP_CHAT (chat));
155
156         g_object_unref (chat);
157         g_object_unref (account);
158 }
159
160 int
161 main (int argc, char *argv[])
162 {
163         EmpathyStatusIcon *icon;
164         GtkWidget         *window;
165         MissionControl    *mc;
166         McAccountMonitor  *monitor;
167         EmpathyChandler   *chandler;
168         GnomeProgram      *program;
169         gboolean           no_connect = FALSE;
170         GOptionContext    *context;
171         GOptionEntry       options[] = {
172                 { "no-connect", 'n',
173                   0, G_OPTION_ARG_NONE, &no_connect,
174                   N_("Don't connect on startup"),
175                   NULL },
176                 { NULL }
177         };
178
179         bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
180         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
181         textdomain (GETTEXT_PACKAGE);
182
183         context = g_option_context_new (_("- Empathy Instant Messenger"));
184         g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);
185
186         g_set_application_name (PACKAGE_NAME);
187
188         program = gnome_program_init ("empathy",
189                                       PACKAGE_VERSION,
190                                       LIBGNOMEUI_MODULE,
191                                       argc, argv,
192                                       GNOME_PROGRAM_STANDARD_PROPERTIES,
193                                       "goption-context", context,
194                                       GNOME_PARAM_HUMAN_READABLE_NAME, PACKAGE_NAME,
195                                       NULL);
196
197         gtk_window_set_default_icon_name ("empathy");
198         gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
199                                            DATADIR G_DIR_SEPARATOR_S "empathy");
200
201         /* Setting up MC */
202         monitor = mc_account_monitor_new ();
203         mc = gossip_mission_control_new ();
204         g_signal_connect (monitor, "account-enabled",
205                           G_CALLBACK (account_enabled_cb),
206                           mc);
207         g_signal_connect (mc, "ServiceEnded",
208                           G_CALLBACK (service_ended_cb),
209                           NULL);
210         g_signal_connect (mc, "Error",
211                           G_CALLBACK (operation_error_cb),
212                           NULL);
213
214         if (!no_connect) {
215                 start_mission_control (mc);
216         }
217
218         /* Setting up UI */
219         window = empathy_main_window_show ();
220         icon = empathy_status_icon_new (GTK_WINDOW (window));
221
222         /* Setting up channel handler  */
223         chandler = empathy_chandler_new (BUS_NAME, OBJECT_PATH);
224         g_signal_connect (chandler, "new-channel",
225                           G_CALLBACK (new_channel_cb),
226                           mc);
227
228         gtk_main ();
229
230         mission_control_set_presence (mc,
231                                       MC_PRESENCE_OFFLINE,
232                                       NULL, NULL, NULL);
233
234         g_object_unref (chandler);
235         g_object_unref (monitor);
236         g_object_unref (mc);
237         g_object_unref (icon);
238         g_object_unref (program);
239
240         return EXIT_SUCCESS;
241 }
242