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