]> git.0d.be Git - empathy.git/blob - src/empathy.c
Fix includes
[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
28 #include <glib.h>
29 #include <glib/gi18n.h>
30 #include <gtk/gtk.h>
31 #include <gdk/gdkx.h>
32
33 #include <libebook/e-book.h>
34
35 #include <telepathy-glib/util.h>
36 #include <libmissioncontrol/mc-account.h>
37 #include <libmissioncontrol/mission-control.h>
38
39 #include <libempathy/empathy-idle.h>
40 #include <libempathy/empathy-tp-chat.h>
41 #include <libempathy/empathy-tp-chatroom.h>
42 #include <libempathy/empathy-chandler.h>
43 #include <libempathy/empathy-utils.h>
44 #include <libempathy/empathy-debug.h>
45
46 #include <libempathy-gtk/empathy-conf.h>
47 #include <libempathy-gtk/empathy-chat.h>
48 #include <libempathy-gtk/empathy-private-chat.h>
49 #include <libempathy-gtk/empathy-group-chat.h>
50
51 #include "empathy-main-window.h"
52 #include "empathy-status-icon.h"
53 #include "empathy-chat-window.h"
54 #include "bacon-message-connection.h"
55
56 #define DEBUG_DOMAIN "EmpathyMain"
57 #define BUS_NAME "org.gnome.Empathy.ChatChandler"
58 #define OBJECT_PATH "/org/gnome/Empathy/ChatChandler"
59
60 static BaconMessageConnection *connection = NULL;
61
62 static void
63 new_text_channel_cb (EmpathyChandler *chandler,
64                      TpConn          *tp_conn,
65                      TpChan          *tp_chan,
66                      MissionControl  *mc)
67 {
68         EmpathyTpChat *tp_chat;
69         McAccount     *account;
70         EmpathyChat   *chat;
71         gchar         *id;
72
73         account = mission_control_get_account_for_connection (mc, tp_conn, NULL);
74         id = empathy_inspect_channel (account, tp_chan);
75         chat = empathy_chat_window_find_chat (account, id);
76         g_free (id);
77
78         if (chat) {
79                 /* The chat already exists */
80                 if (!empathy_chat_is_connected (chat)) {
81                         /* The chat died, give him the new text channel */
82                         if (empathy_chat_is_group_chat (chat)) {
83                                 tp_chat = EMPATHY_TP_CHAT (empathy_tp_chatroom_new (account, tp_chan));
84                         } else {
85                                 tp_chat = empathy_tp_chat_new (account, tp_chan);
86                         }
87                         empathy_chat_set_tp_chat (chat, tp_chat);
88                         g_object_unref (tp_chat);
89                 }
90                 empathy_chat_window_present_chat (chat);
91
92                 g_object_unref (account);
93                 return;
94         }
95
96         if (tp_chan->handle_type == TP_HANDLE_TYPE_CONTACT) {
97                 /* We have a new private chat channel */
98                 tp_chat = empathy_tp_chat_new (account, tp_chan);
99                 chat = EMPATHY_CHAT (empathy_private_chat_new (tp_chat));
100         }
101         else if (tp_chan->handle_type == TP_HANDLE_TYPE_ROOM) {
102                 /* We have a new group chat channel */
103                 tp_chat = EMPATHY_TP_CHAT (empathy_tp_chatroom_new (account, tp_chan));
104                 chat = EMPATHY_CHAT (empathy_group_chat_new (EMPATHY_TP_CHATROOM (tp_chat)));
105         } else {
106                 empathy_debug (DEBUG_DOMAIN,
107                                "Unknown handle type (%d) for Text channel",
108                                tp_chan->handle_type);
109                 g_object_unref (account);
110                 return;
111         }
112
113         empathy_chat_window_present_chat (chat);
114
115         g_object_unref (chat);
116         g_object_unref (account);
117         g_object_unref (tp_chat);
118 }
119
120 static void
121 service_ended_cb (MissionControl *mc,
122                   gpointer        user_data)
123 {
124         empathy_debug (DEBUG_DOMAIN, "Mission Control stopped");
125 }
126
127 static void
128 operation_error_cb (MissionControl *mc,
129                     guint           operation_id,
130                     guint           error_code,
131                     gpointer        user_data)
132 {
133         const gchar *message;
134
135         switch (error_code) {
136         case MC_DISCONNECTED_ERROR:
137                 message = _("Disconnected");
138                 break;
139         case MC_INVALID_HANDLE_ERROR:
140                 message = _("Invalid handle");
141                 break;
142         case MC_NO_MATCHING_CONNECTION_ERROR:
143                 message = _("No matching connection");
144                 break;
145         case MC_INVALID_ACCOUNT_ERROR:
146                 message = _("Invalid account");
147                 break;
148         case MC_PRESENCE_FAILURE_ERROR:
149                 message = _("Presence failure");
150                 break;
151         case MC_NO_ACCOUNTS_ERROR:
152                 message = _("No accounts");
153                 break;
154         case MC_NETWORK_ERROR:
155                 message = _("Network error");
156                 break;
157         case MC_CONTACT_DOES_NOT_SUPPORT_VOICE_ERROR:
158                 message = _("Contact does not support voice");
159                 break;
160         case MC_LOWMEM_ERROR:
161                 message = _("Lowmem");
162                 break;
163         case MC_CHANNEL_REQUEST_GENERIC_ERROR:
164                 message = _("Channel request generic error");
165                 break;
166         case MC_CHANNEL_BANNED_ERROR:
167                 message = _("Channel banned");
168                 break;
169         case MC_CHANNEL_FULL_ERROR:
170                 message = _("Channel full");
171                 break;
172         case MC_CHANNEL_INVITE_ONLY_ERROR:
173                 message = _("Channel invite only");
174                 break;
175         default:
176                 message = _("Unknown error code");
177         }
178
179         empathy_debug (DEBUG_DOMAIN, "Error during operation %d: %s",
180                        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
212         /* Check if we already created a salut account */
213         empathy_conf_get_bool (empathy_conf_get(),
214                                EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
215                                &salut_created);
216         if (salut_created) {
217                 return;
218         }
219
220         empathy_debug (DEBUG_DOMAIN, "Try to add a salut account...");
221
222         /* Check if the salut CM is installed */
223         profile = mc_profile_lookup ("salut");
224         protocol = mc_profile_get_protocol (profile);
225         if (!protocol) {
226                 empathy_debug (DEBUG_DOMAIN, "Salut not installed");
227                 g_object_unref (profile);
228                 return;
229         }
230         g_object_unref (protocol);
231
232         /* Get self EContact from EDS */
233         if (!e_book_get_self (&contact, &book, NULL)) {
234                 empathy_debug (DEBUG_DOMAIN, "Failed to get self econtact");
235                 g_object_unref (profile);
236                 return;
237         }
238
239         empathy_conf_set_bool (empathy_conf_get (),
240                                EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
241                                TRUE);
242
243         /* Check if there is already a salut account */
244         accounts = mc_accounts_list_by_profile (profile);
245         if (accounts) {
246                 empathy_debug (DEBUG_DOMAIN, "There is already a salut account");
247                 mc_accounts_list_free (accounts);
248                 g_object_unref (profile);
249                 return;
250         }
251
252         account = mc_account_create (profile);
253         mc_account_set_display_name (account, _("People nearby"));
254         
255         nickname = e_contact_get (contact, E_CONTACT_NICKNAME);
256         first_name = e_contact_get (contact, E_CONTACT_GIVEN_NAME);
257         last_name = e_contact_get (contact, E_CONTACT_FAMILY_NAME);
258         email = e_contact_get (contact, E_CONTACT_EMAIL_1);
259         jid = e_contact_get (contact, E_CONTACT_IM_JABBER_HOME_1);
260         
261         if (!tp_strdiff (nickname, "nickname")) {
262                 g_free (nickname);
263                 nickname = NULL;
264         }
265
266         empathy_debug (DEBUG_DOMAIN, "Salut account created:\n"
267                                      "  nickname=%s\n"
268                                      "  first-name=%s\n"
269                                      "  last-name=%s\n"
270                                      "  email=%s\n"
271                                      "  jid=%s\n",
272                        nickname, first_name, last_name, email, jid);
273
274         mc_account_set_param_string (account, "nickname", nickname ? nickname : "");
275         mc_account_set_param_string (account, "first-name", first_name ? first_name : "");
276         mc_account_set_param_string (account, "last-name", last_name ? last_name : "");
277         mc_account_set_param_string (account, "email", email ? email : "");
278         mc_account_set_param_string (account, "jid", jid ? jid : "");
279
280         g_free (nickname);
281         g_free (first_name);
282         g_free (last_name);
283         g_free (email);
284         g_free (jid);
285         g_object_unref (account);
286         g_object_unref (profile);
287         g_object_unref (contact);
288         g_object_unref (book);
289 }
290
291 /* The code that handles single-instance and startup notification is
292  * copied from gedit.
293  *
294  * Copyright (C) 2005 - Paolo Maggi 
295  */
296 static void
297 on_bacon_message_received (const char *message,
298                            gpointer    data)
299 {
300         GtkWidget *window = data;
301         guint32    startup_timestamp;
302
303         g_return_if_fail (message != NULL);
304
305         empathy_debug (DEBUG_DOMAIN,
306                        "Other instance launched, presenting the main window "
307                        "(message is '%s')", message);
308
309         startup_timestamp = atoi (message);
310
311         /* Set the proper interaction time on the window.
312          * Fall back to roundtripping to the X server when we
313          * don't have the timestamp, e.g. when launched from
314          * terminal. We also need to make sure that the window
315          * has been realized otherwise it will not work. lame. */
316         if (startup_timestamp == 0) {
317                 /* Work if launched from the terminal */
318                 empathy_debug (DEBUG_DOMAIN, "Using X server timestamp as a fallback");
319
320                 if (!GTK_WIDGET_REALIZED (window)) {
321                         gtk_widget_realize (GTK_WIDGET (window));
322                 }
323
324                 startup_timestamp = gdk_x11_get_server_time (window->window);
325         }
326
327         gtk_window_present_with_time (GTK_WINDOW (window), startup_timestamp);
328 }
329
330 static guint32
331 get_startup_timestamp ()
332 {
333         const gchar *startup_id_env;
334         gchar       *startup_id = NULL;
335         gchar       *time_str;
336         gchar       *end;
337         gulong       retval = 0;
338
339         /* we don't unset the env, since startup-notification
340          * may still need it */
341         startup_id_env = g_getenv ("DESKTOP_STARTUP_ID");
342         if (startup_id_env == NULL) {
343                 goto out;
344         }
345
346         startup_id = g_strdup (startup_id_env);
347
348         time_str = g_strrstr (startup_id, "_TIME");
349         if (time_str == NULL) {
350                 goto out;
351         }
352
353         errno = 0;
354
355         /* Skip past the "_TIME" part */
356         time_str += 5;
357
358         retval = strtoul (time_str, &end, 0);
359         if (end == time_str || errno != 0)
360                 retval = 0;
361
362  out:
363         g_free (startup_id);
364
365         return (retval > 0) ? retval : 0;
366 }
367
368 int
369 main (int argc, char *argv[])
370 {
371         guint32            startup_timestamp;
372         EmpathyStatusIcon *icon;
373         GtkWidget         *window;
374         MissionControl    *mc;
375         EmpathyIdle       *idle;
376         EmpathyChandler   *chandler;
377         gboolean           autoconnect = TRUE;
378         GError            *error = NULL;
379
380         empathy_debug_set_log_file_from_env ();
381
382         bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
383         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
384         textdomain (GETTEXT_PACKAGE);
385
386         startup_timestamp = get_startup_timestamp ();
387
388         if (!gtk_init_with_args (&argc, &argv,
389                                  _("- Empathy Instant Messenger"),
390                                  NULL, GETTEXT_PACKAGE, &error)) {
391                 empathy_debug (DEBUG_DOMAIN, error->message);
392                 return EXIT_FAILURE;
393         }
394
395         g_set_application_name (PACKAGE_NAME);
396
397         gtk_window_set_default_icon_name ("empathy");
398         gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
399                                            PKGDATADIR G_DIR_SEPARATOR_S "icons");
400
401         /* Setting up the bacon connection */
402         connection = bacon_message_connection_new ("empathy");
403         if (connection != NULL) {
404                 if (!bacon_message_connection_get_is_server (connection)) {
405                         gchar *message;
406
407                         empathy_debug (DEBUG_DOMAIN, "Activating existing instance");
408
409                         message = g_strdup_printf ("%" G_GUINT32_FORMAT,
410                                                    startup_timestamp);
411                         bacon_message_connection_send (connection, message);
412
413                         /* We never popup a window, so tell startup-notification
414                          * that we are done. */
415                         gdk_notify_startup_complete ();
416
417                         g_free (message);
418                         bacon_message_connection_free (connection);
419
420                         return EXIT_SUCCESS;
421                 }
422         } else {
423                 g_warning ("Cannot create the 'empathy' bacon connection.");
424         }
425
426         /* Setting up MC */
427         mc = empathy_mission_control_new ();
428         g_signal_connect (mc, "ServiceEnded",
429                           G_CALLBACK (service_ended_cb),
430                           NULL);
431         g_signal_connect (mc, "Error",
432                           G_CALLBACK (operation_error_cb),
433                           NULL);
434
435         /* Setting up Idle */
436         idle = empathy_idle_new ();
437         empathy_idle_set_auto_away (idle, TRUE);
438         use_nm_notify_cb (empathy_conf_get (), EMPATHY_PREFS_USE_NM, idle);
439         empathy_conf_notify_add (empathy_conf_get (), EMPATHY_PREFS_USE_NM,
440                                  use_nm_notify_cb, idle);
441
442         /* Autoconnect */
443         empathy_conf_get_bool (empathy_conf_get(),
444                                EMPATHY_PREFS_AUTOCONNECT,
445                                &autoconnect);
446         if (autoconnect &&
447             empathy_idle_get_state (idle) <= MC_PRESENCE_OFFLINE) {
448                 empathy_idle_set_state (idle, MC_PRESENCE_AVAILABLE);
449         }
450         
451         create_salut_account ();
452
453         /* Setting up UI */
454         window = empathy_main_window_show ();
455         icon = empathy_status_icon_new (GTK_WINDOW (window));
456
457         if (connection) {
458                 /* We se the callback here because we need window */
459                 bacon_message_connection_set_callback (connection,
460                                                        on_bacon_message_received,
461                                                        window);
462         }
463
464         /* Handle text channels */
465         chandler = empathy_chandler_new (BUS_NAME, OBJECT_PATH);
466         g_signal_connect (chandler, "new-channel",
467                           G_CALLBACK (new_text_channel_cb),
468                           mc);
469         empathy_debug (DEBUG_DOMAIN, "Ready to handle new text channels");
470
471         gtk_main ();
472
473         empathy_idle_set_state (idle, MC_PRESENCE_OFFLINE);
474
475         g_object_unref (chandler);
476         g_object_unref (mc);
477         g_object_unref (idle);
478         g_object_unref (icon);
479
480         return EXIT_SUCCESS;
481 }
482