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