]> git.0d.be Git - empathy.git/blob - src/cc-empathy-accounts-page.c
Always popup the accounts wizard if we don't have salut accounts
[empathy.git] / src / cc-empathy-accounts-page.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
2  *
3  * Copyright (C) 2010 Red Hat, Inc.
4  * Copyright (C) 2010 Collabora Ltd.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include "config.h"
22
23 #include <stdlib.h>
24 #include <stdio.h>
25
26 #include <gtk/gtk.h>
27 #include <gio/gio.h>
28 #include <glib/gi18n-lib.h>
29
30 #include <telepathy-glib/account-manager.h>
31 #include <libempathy/empathy-connection-managers.h>
32 #include <libempathy-gtk/empathy-ui-utils.h>
33
34 #include "cc-empathy-accounts-page.h"
35 #include "empathy-accounts-common.h"
36 #include "empathy-account-assistant.h"
37 #include "empathy-accounts-dialog.h"
38
39 #define CC_EMPATHY_ACCOUNTS_PAGE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), CC_TYPE_EMPATHY_ACCOUNTS_PAGE, CcEmpathyAccountsPagePrivate))
40
41 struct CcEmpathyAccountsPagePrivate
42 {
43   /* the original window holding the dialog content; it needs to be retained and
44    * destroyed in our finalize(), since it invalidates its children (even if
45    * they've already been reparented by the time it is destroyed) */
46   GtkWidget *accounts_window;
47 };
48
49 G_DEFINE_TYPE (CcEmpathyAccountsPage, cc_empathy_accounts_page, CC_TYPE_PAGE)
50
51 static void
52 page_pack_with_accounts_dialog (CcEmpathyAccountsPage *page)
53 {
54   GtkWidget *content;
55   GtkWidget *action_area;
56
57   if (!page->priv->accounts_window)
58     {
59       page->priv->accounts_window = empathy_accounts_dialog_show (NULL, NULL);
60       gtk_widget_hide (page->priv->accounts_window);
61
62       content = gtk_dialog_get_content_area (
63           GTK_DIALOG (page->priv->accounts_window));
64       action_area = gtk_dialog_get_action_area (
65           GTK_DIALOG (page->priv->accounts_window));
66       gtk_widget_set_no_show_all (action_area, TRUE);
67       gtk_widget_hide (action_area);
68
69       gtk_widget_reparent (content, GTK_WIDGET (page));
70     }
71 }
72
73 static void
74 connection_managers_prepare (GObject *source,
75     GAsyncResult *result,
76     gpointer user_data)
77 {
78   EmpathyConnectionManagers *cm_mgr = EMPATHY_CONNECTION_MANAGERS (source);
79   TpAccountManager *account_mgr;
80   CcEmpathyAccountsPage *page;
81
82   account_mgr = TP_ACCOUNT_MANAGER (g_object_get_data (G_OBJECT (cm_mgr),
83       "account-manager"));
84   page = CC_EMPATHY_ACCOUNTS_PAGE (g_object_get_data (G_OBJECT (cm_mgr),
85         "page"));
86
87   if (!empathy_connection_managers_prepare_finish (cm_mgr, result, NULL))
88     goto out;
89
90   page_pack_with_accounts_dialog (page);
91
92   empathy_accounts_import (account_mgr, cm_mgr);
93
94   if (!empathy_accounts_has_non_salut_accounts (account_mgr))
95     empathy_account_assistant_show (NULL, cm_mgr);
96
97 out:
98   /* remove ref from active_changed() */
99   g_object_unref (account_mgr);
100   g_object_unref (cm_mgr);
101 }
102
103 static void
104 account_manager_ready_for_accounts_cb (GObject *source_object,
105     GAsyncResult *result,
106     gpointer user_data)
107 {
108   TpAccountManager *account_mgr = TP_ACCOUNT_MANAGER (source_object);
109   CcEmpathyAccountsPage *page = CC_EMPATHY_ACCOUNTS_PAGE (user_data);
110   GError *error = NULL;
111
112   if (!tp_account_manager_prepare_finish (account_mgr, result, &error))
113     {
114       g_warning ("Failed to prepare account manager: %s", error->message);
115       g_error_free (error);
116       return;
117     }
118
119   if (empathy_accounts_has_non_salut_accounts (account_mgr))
120     {
121       page_pack_with_accounts_dialog (page);
122
123       /* remove ref from active_changed() */
124       g_object_unref (account_mgr);
125     }
126   else
127     {
128       EmpathyConnectionManagers *cm_mgr;
129
130       cm_mgr = empathy_connection_managers_dup_singleton ();
131
132       g_object_set_data_full (G_OBJECT (cm_mgr), "account-manager",
133           g_object_ref (account_mgr), (GDestroyNotify) g_object_unref);
134       g_object_set_data_full (G_OBJECT (cm_mgr), "page",
135           g_object_ref (page), (GDestroyNotify) g_object_unref);
136
137       empathy_connection_managers_prepare_async (cm_mgr,
138           connection_managers_prepare, page);
139     }
140 }
141
142 static void
143 active_changed (CcPage *base_page,
144     gboolean is_active)
145 {
146   CcEmpathyAccountsPage *page = CC_EMPATHY_ACCOUNTS_PAGE (base_page);
147   TpAccountManager *account_manager;
148
149   if (is_active)
150     {
151       /* unref'd in final endpoint callbacks */
152       account_manager = tp_account_manager_dup ();
153
154       tp_account_manager_prepare_async (account_manager, NULL,
155           account_manager_ready_for_accounts_cb, page);
156     }
157 }
158
159 static void
160 cc_empathy_accounts_page_finalize (GObject *object)
161 {
162   CcEmpathyAccountsPage *page;
163
164   g_return_if_fail (object != NULL);
165   g_return_if_fail (CC_IS_EMPATHY_ACCOUNTS_PAGE (object));
166
167   page = CC_EMPATHY_ACCOUNTS_PAGE (object);
168
169   g_return_if_fail (page->priv != NULL);
170
171   gtk_widget_destroy (page->priv->accounts_window);
172
173   G_OBJECT_CLASS (cc_empathy_accounts_page_parent_class)->finalize (object);
174 }
175
176 static void
177 cc_empathy_accounts_page_class_init (CcEmpathyAccountsPageClass *klass)
178 {
179   GObjectClass  *object_class = G_OBJECT_CLASS (klass);
180   CcPageClass   *page_class = CC_PAGE_CLASS (klass);
181
182   object_class->finalize = cc_empathy_accounts_page_finalize;
183
184   page_class->active_changed = active_changed;
185
186   g_type_class_add_private (klass, sizeof (CcEmpathyAccountsPagePrivate));
187 }
188
189 static void
190 cc_empathy_accounts_page_init (CcEmpathyAccountsPage *page)
191 {
192   page->priv = CC_EMPATHY_ACCOUNTS_PAGE_GET_PRIVATE (page);
193
194   empathy_gtk_init ();
195 }
196
197 CcPage *
198 cc_empathy_accounts_page_new (void)
199 {
200   GObject *object;
201
202   object = g_object_new (CC_TYPE_EMPATHY_ACCOUNTS_PAGE,
203       "display-name", _("Messaging and VoIP Accounts"),
204       "id", "general",
205       NULL);
206
207   return CC_PAGE (object);
208 }