[gnome-robots] Introduce Themes model.



commit 227180bd59b146c7a7b9d309f661185106144313
Author: Andrey Kutejko <andy128k gmail com>
Date:   Sun Aug 30 14:32:32 2020 +0200

    Introduce Themes model.

 src/file-list.vala  | 225 ----------------------------------------------------
 src/find-file.vala  |  46 -----------
 src/graphics.vala   |  12 +--
 src/meson.build     |   3 +-
 src/properties.vala |  54 +++++++------
 src/robots.vala     |  16 ----
 src/themes.vala     | 140 ++++++++++++++++++++++++++++++++
 7 files changed, 174 insertions(+), 322 deletions(-)
---
diff --git a/src/graphics.vala b/src/graphics.vala
index 9b59cab..5d39dfb 100644
--- a/src/graphics.vala
+++ b/src/graphics.vala
@@ -106,20 +106,12 @@ void load_bubble_graphics () throws Error {
  * Returns:
  * TRUE on success FALSE otherwise
  **/
-public void load_game_graphics () throws Error {
+public void load_game_graphics (string theme_path) throws Error {
     if (theme_preimage != null) {
         free_game_graphics ();
     }
 
-    var themedir = GLib.Path.build_filename (DATA_DIRECTORY, "themes");
-    var filename = games_find_similar_file (properties_theme_name (), themedir);
-
-    try {
-        theme_preimage = new GamesPreimage.from_file (filename);
-    } catch (Error e) {
-        filename = games_find_similar_file ("robots", themedir);
-        theme_preimage = new GamesPreimage.from_file (filename);
-    }
+    theme_preimage = new GamesPreimage.from_file (theme_path);
 
     load_bubble_graphics ();
 
diff --git a/src/meson.build b/src/meson.build
index 31bd238..8d415ed 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -12,8 +12,7 @@ sources = files(
     'config.vapi',
 
     'image-suffix-list.vala',
-    'file-list.vala',
-    'find-file.vala',
+    'themes.vala',
     'preimage.vala',
     'controls.vala',
     'game-config.vala',
diff --git a/src/properties.vala b/src/properties.vala
index e3673bb..3383e7a 100644
--- a/src/properties.vala
+++ b/src/properties.vala
@@ -49,7 +49,6 @@ struct Properties {
 }
 
 Dialog propbox = null;
-GamesFileList theme_list = null;
 Properties properties;
 
 /**
@@ -79,19 +78,19 @@ void apply_cb () {
 void pmap_selection (ComboBox combo) {
     TreeIter iter;
     if (combo.get_active_iter (out iter)) {
-        TreeModel model;
-        Value val;
+        Themes model = combo.get_model () as Themes;
 
-        model = combo.get_model ();
-        model.get_value (iter, 1, out val);
+        string theme_name;
+        string theme_path;
+        model.get_values (iter, out theme_name, out theme_path);
 
         /* FIXME: Should be de-suffixed. */
-        properties.themename = val.get_string ();
+        properties.themename = theme_name;
 
         conf_set_theme (properties.themename);
 
         try {
-            load_game_graphics ();
+            load_game_graphics (theme_path);
         } catch (Error e) {
             // TODO
         }
@@ -139,20 +138,20 @@ void fill_typemenu (ComboBoxText menu) {
     menu.set_active (properties.selected_config);
 }
 
+ComboBox create_theme_picker (Themes themes, string current_theme) {
+    var widget = new ComboBox.with_model (themes);
+    var renderer = new CellRendererText ();
+    widget.pack_start (renderer, true);
+    widget.add_attribute (renderer, "text", 0);
+
+    TreeIter? iter = themes.find_iter_by_name (current_theme);
+    if (iter != null) {
+        widget.set_active_iter (iter);
+    } else {
+        widget.set_active (0);
+    }
 
-/**
- * fills the listbox with pixmap names
- **/
-ComboBox make_theme_menu () {
-    var dir = Path.build_filename (DATA_DIRECTORY, "themes");
-    theme_list = new GamesFileList.images (dir);
-    theme_list.transform_basename ();
-
-    /* FIXME: Get rid of the bubbles images from the list (preferably by
-    * getting tid of the bubble pixmaps. */
-
-    return theme_list.create_widget (properties.themename,
-                                     GamesFileList.Flags.REMOVE_EXTENSION | 
GamesFileList.Flags.REPLACE_UNDERSCORES);
+    return widget;
 }
 
 void bg_color_callback (ColorChooser color_chooser) {
@@ -173,6 +172,8 @@ public string properties_theme_name () {
  * displays the properties dialog
  **/
 public void show_properties_dialog () {
+    var themes = get_themes ();
+
     if (propbox != null)
         return;
 
@@ -255,8 +256,8 @@ public void show_properties_dialog () {
     label.set_halign (Align.START);
     grid.attach (label, 0, 0, 1, 1);
 
-    var pmapmenu = make_theme_menu ();
-    pmapmenu.changed.connect ((combo) => pmap_selection(combo));
+    var pmapmenu = create_theme_picker (themes, properties.themename);
+    pmapmenu.changed.connect ((combo) => pmap_selection (combo));
     label.set_mnemonic_widget (pmapmenu);
     grid.attach (pmapmenu, 1, 0, 1, 1);
 
@@ -343,7 +344,14 @@ public void load_properties () throws Error {
     properties.sound            = settings.get_boolean (KEY_ENABLE_SOUND);
     properties.show_toolbar     = settings.get_boolean (KEY_SHOW_TOOLBAR);
 
-    load_game_graphics ();
+    var themes = get_themes ();
+    var iter = themes.find_best_match (properties.themename);
+
+    string theme_path;
+    themes.get_values (iter, out properties.themename, out theme_path);
+
+    load_game_graphics (theme_path);
+
     game_configs.set_current_index ((uint)properties.selected_config);
     keyboard_set (properties.keys);
 }
diff --git a/src/robots.vala b/src/robots.vala
index 3798934..4317067 100644
--- a/src/robots.vala
+++ b/src/robots.vala
@@ -388,22 +388,6 @@ void activate (Gtk.Application app) {
         app.quit ();
     }
 
-    try {
-        load_game_graphics ();
-    } catch (Error e) {
-        error ("%s", e.message);
-        /* Oops, no graphics, we probably haven't been installed properly. */
-        var errordialog = new MessageDialog.with_markup (window,
-                                                         DialogFlags.MODAL,
-                                                         MessageType.ERROR,
-                                                         ButtonsType.OK,
-                                                         "<b>%s</b>\n\n%s",
-                                                         _("Some graphics files are missing or corrupt."),
-                                                         _("The program Robots was unable to load all the 
necessary graphics files. Please check that the program is installed correctly."));
-        errordialog.run ();
-        app.quit ();
-    }
-
     init_keyboard ();
     game.init_game ();
 
diff --git a/src/themes.vala b/src/themes.vala
new file mode 100644
index 0000000..702421d
--- /dev/null
+++ b/src/themes.vala
@@ -0,0 +1,140 @@
+/*
+ * Copyright 2020 Andrey Kutejko <andy128k gmail com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * For more details see the file COPYING.
+ */
+
+public Themes themes = null;
+
+public Themes get_themes () {
+    if (themes == null) {
+        themes = Themes.from_data_directory ();
+    }
+    return themes;
+}
+
+public class Themes : Gtk.ListStore {
+
+    public enum Column {
+        DISPLAY = 0,
+        NAME,
+        PATH,
+    }
+
+    public Themes () {
+        set_column_types ({ Type.STRING, Type.STRING, Type.STRING });
+    }
+
+    public void add (string path) {
+        var filename = Path.get_basename (path);
+        var display = remove_suffix (filename).replace ("_", " ");
+
+        Gtk.TreeIter iter;
+        append (out iter);
+
+        set_value (iter, Column.DISPLAY, display);
+        set_value (iter, Column.NAME, filename);
+        set_value (iter, Column.PATH, path);
+    }
+
+    public static Themes from_directory (string directory) {
+        var themes = new Themes ();
+        try {
+            var dir = Dir.open (directory);
+            string? filename;
+            while ((filename = dir.read_name ()) != null) {
+                if (ImageSuffixList.has_image_suffix (filename)) {
+                    var fullname = Path.build_filename (directory, filename);
+                    if (FileUtils.test (fullname, FileTest.IS_REGULAR)) {
+                        themes.add (fullname);
+                    }
+                }
+            }
+        } catch (FileError e) {
+            warning ("Themes.from_data_dir: %s.", e.message);
+        }
+        return themes;
+    }
+
+    public static Themes from_data_directory () {
+        var directory = GLib.Path.build_filename (DATA_DIRECTORY, "themes");
+        return from_directory (directory);
+    }
+
+    public Gtk.TreeIter? find_iter_by_name (string name) {
+        Gtk.TreeIter iter;
+        if (get_iter_first (out iter)) {
+            do {
+                Value val;
+                get_value (iter, Column.NAME, out val);
+                if (val == name) {
+                    return iter;
+                }
+            } while (iter_next (ref iter));
+        }
+        return null;
+    }
+
+    public string? find_path_by_name (string name) {
+        var iter = find_iter_by_name (name);
+        if (iter != null) {
+            Value path;
+            get_value (iter, Column.PATH, out path);
+            return path.get_string ();
+        } else {
+            return null;
+        }
+    }
+
+    public void get_values (Gtk.TreeIter iter, out string name, out string path) {
+        Value name_value;
+        Value path_value;
+        get_value (iter, Column.NAME, out name_value);
+        get_value (iter, Column.PATH, out path_value);
+        name = name_value.get_string ();
+        path = path_value.get_string ();
+    }
+
+    public Gtk.TreeIter find_best_match (string name) throws Error {
+        const string DEFAULT_THEME = "robots.svg";
+
+        var iter = find_iter_by_name (name);
+        if (iter != null) {
+            return iter;
+        }
+
+        iter = find_iter_by_name (DEFAULT_THEME);
+        if (iter != null) {
+            return iter;
+        }
+
+        if (themes.get_iter_first (out iter)) {
+            return iter;
+        } else {
+            throw new FileError.NOENT ("No theme was found.");
+        }
+    }
+}
+
+string remove_suffix (string filename) {
+    var s = filename.last_index_of_char ('.');
+    if (s >= 0) {
+        return filename.substring (0, s);
+    } else {
+        return filename;
+    }
+}
+


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