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