]> git.0d.be Git - empathy.git/blob - src/empathy-accounts-common.c
Only create the EmpathyAccountsDialog if required
[empathy.git] / src / empathy-accounts-common.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-lib.h>
34 #include <unique/unique.h>
35
36 #include <telepathy-glib/account-manager.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-common.h"
44 #include "empathy-accounts-dialog.h"
45 #include "empathy-account-assistant.h"
46 #include "empathy-import-mc4-accounts.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 gboolean
53 empathy_accounts_has_non_salut_accounts (TpAccountManager *manager)
54 {
55   gboolean ret = FALSE;
56   GList *accounts, *l;
57
58   accounts = tp_account_manager_get_valid_accounts (manager);
59
60   for (l = accounts ; l != NULL; l = g_list_next (l))
61     {
62       if (tp_strdiff (tp_account_get_protocol (l->data), "local-xmpp"))
63         {
64           ret = TRUE;
65           break;
66         }
67     }
68
69   g_list_free (accounts);
70
71   return ret;
72 }
73
74 gboolean
75 empathy_accounts_has_accounts (TpAccountManager *manager)
76 {
77   GList *accounts;
78   gboolean has_accounts;
79
80   accounts = tp_account_manager_get_valid_accounts (manager);
81   has_accounts = (accounts != NULL);
82   g_list_free (accounts);
83
84   return has_accounts;
85 }
86
87 void
88 empathy_accounts_import (TpAccountManager *account_mgr,
89     EmpathyConnectionManagers *cm_mgr)
90 {
91   g_return_if_fail (tp_account_manager_is_prepared (account_mgr,
92       TP_ACCOUNT_MANAGER_FEATURE_CORE));
93   g_return_if_fail (empathy_connection_managers_is_ready (cm_mgr));
94
95   if (!empathy_import_mc4_has_imported ())
96     empathy_import_mc4_accounts (cm_mgr);
97 }
98
99 static void
100 do_show_accounts_ui (TpAccountManager *manager,
101     TpAccount *account,
102     GCallback window_destroyed_cb)
103 {
104   static GtkWidget *accounts_window = NULL;
105
106   if (accounts_window == NULL)
107     accounts_window = empathy_accounts_dialog_show (NULL, account);
108
109   if (window_destroyed_cb)
110     g_signal_connect (accounts_window, "destroy", window_destroyed_cb, NULL);
111
112   gtk_window_present (GTK_WINDOW (accounts_window));
113 }
114
115 static GtkWidget *
116 show_account_assistant (EmpathyConnectionManagers *connection_mgrs,
117     GCallback assistant_destroy_cb)
118 {
119   GtkWidget *assistant;
120
121   assistant = empathy_account_assistant_show (NULL, connection_mgrs);
122   if (assistant_destroy_cb)
123     g_signal_connect (assistant, "destroy", assistant_destroy_cb, NULL);
124
125   return assistant;
126 }
127
128 static void
129 connection_managers_prepare_for_accounts (GObject *source,
130     GAsyncResult *result,
131     gpointer user_data)
132 {
133   EmpathyConnectionManagers *cm_mgr = EMPATHY_CONNECTION_MANAGERS (source);
134   GCallback assistant_destroy_cb = G_CALLBACK (user_data);
135
136   if (!empathy_connection_managers_prepare_finish (cm_mgr, result, NULL))
137     goto out;
138
139   show_account_assistant (cm_mgr, assistant_destroy_cb);
140   DEBUG ("would show the account assistant");
141
142 out:
143   g_object_unref (cm_mgr);
144 }
145
146 void
147 empathy_accounts_show_accounts_ui (TpAccountManager *manager,
148     TpAccount *account,
149     GCallback window_destroyed_cb)
150 {
151   g_return_if_fail (TP_IS_ACCOUNT_MANAGER (manager));
152   g_return_if_fail (!account || TP_IS_ACCOUNT (account));
153
154   if (empathy_accounts_has_non_salut_accounts (manager))
155     {
156       do_show_accounts_ui (manager, account, window_destroyed_cb);
157     }
158   else
159     {
160       EmpathyConnectionManagers *cm_mgr;
161
162       cm_mgr = empathy_connection_managers_dup_singleton ();
163
164       empathy_connection_managers_prepare_async (cm_mgr,
165           connection_managers_prepare_for_accounts, window_destroyed_cb);
166     }
167 }