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