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