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