]> git.0d.be Git - empathy.git/blob - src/empathy.c
Rename icons to follow fd.o icon naming spec. Move icons to the right
[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 <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>
39
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>
54
55 #define DEBUG_DOMAIN "EmpathyMain"
56
57 #define BUS_NAME "org.gnome.Empathy.Chat"
58 #define OBJECT_PATH "/org/freedesktop/Telepathy/ChannelHandler"
59
60 static void
61 service_ended_cb (MissionControl *mc,
62                   gpointer        user_data)
63 {
64         empathy_debug (DEBUG_DOMAIN, "Mission Control stopped");
65 }
66
67 static void
68 operation_error_cb (MissionControl *mc,
69                     guint           operation_id,
70                     guint           error_code,
71                     gpointer        user_data)
72 {
73         empathy_debug (DEBUG_DOMAIN, "Error code %d during operation %d",
74                       error_code,
75                       operation_id);
76 }
77
78 static void
79 start_mission_control (EmpathyIdle *idle)
80 {
81         McPresence presence;
82
83         presence = empathy_idle_get_state (idle);
84
85         if (presence > MC_PRESENCE_OFFLINE) {
86                 /* MC is already running and online, nothing to do */
87                 return;
88         }
89
90         empathy_idle_set_state (idle, MC_PRESENCE_AVAILABLE);
91 }
92
93 static void
94 account_enabled_cb (McAccountMonitor *monitor,
95                     gchar            *unique_name,
96                     EmpathyIdle      *idle)
97 {
98         empathy_debug (DEBUG_DOMAIN, "Account enabled: %s", unique_name);
99         start_mission_control (idle);
100 }
101
102 static void
103 new_channel_cb (EmpathyChandler *chandler,
104                 TpConn          *tp_conn,
105                 TpChan          *tp_chan,
106                 MissionControl  *mc)
107 {
108         McAccount  *account;
109         EmpathyChat *chat;
110         gchar      *id;
111
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);
115         g_free (id);
116
117         if (chat) {
118                 /* The chat already exists */
119                 if (!empathy_chat_is_connected (chat)) {
120                         EmpathyTpChat *tp_chat;
121
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));
125                         } else {
126                                 tp_chat = empathy_tp_chat_new (account, tp_chan);
127                         }
128                         empathy_chat_set_tp_chat (chat, tp_chat);
129                         g_object_unref (tp_chat);
130                 }
131                 empathy_chat_present (chat);
132
133                 g_object_unref (account);
134                 return;
135         }
136
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));
140         }
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));
144         }
145
146         empathy_chat_present (EMPATHY_CHAT (chat));
147
148         g_object_unref (chat);
149         g_object_unref (account);
150 }
151
152 static void
153 create_salut_account (void)
154 {
155         McProfile  *profile;
156         McProtocol *protocol;
157         gboolean    salut_created;
158         McAccount  *account;
159         GList      *accounts;
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         /* Check if we already created a salut account */
170         if (!empathy_conf_get_bool (empathy_conf_get(),
171                                     EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
172                                     &salut_created)) {
173                 return;
174         }
175         if (salut_created) {
176                 return;
177         }
178
179         empathy_debug (DEBUG_DOMAIN, "Try to add a salut account...");
180
181         /* Check if the salut CM is installed */
182         profile = mc_profile_lookup ("salut");
183         protocol = mc_profile_get_protocol (profile);
184         if (!protocol) {
185                 empathy_debug (DEBUG_DOMAIN, "Salut not installed");
186                 g_object_unref (profile);
187                 return;
188         }
189         g_object_unref (protocol);
190
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);
195                 return;
196         }
197
198         empathy_conf_set_bool (empathy_conf_get (),
199                                EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
200                                TRUE);
201
202         /* Check if there is already a salut account */
203         accounts = mc_accounts_list_by_profile (profile);
204         if (accounts) {
205                 empathy_debug (DEBUG_DOMAIN, "There is already a salut account");
206                 mc_accounts_list_free (accounts);
207                 g_object_unref (profile);
208                 return;
209         }
210
211         account = mc_account_create (profile);
212         mc_account_set_display_name (account, _("People nearby"));
213         
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);
220         
221         if (G_STR_EMPTY (nickname) || !empathy_strdiff (nickname, "nickname")) {
222                 g_free (nickname);
223                 nickname = g_strdup (g_get_user_name ());
224         }
225         if (G_STR_EMPTY (published_name)) {
226                 g_free (published_name);
227                 published_name = g_strdup (g_get_real_name ());
228         }
229
230         empathy_debug (DEBUG_DOMAIN, "Salut account created:\n"
231                                      "  nickname=%s\n"
232                                      "  published-name=%s\n"
233                                      "  first-name=%s\n"
234                                      "  last-name=%s\n"
235                                      "  email=%s\n"
236                                      "  jid=%s\n",
237                        nickname, published_name, first_name, last_name, email, jid);
238
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 : "");
245
246         g_free (nickname);
247         g_free (published_name);
248         g_free (first_name);
249         g_free (last_name);
250         g_free (email);
251         g_free (jid);
252         g_object_unref (account);
253         g_object_unref (profile);
254         g_object_unref (contact);
255         g_object_unref (book);
256 }
257
258 int
259 main (int argc, char *argv[])
260 {
261         EmpathyStatusIcon *icon;
262         GtkWidget         *window;
263         MissionControl    *mc;
264         McAccountMonitor  *monitor;
265         EmpathyIdle       *idle;
266         EmpathyChandler   *chandler;
267         gboolean           no_connect = FALSE;
268         GOptionContext    *context;
269         GOptionEntry       options[] = {
270                 { "no-connect", 'n',
271                   0, G_OPTION_ARG_NONE, &no_connect,
272                   N_("Don't connect on startup"),
273                   NULL },
274                 { NULL }
275         };
276
277         empathy_debug_set_log_file_from_env ();
278
279         bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
280         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
281         textdomain (GETTEXT_PACKAGE);
282
283         context = g_option_context_new (_("- Empathy Instant Messenger"));
284         g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);
285
286         g_set_application_name (PACKAGE_NAME);
287
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");
292         gnome_vfs_init ();
293
294         /* Setting up MC */
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),
300                           idle);
301         g_signal_connect (mc, "ServiceEnded",
302                           G_CALLBACK (service_ended_cb),
303                           NULL);
304         g_signal_connect (mc, "Error",
305                           G_CALLBACK (operation_error_cb),
306                           NULL);
307
308         if (!no_connect) {
309                 start_mission_control (idle);
310         }
311         
312         create_salut_account ();
313
314         /* Setting up UI */
315         window = empathy_main_window_show ();
316         icon = empathy_status_icon_new (GTK_WINDOW (window));
317
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),
322                           mc);
323
324         gtk_main ();
325
326         empathy_idle_set_state (idle, MC_PRESENCE_OFFLINE);
327
328         g_object_unref (chandler);
329         g_object_unref (monitor);
330         g_object_unref (mc);
331         g_object_unref (idle);
332         g_object_unref (icon);
333
334         return EXIT_SUCCESS;
335 }
336