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