[latexila] Separate default and personal build tools



commit dd81d9ec83f9d6027208235fc6f098fe4548d5d7
Author: SÃbastien Wilmet <swilmet src gnome org>
Date:   Tue Jul 17 01:24:57 2012 +0200

    Separate default and personal build tools
    
    Now only the personal build tools are used. The UI will be adapted to
    use the default build tools too (and configure this).
    
    The reset button in the build tools preferences is gone, since it no
    longer makes sense.

 src/build_tool_dialog.vala       |    6 +-
 src/build_tools.vala             |  298 ++++++++++++++++++++------------------
 src/build_tools_preferences.vala |   41 +----
 src/latexila.vala                |    2 +-
 src/main_window_build_tools.vala |    8 +-
 5 files changed, 170 insertions(+), 185 deletions(-)
---
diff --git a/src/build_tool_dialog.vala b/src/build_tool_dialog.vala
index 69e7161..ffafee0 100644
--- a/src/build_tool_dialog.vala
+++ b/src/build_tool_dialog.vala
@@ -98,8 +98,8 @@ private class BuildToolDialog : Dialog
     {
         return_val_if_fail (_instance != null, false);
 
-        BuildTools build_tools = BuildTools.get_default ();
-        BuildTool? build_tool = build_tools.get_by_id (build_tool_num);
+        PersonalBuildTools build_tools = PersonalBuildTools.get_default ();
+        BuildTool? build_tool = build_tools.get_build_tool (build_tool_num);
 
         return_val_if_fail (build_tool != null, false);
 
@@ -133,7 +133,7 @@ private class BuildToolDialog : Dialog
             BuildTool new_build_tool = _instance.retrieve_build_tool ();
             new_build_tool.enabled = true;
 
-            BuildTools build_tools = BuildTools.get_default ();
+            PersonalBuildTools build_tools = PersonalBuildTools.get_default ();
             build_tools.add (new_build_tool);
         }
 
diff --git a/src/build_tools.vala b/src/build_tools.vala
index 4b2c882..12b4a11 100644
--- a/src/build_tools.vala
+++ b/src/build_tools.vala
@@ -75,10 +75,8 @@ public struct BuildTool
     }
 }
 
-public class BuildTools : GLib.Object
+public abstract class BuildTools : GLib.Object
 {
-    private static BuildTools _instance = null;
-
     private static string[] _post_processor_names =
     {
         // Same order as the PostProcessorType enum.
@@ -89,8 +87,11 @@ public class BuildTools : GLib.Object
         "rubber"
     };
 
-    private Gee.LinkedList<BuildTool?> _build_tools;
-    private bool _modified = false;
+    protected Gee.LinkedList<BuildTool?> _build_tools;
+
+    // Possible locations for the XML file, containaing the build tools.
+    // The order is important: the first file is tried, then the second, and so on.
+    protected Gee.List<File> _xml_files = new Gee.LinkedList<File> ();
 
     // Used during the XML file parsing to load the build tools.
     private BuildTool _cur_tool;
@@ -98,32 +99,14 @@ public class BuildTools : GLib.Object
 
     public signal void modified ();
 
-    // Singleton
-    private BuildTools ()
-    {
-        int nb_post_processors = PostProcessorType.N_POST_PROCESSORS;
-        return_if_fail (_post_processor_names.length == nb_post_processors);
-
-        modified.connect (() => _modified = true);
-
-        load ();
-    }
-
-    public static BuildTools get_default ()
-    {
-        if (_instance == null)
-            _instance = new BuildTools ();
-
-        return _instance;
-    }
-
-    public BuildTool? get_by_id (int id)
+    public BuildTool? get_build_tool (int tool_num)
     {
-        return_val_if_fail (0 <= id && id < _build_tools.size, null);
+        return_val_if_fail (0 <= tool_num && tool_num < _build_tools.size, null);
 
-        return _build_tools[id];
+        return _build_tools[tool_num];
     }
 
+    // Support the foreach loop
     public Gee.Iterator<BuildTool?> iterator ()
     {
         return _build_tools.iterator ();
@@ -134,118 +117,30 @@ public class BuildTools : GLib.Object
         return _build_tools.size == 0;
     }
 
-    public void move_up (int num)
-    {
-        return_if_fail (num > 0);
-        swap (num, num - 1);
-    }
-
-    public void move_down (int num)
-    {
-        return_if_fail (num < _build_tools.size - 1);
-        swap (num, num + 1);
-    }
-
-    private void swap (int num1, int num2)
-    {
-        BuildTool tool = _build_tools[num1];
-        _build_tools.remove_at (num1);
-        _build_tools.insert (num2, tool);
-        modified ();
-    }
-
-    public void delete (int num)
-    {
-        return_if_fail (0 <= num && num < _build_tools.size);
-
-        _build_tools.remove_at (num);
-        modified ();
-    }
-
-    public void add (BuildTool tool)
-    {
-        insert (_build_tools.size, tool);
-    }
-
-    public void insert (int pos, BuildTool tool)
-    {
-        return_if_fail (0 <= pos && pos <= _build_tools.size);
-
-        _build_tools.insert (pos, tool);
-        modified ();
-    }
-
-    public void update (int num, BuildTool tool)
+    public static PostProcessorType? get_post_processor_type_from_name (string name)
     {
-        return_if_fail (0 <= num && num < _build_tools.size);
-
-        BuildTool current_tool = _build_tools[num];
-
-        if (! is_equal (current_tool, tool))
+        for (int type = 0 ; type < PostProcessorType.N_POST_PROCESSORS ; type++)
         {
-            _build_tools.remove_at (num);
-            _build_tools.insert (num, tool);
-            modified ();
+            if (_post_processor_names[type] == name)
+                return (PostProcessorType) type;
         }
-    }
 
-    public void reset_all ()
-    {
-        File file = get_user_config_file ();
-        if (file.query_exists ())
-            Utils.delete_file (file);
-
-        load ();
-        modified ();
+        return_val_if_reached (null);
     }
 
-    private bool is_equal (BuildTool tool1, BuildTool tool2)
+    public static string? get_post_processor_name_from_type (PostProcessorType type)
     {
-        if (tool1.enabled != tool2.enabled
-            || tool1.label != tool2.label
-            || tool1.get_description () != tool2.get_description ()
-            || tool1.extensions != tool2.extensions
-            || tool1.icon != tool2.icon
-            || tool1.files_to_open != tool2.files_to_open
-            || tool1.jobs.size != tool2.jobs.size)
-        {
-            return false;
-        }
-
-        for (int job_num = 0 ; job_num < tool1.jobs.size ; job_num++)
-        {
-            BuildJob job1 = tool1.jobs[job_num];
-            BuildJob job2 = tool2.jobs[job_num];
-
-            if (job1.command != job2.command
-                || job1.post_processor != job2.post_processor)
-            {
-                return false;
-            }
-        }
+        return_val_if_fail (type != PostProcessorType.N_POST_PROCESSORS, null);
 
-        return true;
+        return _post_processor_names[type];
     }
 
-    private void load ()
+    protected void load ()
     {
         _build_tools = new Gee.LinkedList<BuildTool?> ();
 
-        // First, try to load the user config file if it exists.
-        // Otherwise try to load the default file (from most desirable to least desirable,
-        // depending of the current locale).
-
-        File[] files = {};
-        files += get_user_config_file ();
-
-        unowned string[] language_names = Intl.get_language_names ();
-        foreach (string language_name in language_names)
-        {
-            files += File.new_for_path (Path.build_filename (Config.DATA_DIR,
-                "build_tools", language_name, "build_tools.xml"));
-        }
-
-        foreach (File file in files)
+        // Try to load the XML file from the most desirable to least desirable location.
+        foreach (File file in _xml_files)
         {
             if (! file.query_exists ())
                 continue;
@@ -383,6 +278,119 @@ public class BuildTools : GLib.Object
                 break;
         }
     }
+}
+
+public class DefaultBuildTools : BuildTools
+{
+    private static DefaultBuildTools _instance = null;
+
+    private DefaultBuildTools ()
+    {
+        unowned string[] language_names = Intl.get_language_names ();
+        foreach (string language_name in language_names)
+        {
+            string path = Path.build_filename (Config.DATA_DIR, "build_tools",
+                language_name, "build_tools.xml");
+
+            _xml_files.add (File.new_for_path (path));
+        }
+
+        load ();
+    }
+
+    public static DefaultBuildTools get_default ()
+    {
+        if (_instance == null)
+            _instance = new DefaultBuildTools ();
+
+        return _instance;
+    }
+}
+
+public class PersonalBuildTools : BuildTools
+{
+    private static PersonalBuildTools _instance = null;
+
+    private bool _modified = false;
+
+    private PersonalBuildTools ()
+    {
+        _xml_files.add (get_user_config_file ());
+        load ();
+
+        modified.connect (() => _modified = true);
+    }
+
+    public static PersonalBuildTools get_default ()
+    {
+        if (_instance == null)
+            _instance = new PersonalBuildTools ();
+
+        return _instance;
+    }
+
+    public void move_up (int tool_num)
+    {
+        return_if_fail (tool_num > 0);
+        swap (tool_num, tool_num - 1);
+    }
+
+    public void move_down (int tool_num)
+    {
+        return_if_fail (tool_num < _build_tools.size - 1);
+        swap (tool_num, tool_num + 1);
+    }
+
+    private void swap (int tool_num1, int tool_num2)
+    {
+        BuildTool tool = _build_tools[tool_num1];
+        _build_tools.remove_at (tool_num1);
+        _build_tools.insert (tool_num2, tool);
+        modified ();
+    }
+
+    public void delete (int tool_num)
+    {
+        return_if_fail (0 <= tool_num && tool_num < _build_tools.size);
+
+        _build_tools.remove_at (tool_num);
+        modified ();
+    }
+
+    public void add (BuildTool tool)
+    {
+        insert (_build_tools.size, tool);
+    }
+
+    public void insert (int pos, BuildTool tool)
+    {
+        return_if_fail (0 <= pos && pos <= _build_tools.size);
+
+        _build_tools.insert (pos, tool);
+        modified ();
+    }
+
+    public void update (int num, BuildTool tool)
+    {
+        return_if_fail (0 <= num && num < _build_tools.size);
+
+        BuildTool current_tool = _build_tools[num];
+
+        if (! is_equal (current_tool, tool))
+        {
+            _build_tools.remove_at (num);
+            _build_tools.insert (num, tool);
+            modified ();
+        }
+    }
+
+    private File get_user_config_file ()
+    {
+        string path = Path.build_filename (Environment.get_user_config_dir (),
+            "latexila", "build_tools.xml");
+
+        return File.new_for_path (path);
+    }
 
     public void save ()
     {
@@ -422,29 +430,31 @@ public class BuildTools : GLib.Object
         Utils.save_file (file, content, true);
     }
 
-    private File get_user_config_file ()
-    {
-        string path = Path.build_filename (Environment.get_user_config_dir (),
-            "latexila", "build_tools.xml");
-
-        return File.new_for_path (path);
-    }
-
-    public static PostProcessorType? get_post_processor_type_from_name (string name)
+    private bool is_equal (BuildTool tool1, BuildTool tool2)
     {
-        for (int type = 0 ; type < PostProcessorType.N_POST_PROCESSORS ; type++)
+        if (tool1.enabled != tool2.enabled
+            || tool1.label != tool2.label
+            || tool1.get_description () != tool2.get_description ()
+            || tool1.extensions != tool2.extensions
+            || tool1.icon != tool2.icon
+            || tool1.files_to_open != tool2.files_to_open
+            || tool1.jobs.size != tool2.jobs.size)
         {
-            if (_post_processor_names[type] == name)
-                return (PostProcessorType) type;
+            return false;
         }
 
-        return_val_if_reached (null);
-    }
+        for (int job_num = 0 ; job_num < tool1.jobs.size ; job_num++)
+        {
+            BuildJob job1 = tool1.jobs[job_num];
+            BuildJob job2 = tool2.jobs[job_num];
 
-    public static string? get_post_processor_name_from_type (PostProcessorType type)
-    {
-        return_val_if_fail (type != PostProcessorType.N_POST_PROCESSORS, null);
+            if (job1.command != job2.command
+                || job1.post_processor != job2.post_processor)
+            {
+                return false;
+            }
+        }
 
-        return _post_processor_names[type];
+        return true;
     }
 }
diff --git a/src/build_tools_preferences.vala b/src/build_tools_preferences.vala
index fdc3ae0..f06a253 100644
--- a/src/build_tools_preferences.vala
+++ b/src/build_tools_preferences.vala
@@ -78,7 +78,6 @@ public class BuildToolsPreferences : GLib.Object
         toolbar.insert (get_remove_button (), -1);
         toolbar.insert (get_up_button (), -1);
         toolbar.insert (get_down_button (), -1);
-        toolbar.insert (get_reset_button (), -1);
 
         toolbar.set_icon_size (IconSize.MENU);
         toolbar.set_style (ToolbarStyle.ICONS);
@@ -152,8 +151,8 @@ public class BuildToolsPreferences : GLib.Object
             _list_store.set (iter, BuildToolColumn.ENABLED, enabled);
 
             int num = int.parse (path_string);
-            BuildTools build_tools = BuildTools.get_default ();
-            BuildTool? build_tool = build_tools.get_by_id (num);
+            PersonalBuildTools build_tools = PersonalBuildTools.get_default ();
+            BuildTool? build_tool = build_tools.get_build_tool (num);
             return_if_fail (build_tool != null);
 
             build_tool.enabled = enabled;
@@ -205,8 +204,8 @@ public class BuildToolsPreferences : GLib.Object
             if (selected_row < 0)
                 return;
 
-            BuildTools build_tools = BuildTools.get_default ();
-            BuildTool? tool = build_tools.get_by_id (selected_row);
+            PersonalBuildTools build_tools = PersonalBuildTools.get_default ();
+            BuildTool? tool = build_tools.get_build_tool (selected_row);
             return_if_fail (tool != null);
 
             tool.enabled = false;
@@ -267,7 +266,7 @@ public class BuildToolsPreferences : GLib.Object
             if (dialog.run () == ResponseType.YES)
             {
                 _list_store.remove (iter);
-                BuildTools.get_default ().delete (selected_row);
+                PersonalBuildTools.get_default ().delete (selected_row);
             }
 
             dialog.destroy ();
@@ -317,7 +316,7 @@ public class BuildToolsPreferences : GLib.Object
                 if (Utils.tree_model_iter_prev (_list_store, ref iter_up))
                 {
                     _list_store.swap (iter_selected, iter_up);
-                    BuildTools.get_default ().move_up (selected_row);
+                    PersonalBuildTools.get_default ().move_up (selected_row);
 
                     // Force the 'changed' signal on the selection to be emitted
                     select.changed ();
@@ -372,7 +371,7 @@ public class BuildToolsPreferences : GLib.Object
                 if (_list_store.iter_next (ref iter_down))
                 {
                     _list_store.swap (iter_selected, iter_down);
-                    BuildTools.get_default ().move_down (selected_row);
+                    PersonalBuildTools.get_default ().move_down (selected_row);
 
                     // Force the 'changed' signal on the selection to be emitted
                     select.changed ();
@@ -383,35 +382,11 @@ public class BuildToolsPreferences : GLib.Object
         return down_button;
     }
 
-    private ToolButton get_reset_button ()
-    {
-        ToolButton reset_button = new ToolButton (null, null);
-        // TODO use the clear symbolic icon when it is available
-        reset_button.set_icon_name ("edit-delete-symbolic");
-        reset_button.set_tooltip_text (_("Reset all the build tools"));
-
-        reset_button.clicked.connect (() =>
-        {
-            Dialog dialog = Utils.get_reset_all_confirm_dialog (_dialog,
-                _("Do you really want to reset all build tools?"));
-
-            if (dialog.run () == ResponseType.YES)
-            {
-                BuildTools.get_default ().reset_all ();
-                update_list_store ();
-            }
-
-            dialog.destroy ();
-        });
-
-        return reset_button;
-    }
-
     private void update_list_store ()
     {
         _list_store.clear ();
 
-        foreach (BuildTool tool in BuildTools.get_default ())
+        foreach (BuildTool tool in PersonalBuildTools.get_default ())
         {
             string description = Markup.escape_text (tool.get_description ());
 
diff --git a/src/latexila.vala b/src/latexila.vala
index 41626af..320620a 100644
--- a/src/latexila.vala
+++ b/src/latexila.vala
@@ -47,7 +47,7 @@ public class Latexila : Gtk.Application
         {
             hold ();
             Projects.get_default ().save ();
-            BuildTools.get_default ().save ();
+            PersonalBuildTools.get_default ().save ();
             MostUsedSymbols.get_default ().save ();
             release ();
         });
diff --git a/src/main_window_build_tools.vala b/src/main_window_build_tools.vala
index 2187f52..426d85d 100644
--- a/src/main_window_build_tools.vala
+++ b/src/main_window_build_tools.vala
@@ -91,7 +91,7 @@ public class MainWindowBuildTools
         ui_manager.insert_action_group (_dynamic_action_group, 0);
         update_menu ();
 
-        BuildTools build_tools = BuildTools.get_default ();
+        PersonalBuildTools build_tools = PersonalBuildTools.get_default ();
         build_tools.modified.connect (() => update_menu ());
     }
 
@@ -125,7 +125,7 @@ public class MainWindowBuildTools
         }
 
         int tool_num = 0;
-        foreach (BuildTool tool in BuildTools.get_default ())
+        foreach (BuildTool tool in PersonalBuildTools.get_default ())
         {
             if (! tool.enabled)
             {
@@ -174,7 +174,7 @@ public class MainWindowBuildTools
             _dynamic_action_group.remove_action (action);
         }
 
-        BuildTools build_tools = BuildTools.get_default ();
+        PersonalBuildTools build_tools = PersonalBuildTools.get_default ();
 
         if (build_tools.is_empty ())
             _menu_ui_id = 0;
@@ -226,7 +226,7 @@ public class MainWindowBuildTools
         string[] name = action.name.split ("_");
         int tool_num = int.parse (name[1]);
 
-        BuildTool? tool = BuildTools.get_default ().get_by_id (tool_num);
+        BuildTool? tool = PersonalBuildTools.get_default ().get_build_tool (tool_num);
         return_if_fail (tool != null);
 
         if (! tool.has_jobs ())



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