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