]> git.0d.be Git - empathy.git/blob - src/empathy.c
fef01b808ed4512512b22ef1b2d6a9269da8c9fd
[empathy.git] / src / empathy.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007 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
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <gtk/gtk.h>
30
31 #include <libebook/e-book.h>
32 #include <libgnomevfs/gnome-vfs.h>
33
34 #include <libmissioncontrol/mc-account.h>
35 #include <libmissioncontrol/mc-account-monitor.h>
36 #include <libmissioncontrol/mission-control.h>
37
38 #include <libempathy/empathy-idle.h>
39 #include <libempathy/empathy-conf.h>
40 #include <libempathy/empathy-utils.h>
41 #include <libempathy/empathy-debug.h>
42
43 #include <libempathy-gtk/empathy-preferences.h>
44 #include <libempathy-gtk/empathy-main-window.h>
45 #include <libempathy-gtk/empathy-status-icon.h>
46
47 #define DEBUG_DOMAIN "EmpathyMain"
48
49 static void
50 service_ended_cb (MissionControl *mc,
51                   gpointer        user_data)
52 {
53         empathy_debug (DEBUG_DOMAIN, "Mission Control stopped");
54 }
55
56 static void
57 operation_error_cb (MissionControl *mc,
58                     guint           operation_id,
59                     guint           error_code,
60                     gpointer        user_data)
61 {
62         empathy_debug (DEBUG_DOMAIN, "Error code %d during operation %d",
63                       error_code,
64                       operation_id);
65 }
66
67 static void
68 start_mission_control (EmpathyIdle *idle)
69 {
70         McPresence presence;
71
72         presence = empathy_idle_get_state (idle);
73
74         if (presence > MC_PRESENCE_OFFLINE) {
75                 /* MC is already running and online, nothing to do */
76                 return;
77         }
78
79         empathy_idle_set_state (idle, MC_PRESENCE_AVAILABLE);
80 }
81
82 static void
83 account_enabled_cb (McAccountMonitor *monitor,
84                     gchar            *unique_name,
85                     EmpathyIdle      *idle)
86 {
87         empathy_debug (DEBUG_DOMAIN, "Account enabled: %s", unique_name);
88         start_mission_control (idle);
89 }
90
91 static void
92 create_salut_account (void)
93 {
94         McProfile  *profile;
95         McProtocol *protocol;
96         gboolean    salut_created;
97         McAccount  *account;
98         GList      *accounts;
99         EBook      *book;
100         EContact   *contact;
101         gchar      *nickname = NULL;
102         gchar      *published_name = NULL;
103         gchar      *first_name = NULL;
104         gchar      *last_name = NULL;
105         gchar      *email = NULL;
106         gchar      *jid = NULL;
107
108         /* Check if we already created a salut account */
109         if (!empathy_conf_get_bool (empathy_conf_get(),
110                                     EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
111                                     &salut_created)) {
112                 return;
113         }
114         if (salut_created) {
115                 return;
116         }
117
118         empathy_debug (DEBUG_DOMAIN, "Try to add a salut account...");
119
120         /* Check if the salut CM is installed */
121         profile = mc_profile_lookup ("salut");
122         protocol = mc_profile_get_protocol (profile);
123         if (!protocol) {
124                 empathy_debug (DEBUG_DOMAIN, "Salut not installed");
125                 g_object_unref (profile);
126                 return;
127         }
128         g_object_unref (protocol);
129
130         /* Get self EContact from EDS */
131         if (!e_book_get_self (&contact, &book, NULL)) {
132                 empathy_debug (DEBUG_DOMAIN, "Failed to get self econtact");
133                 g_object_unref (profile);
134                 return;
135         }
136
137         empathy_conf_set_bool (empathy_conf_get (),
138                                EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
139                                TRUE);
140
141         /* Check if there is already a salut account */
142         accounts = mc_accounts_list_by_profile (profile);
143         if (accounts) {
144                 empathy_debug (DEBUG_DOMAIN, "There is already a salut account");
145                 mc_accounts_list_free (accounts);
146                 g_object_unref (profile);
147                 return;
148         }
149
150         account = mc_account_create (profile);
151         mc_account_set_display_name (account, _("People nearby"));
152         
153         nickname = e_contact_get (contact, E_CONTACT_NICKNAME);
154         published_name = e_contact_get (contact, E_CONTACT_FULL_NAME);
155         first_name = e_contact_get (contact, E_CONTACT_GIVEN_NAME);
156         last_name = e_contact_get (contact, E_CONTACT_FAMILY_NAME);
157         email = e_contact_get (contact, E_CONTACT_EMAIL_1);
158         jid = e_contact_get (contact, E_CONTACT_IM_JABBER_HOME_1);
159         
160         if (G_STR_EMPTY (nickname) || !empathy_strdiff (nickname, "nickname")) {
161                 g_free (nickname);
162                 nickname = g_strdup (g_get_user_name ());
163         }
164         if (G_STR_EMPTY (published_name)) {
165                 g_free (published_name);
166                 published_name = g_strdup (g_get_real_name ());
167         }
168
169         empathy_debug (DEBUG_DOMAIN, "Salut account created:\n"
170                                      "  nickname=%s\n"
171                                      "  published-name=%s\n"
172                                      "  first-name=%s\n"
173                                      "  last-name=%s\n"
174                                      "  email=%s\n"
175                                      "  jid=%s\n",
176                        nickname, published_name, first_name, last_name, email, jid);
177
178         mc_account_set_param_string (account, "nickname", nickname ? nickname : "");
179         mc_account_set_param_string (account, "published-name", published_name ? published_name : "");
180         mc_account_set_param_string (account, "first-name", first_name ? first_name : "");
181         mc_account_set_param_string (account, "last-name", last_name ? last_name : "");
182         mc_account_set_param_string (account, "email", email ? email : "");
183         mc_account_set_param_string (account, "jid", jid ? jid : "");
184
185         g_free (nickname);
186         g_free (published_name);
187         g_free (first_name);
188         g_free (last_name);
189         g_free (email);
190         g_free (jid);
191         g_object_unref (account);
192         g_object_unref (profile);
193         g_object_unref (contact);
194         g_object_unref (book);
195 }
196
197 int
198 main (int argc, char *argv[])
199 {
200         EmpathyStatusIcon *icon;
201         GtkWidget         *window;
202         MissionControl    *mc;
203         McAccountMonitor  *monitor;
204         EmpathyIdle       *idle;
205         gboolean           no_connect = FALSE;
206         GError            *error = NULL;
207         GOptionEntry       options[] = {
208                 { "no-connect", 'n',
209                   0, G_OPTION_ARG_NONE, &no_connect,
210                   N_("Don't connect on startup"),
211                   NULL },
212                 { NULL }
213         };
214
215         empathy_debug_set_log_file_from_env ();
216
217         bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
218         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
219         textdomain (GETTEXT_PACKAGE);
220
221         if (!gtk_init_with_args (&argc, &argv,
222                                  _("- Empathy Instant Messenger"),
223                                  options, GETTEXT_PACKAGE, &error)) {
224                 empathy_debug (DEBUG_DOMAIN, error->message);
225                 return EXIT_FAILURE;
226         }
227
228         g_set_application_name (PACKAGE_NAME);
229
230         gtk_window_set_default_icon_name ("empathy");
231         gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
232                                            PKGDATADIR G_DIR_SEPARATOR_S "icons");
233         gnome_vfs_init ();
234
235         /* Setting up MC */
236         monitor = mc_account_monitor_new ();
237         mc = empathy_mission_control_new ();
238         idle = empathy_idle_new ();
239         g_signal_connect (monitor, "account-enabled",
240                           G_CALLBACK (account_enabled_cb),
241                           idle);
242         g_signal_connect (mc, "ServiceEnded",
243                           G_CALLBACK (service_ended_cb),
244                           NULL);
245         g_signal_connect (mc, "Error",
246                           G_CALLBACK (operation_error_cb),
247                           NULL);
248
249         if (!no_connect) {
250                 start_mission_control (idle);
251         }
252         
253         create_salut_account ();
254
255         /* Setting up UI */
256         window = empathy_main_window_show ();
257         icon = empathy_status_icon_new (GTK_WINDOW (window));
258
259         gtk_main ();
260
261         empathy_idle_set_state (idle, MC_PRESENCE_OFFLINE);
262
263         g_object_unref (monitor);
264         g_object_unref (mc);
265         g_object_unref (idle);
266         g_object_unref (icon);
267
268         return EXIT_SUCCESS;
269 }
270