]> git.0d.be Git - empathy.git/blob - src/empathy.c
Improve dispatcher. Fixes bug #465928.
[empathy.git] / src / empathy.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2008 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 #include <errno.h>
27
28 #include <glib.h>
29 #include <glib/gi18n.h>
30 #include <gtk/gtk.h>
31 #include <gdk/gdkx.h>
32
33 #include <libebook/e-book.h>
34
35 #include <telepathy-glib/util.h>
36 #include <libmissioncontrol/mc-account.h>
37 #include <libmissioncontrol/mission-control.h>
38
39 #include <libempathy/empathy-idle.h>
40 #include <libempathy/empathy-utils.h>
41 #include <libempathy/empathy-dispatcher.h>
42 #include <libempathy/empathy-tp-chat.h>
43 #include <libempathy/empathy-tp-group.h>
44
45 #include <libempathy-gtk/empathy-conf.h>
46
47 #include "empathy-main-window.h"
48 #include "empathy-status-icon.h"
49 #include "empathy-call-window.h"
50 #include "empathy-chat-window.h"
51 #include "bacon-message-connection.h"
52
53 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
54 #include <libempathy/empathy-debug.h>
55
56 static BaconMessageConnection *connection = NULL;
57
58 static void
59 dispatch_channel_cb (EmpathyDispatcher *dispatcher,
60                      TpChannel         *channel,
61                      gpointer           user_data)
62 {
63         gchar *channel_type;
64
65         g_object_get (channel, "channel-type", &channel_type, NULL);
66         if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT)) {
67                 EmpathyTpChat *tp_chat;
68                 EmpathyChat   *chat = NULL;
69                 const gchar   *id;
70
71                 tp_chat = empathy_tp_chat_new (channel);
72                 empathy_run_until_ready (tp_chat);
73
74                 id = empathy_tp_chat_get_id (tp_chat);
75                 if (!id) {
76                         EmpathyContact *contact;
77
78                         contact = empathy_tp_chat_get_remote_contact (tp_chat);
79                         if (contact) {
80                                 id = empathy_contact_get_id (contact);
81                         }
82                 }
83
84                 if (id) {
85                         McAccount *account;
86
87                         account = empathy_tp_chat_get_account (tp_chat);
88                         chat = empathy_chat_window_find_chat (account, id);
89                 }
90
91                 if (chat) {
92                         empathy_chat_set_tp_chat (chat, tp_chat);
93                 } else {
94                         chat = empathy_chat_new (tp_chat);
95                 }
96
97                 empathy_chat_window_present_chat (chat);
98                 g_object_unref (tp_chat);
99         }
100         else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA)) {
101                 GtkWidget *window;
102
103                 window = empathy_call_window_find (channel);
104                 if (window) {
105                         gtk_window_present (GTK_WINDOW (window));
106                 } else {
107                         empathy_call_window_new (channel);
108                 }
109         }
110 }
111
112 static void
113 service_ended_cb (MissionControl *mc,
114                   gpointer        user_data)
115 {
116         DEBUG ("Mission Control stopped");
117 }
118
119 static void
120 operation_error_cb (MissionControl *mc,
121                     guint           operation_id,
122                     guint           error_code,
123                     gpointer        user_data)
124 {
125         const gchar *message;
126
127         switch (error_code) {
128         case MC_DISCONNECTED_ERROR:
129                 message = _("Disconnected");
130                 break;
131         case MC_INVALID_HANDLE_ERROR:
132                 message = _("Invalid handle");
133                 break;
134         case MC_NO_MATCHING_CONNECTION_ERROR:
135                 message = _("No matching connection");
136                 break;
137         case MC_INVALID_ACCOUNT_ERROR:
138                 message = _("Invalid account");
139                 break;
140         case MC_PRESENCE_FAILURE_ERROR:
141                 message = _("Presence failure");
142                 break;
143         case MC_NO_ACCOUNTS_ERROR:
144                 message = _("No accounts");
145                 break;
146         case MC_NETWORK_ERROR:
147                 message = _("Network error");
148                 break;
149         case MC_CONTACT_DOES_NOT_SUPPORT_VOICE_ERROR:
150                 message = _("Contact does not support voice");
151                 break;
152         case MC_LOWMEM_ERROR:
153                 message = _("Lowmem");
154                 break;
155         case MC_CHANNEL_REQUEST_GENERIC_ERROR:
156                 message = _("Channel request generic error");
157                 break;
158         case MC_CHANNEL_BANNED_ERROR:
159                 message = _("Channel banned");
160                 break;
161         case MC_CHANNEL_FULL_ERROR:
162                 message = _("Channel full");
163                 break;
164         case MC_CHANNEL_INVITE_ONLY_ERROR:
165                 message = _("Channel invite only");
166                 break;
167         default:
168                 message = _("Unknown error code");
169         }
170
171         DEBUG ("Error during operation %d: %s", operation_id, message);
172 }
173
174 static void
175 use_nm_notify_cb (EmpathyConf *conf,
176                   const gchar *key,
177                   gpointer     user_data)
178 {
179         EmpathyIdle *idle = user_data;
180         gboolean     use_nm;
181
182         if (empathy_conf_get_bool (conf, key, &use_nm)) {
183                 empathy_idle_set_use_nm (idle, use_nm);
184         }
185 }
186
187 static void
188 create_salut_account (void)
189 {
190         McProfile  *profile;
191         McProtocol *protocol;
192         gboolean    salut_created = FALSE;
193         McAccount  *account;
194         GList      *accounts;
195         EBook      *book;
196         EContact   *contact;
197         gchar      *nickname = NULL;
198         gchar      *first_name = NULL;
199         gchar      *last_name = NULL;
200         gchar      *email = NULL;
201         gchar      *jid = NULL;
202         GError     *error = NULL;
203
204         /* Check if we already created a salut account */
205         empathy_conf_get_bool (empathy_conf_get(),
206                                EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
207                                &salut_created);
208         if (salut_created) {
209                 return;
210         }
211
212         DEBUG ("Try to add a salut account...");
213
214         /* Check if the salut CM is installed */
215         profile = mc_profile_lookup ("salut");
216         protocol = mc_profile_get_protocol (profile);
217         if (!protocol) {
218                 DEBUG ("Salut not installed");
219                 g_object_unref (profile);
220                 return;
221         }
222         g_object_unref (protocol);
223
224         /* Get self EContact from EDS */
225         if (!e_book_get_self (&contact, &book, &error)) {
226                 DEBUG ("Failed to get self econtact: %s",
227                         error ? error->message : "No error given");
228                 g_clear_error (&error);
229                 g_object_unref (profile);
230                 return;
231         }
232
233         empathy_conf_set_bool (empathy_conf_get (),
234                                EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
235                                TRUE);
236
237         /* Check if there is already a salut account */
238         accounts = mc_accounts_list_by_profile (profile);
239         if (accounts) {
240                 DEBUG ("There is already a salut account");
241                 mc_accounts_list_free (accounts);
242                 g_object_unref (profile);
243                 return;
244         }
245
246         account = mc_account_create (profile);
247         mc_account_set_display_name (account, _("People nearby"));
248         
249         nickname = e_contact_get (contact, E_CONTACT_NICKNAME);
250         first_name = e_contact_get (contact, E_CONTACT_GIVEN_NAME);
251         last_name = e_contact_get (contact, E_CONTACT_FAMILY_NAME);
252         email = e_contact_get (contact, E_CONTACT_EMAIL_1);
253         jid = e_contact_get (contact, E_CONTACT_IM_JABBER_HOME_1);
254         
255         if (!tp_strdiff (nickname, "nickname")) {
256                 g_free (nickname);
257                 nickname = NULL;
258         }
259
260         DEBUG ("Salut account created:\nnickname=%s\nfirst-name=%s\n"
261                 "last-name=%s\nemail=%s\njid=%s\n",
262                 nickname, first_name, last_name, email, jid);
263
264         mc_account_set_param_string (account, "nickname", nickname ? nickname : "");
265         mc_account_set_param_string (account, "first-name", first_name ? first_name : "");
266         mc_account_set_param_string (account, "last-name", last_name ? last_name : "");
267         mc_account_set_param_string (account, "email", email ? email : "");
268         mc_account_set_param_string (account, "jid", jid ? jid : "");
269
270         g_free (nickname);
271         g_free (first_name);
272         g_free (last_name);
273         g_free (email);
274         g_free (jid);
275         g_object_unref (account);
276         g_object_unref (profile);
277         g_object_unref (contact);
278         g_object_unref (book);
279 }
280
281 /* The code that handles single-instance and startup notification is
282  * copied from gedit.
283  *
284  * Copyright (C) 2005 - Paolo Maggi 
285  */
286 static void
287 on_bacon_message_received (const char *message,
288                            gpointer    data)
289 {
290         GtkWidget *window = data;
291         guint32    startup_timestamp;
292
293         g_return_if_fail (message != NULL);
294
295         DEBUG ("Other instance launched, presenting the main window. message='%s'",
296                 message);
297
298         startup_timestamp = atoi (message);
299
300         /* Set the proper interaction time on the window.
301          * Fall back to roundtripping to the X server when we
302          * don't have the timestamp, e.g. when launched from
303          * terminal. We also need to make sure that the window
304          * has been realized otherwise it will not work. lame. */
305         if (startup_timestamp == 0) {
306                 /* Work if launched from the terminal */
307                 DEBUG ("Using X server timestamp as a fallback");
308
309                 if (!GTK_WIDGET_REALIZED (window)) {
310                         gtk_widget_realize (GTK_WIDGET (window));
311                 }
312
313                 startup_timestamp = gdk_x11_get_server_time (window->window);
314         }
315
316         gtk_window_present_with_time (GTK_WINDOW (window), startup_timestamp);
317 }
318
319 static guint32
320 get_startup_timestamp ()
321 {
322         const gchar *startup_id_env;
323         gchar       *startup_id = NULL;
324         gchar       *time_str;
325         gchar       *end;
326         gulong       retval = 0;
327
328         /* we don't unset the env, since startup-notification
329          * may still need it */
330         startup_id_env = g_getenv ("DESKTOP_STARTUP_ID");
331         if (startup_id_env == NULL) {
332                 goto out;
333         }
334
335         startup_id = g_strdup (startup_id_env);
336
337         time_str = g_strrstr (startup_id, "_TIME");
338         if (time_str == NULL) {
339                 goto out;
340         }
341
342         errno = 0;
343
344         /* Skip past the "_TIME" part */
345         time_str += 5;
346
347         retval = strtoul (time_str, &end, 0);
348         if (end == time_str || errno != 0)
349                 retval = 0;
350
351  out:
352         g_free (startup_id);
353
354         return (retval > 0) ? retval : 0;
355 }
356
357 int
358 main (int argc, char *argv[])
359 {
360         guint32            startup_timestamp;
361         EmpathyStatusIcon *icon;
362         EmpathyDispatcher *dispatcher;
363         GtkWidget         *window;
364         MissionControl    *mc;
365         EmpathyIdle       *idle;
366         gboolean           autoconnect = TRUE;
367         gboolean           no_connect = FALSE; 
368         GError            *error = NULL;
369         GOptionEntry       options[] = {
370                 { "no-connect", 'n',
371                   0, G_OPTION_ARG_NONE, &no_connect,
372                   N_("Don't connect on startup"),
373                   NULL },
374                 { NULL }
375         };
376
377         bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
378         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
379         textdomain (GETTEXT_PACKAGE);
380
381         startup_timestamp = get_startup_timestamp ();
382
383         if (!gtk_init_with_args (&argc, &argv,
384                                  _("- Empathy Instant Messenger"),
385                                  options, GETTEXT_PACKAGE, &error)) {
386                 g_warning ("Error in gtk init: %s", error->message);
387                 return EXIT_FAILURE;
388         }
389
390         if (g_getenv ("EMPATHY_TIMING") != NULL) {
391                 g_log_set_default_handler (tp_debug_timestamped_log_handler, NULL);
392         }
393         empathy_debug_set_flags (g_getenv ("EMPATHY_DEBUG"));
394         tp_debug_divert_messages (g_getenv ("EMPATHY_LOGFILE"));
395
396         g_set_application_name (PACKAGE_NAME);
397
398         gtk_window_set_default_icon_name ("empathy");
399         gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
400                                            PKGDATADIR G_DIR_SEPARATOR_S "icons");
401
402         /* Setting up the bacon connection */
403         connection = bacon_message_connection_new ("empathy");
404         if (connection != NULL) {
405                 if (!bacon_message_connection_get_is_server (connection)) {
406                         gchar *message;
407
408                         DEBUG ("Activating existing instance");
409
410                         message = g_strdup_printf ("%" G_GUINT32_FORMAT,
411                                                    startup_timestamp);
412                         bacon_message_connection_send (connection, message);
413
414                         /* We never popup a window, so tell startup-notification
415                          * that we are done. */
416                         gdk_notify_startup_complete ();
417
418                         g_free (message);
419                         bacon_message_connection_free (connection);
420
421                         return EXIT_SUCCESS;
422                 }
423         } else {
424                 g_warning ("Cannot create the 'empathy' bacon connection.");
425         }
426
427         /* Setting up MC */
428         mc = empathy_mission_control_new ();
429         g_signal_connect (mc, "ServiceEnded",
430                           G_CALLBACK (service_ended_cb),
431                           NULL);
432         g_signal_connect (mc, "Error",
433                           G_CALLBACK (operation_error_cb),
434                           NULL);
435
436         /* Setting up Idle */
437         idle = empathy_idle_new ();
438         empathy_idle_set_auto_away (idle, TRUE);
439         use_nm_notify_cb (empathy_conf_get (), EMPATHY_PREFS_USE_NM, idle);
440         empathy_conf_notify_add (empathy_conf_get (), EMPATHY_PREFS_USE_NM,
441                                  use_nm_notify_cb, idle);
442
443         /* Autoconnect */
444         empathy_conf_get_bool (empathy_conf_get(),
445                                EMPATHY_PREFS_AUTOCONNECT,
446                                &autoconnect);
447         if (autoconnect && ! no_connect &&
448             empathy_idle_get_state (idle) <= MC_PRESENCE_OFFLINE) {
449                 empathy_idle_set_state (idle, MC_PRESENCE_AVAILABLE);
450         }
451         
452         create_salut_account ();
453
454         /* Setting up UI */
455         window = empathy_main_window_show ();
456         icon = empathy_status_icon_new (GTK_WINDOW (window));
457
458         if (connection) {
459                 /* We se the callback here because we need window */
460                 bacon_message_connection_set_callback (connection,
461                                                        on_bacon_message_received,
462                                                        window);
463         }
464
465         /* Handle channels */
466         dispatcher = empathy_dispatcher_new ();
467         g_signal_connect (dispatcher, "dispatch-channel",
468                           G_CALLBACK (dispatch_channel_cb),
469                           NULL);
470
471         gtk_main ();
472
473         empathy_idle_set_state (idle, MC_PRESENCE_OFFLINE);
474
475         g_object_unref (mc);
476         g_object_unref (idle);
477         g_object_unref (icon);
478         g_object_unref (dispatcher);
479
480         return EXIT_SUCCESS;
481 }
482