]> git.0d.be Git - empathy.git/blob - src/empathy-accounts.c
Separate the accounts dialog into its own program which works with the Gnome preferen...
[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 #include <unique/unique.h>
35
36 #include <telepathy-glib/account-manager.h>
37 #include <telepathy-glib/defs.h>
38 #include <telepathy-glib/util.h>
39
40 #include <libempathy/empathy-utils.h>
41 #include <libempathy/empathy-connection-managers.h>
42 #include <libempathy-gtk/empathy-ui-utils.h>
43
44 #include "empathy-accounts.h"
45 #include "empathy-accounts-common.h"
46 #include "empathy-accounts-dialog.h"
47 #include "empathy-account-assistant.h"
48 #include "empathy-import-mc4-accounts.h"
49 #include "empathy-auto-salut-account-helper.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 try_import = FALSE;
57 static gboolean hidden = FALSE;
58 static gchar *selected_account_name = NULL;
59
60 static void
61 account_manager_ready_for_assistant_cb (GObject *source_object,
62     GAsyncResult *result,
63     gpointer user_data)
64 {
65   TpAccountManager *account_mgr = TP_ACCOUNT_MANAGER (source_object);
66   GError *error = NULL;
67
68   if (!tp_account_manager_prepare_finish (account_mgr, result, &error))
69     {
70       DEBUG ("Failed to prepare account manager: %s", error->message);
71       g_error_free (error);
72       return;
73     }
74
75   g_object_set_data (G_OBJECT (account_mgr), "assistant-destroy-callback",
76       G_CALLBACK (gtk_main_quit));
77
78   empathy_accounts_manager_ready_for_show_assistant (account_mgr, hidden);
79 }
80
81 static void
82 account_prepare_cb (GObject *source_object,
83     GAsyncResult *result,
84     gpointer user_data)
85 {
86   TpAccountManager *manager = TP_ACCOUNT_MANAGER (user_data);
87   TpAccount *account = TP_ACCOUNT (source_object);
88   GError *error = NULL;
89
90   if (!tp_account_prepare_finish (account, result, &error))
91     {
92       DEBUG ("Failed to prepare account: %s", error->message);
93       g_error_free (error);
94
95       account = NULL;
96     }
97
98   empathy_accounts_show_accounts_ui (manager, account,
99       G_CALLBACK (gtk_main_quit));
100 }
101
102 static void
103 account_manager_ready_for_accounts_cb (GObject *source_object,
104     GAsyncResult *result,
105     gpointer user_data)
106 {
107   TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
108   const gchar *account_id = (const gchar*) user_data;
109   GError *error = NULL;
110
111   if (!tp_account_manager_prepare_finish (manager, result, &error))
112     {
113       DEBUG ("Failed to prepare account manager: %s", error->message);
114       g_clear_error (&error);
115       return;
116     }
117
118   if (account_id)
119     {
120       gchar *account_path;
121       TpAccount *account = NULL;
122       TpDBusDaemon *bus;
123
124       /* create and prep the corresponding TpAccount so it's fully ready by the
125        * time we try to select it in the accounts dialog */
126       account_path = g_strdup_printf ("%s%s", TP_ACCOUNT_OBJECT_PATH_BASE,
127           account_id);
128       bus = tp_dbus_daemon_dup (NULL);
129       if ((account = tp_account_new (bus, account_path, &error)))
130         {
131           tp_account_prepare_async (account, NULL, account_prepare_cb, manager);
132           return;
133         }
134       else
135         {
136           DEBUG ("Failed to find account with path %s: %s", account_path,
137               error->message);
138           g_clear_error (&error);
139         }
140
141       g_object_unref (bus);
142       g_free (account_path);
143     }
144   else
145     {
146       empathy_accounts_show_accounts_ui (manager, NULL,
147           G_CALLBACK (gtk_main_quit));
148     }
149 }
150
151 static UniqueResponse
152 unique_app_message_cb (UniqueApp *unique_app,
153     gint command,
154     UniqueMessageData *data,
155     guint timestamp,
156     gpointer user_data)
157 {
158   DEBUG ("Other instance launched, presenting the main window. "
159       "Command=%d, timestamp %u", command, timestamp);
160
161   if (command == UNIQUE_ACTIVATE)
162     {
163       TpAccountManager *account_manager;
164
165       account_manager = tp_account_manager_dup ();
166
167       empathy_accounts_show_accounts_ui (account_manager, NULL,
168           G_CALLBACK (gtk_main_quit));
169
170       g_object_unref (account_manager);
171     }
172   else
173     {
174       g_warning (G_STRLOC "unhandled unique app command %d", command);
175
176       return UNIQUE_RESPONSE_PASSTHROUGH;
177     }
178
179   return UNIQUE_RESPONSE_OK;
180 }
181
182 #define COMMAND_ACCOUNTS_DIALOG 1
183
184 int
185 main (int argc, char *argv[])
186 {
187   TpAccountManager *account_manager;
188   GError *error = NULL;
189   TpDBusDaemon *dbus_daemon;
190   UniqueApp *unique_app;
191
192   GOptionContext *optcontext;
193   GOptionEntry options[] = {
194       { "import", 'i',
195         G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &try_import,
196         N_("Try to import any recognized accounts and display an assistant if "
197             "that fails"),
198         NULL },
199       { "hidden", 'h',
200         0, G_OPTION_ARG_NONE, &hidden,
201         N_("Don't display any dialogs; do any work (eg, importing) and exit"),
202         NULL },
203       { "select-account", 's',
204         G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING, &selected_account_name,
205         N_("Initially select given account (eg, "
206             "gabble/jabber/foo_40example_2eorg0)"),
207         N_("<account-id>") },
208
209       { NULL }
210   };
211
212   g_thread_init (NULL);
213   empathy_init ();
214
215   optcontext = g_option_context_new (N_("- Empathy Accounts"));
216   g_option_context_add_group (optcontext, gtk_get_option_group (TRUE));
217   g_option_context_add_main_entries (optcontext, options, GETTEXT_PACKAGE);
218
219   if (!g_option_context_parse (optcontext, &argc, &argv, &error))
220     {
221       g_print ("%s\nRun '%s --help' to see a full list of available command line options.\n",
222           error->message, argv[0]);
223       g_warning ("Error in empathy init: %s", error->message);
224       return EXIT_FAILURE;
225     }
226
227   g_option_context_free (optcontext);
228
229   empathy_gtk_init ();
230
231   g_set_application_name (_(PACKAGE_NAME " Accounts"));
232
233   gtk_window_set_default_icon_name ("empathy");
234
235   unique_app = unique_app_new (EMPATHY_ACCOUNTS_DBUS_NAME, NULL);
236
237   if (unique_app_is_running (unique_app))
238     {
239       unique_app_send_message (unique_app, UNIQUE_ACTIVATE, NULL);
240
241       g_object_unref (unique_app);
242       return EXIT_SUCCESS;
243     }
244
245   /* Take well-known name */
246   dbus_daemon = tp_dbus_daemon_dup (&error);
247   if (error == NULL)
248     {
249       if (!tp_dbus_daemon_request_name (dbus_daemon,
250           EMPATHY_ACCOUNTS_DBUS_NAME, TRUE, &error))
251         {
252           DEBUG ("Failed to request well-known name: %s",
253                  error ? error->message : "no message");
254           g_clear_error (&error);
255         }
256       g_object_unref (dbus_daemon);
257     }
258   else
259     {
260       DEBUG ("Failed to dup dbus daemon: %s",
261              error ? error->message : "no message");
262       g_clear_error (&error);
263     }
264
265   account_manager = tp_account_manager_dup ();
266
267   if (try_import)
268     {
269       tp_account_manager_prepare_async (account_manager, NULL,
270           account_manager_ready_for_assistant_cb, NULL);
271     }
272   else
273     {
274       tp_account_manager_prepare_async (account_manager, NULL,
275           account_manager_ready_for_accounts_cb, selected_account_name);
276     }
277
278   g_signal_connect (unique_app, "message-received",
279       G_CALLBACK (unique_app_message_cb), NULL);
280
281   gtk_main ();
282
283   g_object_unref (account_manager);
284   g_object_unref (unique_app);
285
286   return EXIT_SUCCESS;
287 }