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