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 <libgnome/gnome-program.h>
32 #include <libgnomeui/gnome-ui-init.h>
33 #include <libebook/e-book.h>
35 #include <libtelepathy/tp-conn.h>
36 #include <libtelepathy/tp-chan.h>
37 #include <libmissioncontrol/mc-account.h>
38 #include <libmissioncontrol/mc-account-monitor.h>
39 #include <libmissioncontrol/mission-control.h>
41 #include <libempathy/empathy-debug.h>
42 #include <libempathy/empathy-utils.h>
43 #include <libempathy/empathy-presence.h>
44 #include <libempathy/empathy-contact.h>
45 #include <libempathy/empathy-chandler.h>
46 #include <libempathy/empathy-tp-chat.h>
47 #include <libempathy/empathy-tp-chatroom.h>
48 #include <libempathy/empathy-idle.h>
49 #include <libempathy/empathy-conf.h>
50 #include <libempathy-gtk/empathy-preferences.h>
51 #include <libempathy-gtk/empathy-main-window.h>
52 #include <libempathy-gtk/empathy-status-icon.h>
53 #include <libempathy-gtk/empathy-private-chat.h>
54 #include <libempathy-gtk/empathy-group-chat.h>
56 #define DEBUG_DOMAIN "EmpathyMain"
58 #define BUS_NAME "org.gnome.Empathy.Chat"
59 #define OBJECT_PATH "/org/freedesktop/Telepathy/ChannelHandler"
62 service_ended_cb (MissionControl *mc,
65 empathy_debug (DEBUG_DOMAIN, "Mission Control stopped");
69 operation_error_cb (MissionControl *mc,
74 empathy_debug (DEBUG_DOMAIN, "Error code %d during operation %d",
80 start_mission_control (EmpathyIdle *idle)
84 presence = empathy_idle_get_state (idle);
86 if (presence > MC_PRESENCE_OFFLINE) {
87 /* MC is already running and online, nothing to do */
91 empathy_idle_set_state (idle, MC_PRESENCE_AVAILABLE);
95 account_enabled_cb (McAccountMonitor *monitor,
99 empathy_debug (DEBUG_DOMAIN, "Account enabled: %s", unique_name);
100 start_mission_control (idle);
104 new_channel_cb (EmpathyChandler *chandler,
113 account = mission_control_get_account_for_connection (mc, tp_conn, NULL);
114 id = empathy_inspect_channel (account, tp_chan);
115 chat = empathy_chat_window_find_chat (account, id);
119 /* The chat already exists */
120 if (!empathy_chat_is_connected (chat)) {
121 EmpathyTpChat *tp_chat;
123 /* The chat died, give him the new text channel */
124 if (empathy_chat_is_group_chat (chat)) {
125 tp_chat = EMPATHY_TP_CHAT (empathy_tp_chatroom_new (account, tp_chan));
127 tp_chat = empathy_tp_chat_new (account, tp_chan);
129 empathy_chat_set_tp_chat (chat, tp_chat);
130 g_object_unref (tp_chat);
132 empathy_chat_present (chat);
134 g_object_unref (account);
138 if (tp_chan->handle_type == TP_HANDLE_TYPE_CONTACT) {
139 /* We have a new private chat channel */
140 chat = EMPATHY_CHAT (empathy_private_chat_new (account, tp_chan));
142 else if (tp_chan->handle_type == TP_HANDLE_TYPE_ROOM) {
143 /* We have a new group chat channel */
144 chat = EMPATHY_CHAT (empathy_group_chat_new (account, tp_chan));
147 empathy_chat_present (EMPATHY_CHAT (chat));
149 g_object_unref (chat);
150 g_object_unref (account);
154 create_salut_account (void)
157 McProtocol *protocol;
158 gboolean salut_created;
163 gchar *nickname = NULL;
164 gchar *published_name = NULL;
165 gchar *first_name = NULL;
166 gchar *last_name = NULL;
170 /* Check if we already created a salut account */
171 if (!empathy_conf_get_bool (empathy_conf_get(),
172 EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
180 empathy_debug (DEBUG_DOMAIN, "Try to add a salut account...");
182 /* Check if the salut CM is installed */
183 profile = mc_profile_lookup ("salut");
184 protocol = mc_profile_get_protocol (profile);
186 empathy_debug (DEBUG_DOMAIN, "Salut not installed");
187 g_object_unref (profile);
190 g_object_unref (protocol);
192 /* Get self EContact from EDS */
193 if (!e_book_get_self (&contact, &book, NULL)) {
194 empathy_debug (DEBUG_DOMAIN, "Failed to get self econtact");
195 g_object_unref (profile);
199 empathy_conf_set_bool (empathy_conf_get (),
200 EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
203 /* Check if there is already a salut account */
204 accounts = mc_accounts_list_by_profile (profile);
206 empathy_debug (DEBUG_DOMAIN, "There is already a salut account");
207 mc_accounts_list_free (accounts);
208 g_object_unref (profile);
212 account = mc_account_create (profile);
213 mc_account_set_display_name (account, _("People nearby"));
215 nickname = e_contact_get (contact, E_CONTACT_NICKNAME);
216 published_name = e_contact_get (contact, E_CONTACT_FULL_NAME);
217 first_name = e_contact_get (contact, E_CONTACT_GIVEN_NAME);
218 last_name = e_contact_get (contact, E_CONTACT_FAMILY_NAME);
219 email = e_contact_get (contact, E_CONTACT_EMAIL_1);
220 jid = e_contact_get (contact, E_CONTACT_IM_JABBER_HOME_1);
222 if (G_STR_EMPTY (nickname) || !empathy_strdiff (nickname, "nickname")) {
224 nickname = g_strdup (g_get_user_name ());
226 if (G_STR_EMPTY (published_name)) {
227 g_free (published_name);
228 published_name = g_strdup (g_get_real_name ());
231 empathy_debug (DEBUG_DOMAIN, "Salut account created:\n"
233 " published-name=%s\n"
238 nickname, published_name, first_name, last_name, email, jid);
240 mc_account_set_param_string (account, "nickname", nickname ? nickname : "");
241 mc_account_set_param_string (account, "published-name", published_name ? published_name : "");
242 mc_account_set_param_string (account, "first-name", first_name ? first_name : "");
243 mc_account_set_param_string (account, "last-name", last_name ? last_name : "");
244 mc_account_set_param_string (account, "email", email ? email : "");
245 mc_account_set_param_string (account, "jid", jid ? jid : "");
248 g_free (published_name);
253 g_object_unref (account);
254 g_object_unref (profile);
255 g_object_unref (contact);
256 g_object_unref (book);
260 main (int argc, char *argv[])
262 EmpathyStatusIcon *icon;
265 McAccountMonitor *monitor;
267 EmpathyChandler *chandler;
268 GnomeProgram *program;
269 gboolean no_connect = FALSE;
270 GOptionContext *context;
271 GOptionEntry options[] = {
273 0, G_OPTION_ARG_NONE, &no_connect,
274 N_("Don't connect on startup"),
279 empathy_debug_set_log_file_from_env ();
281 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
282 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
283 textdomain (GETTEXT_PACKAGE);
285 context = g_option_context_new (_("- Empathy Instant Messenger"));
286 g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);
288 g_set_application_name (PACKAGE_NAME);
290 program = gnome_program_init ("empathy",
294 GNOME_PROGRAM_STANDARD_PROPERTIES,
295 "goption-context", context,
296 GNOME_PARAM_HUMAN_READABLE_NAME, PACKAGE_NAME,
299 gtk_window_set_default_icon_name ("empathy");
300 gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
301 DATADIR G_DIR_SEPARATOR_S "empathy");
304 monitor = mc_account_monitor_new ();
305 mc = empathy_mission_control_new ();
306 idle = empathy_idle_new ();
307 g_signal_connect (monitor, "account-enabled",
308 G_CALLBACK (account_enabled_cb),
310 g_signal_connect (mc, "ServiceEnded",
311 G_CALLBACK (service_ended_cb),
313 g_signal_connect (mc, "Error",
314 G_CALLBACK (operation_error_cb),
318 start_mission_control (idle);
321 create_salut_account ();
324 window = empathy_main_window_show ();
325 icon = empathy_status_icon_new (GTK_WINDOW (window));
327 /* Setting up channel handler */
328 chandler = empathy_chandler_new (BUS_NAME, OBJECT_PATH);
329 g_signal_connect (chandler, "new-channel",
330 G_CALLBACK (new_channel_cb),
335 empathy_idle_set_state (idle, MC_PRESENCE_OFFLINE);
337 g_object_unref (chandler);
338 g_object_unref (monitor);
340 g_object_unref (idle);
341 g_object_unref (icon);
342 g_object_unref (program);