]> git.0d.be Git - empathy.git/blob - src/empathy-accounts.c
Merge branch 'crash-659118'
[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_proxy_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_proxy_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       if (g_str_has_prefix (selected_account_name, TP_ACCOUNT_OBJECT_PATH_BASE))
116         account_path = g_strdup (selected_account_name);
117       else
118         account_path = g_strdup_printf ("%s%s", TP_ACCOUNT_OBJECT_PATH_BASE,
119             selected_account_name);
120
121       bus = tp_dbus_daemon_dup (NULL);
122       if ((account = tp_account_new (bus, account_path, &error)))
123         {
124           tp_proxy_prepare_async (account, NULL, account_prepare_cb, manager);
125           g_object_unref (bus);
126           return;
127         }
128       else
129         {
130           DEBUG ("Failed to find account with path %s: %s", account_path,
131               error->message);
132           g_clear_error (&error);
133
134           maybe_show_accounts_ui (manager);
135         }
136
137       g_object_unref (bus);
138       g_free (account_path);
139     }
140   else
141     {
142       maybe_show_accounts_ui (manager);
143     }
144 }
145
146 static int
147 app_command_line_cb (GApplication *app,
148     GApplicationCommandLine *cmdline)
149 {
150   g_application_hold (app);
151
152   /* if the window is ready, present it; otherwise, it will be presented when
153    * the accounts manager is prepared */
154   if (account_manager_prepared)
155     {
156       TpAccountManager *account_manager;
157
158       account_manager = tp_account_manager_dup ();
159       empathy_accounts_show_accounts_ui (account_manager, NULL,
160               G_CALLBACK (gtk_main_quit));
161
162       g_object_unref (account_manager);
163     }
164
165   return 0;
166 }
167
168 static gboolean
169 local_cmdline (GApplication *app,
170     gchar ***arguments,
171     gint *exit_status)
172 {
173   gint i;
174   gchar **argv;
175   gint argc = 0;
176   gboolean retval = FALSE;
177   GError *error = NULL;
178
179   GOptionContext *optcontext;
180   GOptionEntry options[] = {
181       { "hidden", 'h',
182         0, G_OPTION_ARG_NONE, &hidden,
183         N_("Don't display any dialogs; do any work (eg, importing) and exit"),
184         NULL },
185       { "if-needed", 'n',
186         0, G_OPTION_ARG_NONE, &only_if_needed,
187         N_("Don't display any dialogs unless there are only \"People Nearby\" accounts"),
188         NULL },
189       { "select-account", 's',
190         G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING, &selected_account_name,
191         N_("Initially select given account (eg, "
192             "gabble/jabber/foo_40example_2eorg0)"),
193         N_("<account-id>") },
194
195       { NULL }
196   };
197
198   optcontext = g_option_context_new (N_("- Empathy Accounts"));
199   g_option_context_add_group (optcontext, gtk_get_option_group (TRUE));
200   g_option_context_add_main_entries (optcontext, options, GETTEXT_PACKAGE);
201
202   argv = *arguments;
203   for (i = 0; argv[i] != NULL; i++)
204     argc++;
205
206   if (!g_option_context_parse (optcontext, &argc, &argv, &error))
207     {
208       g_print ("%s\nRun '%s --help' to see a full list of available command line options.\n",
209           error->message, argv[0]);
210       g_warning ("Error in empathy init: %s", error->message);
211
212       *exit_status = EXIT_FAILURE;
213       retval = TRUE;
214     }
215
216   g_option_context_free (optcontext);
217
218   return retval;
219 }
220
221 #define COMMAND_ACCOUNTS_DIALOG 1
222
223 int
224 main (int argc, char *argv[])
225 {
226   TpAccountManager *account_manager;
227   GtkApplication *app;
228   GObjectClass *app_class;
229   gint retval;
230
231   g_thread_init (NULL);
232   empathy_init ();
233
234   gtk_init (&argc, &argv);
235   empathy_gtk_init ();
236
237   g_set_application_name (_("Empathy Accounts"));
238
239   /* Make empathy and empathy-accounts appear as the same app in gnome-shell */
240   gdk_set_program_class ("Empathy");
241   gtk_window_set_default_icon_name ("empathy");
242   textdomain (GETTEXT_PACKAGE);
243
244   app = gtk_application_new (EMPATHY_ACCOUNTS_DBUS_NAME,
245       G_APPLICATION_HANDLES_COMMAND_LINE);
246   app_class = G_OBJECT_GET_CLASS (app);
247   G_APPLICATION_CLASS (app_class)->local_command_line = local_cmdline;
248
249   account_manager = tp_account_manager_dup ();
250
251   tp_proxy_prepare_async (account_manager, NULL,
252     account_manager_ready_for_accounts_cb, NULL);
253
254   g_signal_connect (app, "command-line", G_CALLBACK (app_command_line_cb),
255       NULL);
256
257   retval = g_application_run (G_APPLICATION (app), argc, argv);
258
259   g_object_unref (account_manager);
260   g_object_unref (app);
261
262   return retval;
263 }