]> git.0d.be Git - empathy.git/blob - src/empathy-accounts.c
empathy-account: add a --assistant option to force having assistant
[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
35 #include <telepathy-glib/account-manager.h>
36 #include <telepathy-glib/defs.h>
37 #include <telepathy-glib/util.h>
38
39 #include <libempathy/empathy-utils.h>
40 #include <libempathy/empathy-connection-managers.h>
41 #include <libempathy-gtk/empathy-ui-utils.h>
42
43 #include "empathy-accounts.h"
44 #include "empathy-accounts-common.h"
45 #include "empathy-accounts-dialog.h"
46 #include "empathy-account-assistant.h"
47 #include "empathy-auto-salut-account-helper.h"
48
49 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
50 #include <libempathy/empathy-debug.h>
51
52 #define EMPATHY_ACCOUNTS_DBUS_NAME "org.gnome.EmpathyAccounts"
53
54 static gboolean only_if_needed = FALSE;
55 static gboolean hidden = FALSE;
56 static gchar *selected_account_name = NULL;
57 static gboolean account_manager_prepared = FALSE;
58 static gboolean assistant = FALSE;
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_proxy_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, assistant,
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, assistant, gtk_main_quit);
89 }
90
91 static void
92 account_manager_ready_for_accounts_cb (GObject *source_object,
93     GAsyncResult *result,
94     gpointer user_data)
95 {
96   TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
97   GError *error = NULL;
98
99   if (!tp_proxy_prepare_finish (manager, result, &error))
100     {
101       DEBUG ("Failed to prepare account manager: %s", error->message);
102       g_clear_error (&error);
103       return;
104     }
105
106   account_manager_prepared = TRUE;
107
108   if (selected_account_name != NULL)
109     {
110       gchar *account_path;
111       TpAccount *account = NULL;
112       TpDBusDaemon *bus;
113
114       /* create and prep the corresponding TpAccount so it's fully ready by the
115        * time we try to select it in the accounts dialog */
116       if (g_str_has_prefix (selected_account_name, TP_ACCOUNT_OBJECT_PATH_BASE))
117         account_path = g_strdup (selected_account_name);
118       else
119         account_path = g_strdup_printf ("%s%s", TP_ACCOUNT_OBJECT_PATH_BASE,
120             selected_account_name);
121
122       bus = tp_dbus_daemon_dup (NULL);
123       if ((account = tp_account_new (bus, account_path, &error)))
124         {
125           tp_proxy_prepare_async (account, NULL, account_prepare_cb, manager);
126           g_object_unref (bus);
127           return;
128         }
129       else
130         {
131           DEBUG ("Failed to find account with path %s: %s", account_path,
132               error->message);
133           g_clear_error (&error);
134
135           maybe_show_accounts_ui (manager);
136         }
137
138       g_object_unref (bus);
139       g_free (account_path);
140     }
141   else
142     {
143       maybe_show_accounts_ui (manager);
144     }
145 }
146
147 static int
148 app_command_line_cb (GApplication *app,
149     GApplicationCommandLine *cmdline)
150 {
151   g_application_hold (app);
152
153   /* if the window is ready, present it; otherwise, it will be presented when
154    * the accounts manager is prepared */
155   if (account_manager_prepared)
156     {
157       TpAccountManager *account_manager;
158
159       account_manager = tp_account_manager_dup ();
160       empathy_accounts_show_accounts_ui (account_manager, NULL, assistant,
161               G_CALLBACK (gtk_main_quit));
162
163       g_object_unref (account_manager);
164     }
165
166   return 0;
167 }
168
169 static gboolean
170 local_cmdline (GApplication *app,
171     gchar ***arguments,
172     gint *exit_status)
173 {
174   gint i;
175   gchar **argv;
176   gint argc = 0;
177   gboolean retval = FALSE;
178   GError *error = NULL;
179
180   GOptionContext *optcontext;
181   GOptionEntry options[] = {
182       { "hidden", 'h',
183         0, G_OPTION_ARG_NONE, &hidden,
184         N_("Don't display any dialogs; do any work (eg, importing) and exit"),
185         NULL },
186       { "if-needed", 'n',
187         0, G_OPTION_ARG_NONE, &only_if_needed,
188         N_("Don't display any dialogs unless there are only \"People Nearby\" accounts"),
189         NULL },
190       { "select-account", 's',
191         G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING, &selected_account_name,
192         N_("Initially select given account (eg, "
193             "gabble/jabber/foo_40example_2eorg0)"),
194         N_("<account-id>") },
195       { "assistant", 'a',
196         0, G_OPTION_ARG_NONE, &assistant,
197         N_("Show account assistant"),
198         NULL },
199
200       { NULL }
201   };
202
203   optcontext = g_option_context_new (N_("- Empathy Accounts"));
204   g_option_context_add_group (optcontext, gtk_get_option_group (TRUE));
205   g_option_context_add_main_entries (optcontext, options, GETTEXT_PACKAGE);
206
207   argv = *arguments;
208   for (i = 0; argv[i] != NULL; i++)
209     argc++;
210
211   if (!g_option_context_parse (optcontext, &argc, &argv, &error))
212     {
213       g_print ("%s\nRun '%s --help' to see a full list of available command line options.\n",
214           error->message, argv[0]);
215       g_warning ("Error in empathy init: %s", error->message);
216
217       *exit_status = EXIT_FAILURE;
218       retval = TRUE;
219     }
220
221   g_option_context_free (optcontext);
222
223   return retval;
224 }
225
226 #define COMMAND_ACCOUNTS_DIALOG 1
227
228 int
229 main (int argc, char *argv[])
230 {
231   TpAccountManager *account_manager;
232   GtkApplication *app;
233   GObjectClass *app_class;
234   gint retval;
235
236   g_thread_init (NULL);
237   empathy_init ();
238
239   gtk_init (&argc, &argv);
240   empathy_gtk_init ();
241
242   g_set_application_name (_("Empathy Accounts"));
243
244   /* Make empathy and empathy-accounts appear as the same app in gnome-shell */
245   gdk_set_program_class ("Empathy");
246   gtk_window_set_default_icon_name ("empathy");
247   textdomain (GETTEXT_PACKAGE);
248
249   app = gtk_application_new (EMPATHY_ACCOUNTS_DBUS_NAME,
250       G_APPLICATION_HANDLES_COMMAND_LINE);
251   app_class = G_OBJECT_GET_CLASS (app);
252   G_APPLICATION_CLASS (app_class)->local_command_line = local_cmdline;
253
254   account_manager = tp_account_manager_dup ();
255
256   tp_proxy_prepare_async (account_manager, NULL,
257     account_manager_ready_for_accounts_cb, NULL);
258
259   g_signal_connect (app, "command-line", G_CALLBACK (app_command_line_cb),
260       NULL);
261
262   retval = g_application_run (G_APPLICATION (app), argc, argv);
263
264   g_object_unref (account_manager);
265   g_object_unref (app);
266
267   return retval;
268 }