1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007-2008 Collabora Ltd.
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.
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.
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.
20 * Authors: Xavier Claessens <xclaesse@gmail.com>
29 #include <glib/gi18n.h>
33 #include <libebook/e-book.h>
35 #include <telepathy-glib/util.h>
36 #include <libmissioncontrol/mc-account.h>
37 #include <libmissioncontrol/mission-control.h>
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>
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>
51 #include "empathy-main-window.h"
52 #include "empathy-status-icon.h"
53 #include "empathy-chat-window.h"
54 #include "bacon-message-connection.h"
56 #define DEBUG_DOMAIN "EmpathyMain"
57 #define BUS_NAME "org.gnome.Empathy.ChatChandler"
58 #define OBJECT_PATH "/org/gnome/Empathy/ChatChandler"
60 static BaconMessageConnection *connection = NULL;
63 new_text_channel_cb (EmpathyChandler *chandler,
68 EmpathyTpChat *tp_chat;
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);
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));
85 tp_chat = empathy_tp_chat_new (account, tp_chan);
87 empathy_chat_set_tp_chat (chat, tp_chat);
88 g_object_unref (tp_chat);
90 empathy_chat_window_present_chat (chat);
92 g_object_unref (account);
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));
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)));
106 empathy_debug (DEBUG_DOMAIN,
107 "Unknown handle type (%d) for Text channel",
108 tp_chan->handle_type);
109 g_object_unref (account);
113 empathy_chat_window_present_chat (chat);
115 g_object_unref (chat);
116 g_object_unref (account);
117 g_object_unref (tp_chat);
121 service_ended_cb (MissionControl *mc,
124 empathy_debug (DEBUG_DOMAIN, "Mission Control stopped");
128 operation_error_cb (MissionControl *mc,
133 const gchar *message;
135 switch (error_code) {
136 case MC_DISCONNECTED_ERROR:
137 message = _("Disconnected");
139 case MC_INVALID_HANDLE_ERROR:
140 message = _("Invalid handle");
142 case MC_NO_MATCHING_CONNECTION_ERROR:
143 message = _("No matching connection");
145 case MC_INVALID_ACCOUNT_ERROR:
146 message = _("Invalid account");
148 case MC_PRESENCE_FAILURE_ERROR:
149 message = _("Presence failure");
151 case MC_NO_ACCOUNTS_ERROR:
152 message = _("No accounts");
154 case MC_NETWORK_ERROR:
155 message = _("Network error");
157 case MC_CONTACT_DOES_NOT_SUPPORT_VOICE_ERROR:
158 message = _("Contact does not support voice");
160 case MC_LOWMEM_ERROR:
161 message = _("Lowmem");
163 case MC_CHANNEL_REQUEST_GENERIC_ERROR:
164 message = _("Channel request generic error");
166 case MC_CHANNEL_BANNED_ERROR:
167 message = _("Channel banned");
169 case MC_CHANNEL_FULL_ERROR:
170 message = _("Channel full");
172 case MC_CHANNEL_INVITE_ONLY_ERROR:
173 message = _("Channel invite only");
176 message = _("Unknown error code");
179 empathy_debug (DEBUG_DOMAIN, "Error during operation %d: %s",
180 operation_id, message);
184 use_nm_notify_cb (EmpathyConf *conf,
188 EmpathyIdle *idle = user_data;
191 if (empathy_conf_get_bool (conf, key, &use_nm)) {
192 empathy_idle_set_use_nm (idle, use_nm);
197 create_salut_account (void)
200 McProtocol *protocol;
201 gboolean salut_created = FALSE;
206 gchar *nickname = NULL;
207 gchar *first_name = NULL;
208 gchar *last_name = NULL;
212 /* Check if we already created a salut account */
213 empathy_conf_get_bool (empathy_conf_get(),
214 EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
220 empathy_debug (DEBUG_DOMAIN, "Try to add a salut account...");
222 /* Check if the salut CM is installed */
223 profile = mc_profile_lookup ("salut");
224 protocol = mc_profile_get_protocol (profile);
226 empathy_debug (DEBUG_DOMAIN, "Salut not installed");
227 g_object_unref (profile);
230 g_object_unref (protocol);
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);
239 empathy_conf_set_bool (empathy_conf_get (),
240 EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
243 /* Check if there is already a salut account */
244 accounts = mc_accounts_list_by_profile (profile);
246 empathy_debug (DEBUG_DOMAIN, "There is already a salut account");
247 mc_accounts_list_free (accounts);
248 g_object_unref (profile);
252 account = mc_account_create (profile);
253 mc_account_set_display_name (account, _("People nearby"));
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);
261 if (!tp_strdiff (nickname, "nickname")) {
266 empathy_debug (DEBUG_DOMAIN, "Salut account created:\n"
272 nickname, first_name, last_name, email, jid);
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 : "");
285 g_object_unref (account);
286 g_object_unref (profile);
287 g_object_unref (contact);
288 g_object_unref (book);
291 /* The code that handles single-instance and startup notification is
294 * Copyright (C) 2005 - Paolo Maggi
297 on_bacon_message_received (const char *message,
300 GtkWidget *window = data;
301 guint32 startup_timestamp;
303 g_return_if_fail (message != NULL);
305 empathy_debug (DEBUG_DOMAIN,
306 "Other instance launched, presenting the main window "
307 "(message is '%s')", message);
309 startup_timestamp = atoi (message);
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");
320 if (!GTK_WIDGET_REALIZED (window)) {
321 gtk_widget_realize (GTK_WIDGET (window));
324 startup_timestamp = gdk_x11_get_server_time (window->window);
327 gtk_window_present_with_time (GTK_WINDOW (window), startup_timestamp);
331 get_startup_timestamp ()
333 const gchar *startup_id_env;
334 gchar *startup_id = NULL;
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) {
346 startup_id = g_strdup (startup_id_env);
348 time_str = g_strrstr (startup_id, "_TIME");
349 if (time_str == NULL) {
355 /* Skip past the "_TIME" part */
358 retval = strtoul (time_str, &end, 0);
359 if (end == time_str || errno != 0)
365 return (retval > 0) ? retval : 0;
369 main (int argc, char *argv[])
371 guint32 startup_timestamp;
372 EmpathyStatusIcon *icon;
376 EmpathyChandler *chandler;
377 gboolean autoconnect = TRUE;
378 GError *error = NULL;
380 empathy_debug_set_log_file_from_env ();
382 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
383 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
384 textdomain (GETTEXT_PACKAGE);
386 startup_timestamp = get_startup_timestamp ();
388 if (!gtk_init_with_args (&argc, &argv,
389 _("- Empathy Instant Messenger"),
390 NULL, GETTEXT_PACKAGE, &error)) {
391 empathy_debug (DEBUG_DOMAIN, error->message);
395 g_set_application_name (PACKAGE_NAME);
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");
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)) {
407 empathy_debug (DEBUG_DOMAIN, "Activating existing instance");
409 message = g_strdup_printf ("%" G_GUINT32_FORMAT,
411 bacon_message_connection_send (connection, message);
413 /* We never popup a window, so tell startup-notification
414 * that we are done. */
415 gdk_notify_startup_complete ();
418 bacon_message_connection_free (connection);
423 g_warning ("Cannot create the 'empathy' bacon connection.");
427 mc = empathy_mission_control_new ();
428 g_signal_connect (mc, "ServiceEnded",
429 G_CALLBACK (service_ended_cb),
431 g_signal_connect (mc, "Error",
432 G_CALLBACK (operation_error_cb),
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);
443 empathy_conf_get_bool (empathy_conf_get(),
444 EMPATHY_PREFS_AUTOCONNECT,
447 empathy_idle_get_state (idle) <= MC_PRESENCE_OFFLINE) {
448 empathy_idle_set_state (idle, MC_PRESENCE_AVAILABLE);
451 create_salut_account ();
454 window = empathy_main_window_show ();
455 icon = empathy_status_icon_new (GTK_WINDOW (window));
458 /* We se the callback here because we need window */
459 bacon_message_connection_set_callback (connection,
460 on_bacon_message_received,
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),
469 empathy_debug (DEBUG_DOMAIN, "Ready to handle new text channels");
473 empathy_idle_set_state (idle, MC_PRESENCE_OFFLINE);
475 g_object_unref (chandler);
477 g_object_unref (idle);
478 g_object_unref (icon);