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