[balsa/gtk3] Implement a GtkComboBox for selecting an identity



commit 301b1298d44ba3d238cb9c83f6c2ac662d39cda1
Author: Peter Bloomfield <PeterBloomfield bellsouth net>
Date:   Thu Jul 28 15:19:26 2016 -0400

    Implement a GtkComboBox for selecting an identity
    
        Implement a GtkComboBox for selecting a LibBalsaIdentity, and
        use it for the mailbox properties dialog and as the From address
        in the compose window.
    
        * libbalsa/identity.c (libbalsa_identity_combo_box): implement
          the widget;
        * libbalsa/identity.h: export it;
        * src/mailbox-conf.c (check_for_blank_fields): allow NULL
          argument;
          (mailbox_conf_view_new_full): use the widget;
          (mailbox_conf_view_check): get the identity from the widget
          instead of the list;
        * src/sendmsg-window.c (create_from_entry): use the widget.

 ChangeLog            |   16 +++++++++++
 libbalsa/identity.c  |   61 +++++++++++++++++++++++++++++++++++++++++
 libbalsa/identity.h  |    4 +++
 src/mailbox-conf.c   |   73 ++++++++++++++-----------------------------------
 src/sendmsg-window.c |   49 ++-------------------------------
 5 files changed, 105 insertions(+), 98 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 1ab89ad..80a3e62 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+2016-07-28  Peter Bloomfield  <pbloomfield bellsouth net>
+
+       Implement a GtkComboBox for selecting a LibBalsaIdentity, and
+       use it for the mailbox properties dialog and as the From address
+       in the compose window.
+
+       * libbalsa/identity.c (libbalsa_identity_combo_box): implement
+         the widget;
+       * libbalsa/identity.h: export it;
+       * src/mailbox-conf.c (check_for_blank_fields): allow NULL
+         argument;
+         (mailbox_conf_view_new_full): use the widget;
+         (mailbox_conf_view_check): get the identity from the widget
+         instead of the list;
+       * src/sendmsg-window.c (create_from_entry): use the widget.
+
 2016-07-28  Albrecht Dreß
 
        Fix a bug in the configure script discovered by Jack
diff --git a/libbalsa/identity.c b/libbalsa/identity.c
index b022247..676212a 100644
--- a/libbalsa/identity.c
+++ b/libbalsa/identity.c
@@ -2216,3 +2216,64 @@ ident_dialog_get_value(GObject * dialog, const gchar * key)
 
     return g_ptr_array_index(values, value);
 }
+
+GtkWidget *
+libbalsa_identity_combo_box(GList       * identities,
+                            const gchar * active_name,
+                            GCallback     changed_cb,
+                            gpointer      changed_data)
+{
+    GList *list;
+    GtkListStore *store;
+    GtkWidget *combo_box;
+    GtkCellLayout *layout;
+    GtkCellRenderer *renderer;
+
+    /* For each identity, store the address, the identity name, and a
+     * ref to the identity in a combo-box.
+     * Note: we can't depend on identities staying in the same
+     * order while the combo-box is open, so we need a ref to the
+     * actual identity. */
+    store = gtk_list_store_new(3,
+                               G_TYPE_STRING,
+                               G_TYPE_STRING,
+                               G_TYPE_OBJECT);
+    combo_box = gtk_combo_box_new_with_model(GTK_TREE_MODEL(store));
+
+    for (list = identities; list != NULL; list = list->next) {
+        LibBalsaIdentity *ident;
+        gchar *from;
+        gchar *name;
+        GtkTreeIter iter;
+
+        ident = list->data;
+        from = internet_address_to_string(ident->ia, FALSE);
+       name = g_strconcat("(", ident->identity_name, ")", NULL);
+
+        gtk_list_store_append(store, &iter);
+        gtk_list_store_set(store, &iter,
+                           0, from,
+                           1, name,
+                           2, ident,
+                           -1);
+
+        if (active_name && strcmp(active_name, name) == 0)
+            gtk_combo_box_set_active_iter(GTK_COMBO_BOX(combo_box), &iter);
+
+        g_free(from);
+        g_free(name);
+    }
+    g_object_unref(store);
+
+    layout = GTK_CELL_LAYOUT(combo_box);
+    renderer = gtk_cell_renderer_text_new();
+    gtk_cell_layout_pack_start(layout, renderer, TRUE);
+    gtk_cell_layout_set_attributes(layout, renderer, "text", 0, NULL);
+    renderer = gtk_cell_renderer_text_new();
+    gtk_cell_layout_pack_start(layout, renderer, FALSE);
+    gtk_cell_layout_set_attributes(layout, renderer, "text", 1, NULL);
+
+    g_signal_connect(combo_box, "changed", changed_cb, changed_data);
+
+    return combo_box;
+}
diff --git a/libbalsa/identity.h b/libbalsa/identity.h
index 51d4712..e15e239 100644
--- a/libbalsa/identity.h
+++ b/libbalsa/identity.h
@@ -144,6 +144,10 @@ G_BEGIN_DECLS
                                          LibBalsaIdentity * initial_id,
                                          LibBalsaIdentityCallback update,
                                          gpointer data);
+    GtkWidget * libbalsa_identity_combo_box(GList       * identities,
+                                            const gchar * active_name,
+                                            GCallback     changed_cb,
+                                            gpointer      changed_data);
 
     LibBalsaIdentity* libbalsa_identity_new_config(const gchar* name);
     void libbalsa_identity_save(LibBalsaIdentity* id, const gchar* prefix);
diff --git a/src/mailbox-conf.c b/src/mailbox-conf.c
index b0ad2a7..98e4ea5 100644
--- a/src/mailbox-conf.c
+++ b/src/mailbox-conf.c
@@ -730,7 +730,12 @@ mailbox_conf_set_values(MailboxConfWindow *mcw)
 static void
 check_for_blank_fields(GtkWidget *widget, MailboxConfWindow *mcw)
 {
-    gboolean sensitive = TRUE;
+    gboolean sensitive;
+
+    if (mcw == NULL || mcw->window == NULL)
+        return;
+
+    sensitive = TRUE;
 
     if (mcw->mailbox_name &&!*gtk_entry_get_text(GTK_ENTRY(mcw->mailbox_name)))
        sensitive = FALSE;
@@ -1404,11 +1409,7 @@ mailbox_conf_view_new_full(LibBalsaMailbox * mailbox,
     GtkWidget *label;
     BalsaMailboxConfView *view_info;
     GtkWidget *widget;
-    GList *list;
     const gchar *identity_name;
-    gint active;
-    GtkListStore *list_store;
-    GtkCellRenderer *cell;
 
     view_info = g_new(BalsaMailboxConfView, 1);
     g_object_weak_ref(G_OBJECT(window), (GWeakNotify) g_free, view_info);
@@ -1418,54 +1419,18 @@ mailbox_conf_view_new_full(LibBalsaMailbox * mailbox,
     if (size_group)
         gtk_size_group_add_widget(size_group, label);
 
-    list_store = gtk_list_store_new(IDENTITY_COMBO_BOX_N_COLUMNS,
-                                    G_TYPE_STRING, G_TYPE_STRING);
+    identity_name = libbalsa_mailbox_get_identity_name(mailbox);
     view_info->identity_combo_box = widget =
-        gtk_combo_box_new_with_model(GTK_TREE_MODEL(list_store));
-
-    cell = gtk_cell_renderer_text_new();
-    gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(widget), cell, TRUE);
-    gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(widget), cell,
-                                   "text", IDENTITY_COMBO_BOX_IDENTITY_NAME_COLUMN,
-                                   NULL);
-    cell = gtk_cell_renderer_text_new();
-    gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(widget), cell, TRUE);
-    gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(widget), cell,
-                                   "text", IDENTITY_COMBO_BOX_ADDRESS_COLUMN,
-                                   NULL);
+        libbalsa_identity_combo_box(balsa_app.identities, identity_name,
+                                    G_CALLBACK(check_for_blank_fields), mcw);
 
     gtk_label_set_mnemonic_widget(GTK_LABEL(label), widget);
-    identity_name = libbalsa_mailbox_get_identity_name(mailbox);
-
-    for (list = balsa_app.identities, active = 0; list;
-         list = list->next, ++active) {
-        LibBalsaIdentity *ident = list->data;
-        gchar *address;
-        GtkTreeIter iter;
-
-        address = internet_address_to_string(ident->ia, FALSE);
-        gtk_list_store_append(list_store, &iter);
-        gtk_list_store_set(list_store, &iter,
-                           IDENTITY_COMBO_BOX_IDENTITY_NAME_COLUMN,
-                           ident->identity_name,
-                           IDENTITY_COMBO_BOX_ADDRESS_COLUMN, address,
-                           -1);
-        g_free(address);
-
-        if (identity_name
-            && strcmp(identity_name, ident->identity_name) == 0)
-            gtk_combo_box_set_active(GTK_COMBO_BOX(widget), active);
-    }
 
     gtk_widget_set_hexpand(widget, TRUE);
     gtk_grid_attach(GTK_GRID(grid), widget, 1, row, 1, 1);
 
-    if (mcw)
-        g_signal_connect(view_info->identity_combo_box, "changed",
-                         G_CALLBACK(check_for_blank_fields), mcw);
     if (callback)
-        g_signal_connect_swapped(view_info->identity_combo_box, "changed",
-                                 callback, window);
+        g_signal_connect_swapped(widget, "changed", callback, window);
 
 #ifdef HAVE_GPGME
     {
@@ -1567,6 +1532,8 @@ mailbox_conf_view_check(BalsaMailboxConfView * view_info,
                        LibBalsaMailbox * mailbox)
 {
     gboolean changed;
+    GtkComboBox *combo_box;
+    GtkTreeIter iter;
     gint active;
 
     g_return_if_fail(LIBBALSA_IS_MAILBOX(mailbox));
@@ -1575,7 +1542,7 @@ mailbox_conf_view_check(BalsaMailboxConfView * view_info,
 
     changed = FALSE;
 
-    g_warn_if_fail(mailbox->view == NULL);
+    libbalsa_mailbox_view_free(mailbox->view);
     g_print("%s set view on %s\n", __func__, mailbox->name);
     mailbox->view = config_load_mailbox_view(mailbox->url);
     if (!mailbox->view) {
@@ -1583,13 +1550,15 @@ mailbox_conf_view_check(BalsaMailboxConfView * view_info,
        mailbox->view = libbalsa_mailbox_view_new();
     }
 
-    active =
-        gtk_combo_box_get_active(GTK_COMBO_BOX
-                                 (view_info->identity_combo_box));
-    if (active >= 0) {
-        LibBalsaIdentity *ident =
-            g_list_nth_data(balsa_app.identities, active);
+    combo_box = GTK_COMBO_BOX(view_info->identity_combo_box);
+    if (gtk_combo_box_get_active_iter(combo_box, &iter)) {
+        GtkTreeModel *model;
+        LibBalsaIdentity *ident;
+
+        model = gtk_combo_box_get_model(combo_box);
+        gtk_tree_model_get(model, &iter, 2, &ident, -1);
         libbalsa_mailbox_set_identity_name(mailbox, ident->identity_name);
+        g_object_unref(ident);
         changed = TRUE;
     }
 
diff --git a/src/sendmsg-window.c b/src/sendmsg-window.c
index ac4815b..a58df68 100644
--- a/src/sendmsg-window.c
+++ b/src/sendmsg-window.c
@@ -2428,52 +2428,9 @@ sw_combo_box_changed(GtkComboBox * combo_box, BalsaSendmsg * bsmsg)
 static void
 create_from_entry(GtkWidget * grid, BalsaSendmsg * bsmsg)
 {
-    GList *list;
-    GtkListStore *store;
-    GtkCellRenderer *renderer;
-
-    /* For each identity, store the address, the identity name, and a
-     * ref to the identity in a combo-box.
-     * Note: we can't depend on balsa_app.identities staying in the same
-     * order while the compose window is open, so we need a ref to the
-     * actual identity. */
-    store = gtk_list_store_new(3,
-                               G_TYPE_STRING,
-                               G_TYPE_STRING,
-                               G_TYPE_OBJECT);
-    for (list = balsa_app.identities; list; list = list->next) {
-        LibBalsaIdentity *ident;
-        gchar *from, *name;
-        GtkTreeIter iter;
-
-        ident = list->data;
-        from = internet_address_to_string(ident->ia, FALSE);
-       name = g_strconcat("(", ident->identity_name, ")", NULL);
-
-        gtk_list_store_append(store, &iter);
-        gtk_list_store_set(store, &iter,
-                           0, from,
-                           1, name,
-                           2, ident,
-                           -1);
-
-        g_free(from);
-        g_free(name);
-    }
-    bsmsg->from[1] = gtk_combo_box_new_with_model(GTK_TREE_MODEL(store));
-    renderer = gtk_cell_renderer_text_new();
-    gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(bsmsg->from[1]), renderer,
-                               TRUE);
-    gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(bsmsg->from[1]),
-                                   renderer, "text", 0, NULL);
-    renderer = gtk_cell_renderer_text_new();
-    gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(bsmsg->from[1]), renderer,
-                              FALSE);
-    gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(bsmsg->from[1]),
-                                   renderer, "text", 1, NULL);
-    g_object_unref(store);
-    g_signal_connect(bsmsg->from[1], "changed",
-                     G_CALLBACK(sw_combo_box_changed), bsmsg);
+    bsmsg->from[1] =
+        libbalsa_identity_combo_box(balsa_app.identities, NULL,
+                                    G_CALLBACK(sw_combo_box_changed), bsmsg);
     create_email_or_string_entry(bsmsg, grid, _("F_rom:"), 0, bsmsg->from);
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]