]> git.0d.be Git - empathy.git/blob - src/empathy-main.c
47f2c566f11a73a5ccdf0ee2e1c56229b21348eb
[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-gtk/empathy-main-window.h>
38 #include <libempathy-gtk/empathy-images.h>
39 #include <libempathy-gtk/gossip-accounts-dialog.h>
40
41 #include "empathy-filter.h"
42
43 #define DEBUG_DOMAIN "EmpathyMain"
44
45 static void error_cb              (MissionControl *mc,
46                                    GError         *error,
47                                    gpointer        data);
48 static void service_ended_cb      (MissionControl *mc,
49                                    gpointer        user_data);
50 static void start_mission_control (MissionControl *mc);
51 static void destroy_cb            (GtkWidget      *window,
52                                    MissionControl *mc);
53 static void icon_activate_cb      (GtkStatusIcon  *status_icon,
54                                    GtkWidget      *window);
55
56 static void
57 error_cb (MissionControl *mc,
58           GError         *error,
59           gpointer        data)
60 {
61         if (error) {
62                 gossip_debug (DEBUG_DOMAIN, "Error: %s", error->message);
63         }
64 }
65
66 static void
67 service_ended_cb (MissionControl *mc,
68                   gpointer        user_data)
69 {
70         gossip_debug (DEBUG_DOMAIN, "Mission Control stopped");
71 }
72
73 static void
74 account_enabled_cb (McAccountMonitor *monitor,
75                     gchar            *unique_name,
76                     MissionControl   *mc)
77 {
78         gossip_debug (DEBUG_DOMAIN, "Account enabled: %s", unique_name);
79         start_mission_control (mc);
80 }
81
82 static void
83 start_mission_control (MissionControl *mc)
84 {
85         McPresence presence;
86
87         presence = mission_control_get_presence_actual (mc, NULL);
88
89         if (presence > MC_PRESENCE_OFFLINE) {
90                 /* MC is already running and online, nothing to do */
91                 return;
92         }
93
94         gossip_debug (DEBUG_DOMAIN, "Starting Mission Control...");
95
96         mission_control_set_presence (mc,
97                                       MC_PRESENCE_AVAILABLE,
98                                       NULL,
99                                       (McCallback) error_cb,
100                                       NULL);
101 }
102
103 static void
104 destroy_cb (GtkWidget      *window,
105             MissionControl *mc)
106 {
107         mission_control_set_presence (mc,
108                                       MC_PRESENCE_OFFLINE,
109                                       NULL, NULL, NULL);
110
111         gtk_main_quit ();
112 }
113
114 static void
115 icon_activate_cb (GtkStatusIcon *status_icon,
116                   GtkWidget     *window)
117 {
118         if (GTK_WIDGET_VISIBLE (window)) {
119                 gtk_widget_hide (window);
120         } else {
121                 gtk_widget_show (window);
122         }
123 }
124
125 static void
126 new_channel_cb (EmpathyFilter *filter,
127                 TpConn        *tp_conn,
128                 TpChan        *tp_chan,
129                 guint          context_handle,
130                 gpointer       user_data)
131 {
132         gossip_debug (DEBUG_DOMAIN, "Filtering context handle: %d",
133                       context_handle);
134         empathy_filter_process (filter, context_handle, TRUE);
135 }
136
137 int
138 main (int argc, char *argv[])
139 {
140         GList            *accounts;
141         GtkStatusIcon    *icon;
142         GtkWidget        *window;
143         MissionControl   *mc;
144         McAccountMonitor *monitor;
145         EmpathyFilter    *filter;
146
147         gtk_init (&argc, &argv);
148
149         /* Setting up channel filter */
150         filter = empathy_filter_new ();
151         g_signal_connect (filter, "new-channel",
152                           G_CALLBACK (new_channel_cb),
153                           NULL);
154
155         /* Setting up MC */
156         monitor = mc_account_monitor_new ();
157         mc = mission_control_new (tp_get_bus ());
158         g_signal_connect (monitor, "account-enabled",
159                           G_CALLBACK (account_enabled_cb),
160                           mc);
161         g_signal_connect (mc, "ServiceEnded",
162                           G_CALLBACK (service_ended_cb),
163                           NULL);
164         start_mission_control (mc);
165
166         /* Setting up the main window */
167         window = empathy_main_window_show ();
168         g_signal_connect (window, "destroy",
169                           G_CALLBACK (destroy_cb),
170                           mc);
171         g_signal_connect (window, "delete-event",
172                           G_CALLBACK (gtk_widget_hide_on_delete),
173                           NULL);
174
175         /* Setting up the tray icon */
176         icon = gtk_status_icon_new_from_icon_name (EMPATHY_IMAGE_MESSAGE);
177         gtk_status_icon_set_tooltip (icon, "Empathy - click here to show/hide the main window");
178         gtk_status_icon_set_visible (icon, TRUE);
179         g_signal_connect (icon, "activate",
180                           G_CALLBACK (icon_activate_cb),
181                           window);
182
183         /* Show the accounts dialog if there is no enabled accounts */
184         accounts = mc_accounts_list_by_enabled (TRUE);
185         if (accounts) {
186                 mc_accounts_list_free (accounts);
187         } else {
188                 gossip_accounts_dialog_show ();
189         }
190
191         gtk_main ();
192
193         g_object_unref (monitor);
194         g_object_unref (mc);
195         g_object_unref (icon);
196
197         return EXIT_SUCCESS;
198 }
199