]> git.0d.be Git - empathy.git/blob - src/empathy-accounts.c
Drop unused/redundant header inclusions
[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 <glib/gi18n.h>
30
31 #ifdef HAVE_CHEESE
32 #include <cheese-gtk.h>
33 #endif
34
35 #include <libempathy/empathy-utils.h>
36 #include <libempathy-gtk/empathy-ui-utils.h>
37
38 #include "empathy-accounts-common.h"
39
40 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
41 #include <libempathy/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 int
140 app_command_line_cb (GApplication *app,
141     GApplicationCommandLine *cmdline)
142 {
143   TpAccountManager *account_manager;
144
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   return 0;
156 }
157
158 static gboolean
159 local_cmdline (GApplication *app,
160     gchar ***arguments,
161     gint *exit_status)
162 {
163   gint i;
164   gchar **argv;
165   gint argc = 0;
166   gboolean retval = FALSE;
167   GError *error = NULL;
168
169   GOptionContext *optcontext;
170   GOptionEntry options[] = {
171       { "hidden", 'h',
172         0, G_OPTION_ARG_NONE, &hidden,
173         N_("Don't display any dialogs; do any work (eg, importing) and exit"),
174         NULL },
175       { "if-needed", 'n',
176         0, G_OPTION_ARG_NONE, &only_if_needed,
177         N_("Don't display any dialogs unless there are only \"People Nearby\" accounts"),
178         NULL },
179       { "select-account", 's',
180         G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING, &selected_account_name,
181         N_("Initially select given account (eg, "
182             "gabble/jabber/foo_40example_2eorg0)"),
183         N_("<account-id>") },
184
185       { NULL }
186   };
187
188   optcontext = g_option_context_new (N_("- Empathy Accounts"));
189   g_option_context_add_group (optcontext, gtk_get_option_group (TRUE));
190   g_option_context_add_main_entries (optcontext, options, GETTEXT_PACKAGE);
191   g_option_context_set_translation_domain (optcontext, GETTEXT_PACKAGE);
192
193   argv = *arguments;
194   for (i = 0; argv[i] != NULL; i++)
195     argc++;
196
197   if (!g_option_context_parse (optcontext, &argc, &argv, &error))
198     {
199       g_print ("%s\nRun '%s --help' to see a full list of available command line options.\n",
200           error->message, argv[0]);
201       g_warning ("Error in empathy init: %s", error->message);
202
203       *exit_status = EXIT_FAILURE;
204       retval = TRUE;
205     }
206
207   g_option_context_free (optcontext);
208
209   return retval;
210 }
211
212 int
213 main (int argc, char *argv[])
214 {
215   GtkApplication *app;
216   GObjectClass *app_class;
217   gint retval;
218
219   g_type_init ();
220
221 #ifdef HAVE_CHEESE
222   /* Used by the avatar chooser */
223   g_return_val_if_fail (cheese_gtk_init (&argc, &argv), 1);
224 #endif
225
226   empathy_init ();
227
228   gtk_init (&argc, &argv);
229   empathy_gtk_init ();
230
231   g_set_application_name (_("Empathy Accounts"));
232
233   /* Make empathy and empathy-accounts appear as the same app in gnome-shell */
234   gdk_set_program_class ("Empathy");
235   gtk_window_set_default_icon_name ("empathy");
236
237   app = gtk_application_new (EMPATHY_ACCOUNTS_DBUS_NAME,
238       G_APPLICATION_HANDLES_COMMAND_LINE);
239   app_class = G_OBJECT_GET_CLASS (app);
240   G_APPLICATION_CLASS (app_class)->local_command_line = local_cmdline;
241
242   g_signal_connect (app, "command-line", G_CALLBACK (app_command_line_cb),
243       NULL);
244
245   retval = g_application_run (G_APPLICATION (app), argc, argv);
246
247   g_object_unref (app);
248
249   return retval;
250 }