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