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 <libtelepathy/tp-conn.h>
35 #include <libtelepathy/tp-chan.h>
36 #include <libmissioncontrol/mc-account.h>
37 #include <libmissioncontrol/mc-account-monitor.h>
38 #include <libmissioncontrol/mission-control.h>
40 #include <libempathy/empathy-debug.h>
41 #include <libempathy/empathy-utils.h>
42 #include <libempathy/empathy-presence.h>
43 #include <libempathy/empathy-contact.h>
44 #include <libempathy/empathy-chandler.h>
45 #include <libempathy/empathy-tp-chat.h>
46 #include <libempathy/empathy-tp-chatroom.h>
47 #include <libempathy/empathy-idle.h>
48 #include <libempathy/empathy-conf.h>
49 #include <libempathy-gtk/empathy-preferences.h>
50 #include <libempathy-gtk/empathy-main-window.h>
51 #include <libempathy-gtk/empathy-status-icon.h>
52 #include <libempathy-gtk/empathy-private-chat.h>
53 #include <libempathy-gtk/empathy-group-chat.h>
55 #define DEBUG_DOMAIN "EmpathyMain"
57 #define BUS_NAME "org.gnome.Empathy.Chat"
58 #define OBJECT_PATH "/org/freedesktop/Telepathy/ChannelHandler"
61 service_ended_cb (MissionControl *mc,
64 empathy_debug (DEBUG_DOMAIN, "Mission Control stopped");
68 operation_error_cb (MissionControl *mc,
73 empathy_debug (DEBUG_DOMAIN, "Error code %d during operation %d",
79 start_mission_control (EmpathyIdle *idle)
83 presence = empathy_idle_get_state (idle);
85 if (presence > MC_PRESENCE_OFFLINE) {
86 /* MC is already running and online, nothing to do */
90 empathy_idle_set_state (idle, MC_PRESENCE_AVAILABLE);
94 account_enabled_cb (McAccountMonitor *monitor,
98 empathy_debug (DEBUG_DOMAIN, "Account enabled: %s", unique_name);
99 start_mission_control (idle);
103 new_channel_cb (EmpathyChandler *chandler,
112 account = mission_control_get_account_for_connection (mc, tp_conn, NULL);
113 id = empathy_inspect_channel (account, tp_chan);
114 chat = empathy_chat_window_find_chat (account, id);
118 /* The chat already exists */
119 if (!empathy_chat_is_connected (chat)) {
120 EmpathyTpChat *tp_chat;
122 /* The chat died, give him the new text channel */
123 if (empathy_chat_is_group_chat (chat)) {
124 tp_chat = EMPATHY_TP_CHAT (empathy_tp_chatroom_new (account, tp_chan));
126 tp_chat = empathy_tp_chat_new (account, tp_chan);
128 empathy_chat_set_tp_chat (chat, tp_chat);
129 g_object_unref (tp_chat);
131 empathy_chat_present (chat);
133 g_object_unref (account);
137 if (tp_chan->handle_type == TP_HANDLE_TYPE_CONTACT) {
138 /* We have a new private chat channel */
139 chat = EMPATHY_CHAT (empathy_private_chat_new (account, tp_chan));
141 else if (tp_chan->handle_type == TP_HANDLE_TYPE_ROOM) {
142 /* We have a new group chat channel */
143 chat = EMPATHY_CHAT (empathy_group_chat_new (account, tp_chan));
146 empathy_chat_present (EMPATHY_CHAT (chat));
148 g_object_unref (chat);
149 g_object_unref (account);
153 create_salut_account (void)
156 McProtocol *protocol;
157 gboolean salut_created;
162 gchar *nickname = NULL;
163 gchar *published_name = NULL;
164 gchar *first_name = NULL;
165 gchar *last_name = NULL;
169 /* Check if we already created a salut account */
170 if (!empathy_conf_get_bool (empathy_conf_get(),
171 EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
179 empathy_debug (DEBUG_DOMAIN, "Try to add a salut account...");
181 /* Check if the salut CM is installed */
182 profile = mc_profile_lookup ("salut");
183 protocol = mc_profile_get_protocol (profile);
185 empathy_debug (DEBUG_DOMAIN, "Salut not installed");
186 g_object_unref (profile);
189 g_object_unref (protocol);
191 /* Get self EContact from EDS */
192 if (!e_book_get_self (&contact, &book, NULL)) {
193 empathy_debug (DEBUG_DOMAIN, "Failed to get self econtact");
194 g_object_unref (profile);
198 empathy_conf_set_bool (empathy_conf_get (),
199 EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
202 /* Check if there is already a salut account */
203 accounts = mc_accounts_list_by_profile (profile);
205 empathy_debug (DEBUG_DOMAIN, "There is already a salut account");
206 mc_accounts_list_free (accounts);
207 g_object_unref (profile);
211 account = mc_account_create (profile);
212 mc_account_set_display_name (account, _("People nearby"));
214 nickname = e_contact_get (contact, E_CONTACT_NICKNAME);
215 published_name = e_contact_get (contact, E_CONTACT_FULL_NAME);
216 first_name = e_contact_get (contact, E_CONTACT_GIVEN_NAME);
217 last_name = e_contact_get (contact, E_CONTACT_FAMILY_NAME);
218 email = e_contact_get (contact, E_CONTACT_EMAIL_1);
219 jid = e_contact_get (contact, E_CONTACT_IM_JABBER_HOME_1);
221 if (G_STR_EMPTY (nickname) || !empathy_strdiff (nickname, "nickname")) {
223 nickname = g_strdup (g_get_user_name ());
225 if (G_STR_EMPTY (published_name)) {
226 g_free (published_name);
227 published_name = g_strdup (g_get_real_name ());
230 empathy_debug (DEBUG_DOMAIN, "Salut account created:\n"
232 " published-name=%s\n"
237 nickname, published_name, first_name, last_name, email, jid);
239 mc_account_set_param_string (account, "nickname", nickname ? nickname : "");
240 mc_account_set_param_string (account, "published-name", published_name ? published_name : "");
241 mc_account_set_param_string (account, "first-name", first_name ? first_name : "");
242 mc_account_set_param_string (account, "last-name", last_name ? last_name : "");
243 mc_account_set_param_string (account, "email", email ? email : "");
244 mc_account_set_param_string (account, "jid", jid ? jid : "");
247 g_free (published_name);
252 g_object_unref (account);
253 g_object_unref (profile);
254 g_object_unref (contact);
255 g_object_unref (book);
259 main (int argc, char *argv[])
261 EmpathyStatusIcon *icon;
264 McAccountMonitor *monitor;
266 EmpathyChandler *chandler;
267 gboolean no_connect = FALSE;
268 GOptionContext *context;
269 GOptionEntry options[] = {
271 0, G_OPTION_ARG_NONE, &no_connect,
272 N_("Don't connect on startup"),
277 empathy_debug_set_log_file_from_env ();
279 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
280 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
281 textdomain (GETTEXT_PACKAGE);
283 context = g_option_context_new (_("- Empathy Instant Messenger"));
284 g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);
286 g_set_application_name (PACKAGE_NAME);
288 gtk_init (&argc, &argv);
289 gtk_window_set_default_icon_name ("empathy");
290 gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
291 PKGDATADIR G_DIR_SEPARATOR_S "icons");
295 monitor = mc_account_monitor_new ();
296 mc = empathy_mission_control_new ();
297 idle = empathy_idle_new ();
298 g_signal_connect (monitor, "account-enabled",
299 G_CALLBACK (account_enabled_cb),
301 g_signal_connect (mc, "ServiceEnded",
302 G_CALLBACK (service_ended_cb),
304 g_signal_connect (mc, "Error",
305 G_CALLBACK (operation_error_cb),
309 start_mission_control (idle);
312 create_salut_account ();
315 window = empathy_main_window_show ();
316 icon = empathy_status_icon_new (GTK_WINDOW (window));
318 /* Setting up channel handler */
319 chandler = empathy_chandler_new (BUS_NAME, OBJECT_PATH);
320 g_signal_connect (chandler, "new-channel",
321 G_CALLBACK (new_channel_cb),
326 empathy_idle_set_state (idle, MC_PRESENCE_OFFLINE);
328 g_object_unref (chandler);
329 g_object_unref (monitor);
331 g_object_unref (idle);
332 g_object_unref (icon);