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