]> git.0d.be Git - empathy.git/blob - src/cc-empathy-accounts-page.c
Remove EmpathyAccountsPlugin's requirement that it be a singleton, so we can reset...
[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 != NULL)
58     {
59       gtk_widget_destroy (page->priv->accounts_window);
60       gtk_container_remove (GTK_CONTAINER (page),
61           gtk_bin_get_child (GTK_BIN (page)));
62     }
63
64     page->priv->accounts_window = empathy_accounts_dialog_show (NULL, NULL);
65     gtk_widget_hide (page->priv->accounts_window);
66
67     content = gtk_dialog_get_content_area (
68         GTK_DIALOG (page->priv->accounts_window));
69     action_area = gtk_dialog_get_action_area (
70         GTK_DIALOG (page->priv->accounts_window));
71     gtk_widget_set_no_show_all (action_area, TRUE);
72     gtk_widget_hide (action_area);
73
74     gtk_widget_reparent (content, GTK_WIDGET (page));
75 }
76
77 static void
78 connection_managers_prepare (GObject *source,
79     GAsyncResult *result,
80     gpointer user_data)
81 {
82   EmpathyConnectionManagers *cm_mgr = EMPATHY_CONNECTION_MANAGERS (source);
83   TpAccountManager *account_mgr;
84   CcEmpathyAccountsPage *page;
85
86   account_mgr = TP_ACCOUNT_MANAGER (g_object_get_data (G_OBJECT (cm_mgr),
87       "account-manager"));
88   page = CC_EMPATHY_ACCOUNTS_PAGE (g_object_get_data (G_OBJECT (cm_mgr),
89         "page"));
90
91   if (!empathy_connection_managers_prepare_finish (cm_mgr, result, NULL))
92     goto out;
93
94   page_pack_with_accounts_dialog (page);
95
96   if (empathy_accounts_import (account_mgr, cm_mgr))
97     empathy_account_assistant_show (NULL, cm_mgr);
98
99 out:
100   /* remove ref from active_changed() */
101   g_object_unref (account_mgr);
102   g_object_unref (cm_mgr);
103 }
104
105 static void
106 account_manager_ready_for_accounts_cb (GObject *source_object,
107     GAsyncResult *result,
108     gpointer user_data)
109 {
110   TpAccountManager *account_mgr = TP_ACCOUNT_MANAGER (source_object);
111   CcEmpathyAccountsPage *page = CC_EMPATHY_ACCOUNTS_PAGE (user_data);
112   GError *error = NULL;
113
114   if (!tp_account_manager_prepare_finish (account_mgr, result, &error))
115     {
116       g_warning ("Failed to prepare account manager: %s", error->message);
117       g_error_free (error);
118       return;
119     }
120
121   if (empathy_accounts_has_non_salut_accounts (account_mgr))
122     {
123       page_pack_with_accounts_dialog (page);
124
125       /* remove ref from active_changed() */
126       g_object_unref (account_mgr);
127     }
128   else
129     {
130       EmpathyConnectionManagers *cm_mgr;
131
132       cm_mgr = empathy_connection_managers_dup_singleton ();
133
134       g_object_set_data_full (G_OBJECT (cm_mgr), "account-manager",
135           g_object_ref (account_mgr), (GDestroyNotify) g_object_unref);
136       g_object_set_data_full (G_OBJECT (cm_mgr), "page",
137           g_object_ref (page), (GDestroyNotify) g_object_unref);
138
139       empathy_connection_managers_prepare_async (cm_mgr,
140           connection_managers_prepare, page);
141     }
142 }
143
144 static void
145 active_changed (CcPage *base_page,
146     gboolean is_active)
147 {
148   CcEmpathyAccountsPage *page = CC_EMPATHY_ACCOUNTS_PAGE (base_page);
149   TpAccountManager *account_manager;
150
151   if (is_active)
152     {
153       /* unref'd in final endpoint callbacks */
154       account_manager = tp_account_manager_dup ();
155
156       tp_account_manager_prepare_async (account_manager, NULL,
157           account_manager_ready_for_accounts_cb, page);
158     }
159 }
160
161 static void
162 cc_empathy_accounts_page_finalize (GObject *object)
163 {
164   CcEmpathyAccountsPage *page;
165
166   g_return_if_fail (object != NULL);
167   g_return_if_fail (CC_IS_EMPATHY_ACCOUNTS_PAGE (object));
168
169   page = CC_EMPATHY_ACCOUNTS_PAGE (object);
170
171   g_return_if_fail (page->priv != NULL);
172
173   gtk_widget_destroy (page->priv->accounts_window);
174
175   G_OBJECT_CLASS (cc_empathy_accounts_page_parent_class)->finalize (object);
176 }
177
178 static void
179 cc_empathy_accounts_page_class_init (CcEmpathyAccountsPageClass *klass)
180 {
181   GObjectClass  *object_class = G_OBJECT_CLASS (klass);
182   CcPageClass   *page_class = CC_PAGE_CLASS (klass);
183
184   object_class->finalize = cc_empathy_accounts_page_finalize;
185
186   page_class->active_changed = active_changed;
187
188   g_type_class_add_private (klass, sizeof (CcEmpathyAccountsPagePrivate));
189 }
190
191 static void
192 cc_empathy_accounts_page_init (CcEmpathyAccountsPage *page)
193 {
194   page->priv = CC_EMPATHY_ACCOUNTS_PAGE_GET_PRIVATE (page);
195
196   empathy_gtk_init ();
197 }
198
199 CcPage *
200 cc_empathy_accounts_page_new (void)
201 {
202   GObject *object;
203
204   object = g_object_new (CC_TYPE_EMPATHY_ACCOUNTS_PAGE,
205       "display-name", _("Messaging and VoIP Accounts"),
206       "id", "general",
207       NULL);
208
209   return CC_PAGE (object);
210 }