]> git.0d.be Git - empathy.git/blob - src/empathy-main.c
0804fe8177162c234df4b9931a02ec3281bc401b
[empathy.git] / src / empathy-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 <libmissioncontrol/mc-account.h>
35 #include <libmissioncontrol/mc-account-monitor.h>
36 #include <libmissioncontrol/mission-control.h>
37
38 #include <libempathy/gossip-debug.h>
39 #include <libempathy/gossip-utils.h>
40 #include <libempathy/gossip-presence.h>
41 #include <libempathy/gossip-paths.h>
42 #include <libempathy-gtk/empathy-main-window.h>
43 #include <libempathy-gtk/empathy-status-icon.h>
44
45 #include "empathy-filter.h"
46
47 #define DEBUG_DOMAIN "EmpathyMain"
48
49 static void error_cb              (MissionControl *mc,
50                                    GError         *error,
51                                    gpointer        data);
52 static void service_ended_cb      (MissionControl *mc,
53                                    gpointer        user_data);
54 static void operation_error_cb    (MissionControl *mc,
55                                    guint           operation_id,
56                                    guint           error_code,
57                                    gpointer        user_data);
58 static void start_mission_control (MissionControl *mc);
59
60 static void
61 error_cb (MissionControl *mc,
62           GError         *error,
63           gpointer        data)
64 {
65         if (error) {
66                 gossip_debug (DEBUG_DOMAIN, "Error: %s", error->message);
67         }
68 }
69
70 static void
71 service_ended_cb (MissionControl *mc,
72                   gpointer        user_data)
73 {
74         gossip_debug (DEBUG_DOMAIN, "Mission Control stopped");
75 }
76
77 static void
78 operation_error_cb (MissionControl *mc,
79                     guint           operation_id,
80                     guint           error_code,
81                     gpointer        user_data)
82 {
83         gossip_debug (DEBUG_DOMAIN, "Error code %d during operation %d",
84                       error_code,
85                       operation_id);
86 }
87
88 static void
89 account_enabled_cb (McAccountMonitor *monitor,
90                     gchar            *unique_name,
91                     MissionControl   *mc)
92 {
93         gossip_debug (DEBUG_DOMAIN, "Account enabled: %s", unique_name);
94         start_mission_control (mc);
95 }
96
97 static void
98 start_mission_control (MissionControl *mc)
99 {
100         McPresence presence;
101
102         presence = mission_control_get_presence_actual (mc, NULL);
103
104         if (presence > MC_PRESENCE_OFFLINE) {
105                 /* MC is already running and online, nothing to do */
106                 return;
107         }
108
109         gossip_debug (DEBUG_DOMAIN, "Starting Mission Control...");
110
111         mission_control_set_presence (mc,
112                                       MC_PRESENCE_AVAILABLE,
113                                       NULL,
114                                       (McCallback) error_cb,
115                                       NULL);
116 }
117
118 static void
119 new_channel_cb (EmpathyFilter *filter,
120                 TpConn        *tp_conn,
121                 TpChan        *tp_chan,
122                 guint          context_handle,
123                 gpointer       user_data)
124 {
125         gossip_debug (DEBUG_DOMAIN, "Filtering context handle: %d",
126                       context_handle);
127         empathy_filter_process (filter, context_handle, TRUE);
128 }
129
130 int
131 main (int argc, char *argv[])
132 {
133         EmpathyStatusIcon *icon;
134         GtkWidget         *window;
135         MissionControl    *mc;
136         McAccountMonitor  *monitor;
137         EmpathyFilter     *filter;
138         gchar             *localedir;
139         GnomeProgram      *program;
140         gboolean           no_connect = FALSE;
141         GOptionContext    *context;
142         GOptionEntry       options[] = {
143                 { "no-connect", 'n',
144                   0, G_OPTION_ARG_NONE, &no_connect,
145                   N_("Don't connect on startup"),
146                   NULL },
147         };
148
149         localedir = gossip_paths_get_locale_path ();
150         bindtextdomain (GETTEXT_PACKAGE, localedir);
151         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
152         textdomain (GETTEXT_PACKAGE);
153         g_free (localedir);
154
155         context = g_option_context_new (_("- Empathy Instant Messenger"));
156         g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);
157
158         g_set_application_name (PACKAGE_NAME);
159
160         program = gnome_program_init ("empathy",
161                                       PACKAGE_VERSION,
162                                       LIBGNOMEUI_MODULE,
163                                       argc, argv,
164                                       GNOME_PROGRAM_STANDARD_PROPERTIES,
165                                       "goption-context", context,
166                                       GNOME_PARAM_HUMAN_READABLE_NAME, PACKAGE_NAME,
167                                       NULL);
168
169         /* Setting up channel filter */
170         filter = empathy_filter_new ();
171         g_signal_connect (filter, "new-channel",
172                           G_CALLBACK (new_channel_cb),
173                           NULL);
174
175         /* Setting up MC */
176         monitor = mc_account_monitor_new ();
177         mc = gossip_mission_control_new ();
178         g_signal_connect (monitor, "account-enabled",
179                           G_CALLBACK (account_enabled_cb),
180                           mc);
181         g_signal_connect (mc, "ServiceEnded",
182                           G_CALLBACK (service_ended_cb),
183                           NULL);
184         g_signal_connect (mc, "Error",
185                           G_CALLBACK (operation_error_cb),
186                           NULL);
187
188         if (!no_connect) {
189                 start_mission_control (mc);
190         }
191
192         /* Setting up UI */
193         window = empathy_main_window_show ();
194         icon = empathy_status_icon_new (GTK_WINDOW (window));
195
196         gtk_main ();
197
198         mission_control_set_presence (mc,
199                                       MC_PRESENCE_OFFLINE,
200                                       NULL, NULL, NULL);
201
202         g_object_unref (monitor);
203         g_object_unref (mc);
204         g_object_unref (icon);
205         g_object_unref (program);
206
207         return EXIT_SUCCESS;
208 }
209