[gnome-games/wip/exalm/savestates: 9/20] savestate: Make name a property



commit 71312a0e1269b7614fbd53cb9354f3722f08e004
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Sun Jan 26 00:01:42 2020 +0500

    savestate: Make name a property

 src/core/savestate.vala           | 49 ++++++++++-----------------------------
 src/retro/retro-runner.vala       |  3 +--
 src/ui/savestate-listbox-row.vala |  4 ++--
 src/ui/savestates-list.vala       |  4 ++--
 4 files changed, 17 insertions(+), 43 deletions(-)
---
diff --git a/src/core/savestate.vala b/src/core/savestate.vala
index 5c3f717d..40e43e32 100644
--- a/src/core/savestate.vala
+++ b/src/core/savestate.vala
@@ -5,6 +5,7 @@ public class Games.Savestate : Object {
        // 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; }
+       public string name { get; set; }
 
        private static Savestate load (Platform platform, string path) {
                var type = platform.get_savestate_type ();
@@ -40,37 +41,6 @@ public class Games.Savestate : Object {
                return metadata;
        }
 
-       public string? get_name () {
-               var metadata = get_metadata ();
-
-               try {
-                       var is_automatic = metadata.get_boolean ("Metadata", "Automatic");
-
-                       if (is_automatic)
-                               return null;
-                       else
-                               return metadata.get_string ("Metadata", "Name");
-               }
-               catch (KeyFileError e) {
-                       critical ("Failed to get name from metadata file for snapshot at %s: %s", path, 
e.message);
-                       return null;
-               }
-       }
-
-       public void set_name (string name) {
-               var metadata = new KeyFile ();
-               var metadata_file_path = Path.build_filename (path, "metadata");
-
-               try {
-                       metadata.load_from_file (metadata_file_path, KeyFileFlags.NONE);
-                       metadata.set_string ("Metadata", "Name", name);
-                       metadata.save_to_file (metadata_file_path);
-               }
-               catch (Error e) {
-                       critical ("Failed to set name in metadata file for snapshot at %s: %s", path, 
e.message);
-               }
-       }
-
        public DateTime? get_creation_date () {
                var metadata = get_metadata ();
 
@@ -198,25 +168,33 @@ public class Games.Savestate : Object {
        public void set_metadata_automatic (DateTime creation_date, string platform, string core, double 
aspect_ratio) throws Error {
                is_automatic = true;
 
-               set_metadata (null, creation_date, platform, core, aspect_ratio);
+               set_metadata (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 {
                is_automatic = false;
+               this.name = name;
 
-               set_metadata (name, creation_date, platform, core, aspect_ratio);
+               set_metadata (creation_date, platform, core, aspect_ratio);
        }
 
        protected virtual void load_metadata (KeyFile keyfile) throws KeyFileError {
                is_automatic = keyfile.get_boolean ("Metadata", "Automatic");
+
+               if (is_automatic)
+                       name = null;
+               else
+                       name = keyfile.get_string ("Metadata", "Name");
        }
 
        protected virtual void save_metadata (KeyFile keyfile) {
                keyfile.set_boolean ("Metadata", "Automatic", is_automatic);
+               if (name != null)
+                       keyfile.set_string ("Metadata", "Name", name);
        }
 
-       private void set_metadata (string? name, DateTime creation_date,
+       private void set_metadata (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);
@@ -225,9 +203,6 @@ public class Games.Savestate : Object {
                if (metadata_file.query_exists ())
                        metadata_file.@delete ();
 
-               if (name != null)
-                       metadata.set_string ("Metadata", "Name", name);
-
                metadata.set_string ("Metadata", "Creation Date", creation_date.to_string ());
                metadata.set_string ("Metadata", "Platform", platform);
                metadata.set_string ("Metadata", "Core", core);
diff --git a/src/retro/retro-runner.vala b/src/retro/retro-runner.vala
index 2c7faebe..a9b77815 100644
--- a/src/retro/retro-runner.vala
+++ b/src/retro/retro-runner.vala
@@ -617,10 +617,9 @@ public class Games.RetroRunner : Object, Runner {
                        if (savestate.is_automatic)
                                continue;
 
-                       var savestate_name = savestate.get_name ();
                        MatchInfo match_info = null;
 
-                       if (regex.match (savestate_name, 0, out match_info)) {
+                       if (regex.match (savestate.name, 0, out match_info)) {
                                var number = match_info.fetch (1);
                                list.prepend (int.parse (number));
                        }
diff --git a/src/ui/savestate-listbox-row.vala b/src/ui/savestate-listbox-row.vala
index a7fa8625..18330bcc 100644
--- a/src/ui/savestate-listbox-row.vala
+++ b/src/ui/savestate-listbox-row.vala
@@ -22,7 +22,7 @@ private class Games.SavestateListBoxRow : Gtk.ListBoxRow {
                        if (savestate.is_automatic)
                                name_label.label = _("Autosave");
                        else
-                               name_label.label = savestate.get_name ();
+                               name_label.label = savestate.name;
 
                        var creation_date = savestate.get_creation_date ();
                        var date_format = get_date_format (creation_date);
@@ -87,7 +87,7 @@ private class Games.SavestateListBoxRow : Gtk.ListBoxRow {
 
        public void set_name (string name) {
                name_label.label = name;
-               savestate.set_name (name);
+               savestate.name = name;
        }
 
        public void reveal () {
diff --git a/src/ui/savestates-list.vala b/src/ui/savestates-list.vala
index cd3b0b53..99b4d6cf 100644
--- a/src/ui/savestates-list.vala
+++ b/src/ui/savestates-list.vala
@@ -191,7 +191,7 @@ private class Games.SavestatesList : Gtk.Box {
 
                ensure_row_is_visible (selected_row);
 
-               rename_entry.text = state.selected_savestate.get_name ();
+               rename_entry.text = state.selected_savestate.name;
                rename_popover.relative_to = selected_row;
                rename_popover.popup ();
        }
@@ -241,7 +241,7 @@ private class Games.SavestatesList : Gtk.Box {
                        if (savestate.is_automatic)
                                continue;
 
-                       if (savestate.get_name () == entry_text) {
+                       if (savestate.name == entry_text) {
                                rename_entry.get_style_context ().add_class ("error");
                                rename_popover_btn.sensitive = false;
                                rename_error_label.label = _("A snapshot with this name already exists");


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