]> git.0d.be Git - empathy.git/blob - src/empathy-main.c
94e29e2b510f5a318c4f9fceaaeb777150a648ce
[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/empathy-session.h>
37 #include <libempathy/gossip-debug.h>
38 #include <libempathy-gtk/empathy-main-window.h>
39 #include <libempathy-gtk/gossip-status-presets.h>
40 #include <libempathy-gtk/gossip-stock.h>
41 #include <libempathy-gtk/gossip-accounts-dialog.h>
42
43 #define DEBUG_DOMAIN "Empathy"
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                                    gpointer        user_data);
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         gossip_status_presets_get_all ();
97         mission_control_set_presence (mc,
98                                       gossip_status_presets_get_default_state (),
99                                       gossip_status_presets_get_default_status (),
100                                       (McCallback) error_cb,
101                                       NULL);
102 }
103
104 static void
105 destroy_cb (GtkWidget *window,
106             gpointer   user_data)
107 {
108         gossip_stock_finalize ();
109         empathy_session_finalize ();
110         gtk_main_quit ();
111 }
112
113 static void
114 icon_activate_cb (GtkStatusIcon *status_icon,
115                   GtkWidget     *window)
116 {
117         if (GTK_WIDGET_VISIBLE (window)) {
118                 gtk_widget_hide (window);
119         } else {
120                 gtk_widget_show (window);
121         }
122 }
123
124 int
125 main (int argc, char *argv[])
126 {
127         GList            *accounts;
128         GtkStatusIcon    *icon;
129         GtkWidget        *window;
130         MissionControl   *mc;
131         McAccountMonitor *monitor;
132
133         gtk_init (&argc, &argv);
134
135         /* FIXME: This is a horrible hack */
136         gossip_stock_init (gtk_window_new (GTK_WINDOW_TOPLEVEL));
137
138         /* Setting up MC */
139         monitor = mc_account_monitor_new ();
140         mc = mission_control_new (tp_get_bus ());
141         g_signal_connect (monitor, "account-enabled",
142                           G_CALLBACK (account_enabled_cb),
143                           mc);
144         g_signal_connect (mc, "ServiceEnded",
145                           G_CALLBACK (service_ended_cb),
146                           NULL);
147         start_mission_control (mc);
148
149         /* Setting up the main window */
150         window = empathy_main_window_show ();
151         g_signal_connect (window, "destroy",
152                           G_CALLBACK (destroy_cb),
153                           NULL);
154         g_signal_connect (window, "delete-event",
155                           G_CALLBACK (gtk_widget_hide_on_delete),
156                           NULL);
157
158         /* Setting up the tray icon */
159         icon = gtk_status_icon_new_from_stock (GOSSIP_STOCK_MESSAGE);
160         gtk_status_icon_set_tooltip (icon, "Empathy - click here to show/hide the main window");
161         gtk_status_icon_set_visible (icon, TRUE);
162         g_signal_connect (icon, "activate",
163                           G_CALLBACK (icon_activate_cb),
164                           window);
165
166         /* Show the accounts dialog if there is no enabled accounts */
167         accounts = mc_accounts_list_by_enabled (TRUE);
168         if (accounts) {
169                 mc_accounts_list_free (accounts);
170         } else {
171                 gossip_accounts_dialog_show ();
172         }
173
174         gtk_main ();
175
176         g_object_unref (monitor);
177         g_object_unref (mc);
178         g_object_unref (icon);
179
180         return EXIT_SUCCESS;
181 }
182