[gnome-games/wip/exalm/savestates: 8/20] savestate: Make is_automatic a property



commit 6050d6249f32620356470b668b679d60c10b5d2e
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Sat Jan 25 23:52:53 2020 +0500

    savestate: Make is_automatic a property

 src/core/savestate.vala           | 32 +++++++++++++-------------------
 src/retro/retro-runner.vala       |  4 ++--
 src/ui/savestate-listbox-row.vala |  2 +-
 src/ui/savestates-list.vala       |  5 +++--
 4 files changed, 19 insertions(+), 24 deletions(-)
---
diff --git a/src/core/savestate.vala b/src/core/savestate.vala
index 20afe186..5c3f717d 100644
--- a/src/core/savestate.vala
+++ b/src/core/savestate.vala
@@ -2,6 +2,10 @@ public class Games.Savestate : Object {
        public string path { get; construct; }
        public Platform platform { get; construct; }
 
+       // Automatic means whether the savestate was created automatically when
+       // quitting/loading the game or manually by the user using the Save button
+       public bool is_automatic { get; private set; }
+
        private static Savestate load (Platform platform, string path) {
                var type = platform.get_savestate_type ();
 
@@ -192,21 +196,27 @@ public class Games.Savestate : Object {
 
        // Set the metadata for an automatic savestate
        public void set_metadata_automatic (DateTime creation_date, string platform, string core, double 
aspect_ratio) throws Error {
-               set_metadata (true, null, creation_date, platform, core, aspect_ratio);
+               is_automatic = true;
+
+               set_metadata (null, creation_date, platform, core, aspect_ratio);
        }
 
        // Set the metadata for a manual savestate
        public void set_metadata_manual (string name, DateTime creation_date, string platform, string core, 
double aspect_ratio) throws Error {
-               set_metadata (false, name, creation_date, platform, core, aspect_ratio);
+               is_automatic = false;
+
+               set_metadata (name, creation_date, platform, core, aspect_ratio);
        }
 
        protected virtual void load_metadata (KeyFile keyfile) throws KeyFileError {
+               is_automatic = keyfile.get_boolean ("Metadata", "Automatic");
        }
 
        protected virtual void save_metadata (KeyFile keyfile) {
+               keyfile.set_boolean ("Metadata", "Automatic", is_automatic);
        }
 
-       private void set_metadata (bool is_automatic, string? name, DateTime creation_date,
+       private void set_metadata (string? name, DateTime creation_date,
                                   string platform, string core, double aspect_ratio) throws Error {
                var metadata_file_path = Path.build_filename (path, "metadata");
                var metadata_file = File.new_for_path (metadata_file_path);
@@ -215,8 +225,6 @@ public class Games.Savestate : Object {
                if (metadata_file.query_exists ())
                        metadata_file.@delete ();
 
-               metadata.set_boolean ("Metadata", "Automatic", is_automatic);
-
                if (name != null)
                        metadata.set_string ("Metadata", "Name", name);
 
@@ -230,20 +238,6 @@ public class Games.Savestate : Object {
                metadata.save_to_file (metadata_file_path);
        }
 
-       // Automatic means whether the savestate was created automatically when
-       // quitting/loading the game or manually by the user using the Save button
-       public bool is_automatic () {
-               var metadata = get_metadata ();
-
-               try {
-                       return metadata.get_boolean ("Metadata", "Automatic");
-               }
-               catch (Error e) {
-                       critical ("Failed to get Automatic field from metadata file for snapshot at %s: %s", 
path, e.message);
-                       return false;
-               }
-       }
-
        public void delete_from_disk () {
                var savestate_dir = File.new_for_path (path);
 
diff --git a/src/retro/retro-runner.vala b/src/retro/retro-runner.vala
index 1429ad84..2c7faebe 100644
--- a/src/retro/retro-runner.vala
+++ b/src/retro/retro-runner.vala
@@ -614,7 +614,7 @@ public class Games.RetroRunner : Object, Runner {
                var regex = new Regex (_("New snapshot %s").printf ("([1-9]\\d*)"));
 
                foreach (var savestate in game_savestates) {
-                       if (savestate.is_automatic ())
+                       if (savestate.is_automatic)
                                continue;
 
                        var savestate_name = savestate.get_name ();
@@ -648,7 +648,7 @@ public class Games.RetroRunner : Object, Runner {
                int autosaves_counter = 1;
 
                foreach (var savestate in game_savestates) {
-                       if (savestate.is_automatic ()) {
+                       if (savestate.is_automatic) {
                                if (autosaves_counter < MAX_AUTOSAVES)
                                        autosaves_counter++;
                                else
diff --git a/src/ui/savestate-listbox-row.vala b/src/ui/savestate-listbox-row.vala
index f1846b5e..a7fa8625 100644
--- a/src/ui/savestate-listbox-row.vala
+++ b/src/ui/savestate-listbox-row.vala
@@ -19,7 +19,7 @@ private class Games.SavestateListBoxRow : Gtk.ListBoxRow {
                set {
                        _savestate = value;
 
-                       if (savestate.is_automatic ())
+                       if (savestate.is_automatic)
                                name_label.label = _("Autosave");
                        else
                                name_label.label = savestate.get_name ();
diff --git a/src/ui/savestates-list.vala b/src/ui/savestates-list.vala
index 927e3894..cd3b0b53 100644
--- a/src/ui/savestates-list.vala
+++ b/src/ui/savestates-list.vala
@@ -238,7 +238,7 @@ private class Games.SavestatesList : Gtk.Box {
                        var savestate_row = list_child as SavestateListBoxRow;
                        var savestate = savestate_row.savestate;
 
-                       if (savestate.is_automatic ())
+                       if (savestate.is_automatic)
                                continue;
 
                        if (savestate.get_name () == entry_text) {
@@ -300,7 +300,8 @@ private class Games.SavestatesList : Gtk.Box {
                }
 
                delete_btn.sensitive = (state.selected_savestate != null);
-               rename_btn.sensitive = (state.selected_savestate != null && 
!state.selected_savestate.is_automatic ());
+               rename_btn.sensitive = (state.selected_savestate != null &&
+                                       !state.selected_savestate.is_automatic);
        }
 
        public bool on_key_press_event (uint keyval, Gdk.ModifierType state) {


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