]> git.0d.be Git - empathy.git/blob - src/empathy-accounts.c
Merge branch 'bug-662727'
[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 assistant = FALSE;
58
59 static void
60 maybe_show_accounts_ui (TpAccountManager *manager,
61     GApplication *app)
62 {
63   if (hidden)
64     return;
65
66   if (only_if_needed && empathy_accounts_has_non_salut_accounts (manager))
67     return;
68
69   empathy_accounts_show_accounts_ui (manager, NULL, assistant, app);
70 }
71
72 static TpAccount *
73 find_account (TpAccountManager *mgr,
74     const gchar *path)
75 {
76   GList *accounts, *l;
77   TpAccount *found = NULL;
78
79   accounts = tp_account_manager_get_valid_accounts (mgr);
80   for (l = accounts; l != NULL; l = g_list_next (l))
81     {
82       if (!tp_strdiff (tp_proxy_get_object_path (l->data), path))
83         {
84           found = l->data;
85           break;
86         }
87     }
88
89   g_list_free (accounts);
90   return found;
91 }
92
93 static void
94 account_manager_ready_for_accounts_cb (GObject *source_object,
95     GAsyncResult *result,
96     gpointer user_data)
97 {
98   TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
99   GError *error = NULL;
100   GApplication *app = G_APPLICATION (user_data);
101
102   if (!tp_proxy_prepare_finish (manager, result, &error))
103     {
104       DEBUG ("Failed to prepare account manager: %s", error->message);
105       g_clear_error (&error);
106       goto out;
107     }
108
109   if (selected_account_name != NULL)
110     {
111       gchar *account_path;
112       TpAccount *account;
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       account = find_account (manager, account_path);
123
124       if (account != NULL)
125         {
126           empathy_accounts_show_accounts_ui (manager, account, assistant, app);
127           goto out;
128         }
129       else
130         {
131           DEBUG ("Failed to find account with path %s", account_path);
132
133           g_clear_error (&error);
134
135           maybe_show_accounts_ui (manager, app);
136         }
137
138       g_free (account_path);
139     }
140   else
141     {
142       maybe_show_accounts_ui (manager, app);
143     }
144
145 out:
146   g_application_release (app);
147 }
148
149 static int
150 app_command_line_cb (GApplication *app,
151     GApplicationCommandLine *cmdline)
152 {
153   TpAccountManager *account_manager;
154
155   account_manager = tp_account_manager_dup ();
156
157   /* Hold the application while preparing the AM */
158   g_application_hold (app);
159
160   tp_proxy_prepare_async (account_manager, NULL,
161     account_manager_ready_for_accounts_cb, app);
162
163   g_object_unref (account_manager);
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       { "assistant", 'a',
195         0, G_OPTION_ARG_NONE, &assistant,
196         N_("Show account assistant"),
197         NULL },
198
199       { NULL }
200   };
201
202   optcontext = g_option_context_new (N_("- Empathy Accounts"));
203   g_option_context_add_group (optcontext, gtk_get_option_group (TRUE));
204   g_option_context_add_main_entries (optcontext, options, GETTEXT_PACKAGE);
205
206   argv = *arguments;
207   for (i = 0; argv[i] != NULL; i++)
208     argc++;
209
210   if (!g_option_context_parse (optcontext, &argc, &argv, &error))
211     {
212       g_print ("%s\nRun '%s --help' to see a full list of available command line options.\n",
213           error->message, argv[0]);
214       g_warning ("Error in empathy init: %s", error->message);
215
216       *exit_status = EXIT_FAILURE;
217       retval = TRUE;
218     }
219
220   g_option_context_free (optcontext);
221
222   return retval;
223 }
224
225 int
226 main (int argc, char *argv[])
227 {
228   GtkApplication *app;
229   GObjectClass *app_class;
230   gint retval;
231
232   g_thread_init (NULL);
233   empathy_init ();
234
235   gtk_init (&argc, &argv);
236   empathy_gtk_init ();
237
238   g_set_application_name (_("Empathy Accounts"));
239
240   /* Make empathy and empathy-accounts appear as the same app in gnome-shell */
241   gdk_set_program_class ("Empathy");
242   gtk_window_set_default_icon_name ("empathy");
243   textdomain (GETTEXT_PACKAGE);
244
245   app = gtk_application_new (EMPATHY_ACCOUNTS_DBUS_NAME,
246       G_APPLICATION_HANDLES_COMMAND_LINE);
247   app_class = G_OBJECT_GET_CLASS (app);
248   G_APPLICATION_CLASS (app_class)->local_command_line = local_cmdline;
249
250   g_signal_connect (app, "command-line", G_CALLBACK (app_command_line_cb),
251       NULL);
252
253   retval = g_application_run (G_APPLICATION (app), argc, argv);
254
255   g_object_unref (app);
256
257   return retval;
258 }