[polari] util: Add needsOneTimeAction() helper



commit 18f2ca71a95d0a2f616c59c492482cca98b508a0
Author: Florian Müllner <fmuellner gnome org>
Date:   Sun Apr 21 21:48:27 2019 +0200

    util: Add needsOneTimeAction() helper
    
    Split out a helper method from the existing initial-setup check,
    as we are about to add another one-time action.
    
    https://gitlab.gnome.org/GNOME/polari/-/merge_requests/153

 src/application.js | 24 ++----------------------
 src/utils.js       | 36 +++++++++++++++++++++++++++++++++---
 2 files changed, 35 insertions(+), 25 deletions(-)
---
diff --git a/src/application.js b/src/application.js
index 25a8fa90..b413ef5c 100644
--- a/src/application.js
+++ b/src/application.js
@@ -521,34 +521,14 @@ var Application = GObject.registerClass({
         });
     }
 
-    _touchFile(file) {
-        try {
-            file.get_parent().make_directory_with_parents(null);
-        } catch (e) {
-            if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.EXISTS))
-                throw e;
-            // not an error, carry on
-        }
-
-        let stream = file.create(0, null);
-        stream.close(null);
-    }
-
     _needsInitialSetup() {
         if (GLib.getenv('POLARI_FORCE_INITIAL_SETUP')) {
             GLib.unsetenv('POLARI_FORCE_INITIAL_SETUP');
             return true;
         }
 
-        let path = `${GLib.get_user_data_dir()}/polari/initial-setup-completed`;
-        let f = Gio.File.new_for_path(path);
-        try {
-            this._touchFile(f);
-        } catch (e) {
-            if (e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.EXISTS))
-                return false; // initial setup has completed
-            log(`Failed to mark initial setup as completed: ${e.message}`);
-        }
+        if (!Utils.needsOnetimeAction('initial-setup'))
+            return;
 
         let savedRooms = this._settings.get_value('saved-channel-list');
         return savedRooms.n_children() === 0;
diff --git a/src/utils.js b/src/utils.js
index 1bd3b025..602eef60 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -1,7 +1,7 @@
-/* exported isFlatpakSandbox getTpEventTime findUrls findChannels openURL
+/* exported isFlatpakSandbox touchFile needsOnetimeAction getTpEventTime
+            findUrls findChannels openURL updateTerms gpaste imgurPaste
             storeAccountPassword storeIdentifyPassword
-            lookupAccountPassword lookupIdentifyPassword
-            updateTerms gpaste imgurPaste */
+            lookupAccountPassword lookupIdentifyPassword */
 /*
  * Copyright (c) 2011 Red Hat, Inc.
  *
@@ -93,6 +93,36 @@ function isFlatpakSandbox() {
     return _inFlatpakSandbox;
 }
 
+function touchFile(file) {
+    try {
+        file.get_parent().make_directory_with_parents(null);
+    } catch (e) {
+        if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.EXISTS))
+            throw e;
+        // not an error, carry on
+    }
+
+    let stream = file.create(0, null);
+    stream.close(null);
+}
+
+function needsOnetimeAction(name) {
+    let path = GLib.build_filenamev([
+        GLib.get_user_data_dir(),
+        'polari',
+        `${name}-completed`,
+    ]);
+    let file = Gio.File.new_for_path(path);
+    try {
+        touchFile(file);
+    } catch (e) {
+        if (e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.EXISTS))
+            return false;
+        log(`Failed to mark onetime action ${name} as completed: ${e.message}`);
+    }
+    return true;
+}
+
 function getTpEventTime() {
     let time = Gtk.get_current_event_time();
     if (time === 0)


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