]> git.0d.be Git - empathy.git/blob - src/empathy-accounts.c
remove empathy-auto-salut-account-helper
[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
47 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
48 #include <libempathy/empathy-debug.h>
49
50 #define EMPATHY_ACCOUNTS_DBUS_NAME "org.gnome.EmpathyAccounts"
51
52 static gboolean only_if_needed = FALSE;
53 static gboolean hidden = FALSE;
54 static gchar *selected_account_name = NULL;
55
56 static void
57 maybe_show_accounts_ui (TpAccountManager *manager,
58     GApplication *app)
59 {
60   if (hidden)
61     return;
62
63   if (only_if_needed && empathy_accounts_has_non_salut_accounts (manager))
64     return;
65
66   empathy_accounts_show_accounts_ui (manager, NULL, app);
67 }
68
69 static TpAccount *
70 find_account (TpAccountManager *mgr,
71     const gchar *path)
72 {
73   GList *accounts, *l;
74   TpAccount *found = NULL;
75
76   accounts = tp_account_manager_get_valid_accounts (mgr);
77   for (l = accounts; l != NULL; l = g_list_next (l))
78     {
79       if (!tp_strdiff (tp_proxy_get_object_path (l->data), path))
80         {
81           found = l->data;
82           break;
83         }
84     }
85
86   g_list_free (accounts);
87   return found;
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   GApplication *app = G_APPLICATION (user_data);
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       goto out;
104     }
105
106   if (selected_account_name != NULL)
107     {
108       gchar *account_path;
109       TpAccount *account;
110
111       /* create and prep the corresponding TpAccount so it's fully ready by the
112        * time we try to select it in the accounts dialog */
113       if (g_str_has_prefix (selected_account_name, TP_ACCOUNT_OBJECT_PATH_BASE))
114         account_path = g_strdup (selected_account_name);
115       else
116         account_path = g_strdup_printf ("%s%s", TP_ACCOUNT_OBJECT_PATH_BASE,
117             selected_account_name);
118
119       account = find_account (manager, account_path);
120
121       if (account != NULL)
122         {
123           empathy_accounts_show_accounts_ui (manager, account, app);
124           goto out;
125         }
126       else
127         {
128           DEBUG ("Failed to find account with path %s", account_path);
129
130           g_clear_error (&error);
131
132           maybe_show_accounts_ui (manager, app);
133         }
134
135       g_free (account_path);
136     }
137   else
138     {
139       maybe_show_accounts_ui (manager, app);
140     }
141
142 out:
143   g_application_release (app);
144 }
145
146 static int
147 app_command_line_cb (GApplication *app,
148     GApplicationCommandLine *cmdline)
149 {
150   TpAccountManager *account_manager;
151
152   account_manager = tp_account_manager_dup ();
153
154   /* Hold the application while preparing the AM */
155   g_application_hold (app);
156
157   tp_proxy_prepare_async (account_manager, NULL,
158     account_manager_ready_for_accounts_cb, app);
159
160   g_object_unref (account_manager);
161
162   return 0;
163 }
164
165 static gboolean
166 local_cmdline (GApplication *app,
167     gchar ***arguments,
168     gint *exit_status)
169 {
170   gint i;
171   gchar **argv;
172   gint argc = 0;
173   gboolean retval = FALSE;
174   GError *error = NULL;
175
176   GOptionContext *optcontext;
177   GOptionEntry options[] = {
178       { "hidden", 'h',
179         0, G_OPTION_ARG_NONE, &hidden,
180         N_("Don't display any dialogs; do any work (eg, importing) and exit"),
181         NULL },
182       { "if-needed", 'n',
183         0, G_OPTION_ARG_NONE, &only_if_needed,
184         N_("Don't display any dialogs unless there are only \"People Nearby\" accounts"),
185         NULL },
186       { "select-account", 's',
187         G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING, &selected_account_name,
188         N_("Initially select given account (eg, "
189             "gabble/jabber/foo_40example_2eorg0)"),
190         N_("<account-id>") },
191
192       { NULL }
193   };
194
195   optcontext = g_option_context_new (N_("- Empathy Accounts"));
196   g_option_context_add_group (optcontext, gtk_get_option_group (TRUE));
197   g_option_context_add_main_entries (optcontext, options, GETTEXT_PACKAGE);
198
199   argv = *arguments;
200   for (i = 0; argv[i] != NULL; i++)
201     argc++;
202
203   if (!g_option_context_parse (optcontext, &argc, &argv, &error))
204     {
205       g_print ("%s\nRun '%s --help' to see a full list of available command line options.\n",
206           error->message, argv[0]);
207       g_warning ("Error in empathy init: %s", error->message);
208
209       *exit_status = EXIT_FAILURE;
210       retval = TRUE;
211     }
212
213   g_option_context_free (optcontext);
214
215   return retval;
216 }
217
218 int
219 main (int argc, char *argv[])
220 {
221   GtkApplication *app;
222   GObjectClass *app_class;
223   gint retval;
224
225   g_thread_init (NULL);
226   empathy_init ();
227
228   gtk_init (&argc, &argv);
229   empathy_gtk_init ();
230
231   g_set_application_name (_("Empathy Accounts"));
232
233   /* Make empathy and empathy-accounts appear as the same app in gnome-shell */
234   gdk_set_program_class ("Empathy");
235   gtk_window_set_default_icon_name ("empathy");
236   textdomain (GETTEXT_PACKAGE);
237
238   app = gtk_application_new (EMPATHY_ACCOUNTS_DBUS_NAME,
239       G_APPLICATION_HANDLES_COMMAND_LINE);
240   app_class = G_OBJECT_GET_CLASS (app);
241   G_APPLICATION_CLASS (app_class)->local_command_line = local_cmdline;
242
243   g_signal_connect (app, "command-line", G_CALLBACK (app_command_line_cb),
244       NULL);
245
246   retval = g_application_run (G_APPLICATION (app), argc, argv);
247
248   g_object_unref (app);
249
250   return retval;
251 }