Add gtkut_tree_view_get_selected_pointer() helper function.
authorAndrej Kacian <ticho@claws-mail.org>
Sun, 25 Feb 2018 19:40:38 +0000 (20:40 +0100)
committerAndrej Kacian <ticho@claws-mail.org>
Tue, 27 Feb 2018 18:45:57 +0000 (19:45 +0100)
It's meant for the simple cases when you just want to get
a pointer out of a treeview's selected row and you're not
going to do anything with the view, nor the model.

src/account.c
src/addressadd.c
src/gtk/gtkutils.c
src/gtk/gtkutils.h
src/importldif.c
src/mimeview.c
src/plugins/pgpcore/select-keys.c
src/ssl_manager.c

index 87f8953..52e88f5 100644 (file)
@@ -1630,19 +1630,12 @@ static void account_create_list_view_columns(GtkWidget *list_view)
  */
 static gint account_list_view_get_selected_account_id(GtkWidget *list_view)
 {
-       GtkTreeSelection *selector;
-       GtkTreeModel *model;
-       GtkTreeIter iter;
        PrefsAccount *res = NULL;
 
-       selector = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
-       
-       if (!gtk_tree_selection_get_selected(selector, &model, &iter))
-               return -1;
-
-       gtk_tree_model_get(model, &iter, ACCOUNT_DATA, &res, -1);
+       res = (PrefsAccount *)gtkut_tree_view_get_selected_pointer(
+                       GTK_TREE_VIEW(list_view), ACCOUNT_DATA);
 
-       return res->account_id;                    
+       return (res != NULL ? res->account_id : -1);
 }
 
 /*!
@@ -1668,19 +1661,11 @@ static GtkTreePath *account_list_view_get_selected_account_path(GtkWidget *list_
  */
 static PrefsAccount *account_list_view_get_selected_account(GtkWidget *list_view)
 {
-       GtkTreeSelection *selector;
-       GtkTreeModel *model;
-       GtkTreeIter iter;
-       PrefsAccount *res = NULL;
-
-       selector = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
-       
-       if (!gtk_tree_selection_get_selected(selector, &model, &iter))
-               return NULL;
-
-       gtk_tree_model_get(model, &iter, ACCOUNT_DATA, &res, -1);
+       PrefsAccount *res =
+               (PrefsAccount *)gtkut_tree_view_get_selected_pointer(
+                       GTK_TREE_VIEW(list_view), ACCOUNT_DATA);
 
-       return res;                        
+       return res;
 }
 
 /*!
index 44e28ca..a6a7f2a 100644 (file)
@@ -131,16 +131,8 @@ static gboolean addressadd_key_pressed( GtkWidget *widget, GdkEventKey *event, g
 /* Points addressadd_dlg.fiSelected to the selected item */
 static void set_selected_ptr()
 {
-       GtkWidget *view = addressadd_dlg.tree_folder;
-       GtkTreeModel *model;
-       GtkTreeSelection *sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
-       GtkTreeIter iter;
-
-       if (gtk_tree_selection_get_selected(sel, &model, &iter)) {
-               gtk_tree_model_get(model, &iter,
-                               ADDRADD_COL_PTR, &addressadd_dlg.fiSelected,
-                               -1);
-       }
+       addressadd_dlg.fiSelected = gtkut_tree_view_get_selected_pointer(
+                       GTK_TREE_VIEW(addressadd_dlg.tree_folder), ADDRADD_COL_PTR);
 }
 
 static void addressadd_ok( GtkWidget *widget, gboolean *cancelled ) {
index a933471..689af37 100644 (file)
@@ -1989,3 +1989,35 @@ gboolean auto_configure_service_sync(const gchar *service, const gchar *domain,
        return result;
 }
 #endif
+
+gpointer gtkut_tree_view_get_selected_pointer(GtkTreeView *view,
+               gint column)
+{
+       GtkTreeIter iter;
+       GtkTreeModel *model;
+       GtkTreeSelection *sel;
+       gpointer ptr;
+
+       cm_return_val_if_fail(view != NULL, NULL);
+       cm_return_val_if_fail(column >= 0, NULL);
+
+       sel = gtk_tree_view_get_selection(view);
+
+       cm_return_val_if_fail(
+                       gtk_tree_selection_count_selected_rows(sel) == 1,
+                       NULL);
+
+       if (!gtk_tree_selection_get_selected(sel, &model, &iter))
+               return NULL; /* No row selected */
+
+       cm_return_val_if_fail(
+                       gtk_tree_model_get_n_columns(model) > column,
+                       NULL);
+       cm_return_val_if_fail(
+                       gtk_tree_model_get_column_type(model, column) == G_TYPE_POINTER,
+                       NULL);
+
+       gtk_tree_model_get(model, &iter, column, &ptr, -1);
+
+       return ptr;
+}
index 6942831..eaf0fb0 100644 (file)
@@ -232,6 +232,10 @@ void auto_configure_service(AutoConfigureData *data);
 gboolean auto_configure_service_sync(const gchar *service, const gchar *domain, gchar **srvhost, guint16 *srvport);
 #endif
 
+/* Returns pointer stored in selected row of a tree view's model
+ * in a given column. The column has to be of type G_TYPE_POINTER. */
+gpointer gtkut_tree_view_get_selected_pointer(GtkTreeView *view,
+               gint column);
 
 #if GTK_CHECK_VERSION (3, 2, 0)
 #define GTK_TYPE_VBOX GTK_TYPE_BOX
index 379ea7c..707146c 100644 (file)
@@ -588,16 +588,12 @@ static void imp_ldif_page_file( gint pageNum, gchar *pageLbl ) {
 static void imp_ldif_field_list_cursor_changed(GtkTreeView *view,
                gpointer user_data)
 {
-       GtkTreeModel *model;
-       GtkTreeSelection *sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
-       GtkTreeIter iter;
        Ldif_FieldRec *rec;
 
-       if (!gtk_tree_selection_get_selected(sel, &model, &iter))
-               return; /* No row selected */
-       gtk_tree_model_get(model, &iter, FIELD_COL_PTR, &rec, -1);
-
        gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryAttrib), "" );
+
+       rec = gtkut_tree_view_get_selected_pointer(view, FIELD_COL_PTR);
+
        if( rec != NULL) {
                /* Update widget contents */
                gtk_label_set_text(
index 223af09..82cfd78 100644 (file)
@@ -568,16 +568,8 @@ void mimeview_destroy(MimeView *mimeview)
 
 MimeInfo *mimeview_get_selected_part(MimeView *mimeview)
 {
-       GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
-       GtkTreeIter iter;
-       GtkTreeSelection *selection;
-       MimeInfo *partinfo = NULL;
-       
-       selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(mimeview->ctree));
-       if (gtk_tree_selection_get_selected(selection, NULL, &iter))
-               gtk_tree_model_get(model, &iter, COL_DATA, &partinfo, -1);
-
-       return partinfo;
+       return gtkut_tree_view_get_selected_pointer(
+                       GTK_TREE_VIEW(mimeview->ctree), COL_DATA);
 }
 
 MimeInfo *mimeview_get_node_part(MimeView *mimeview, GtkTreePath *path)
index f558f6b..0598851 100644 (file)
@@ -610,18 +610,11 @@ select_btn_cb (GtkWidget *widget, gpointer data)
     struct select_keys_s *sk = data;
     gboolean use_key;
     gpgme_key_t key;
-    GtkTreeModel *model;
-    GtkTreeSelection *sel;
-    GtkTreeIter iter;
 
     cm_return_if_fail (sk);
-               sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(sk->view));
-    if (!gtk_tree_selection_get_selected(sel, &model, &iter)) {
-        debug_print ("** nothing selected\n");
-        return;
-    }
 
-    gtk_tree_model_get(model, &iter, COL_PTR, &key, -1);
+    key = gtkut_tree_view_get_selected_pointer(
+        GTK_TREE_VIEW(sk->view), COL_PTR);
     if (key) {
         gpgme_user_id_t uid;
        for (uid = key->uids; uid; uid = uid->next) {
index 9c75cf0..1792b35 100644 (file)
@@ -425,17 +425,10 @@ static void ssl_manager_view_cb(GtkWidget *widget,
                                gpointer data) 
 {
        SSLCertificate *cert;
-       GtkTreeIter sel;
-       GtkTreeModel *model;
 
-       if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
-                               (GTK_TREE_VIEW(manager.certlist)),
-                               &model, &sel))
-               return;
-       
-       gtk_tree_model_get(model, &sel,
-                          SSL_MANAGER_CERT, &cert,
-                          -1);
+       cert = gtkut_tree_view_get_selected_pointer(
+                       GTK_TREE_VIEW(manager.certlist), SSL_MANAGER_CERT);
+
        if (!cert)
                return;