[darcs-to-svn @ connect to the error signal on MC]
[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 <gtk/gtk.h>
29
30 #include <libtelepathy/tp-helpers.h>
31
32 #include <libmissioncontrol/mc-account.h>
33 #include <libmissioncontrol/mc-account-monitor.h>
34 #include <libmissioncontrol/mission-control.h>
35
36 #include <libempathy/gossip-debug.h>
37 #include <libempathy/gossip-presence.h>
38 #include <libempathy-gtk/empathy-main-window.h>
39 #include <libempathy-gtk/empathy-status-icon.h>
40 #include <libempathy-gtk/gossip-accounts-dialog.h>
41
42 #include "empathy-filter.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 static void destroy_cb            (GtkWidget         *window,
57                                    MissionControl    *mc);
58 static void icon_activate_cb      (EmpathyStatusIcon *icon,
59                                    GtkWidget         *window);
60
61 static void
62 error_cb (MissionControl *mc,
63           GError         *error,
64           gpointer        data)
65 {
66         if (error) {
67                 gossip_debug (DEBUG_DOMAIN, "Error: %s", error->message);
68         }
69 }
70
71 static void
72 service_ended_cb (MissionControl *mc,
73                   gpointer        user_data)
74 {
75         gossip_debug (DEBUG_DOMAIN, "Mission Control stopped");
76 }
77
78 static void
79 operation_error_cb (MissionControl *mc,
80                     guint           operation_id,
81                     guint           error_code,
82                     gpointer        user_data)
83 {
84         gossip_debug (DEBUG_DOMAIN, "Error code %d during operation %d",
85                       error_code,
86                       operation_id);
87 }
88
89 static void
90 account_enabled_cb (McAccountMonitor *monitor,
91                     gchar            *unique_name,
92                     MissionControl   *mc)
93 {
94         gossip_debug (DEBUG_DOMAIN, "Account enabled: %s", unique_name);
95         start_mission_control (mc);
96 }
97
98 static void
99 start_mission_control (MissionControl *mc)
100 {
101         McPresence presence;
102
103         presence = mission_control_get_presence_actual (mc, NULL);
104
105         if (presence > MC_PRESENCE_OFFLINE) {
106                 /* MC is already running and online, nothing to do */
107                 return;
108         }
109
110         gossip_debug (DEBUG_DOMAIN, "Starting Mission Control...");
111
112         mission_control_set_presence (mc,
113                                       MC_PRESENCE_AVAILABLE,
114                                       NULL,
115                                       (McCallback) error_cb,
116                                       NULL);
117 }
118
119 static void
120 destroy_cb (GtkWidget      *window,
121             MissionControl *mc)
122 {
123         mission_control_set_presence (mc,
124                                       MC_PRESENCE_OFFLINE,
125                                       NULL, NULL, NULL);
126
127         gtk_main_quit ();
128 }
129
130 static void
131 icon_activate_cb (EmpathyStatusIcon *icon,
132                   GtkWidget         *window)
133 {
134         if (GTK_WIDGET_VISIBLE (window)) {
135                 gtk_widget_hide (window);
136         } else {
137                 gtk_widget_show (window);
138         }
139 }
140
141 static void
142 new_channel_cb (EmpathyFilter *filter,
143                 TpConn        *tp_conn,
144                 TpChan        *tp_chan,
145                 guint          context_handle,
146                 gpointer       user_data)
147 {
148         gossip_debug (DEBUG_DOMAIN, "Filtering context handle: %d",
149                       context_handle);
150         empathy_filter_process (filter, context_handle, TRUE);
151 }
152
153 int
154 main (int argc, char *argv[])
155 {
156         GList             *accounts;
157         EmpathyStatusIcon *icon;
158         GtkWidget         *window;
159         MissionControl    *mc;
160         McAccountMonitor  *monitor;
161         EmpathyFilter     *filter;
162
163         gtk_init (&argc, &argv);
164
165         /* Setting up channel filter */
166         filter = empathy_filter_new ();
167         g_signal_connect (filter, "new-channel",
168                           G_CALLBACK (new_channel_cb),
169                           NULL);
170
171         /* Setting up MC */
172         monitor = mc_account_monitor_new ();
173         mc = mission_control_new (tp_get_bus ());
174         g_signal_connect (monitor, "account-enabled",
175                           G_CALLBACK (account_enabled_cb),
176                           mc);
177         g_signal_connect (mc, "ServiceEnded",
178                           G_CALLBACK (service_ended_cb),
179                           NULL);
180         g_signal_connect (mc, "Error",
181                           G_CALLBACK (operation_error_cb),
182                           NULL);
183         start_mission_control (mc);
184
185         /* Setting up the main window */
186         window = empathy_main_window_show ();
187         g_signal_connect (window, "destroy",
188                           G_CALLBACK (destroy_cb),
189                           mc);
190         g_signal_connect (window, "delete-event",
191                           G_CALLBACK (gtk_widget_hide_on_delete),
192                           NULL);
193
194         /* Setting up the status icon */
195         icon = empathy_status_icon_new ();
196         g_signal_connect (icon, "activate",
197                           G_CALLBACK (icon_activate_cb),
198                           window);
199
200         /* Show the accounts dialog if there is no enabled accounts */
201         accounts = mc_accounts_list_by_enabled (TRUE);
202         if (accounts) {
203                 mc_accounts_list_free (accounts);
204         } else {
205                 gossip_accounts_dialog_show ();
206         }
207
208         gtk_main ();
209
210         g_object_unref (monitor);
211         g_object_unref (mc);
212         g_object_unref (icon);
213
214         return EXIT_SUCCESS;
215 }
216