]> git.0d.be Git - empathy.git/blob - src/empathy.c
Add specialised UI for salut settings. Configure a Salut account first
[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 <libgnome/gnome-program.h>
32 #include <libgnomeui/gnome-ui-init.h>
33 #include <libebook/e-book.h>
34
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>
40
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>
55
56 #define DEBUG_DOMAIN "EmpathyMain"
57
58 #define BUS_NAME "org.gnome.Empathy.Chat"
59 #define OBJECT_PATH "/org/freedesktop/Telepathy/ChannelHandler"
60
61 static void
62 service_ended_cb (MissionControl *mc,
63                   gpointer        user_data)
64 {
65         empathy_debug (DEBUG_DOMAIN, "Mission Control stopped");
66 }
67
68 static void
69 operation_error_cb (MissionControl *mc,
70                     guint           operation_id,
71                     guint           error_code,
72                     gpointer        user_data)
73 {
74         empathy_debug (DEBUG_DOMAIN, "Error code %d during operation %d",
75                       error_code,
76                       operation_id);
77 }
78
79 static void
80 start_mission_control (EmpathyIdle *idle)
81 {
82         McPresence presence;
83
84         presence = empathy_idle_get_state (idle);
85
86         if (presence > MC_PRESENCE_OFFLINE) {
87                 /* MC is already running and online, nothing to do */
88                 return;
89         }
90
91         empathy_idle_set_state (idle, MC_PRESENCE_AVAILABLE);
92 }
93
94 static void
95 account_enabled_cb (McAccountMonitor *monitor,
96                     gchar            *unique_name,
97                     EmpathyIdle      *idle)
98 {
99         empathy_debug (DEBUG_DOMAIN, "Account enabled: %s", unique_name);
100         start_mission_control (idle);
101 }
102
103 static void
104 new_channel_cb (EmpathyChandler *chandler,
105                 TpConn          *tp_conn,
106                 TpChan          *tp_chan,
107                 MissionControl  *mc)
108 {
109         McAccount  *account;
110         EmpathyChat *chat;
111         gchar      *id;
112
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);
116         g_free (id);
117
118         if (chat) {
119                 /* The chat already exists */
120                 if (!empathy_chat_is_connected (chat)) {
121                         EmpathyTpChat *tp_chat;
122
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));
126                         } else {
127                                 tp_chat = empathy_tp_chat_new (account, tp_chan);
128                         }
129                         empathy_chat_set_tp_chat (chat, tp_chat);
130                         g_object_unref (tp_chat);
131                 }
132                 empathy_chat_present (chat);
133
134                 g_object_unref (account);
135                 return;
136         }
137
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));
141         }
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));
145         }
146
147         empathy_chat_present (EMPATHY_CHAT (chat));
148
149         g_object_unref (chat);
150         g_object_unref (account);
151 }
152
153 static void
154 create_salut_account (void)
155 {
156         McProfile  *profile;
157         McProtocol *protocol;
158         gboolean    salut_created;
159         McAccount  *account;
160         EBook      *book;
161         EContact   *contact;
162         gchar      *nickname = NULL;
163         gchar      *published_name = NULL;
164         gchar      *first_name = NULL;
165         gchar      *last_name = NULL;
166         gchar      *email = NULL;
167         gchar      *jid = NULL;
168
169         if (!empathy_conf_get_bool (empathy_conf_get(),
170                                     EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
171                                     &salut_created)) {
172                 return;
173         }
174         if (salut_created) {
175                 return;
176         }
177
178         profile = mc_profile_lookup ("salut");
179         protocol = mc_profile_get_protocol (profile);
180         if (!protocol) {
181                 g_object_unref (profile);
182                 return;
183         }
184         g_object_unref (protocol);
185
186         if (!e_book_get_self (&contact, &book, NULL)) {
187                 return;
188         }
189
190         empathy_conf_set_bool (empathy_conf_get (),
191                                EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
192                                TRUE);
193
194         account = mc_account_create (profile);
195         mc_account_set_display_name (account, _("People nearby"));
196         
197         nickname = e_contact_get (contact, E_CONTACT_NICKNAME);
198         published_name = e_contact_get (contact, E_CONTACT_FULL_NAME);
199         first_name = e_contact_get (contact, E_CONTACT_GIVEN_NAME);
200         last_name = e_contact_get (contact, E_CONTACT_FAMILY_NAME);
201         email = e_contact_get (contact, E_CONTACT_EMAIL_1);
202         jid = e_contact_get (contact, E_CONTACT_IM_JABBER_HOME_1);
203         
204         if (G_STR_EMPTY (nickname) || !empathy_strdiff (nickname, "nickname")) {
205                 g_free (nickname);
206                 nickname = g_strdup (g_get_user_name ());
207         }
208         if (G_STR_EMPTY (published_name)) {
209                 g_free (published_name);
210                 published_name = g_strdup (g_get_real_name ());
211         }
212
213         mc_account_set_param_string (account, "nickname", nickname ? nickname : "");
214         mc_account_set_param_string (account, "published-name", published_name ? published_name : "");
215         mc_account_set_param_string (account, "first-name", first_name ? first_name : "");
216         mc_account_set_param_string (account, "last-name", last_name ? last_name : "");
217         mc_account_set_param_string (account, "email", email ? email : "");
218         mc_account_set_param_string (account, "jid", jid ? jid : "");
219
220         g_free (nickname);
221         g_free (published_name);
222         g_free (first_name);
223         g_free (last_name);
224         g_free (email);
225         g_free (jid);
226         g_object_unref (account);
227         g_object_unref (profile);
228         g_object_unref (contact);
229         g_object_unref (book);
230 }
231
232 int
233 main (int argc, char *argv[])
234 {
235         EmpathyStatusIcon *icon;
236         GtkWidget         *window;
237         MissionControl    *mc;
238         McAccountMonitor  *monitor;
239         EmpathyIdle       *idle;
240         EmpathyChandler   *chandler;
241         GnomeProgram      *program;
242         gboolean           no_connect = FALSE;
243         GOptionContext    *context;
244         GOptionEntry       options[] = {
245                 { "no-connect", 'n',
246                   0, G_OPTION_ARG_NONE, &no_connect,
247                   N_("Don't connect on startup"),
248                   NULL },
249                 { NULL }
250         };
251
252         empathy_debug_set_log_file_from_env ();
253
254         bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
255         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
256         textdomain (GETTEXT_PACKAGE);
257
258         context = g_option_context_new (_("- Empathy Instant Messenger"));
259         g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);
260
261         g_set_application_name (PACKAGE_NAME);
262
263         program = gnome_program_init ("empathy",
264                                       PACKAGE_VERSION,
265                                       LIBGNOMEUI_MODULE,
266                                       argc, argv,
267                                       GNOME_PROGRAM_STANDARD_PROPERTIES,
268                                       "goption-context", context,
269                                       GNOME_PARAM_HUMAN_READABLE_NAME, PACKAGE_NAME,
270                                       NULL);
271
272         gtk_window_set_default_icon_name ("empathy");
273         gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
274                                            DATADIR G_DIR_SEPARATOR_S "empathy");
275
276         /* Setting up MC */
277         monitor = mc_account_monitor_new ();
278         mc = empathy_mission_control_new ();
279         idle = empathy_idle_new ();
280         g_signal_connect (monitor, "account-enabled",
281                           G_CALLBACK (account_enabled_cb),
282                           idle);
283         g_signal_connect (mc, "ServiceEnded",
284                           G_CALLBACK (service_ended_cb),
285                           NULL);
286         g_signal_connect (mc, "Error",
287                           G_CALLBACK (operation_error_cb),
288                           NULL);
289
290         if (!no_connect) {
291                 start_mission_control (idle);
292         }
293         
294         create_salut_account ();
295
296         /* Setting up UI */
297         window = empathy_main_window_show ();
298         icon = empathy_status_icon_new (GTK_WINDOW (window));
299
300         /* Setting up channel handler  */
301         chandler = empathy_chandler_new (BUS_NAME, OBJECT_PATH);
302         g_signal_connect (chandler, "new-channel",
303                           G_CALLBACK (new_channel_cb),
304                           mc);
305
306         gtk_main ();
307
308         empathy_idle_set_state (idle, MC_PRESENCE_OFFLINE);
309
310         g_object_unref (chandler);
311         g_object_unref (monitor);
312         g_object_unref (mc);
313         g_object_unref (idle);
314         g_object_unref (icon);
315         g_object_unref (program);
316
317         return EXIT_SUCCESS;
318 }
319