]> git.0d.be Git - empathy.git/blob - src/cc-empathy-accounts-panel.c
Add Aragonese translation.
[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 #include <gconf/gconf-client.h>
33
34 #include <libempathy/empathy-utils.h>
35 #include <libempathy/empathy-connection-managers.h>
36 #include <libempathy-gtk/empathy-ui-utils.h>
37 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
38 #include <libempathy/empathy-debug.h>
39
40 #include "empathy-accounts-common.h"
41 #include "empathy-account-assistant.h"
42 #include "empathy-accounts-dialog.h"
43
44 #include "cc-empathy-accounts-panel.h"
45
46 #define CC_EMPATHY_ACCOUNTS_PANEL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), CC_TYPE_EMPATHY_ACCOUNTS_PANEL, CcEmpathyAccountsPanelPrivate))
47
48 struct CcEmpathyAccountsPanelPrivate
49 {
50   /* the original window holding the dialog content; it needs to be retained and
51    * destroyed in our finalize(), since it invalidates its children (even if
52    * they've already been reparented by the time it is destroyed) */
53   GtkWidget *accounts_window;
54
55   GtkWidget *assistant;
56 };
57
58 G_DEFINE_DYNAMIC_TYPE (CcEmpathyAccountsPanel, cc_empathy_accounts_panel, CC_TYPE_PANEL)
59
60 static void
61 panel_pack_with_accounts_dialog (CcEmpathyAccountsPanel *panel)
62 {
63   GtkWidget *content;
64   GtkWidget *action_area;
65
66   if (panel->priv->accounts_window != NULL)
67     {
68       gtk_widget_destroy (panel->priv->accounts_window);
69       gtk_container_remove (GTK_CONTAINER (panel),
70           gtk_bin_get_child (GTK_BIN (panel)));
71     }
72
73     panel->priv->accounts_window = empathy_accounts_dialog_show (NULL, NULL);
74     gtk_widget_hide (panel->priv->accounts_window);
75
76     content = gtk_dialog_get_content_area (
77         GTK_DIALOG (panel->priv->accounts_window));
78     action_area = gtk_dialog_get_action_area (
79         GTK_DIALOG (panel->priv->accounts_window));
80     gtk_widget_set_no_show_all (action_area, TRUE);
81     gtk_widget_hide (action_area);
82
83     gtk_widget_reparent (content, GTK_WIDGET (panel));
84 }
85
86 static void
87 account_assistant_closed_cb (GtkWidget *widget,
88     gpointer user_data)
89 {
90   CcEmpathyAccountsPanel *panel = CC_EMPATHY_ACCOUNTS_PANEL (user_data);
91
92   if (empathy_accounts_dialog_is_creating (
93       EMPATHY_ACCOUNTS_DIALOG (panel->priv->accounts_window)))
94     {
95       empathy_account_dialog_cancel (
96         EMPATHY_ACCOUNTS_DIALOG (panel->priv->accounts_window));
97     }
98
99   gtk_widget_set_sensitive (GTK_WIDGET (panel), TRUE);
100   panel->priv->assistant = NULL;
101 }
102
103 static void
104 connection_managers_prepare (GObject *source,
105     GAsyncResult *result,
106     gpointer user_data)
107 {
108   EmpathyConnectionManagers *cm_mgr = EMPATHY_CONNECTION_MANAGERS (source);
109   TpAccountManager *account_mgr;
110   CcEmpathyAccountsPanel *panel = CC_EMPATHY_ACCOUNTS_PANEL (user_data);
111
112   account_mgr = TP_ACCOUNT_MANAGER (g_object_get_data (G_OBJECT (cm_mgr),
113       "account-manager"));
114
115   if (!empathy_connection_managers_prepare_finish (cm_mgr, result, NULL))
116     goto out;
117
118   panel_pack_with_accounts_dialog (panel);
119
120   empathy_accounts_import (account_mgr, cm_mgr);
121
122   if (!empathy_accounts_has_non_salut_accounts (account_mgr))
123     {
124       GtkWindow *parent;
125
126       parent = empathy_get_toplevel_window (GTK_WIDGET (panel));
127       panel->priv->assistant = empathy_account_assistant_show (parent, cm_mgr);
128
129       gtk_widget_set_sensitive (GTK_WIDGET (panel), FALSE);
130
131       tp_g_signal_connect_object (panel->priv->assistant, "hide",
132         G_CALLBACK (account_assistant_closed_cb),
133         panel, 0);
134     }
135
136 out:
137   /* remove ref from active_changed() */
138   g_object_unref (account_mgr);
139   g_object_unref (cm_mgr);
140 }
141
142 static void
143 account_manager_ready_for_accounts_cb (GObject *source_object,
144     GAsyncResult *result,
145     gpointer user_data)
146 {
147   TpAccountManager *account_mgr = TP_ACCOUNT_MANAGER (source_object);
148   CcEmpathyAccountsPanel *panel = CC_EMPATHY_ACCOUNTS_PANEL (user_data);
149   GError *error = NULL;
150
151   if (!tp_account_manager_prepare_finish (account_mgr, result, &error))
152     {
153       g_warning ("Failed to prepare account manager: %s", error->message);
154       g_error_free (error);
155       return;
156     }
157
158   if (empathy_accounts_has_non_salut_accounts (account_mgr))
159     {
160       panel_pack_with_accounts_dialog (panel);
161
162       /* remove ref from active_changed() */
163       g_object_unref (account_mgr);
164     }
165   else
166     {
167       EmpathyConnectionManagers *cm_mgr;
168
169       cm_mgr = empathy_connection_managers_dup_singleton ();
170
171       g_object_set_data_full (G_OBJECT (cm_mgr), "account-manager",
172           g_object_ref (account_mgr), (GDestroyNotify) g_object_unref);
173
174       empathy_connection_managers_prepare_async (cm_mgr,
175           connection_managers_prepare, panel);
176     }
177 }
178
179 static void
180 cc_empathy_accounts_panel_finalize (GObject *object)
181 {
182   CcEmpathyAccountsPanel *panel;
183
184   g_return_if_fail (object != NULL);
185   g_return_if_fail (CC_IS_EMPATHY_ACCOUNTS_PANEL (object));
186
187   panel = CC_EMPATHY_ACCOUNTS_PANEL (object);
188
189   g_return_if_fail (panel->priv != NULL);
190
191   gtk_widget_destroy (panel->priv->accounts_window);
192
193   if (panel->priv->assistant != NULL)
194     gtk_widget_destroy (panel->priv->assistant);
195
196   G_OBJECT_CLASS (cc_empathy_accounts_panel_parent_class)->finalize (object);
197 }
198
199 static void
200 cc_empathy_accounts_panel_class_init (CcEmpathyAccountsPanelClass *klass)
201 {
202   GObjectClass *object_class = G_OBJECT_CLASS (klass);
203
204   object_class->finalize = cc_empathy_accounts_panel_finalize;
205
206   g_type_class_add_private (klass, sizeof (CcEmpathyAccountsPanelPrivate));
207 }
208
209 static void
210 cc_empathy_accounts_panel_class_finalize (CcEmpathyAccountsPanelClass *klass)
211 {
212 }
213
214 static void
215 cc_empathy_accounts_panel_init (CcEmpathyAccountsPanel *panel)
216 {
217   GConfClient *client;
218   TpAccountManager *account_manager;
219
220   panel->priv = CC_EMPATHY_ACCOUNTS_PANEL_GET_PRIVATE (panel);
221
222   empathy_gtk_init ();
223
224   client = gconf_client_get_default ();
225   gconf_client_add_dir (client, "/desktop/gnome/peripherals/empathy_accounts",
226       GCONF_CLIENT_PRELOAD_ONELEVEL, NULL);
227   gconf_client_add_dir (client, "/desktop/gnome/interface",
228       GCONF_CLIENT_PRELOAD_ONELEVEL, NULL);
229   g_object_unref (client);
230
231   /* unref'd in final endpoint callbacks */
232   account_manager = tp_account_manager_dup ();
233
234   tp_account_manager_prepare_async (account_manager, NULL,
235       account_manager_ready_for_accounts_cb, panel);
236 }
237
238 void
239 cc_empathy_accounts_panel_register (GIOModule *module)
240 {
241   /* Setup gettext */
242   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
243   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
244
245   cc_empathy_accounts_panel_register_type (G_TYPE_MODULE (module));
246   g_io_extension_point_implement (CC_SHELL_PANEL_EXTENSION_POINT,
247       CC_TYPE_EMPATHY_ACCOUNTS_PANEL, "empathy-accounts", 10);
248 }