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