]> git.0d.be Git - empathy.git/blob - src/empathy-main.c
[darcs-to-svn @ Replace the launcher by the main program with tray icon]
[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-stock.h>
40 #include <libempathy-gtk/gossip-accounts-dialog.h>
41
42 #define DEBUG_DOMAIN "Empathy"
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 start_mission_control (MissionControl *mc);
50 static void destroy_cb            (GtkWidget      *window,
51                                    gpointer        user_data);
52 static void icon_activate_cb      (GtkStatusIcon  *status_icon,
53                                    GtkWidget      *window);
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 account_enabled_cb (McAccountMonitor *monitor,
74                     gchar            *unique_name,
75                     MissionControl   *mc)
76 {
77         gossip_debug (DEBUG_DOMAIN, "Account enabled: %s", unique_name);
78         start_mission_control (mc);
79 }
80
81 static void
82 start_mission_control (MissionControl *mc)
83 {
84         McPresence presence;
85
86         presence = mission_control_get_presence_actual (mc, NULL);
87
88         if (presence != MC_PRESENCE_UNSET &&
89             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         /* FIXME: Save/Restore status message */
97         mission_control_set_presence (mc, MC_PRESENCE_AVAILABLE,
98                                       NULL,
99                                       (McCallback) error_cb,
100                                       NULL);
101
102         mission_control_connect_all_with_default_presence (mc,
103                                                            (McCallback) error_cb,
104                                                            NULL);
105 }
106
107 static void
108 destroy_cb (GtkWidget *window,
109             gpointer   user_data)
110 {
111         gossip_stock_finalize ();
112         empathy_session_finalize ();
113         gtk_main_quit ();
114 }
115
116 static void
117 icon_activate_cb (GtkStatusIcon *status_icon,
118                   GtkWidget     *window)
119 {
120         if (GTK_WIDGET_VISIBLE (window)) {
121                 gtk_widget_hide (window);
122         } else {
123                 gtk_widget_show (window);
124         }
125 }
126
127 int
128 main (int argc, char *argv[])
129 {
130         GList            *accounts;
131         GtkStatusIcon    *icon;
132         GtkWidget        *window;
133         MissionControl   *mc;
134         McAccountMonitor *monitor;
135
136         gtk_init (&argc, &argv);
137
138         /* FIXME: This is a horrible hack */
139         gossip_stock_init (gtk_window_new (GTK_WINDOW_TOPLEVEL));
140
141         /* Setting up the main window */
142         window = empathy_main_window_show ();
143         g_signal_connect (window, "destroy",
144                           G_CALLBACK (destroy_cb),
145                           NULL);
146         g_signal_connect (window, "delete-event",
147                           G_CALLBACK (gtk_widget_hide_on_delete),
148                           NULL);
149
150         /* Setting up the tray icon */
151         icon = gtk_status_icon_new_from_stock (GOSSIP_STOCK_AVAILABLE);
152         gtk_status_icon_set_tooltip (icon, "Empathy - click here to show/hide the main window");
153         gtk_status_icon_set_visible (icon, TRUE);
154         g_signal_connect (icon, "activate",
155                           G_CALLBACK (icon_activate_cb),
156                           window);
157
158         /* Show the accounts dialog if there is no enabled accounts */
159         accounts = mc_accounts_list_by_enabled (TRUE);
160         if (accounts) {
161                 mc_accounts_list_free (accounts);
162         } else {
163                 gossip_accounts_dialog_show ();
164         }
165
166         /* Setting up MC */
167         monitor = mc_account_monitor_new ();
168         mc = mission_control_new (tp_get_bus ());
169         g_signal_connect (monitor, "account-enabled",
170                           G_CALLBACK (account_enabled_cb),
171                           mc);
172         g_signal_connect (mc, "ServiceEnded",
173                           G_CALLBACK (service_ended_cb),
174                           NULL);
175         start_mission_control (mc);
176
177         gtk_main ();
178
179         g_object_unref (monitor);
180         g_object_unref (mc);
181         g_object_unref (icon);
182
183         return EXIT_SUCCESS;
184 }
185