Implemented 'Do not show this message again' checkbox for warning dialogs
authorDan Breslau <dbreslau@painless-security.com>
Fri, 5 Aug 2016 21:12:34 +0000 (17:12 -0400)
committerDan Breslau <dbreslau@painless-security.com>
Fri, 5 Aug 2016 21:12:34 +0000 (17:12 -0400)
.gitignore
Makefile.am
src/moonshot-identity-dialog.vala
src/moonshot-settings.vala [new file with mode: 0644]
src/moonshot-warning-dialog.vala

index 22066a7..a39248a 100755 (executable)
@@ -46,7 +46,9 @@ src/moonshot-password-dialog.c
 src/moonshot-provisioning-common-new.vala
 src/moonshot-provisioning-common.c
 src/moonshot-server.c
+src/moonshot-settings.c
 src/moonshot-utils.c
+src/moonshot-warning-dialog.c
 src/moonshot-webp-parser.c
 src/moonshot-window.c
 src/msrpc-client.c
index 5a537cf..f7dcb5d 100644 (file)
@@ -65,6 +65,7 @@ src_moonshot_SOURCES = \
         src/moonshot-identities-manager.vala \
         src/moonshot-identity-request.vala \
         src/moonshot-server.vala \
+        src/moonshot-settings.vala \
         src/moonshot-password-dialog.vala \
         src/moonshot-provisioning-common.vala \
         src/moonshot-utils.vala \
index 0f97f3c..93bcdb2 100644 (file)
@@ -362,8 +362,9 @@ class IdentityDialog : Dialog
         remove_button.clicked.connect((remove_button) =>
             {
                 var result = WarningDialog.confirm(this,
-                                                   "<span font-weight='heavy'>You are about to remove the service '%s'.</span>"
-                                                   .printf(selected_item.label)
+                                                   Markup.printf_escaped(
+                                                       "<span font-weight='heavy'>You are about to remove the service '%s'.</span>",
+                                                       selected_item.label)
                                                    + "\n\nAre you sure you want to do this?",
                                                    "delete_service");
 
diff --git a/src/moonshot-settings.vala b/src/moonshot-settings.vala
new file mode 100644 (file)
index 0000000..1549213
--- /dev/null
@@ -0,0 +1,137 @@
+/*
+ * Copyright (c) 2011-2016, JANET(UK)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of JANET(UK) nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+*/
+
+using Gtk;
+
+   
+private MoonshotLogger logger()
+{
+    return get_logger("MoonshotSettings");
+}
+
+static const string KEY_FILE_NAME="moonshot-ui.config";
+
+private KeyFile get_keyfile()
+{
+    KeyFile key_file = new KeyFile();
+    string config_dir = Environment.get_user_config_dir();
+    logger().trace("get_keyfile: config_dir=" + config_dir);
+
+    File dir = File.new_for_path(config_dir);
+    string path = dir.get_child(KEY_FILE_NAME).get_path();
+
+    try {
+        if (key_file.load_from_file(path, KeyFileFlags.NONE))
+            logger().trace("get_keyfile: load_from_file returned successfully");
+        else
+            logger().trace("get_keyfile: load_from_file returned false");            
+    }
+    catch (FileError e) {
+        logger().trace("get_keyfile: FileError: " + e.message);
+    }
+    catch (KeyFileError e) {
+        logger().trace("get_keyfile: KeyFileError: " + e.message);
+    }
+
+    return key_file;
+}
+
+
+private void save_keyfile(KeyFile key_file)
+{
+    string config_dir = Environment.get_user_config_dir();
+    File dest = null;
+
+    // Make the directory if it doesn't already exist; ignore errors.
+       try {
+               File dir = File.new_for_path(config_dir);
+        dest = dir.get_child(KEY_FILE_NAME);
+               dir.make_directory_with_parents();
+       } catch (Error e) {
+        logger().trace("save_keyfile: make_directory_with_parents threw error (this is usually ignorable) : " + e.message);
+       }
+
+    // It would be nice to use key_file.save_to_file, but the binding doesn't exist
+    // in earlier versions of valac
+    // key_file.save_to_file(path.get_path());
+
+    string data = key_file.to_data();
+    try {
+        logger().trace("save_keyfile: saving to file path '%s'".printf(dest.get_path()));
+        // FileOutputStream s = dest.create(FileCreateFlags.REPLACE_DESTINATION | FileCreateFlags.PRIVATE);
+        // var ds = new DataOutputStream(s);
+        // ds.put_string(data);
+        string new_etag;
+        dest.replace_contents(data.data, null, false, FileCreateFlags.REPLACE_DESTINATION | FileCreateFlags.PRIVATE, out new_etag);
+    }
+    catch(Error e) {
+        logger().error("save_keyfile: error when writing to file: " + e.message);
+    }
+
+    // streams close automatically
+}
+
+internal void set_bool_setting(string group_name, string key_name, bool value)
+{
+    KeyFile key_file = get_keyfile();
+
+    key_file.set_boolean(group_name, key_name, value);
+    save_keyfile(key_file);
+}
+
+internal bool get_bool_setting(string group_name, string key_name, bool default=false)
+{
+    KeyFile key_file = get_keyfile();
+
+    if (key_file == null)
+        return default;
+
+    try {
+        if (!key_file.has_key(group_name, key_name))
+        {
+            logger().info(@"get_bool_setting : key file doesn't contain key '$key_name' in group '$group_name'");
+            return default;
+        }
+    }
+    catch(KeyFileError e) {
+        logger().info(@"get_bool_setting : KeyFileError checking if key '$key_name' exists in group '$group_name' (maybe ignorable?) : " + e.message);
+    }
+
+    try {
+        // throws KeyFileError if key is not found
+        return key_file.get_boolean(group_name, key_name);
+    }
+    catch (KeyFileError e) {
+        logger().info("get_bool_setting got KeyFileError (may be ignorable) : " + e.message);
+    }
+    return default;
+}
index 8ec583a..131ad1b 100644 (file)
 */
 using Gtk;
 
+static const string GROUP_NAME="WarningDialogs";
+
 // MessageDialog doesn't allow subclassing, so we merely wrap the
 // constructor for it the dialog, and then run it, returning the result.
 class WarningDialog 
 {
+    private static MoonshotLogger _logger = null;
+    private static MoonshotLogger logger()
+        {
+            if (_logger == null) {
+                _logger = get_logger("WarningDialog");
+            }
+            return _logger;
+        }
+
     public static bool confirm(Window parent, string message, string dialog_name)
     {
+
+        if (get_bool_setting(GROUP_NAME, dialog_name, false))
+        {
+            logger().trace(@"confirm: Settings group $GROUP_NAME has 'true' for key $dialog_name; skipping dialog and returning true.");
+            return true;
+        }
+
         Gdk.Color white = make_color(65535, 65535, 65535);
 
         MessageDialog dialog = new Gtk.MessageDialog(parent,
@@ -46,7 +64,7 @@ class WarningDialog
                                                      "");
 
         var content_area = dialog.get_content_area();
-        CheckButton remember_checkbutton;
+        CheckButton remember_checkbutton = null;
 
         if (dialog_name != null && dialog_name != "")
         {
@@ -79,6 +97,12 @@ class WarningDialog
         dialog.set_markup(message);
 
         var ret = dialog.run();
+
+        if (ret == Gtk.ResponseType.YES && remember_checkbutton != null && remember_checkbutton.active)
+        {
+            set_bool_setting(GROUP_NAME, dialog_name, true);
+        }
+
         dialog.destroy();
         return (ret == Gtk.ResponseType.YES);
     }