]> git.0d.be Git - empathy.git/blob - src/empathy-accounts.c
Make the empathy-account code be slightly more streamlined
[empathy.git] / src / empathy-accounts.c
1 /*
2  * Copyright (C) 2005-2007 Imendio AB
3  * Copyright (C) 2007-2010 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., 51 Franklin St, Fifth Floor,
18  * Boston, MA  02110-1301  USA
19  *
20  * Authors: Martyn Russell <martyn@imendio.com>
21  *          Xavier Claessens <xclaesse@gmail.com>
22  *          Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
23  *          Jonathan Tellier <jonathan.tellier@gmail.com>
24  *          Travis Reitter <travis.reitter@collabora.co.uk>
25  */
26
27 #include <config.h>
28
29 #include <string.h>
30 #include <stdlib.h>
31
32 #include <gtk/gtk.h>
33 #include <glib/gi18n.h>
34 #include <unique/unique.h>
35
36 #include <telepathy-glib/account-manager.h>
37 #include <telepathy-glib/defs.h>
38 #include <telepathy-glib/util.h>
39
40 #include <libempathy/empathy-utils.h>
41 #include <libempathy/empathy-connection-managers.h>
42 #include <libempathy-gtk/empathy-ui-utils.h>
43
44 #include "empathy-accounts.h"
45 #include "empathy-accounts-common.h"
46 #include "empathy-accounts-dialog.h"
47 #include "empathy-account-assistant.h"
48 #include "empathy-import-mc4-accounts.h"
49 #include "empathy-auto-salut-account-helper.h"
50
51 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
52 #include <libempathy/empathy-debug.h>
53
54 #define EMPATHY_ACCOUNTS_DBUS_NAME "org.gnome.EmpathyAccounts"
55
56 static gboolean only_if_needed = FALSE;
57 static gboolean hidden = FALSE;
58 static gchar *selected_account_name = NULL;
59
60 static void
61 account_prepare_cb (GObject *source_object,
62     GAsyncResult *result,
63     gpointer user_data)
64 {
65   TpAccountManager *manager = TP_ACCOUNT_MANAGER (user_data);
66   TpAccount *account = TP_ACCOUNT (source_object);
67   GError *error = NULL;
68
69   if (!tp_account_prepare_finish (account, result, &error))
70     {
71       DEBUG ("Failed to prepare account: %s", error->message);
72       g_error_free (error);
73
74       account = NULL;
75     }
76
77   empathy_accounts_show_accounts_ui (manager, account,
78       G_CALLBACK (gtk_main_quit));
79 }
80
81 static void
82 maybe_show_accounts_ui (TpAccountManager *manager)
83 {
84   if (hidden ||
85       (only_if_needed && empathy_accounts_has_non_salut_accounts (manager)))
86     gtk_main_quit ();
87   else
88     empathy_accounts_show_accounts_ui (manager, NULL, gtk_main_quit);
89 }
90
91 static void
92 cm_manager_prepared_cb (GObject *source,
93     GAsyncResult *result,
94     gpointer user_data)
95 {
96   if (!empathy_connection_managers_prepare_finish (
97       EMPATHY_CONNECTION_MANAGERS (source), result, NULL))
98     {
99       g_warning ("Failed to prepare connection managers singleton");
100       gtk_main_quit ();
101       return;
102     }
103
104   empathy_accounts_import (TP_ACCOUNT_MANAGER (user_data),
105     EMPATHY_CONNECTION_MANAGERS (source));
106
107   maybe_show_accounts_ui (TP_ACCOUNT_MANAGER (user_data));
108 }
109
110 static void
111 account_manager_ready_for_accounts_cb (GObject *source_object,
112     GAsyncResult *result,
113     gpointer user_data)
114 {
115   TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
116   const gchar *account_id = (const gchar*) user_data;
117   GError *error = NULL;
118
119   if (!tp_account_manager_prepare_finish (manager, result, &error))
120     {
121       DEBUG ("Failed to prepare account manager: %s", error->message);
122       g_clear_error (&error);
123       return;
124     }
125
126   if (account_id != NULL)
127     {
128       gchar *account_path;
129       TpAccount *account = NULL;
130       TpDBusDaemon *bus;
131
132       /* create and prep the corresponding TpAccount so it's fully ready by the
133        * time we try to select it in the accounts dialog */
134       account_path = g_strdup_printf ("%s%s", TP_ACCOUNT_OBJECT_PATH_BASE,
135           account_id);
136       bus = tp_dbus_daemon_dup (NULL);
137       if ((account = tp_account_new (bus, account_path, &error)))
138         {
139           tp_account_prepare_async (account, NULL, account_prepare_cb, manager);
140           return;
141         }
142       else
143         {
144           DEBUG ("Failed to find account with path %s: %s", account_path,
145               error->message);
146           g_clear_error (&error);
147         }
148
149       g_object_unref (bus);
150       g_free (account_path);
151     }
152   else
153     {
154       if (empathy_import_mc4_has_imported ())
155         {
156           maybe_show_accounts_ui (manager);
157         }
158       else
159         {
160           EmpathyConnectionManagers *cm_mgr =
161             empathy_connection_managers_dup_singleton ();
162
163           empathy_connection_managers_prepare_async (
164             cm_mgr, cm_manager_prepared_cb, manager);
165         }
166     }
167 }
168
169 static UniqueResponse
170 unique_app_message_cb (UniqueApp *unique_app,
171     gint command,
172     UniqueMessageData *data,
173     guint timestamp,
174     gpointer user_data)
175 {
176   DEBUG ("Other instance launched, presenting the main window. "
177       "Command=%d, timestamp %u", command, timestamp);
178
179   if (command == UNIQUE_ACTIVATE)
180     {
181       TpAccountManager *account_manager;
182
183       account_manager = tp_account_manager_dup ();
184
185       empathy_accounts_show_accounts_ui (account_manager, NULL,
186           G_CALLBACK (gtk_main_quit));
187
188       g_object_unref (account_manager);
189     }
190   else
191     {
192       g_warning (G_STRLOC "unhandled unique app command %d", command);
193
194       return UNIQUE_RESPONSE_PASSTHROUGH;
195     }
196
197   return UNIQUE_RESPONSE_OK;
198 }
199
200 #define COMMAND_ACCOUNTS_DIALOG 1
201
202 int
203 main (int argc, char *argv[])
204 {
205   TpAccountManager *account_manager;
206   GError *error = NULL;
207   TpDBusDaemon *dbus_daemon;
208   UniqueApp *unique_app;
209
210   GOptionContext *optcontext;
211   GOptionEntry options[] = {
212       { "hidden", 'h',
213         0, G_OPTION_ARG_NONE, &hidden,
214         N_("Don't display any dialogs; do any work (eg, importing) and exit"),
215         NULL },
216       { "if-needed", 'n',
217         0, G_OPTION_ARG_NONE, &only_if_needed,
218         N_("Don't display any dialogs if there are any non-salut accounts"),
219         NULL },
220       { "select-account", 's',
221         G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING, &selected_account_name,
222         N_("Initially select given account (eg, "
223             "gabble/jabber/foo_40example_2eorg0)"),
224         N_("<account-id>") },
225
226       { NULL }
227   };
228
229   g_thread_init (NULL);
230   empathy_init ();
231
232   optcontext = g_option_context_new (N_("- Empathy Accounts"));
233   g_option_context_add_group (optcontext, gtk_get_option_group (TRUE));
234   g_option_context_add_main_entries (optcontext, options, GETTEXT_PACKAGE);
235
236   if (!g_option_context_parse (optcontext, &argc, &argv, &error))
237     {
238       g_print ("%s\nRun '%s --help' to see a full list of available command line options.\n",
239           error->message, argv[0]);
240       g_warning ("Error in empathy init: %s", error->message);
241       return EXIT_FAILURE;
242     }
243
244   g_option_context_free (optcontext);
245
246   empathy_gtk_init ();
247
248   g_set_application_name (_(PACKAGE_NAME " Accounts"));
249
250   gtk_window_set_default_icon_name ("empathy");
251
252   unique_app = unique_app_new (EMPATHY_ACCOUNTS_DBUS_NAME, NULL);
253
254   if (unique_app_is_running (unique_app))
255     {
256       unique_app_send_message (unique_app, UNIQUE_ACTIVATE, NULL);
257
258       g_object_unref (unique_app);
259       return EXIT_SUCCESS;
260     }
261
262   /* Take well-known name */
263   dbus_daemon = tp_dbus_daemon_dup (&error);
264   if (error == NULL)
265     {
266       if (!tp_dbus_daemon_request_name (dbus_daemon,
267           EMPATHY_ACCOUNTS_DBUS_NAME, TRUE, &error))
268         {
269           DEBUG ("Failed to request well-known name: %s",
270                  error ? error->message : "no message");
271           g_clear_error (&error);
272         }
273       g_object_unref (dbus_daemon);
274     }
275   else
276     {
277       DEBUG ("Failed to dup dbus daemon: %s",
278              error ? error->message : "no message");
279       g_clear_error (&error);
280     }
281
282   account_manager = tp_account_manager_dup ();
283
284   tp_account_manager_prepare_async (account_manager, NULL,
285     account_manager_ready_for_accounts_cb, selected_account_name);
286
287   g_signal_connect (unique_app, "message-received",
288       G_CALLBACK (unique_app_message_cb), NULL);
289
290   gtk_main ();
291
292   g_object_unref (account_manager);
293   g_object_unref (unique_app);
294
295   return EXIT_SUCCESS;
296 }