anastasis-gtk_progress.c (4032B)
1 /* 2 This file is part of anastasis-gtk. 3 Copyright (C) 2021 Anastasis SARL 4 5 Anastasis is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published 7 by the Free Software Foundation; either version 3, or (at your 8 option) any later version. 9 10 Anastasis is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with Anastasis; see the file COPYING. If not, write to the 17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 18 Boston, MA 02110-1301, USA. 19 */ 20 /** 21 * @file src/anastasis/anastasis-gtk_progress.c 22 * @brief Functions dealing with the tree views used to show the user where we are in the process 23 * @author Christian Grothoff 24 */ 25 #include <gnunet/gnunet_util_lib.h> 26 #include "anastasis-gtk_helper.h" 27 #include <gdk-pixbuf/gdk-pixbuf.h> 28 29 30 /** 31 * Ensure signals are ignored where the user 32 * clicks in the widget. 33 */ 34 gboolean 35 anastasis_gtk_backup_progress_treeview_button_press_event_cb ( 36 GtkWidget *widget, 37 GdkEvent *event, 38 gpointer user_data) 39 { 40 return TRUE; 41 } 42 43 44 /** 45 * Ensure signals are ignored where the user 46 * clicks in the widget. 47 */ 48 gboolean 49 anastasis_gtk_recovery_progress_treeview_button_press_event_cb ( 50 GtkWidget *widget, 51 GdkEvent *event, 52 gpointer 53 user_data) 54 { 55 return TRUE; 56 } 57 58 59 /** 60 * Function to validate an input by regular expression ("validation-regex"). 61 * 62 * @param input text to validate 63 * @param regexp regular expression to validate form 64 * @return true if validation passed, else false 65 */ 66 static bool 67 validate_regex (const char *input, 68 const char *regexp) 69 { 70 regex_t regex; 71 72 if (0 != regcomp (®ex, 73 regexp, 74 REG_EXTENDED)) 75 { 76 GNUNET_break (0); 77 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 78 "Failed to compile regular expression `%s'.", 79 regexp); 80 return true; 81 } 82 /* check if input has correct form */ 83 if (0 != regexec (®ex, 84 input, 85 0, 86 NULL, 87 0)) 88 { 89 regfree (®ex); 90 return false; 91 } 92 regfree (®ex); 93 return true; 94 } 95 96 97 void 98 AG_progress_update (void) 99 { 100 GtkTreeSelection *ts; 101 GtkTreeModel *tm; 102 GtkTreeIter iter; 103 const char *state; 104 105 state = json_string_value (json_object_get (AG_redux_state, 106 "backup_state")); 107 if (NULL == state) 108 { 109 state = json_string_value (json_object_get (AG_redux_state, 110 "recovery_state")); 111 if (NULL == state) 112 { 113 GNUNET_break (0); 114 return; 115 } 116 ts = GTK_TREE_SELECTION (GCG_get_main_window_object ( 117 "anastasis_gtk_recovery_progress_tree_selection") 118 ); 119 } 120 else 121 { 122 ts = GTK_TREE_SELECTION (GCG_get_main_window_object ( 123 "anastasis_gtk_backup_progress_tree_selection")); 124 } 125 if (! gtk_tree_selection_get_selected (ts, 126 &tm, 127 &iter)) 128 { 129 return; 130 } 131 if (! gtk_tree_model_get_iter_first (tm, 132 &iter)) 133 { 134 GNUNET_break (0); 135 return; 136 } 137 do { 138 char *regex; 139 140 gtk_tree_model_get (tm, 141 &iter, 142 AG_PRGMC_REGEX, ®ex, 143 -1); 144 if (validate_regex (state, 145 regex)) 146 { 147 g_free (regex); 148 gtk_tree_selection_select_iter (ts, 149 &iter); 150 return; 151 } 152 g_free (regex); 153 } while (gtk_tree_model_iter_next (tm, 154 &iter)); 155 GNUNET_break (0); 156 return; 157 }