gnunet-svn
[Top][All Lists]
Advanced

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

[taler-anastasis-gtk] 02/07: worked on select country


From: gnunet
Subject: [taler-anastasis-gtk] 02/07: worked on select country
Date: Tue, 15 Sep 2020 19:27:12 +0200

This is an automated email from the git hooks/post-receive script.

dennis-neufeld pushed a commit to branch master
in repository anastasis-gtk.

commit 292c0a2a86705bba2d9c1078edcccb6f9d4c9b49
Author: Dennis Neufeld <dennis.neufeld@students.bfh.ch>
AuthorDate: Mon Sep 14 18:52:35 2020 +0200

    worked on select country
---
 contrib/anastasis_gtk_main_window.glade |   5 +-
 src/anastasis/Makefile.am               |   2 +
 src/anastasis/anastasis-gtk.c           |  14 +-
 src/anastasis/anastasis-gtk_backup.c    |  26 +---
 src/anastasis/anastasis-gtk_helper.c    | 234 ++++++++++++++++++++++++++++++++
 src/include/anastasis-gtk_helper.h      |  60 ++++++++
 6 files changed, 309 insertions(+), 32 deletions(-)

diff --git a/contrib/anastasis_gtk_main_window.glade 
b/contrib/anastasis_gtk_main_window.glade
index c1bc601..4618da6 100644
--- a/contrib/anastasis_gtk_main_window.glade
+++ b/contrib/anastasis_gtk_main_window.glade
@@ -8,7 +8,7 @@
     <property name="stock">gtk-go-up</property>
     <property name="icon_size">6</property>
   </object>
-  <object class="GtkListStore" id="continent_Europa_liststore">
+  <object class="GtkListStore" id="continent_Europe_liststore">
     <columns>
       <!-- column-name country_name -->
       <column type="gchararray"/>
@@ -207,6 +207,8 @@
                                         <property 
name="model">continent_liststore</property>
                                         <property 
name="enable_search">False</property>
                                         <property 
name="search_column">0</property>
+                                        <property 
name="activate_on_single_click">True</property>
+                                        <signal name="row-activated" 
handler="anastasis_gtk_continent_activated" swapped="no"/>
                                         <child internal-child="selection">
                                           <object class="GtkTreeSelection"/>
                                         </child>
@@ -233,6 +235,7 @@
                                         <property 
name="visible">True</property>
                                         <property 
name="can_focus">True</property>
                                         <property 
name="search_column">0</property>
+                                        <property 
name="activate_on_single_click">True</property>
                                         <child internal-child="selection">
                                           <object class="GtkTreeSelection"/>
                                         </child>
diff --git a/src/anastasis/Makefile.am b/src/anastasis/Makefile.am
index 3d50473..be38f24 100644
--- a/src/anastasis/Makefile.am
+++ b/src/anastasis/Makefile.am
@@ -12,6 +12,7 @@ bin_PROGRAMS = anastasis-gtk
 
 anastasis_gtk_SOURCES = \
   anastasis-gtk.c \
+  anastasis-gtk_helper.c \
   anastasis-gtk_about.c \
   anastasis-gtk_backup.c \
   os_installation.c
@@ -22,6 +23,7 @@ anastasis_gtk_LDADD = \
   -lanastasisredux \
   -lgnunetgtk \
   -lgnunetutil \
+  -ljansson \
   $(INTLLIBS)
 anastasis_gtk_LDFLAGS = \
   -export-dynamic
diff --git a/src/anastasis/anastasis-gtk.c b/src/anastasis/anastasis-gtk.c
index 0071f00..ed95502 100644
--- a/src/anastasis/anastasis-gtk.c
+++ b/src/anastasis/anastasis-gtk.c
@@ -28,6 +28,7 @@
 #include <gnunet/gnunet_util_lib.h>
 #include <gnunet-gtk/gnunet_gtk.h>
 #include "anastasis-gtk_helper.h"
+#include <jansson.h>
 
 /**
  * Handle to our main loop.
@@ -40,18 +41,9 @@ struct GNUNET_GTK_MainLoop *ml;
 struct GNUNET_CONFIGURATION_Handle *cfg;
 
 /**
- * Get an object from the main window.
- *
- * @param name name of the object
- * @return NULL on error
+ * Actual state.
  */
-GObject *
-GCG_get_main_window_object (const char *name)
-{
-  if (NULL == ml)
-    return NULL;
-  return GNUNET_GTK_main_loop_get_object (ml, name);
-}
+json_t *backup_state;
 
 
 /**
diff --git a/src/anastasis/anastasis-gtk_backup.c 
b/src/anastasis/anastasis-gtk_backup.c
index 153f8c3..b465357 100644
--- a/src/anastasis/anastasis-gtk_backup.c
+++ b/src/anastasis/anastasis-gtk_backup.c
@@ -29,7 +29,7 @@
 #include <gnunet-gtk/gnunet_gtk.h>
 #include <gtk/gtk.h>
 #include "anastasis-gtk_helper.h"
-#include <anastasis/anastasis_redux.h>
+#include <jansson.h>
 
 
 /**
@@ -42,26 +42,13 @@ void
 anastasis_gtk_backup_button_clicked (GObject *object,
                                      gpointer user_data)
 {
-     GtkListStore *continent_liststore;
-     GtkTreeIter iter;
-     json_t *init_state = ANASTASIS_backup_start (cfg);
-     const char *state = json_string_value (json_object_get (init_state, 
"backup-state"));
-     json_t *continents = json_object_get (init_state, "continents");
-     json_t *continent;
-     size_t index;
+     backup_state = ANASTASIS_backup_start (cfg);
 
-     GNUNET_assert (0 == strcmp (state, "ReduxInitialBackupState"));
-     continent_liststore = GTK_LIST_STORE (
-         GCG_get_main_window_object ("continent_liststore")); 
-
-     json_array_foreach (continents, index, continent)
-     {
-          gtk_list_store_append (continent_liststore, &iter);
-          gtk_list_store_set (continent_liststore, &iter,
-                              0, json_string_value (continent),
-                              -1);
-     }
+     GNUNET_assert (check_state (backup_state, "ReduxInitialBackupState")); 
+     GNUNET_assert (0 == persist_state (backup_state));
 
+     init_continent_list (backup_state);
+     
      gtk_widget_hide (GTK_WIDGET (GCG_get_main_window_object (
                                    "anastasis_gtk_start_frame")));
      gtk_widget_show (GTK_WIDGET (GCG_get_main_window_object (
@@ -70,7 +57,6 @@ anastasis_gtk_backup_button_clicked (GObject *object,
                                    "anastasis_gtk_continent_frame")));
 }
 
-
 /**
  * Callback invoked if the the "back"-button is clicked.
  *
diff --git a/src/anastasis/anastasis-gtk_helper.c 
b/src/anastasis/anastasis-gtk_helper.c
new file mode 100644
index 0000000..fadcdc8
--- /dev/null
+++ b/src/anastasis/anastasis-gtk_helper.c
@@ -0,0 +1,234 @@
+/*
+     This file is part of anastasis-gtk.
+     Copyright (C) 2020 Anastasis SARL
+
+     Anastasis is free software; you can redistribute it and/or modify
+     it under the terms of the GNU General Public License as published
+     by the Free Software Foundation; either version 3, or (at your
+     option) any later version.
+
+     Anastasis 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
+     General Public License for more details.
+
+     You should have received a copy of the GNU General Public License
+     along with Anastasis; see the file COPYING.  If not, write to the
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
+*/
+
+/**
+ * @file src/anastasis/anastasis-gtk_helper.c
+ * @brief Helper functions of anastasis-gtk
+ * @author Christian Grothoff
+ * @author Dennis Neufeld
+ */
+#include <gnunet/platform.h>
+#include <gnunet/gnunet_util_lib.h>
+#include <gnunet-gtk/gnunet_gtk.h>
+#include <gtk/gtk.h>
+#include "anastasis-gtk_helper.h"
+#include <jansson.h>
+
+
+/**
+ * Get an object from the main window.
+ *
+ * @param name name of the object
+ * @return NULL on error
+ */
+GObject *
+GCG_get_main_window_object (const char *name)
+{
+  if (NULL == ml)
+    return NULL;
+  return GNUNET_GTK_main_loop_get_object (ml, name);
+}
+
+
+/**
+ * Function called with the results of #ANASTASIS_backup_action
+ * or #ANASTASIS_recovery_action.
+ *
+ * @param cls closure
+ * @param error_code Error code
+ * @param new_state new state as result
+ */
+static void
+action_cb (void *cls,
+           enum ANASTASIS_ErrorCode error_code,
+           json_t *result_state)
+{
+    if (NULL != result_state)
+    {
+        backup_state = result_state;
+        if (0 == persist_state (backup_state))
+            return;
+    }
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Failed to persist new state! \n");
+    GNUNET_SCHEDULER_shutdown ();
+}
+
+
+/**
+ * Function to persist a state.
+ * 
+ * @param state state to persist
+ */
+int
+persist_state (json_t *state)
+{
+    char *dn;
+
+    {
+        char *path;
+        char *mode;
+
+        if (NULL != json_object_get (state, "backup-state"))
+            mode = "backup";
+        else if (NULL != json_object_get (state, "recovery-state"))
+            mode = "recovery";
+        else
+        {
+            GNUNET_break (0);
+            return GNUNET_SYSERR;
+        }
+        
+        path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_PREFIX);
+        if (NULL == path)
+        {
+            GNUNET_break (0);
+            return GNUNET_SYSERR;
+        }
+        GNUNET_asprintf (&dn,
+                        "%s/share/anastasis/%s_state.json",
+                        path,
+                        mode);
+        GNUNET_free (path);
+    }
+    return json_dump_file (state, dn, JSON_COMPACT);
+}
+
+
+/**
+ * Checks the actual state. True, if state is correct, else false.
+ * 
+ * @param state the state to check
+ * @param state_name the expected state
+ * @return bool
+ */
+bool
+check_state (json_t *state, char *expected_state)
+{
+    const char *state_name = json_string_value (json_object_get (state, 
"backup-state"));
+    if (0 == strcmp (state_name, expected_state))
+        return true;
+    else
+        return false;
+}
+
+
+/**
+ * Initialize the continent list.
+ * 
+ * @param init_state the initial state to get the continents from
+ */
+void
+init_continent_list (json_t *init_state)
+{
+    GtkListStore *continent_liststore;
+    GtkTreeIter iter;
+    json_t *continent;
+    size_t index;
+    json_t *continents = json_object_get (init_state, "continents");
+
+    continent_liststore = GTK_LIST_STORE (
+         GCG_get_main_window_object ("continent_liststore"));
+    gtk_list_store_clear (continent_liststore);
+    json_array_foreach (continents, index, continent)
+    {
+        gtk_list_store_append (continent_liststore, &iter);
+        gtk_list_store_set (continent_liststore, &iter,
+                            0, json_string_value (continent),
+                            -1);
+    }
+}
+
+
+/**
+ * Initialize the country list in dependence of selected continent.
+ * 
+ * @param continent_selected_state the state to get the countries from
+ * @param continent name of the selected country
+ */
+void
+init_country_list (json_t *continent_selected_state,
+                   const char *continent)
+{
+    if (check_state (backup_state, "ReduxContinentSelectedState"))
+    {
+        GtkListStore *country_liststore;
+        GtkTreeIter iter;
+        char *country_list;
+        json_t *country;
+        size_t index;
+        json_t *countries = json_object_get (continent_selected_state, 
"countries");
+
+        sprintf (country_list,
+                 "continent_%s_liststore",
+                 continent);
+        country_liststore = GTK_LIST_STORE (
+            GCG_get_main_window_object (country_list));
+        gtk_list_store_clear (country_liststore);
+        json_array_foreach (countries, index, country)
+        {
+            gtk_list_store_append (country_liststore, &iter);
+            gtk_list_store_set (country_liststore, &iter,
+                                0, json_string_value (json_object_get 
(country, "name")), //FIXME implement i18n
+                                1, json_string_value (json_object_get 
(country, "code")),
+                                -1);
+        }
+        GNUNET_free (country_list);
+    }
+}
+
+
+/**
+ * Callback invoked if a continent is selected (row-activated signal).
+ *
+ * @param tree_view the object on which the signal is emitted
+ * @param path the GtkTreePath for the activated row
+ * @param column the GtkTreeViewColumn in which the activation occurred
+ * @param user_data user data set when the signal handler was connected 
(unused)
+ */
+void
+anastasis_gtk_continent_activated (GtkTreeView *tree_view,
+                                   GtkTreePath *path,
+                                   GtkTreeViewColumn *column,
+                                   gpointer user_data)
+{
+    GtkTreeIter iter;
+    GtkTreeModel *model= gtk_tree_view_get_model (tree_view);
+
+    if (gtk_tree_model_get_iter(model, &iter, path))
+    {
+        gchar *continent_name;
+        json_t *arguments = json_object ();
+
+        if (NULL == arguments)
+        {
+            GNUNET_break (0);
+        }
+        gtk_tree_model_get (model, &iter, 0, &continent_name, -1);
+        json_object_set (arguments, "continent", json_string (continent_name));
+        ANASTASIS_backup_action (backup_state,
+                                 "selection_continent",
+                                 arguments,
+                                 &action_cb,
+                                 NULL);
+        init_country_list (backup_state, continent_name);
+        g_free(continent_name);
+    }
+}
\ No newline at end of file
diff --git a/src/include/anastasis-gtk_helper.h 
b/src/include/anastasis-gtk_helper.h
index 1363a4c..270472d 100644
--- a/src/include/anastasis-gtk_helper.h
+++ b/src/include/anastasis-gtk_helper.h
@@ -26,6 +26,7 @@
  */
 
 #include <gnunet-gtk/gnunet_gtk.h>
+#include <anastasis/anastasis_redux.h>
 
 /**
  * Handle to our main loop.
@@ -37,6 +38,34 @@ extern struct GNUNET_GTK_MainLoop *ml;
  */
 extern struct GNUNET_CONFIGURATION_Handle *cfg;
 
+/**
+ * Actual state.
+ */
+extern json_t *backup_state;
+
+
+/**
+ * Function called with the results of #ANASTASIS_backup_action
+ * or #ANASTASIS_recovery_action.
+ *
+ * @param cls closure
+ * @param error_code Error code
+ * @param new_state new state as result
+ */
+static void
+action_cb (void *cls,
+           enum ANASTASIS_ErrorCode error,
+           json_t *new_state);
+
+
+/**
+ * Function to persist a state.
+ * 
+ * @param state state to persist
+ */
+int
+persist_state (json_t *state);
+
 
 /**
  * Get an object from the main window.
@@ -47,3 +76,34 @@ extern struct GNUNET_CONFIGURATION_Handle *cfg;
 GObject *
 GCG_get_main_window_object (const char *name);
 
+
+/**
+ * Checks the actual state. True, if state is correct, else false.
+ * 
+ * @param state the state to check
+ * @param state_name the expected state
+ * @return bool
+ */
+bool
+check_state (json_t *state, char *expected_state);
+
+
+/**
+ * Initialize the continent list.
+ * 
+ * @param init_state the initial state to get the continents from
+ */
+void
+init_continent_list (json_t *init_state);
+
+
+/**
+ * Initialize the country list in dependence of selected continent.
+ * 
+ * @param init_state the initial state to get the countries from
+ * @param continent name of the selected country
+ */
+void
+init_country_list (json_t *init_state,
+                   const char *continent);
+

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]