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