]> git.0d.be Git - empathy.git/blob - src/empathy-main.c
d693ba6b5221e64585d81551e92618a4b9f6e5e5
[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 <libmissioncontrol/mc-account.h>
31 #include <libmissioncontrol/mc-account-monitor.h>
32 #include <libmissioncontrol/mission-control.h>
33
34 #include <libempathy/gossip-debug.h>
35 #include <libempathy/gossip-utils.h>
36 #include <libempathy/gossip-presence.h>
37 #include <libempathy-gtk/empathy-main-window.h>
38 #include <libempathy-gtk/empathy-status-icon.h>
39
40 #include "empathy-filter.h"
41
42 #define DEBUG_DOMAIN "EmpathyMain"
43
44 static void error_cb              (MissionControl    *mc,
45                                    GError            *error,
46                                    gpointer           data);
47 static void service_ended_cb      (MissionControl    *mc,
48                                    gpointer           user_data);
49 static void operation_error_cb    (MissionControl    *mc,
50                                    guint              operation_id,
51                                    guint              error_code,
52                                    gpointer           user_data);
53 static void start_mission_control (MissionControl    *mc);
54
55 static void
56 error_cb (MissionControl *mc,
57           GError         *error,
58           gpointer        data)
59 {
60         if (error) {
61                 gossip_debug (DEBUG_DOMAIN, "Error: %s", error->message);
62         }
63 }
64
65 static void
66 service_ended_cb (MissionControl *mc,
67                   gpointer        user_data)
68 {
69         gossip_debug (DEBUG_DOMAIN, "Mission Control stopped");
70 }
71
72 static void
73 operation_error_cb (MissionControl *mc,
74                     guint           operation_id,
75                     guint           error_code,
76                     gpointer        user_data)
77 {
78         gossip_debug (DEBUG_DOMAIN, "Error code %d during operation %d",
79                       error_code,
80                       operation_id);
81 }
82
83 static void
84 account_enabled_cb (McAccountMonitor *monitor,
85                     gchar            *unique_name,
86                     MissionControl   *mc)
87 {
88         gossip_debug (DEBUG_DOMAIN, "Account enabled: %s", unique_name);
89         start_mission_control (mc);
90 }
91
92 static void
93 start_mission_control (MissionControl *mc)
94 {
95         McPresence presence;
96
97         presence = mission_control_get_presence_actual (mc, NULL);
98
99         if (presence > MC_PRESENCE_OFFLINE) {
100                 /* MC is already running and online, nothing to do */
101                 return;
102         }
103
104         gossip_debug (DEBUG_DOMAIN, "Starting Mission Control...");
105
106         mission_control_set_presence (mc,
107                                       MC_PRESENCE_AVAILABLE,
108                                       NULL,
109                                       (McCallback) error_cb,
110                                       NULL);
111 }
112
113 static void
114 new_channel_cb (EmpathyFilter *filter,
115                 TpConn        *tp_conn,
116                 TpChan        *tp_chan,
117                 guint          context_handle,
118                 gpointer       user_data)
119 {
120         gossip_debug (DEBUG_DOMAIN, "Filtering context handle: %d",
121                       context_handle);
122         empathy_filter_process (filter, context_handle, TRUE);
123 }
124
125 int
126 main (int argc, char *argv[])
127 {
128         EmpathyStatusIcon *icon;
129         GtkWidget         *window;
130         MissionControl    *mc;
131         McAccountMonitor  *monitor;
132         EmpathyFilter     *filter;
133
134         gtk_init (&argc, &argv);
135
136         /* Setting up channel filter */
137         filter = empathy_filter_new ();
138         g_signal_connect (filter, "new-channel",
139                           G_CALLBACK (new_channel_cb),
140                           NULL);
141
142         /* Setting up MC */
143         monitor = mc_account_monitor_new ();
144         mc = gossip_mission_control_new ();
145         g_signal_connect (monitor, "account-enabled",
146                           G_CALLBACK (account_enabled_cb),
147                           mc);
148         g_signal_connect (mc, "ServiceEnded",
149                           G_CALLBACK (service_ended_cb),
150                           NULL);
151         g_signal_connect (mc, "Error",
152                           G_CALLBACK (operation_error_cb),
153                           NULL);
154         start_mission_control (mc);
155
156         /* Setting up UI */
157         window = empathy_main_window_show ();
158         icon = empathy_status_icon_new (GTK_WINDOW (window));
159
160         gtk_main ();
161
162         mission_control_set_presence (mc,
163                                       MC_PRESENCE_OFFLINE,
164                                       NULL, NULL, NULL);
165
166         g_object_unref (monitor);
167         g_object_unref (mc);
168         g_object_unref (icon);
169
170         return EXIT_SUCCESS;
171 }
172