]> git.0d.be Git - empathy.git/blob - src/cc-empathy-accounts-panel.c
Merge branch 'sasl'
[empathy.git] / src / cc-empathy-accounts-panel.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
2  *
3  * Copyright (C) 2010 Collabora, Ltd.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * Authors: Travis Reitter <travis.reitter@collabora.co.uk>
20  */
21
22 #include "config.h"
23
24 #include <stdlib.h>
25 #include <stdio.h>
26
27 #include <gtk/gtk.h>
28 #include <gio/gio.h>
29 #include <glib/gi18n-lib.h>
30
31 #include <telepathy-glib/telepathy-glib.h>
32
33 #include <libempathy/empathy-utils.h>
34 #include <libempathy/empathy-connection-managers.h>
35 #include <libempathy-gtk/empathy-ui-utils.h>
36 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
37 #include <libempathy/empathy-debug.h>
38
39 #include "empathy-accounts-common.h"
40 #include "empathy-account-assistant.h"
41 #include "empathy-accounts-dialog.h"
42
43 #include "cc-empathy-accounts-panel.h"
44
45 #define CC_EMPATHY_ACCOUNTS_PANEL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), CC_TYPE_EMPATHY_ACCOUNTS_PANEL, CcEmpathyAccountsPanelPrivate))
46
47 struct CcEmpathyAccountsPanelPrivate
48 {
49   /* the original window holding the dialog content; it needs to be retained and
50    * destroyed in our finalize(), since it invalidates its children (even if
51    * they've already been reparented by the time it is destroyed) */
52   GtkWidget *accounts_window;
53
54   GtkWidget *assistant;
55 };
56
57 G_DEFINE_DYNAMIC_TYPE (CcEmpathyAccountsPanel, cc_empathy_accounts_panel, CC_TYPE_PANEL)
58
59 static void
60 panel_pack_with_accounts_dialog (CcEmpathyAccountsPanel *panel)
61 {
62   GtkWidget *content;
63   GtkWidget *action_area;
64
65   if (panel->priv->accounts_window != NULL)
66     {
67       gtk_widget_destroy (panel->priv->accounts_window);
68       gtk_container_remove (GTK_CONTAINER (panel),
69           gtk_bin_get_child (GTK_BIN (panel)));
70     }
71
72     panel->priv->accounts_window = empathy_accounts_dialog_show (NULL, NULL);
73     gtk_widget_hide (panel->priv->accounts_window);
74
75     content = gtk_dialog_get_content_area (
76         GTK_DIALOG (panel->priv->accounts_window));
77     action_area = gtk_dialog_get_action_area (
78         GTK_DIALOG (panel->priv->accounts_window));
79     gtk_widget_set_no_show_all (action_area, TRUE);
80     gtk_widget_hide (action_area);
81
82     gtk_widget_reparent (content, GTK_WIDGET (panel));
83 }
84
85 static void
86 account_assistant_closed_cb (GtkWidget *widget,
87     gpointer user_data)
88 {
89   CcEmpathyAccountsPanel *panel = CC_EMPATHY_ACCOUNTS_PANEL (user_data);
90
91   if (empathy_accounts_dialog_is_creating (
92       EMPATHY_ACCOUNTS_DIALOG (panel->priv->accounts_window)))
93     {
94       empathy_account_dialog_cancel (
95         EMPATHY_ACCOUNTS_DIALOG (panel->priv->accounts_window));
96     }
97
98   gtk_widget_set_sensitive (GTK_WIDGET (panel), TRUE);
99   panel->priv->assistant = NULL;
100 }
101
102 static void
103 connection_managers_prepare (GObject *source,
104     GAsyncResult *result,
105     gpointer user_data)
106 {
107   EmpathyConnectionManagers *cm_mgr = EMPATHY_CONNECTION_MANAGERS (source);
108   TpAccountManager *account_mgr;
109   CcEmpathyAccountsPanel *panel = CC_EMPATHY_ACCOUNTS_PANEL (user_data);
110
111   account_mgr = TP_ACCOUNT_MANAGER (g_object_get_data (G_OBJECT (cm_mgr),
112       "account-manager"));
113
114   if (!empathy_connection_managers_prepare_finish (cm_mgr, result, NULL))
115     goto out;
116
117   panel_pack_with_accounts_dialog (panel);
118
119   empathy_accounts_import (account_mgr, cm_mgr);
120
121   if (!empathy_accounts_has_non_salut_accounts (account_mgr))
122     {
123       GtkWindow *parent;
124
125       parent = empathy_get_toplevel_window (GTK_WIDGET (panel));
126       panel->priv->assistant = empathy_account_assistant_show (parent, cm_mgr);
127
128       gtk_widget_set_sensitive (GTK_WIDGET (panel), FALSE);
129
130       tp_g_signal_connect_object (panel->priv->assistant, "hide",
131         G_CALLBACK (account_assistant_closed_cb),
132         panel, 0);
133     }
134
135 out:
136   /* remove ref from active_changed() */
137   g_object_unref (account_mgr);
138   g_object_unref (cm_mgr);
139 }
140
141 static void
142 account_manager_ready_for_accounts_cb (GObject *source_object,
143     GAsyncResult *result,
144     gpointer user_data)
145 {
146   TpAccountManager *account_mgr = TP_ACCOUNT_MANAGER (source_object);
147   CcEmpathyAccountsPanel *panel = CC_EMPATHY_ACCOUNTS_PANEL (user_data);
148   GError *error = NULL;
149
150   if (!tp_account_manager_prepare_finish (account_mgr, result, &error))
151     {
152       g_warning ("Failed to prepare account manager: %s", error->message);
153       g_error_free (error);
154       return;
155     }
156
157   if (empathy_accounts_has_non_salut_accounts (account_mgr))
158     {
159       panel_pack_with_accounts_dialog (panel);
160
161       /* remove ref from active_changed() */
162       g_object_unref (account_mgr);
163     }
164   else
165     {
166       EmpathyConnectionManagers *cm_mgr;
167
168       cm_mgr = empathy_connection_managers_dup_singleton ();
169
170       g_object_set_data_full (G_OBJECT (cm_mgr), "account-manager",
171           g_object_ref (account_mgr), (GDestroyNotify) g_object_unref);
172
173       empathy_connection_managers_prepare_async (cm_mgr,
174           connection_managers_prepare, panel);
175     }
176 }
177
178 static void
179 cc_empathy_accounts_panel_finalize (GObject *object)
180 {
181   CcEmpathyAccountsPanel *panel;
182
183   g_return_if_fail (object != NULL);
184   g_return_if_fail (CC_IS_EMPATHY_ACCOUNTS_PANEL (object));
185
186   panel = CC_EMPATHY_ACCOUNTS_PANEL (object);
187
188   g_return_if_fail (panel->priv != NULL);
189
190   gtk_widget_destroy (panel->priv->accounts_window);
191
192   if (panel->priv->assistant != NULL)
193     gtk_widget_destroy (panel->priv->assistant);
194
195   G_OBJECT_CLASS (cc_empathy_accounts_panel_parent_class)->finalize (object);
196 }
197
198 static void
199 cc_empathy_accounts_panel_class_init (CcEmpathyAccountsPanelClass *klass)
200 {
201   GObjectClass *object_class = G_OBJECT_CLASS (klass);
202
203   object_class->finalize = cc_empathy_accounts_panel_finalize;
204
205   g_type_class_add_private (klass, sizeof (CcEmpathyAccountsPanelPrivate));
206 }
207
208 static void
209 cc_empathy_accounts_panel_class_finalize (CcEmpathyAccountsPanelClass *klass)
210 {
211 }
212
213 static void
214 cc_empathy_accounts_panel_init (CcEmpathyAccountsPanel *panel)
215 {
216   TpAccountManager *account_manager;
217
218   panel->priv = CC_EMPATHY_ACCOUNTS_PANEL_GET_PRIVATE (panel);
219
220   empathy_gtk_init ();
221
222   /* unref'd in final endpoint callbacks */
223   account_manager = tp_account_manager_dup ();
224
225   tp_account_manager_prepare_async (account_manager, NULL,
226       account_manager_ready_for_accounts_cb, panel);
227 }
228
229 void
230 cc_empathy_accounts_panel_register (GIOModule *module)
231 {
232   /* Setup gettext */
233   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
234   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
235
236   cc_empathy_accounts_panel_register_type (G_TYPE_MODULE (module));
237   g_io_extension_point_implement (CC_SHELL_PANEL_EXTENSION_POINT,
238       CC_TYPE_EMPATHY_ACCOUNTS_PANEL, "empathy-accounts", 10);
239 }