]> git.0d.be Git - empathy.git/blob - src/empathy.c
Handle construction errors
[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., 51 Franklin St, Fifth Floor,
18  * Boston, MA  02110-1301  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 #include <string.h>
28
29 #include <glib.h>
30 #include <glib/gi18n.h>
31 #include <gtk/gtk.h>
32 #include <gdk/gdkx.h>
33
34 #if HAVE_LIBCHAMPLAIN
35 #include <clutter-gtk/gtk-clutter-embed.h>
36 #endif
37
38 #include <libebook/e-book.h>
39 #include <libnotify/notify.h>
40
41 #include <telepathy-glib/util.h>
42 #include <libmissioncontrol/mc-account.h>
43 #include <libmissioncontrol/mission-control.h>
44
45 #include <libempathy/empathy-idle.h>
46 #include <libempathy/empathy-utils.h>
47 #include <libempathy/empathy-call-factory.h>
48 #include <libempathy/empathy-chatroom-manager.h>
49 #include <libempathy/empathy-account-manager.h>
50 #include <libempathy/empathy-dispatcher.h>
51 #include <libempathy/empathy-dispatch-operation.h>
52 #include <libempathy/empathy-log-manager.h>
53 #include <libempathy/empathy-ft-factory.h>
54 #include <libempathy/empathy-tp-chat.h>
55 #include <libempathy/empathy-tp-call.h>
56
57 #include <libempathy-gtk/empathy-conf.h>
58 #include <libempathy-gtk/empathy-ui-utils.h>
59 #include <libempathy-gtk/empathy-location-manager.h>
60
61 #include "empathy-accounts-dialog.h"
62 #include "empathy-main-window.h"
63 #include "empathy-status-icon.h"
64 #include "empathy-call-window.h"
65 #include "empathy-chat-window.h"
66 #include "empathy-ft-manager.h"
67 #include "bacon-message-connection.h"
68
69 #include "extensions/extensions.h"
70
71 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
72 #include <libempathy/empathy-debug.h>
73
74 #include <gst/gst.h>
75
76 static BaconMessageConnection *connection = NULL;
77
78 static void
79 dispatch_cb (EmpathyDispatcher *dispatcher,
80              EmpathyDispatchOperation *operation,
81              gpointer           user_data)
82 {
83         GQuark channel_type;
84
85         channel_type = empathy_dispatch_operation_get_channel_type_id (operation);
86
87         if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT) {
88                 EmpathyTpChat *tp_chat;
89                 EmpathyChat   *chat = NULL;
90                 const gchar   *id;
91
92                 tp_chat = EMPATHY_TP_CHAT (
93                         empathy_dispatch_operation_get_channel_wrapper (operation));
94
95                 id = empathy_tp_chat_get_id (tp_chat);
96                 if (!id) {
97                         EmpathyContact *contact;
98
99                         contact = empathy_tp_chat_get_remote_contact (tp_chat);
100                         if (contact) {
101                                 id = empathy_contact_get_id (contact);
102                         }
103                 }
104
105                 if (id) {
106                         EmpathyAccountManager *manager;
107                         TpConnection *connection;
108                         McAccount *account;
109
110                         manager = empathy_account_manager_dup_singleton ();
111                         connection = empathy_tp_chat_get_connection (tp_chat);
112                         account = empathy_account_manager_get_account (manager,
113                                                                        connection);
114                         chat = empathy_chat_window_find_chat (account, id);
115                         g_object_unref (manager);
116                 }
117
118                 if (chat) {
119                         empathy_chat_set_tp_chat (chat, tp_chat);
120                 } else {
121                         chat = empathy_chat_new (tp_chat);
122                 }
123
124                 empathy_chat_window_present_chat (chat);
125
126                 empathy_dispatch_operation_claim (operation);
127         } else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA) {
128                 EmpathyCallFactory *factory;
129
130                 factory = empathy_call_factory_get ();
131                 empathy_call_factory_claim_channel (factory, operation);
132         } else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER) {
133                 EmpathyFTFactory *factory;
134
135                 factory = empathy_ft_factory_dup_singleton ();
136                 empathy_ft_factory_claim_channel (factory, operation);
137         }
138 }
139
140 static void
141 service_ended_cb (MissionControl *mc,
142                   gpointer        user_data)
143 {
144         DEBUG ("Mission Control stopped");
145 }
146
147 static void
148 operation_error_cb (MissionControl *mc,
149                     guint           operation_id,
150                     guint           error_code,
151                     gpointer        user_data)
152 {
153         const gchar *message;
154
155         switch (error_code) {
156         case MC_DISCONNECTED_ERROR:
157                 message = "Disconnected";
158                 break;
159         case MC_INVALID_HANDLE_ERROR:
160                 message = "Invalid handle";
161                 break;
162         case MC_NO_MATCHING_CONNECTION_ERROR:
163                 message = "No matching connection";
164                 break;
165         case MC_INVALID_ACCOUNT_ERROR:
166                 message = "Invalid account";
167                 break;
168         case MC_PRESENCE_FAILURE_ERROR:
169                 message = "Presence failure";
170                 break;
171         case MC_NO_ACCOUNTS_ERROR:
172                 message = "No accounts";
173                 break;
174         case MC_NETWORK_ERROR:
175                 message = "Network error";
176                 break;
177         case MC_CONTACT_DOES_NOT_SUPPORT_VOICE_ERROR:
178                 message = "Contact does not support voice";
179                 break;
180         case MC_LOWMEM_ERROR:
181                 message = "Lowmem";
182                 break;
183         case MC_CHANNEL_REQUEST_GENERIC_ERROR:
184                 message = "Channel request generic error";
185                 break;
186         case MC_CHANNEL_BANNED_ERROR:
187                 message = "Channel banned";
188                 break;
189         case MC_CHANNEL_FULL_ERROR:
190                 message = "Channel full";
191                 break;
192         case MC_CHANNEL_INVITE_ONLY_ERROR:
193                 message = "Channel invite only";
194                 break;
195         default:
196                 message = "Unknown error code";
197         }
198
199         DEBUG ("Error during operation %d: %s", operation_id, message);
200 }
201
202 static void
203 use_nm_notify_cb (EmpathyConf *conf,
204                   const gchar *key,
205                   gpointer     user_data)
206 {
207         EmpathyIdle *idle = user_data;
208         gboolean     use_nm;
209
210         if (empathy_conf_get_bool (conf, key, &use_nm)) {
211                 empathy_idle_set_use_nm (idle, use_nm);
212         }
213 }
214
215 static void
216 create_salut_account (void)
217 {
218         McProfile  *profile;
219         McProtocol *protocol;
220         gboolean    salut_created = FALSE;
221         McAccount  *account;
222         GList      *accounts;
223         EBook      *book;
224         EContact   *contact;
225         gchar      *nickname = NULL;
226         gchar      *first_name = NULL;
227         gchar      *last_name = NULL;
228         gchar      *email = NULL;
229         gchar      *jid = NULL;
230         GError     *error = NULL;
231
232         /* Check if we already created a salut account */
233         empathy_conf_get_bool (empathy_conf_get (),
234                                EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
235                                &salut_created);
236         if (salut_created) {
237                 return;
238         }
239
240         DEBUG ("Try to add a salut account...");
241
242         /* Check if the salut CM is installed */
243         profile = mc_profile_lookup ("salut");
244         if (!profile) {
245                 DEBUG ("No salut profile");
246                 return;
247         }
248         protocol = mc_profile_get_protocol (profile);
249         if (!protocol) {
250                 DEBUG ("Salut not installed");
251                 g_object_unref (profile);
252                 return;
253         }
254         g_object_unref (protocol);
255
256         /* Get self EContact from EDS */
257         if (!e_book_get_self (&contact, &book, &error)) {
258                 DEBUG ("Failed to get self econtact: %s",
259                         error ? error->message : "No error given");
260                 g_clear_error (&error);
261                 g_object_unref (profile);
262                 return;
263         }
264
265         empathy_conf_set_bool (empathy_conf_get (),
266                                EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
267                                TRUE);
268
269         /* Check if there is already a salut account */
270         accounts = mc_accounts_list_by_profile (profile);
271         if (accounts) {
272                 DEBUG ("There is already a salut account");
273                 mc_accounts_list_free (accounts);
274                 g_object_unref (profile);
275                 return;
276         }
277
278         account = mc_account_create (profile);
279         mc_account_set_display_name (account, _("People nearby"));
280         
281         nickname = e_contact_get (contact, E_CONTACT_NICKNAME);
282         first_name = e_contact_get (contact, E_CONTACT_GIVEN_NAME);
283         last_name = e_contact_get (contact, E_CONTACT_FAMILY_NAME);
284         email = e_contact_get (contact, E_CONTACT_EMAIL_1);
285         jid = e_contact_get (contact, E_CONTACT_IM_JABBER_HOME_1);
286         
287         if (!tp_strdiff (nickname, "nickname")) {
288                 g_free (nickname);
289                 nickname = NULL;
290         }
291
292         DEBUG ("Salut account created:\nnickname=%s\nfirst-name=%s\n"
293                 "last-name=%s\nemail=%s\njid=%s\n",
294                 nickname, first_name, last_name, email, jid);
295
296         mc_account_set_param_string (account, "nickname", nickname ? nickname : "");
297         mc_account_set_param_string (account, "first-name", first_name ? first_name : "");
298         mc_account_set_param_string (account, "last-name", last_name ? last_name : "");
299         mc_account_set_param_string (account, "email", email ? email : "");
300         mc_account_set_param_string (account, "jid", jid ? jid : "");
301
302         g_free (nickname);
303         g_free (first_name);
304         g_free (last_name);
305         g_free (email);
306         g_free (jid);
307         g_object_unref (account);
308         g_object_unref (profile);
309         g_object_unref (contact);
310         g_object_unref (book);
311 }
312
313 /* The code that handles single-instance and startup notification is
314  * copied from gedit.
315  *
316  * Copyright (C) 2005 - Paolo Maggi
317  */
318 static void
319 on_bacon_message_received (const char *message,
320                            gpointer    data)
321 {
322         GtkWidget *window = data;
323         guint32    startup_timestamp;
324
325         g_return_if_fail (message != NULL);
326
327         DEBUG ("Other instance launched, presenting the main window. message='%s'",
328                 message);
329
330         if (strcmp (message, "accounts") == 0) {
331                 /* accounts dialog requested */
332                 empathy_accounts_dialog_show (GTK_WINDOW (window), NULL);
333         } else {
334                 startup_timestamp = atoi (message);
335
336                 /* Set the proper interaction time on the window.
337                  * Fall back to roundtripping to the X server when we
338                  * don't have the timestamp, e.g. when launched from
339                  * terminal. We also need to make sure that the window
340                  * has been realized otherwise it will not work. lame. */
341                 if (startup_timestamp == 0) {
342                         /* Work if launched from the terminal */
343                         DEBUG ("Using X server timestamp as a fallback");
344
345                         if (!GTK_WIDGET_REALIZED (window)) {
346                                 gtk_widget_realize (GTK_WIDGET (window));
347                         }
348
349                         startup_timestamp = gdk_x11_get_server_time (window->window);
350                 }
351
352                 gtk_window_present_with_time (GTK_WINDOW (window), startup_timestamp);
353         }
354 }
355
356 static guint32
357 get_startup_timestamp ()
358 {
359         const gchar *startup_id_env;
360         gchar       *startup_id = NULL;
361         gchar       *time_str;
362         gchar       *end;
363         gulong       retval = 0;
364
365         /* we don't unset the env, since startup-notification
366          * may still need it */
367         startup_id_env = g_getenv ("DESKTOP_STARTUP_ID");
368         if (startup_id_env == NULL) {
369                 goto out;
370         }
371
372         startup_id = g_strdup (startup_id_env);
373
374         time_str = g_strrstr (startup_id, "_TIME");
375         if (time_str == NULL) {
376                 goto out;
377         }
378
379         errno = 0;
380
381         /* Skip past the "_TIME" part */
382         time_str += 5;
383
384         retval = strtoul (time_str, &end, 0);
385         if (end == time_str || errno != 0)
386                 retval = 0;
387
388  out:
389         g_free (startup_id);
390
391         return (retval > 0) ? retval : 0;
392 }
393
394 static gboolean
395 show_version_cb (const char *option_name,
396                  const char *value,
397                  gpointer data,
398                  GError **error)
399 {
400         g_print ("%s\n", PACKAGE_STRING);
401
402         exit (EXIT_SUCCESS);
403
404         return FALSE;
405 }
406
407 static void
408 new_incoming_transfer_cb (EmpathyFTFactory *factory,
409                           EmpathyFTHandler *handler,
410                           GError *error,
411                           gpointer user_data)
412 {
413         if (error) {
414                 empathy_ft_manager_display_error (handler, error);
415         } else {
416                 empathy_receive_file_with_file_chooser (handler);
417         }
418 }
419
420 static void
421 new_ft_handler_cb (EmpathyFTFactory *factory,
422                    EmpathyFTHandler *handler,
423                    GError *error,
424                    gpointer user_data)
425 {
426         if (error) {
427                 empathy_ft_manager_display_error (handler, error);
428         } else {
429                 empathy_ft_manager_add_handler (handler);
430         }
431
432         g_object_unref (handler);
433 }
434
435 static void
436 new_call_handler_cb (EmpathyCallFactory *factory, EmpathyCallHandler *handler,
437         gboolean outgoing, gpointer user_data)
438 {
439         EmpathyCallWindow *window;
440
441         window = empathy_call_window_new (handler);
442         gtk_widget_show (GTK_WIDGET (window));
443 }
444
445 int
446 main (int argc, char *argv[])
447 {
448         guint32            startup_timestamp;
449 #if HAVE_GEOCLUE
450         EmpathyLocationManager *location_manager = NULL;
451 #endif
452         EmpathyStatusIcon *icon;
453         EmpathyDispatcher *dispatcher;
454         EmpathyLogManager *log_manager;
455         EmpathyChatroomManager *chatroom_manager;
456         EmpathyCallFactory *call_factory;
457         EmpathyFTFactory  *ft_factory;
458         GtkWidget         *window;
459         MissionControl    *mc;
460         EmpathyIdle       *idle;
461         gboolean           autoconnect = TRUE;
462         gboolean           no_connect = FALSE;
463         gboolean           hide_contact_list = FALSE;
464         gboolean           accounts_dialog = FALSE;
465         GError            *error = NULL;
466         GOptionEntry       options[] = {
467                 { "no-connect", 'n',
468                   0, G_OPTION_ARG_NONE, &no_connect,
469                   N_("Don't connect on startup"),
470                   NULL },
471                 { "hide-contact-list", 'h',
472                   0, G_OPTION_ARG_NONE, &hide_contact_list,
473                   N_("Don't show the contact list on startup"),
474                   NULL },
475                 { "accounts", 'a',
476                   0, G_OPTION_ARG_NONE, &accounts_dialog,
477                   N_("Show the accounts dialog"),
478                   NULL },
479                 { "version", 'v',
480                   G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, show_version_cb, NULL, NULL },
481                 { NULL }
482         };
483
484         /* Init */
485         g_thread_init (NULL);
486         empathy_init ();
487
488         if (!gtk_init_with_args (&argc, &argv,
489                                  N_("- Empathy Instant Messenger"),
490                                  options, GETTEXT_PACKAGE, &error)) {
491                 g_warning ("Error in empathy init: %s", error->message);
492                 return EXIT_FAILURE;
493         }
494
495         empathy_gtk_init ();
496         g_set_application_name (_(PACKAGE_NAME));
497         g_setenv ("PULSE_PROP_media.role", "phone", TRUE);
498
499         gst_init (&argc, &argv);
500
501 #if HAVE_LIBCHAMPLAIN
502         gtk_clutter_init (&argc, &argv);
503 #endif
504
505         gtk_window_set_default_icon_name ("empathy");
506         textdomain (GETTEXT_PACKAGE);
507
508         /* Setting up the bacon connection */
509         startup_timestamp = get_startup_timestamp ();
510         connection = bacon_message_connection_new ("empathy");
511         if (connection != NULL) {
512                 if (!bacon_message_connection_get_is_server (connection)) {
513                         gchar *message;
514
515                         if (accounts_dialog) {
516                                 DEBUG ("Showing accounts dialog from existing Empathy instance");
517
518                                 message = g_strdup ("accounts");
519
520                         } else {
521
522                                 DEBUG ("Activating existing instance");
523
524                                 message = g_strdup_printf ("%" G_GUINT32_FORMAT,
525                                                            startup_timestamp);
526                         }
527
528                         bacon_message_connection_send (connection, message);
529
530                         /* We never popup a window, so tell startup-notification
531                          * that we are done. */
532                         gdk_notify_startup_complete ();
533
534                         g_free (message);
535                         bacon_message_connection_free (connection);
536
537                         return EXIT_SUCCESS;
538                 }
539         } else {
540                 g_warning ("Cannot create the 'empathy' bacon connection.");
541         }
542
543         /* Setting up MC */
544         mc = empathy_mission_control_dup_singleton ();
545         g_signal_connect (mc, "ServiceEnded",
546                           G_CALLBACK (service_ended_cb),
547                           NULL);
548         g_signal_connect (mc, "Error",
549                           G_CALLBACK (operation_error_cb),
550                           NULL);
551
552         if (accounts_dialog) {
553                 GtkWidget *dialog;
554
555                 dialog = empathy_accounts_dialog_show (NULL, NULL);
556                 g_signal_connect (dialog, "destroy",
557                                   G_CALLBACK (gtk_main_quit),
558                                   NULL);
559
560                 gtk_main ();
561                 return 0;
562         }
563
564         /* Setting up Idle */
565         idle = empathy_idle_dup_singleton ();
566         empathy_idle_set_auto_away (idle, TRUE);
567         use_nm_notify_cb (empathy_conf_get (), EMPATHY_PREFS_USE_NM, idle);
568         empathy_conf_notify_add (empathy_conf_get (), EMPATHY_PREFS_USE_NM,
569                                  use_nm_notify_cb, idle);
570
571         /* Autoconnect */
572         empathy_conf_get_bool (empathy_conf_get (),
573                                EMPATHY_PREFS_AUTOCONNECT,
574                                &autoconnect);
575         if (autoconnect && ! no_connect &&
576                 tp_connection_presence_type_cmp_availability (empathy_idle_get_state
577                         (idle), TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0) {
578                 empathy_idle_set_state (idle, MC_PRESENCE_AVAILABLE);
579         }
580         
581         create_salut_account ();
582
583         /* Setting up UI */
584         window = empathy_main_window_show ();
585         icon = empathy_status_icon_new (GTK_WINDOW (window), hide_contact_list);
586
587         if (connection) {
588                 /* We se the callback here because we need window */
589                 bacon_message_connection_set_callback (connection,
590                                                        on_bacon_message_received,
591                                                        window);
592         }
593
594         /* Handle channels */
595         dispatcher = empathy_dispatcher_dup_singleton ();
596         g_signal_connect (dispatcher, "dispatch", G_CALLBACK (dispatch_cb), NULL);
597
598         /* Logging */
599         log_manager = empathy_log_manager_dup_singleton ();
600         empathy_log_manager_observe (log_manager, dispatcher);
601
602         chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
603         empathy_chatroom_manager_observe (chatroom_manager, dispatcher);
604
605         notify_init (_(PACKAGE_NAME));
606         /* Create the call factory */
607         call_factory = empathy_call_factory_initialise ();
608         g_signal_connect (G_OBJECT (call_factory), "new-call-handler",
609                 G_CALLBACK (new_call_handler_cb), NULL);
610         /* Create the FT factory */
611         ft_factory = empathy_ft_factory_dup_singleton ();
612         g_signal_connect (ft_factory, "new-ft-handler",
613                 G_CALLBACK (new_ft_handler_cb), NULL);
614         g_signal_connect (ft_factory, "new-incoming-transfer",
615                 G_CALLBACK (new_incoming_transfer_cb), NULL);
616
617         /* Location mananger */
618 #if HAVE_GEOCLUE
619         location_manager = empathy_location_manager_dup_singleton ();
620 #endif
621
622         gtk_main ();
623
624         empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
625
626         g_object_unref (mc);
627         g_object_unref (idle);
628         g_object_unref (icon);
629         g_object_unref (log_manager);
630         g_object_unref (dispatcher);
631         g_object_unref (chatroom_manager);
632         g_object_unref (ft_manager);
633 #if HAVE_GEOCLUE
634         g_object_unref (location_manager);
635 #endif
636         g_object_unref (ft_factory);
637
638         notify_uninit ();
639
640         return EXIT_SUCCESS;
641 }
642