/* TMut * Copyright (C) 2006-2007 Philip Van Hoof * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with self library; if not, write to the * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #if HAVE_CONFIG_H #include "config.h" #endif #include #include "tmut-account-manager.h" #include "tmut-account-editor.h" #include "tmut-shell-window.h" #include "tmut-shell-child.h" #include "tmut-account-store.h" #include #include static GObjectClass *parent_class = NULL; typedef struct _TMutAccountManagerPriv TMutAccountManagerPriv; struct _TMutAccountManagerPriv { GtkComboBox *accounts_combo; TMutShellWindow *shell; TnyAccount *account; TnyAccountStore *account_store; gint account_created_signal, account_deleted_signal; }; #define TMUT_ACCOUNT_MANAGER_GET_PRIVATE(o) \ (G_TYPE_INSTANCE_GET_PRIVATE ((o), TMUT_TYPE_ACCOUNT_MANAGER, TMutAccountManagerPriv)) typedef struct { TMutAccountManager *self; } OnEditOrNewInfo; static void on_account_editor_destroy (GtkWidget *widget, gpointer user_data) { OnEditOrNewInfo *info = (OnEditOrNewInfo *) user_data; g_object_unref (info->self); g_slice_free (OnEditOrNewInfo, info); return; } static void on_create_new_account (GtkWidget *widget, gpointer user_data) { TMutAccountEditor *editor = TMUT_ACCOUNT_EDITOR (widget); OnEditOrNewInfo *info = (OnEditOrNewInfo *) user_data; TMutAccountManagerPriv *priv = TMUT_ACCOUNT_MANAGER_GET_PRIVATE (info->self); gchar **options = tmut_account_editor_get_options (editor); tmut_account_store_create_account (TMUT_ACCOUNT_STORE (priv->account_store), tmut_account_editor_get_enabled (editor), tmut_account_editor_get_name (editor), tmut_account_editor_get_hostname (editor), tmut_account_editor_get_proto (editor), tmut_account_editor_get_account_type (editor), tmut_account_editor_get_user (editor), NULL, tmut_account_editor_get_from (editor), -1, (const char **) options); g_strfreev (options); } static void on_edit_account (GtkWidget *widget, gpointer user_data) { TMutAccountEditor *editor = TMUT_ACCOUNT_EDITOR (widget); OnEditOrNewInfo *info = (OnEditOrNewInfo *) user_data; TMutAccountManagerPriv *priv = TMUT_ACCOUNT_MANAGER_GET_PRIVATE (info->self); TnyAccount *account = tmut_account_editor_get_account (editor); gchar **options = tmut_account_editor_get_options (editor); tmut_account_store_edit_account (TMUT_ACCOUNT_STORE (priv->account_store), account, tmut_account_editor_get_enabled (editor), tmut_account_editor_get_name (editor), tmut_account_editor_get_hostname (editor), tmut_account_editor_get_proto (editor), tmut_account_editor_get_user (editor), NULL, tmut_account_editor_get_from (editor), -1, (const char **) options); g_strfreev (options); g_object_unref (account); } void tmut_account_manager_on_edit_account_activated (GObject *sender, TMutAccountManager *self) { TMutAccountManagerPriv *priv = TMUT_ACCOUNT_MANAGER_GET_PRIVATE (self); if (priv->account) { TMutAccountEditor *view = tmut_account_editor_new (priv->account); OnEditOrNewInfo *info; info = g_slice_new0 (OnEditOrNewInfo); info->self = TMUT_ACCOUNT_MANAGER (g_object_ref (self)); g_signal_connect (G_OBJECT (view), "ok-clicked", G_CALLBACK (on_edit_account), info); g_signal_connect (G_OBJECT (view), "destroy", G_CALLBACK (on_account_editor_destroy), info); gtk_widget_show (GTK_WIDGET (view)); tmut_shell_window_set_child ( tmut_shell_child_get_window (TMUT_SHELL_CHILD (self)), TMUT_SHELL_CHILD (view), NULL);; } return; } void tmut_account_manager_on_new_account_activated (GObject *sender, TMutAccountManager *self) { TMutAccountManagerPriv *priv = TMUT_ACCOUNT_MANAGER_GET_PRIVATE (self); TMutAccountEditor *view = tmut_account_editor_new (NULL); OnEditOrNewInfo *info; info = g_slice_new0 (OnEditOrNewInfo); info->self = TMUT_ACCOUNT_MANAGER (g_object_ref (self)); g_signal_connect (G_OBJECT (view), "ok-clicked", G_CALLBACK (on_create_new_account), info); g_signal_connect (G_OBJECT (view), "destroy", G_CALLBACK (on_account_editor_destroy), info); gtk_widget_show (GTK_WIDGET (view)); tmut_shell_window_set_child ( tmut_shell_child_get_window (TMUT_SHELL_CHILD (self)), TMUT_SHELL_CHILD (view), NULL); return; } void tmut_account_manager_on_delete_account_activated (GObject *sender, TMutAccountManager *self) { TMutAccountManagerPriv *priv = TMUT_ACCOUNT_MANAGER_GET_PRIVATE (self); if (priv->account) { tmut_account_store_delete_account (TMUT_ACCOUNT_STORE (priv->account_store), priv->account); g_object_unref (priv->account); priv->account = NULL; } return; } static void popup_the_actions_account (GtkButton *button, gpointer user_data) { GtkMenu *menu = (GtkMenu *) user_data; gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL, 0, gtk_get_current_event_time ()); return; } static void tmut_account_manager_create_menu_default (TMutAccountManager *self) { TMutAccountManagerPriv *priv = TMUT_ACCOUNT_MANAGER_GET_PRIVATE (self); GtkMenu *menu; GtkWidget *new_menuitem, *delete_menuitem, *edit_menuitem; GtkButton *menu_button; menu_button = GTK_BUTTON (gtk_button_new_with_label (_("Actions"))); menu = GTK_MENU (gtk_menu_new ()); new_menuitem = gtk_menu_item_new_with_label (_("Create new account")); delete_menuitem = gtk_menu_item_new_with_label (_("Delete selected account")); edit_menuitem = gtk_menu_item_new_with_label (_("Edit selected account")); gtk_widget_show (edit_menuitem); gtk_widget_show (delete_menuitem); gtk_widget_show (new_menuitem); gtk_widget_show (GTK_WIDGET (menu)); gtk_widget_show (GTK_WIDGET (menu_button)); gtk_menu_prepend (menu, edit_menuitem); gtk_menu_prepend (menu, delete_menuitem); gtk_menu_prepend (menu, new_menuitem); gtk_menu_attach_to_widget (menu, GTK_WIDGET (menu_button), NULL); gtk_box_pack_start (GTK_BOX (self), GTK_WIDGET (menu_button), FALSE, TRUE, 0); g_signal_connect (G_OBJECT (menu_button), "clicked", G_CALLBACK (popup_the_actions_account), menu); g_signal_connect (G_OBJECT (edit_menuitem), "activate", G_CALLBACK (tmut_account_manager_on_edit_account_activated), self); g_signal_connect (G_OBJECT (delete_menuitem), "activate", G_CALLBACK (tmut_account_manager_on_delete_account_activated), self); g_signal_connect (G_OBJECT (new_menuitem), "activate", G_CALLBACK (tmut_account_manager_on_new_account_activated), self); return; } void tmut_account_manager_set_active_account (TMutAccountManager *self, TnyAccount *account) { TMutAccountManagerPriv *priv = TMUT_ACCOUNT_MANAGER_GET_PRIVATE (self); priv->account = TNY_ACCOUNT (g_object_ref (account)); return; } static void on_account_changed (GtkComboBox *combobox, gpointer user_data) { TMutAccountManager *self = TMUT_ACCOUNT_MANAGER (user_data); TMutAccountManagerPriv *priv = TMUT_ACCOUNT_MANAGER_GET_PRIVATE (self); GtkTreeIter iter; TnyAccount *account = NULL; GtkTreeModel *model = gtk_combo_box_get_model (combobox); if (gtk_combo_box_get_active_iter (priv->accounts_combo, &iter)) { gtk_tree_model_get (model, &iter, TNY_GTK_ACCOUNT_LIST_MODEL_INSTANCE_COLUMN, &account, -1); if (account) { tmut_account_manager_set_active_account (TMUT_ACCOUNT_MANAGER (user_data), account); g_object_unref (account); } } return; } static void on_account_created (TMutAccountStore *store, TnyAccount *account, TMutAccountManager *self) { TMutAccountManagerPriv *priv = TMUT_ACCOUNT_MANAGER_GET_PRIVATE (self); TnyList *model = TNY_LIST (gtk_combo_box_get_model (priv->accounts_combo)); tny_list_prepend (model, (GObject *) account); } static void on_account_deleted (TMutAccountStore *store, TnyAccount *account, TMutAccountManager *self) { TMutAccountManagerPriv *priv = TMUT_ACCOUNT_MANAGER_GET_PRIVATE (self); gtk_combo_box_set_active (priv->accounts_combo, -1); TnyList *model = TNY_LIST (gtk_combo_box_get_model (priv->accounts_combo)); tny_list_remove (model, (GObject *) account); } static void disconnect_account_store (TMutAccountManagerPriv *priv) { g_signal_handler_disconnect (priv->account_store, priv->account_created_signal); g_signal_handler_disconnect (priv->account_store, priv->account_deleted_signal); g_object_unref (priv->account_store); priv->account_store = NULL; } static void tmut_account_manager_set_account_store (TnyAccountStoreView *self, TnyAccountStore *account_store) { TMutAccountManagerPriv *priv = TMUT_ACCOUNT_MANAGER_GET_PRIVATE (self); TnyList *accounts = TNY_LIST (tny_gtk_account_list_model_new ()); tny_account_store_get_accounts (account_store, accounts, TNY_ACCOUNT_STORE_BOTH); gtk_combo_box_set_model (priv->accounts_combo, GTK_TREE_MODEL (accounts)); gtk_combo_box_set_active (priv->accounts_combo, 0); if (priv->account_store) disconnect_account_store (priv); priv->account_store = TNY_ACCOUNT_STORE (g_object_ref (account_store)); priv->account_created_signal = g_signal_connect (G_OBJECT (priv->account_store), "account_created", G_CALLBACK (on_account_created), self); priv->account_deleted_signal = g_signal_connect (G_OBJECT (priv->account_store), "account_deleted", G_CALLBACK (on_account_deleted), self); TMUT_ACCOUNT_MANAGER_GET_CLASS (self)->create_menu (TMUT_ACCOUNT_MANAGER (self)); return; } static void tmut_account_manager_instance_init (GTypeInstance *instance, gpointer g_class) { TMutAccountManager *self = (TMutAccountManager *) instance; TMutAccountManagerPriv *priv = TMUT_ACCOUNT_MANAGER_GET_PRIVATE (self); GtkVBox *vbox = GTK_VBOX (self); GtkCellRenderer *renderer; GtkLabel *label = GTK_LABEL (gtk_label_new ("Account manager")); priv->account_store = NULL; priv->account_created_signal = -1; priv->account_deleted_signal = -1; priv->account = NULL; gtk_widget_show (GTK_WIDGET (label)); priv->accounts_combo = GTK_COMBO_BOX (gtk_combo_box_new ()); gtk_widget_show (GTK_WIDGET (priv->accounts_combo)); renderer = gtk_cell_renderer_text_new(); gtk_cell_layout_pack_start(GTK_CELL_LAYOUT (priv->accounts_combo), renderer, TRUE); gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT (priv->accounts_combo), renderer, "text", TNY_GTK_ACCOUNT_LIST_MODEL_NAME_COLUMN, NULL); gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (priv->accounts_combo), FALSE, TRUE, 0); gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (label), TRUE, TRUE, 0); g_signal_connect (G_OBJECT (priv->accounts_combo), "changed", G_CALLBACK (on_account_changed), self); return; } static void tmut_account_manager_finalize (GObject *object) { TMutAccountManagerPriv *priv = TMUT_ACCOUNT_MANAGER_GET_PRIVATE (object); if (priv->account) g_object_unref (priv->account); if (priv->account_store) disconnect_account_store (priv); (*parent_class->finalize) (object); return; } TMutShellWindow* tmut_account_manager_get_window (TMutShellChild *self) { TMutAccountManagerPriv *priv = TMUT_ACCOUNT_MANAGER_GET_PRIVATE (self); return priv->shell; } void tmut_account_manager_set_window (TMutShellChild *self, TMutShellWindow *window) { TMutAccountManagerPriv *priv = TMUT_ACCOUNT_MANAGER_GET_PRIVATE (self); priv->shell = window; return; } static void tmut_shell_child_init (gpointer g, gpointer iface_data) { TMutShellChildIface *klass = (TMutShellChildIface *)g; klass->get_window= tmut_account_manager_get_window; klass->set_window= tmut_account_manager_set_window; return; } static void tny_account_store_view_init (gpointer g, gpointer iface_data) { TnyAccountStoreViewIface *klass = (TnyAccountStoreViewIface *)g; klass->set_account_store= tmut_account_manager_set_account_store; return; } static void tmut_account_manager_class_init (TMutAccountManagerClass *class) { GObjectClass *object_class; parent_class = g_type_class_peek_parent (class); object_class = (GObjectClass*) class; object_class->finalize = tmut_account_manager_finalize; class->create_menu= tmut_account_manager_create_menu_default; g_type_class_add_private (object_class, sizeof (TMutAccountManagerPriv)); return; } /** * tmut_account_manager_new: * * * Return value: A new #TnySummaryManager instance implemented for TMUT **/ TMutAccountManager* tmut_account_manager_new (void) { TMutAccountManager *self = g_object_new (TMUT_TYPE_ACCOUNT_MANAGER, NULL); return TMUT_ACCOUNT_MANAGER (self); } GType tmut_account_manager_get_type (void) { static GType type = 0; if (G_UNLIKELY(type == 0)) { static const GTypeInfo info = { sizeof (TMutAccountManagerClass), NULL, /* base_init */ NULL, /* base_finalize */ (GClassInitFunc) tmut_account_manager_class_init, /* class_init */ NULL, /* class_finalize */ NULL, /* class_data */ sizeof (TMutAccountManager), 0, /* n_preallocs */ tmut_account_manager_instance_init /* instance_init */ }; static const GInterfaceInfo tny_account_store_view_info = { (GInterfaceInitFunc) tny_account_store_view_init, /* interface_init */ NULL, /* interface_finalize */ NULL /* interface_data */ }; static const GInterfaceInfo tmut_shell_child_info = { (GInterfaceInitFunc) tmut_shell_child_init, /* interface_init */ NULL, /* interface_finalize */ NULL /* interface_data */ }; type = g_type_register_static (GTK_TYPE_VBOX, "TMutAccountManager", &info, 0); g_type_add_interface_static (type, TNY_TYPE_ACCOUNT_STORE_VIEW, &tny_account_store_view_info); g_type_add_interface_static (type, TMUT_TYPE_SHELL_CHILD, &tmut_shell_child_info); } return type; }