]> git.0d.be Git - empathy.git/blob - src/empathy-accounts.c
always set account_manager_prepared
[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
59 static void
60 account_prepare_cb (GObject *source_object,
61     GAsyncResult *result,
62     gpointer user_data)
63 {
64   TpAccountManager *manager = TP_ACCOUNT_MANAGER (user_data);
65   TpAccount *account = TP_ACCOUNT (source_object);
66   GError *error = NULL;
67
68   if (!tp_account_prepare_finish (account, result, &error))
69     {
70       DEBUG ("Failed to prepare account: %s", error->message);
71       g_error_free (error);
72
73       account = NULL;
74     }
75
76   empathy_accounts_show_accounts_ui (manager, account,
77       G_CALLBACK (gtk_main_quit));
78 }
79
80 static void
81 maybe_show_accounts_ui (TpAccountManager *manager)
82 {
83   if (hidden ||
84       (only_if_needed && empathy_accounts_has_non_salut_accounts (manager)))
85     gtk_main_quit ();
86   else
87     empathy_accounts_show_accounts_ui (manager, NULL, gtk_main_quit);
88 }
89
90 static void
91 account_manager_ready_for_accounts_cb (GObject *source_object,
92     GAsyncResult *result,
93     gpointer user_data)
94 {
95   TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
96   GError *error = NULL;
97
98   if (!tp_account_manager_prepare_finish (manager, result, &error))
99     {
100       DEBUG ("Failed to prepare account manager: %s", error->message);
101       g_clear_error (&error);
102       return;
103     }
104
105   account_manager_prepared = TRUE;
106
107   if (selected_account_name != NULL)
108     {
109       gchar *account_path;
110       TpAccount *account = NULL;
111       TpDBusDaemon *bus;
112
113       /* create and prep the corresponding TpAccount so it's fully ready by the
114        * time we try to select it in the accounts dialog */
115       account_path = g_strdup_printf ("%s%s", TP_ACCOUNT_OBJECT_PATH_BASE,
116           selected_account_name);
117       bus = tp_dbus_daemon_dup (NULL);
118       if ((account = tp_account_new (bus, account_path, &error)))
119         {
120           tp_account_prepare_async (account, NULL, account_prepare_cb, manager);
121           return;
122         }
123       else
124         {
125           DEBUG ("Failed to find account with path %s: %s", account_path,
126               error->message);
127           g_clear_error (&error);
128         }
129
130       g_object_unref (bus);
131       g_free (account_path);
132     }
133   else
134     {
135       maybe_show_accounts_ui (manager);
136     }
137 }
138
139 static int
140 app_command_line_cb (GApplication *app,
141     GApplicationCommandLine *cmdline)
142 {
143   g_application_hold (app);
144
145   /* if the window is ready, present it; otherwise, it will be presented when
146    * the accounts manager is prepared */
147   if (account_manager_prepared)
148     {
149       TpAccountManager *account_manager;
150
151       account_manager = tp_account_manager_dup ();
152       empathy_accounts_show_accounts_ui (account_manager, NULL,
153               G_CALLBACK (gtk_main_quit));
154
155       g_object_unref (account_manager);
156     }
157
158   return 0;
159 }
160
161 static gboolean
162 local_cmdline (GApplication *app,
163     gchar ***arguments,
164     gint *exit_status)
165 {
166   gint i;
167   gchar **argv;
168   gint argc = 0;
169   gboolean retval = FALSE;
170   GError *error = NULL;
171
172   GOptionContext *optcontext;
173   GOptionEntry options[] = {
174       { "hidden", 'h',
175         0, G_OPTION_ARG_NONE, &hidden,
176         N_("Don't display any dialogs; do any work (eg, importing) and exit"),
177         NULL },
178       { "if-needed", 'n',
179         0, G_OPTION_ARG_NONE, &only_if_needed,
180         N_("Don't display any dialogs unless there are only \"People Nearby\" accounts"),
181         NULL },
182       { "select-account", 's',
183         G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING, &selected_account_name,
184         N_("Initially select given account (eg, "
185             "gabble/jabber/foo_40example_2eorg0)"),
186         N_("<account-id>") },
187
188       { NULL }
189   };
190
191   optcontext = g_option_context_new (N_("- Empathy Accounts"));
192   g_option_context_add_group (optcontext, gtk_get_option_group (TRUE));
193   g_option_context_add_main_entries (optcontext, options, GETTEXT_PACKAGE);
194
195   argv = *arguments;
196   for (i = 0; argv[i] != NULL; i++)
197     argc++;
198
199   if (!g_option_context_parse (optcontext, &argc, &argv, &error))
200     {
201       g_print ("%s\nRun '%s --help' to see a full list of available command line options.\n",
202           error->message, argv[0]);
203       g_warning ("Error in empathy init: %s", error->message);
204
205       *exit_status = EXIT_FAILURE;
206       retval = TRUE;
207     }
208
209   g_option_context_free (optcontext);
210
211   return retval;
212 }
213
214 #define COMMAND_ACCOUNTS_DIALOG 1
215
216 int
217 main (int argc, char *argv[])
218 {
219   TpAccountManager *account_manager;
220   GtkApplication *app;
221   GObjectClass *app_class;
222   gint retval;
223
224   g_thread_init (NULL);
225   empathy_init ();
226
227   gtk_init (&argc, &argv);
228   empathy_gtk_init ();
229
230   g_set_application_name (_("Empathy Accounts"));
231
232   gtk_window_set_default_icon_name ("empathy");
233   textdomain (GETTEXT_PACKAGE);
234
235   app = gtk_application_new (EMPATHY_ACCOUNTS_DBUS_NAME,
236       G_APPLICATION_HANDLES_COMMAND_LINE);
237   app_class = G_OBJECT_GET_CLASS (app);
238   G_APPLICATION_CLASS (app_class)->local_command_line = local_cmdline;
239
240   account_manager = tp_account_manager_dup ();
241
242   tp_account_manager_prepare_async (account_manager, NULL,
243     account_manager_ready_for_accounts_cb, NULL);
244
245   g_signal_connect (app, "command-line", G_CALLBACK (app_command_line_cb),
246       NULL);
247
248   retval = g_application_run (G_APPLICATION (app), argc, argv);
249
250   g_object_unref (account_manager);
251   g_object_unref (app);
252
253   return retval;
254 }