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