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