]> git.0d.be Git - empathy.git/blob - src/empathy.c
Fix a leaked ref.
[empathy.git] / src / empathy.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 <glib/gi18n.h>
29 #include <gtk/gtk.h>
30
31 #include <libgnome/gnome-program.h>
32 #include <libgnomeui/gnome-ui-init.h>
33
34 #include <libtelepathy/tp-conn.h>
35 #include <libtelepathy/tp-chan.h>
36 #include <libmissioncontrol/mc-account.h>
37 #include <libmissioncontrol/mc-account-monitor.h>
38 #include <libmissioncontrol/mission-control.h>
39
40 #include <libempathy/empathy-debug.h>
41 #include <libempathy/empathy-utils.h>
42 #include <libempathy/empathy-presence.h>
43 #include <libempathy/empathy-contact.h>
44 #include <libempathy/empathy-chandler.h>
45 #include <libempathy/empathy-tp-chat.h>
46 #include <libempathy/empathy-tp-chatroom.h>
47 #include <libempathy/empathy-idle.h>
48 #include <libempathy-gtk/empathy-main-window.h>
49 #include <libempathy-gtk/empathy-status-icon.h>
50 #include <libempathy-gtk/empathy-private-chat.h>
51 #include <libempathy-gtk/empathy-group-chat.h>
52
53 #define DEBUG_DOMAIN "EmpathyMain"
54
55 #define BUS_NAME "org.gnome.Empathy.Chat"
56 #define OBJECT_PATH "/org/freedesktop/Telepathy/ChannelHandler"
57
58 static void
59 service_ended_cb (MissionControl *mc,
60                   gpointer        user_data)
61 {
62         empathy_debug (DEBUG_DOMAIN, "Mission Control stopped");
63 }
64
65 static void
66 operation_error_cb (MissionControl *mc,
67                     guint           operation_id,
68                     guint           error_code,
69                     gpointer        user_data)
70 {
71         empathy_debug (DEBUG_DOMAIN, "Error code %d during operation %d",
72                       error_code,
73                       operation_id);
74 }
75
76 static void
77 start_mission_control (EmpathyIdle *idle)
78 {
79         McPresence presence;
80
81         presence = empathy_idle_get_state (idle);
82
83         if (presence > MC_PRESENCE_OFFLINE) {
84                 /* MC is already running and online, nothing to do */
85                 return;
86         }
87
88         empathy_idle_set_state (idle, MC_PRESENCE_AVAILABLE);
89 }
90
91 static void
92 account_enabled_cb (McAccountMonitor *monitor,
93                     gchar            *unique_name,
94                     EmpathyIdle      *idle)
95 {
96         empathy_debug (DEBUG_DOMAIN, "Account enabled: %s", unique_name);
97         start_mission_control (idle);
98 }
99
100 static void
101 new_channel_cb (EmpathyChandler *chandler,
102                 TpConn          *tp_conn,
103                 TpChan          *tp_chan,
104                 MissionControl  *mc)
105 {
106         McAccount  *account;
107         EmpathyChat *chat;
108         gchar      *id;
109
110         account = mission_control_get_account_for_connection (mc, tp_conn, NULL);
111         id = empathy_get_channel_id (account, tp_chan);
112         chat = empathy_chat_window_find_chat (account, id);
113         g_free (id);
114
115         if (chat) {
116                 /* The chat already exists */
117                 if (!empathy_chat_is_connected (chat)) {
118                         EmpathyTpChat *tp_chat;
119
120                         /* The chat died, give him the new text channel */
121                         if (empathy_chat_is_group_chat (chat)) {
122                                 tp_chat = EMPATHY_TP_CHAT (empathy_tp_chatroom_new (account, tp_chan));
123                         } else {
124                                 tp_chat = empathy_tp_chat_new (account, tp_chan);
125                         }
126                         empathy_chat_set_tp_chat (chat, tp_chat);
127                         g_object_unref (tp_chat);
128                 }
129                 empathy_chat_present (chat);
130
131                 g_object_unref (account);
132                 return;
133         }
134
135         if (tp_chan->handle_type == TP_HANDLE_TYPE_CONTACT) {
136                 /* We have a new private chat channel */
137                 chat = EMPATHY_CHAT (empathy_private_chat_new (account, tp_chan));
138         }
139         else if (tp_chan->handle_type == TP_HANDLE_TYPE_ROOM) {
140                 /* We have a new group chat channel */
141                 chat = EMPATHY_CHAT (empathy_group_chat_new (account, tp_chan));
142         }
143
144         empathy_chat_present (EMPATHY_CHAT (chat));
145
146         g_object_unref (chat);
147         g_object_unref (account);
148 }
149
150 int
151 main (int argc, char *argv[])
152 {
153         EmpathyStatusIcon *icon;
154         GtkWidget         *window;
155         MissionControl    *mc;
156         McAccountMonitor  *monitor;
157         EmpathyIdle       *idle;
158         EmpathyChandler   *chandler;
159         GnomeProgram      *program;
160         gboolean           no_connect = FALSE;
161         GOptionContext    *context;
162         GOptionEntry       options[] = {
163                 { "no-connect", 'n',
164                   0, G_OPTION_ARG_NONE, &no_connect,
165                   N_("Don't connect on startup"),
166                   NULL },
167                 { NULL }
168         };
169
170         bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
171         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
172         textdomain (GETTEXT_PACKAGE);
173
174         context = g_option_context_new (_("- Empathy Instant Messenger"));
175         g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);
176
177         g_set_application_name (PACKAGE_NAME);
178
179         program = gnome_program_init ("empathy",
180                                       PACKAGE_VERSION,
181                                       LIBGNOMEUI_MODULE,
182                                       argc, argv,
183                                       GNOME_PROGRAM_STANDARD_PROPERTIES,
184                                       "goption-context", context,
185                                       GNOME_PARAM_HUMAN_READABLE_NAME, PACKAGE_NAME,
186                                       NULL);
187
188         gtk_window_set_default_icon_name ("empathy");
189         gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
190                                            DATADIR G_DIR_SEPARATOR_S "empathy");
191
192         /* Setting up MC */
193         monitor = mc_account_monitor_new ();
194         mc = empathy_mission_control_new ();
195         idle = empathy_idle_new ();
196         g_signal_connect (monitor, "account-enabled",
197                           G_CALLBACK (account_enabled_cb),
198                           idle);
199         g_signal_connect (mc, "ServiceEnded",
200                           G_CALLBACK (service_ended_cb),
201                           NULL);
202         g_signal_connect (mc, "Error",
203                           G_CALLBACK (operation_error_cb),
204                           NULL);
205
206         if (!no_connect) {
207                 start_mission_control (idle);
208         }
209
210         /* Setting up UI */
211         window = empathy_main_window_show ();
212         icon = empathy_status_icon_new (GTK_WINDOW (window));
213
214         /* Setting up channel handler  */
215         chandler = empathy_chandler_new (BUS_NAME, OBJECT_PATH);
216         g_signal_connect (chandler, "new-channel",
217                           G_CALLBACK (new_channel_cb),
218                           mc);
219
220         gtk_main ();
221
222         empathy_idle_set_state (idle, MC_PRESENCE_OFFLINE);
223
224         g_object_unref (chandler);
225         g_object_unref (monitor);
226         g_object_unref (mc);
227         g_object_unref (idle);
228         g_object_unref (icon);
229         g_object_unref (program);
230
231         return EXIT_SUCCESS;
232 }
233