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