]> git.0d.be Git - empathy.git/blob - src/empathy-accounts.c
Port the accounts dialog to new G/tkApplication.
[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 #include <telepathy-glib/account-manager.h>
36 #include <telepathy-glib/defs.h>
37 #include <telepathy-glib/util.h>
38
39 #include <libempathy/empathy-utils.h>
40 #include <libempathy/empathy-connection-managers.h>
41 #include <libempathy-gtk/empathy-ui-utils.h>
42
43 #include "empathy-accounts.h"
44 #include "empathy-accounts-common.h"
45 #include "empathy-accounts-dialog.h"
46 #include "empathy-account-assistant.h"
47 #include "empathy-auto-salut-account-helper.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 account_prepare_cb (GObject *source_object,
60     GAsyncResult *result,
61     gpointer user_data)
62 {
63   TpAccountManager *manager = TP_ACCOUNT_MANAGER (user_data);
64   TpAccount *account = TP_ACCOUNT (source_object);
65   GError *error = NULL;
66
67   if (!tp_account_prepare_finish (account, result, &error))
68     {
69       DEBUG ("Failed to prepare account: %s", error->message);
70       g_error_free (error);
71
72       account = NULL;
73     }
74
75   empathy_accounts_show_accounts_ui (manager, account,
76       G_CALLBACK (gtk_main_quit));
77 }
78
79 static void
80 maybe_show_accounts_ui (TpAccountManager *manager)
81 {
82   if (hidden ||
83       (only_if_needed && empathy_accounts_has_non_salut_accounts (manager)))
84     gtk_main_quit ();
85   else
86     empathy_accounts_show_accounts_ui (manager, NULL, gtk_main_quit);
87 }
88
89 static void
90 account_manager_ready_for_accounts_cb (GObject *source_object,
91     GAsyncResult *result,
92     gpointer user_data)
93 {
94   TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
95   const gchar *account_id = (const gchar*) user_data;
96   GError *error = NULL;
97
98   if (!tp_account_manager_prepare_finish (manager, result, &error))
99     {
100       DEBUG ("Failed to prepare account manager: %s", error->message);
101       g_clear_error (&error);
102       return;
103     }
104
105   if (account_id != NULL)
106     {
107       gchar *account_path;
108       TpAccount *account = NULL;
109       TpDBusDaemon *bus;
110
111       /* create and prep the corresponding TpAccount so it's fully ready by the
112        * time we try to select it in the accounts dialog */
113       account_path = g_strdup_printf ("%s%s", TP_ACCOUNT_OBJECT_PATH_BASE,
114           account_id);
115       bus = tp_dbus_daemon_dup (NULL);
116       if ((account = tp_account_new (bus, account_path, &error)))
117         {
118           tp_account_prepare_async (account, NULL, account_prepare_cb, manager);
119           return;
120         }
121       else
122         {
123           DEBUG ("Failed to find account with path %s: %s", account_path,
124               error->message);
125           g_clear_error (&error);
126         }
127
128       g_object_unref (bus);
129       g_free (account_path);
130     }
131   else
132     {
133       maybe_show_accounts_ui (manager);
134     }
135 }
136
137 static void
138 app_activate_cb (GApplication *app)
139 {
140   TpAccountManager *account_manager;
141
142   account_manager = tp_account_manager_dup ();
143
144   empathy_accounts_show_accounts_ui (account_manager, NULL,
145           G_CALLBACK (gtk_main_quit));
146
147   g_object_unref (account_manager);
148 }
149
150 #define COMMAND_ACCOUNTS_DIALOG 1
151
152 int
153 main (int argc, char *argv[])
154 {
155   TpAccountManager *account_manager;
156   GError *error = NULL;
157   GtkApplication *app;
158
159   GOptionContext *optcontext;
160   GOptionEntry options[] = {
161       { "hidden", 'h',
162         0, G_OPTION_ARG_NONE, &hidden,
163         N_("Don't display any dialogs; do any work (eg, importing) and exit"),
164         NULL },
165       { "if-needed", 'n',
166         0, G_OPTION_ARG_NONE, &only_if_needed,
167         N_("Don't display any dialogs if there are any non-Salut accounts"),
168         NULL },
169       { "select-account", 's',
170         G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING, &selected_account_name,
171         N_("Initially select given account (eg, "
172             "gabble/jabber/foo_40example_2eorg0)"),
173         N_("<account-id>") },
174
175       { NULL }
176   };
177
178   g_thread_init (NULL);
179   empathy_init ();
180
181   optcontext = g_option_context_new (N_("- Empathy Accounts"));
182   g_option_context_add_group (optcontext, gtk_get_option_group (TRUE));
183   g_option_context_add_main_entries (optcontext, options, GETTEXT_PACKAGE);
184
185   if (!g_option_context_parse (optcontext, &argc, &argv, &error))
186     {
187       g_print ("%s\nRun '%s --help' to see a full list of available command line options.\n",
188           error->message, argv[0]);
189       g_warning ("Error in empathy init: %s", error->message);
190       return EXIT_FAILURE;
191     }
192
193   g_option_context_free (optcontext);
194
195   empathy_gtk_init ();
196
197   g_set_application_name (_("Empathy Accounts"));
198
199   gtk_window_set_default_icon_name ("empathy");
200   textdomain (GETTEXT_PACKAGE);
201
202   app = gtk_application_new (EMPATHY_ACCOUNTS_DBUS_NAME,
203       G_APPLICATION_IS_SERVICE);
204
205   account_manager = tp_account_manager_dup ();
206
207   tp_account_manager_prepare_async (account_manager, NULL,
208     account_manager_ready_for_accounts_cb, selected_account_name);
209
210   g_signal_connect (app, "activate", G_CALLBACK (app_activate_cb), NULL);
211
212   /* don't let this application exit automatically */
213   g_application_hold (G_APPLICATION (app));
214   g_application_run (G_APPLICATION (app), argc, argv);
215
216   g_object_unref (account_manager);
217   g_object_unref (app);
218
219   return EXIT_SUCCESS;
220 }