[gnome-chess] Miscellaneous improvements to AIProfile



commit 7fd9c75c68d11fd347c56956a610b0dd03286364
Author: Michael Catanzaro <mike catanzaro gmail com>
Date:   Fri Aug 2 22:15:25 2013 -0500

    Miscellaneous improvements to AIProfile
    
    * Instead of public fields, use properties with private setters.
    * Change associated free functions into static class functions
    * Warn on error reading KeyFile

 src/ai-profile.vala  |  123 +++++++++++++++++++++++++-------------------------
 src/gnome-chess.vala |    2 +-
 2 files changed, 63 insertions(+), 62 deletions(-)
---
diff --git a/src/ai-profile.vala b/src/ai-profile.vala
index eedb650..bf6a97a 100644
--- a/src/ai-profile.vala
+++ b/src/ai-profile.vala
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2010-2013 Robert Ancell
+ * Copyright (C) 2013 Michael Catanzaro
  *
  * 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
@@ -10,80 +11,80 @@
 
 public class AIProfile
 {
-    public string name;
-    public string protocol;
-    public string binary;
-    public string path;
-    public string[] easy_args;
-    public string[] normal_args;
-    public string[] hard_args;
-    public string[] easy_options;
-    public string[] normal_options;
-    public string[] hard_options;
-}
-
-public List<AIProfile> load_ai_profiles (string filename)
-{
-   var profiles = new List<AIProfile> ();
+    public string name { get; private set; }
+    public string protocol { get; private set; }
+    public string binary { get; private set; }
+    public string path { get; private set; }
+    public string[] easy_args { get; private set; }
+    public string[] normal_args { get; private set; }
+    public string[] hard_args { get; private set; }
+    public string[] easy_options { get; private set; }
+    public string[] normal_options { get; private set; }
+    public string[] hard_options { get; private set; }
 
-   var file = new KeyFile ();
-   try
-   {
-       file.load_from_file (filename, KeyFileFlags.NONE);
-   }
-   catch (KeyFileError e)
-   {
-       warning ("Failed to load AI profiles: %s", e.message);
-       return profiles;   
-   }
-   catch (FileError e)
-   {
-       warning ("Failed to load AI profiles: %s", e.message);
-       return profiles;
-   }
+    public static List<AIProfile> load_ai_profiles (string filename)
+    {
+       var profiles = new List<AIProfile> ();
 
-   foreach (string name in file.get_groups ())
-   {
-       debug ("Loading AI profile %s", name);
-       var profile = new AIProfile ();
+       var file = new KeyFile ();
        try
        {
-           profile.name = name;
-           profile.protocol = file.get_value (name, "protocol");
-           profile.binary = file.get_value (name, "binary");
-
-           profile.easy_args = load_array (file, name, "arg", "easy");
-           profile.normal_args = load_array (file, name, "arg", "normal");
-           profile.hard_args = load_array (file, name, "arg", "hard");
-           profile.easy_options = load_array (file, name, "option", "easy");
-           profile.normal_options = load_array (file, name, "option", "normal");
-           profile.hard_options = load_array (file, name, "option", "hard");
+           file.load_from_file (filename, KeyFileFlags.NONE);
        }
        catch (KeyFileError e)
        {
-           continue;
+           warning ("Failed to load AI profiles: %s", e.message);
+           return profiles;
+       }
+       catch (FileError e)
+       {
+           warning ("Failed to load AI profiles: %s", e.message);
+           return profiles;
        }
 
-       var path = Environment.find_program_in_path (profile.binary);
-       if (path != null)
+       foreach (string name in file.get_groups ())
        {
-           profile.path = path;
-           profiles.append (profile);
+           debug ("Loading AI profile %s", name);
+           var profile = new AIProfile ();
+           try
+           {
+               profile.name = name;
+               profile.protocol = file.get_value (name, "protocol");
+               profile.binary = file.get_value (name, "binary");
+               profile.easy_args = load_array (file, name, "arg", "easy");
+               profile.normal_args = load_array (file, name, "arg", "normal");
+               profile.hard_args = load_array (file, name, "arg", "hard");
+               profile.easy_options = load_array (file, name, "option", "easy");
+               profile.normal_options = load_array (file, name, "option", "normal");
+               profile.hard_options = load_array (file, name, "option", "hard");
+           }
+           catch (KeyFileError e)
+           {
+               warning ("Error reading AI profiles: %s", e.message);
+               continue;
+           }
+
+           var path = Environment.find_program_in_path (profile.binary);
+           if (path != null)
+           {
+               profile.path = path;
+               profiles.append (profile);
+           }
        }
-   }
 
-   return profiles;
-}
+       return profiles;
+    }
 
-private string[] load_array (KeyFile file, string name, string type, string difficulty) throws KeyFileError
-{
-    int count = 0;
-    while (file.has_key (name, "%s-%s-%d".printf (type, difficulty, count)))
-        count++;
+    private static string[] load_array (KeyFile file, string name, string type, string difficulty) throws 
KeyFileError
+    {
+        int count = 0;
+        while (file.has_key (name, "%s-%s-%d".printf (type, difficulty, count)))
+            count++;
 
-    string[] options = new string[count];
-    for (var i = 0; i < count; i++)
-        options[i] = file.get_value (name, "%s-%s-%d".printf (type, difficulty, i));
+        string[] options = new string[count];
+        for (var i = 0; i < count; i++)
+            options[i] = file.get_value (name, "%s-%s-%d".printf (type, difficulty, i));
 
-    return options;
+        return options;
+    }
 }
diff --git a/src/gnome-chess.vala b/src/gnome-chess.vala
index 5abb3f3..932c4fd 100644
--- a/src/gnome-chess.vala
+++ b/src/gnome-chess.vala
@@ -197,7 +197,7 @@ public class Application : Gtk.Application
         settings.changed.connect (settings_changed_cb);
         settings_changed_cb (settings, "show-3d");
 
-        ai_profiles = load_ai_profiles (Path.build_filename (PKGDATADIR, "engines.conf", null));
+        ai_profiles = AIProfile.load_ai_profiles (Path.build_filename (PKGDATADIR, "engines.conf", null));
         foreach (var profile in ai_profiles)
             message ("Detected AI profile %s in %s", profile.name, profile.path);
 


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