context: /*
  
  Copyright (C) 2007 Martin Richards, Stephen Kennedy
  
  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 2 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, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
  
  /*
  How to enable a new setting:
   * Add a public serialisable field to the relevant settings class in this code file
   * Add a parameter to the public constructor, so that the UI-value can be stored in the object when saving settings
   * Add code to the object creation line in MakePrefs() in UserSettings.cs - this is where the UI passes it's
     settings state to new SettingsClass objects prior to saving to XML
   * Add code to read the deserialised value in UserSettings.cs:LoadPrefs
  */
  
  using System;
  using System.Collections.Generic;
  using System.Text;
  using System.Xml.Serialization;
  
  using System.IO;
  
  namespace WikiFunctions.AWBSettings
  {
      //mother class
      [Serializable, XmlRoot("AutoWikiBrowserPreferences")]
      public class UserPrefs
      {
          // the internal constructors are used during deserialisation or when a blank object is required
          public UserPrefs()
          {
              FindAndReplace = new FaRPrefs();
              Editprefs = new EditPrefs();
              List = new ListPrefs();
              SkipOptions = new SkipPrefs();
              General = new GeneralPrefs();
              Disambiguation = new DabPrefs();
              Module = new ModulePrefs();
              Logging = new LoggingPrefs();
          }
  
          // the public constructors are used to create an object with settings from the UI
          public UserPrefs(FaRPrefs mFaRPrefs, EditPrefs mEditprefs, ListPrefs mList, SkipPrefs mSkipOptions,
              GeneralPrefs mGeneral, DabPrefs mDisambiguation, ModulePrefs mModule, ExternalProgramPrefs mExternalProgram, LoggingPrefs mLogging, SpecialFilterPrefs mSpecial,
              Dictionary<string, Plugin.IAWBPlugin> plugins)
          {
              LanguageCode = Variables.LangCode;
              Project = Variables.Project;
              CustomProject = Variables.CustomProject;
              PHP5 = Variables.PHP5;
  
              FindAndReplace = mFaRPrefs;
              Editprefs = mEditprefs;
              List = mList;
              SkipOptions = mSkipOptions;
              General = mGeneral;
              Disambiguation = mDisambiguation;
              Module = mModule;
              ExternalProgram = mExternalProgram;
              Logging = mLogging;
              Special = mSpecial;
  
              foreach (KeyValuePair<string, Plugin.IAWBPlugin> a in plugins)
              {
                  Plugin.Add(new PluginPrefs(a.Key, a.Value.SaveSettings()));
              }
          }
  
          [XmlAttribute("xml:space")]
          public String SpacePreserve = "preserve";
  
          [XmlAttribute]
          public string Version = Tools.VersionString;
          public ProjectEnum Project = ProjectEnum.wikipedia;
          public LangCodeEnum LanguageCode = LangCodeEnum.en;
          public string CustomProject = "";
          public bool PHP5;
  
          public ListPrefs List;
          public FaRPrefs FindAndReplace;
          public EditPrefs Editprefs;
          public GeneralPrefs General;
          public SkipPrefs SkipOptions;
          public ModulePrefs Module;
          public ExternalProgramPrefs ExternalProgram = new ExternalProgramPrefs();
          public DabPrefs Disambiguation;
          public LoggingPrefs Logging;
          public SpecialFilterPrefs Special = new SpecialFilterPrefs();
  
          public List<PluginPrefs> Plugin = new List<PluginPrefs>();
  
          /// <summary>
          /// Loads the UserPrefs from the specified file
          /// </summary>
          /// <param name="file">File to load from</param>
          /// <returns>Loaded UserPrefs</returns>
          public static UserPrefs LoadPrefs(string file)
          {
              string settings;
              using (StreamReader f = new StreamReader(file, Encoding.UTF8))
              {
                  settings = f.ReadToEnd();
              }
  
              //test to see if it is an old AWB file
              if (settings.Contains("<projectlang proj="))
                  throw new Exception("This file uses old settings format unsupported by this version of AWB.");
  
              XmlSerializer xs = new XmlSerializer(typeof(UserPrefs), new [] { typeof(PrefsKeyPair) });
              return (UserPrefs)xs.Deserialize(new StringReader(settings));
          }
  
          /// <summary>
          /// Saves the UserPrefs to the specified file
          /// </summary>
          /// <param name="prefs">UserPrefs object to save</param>
          /// <param name="file">File to save to</param>
          public static void SavePrefs(UserPrefs prefs, string file)
          {
              try
              {
                  using (FileStream fStream = new FileStream(file, FileMode.Create))
                  {
                      List<Type> types = SavePluginSettings(prefs);
  
                      XmlSerializer xs = new XmlSerializer(typeof(UserPrefs), types.ToArray());
                      xs.Serialize(fStream, prefs);
                  }
              }
              catch (Exception ex) { ErrorHandler.Handle(ex); }
          }
  
          /// <summary>
          /// Saves the plugin Settings
          /// </summary>
          /// <param name="prefs">UserPrefs object</param>
          /// <returns>A list of the plugin types</returns>
          public static List<Type> SavePluginSettings(UserPrefs prefs)
          {
              List<Type> types = new List<Type>();
              /* Find out what types the plugins are using for their settings so we can 
                 add them to the Serializer. The plugin author must ensure s(he) is using
                 serializable types.*/
  
              foreach (PluginPrefs pl in prefs.Plugin)
              {
                  if ((pl.PluginSettings != null) && (pl.PluginSettings.Length >= 1))
                  {
                      foreach (object pl2 in pl.PluginSettings)
                      {
                          types.Add(pl2.GetType());
                      }
                  }
              }
              return types;
          }
      }
  
      //find and replace prefs
      [Serializable]
      public class FaRPrefs
      {
          internal FaRPrefs() { }
  
          /// <summary>
          /// Fill the object with settings from UI
          /// </summary>
          public FaRPrefs(Parse.FindandReplace findAndReplace,
              ReplaceSpecial.ReplaceSpecial replaceSpecial, SubstTemplates substTemplates)
          {
              IgnoreSomeText = findAndReplace.IgnoreLinks;
              IgnoreMoreText = findAndReplace.IgnoreMore;
              AppendSummary = findAndReplace.AppendToSummary;
              AfterOtherFixes = findAndReplace.AfterOtherFixes;
              Replacements = findAndReplace.GetList();
              AdvancedReps = replaceSpecial.GetRules();
  
              SubstTemplates = substTemplates.TemplateList;
              IncludeComments = substTemplates.IncludeComments;
              ExpandRecursively = substTemplates.ExpandRecursively;
              IgnoreUnformatted = substTemplates.IgnoreUnformatted;
          }
  
          public bool Enabled = false;
          public bool IgnoreSomeText = false;
          public bool IgnoreMoreText = false;
          public bool AppendSummary = true;
          public bool AfterOtherFixes = false;
          public List<Parse.Replacement> Replacements = new List<Parse.Replacement>();
  
          public List<ReplaceSpecial.IRule> AdvancedReps = new List<ReplaceSpecial.IRule>();
  
          public string[] SubstTemplates = new string[0];
          public bool IncludeComments = false;
          public bool ExpandRecursively = true;
          public bool IgnoreUnformatted = false;
      }
  
      [Serializable]
      public class ListPrefs
      {
  
gt:         internal ListPrefs() { }