]> saetta.ns0.it Git - solipa/libsolipa/commitdiff
Implementata la gui per l'esportazione in csv.
authorAndrea Zagli <azagli@libero.it>
Sat, 18 Dec 2010 12:45:51 +0000 (13:45 +0100)
committerAndrea Zagli <azagli@libero.it>
Sat, 18 Dec 2010 12:45:51 +0000 (13:45 +0100)
data/libsolipa/gui/libsolipa.ui
src/solipa.c
src/solipa.h
src/utils.c
src/utils.h
tests/utils.c

index ae7ca866a002ffb0c97ff1e44cd5e4cec8729313..ad6bd4f1f37c67ede00996fa48b744fb69cce187 100644 (file)
       </object>
     </child>
   </object>
-  <object class="GtkListStore" id="lstore_treemodel_campi">
-    <columns>
-      <!-- column-name selezionato -->
-      <column type="gboolean"/>
-      <!-- column-name nome -->
-      <column type="gchararray"/>
-    </columns>
-  </object>
   <object class="GtkDialog" id="diag_gtktreemodel_to_csv">
     <property name="border_width">5</property>
     <property name="title" translatable="yes">Esporta CSV</property>
                     <property name="left_padding">5</property>
                     <property name="right_padding">5</property>
                     <child>
-                      <object class="GtkScrolledWindow" id="scrolledwindow1">
+                      <object class="GtkVBox" id="vbox2">
                         <property name="visible">True</property>
-                        <property name="can_focus">True</property>
-                        <property name="hscrollbar_policy">automatic</property>
-                        <property name="vscrollbar_policy">automatic</property>
-                        <property name="shadow_type">etched-in</property>
+                        <property name="orientation">vertical</property>
+                        <property name="spacing">5</property>
                         <child>
-                          <object class="GtkTreeView" id="treeview1">
+                          <object class="GtkScrolledWindow" id="scrolledwindow1">
                             <property name="visible">True</property>
                             <property name="can_focus">True</property>
-                            <property name="model">lstore_treemodel_campi</property>
+                            <property name="hscrollbar_policy">automatic</property>
+                            <property name="vscrollbar_policy">automatic</property>
+                            <property name="shadow_type">etched-in</property>
                             <child>
-                              <object class="GtkTreeViewColumn" id="treeviewcolumn1">
+                              <object class="GtkTreeView" id="treeview1">
+                                <property name="visible">True</property>
+                                <property name="can_focus">True</property>
+                                <property name="model">lstore_treemodel_campi</property>
+                                <property name="headers_clickable">False</property>
+                                <property name="search_column">0</property>
+                                <child>
+                                  <object class="GtkTreeViewColumn" id="treeviewcolumn1">
+                                    <child>
+                                      <object class="GtkCellRendererToggle" id="cellrenderertoggle1"/>
+                                      <attributes>
+                                        <attribute name="active">0</attribute>
+                                      </attributes>
+                                    </child>
+                                  </object>
+                                </child>
                                 <child>
-                                  <object class="GtkCellRendererToggle" id="cellrenderertoggle1"/>
-                                  <attributes>
-                                    <attribute name="active">0</attribute>
-                                  </attributes>
+                                  <object class="GtkTreeViewColumn" id="treeviewcolumn2">
+                                    <property name="title">Campo</property>
+                                    <child>
+                                      <object class="GtkCellRendererText" id="cellrenderertext1"/>
+                                      <attributes>
+                                        <attribute name="text">2</attribute>
+                                      </attributes>
+                                    </child>
+                                  </object>
                                 </child>
                               </object>
                             </child>
+                          </object>
+                          <packing>
+                            <property name="position">0</property>
+                          </packing>
+                        </child>
+                        <child>
+                          <object class="GtkHButtonBox" id="hbuttonbox1">
+                            <property name="visible">True</property>
+                            <property name="spacing">5</property>
+                            <property name="layout_style">end</property>
                             <child>
-                              <object class="GtkTreeViewColumn" id="treeviewcolumn2">
-                                <property name="title">Campo</property>
-                                <child>
-                                  <object class="GtkCellRendererText" id="cellrenderertext1"/>
-                                  <attributes>
-                                    <attribute name="text">1</attribute>
-                                  </attributes>
-                                </child>
+                              <object class="GtkButton" id="button3">
+                                <property name="label" translatable="yes">Deseleziona tutti</property>
+                                <property name="visible">True</property>
+                                <property name="can_focus">True</property>
+                                <property name="receives_default">True</property>
                               </object>
+                              <packing>
+                                <property name="expand">False</property>
+                                <property name="fill">False</property>
+                                <property name="position">0</property>
+                              </packing>
+                            </child>
+                            <child>
+                              <object class="GtkButton" id="button4">
+                                <property name="label" translatable="yes">Seleziona tutti</property>
+                                <property name="visible">True</property>
+                                <property name="can_focus">True</property>
+                                <property name="receives_default">True</property>
+                              </object>
+                              <packing>
+                                <property name="expand">False</property>
+                                <property name="fill">False</property>
+                                <property name="position">1</property>
+                              </packing>
                             </child>
                           </object>
+                          <packing>
+                            <property name="position">1</property>
+                          </packing>
                         </child>
                       </object>
                     </child>
                         <property name="visible">True</property>
                         <property name="can_focus">True</property>
                         <property name="invisible_char">&#x25CF;</property>
+                        <property name="text" translatable="yes">"</property>
                       </object>
                       <packing>
                         <property name="left_attach">1</property>
                         <property name="visible">True</property>
                         <property name="can_focus">True</property>
                         <property name="invisible_char">&#x25CF;</property>
+                        <property name="text" translatable="yes">,</property>
                       </object>
                       <packing>
                         <property name="left_attach">1</property>
                       <object class="GtkFileChooserWidget" id="filechooserwidget1">
                         <property name="visible">True</property>
                         <property name="orientation">vertical</property>
+                        <property name="use_preview_label">False</property>
+                        <property name="preview_widget_active">False</property>
                         <property name="action">save</property>
                         <property name="local_only">False</property>
                       </object>
       <action-widget response="-5">button2</action-widget>
     </action-widgets>
   </object>
+  <object class="GtkListStore" id="lstore_treemodel_campi">
+    <columns>
+      <!-- column-name selezionato -->
+      <column type="gboolean"/>
+      <!-- column-name id -->
+      <column type="guint"/>
+      <!-- column-name nome -->
+      <column type="gchararray"/>
+    </columns>
+  </object>
 </interface>
index ced98c078066877583c97fa43d645349ba6f9c91..891c4288a7cb3b87738bc2ca0e5baef48e337f2f 100644 (file)
@@ -106,7 +106,7 @@ Solipa
        gchar *moddir;
        gchar *p;
 
-       moddir = g_win32_get_package_installation_directory_of_module (NULL);        
+       moddir = g_win32_get_package_installation_directory_of_module (NULL);
 
        p = strrchr (moddir, G_DIR_SEPARATOR);
        if (p != NULL
@@ -231,7 +231,31 @@ GdaEx
 
        priv = SOLIPA_GET_PRIVATE (solipa);
 
-       return priv->gdaex;     
+       return priv->gdaex;
+}
+
+const gchar
+*solipa_get_guifile (Solipa *solipa)
+{
+       SolipaPrivate *priv;
+
+       g_return_val_if_fail (IS_SOLIPA (solipa), NULL);
+
+       priv = SOLIPA_GET_PRIVATE (solipa);
+
+       return g_strdup (priv->guifile);
+}
+
+GtkBuilder
+*solipa_get_gtkbuilder (Solipa *solipa)
+{
+       SolipaPrivate *priv;
+
+       g_return_val_if_fail (IS_SOLIPA (solipa), NULL);
+
+       priv = SOLIPA_GET_PRIVATE (solipa);
+
+       return priv->gtkbuilder;
 }
 
 /* PRIVATE */
@@ -250,7 +274,7 @@ solipa_set_property (GObject *object,
                        default:
                                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
                                break;
-         }
+               }
 }
 
 static void
@@ -268,5 +292,5 @@ solipa_get_property (GObject *object,
                        default:
                                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
                                break;
-         }
+               }
 }
index 312ee317841db1399159663373caddf84d68ffc3..3b39886a2fd298bd16e9d9e7860980ef4d5c66c8 100644 (file)
@@ -22,6 +22,8 @@
 #include <glib.h>
 #include <glib-object.h>
 
+#include <gtk/gtk.h>
+
 #include <camel/camel.h>
 
 #include <libgdaex.h>
@@ -63,6 +65,10 @@ CamelSession *solipa_get_camel_session (Solipa *solipa);
 void solipa_set_gdaex (Solipa *solipa, GdaEx *gdaex);
 GdaEx *solipa_get_gdaex (Solipa *solipa);
 
+const gchar *solipa_get_guifile (Solipa *solipa);
+
+GtkBuilder *solipa_get_gtkbuilder (Solipa *solipa);
+
 
 G_END_DECLS
 
index 9926d1a7f3b2723bac71a27861a597fa4fa2010e..3b46acc43b2b58edb91cce3c8bba1fe103fb6056 100644 (file)
 #endif
 
 #include <gio/gio.h>
+#include <gtk/gtk.h>
+
+#include <solipa.h>
 
 #include "utils.h"
 
+static void solipa_fixed_toggled (GtkCellRendererToggle *cell,
+                                  gchar *path_str,
+                                  gpointer data);
+
+static void solipa_gtkliststore_select_unselect_all (GtkListStore *store,
+                                                     gboolean select);
+
+static void solipa_on_btn_select_all_clicked (GtkButton *button,
+                                    gpointer user_data);
+static void solipa_on_btn_unselect_all_clicked (GtkButton *button,
+                                    gpointer user_data);
+
+gboolean
+solipa_file_exists (const gchar *filename)
+{
+       return (g_file_query_exists (g_file_new_for_path (filename), NULL));
+}
+
 void
 solipa_gtktreemodel_to_csv (GtkTreeModel *model, const gchar *filename,
                             gboolean overwrite,
@@ -181,13 +202,195 @@ solipa_gtktreemodel_to_csv (GtkTreeModel *model, const gchar *filename,
                }
 }
 
+static void
+solipa_fixed_toggled (GtkCellRendererToggle *cell,
+               gchar                 *path_str,
+               gpointer               data)
+{
+       GtkTreeModel *model = (GtkTreeModel *)data;
+       GtkTreeIter  iter;
+       GtkTreePath *path = gtk_tree_path_new_from_string (path_str);
+
+       gboolean choosen;
+
+       /* get toggled iter */
+       gtk_tree_model_get_iter (model, &iter, path);
+       gtk_tree_model_get (model, &iter, 0, &choosen, -1);
+
+       choosen ^= 1;
+
+       /* set new value */
+       gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0, choosen, -1);
+
+       /* clean up */
+       gtk_tree_path_free (path);
+}
+
+static void
+solipa_gtkliststore_select_unselect_all (GtkListStore *store, gboolean select)
+{
+       GtkTreeIter iter;
+
+       if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &iter))
+               {
+                       do
+                               {
+                                       gtk_list_store_set (store, &iter, 0, select, -1);
+                               } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &iter));
+               }
+}
+
+static void
+solipa_on_btn_select_all_clicked (GtkButton *button,
+                                    gpointer user_data)
+{
+       solipa_gtkliststore_select_unselect_all ((GtkListStore *)user_data, TRUE);
+}
+
+static void
+solipa_on_btn_unselect_all_clicked (GtkButton *button,
+                                    gpointer user_data)
+{
+       solipa_gtkliststore_select_unselect_all ((GtkListStore *)user_data, FALSE);
+}
+
 void
 solipa_gtktreemodel_to_csv_gui (Solipa *solipa, GtkTreeModel *model,
                                  gchar **columns_title,
                                  guint n_columns)
 {
        GtkWidget *diag;
+       GError *error;
+       GtkBuilder *builder;
+
+       GtkListStore *store;
+       GtkTreeIter iter;
 
-       
+       gchar *filename;
+
+       guint col;
+       gboolean sel;
+
+       guint *columns;
+       guint _n_columns;
+
+       builder = solipa_get_gtkbuilder (solipa);
+
+       error = NULL;
+       gtk_builder_add_objects_from_file (builder,
+                                          solipa_get_guifile (solipa),
+                                          g_strsplit ("diag_gtktreemodel_to_csv"
+                                                      "|lstore_treemodel_campi",
+                                                      "|", -1),
+                                          &error);
+       if (error != NULL)
+               {
+                       g_warning ("Error: %s.",
+                                  error->message != NULL ? error->message : "no details");
+                       return;
+               }
+
+       diag = GTK_WIDGET (gtk_builder_get_object (builder, "diag_gtktreemodel_to_csv"));
+       if (GTK_IS_WIDGET (diag))
+               {
+                       /* carico i nomi campi per la selezione nel model */
+                       store = GTK_LIST_STORE (gtk_builder_get_object (builder, "lstore_treemodel_campi"));
+
+                       g_signal_connect (gtk_builder_get_object (builder, "cellrenderertoggle1"),
+                                          "toggled", G_CALLBACK (solipa_fixed_toggled), (gpointer)store);
+
+                       g_signal_connect (gtk_builder_get_object (builder, "button3"),
+                                         "clicked", G_CALLBACK (solipa_on_btn_unselect_all_clicked), (gpointer *)store);
+                       g_signal_connect (gtk_builder_get_object (builder, "button4"),
+                                         "clicked", G_CALLBACK (solipa_on_btn_select_all_clicked), (gpointer *)store);
+
+                       gtk_list_store_clear (store);
+
+                       for (col = 0; col < n_columns; col++)
+                               {
+                                       gtk_list_store_append (store, &iter);
+                                       gtk_list_store_set (store, &iter,
+                                                           0, FALSE,
+                                                           1, col,
+                                                           2, columns_title[col],
+                                                           -1);
+                               }
+
+                       do
+                               {
+                                       if (gtk_dialog_run (GTK_DIALOG (diag)) == GTK_RESPONSE_OK)
+                                               {
+                                                       filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (gtk_builder_get_object (builder, "filechooserwidget1")));
+                                                       if (filename == NULL
+                                                           || g_strcmp0 (g_strstrip (filename), "") == 0)
+                                                               {
+                                                                       GtkWidget *diag_req;
+
+                                                                       diag_req = gtk_message_dialog_new (GTK_WINDOW (diag),
+                                                                                                          GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
+                                                                                                          GTK_MESSAGE_WARNING,
+                                                                                                          GTK_BUTTONS_OK,
+                                                                                                          "Immettere il nome del file nel quale esportare i dati.");
+                                                                       gtk_dialog_run (GTK_DIALOG (diag_req));
+                                                                       gtk_widget_destroy (diag_req);
+                                                                       continue;
+                                                               }
+
+                                                       if (solipa_file_exists (filename))
+                                                               {
+                                                                       GtkWidget *diag_req;
+
+                                                                       diag_req = gtk_message_dialog_new (GTK_WINDOW (diag),
+                                                                                                          GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
+                                                                                                          GTK_MESSAGE_QUESTION,
+                                                                                                          GTK_BUTTONS_YES_NO,
+                                                                                                          "File esistente. Sovrascriverlo?");
+                                                                       if (gtk_dialog_run (GTK_DIALOG (diag_req)) == GTK_RESPONSE_YES)
+                                                                               {
+                                                                                       gtk_widget_destroy (diag_req);
+                                                                                       break;
+                                                                               }
+                                                                       gtk_widget_destroy (diag_req);
+                                                               }
+                                                       break;
+                                               }
+                                       else
+                                               {
+                                                       gtk_widget_destroy (diag);
+                                                       return;
+                                               }
+                               } while (TRUE);
+
+                       /* ricavo i campi selezionati che devono essere esportati */
+                       _n_columns = 0;
+                       if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &iter))
+                               {
+                                       columns = g_malloc0 (sizeof (guint));
+                                       do
+                                               {
+                                                       gtk_tree_model_get (GTK_TREE_MODEL (store), &iter, 0, &sel, 1, &col, -1);
+                                                       if (sel)
+                                                               {
+                                                                       _n_columns++;
+                                                                       columns = g_realloc_n (columns, _n_columns, sizeof (guint));
+                                                                       columns[_n_columns - 1] = col;
+                                                               }
+                                               } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &iter));
+                               }
+
+                       /* export */
+                       solipa_gtktreemodel_to_csv (model,
+                                                   filename,
+                                                   TRUE,
+                                                   gtk_entry_get_text (GTK_ENTRY (gtk_builder_get_object (builder, "entry1"))),
+                                                   gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (gtk_builder_get_object (builder, "checkbutton1"))),
+                                                   gtk_entry_get_text (GTK_ENTRY (gtk_builder_get_object (builder, "entry2"))),
+                                                   gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (gtk_builder_get_object (builder, "checkbutton3"))),
+                                                   columns_title,
+                                                   columns,
+                                                   _n_columns);
+
+                       gtk_widget_destroy (diag);
+               }
 }
 
index 91cf84f9c99361546a512a6cd4c0cabdef59ebf1..7c53b060739d07580710562c39ca615ecb6fc6f9 100644 (file)
@@ -27,6 +27,8 @@
 G_BEGIN_DECLS
 
 
+gboolean solipa_file_exists (const gchar *filename);
+
 void solipa_gtktreemodel_to_csv (GtkTreeModel *model, const gchar *filename,
                                  gboolean overwrite,
                                  const gchar *quote, gboolean quote_all,
index 9b433dfa7cfd1c463d27212022b168edc854db8e..7e64d4d35898389441850ef3043512e8be6d2afd 100644 (file)
@@ -22,6 +22,8 @@
 int
 main (int argc, char *argv[])
 {
+       Solipa *solipa;
+
        GtkListStore *model;
 
        gchar **columns_title;
@@ -34,6 +36,8 @@ main (int argc, char *argv[])
 
        gtk_init (&argc, &argv);
 
+       solipa = solipa_new ();
+
        columns_title = g_malloc0 (3 * sizeof (gchar));
        columns_title[0] = g_strdup ("ID");
        columns_title[1] = g_strdup ("Ragione Sociale");
@@ -68,5 +72,8 @@ main (int argc, char *argv[])
                                    columns,
                                    2);
 
+       solipa_gtktreemodel_to_csv_gui (solipa, GTK_TREE_MODEL (model),
+                                       columns_title, 3);
+
        return 0;
 }