1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007 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>
28 #include <glib/gi18n.h>
31 #include <libebook/e-book.h>
32 #include <libgnomevfs/gnome-vfs.h>
34 #include <telepathy-glib/util.h>
35 #include <libmissioncontrol/mc-account.h>
36 #include <libmissioncontrol/mc-account-monitor.h>
37 #include <libmissioncontrol/mission-control.h>
39 #include <libempathy/empathy-idle.h>
40 #include <libempathy/empathy-utils.h>
41 #include <libempathy/empathy-debug.h>
43 #include <libempathy-gtk/empathy-conf.h>
44 #include <libempathy-gtk/empathy-preferences.h>
45 #include <libempathy-gtk/empathy-main-window.h>
46 #include <libempathy-gtk/empathy-status-icon.h>
48 #define DEBUG_DOMAIN "EmpathyMain"
51 service_ended_cb (MissionControl *mc,
54 empathy_debug (DEBUG_DOMAIN, "Mission Control stopped");
58 operation_error_cb (MissionControl *mc,
63 empathy_debug (DEBUG_DOMAIN, "Error code %d during operation %d",
69 start_mission_control (EmpathyIdle *idle)
73 presence = empathy_idle_get_state (idle);
75 if (presence > MC_PRESENCE_OFFLINE) {
76 /* MC is already running and online, nothing to do */
80 empathy_idle_set_state (idle, MC_PRESENCE_AVAILABLE);
84 account_enabled_cb (McAccountMonitor *monitor,
88 empathy_debug (DEBUG_DOMAIN, "Account enabled: %s", unique_name);
89 start_mission_control (idle);
93 create_salut_account (void)
97 gboolean salut_created;
102 gchar *nickname = NULL;
103 gchar *published_name = NULL;
104 gchar *first_name = NULL;
105 gchar *last_name = NULL;
109 /* Check if we already created a salut account */
110 if (!empathy_conf_get_bool (empathy_conf_get(),
111 EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
119 empathy_debug (DEBUG_DOMAIN, "Try to add a salut account...");
121 /* Check if the salut CM is installed */
122 profile = mc_profile_lookup ("salut");
123 protocol = mc_profile_get_protocol (profile);
125 empathy_debug (DEBUG_DOMAIN, "Salut not installed");
126 g_object_unref (profile);
129 g_object_unref (protocol);
131 /* Get self EContact from EDS */
132 if (!e_book_get_self (&contact, &book, NULL)) {
133 empathy_debug (DEBUG_DOMAIN, "Failed to get self econtact");
134 g_object_unref (profile);
138 empathy_conf_set_bool (empathy_conf_get (),
139 EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
142 /* Check if there is already a salut account */
143 accounts = mc_accounts_list_by_profile (profile);
145 empathy_debug (DEBUG_DOMAIN, "There is already a salut account");
146 mc_accounts_list_free (accounts);
147 g_object_unref (profile);
151 account = mc_account_create (profile);
152 mc_account_set_display_name (account, _("People nearby"));
154 nickname = e_contact_get (contact, E_CONTACT_NICKNAME);
155 published_name = e_contact_get (contact, E_CONTACT_FULL_NAME);
156 first_name = e_contact_get (contact, E_CONTACT_GIVEN_NAME);
157 last_name = e_contact_get (contact, E_CONTACT_FAMILY_NAME);
158 email = e_contact_get (contact, E_CONTACT_EMAIL_1);
159 jid = e_contact_get (contact, E_CONTACT_IM_JABBER_HOME_1);
161 if (G_STR_EMPTY (nickname) || !tp_strdiff (nickname, "nickname")) {
163 nickname = g_strdup (g_get_user_name ());
165 if (G_STR_EMPTY (published_name)) {
166 g_free (published_name);
167 published_name = g_strdup (g_get_real_name ());
170 empathy_debug (DEBUG_DOMAIN, "Salut account created:\n"
172 " published-name=%s\n"
177 nickname, published_name, first_name, last_name, email, jid);
179 mc_account_set_param_string (account, "nickname", nickname ? nickname : "");
180 mc_account_set_param_string (account, "published-name", published_name ? published_name : "");
181 mc_account_set_param_string (account, "first-name", first_name ? first_name : "");
182 mc_account_set_param_string (account, "last-name", last_name ? last_name : "");
183 mc_account_set_param_string (account, "email", email ? email : "");
184 mc_account_set_param_string (account, "jid", jid ? jid : "");
187 g_free (published_name);
192 g_object_unref (account);
193 g_object_unref (profile);
194 g_object_unref (contact);
195 g_object_unref (book);
199 main (int argc, char *argv[])
201 EmpathyStatusIcon *icon;
204 McAccountMonitor *monitor;
206 gboolean autoconnect = TRUE;
207 GError *error = NULL;
209 empathy_debug_set_log_file_from_env ();
211 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
212 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
213 textdomain (GETTEXT_PACKAGE);
215 if (!gtk_init_with_args (&argc, &argv,
216 _("- Empathy Instant Messenger"),
217 NULL, GETTEXT_PACKAGE, &error)) {
218 empathy_debug (DEBUG_DOMAIN, error->message);
222 g_set_application_name (PACKAGE_NAME);
224 gtk_window_set_default_icon_name ("empathy");
225 gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
226 PKGDATADIR G_DIR_SEPARATOR_S "icons");
230 monitor = mc_account_monitor_new ();
231 mc = empathy_mission_control_new ();
232 idle = empathy_idle_new ();
233 g_signal_connect (monitor, "account-enabled",
234 G_CALLBACK (account_enabled_cb),
236 g_signal_connect (mc, "ServiceEnded",
237 G_CALLBACK (service_ended_cb),
239 g_signal_connect (mc, "Error",
240 G_CALLBACK (operation_error_cb),
243 empathy_conf_get_bool (empathy_conf_get(),
244 EMPATHY_PREFS_AUTOCONNECT,
248 start_mission_control (idle);
251 create_salut_account ();
254 window = empathy_main_window_show ();
255 icon = empathy_status_icon_new (GTK_WINDOW (window));
259 empathy_idle_set_state (idle, MC_PRESENCE_OFFLINE);
261 g_object_unref (monitor);
263 g_object_unref (idle);
264 g_object_unref (icon);