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