]> git.0d.be Git - empathy.git/blob - src/empathy-accounts.c
empathy-accounts.c: check return value of unique_app_send_message (#629782)
[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-auto-salut-account-helper.h"
49
50 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
51 #include <libempathy/empathy-debug.h>
52
53 #define EMPATHY_ACCOUNTS_DBUS_NAME "org.gnome.EmpathyAccounts"
54
55 static gboolean only_if_needed = FALSE;
56 static gboolean hidden = FALSE;
57 static gchar *selected_account_name = NULL;
58
59 static void
60 account_prepare_cb (GObject *source_object,
61     GAsyncResult *result,
62     gpointer user_data)
63 {
64   TpAccountManager *manager = TP_ACCOUNT_MANAGER (user_data);
65   TpAccount *account = TP_ACCOUNT (source_object);
66   GError *error = NULL;
67
68   if (!tp_account_prepare_finish (account, result, &error))
69     {
70       DEBUG ("Failed to prepare account: %s", error->message);
71       g_error_free (error);
72
73       account = NULL;
74     }
75
76   empathy_accounts_show_accounts_ui (manager, account,
77       G_CALLBACK (gtk_main_quit));
78 }
79
80 static void
81 maybe_show_accounts_ui (TpAccountManager *manager)
82 {
83   if (hidden ||
84       (only_if_needed && empathy_accounts_has_non_salut_accounts (manager)))
85     gtk_main_quit ();
86   else
87     empathy_accounts_show_accounts_ui (manager, NULL, gtk_main_quit);
88 }
89
90 static void
91 account_manager_ready_for_accounts_cb (GObject *source_object,
92     GAsyncResult *result,
93     gpointer user_data)
94 {
95   TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
96   const gchar *account_id = (const gchar*) user_data;
97   GError *error = NULL;
98
99   if (!tp_account_manager_prepare_finish (manager, result, &error))
100     {
101       DEBUG ("Failed to prepare account manager: %s", error->message);
102       g_clear_error (&error);
103       return;
104     }
105
106   if (account_id != NULL)
107     {
108       gchar *account_path;
109       TpAccount *account = NULL;
110       TpDBusDaemon *bus;
111
112       /* create and prep the corresponding TpAccount so it's fully ready by the
113        * time we try to select it in the accounts dialog */
114       account_path = g_strdup_printf ("%s%s", TP_ACCOUNT_OBJECT_PATH_BASE,
115           account_id);
116       bus = tp_dbus_daemon_dup (NULL);
117       if ((account = tp_account_new (bus, account_path, &error)))
118         {
119           tp_account_prepare_async (account, NULL, account_prepare_cb, manager);
120           return;
121         }
122       else
123         {
124           DEBUG ("Failed to find account with path %s: %s", account_path,
125               error->message);
126           g_clear_error (&error);
127         }
128
129       g_object_unref (bus);
130       g_free (account_path);
131     }
132   else
133     {
134       maybe_show_accounts_ui (manager);
135     }
136 }
137
138 static UniqueResponse
139 unique_app_message_cb (UniqueApp *unique_app,
140     gint command,
141     UniqueMessageData *data,
142     guint timestamp,
143     gpointer user_data)
144 {
145   DEBUG ("Other instance launched, presenting the main window. "
146       "Command=%d, timestamp %u", command, timestamp);
147
148   if (command == UNIQUE_ACTIVATE)
149     {
150       TpAccountManager *account_manager;
151
152       account_manager = tp_account_manager_dup ();
153
154       empathy_accounts_show_accounts_ui (account_manager, NULL,
155           G_CALLBACK (gtk_main_quit));
156
157       g_object_unref (account_manager);
158     }
159   else
160     {
161       g_warning (G_STRLOC "unhandled unique app command %d", command);
162
163       return UNIQUE_RESPONSE_PASSTHROUGH;
164     }
165
166   return UNIQUE_RESPONSE_OK;
167 }
168
169 #define COMMAND_ACCOUNTS_DIALOG 1
170
171 int
172 main (int argc, char *argv[])
173 {
174   TpAccountManager *account_manager;
175   GError *error = NULL;
176   UniqueApp *unique_app;
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 if there are any non-Salut 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   g_thread_init (NULL);
198   empathy_init ();
199
200   optcontext = g_option_context_new (N_("- Empathy Accounts"));
201   g_option_context_add_group (optcontext, gtk_get_option_group (TRUE));
202   g_option_context_add_main_entries (optcontext, options, GETTEXT_PACKAGE);
203
204   if (!g_option_context_parse (optcontext, &argc, &argv, &error))
205     {
206       g_print ("%s\nRun '%s --help' to see a full list of available command line options.\n",
207           error->message, argv[0]);
208       g_warning ("Error in empathy init: %s", error->message);
209       return EXIT_FAILURE;
210     }
211
212   g_option_context_free (optcontext);
213
214   empathy_gtk_init ();
215
216   g_set_application_name (_("Empathy Accounts"));
217
218   gtk_window_set_default_icon_name ("empathy");
219   textdomain (GETTEXT_PACKAGE);
220
221   unique_app = unique_app_new (EMPATHY_ACCOUNTS_DBUS_NAME, NULL);
222
223   if (unique_app_is_running (unique_app))
224     {
225       if (unique_app_send_message (unique_app, UNIQUE_ACTIVATE, NULL) ==
226           UNIQUE_RESPONSE_OK)
227         {
228           g_object_unref (unique_app);
229           return EXIT_SUCCESS;
230         }
231     }
232
233   account_manager = tp_account_manager_dup ();
234
235   tp_account_manager_prepare_async (account_manager, NULL,
236     account_manager_ready_for_accounts_cb, selected_account_name);
237
238   g_signal_connect (unique_app, "message-received",
239       G_CALLBACK (unique_app_message_cb), NULL);
240
241   gtk_main ();
242
243   g_object_unref (account_manager);
244   g_object_unref (unique_app);
245
246   return EXIT_SUCCESS;
247 }