]> git.0d.be Git - empathy.git/blob - src/empathy.c
Rethink a bit the logic for an incoming transfer.
[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 == EMP_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                           gpointer user_data)
411 {
412         empathy_receive_file_with_file_chooser (handler);
413 }
414
415 static void
416 new_ft_handler_cb (EmpathyFTFactory *factory,
417                    EmpathyFTHandler *handler,
418                    gpointer user_data)
419 {
420         /* TODO: add to FT window */
421 }
422
423 static void
424 new_call_handler_cb (EmpathyCallFactory *factory, EmpathyCallHandler *handler,
425         gboolean outgoing, gpointer user_data)
426 {
427                 EmpathyCallWindow *window;
428
429                 window = empathy_call_window_new (handler);
430                 gtk_widget_show (GTK_WIDGET (window));
431 }
432
433 int
434 main (int argc, char *argv[])
435 {
436         guint32            startup_timestamp;
437 #if HAVE_GEOCLUE
438         EmpathyLocationManager *location_manager = NULL;
439 #endif
440         EmpathyStatusIcon *icon;
441         EmpathyDispatcher *dispatcher;
442         EmpathyLogManager *log_manager;
443         EmpathyChatroomManager *chatroom_manager;
444         EmpathyFTManager  *ft_manager;
445         EmpathyCallFactory *call_factory;
446         EmpathyFTFactory  *ft_factory;
447         GtkWidget         *window;
448         MissionControl    *mc;
449         EmpathyIdle       *idle;
450         gboolean           autoconnect = TRUE;
451         gboolean           no_connect = FALSE;
452         gboolean           hide_contact_list = FALSE;
453         gboolean           accounts_dialog = FALSE;
454         GError            *error = NULL;
455         GOptionEntry       options[] = {
456                 { "no-connect", 'n',
457                   0, G_OPTION_ARG_NONE, &no_connect,
458                   N_("Don't connect on startup"),
459                   NULL },
460                 { "hide-contact-list", 'h',
461                   0, G_OPTION_ARG_NONE, &hide_contact_list,
462                   N_("Don't show the contact list on startup"),
463                   NULL },
464                 { "accounts", 'a',
465                   0, G_OPTION_ARG_NONE, &accounts_dialog,
466                   N_("Show the accounts dialog"),
467                   NULL },
468                 { "version", 'v',
469                   G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, show_version_cb, NULL, NULL },
470                 { NULL }
471         };
472
473         /* Init */
474         g_thread_init (NULL);
475         empathy_init ();
476
477         if (!gtk_init_with_args (&argc, &argv,
478                                  N_("- Empathy Instant Messenger"),
479                                  options, GETTEXT_PACKAGE, &error)) {
480                 g_warning ("Error in empathy init: %s", error->message);
481                 return EXIT_FAILURE;
482         }
483
484         empathy_gtk_init ();
485         g_set_application_name (_(PACKAGE_NAME));
486         g_setenv ("PULSE_PROP_media.role", "phone", TRUE);
487
488         gst_init (&argc, &argv);
489
490 #if HAVE_LIBCHAMPLAIN
491         gtk_clutter_init (&argc, &argv);
492 #endif
493
494         gtk_window_set_default_icon_name ("empathy");
495         textdomain (GETTEXT_PACKAGE);
496
497         /* Setting up the bacon connection */
498         startup_timestamp = get_startup_timestamp ();
499         connection = bacon_message_connection_new ("empathy");
500         if (connection != NULL) {
501                 if (!bacon_message_connection_get_is_server (connection)) {
502                         gchar *message;
503
504                         if (accounts_dialog) {
505                                 DEBUG ("Showing accounts dialog from existing Empathy instance");
506
507                                 message = g_strdup ("accounts");
508
509                         } else {
510
511                                 DEBUG ("Activating existing instance");
512
513                                 message = g_strdup_printf ("%" G_GUINT32_FORMAT,
514                                                            startup_timestamp);
515                         }
516
517                         bacon_message_connection_send (connection, message);
518
519                         /* We never popup a window, so tell startup-notification
520                          * that we are done. */
521                         gdk_notify_startup_complete ();
522
523                         g_free (message);
524                         bacon_message_connection_free (connection);
525
526                         return EXIT_SUCCESS;
527                 }
528         } else {
529                 g_warning ("Cannot create the 'empathy' bacon connection.");
530         }
531
532         /* Setting up MC */
533         mc = empathy_mission_control_dup_singleton ();
534         g_signal_connect (mc, "ServiceEnded",
535                           G_CALLBACK (service_ended_cb),
536                           NULL);
537         g_signal_connect (mc, "Error",
538                           G_CALLBACK (operation_error_cb),
539                           NULL);
540
541         if (accounts_dialog) {
542                 GtkWidget *dialog;
543
544                 dialog = empathy_accounts_dialog_show (NULL, NULL);
545                 g_signal_connect (dialog, "destroy",
546                                   G_CALLBACK (gtk_main_quit),
547                                   NULL);
548
549                 gtk_main ();
550                 return 0;
551         }
552
553         /* Setting up Idle */
554         idle = empathy_idle_dup_singleton ();
555         empathy_idle_set_auto_away (idle, TRUE);
556         use_nm_notify_cb (empathy_conf_get (), EMPATHY_PREFS_USE_NM, idle);
557         empathy_conf_notify_add (empathy_conf_get (), EMPATHY_PREFS_USE_NM,
558                                  use_nm_notify_cb, idle);
559
560         /* Autoconnect */
561         empathy_conf_get_bool (empathy_conf_get (),
562                                EMPATHY_PREFS_AUTOCONNECT,
563                                &autoconnect);
564         if (autoconnect && ! no_connect &&
565                 tp_connection_presence_type_cmp_availability (empathy_idle_get_state
566                         (idle), TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0) {
567                 empathy_idle_set_state (idle, MC_PRESENCE_AVAILABLE);
568         }
569         
570         create_salut_account ();
571
572         /* Setting up UI */
573         window = empathy_main_window_show ();
574         icon = empathy_status_icon_new (GTK_WINDOW (window), hide_contact_list);
575
576         if (connection) {
577                 /* We se the callback here because we need window */
578                 bacon_message_connection_set_callback (connection,
579                                                        on_bacon_message_received,
580                                                        window);
581         }
582
583         /* Handle channels */
584         dispatcher = empathy_dispatcher_dup_singleton ();
585         g_signal_connect (dispatcher, "dispatch", G_CALLBACK (dispatch_cb), NULL);
586
587         /* Logging */
588         log_manager = empathy_log_manager_dup_singleton ();
589         empathy_log_manager_observe (log_manager, dispatcher);
590
591         chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
592         empathy_chatroom_manager_observe (chatroom_manager, dispatcher);
593
594         ft_manager = empathy_ft_manager_dup_singleton ();
595
596         notify_init (_(PACKAGE_NAME));
597         /* Create the call factory */
598         call_factory = empathy_call_factory_initialise ();
599         g_signal_connect (G_OBJECT (call_factory), "new-call-handler",
600                 G_CALLBACK (new_call_handler_cb), NULL);
601         /* Create the FT factory */
602         ft_factory = empathy_ft_factory_dup_singleton ();
603         g_signal_connect (ft_factory, "new-ft-handler",
604                 G_CALLBACK (new_ft_handler_cb), NULL);
605         g_signal_connect (ft_factory, "new-incoming-transfer",
606                 G_CALLBACK (new_incoming_transfer_cb), NULL);
607
608         /* Location mananger */
609 #if HAVE_GEOCLUE
610         location_manager = empathy_location_manager_dup_singleton ();
611 #endif
612
613         gtk_main ();
614
615         empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
616
617         g_object_unref (mc);
618         g_object_unref (idle);
619         g_object_unref (icon);
620         g_object_unref (log_manager);
621         g_object_unref (dispatcher);
622         g_object_unref (chatroom_manager);
623         g_object_unref (ft_manager);
624 #if HAVE_GEOCLUE
625         g_object_unref (location_manager);
626 #endif
627         g_object_unref (ft_factory);
628
629         notify_uninit ();
630
631         return EXIT_SUCCESS;
632 }
633