/* 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-editor.h" #include "tmut-shell-window.h" #include "tmut-shell-child.h" #include #include #include #include #include static GObjectClass *parent_class = NULL; typedef struct _TMutAccountEditorPriv TMutAccountEditorPriv; typedef enum { TMUT_ACCOUNT_EDITOR_OK_CLICKED, TMUT_ACCOUNT_EDITOR_LAST_SIGNAL } TMutAccountEditorSignal; static guint tmut_account_editor_signals [TMUT_ACCOUNT_EDITOR_LAST_SIGNAL]; struct _TMutAccountEditorPriv { TnyAccount *account; TMutShellWindow *shell; GtkWidget *name_entry, *hostname_entry, *enabled_checkbutton, *proto_entry, *type_entry, *user_entry, *options_entry, *from_entry; }; #define TMUT_ACCOUNT_EDITOR_GET_PRIVATE(o) \ (G_TYPE_INSTANCE_GET_PRIVATE ((o), TMUT_TYPE_ACCOUNT_EDITOR, TMutAccountEditorPriv)) gboolean tmut_account_editor_get_enabled (TMutAccountEditor *self) { return TRUE; } const gchar* tmut_account_editor_get_name (TMutAccountEditor *self) { TMutAccountEditorPriv *priv = TMUT_ACCOUNT_EDITOR_GET_PRIVATE (self); return gtk_entry_get_text (GTK_ENTRY (priv->name_entry)); } const gchar* tmut_account_editor_get_hostname (TMutAccountEditor *self) { TMutAccountEditorPriv *priv = TMUT_ACCOUNT_EDITOR_GET_PRIVATE (self); return gtk_entry_get_text (GTK_ENTRY (priv->hostname_entry)); } const gchar* tmut_account_editor_get_from (TMutAccountEditor *self) { TMutAccountEditorPriv *priv = TMUT_ACCOUNT_EDITOR_GET_PRIVATE (self); return gtk_entry_get_text (GTK_ENTRY (priv->from_entry)); } const gchar* tmut_account_editor_get_proto (TMutAccountEditor *self) { TMutAccountEditorPriv *priv = TMUT_ACCOUNT_EDITOR_GET_PRIVATE (self); return gtk_entry_get_text (GTK_ENTRY (priv->proto_entry)); } const gchar* tmut_account_editor_get_account_type (TMutAccountEditor *self) { TMutAccountEditorPriv *priv = TMUT_ACCOUNT_EDITOR_GET_PRIVATE (self); return gtk_entry_get_text (GTK_ENTRY (priv->type_entry)); } const gchar* tmut_account_editor_get_user (TMutAccountEditor *self) { TMutAccountEditorPriv *priv = TMUT_ACCOUNT_EDITOR_GET_PRIVATE (self); return gtk_entry_get_text (GTK_ENTRY (priv->user_entry)); } gchar** tmut_account_editor_get_options (TMutAccountEditor *self) { TMutAccountEditorPriv *priv = TMUT_ACCOUNT_EDITOR_GET_PRIVATE (self); const gchar *str = gtk_entry_get_text (GTK_ENTRY (priv->options_entry)); return g_strsplit (str?str:"", ",", -1); } TnyAccount * tmut_account_editor_get_account (TMutAccountEditor *self) { TMutAccountEditorPriv *priv = TMUT_ACCOUNT_EDITOR_GET_PRIVATE (self); if (priv->account) g_object_ref (priv->account); return priv->account; } static void load_account (TMutAccountEditor *self, TnyAccount *account) { TMutAccountEditorPriv *priv = TMUT_ACCOUNT_EDITOR_GET_PRIVATE (self); if (account) { TnyList *options = tny_simple_list_new (); gboolean first = TRUE; TnyIterator *iter; GString *options_string = g_string_new (""); gtk_entry_set_text (GTK_ENTRY (priv->name_entry), tny_account_get_name (account)?tny_account_get_name (account):""); gtk_entry_set_text (GTK_ENTRY (priv->proto_entry), tny_account_get_proto (account)?tny_account_get_proto (account):""); gtk_entry_set_text (GTK_ENTRY (priv->hostname_entry), tny_account_get_hostname (account)?tny_account_get_hostname (account):""); gtk_widget_set_sensitive (GTK_WIDGET (priv->type_entry), FALSE); if (tny_account_get_proto (account) && strcmp (tny_account_get_proto (account), "smtp") == 0) { gtk_entry_set_text (GTK_ENTRY (priv->type_entry), "transport"); gtk_widget_show (GTK_WIDGET (priv->from_entry)); } else { gtk_entry_set_text (GTK_ENTRY (priv->type_entry), "store"); gtk_widget_hide (GTK_WIDGET (priv->from_entry)); } gtk_entry_set_text (GTK_ENTRY (priv->user_entry), tny_account_get_user (account)?tny_account_get_user (account):""); if (TNY_IS_CAMEL_TRANSPORT_ACCOUNT (account)) { TnyCamelTransportAccount *ta = (TnyCamelTransportAccount *) account; gtk_entry_set_text (GTK_ENTRY (priv->from_entry), tny_camel_transport_account_get_from (ta)?tny_camel_transport_account_get_from (ta):""); } tny_camel_account_get_options (TNY_CAMEL_ACCOUNT (account), options); iter = tny_list_create_iterator (options); while (!tny_iterator_is_done (iter)) { TnyPair *pair = TNY_PAIR (tny_iterator_get_current (iter)); const gchar *value = tny_pair_get_value (pair); if (!first) g_string_append_c (options_string, ','); g_string_append (options_string, tny_pair_get_name (pair)); if (value) { g_string_append_c (options_string, '='); g_string_append (options_string, value); } first = FALSE; tny_iterator_next (iter); } g_object_unref (iter); g_object_unref (options); gtk_entry_set_text (GTK_ENTRY (priv->options_entry), options_string->str); g_string_free (options_string, TRUE); } else { gtk_widget_set_sensitive (GTK_WIDGET (priv->type_entry), TRUE); gtk_widget_show (GTK_WIDGET (priv->from_entry)); gtk_entry_set_text (GTK_ENTRY (priv->name_entry), _("Account name")); gtk_entry_set_text (GTK_ENTRY (priv->proto_entry), "imap"); gtk_entry_set_text (GTK_ENTRY (priv->hostname_entry), _("imap.gmail.com")); gtk_entry_set_text (GTK_ENTRY (priv->type_entry), "store"); gtk_entry_set_text (GTK_ENTRY (priv->user_entry), _("user.name")); gtk_entry_set_text (GTK_ENTRY (priv->options_entry), _("use_ssl=wrapped")); } } static void tmut_account_editor_on_ok_clicked (GtkWidget *button, gpointer user_data) { TMutAccountEditor *self = (TMutAccountEditor *) user_data; g_signal_emit (G_OBJECT (self), tmut_account_editor_signals [TMUT_ACCOUNT_EDITOR_OK_CLICKED], 0); tmut_shell_window_back (tmut_shell_child_get_window (TMUT_SHELL_CHILD (user_data))); return; } static void tmut_account_editor_on_cancel_clicked (GtkWidget *button, gpointer user_data) { tmut_shell_window_back (tmut_shell_child_get_window (TMUT_SHELL_CHILD (user_data))); return; } static void tmut_account_editor_instance_init (GTypeInstance *instance, gpointer g_class) { TMutAccountEditor *self = (TMutAccountEditor *) instance; TMutAccountEditorPriv *priv = TMUT_ACCOUNT_EDITOR_GET_PRIVATE (self); GtkWidget *label, *table; GtkWidget *ok_button, *cancel_button; GtkWidget *hbox = gtk_hbox_new (FALSE, 0); GtkWidget *mhbox = gtk_hbox_new (FALSE, 0); table = gtk_table_new (6, 2, FALSE); gtk_widget_show (hbox); ok_button = gtk_button_new_with_label (_("Ok")); cancel_button = gtk_button_new_with_label (_("Cancel")); gtk_widget_show (cancel_button); gtk_widget_show (ok_button); gtk_widget_show (mhbox); gtk_box_pack_start (GTK_BOX (hbox), GTK_WIDGET (cancel_button), TRUE, TRUE, 0); gtk_box_pack_start (GTK_BOX (hbox), GTK_WIDGET (ok_button), TRUE, TRUE, 0); label = gtk_label_new (_("Account name")); gtk_widget_show (label); gtk_table_attach (GTK_TABLE (table), label, 0, 1, 0, 1, (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) (0), 0, 0); gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5); label = gtk_label_new (_("Protocol")); gtk_widget_show (label); gtk_table_attach (GTK_TABLE (table), label, 0, 1, 1, 2, (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) (0), 0, 0); gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5); label = gtk_label_new (_("Hostname")); gtk_widget_show (label); gtk_table_attach (GTK_TABLE (table), label, 0, 1, 2, 3, (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) (0), 0, 0); gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5); label = gtk_label_new (_("User")); gtk_widget_show (label); gtk_table_attach (GTK_TABLE (table), label, 0, 1, 3, 4, (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) (0), 0, 0); gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5); label = gtk_label_new (_("Type (+ From)")); gtk_widget_show (label); gtk_table_attach (GTK_TABLE (table), label, 0, 1, 4, 5, (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) (0), 0, 0); gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5); label = gtk_label_new (_("Options")); gtk_widget_show (label); gtk_table_attach (GTK_TABLE (table), label, 0, 1, 5, 6, (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) (0), 0, 0); gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5); priv->name_entry = gtk_entry_new (); gtk_widget_show (priv->name_entry); gtk_table_attach (GTK_TABLE (table), priv->name_entry, 1, 2, 0, 1, (GtkAttachOptions) (GTK_EXPAND | GTK_FILL), (GtkAttachOptions) (0), 0, 0); priv->proto_entry = gtk_entry_new (); gtk_widget_show (priv->proto_entry); gtk_table_attach (GTK_TABLE (table), priv->proto_entry, 1, 2, 1, 2, (GtkAttachOptions) (GTK_EXPAND | GTK_FILL), (GtkAttachOptions) (0), 0, 0); priv->hostname_entry = gtk_entry_new (); gtk_widget_show (priv->hostname_entry); gtk_table_attach (GTK_TABLE (table), priv->hostname_entry, 1, 2, 2, 3, (GtkAttachOptions) (GTK_EXPAND | GTK_FILL), (GtkAttachOptions) (0), 0, 0); priv->user_entry = gtk_entry_new (); gtk_widget_show (priv->user_entry); gtk_table_attach (GTK_TABLE (table), priv->user_entry, 1, 2, 3, 4, (GtkAttachOptions) (GTK_EXPAND | GTK_FILL), (GtkAttachOptions) (0), 0, 0); priv->type_entry = gtk_entry_new (); gtk_widget_show (priv->type_entry); priv->from_entry = gtk_entry_new (); gtk_widget_show (priv->from_entry); gtk_box_pack_start (GTK_BOX (mhbox), GTK_WIDGET (priv->type_entry), TRUE, TRUE, 0); gtk_box_pack_start (GTK_BOX (mhbox), GTK_WIDGET (priv->from_entry), TRUE, TRUE, 0); gtk_table_attach (GTK_TABLE (table), mhbox, 1, 2, 4, 5, (GtkAttachOptions) (GTK_EXPAND | GTK_FILL), (GtkAttachOptions) (0), 0, 0); priv->options_entry = gtk_entry_new (); gtk_widget_show (priv->options_entry); gtk_table_attach (GTK_TABLE (table), priv->options_entry, 1, 2, 5, 6, (GtkAttachOptions) (GTK_EXPAND | GTK_FILL), (GtkAttachOptions) (0), 0, 0); gtk_widget_show (table); g_signal_connect (G_OBJECT (ok_button), "clicked", G_CALLBACK (tmut_account_editor_on_ok_clicked), self); g_signal_connect (G_OBJECT (cancel_button), "clicked", G_CALLBACK (tmut_account_editor_on_cancel_clicked), self); gtk_box_pack_start (GTK_BOX (self), GTK_WIDGET (table), TRUE, TRUE, 0); gtk_box_pack_start (GTK_BOX (self), GTK_WIDGET (hbox), TRUE, TRUE, 0); return; } static void tmut_account_editor_finalize (GObject *object) { TMutAccountEditorPriv *priv = TMUT_ACCOUNT_EDITOR_GET_PRIVATE (object); if (priv->account) g_object_unref (priv->account); (*parent_class->finalize) (object); return; } static void tmut_account_editor_class_init (TMutAccountEditorClass *class) { GObjectClass *object_class; parent_class = g_type_class_peek_parent (class); object_class = (GObjectClass*) class; object_class->finalize = tmut_account_editor_finalize; g_type_class_add_private (object_class, sizeof (TMutAccountEditorPriv)); return; } /** * tmut_account_editor_new: * @account: (null-ok): a #TnyAccount * * Return value: A new #TMutAccountEditor instance **/ TMutAccountEditor* tmut_account_editor_new (TnyAccount *account) { TMutAccountEditor *self = g_object_new (TMUT_TYPE_ACCOUNT_EDITOR, NULL); TMutAccountEditorPriv *priv = TMUT_ACCOUNT_EDITOR_GET_PRIVATE (self); if (account) priv->account = TNY_ACCOUNT (g_object_ref (account)); else priv->account = NULL; load_account (self, priv->account); return TMUT_ACCOUNT_EDITOR (self); } TMutShellWindow* tmut_account_editor_get_window (TMutShellChild *self) { TMutAccountEditorPriv *priv = TMUT_ACCOUNT_EDITOR_GET_PRIVATE (self); return priv->shell; } void tmut_account_editor_set_window (TMutShellChild *self, TMutShellWindow *window) { TMutAccountEditorPriv *priv = TMUT_ACCOUNT_EDITOR_GET_PRIVATE (self); priv->shell = window; } static void tmut_shell_child_init (gpointer g, gpointer iface_data) { TMutShellChildIface *klass = (TMutShellChildIface *)g; klass->get_window= tmut_account_editor_get_window; klass->set_window= tmut_account_editor_set_window; return; } static void tmut_account_editor_base_init (gpointer g_class) { static gboolean tmut_account_editor_initialized = FALSE; if (!tmut_account_editor_initialized) { tmut_account_editor_signals[TMUT_ACCOUNT_EDITOR_OK_CLICKED] = g_signal_new ("ok_clicked", TMUT_TYPE_ACCOUNT_EDITOR, G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (TMutAccountEditorClass, ok_clicked), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0); tmut_account_editor_initialized = TRUE; } } GType tmut_account_editor_get_type (void) { static GType type = 0; if (G_UNLIKELY(type == 0)) { static const GTypeInfo info = { sizeof (TMutAccountEditorClass), tmut_account_editor_base_init, /* base_init */ NULL, /* base_finalize */ (GClassInitFunc) tmut_account_editor_class_init, /* class_init */ NULL, /* class_finalize */ NULL, /* class_data */ sizeof (TMutAccountEditor), 0, /* n_preallocs */ tmut_account_editor_instance_init /* instance_init */ }; 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, "TMutAccountEditor", &info, 0); g_type_add_interface_static (type, TMUT_TYPE_SHELL_CHILD, &tmut_shell_child_info); } return type; }