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