]> git.0d.be Git - empathy.git/blob - src/empathy-main.c
6780ff6808de7cf48136511b4aabc6154555060f
[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
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 static void
116 new_channel_cb (EmpathyFilter *filter,
117                 TpConn        *tp_conn,
118                 TpChan        *tp_chan,
119                 guint          context_handle,
120                 gpointer       user_data)
121 {
122         gossip_debug (DEBUG_DOMAIN, "Filtering context handle: %d",
123                       context_handle);
124         empathy_filter_process (filter, context_handle, TRUE);
125 }
126
127 int
128 main (int argc, char *argv[])
129 {
130         GList             *accounts;
131         EmpathyStatusIcon *icon;
132         GtkWidget         *window;
133         MissionControl    *mc;
134         McAccountMonitor  *monitor;
135         EmpathyFilter     *filter;
136
137         gtk_init (&argc, &argv);
138
139         /* Setting up channel filter */
140         filter = empathy_filter_new ();
141         g_signal_connect (filter, "new-channel",
142                           G_CALLBACK (new_channel_cb),
143                           NULL);
144
145         /* Setting up MC */
146         monitor = mc_account_monitor_new ();
147         mc = mission_control_new (tp_get_bus ());
148         g_signal_connect (monitor, "account-enabled",
149                           G_CALLBACK (account_enabled_cb),
150                           mc);
151         g_signal_connect (mc, "ServiceEnded",
152                           G_CALLBACK (service_ended_cb),
153                           NULL);
154         g_signal_connect (mc, "Error",
155                           G_CALLBACK (operation_error_cb),
156                           NULL);
157         start_mission_control (mc);
158
159         /* Setting up UI */
160         window = empathy_main_window_show ();
161         icon = empathy_status_icon_new (GTK_WINDOW (window));
162
163         /* Show the accounts dialog if there is no enabled accounts */
164         accounts = mc_accounts_list_by_enabled (TRUE);
165         if (accounts) {
166                 mc_accounts_list_free (accounts);
167         } else {
168                 gossip_accounts_dialog_show ();
169         }
170
171         gtk_main ();
172
173         mission_control_set_presence (mc,
174                                       MC_PRESENCE_OFFLINE,
175                                       NULL, NULL, NULL);
176
177         g_object_unref (monitor);
178         g_object_unref (mc);
179         g_object_unref (icon);
180
181         return EXIT_SUCCESS;
182 }
183