From 51587ef41ab94dca2900267a5edcab4345b8f663 Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Wed, 26 Jul 2017 23:06:58 +0200 Subject: Add SSL interception to Setup in GUI --- src/gui_gtk.cc | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 111 insertions(+), 14 deletions(-) (limited to 'src/gui_gtk.cc') diff --git a/src/gui_gtk.cc b/src/gui_gtk.cc index c7031a8..301b92f 100644 --- a/src/gui_gtk.cc +++ b/src/gui_gtk.cc @@ -529,7 +529,6 @@ public: g_settings_sync(); } - using Config::get; std::string const& get(std::string const& key, std::string const& fallback) override { auto variant = g_settings_get_user_value(settings_.get(), key.c_str()); @@ -546,6 +545,13 @@ public: g_variant_unref(variant); return memory_[key].c_str(); } + bool get(std::string const& key, bool fallback) override { + auto variant = g_settings_get_user_value(settings_.get(), key.c_str()); + if (!variant) return fallback; + bool ret = g_variant_get_boolean(variant); + g_variant_unref(variant); + return ret; + } bool is_set(std::string const& key) override { auto variant = g_settings_get_user_value(settings_.get(), key.c_str()); if (!variant) return false; @@ -555,6 +561,9 @@ public: void set(std::string const& key, std::string const& value) override { g_settings_set_string(settings_.get(), key.c_str(), value.c_str()); } + void set(std::string const& key, bool value) override { + g_settings_set_boolean(settings_.get(), key.c_str(), value); + } void remove(std::string const& key) override { g_settings_reset(settings_.get(), key.c_str()); } @@ -931,6 +940,37 @@ public: flags, filter)); } + void add_bool(std::string const& id, std::string const& label, + bool value, std::string const& description) override { + values_.emplace_back(new BoolValue(this, id, label, description, value)); + } + + void enable(std::string const& id, bool enable) override { + for (auto const& value : values_) { + if (value->id_ == id) { + value->enable_ = enable; + if (value->entry_) { + gtk_widget_set_sensitive(value->entry_, enable); + } + switch (value->type_) { + case STRING: + case NUMBER: + case BOOLEAN: + break; + case FILE: { + auto v = static_cast(value.get()); + if (v->button_) { + gtk_widget_set_sensitive(GTK_WIDGET(v->button_), enable); + } else if (v->chooser_) { + gtk_widget_set_sensitive(GTK_WIDGET(v->chooser_), enable); + } + break; + } + } + } + } + } + std::string get_string(std::string const& id) const override { for (auto const& value : values_) { if (value->id_ == id && value->type_ == STRING) { @@ -980,6 +1020,19 @@ public: return ""; } + bool get_bool(std::string const& id) const override { + for (auto const& value : values_) { + if (value->id_ == id && value->type_ == BOOLEAN) { + if (value->entry_) { + return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(value->entry_)); + } + return static_cast(value.get())->value_; + } + } + assert(false); + return false; + } + void set_error(std::string const& error) override { if (!error_) { assert(false); @@ -1019,9 +1072,17 @@ public: gtk_grid_attach(GTK_GRID(grid), label, 0, row++, 3, 1); } for (auto& value : values_) { - auto label = gtk_label_new(value->label_.c_str()); - value->entry_ = gtk_entry_new(); - gtk_entry_set_activates_default(GTK_ENTRY(value->entry_), true); + GtkWidget* label; + if (value->type_ != BOOLEAN) { + label = gtk_label_new(value->label_.c_str()); + } else { + label = nullptr; + } + if (value->type_ != FILE + || (static_cast(value.get())->flags_ & FILE_SAVE)) { + value->entry_ = gtk_entry_new(); + gtk_entry_set_activates_default(GTK_ENTRY(value->entry_), true); + } GtkWidget* extra = nullptr; switch (value->type_) { case STRING: @@ -1044,11 +1105,10 @@ public: case FILE: { auto v = static_cast(value.get()); if ((v->flags_ & FILE_SAVE) == 0) { - gtk_widget_destroy(value->entry_); - auto button = gtk_file_chooser_button_new( + assert(!value->entry_); + value->entry_ = gtk_file_chooser_button_new( v->label_.c_str(), GTK_FILE_CHOOSER_ACTION_OPEN); - v->chooser_ = GTK_FILE_CHOOSER(button); - value->entry_ = button; + v->chooser_ = GTK_FILE_CHOOSER(value->entry_); g_signal_connect(G_OBJECT(v->chooser_), "file-set", G_CALLBACK(file_set), value.get()); } else { @@ -1061,6 +1121,7 @@ public: g_signal_connect(G_OBJECT(extra), "clicked", G_CALLBACK(show_filechooser), v); v->chooser_ = GTK_FILE_CHOOSER(chooser); + v->button_ = extra; gtk_editable_set_editable(GTK_EDITABLE(value->entry_), false); gtk_file_chooser_set_do_overwrite_confirmation(v->chooser_, true); g_signal_connect(G_OBJECT(value->entry_), "changed", @@ -1079,13 +1140,28 @@ public: } break; } + case BOOLEAN: + assert(!label); + gtk_widget_destroy(value->entry_); + value->entry_ = gtk_check_button_new_with_label(value->label_.c_str()); + gtk_toggle_button_set_active( + GTK_TOGGLE_BUTTON(value->entry_), + static_cast(value.get())->value_); + g_signal_connect(G_OBJECT(value->entry_), "toggled", + G_CALLBACK(toggled), value.get()); + break; + } + gint col = 0; + if (label) { + gtk_grid_attach(GTK_GRID(grid), label, col++, row, 1, 1); } - gtk_grid_attach(GTK_GRID(grid), label, 0, row, 1, 1); + gtk_widget_set_sensitive(value->entry_, value->enable_); if (extra) { - gtk_grid_attach(GTK_GRID(grid), value->entry_, 1, row, 1, 1); - gtk_grid_attach(GTK_GRID(grid), extra, 2, row, 1, 1); + gtk_widget_set_sensitive(extra, value->enable_); + gtk_grid_attach(GTK_GRID(grid), value->entry_, col, row, 2 - col, 1); + gtk_grid_attach(GTK_GRID(grid), extra, ++col, row, 1, 1); } else { - gtk_grid_attach(GTK_GRID(grid), value->entry_, 1, row, 2, 1); + gtk_grid_attach(GTK_GRID(grid), value->entry_, col, row, 3 - col, 1); } row++; if (!value->description_.empty()) { @@ -1138,6 +1214,10 @@ public: v->chooser_ = nullptr; break; } + case BOOLEAN: + static_cast(value.get())->value_ = + gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(value->entry_)); + break; } value->entry_ = nullptr; } @@ -1153,6 +1233,7 @@ protected: STRING, NUMBER, FILE, + BOOLEAN, }; struct Value { @@ -1161,12 +1242,13 @@ protected: std::string const id_; std::string const label_; std::string const description_; + bool enable_; GtkWidget* entry_; Value(GtkGuiForm* me, Type type, std::string const& id, std::string const& label, std::string const& description) : me_(me), type_(type), id_(id), label_(label), description_(description), - entry_(nullptr) { + enable_(true), entry_(nullptr) { } }; @@ -1193,12 +1275,22 @@ protected: uint8_t flags_; std::vector filter_; GtkFileChooser* chooser_; + GtkWidget* button_; FileValue(GtkGuiForm* me, std::string const& id, std::string const& label, std::string const& description, std::string const& value, uint8_t flags, std::vector const& filter) : Value(me, FILE, id, label, description), value_(value), - flags_(flags), filter_(filter) { + flags_(flags), filter_(filter), chooser_(nullptr), button_(nullptr) { + } + }; + + struct BoolValue : public Value { + bool value_; + + BoolValue(GtkGuiForm* me, std::string const& id, std::string const& label, + std::string const& description, bool value) + : Value(me, BOOLEAN, id, label, description), value_(value) { } }; @@ -1264,6 +1356,11 @@ protected: value->me_->notify_changed(value->id_); } + static void toggled(GtkToggleButton* UNUSED(button), gpointer user_data) { + auto value = reinterpret_cast(user_data); + value->me_->notify_changed(value->id_); + } + virtual gint add_extra_widgets(GtkGrid* UNUSED(grid), gint row, gint UNUSED(colspan)) { return row; -- cgit v1.2.3-70-g09d2