]> git.0d.be Git - empathy.git/blob - src/empathy-accounts-common.c
Move should_create_salut_account to local-xmpp-assistant-widget
[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
35 #include <telepathy-glib/account-manager.h>
36 #include <telepathy-glib/util.h>
37
38 #include <libempathy/empathy-utils.h>
39 #include <libempathy/empathy-connection-managers.h>
40 #include <libempathy-gtk/empathy-ui-utils.h>
41
42 #include "empathy-accounts-common.h"
43 #include "empathy-accounts-dialog.h"
44 #include "empathy-account-assistant.h"
45 #include "empathy-auto-salut-account-helper.h"
46
47 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
48 #include <libempathy/empathy-debug.h>
49
50 gboolean
51 empathy_accounts_has_non_salut_accounts (TpAccountManager *manager)
52 {
53   gboolean ret = FALSE;
54   GList *accounts, *l;
55
56   accounts = tp_account_manager_get_valid_accounts (manager);
57
58   for (l = accounts ; l != NULL; l = g_list_next (l))
59     {
60       if (tp_strdiff (tp_account_get_protocol (l->data), "local-xmpp"))
61         {
62           ret = TRUE;
63           break;
64         }
65     }
66
67   g_list_free (accounts);
68
69   return ret;
70 }
71
72 gboolean
73 empathy_accounts_has_accounts (TpAccountManager *manager)
74 {
75   GList *accounts;
76   gboolean has_accounts;
77
78   accounts = tp_account_manager_get_valid_accounts (manager);
79   has_accounts = (accounts != NULL);
80   g_list_free (accounts);
81
82   return has_accounts;
83 }
84
85 static void
86 do_show_accounts_ui (TpAccountManager *manager,
87     TpAccount *account,
88     GApplication *app)
89 {
90   static GtkWidget *accounts_window = NULL;
91
92   if (accounts_window == NULL)
93     {
94       accounts_window = empathy_accounts_dialog_show (NULL, account);
95
96       gtk_application_add_window (GTK_APPLICATION (app),
97           GTK_WINDOW (accounts_window));
98     }
99
100   gtk_window_present (GTK_WINDOW (accounts_window));
101 }
102
103 static GtkWidget *
104 show_account_assistant (EmpathyConnectionManagers *connection_mgrs,
105     GApplication *app)
106 {
107   GtkWidget *assistant;
108
109   assistant = empathy_account_assistant_show (NULL, connection_mgrs);
110
111   gtk_application_add_window (GTK_APPLICATION (app), GTK_WINDOW (assistant));
112
113   return assistant;
114 }
115
116 static void
117 connection_managers_prepare_for_accounts (GObject *source,
118     GAsyncResult *result,
119     gpointer user_data)
120 {
121   EmpathyConnectionManagers *cm_mgr = EMPATHY_CONNECTION_MANAGERS (source);
122   GApplication *app = user_data;
123
124   if (!empathy_connection_managers_prepare_finish (cm_mgr, result, NULL))
125     goto out;
126
127   show_account_assistant (cm_mgr, app);
128   DEBUG ("would show the account assistant");
129
130 out:
131   g_object_unref (cm_mgr);
132   g_application_release (app);
133 }
134
135 void
136 empathy_accounts_show_accounts_ui (TpAccountManager *manager,
137     TpAccount *account,
138     gboolean assistant,
139     GApplication *app)
140 {
141   g_return_if_fail (TP_IS_ACCOUNT_MANAGER (manager));
142   g_return_if_fail (!account || TP_IS_ACCOUNT (account));
143
144   if ((empathy_accounts_has_non_salut_accounts (manager) && !assistant) ||
145           account != NULL)
146     {
147       do_show_accounts_ui (manager, account, app);
148     }
149   else
150     {
151       EmpathyConnectionManagers *cm_mgr;
152
153       cm_mgr = empathy_connection_managers_dup_singleton ();
154
155       /* Hold the application while preparing cm_mgr */
156       g_application_hold (app);
157
158       empathy_connection_managers_prepare_async (cm_mgr,
159           connection_managers_prepare_for_accounts, app);
160     }
161 }