2001-06-28 04:19:25 +04:00
|
|
|
/* -*- Mode: C#; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
2004-04-19 02:14:17 +04:00
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
2001-06-28 04:19:25 +04:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
2004-04-19 02:14:17 +04:00
|
|
|
* License.
|
2001-06-28 04:19:25 +04:00
|
|
|
*
|
|
|
|
* The Original Code is Manticore.
|
|
|
|
*
|
2004-04-19 02:14:17 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Silverstone Interactive.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2001
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
2001-06-28 04:19:25 +04:00
|
|
|
*
|
|
|
|
* Contributor(s):
|
2004-04-19 02:14:17 +04:00
|
|
|
* Ben Goodger <ben@netscape.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-06-28 04:19:25 +04:00
|
|
|
*
|
2004-04-19 02:14:17 +04:00
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2001-06-28 04:19:25 +04:00
|
|
|
|
2001-06-30 05:11:21 +04:00
|
|
|
namespace Silverstone.Manticore.Core
|
2001-06-28 04:19:25 +04:00
|
|
|
{
|
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
|
|
|
|
using System.IO;
|
|
|
|
using System.Xml;
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
public class Preferences
|
|
|
|
{
|
|
|
|
XmlDocument mDefaultsDocument;
|
|
|
|
XmlDocument mPrefsDocument;
|
|
|
|
|
|
|
|
public Preferences()
|
|
|
|
{
|
|
|
|
mDefaultsDocument = new XmlDocument();
|
|
|
|
mPrefsDocument = new XmlDocument();
|
|
|
|
}
|
|
|
|
|
2001-07-02 11:07:08 +04:00
|
|
|
public void InitializeDefaults()
|
2001-06-28 04:19:25 +04:00
|
|
|
{
|
|
|
|
// Do we ever want to support multiple defaults files? For now, no.
|
2001-07-02 11:07:08 +04:00
|
|
|
// XXX need a better place for this file.
|
2001-07-03 02:54:41 +04:00
|
|
|
ReadDocument(@"defaults\default-prefs.xml", mDefaultsDocument);
|
2001-06-28 04:19:25 +04:00
|
|
|
}
|
|
|
|
|
2001-06-30 05:11:21 +04:00
|
|
|
private void ReadDocument(String aFile, XmlDocument aDocument)
|
|
|
|
{
|
|
|
|
XmlTextReader reader = new XmlTextReader(aFile);
|
|
|
|
reader.WhitespaceHandling = WhitespaceHandling.None;
|
|
|
|
reader.MoveToContent();
|
|
|
|
String xml = reader.ReadOuterXml();
|
|
|
|
reader.Close();
|
|
|
|
aDocument.LoadXml(xml);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called once at Startup
|
2001-06-28 04:19:25 +04:00
|
|
|
public void LoadPreferencesFile(String aFile)
|
|
|
|
{
|
2001-06-30 05:11:21 +04:00
|
|
|
ReadDocument(aFile, mPrefsDocument);
|
|
|
|
|
|
|
|
mPrefsDocument.NodeChanged += new XmlNodeChangedEventHandler(OnNodeChanged);
|
|
|
|
mPrefsDocument.NodeRemoved += new XmlNodeChangedEventHandler(OnNodeRemoved);
|
|
|
|
mPrefsDocument.NodeInserted += new XmlNodeChangedEventHandler(OnNodeInserted);
|
|
|
|
}
|
|
|
|
|
2001-07-02 11:07:08 +04:00
|
|
|
public void LoadUserPreferences()
|
|
|
|
{
|
2001-07-03 02:54:41 +04:00
|
|
|
String manticoreAppData = FileLocator.GetManticorePath("AppData");
|
|
|
|
String prefPath = FileLocator.GetManticorePath("UserPrefs");
|
|
|
|
try {
|
|
|
|
LoadPreferencesFile(prefPath);
|
|
|
|
}
|
|
|
|
catch (XmlException) {
|
|
|
|
// Something went wrong, we'll just assume a malformed or non-existant
|
|
|
|
// preferences file, blow it away and insert a new one. Could potentially
|
|
|
|
// be dangerous.
|
|
|
|
try {
|
|
|
|
File.Copy(@"defaults\user-prefs.xml", prefPath, true);
|
|
|
|
}
|
|
|
|
catch (DirectoryNotFoundException) {
|
|
|
|
Directory.CreateDirectory(manticoreAppData);
|
|
|
|
File.Copy(@"defaults\user-prefs.xml", prefPath, true);
|
|
|
|
}
|
|
|
|
LoadPreferencesFile(prefPath);
|
|
|
|
}
|
2001-07-02 11:07:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void FlushUserPreferences()
|
|
|
|
{
|
2001-07-03 02:54:41 +04:00
|
|
|
FlushPreferencesFile(FileLocator.GetManticorePath("UserPrefs"));
|
2001-07-02 11:07:08 +04:00
|
|
|
}
|
|
|
|
|
2001-06-30 05:11:21 +04:00
|
|
|
public void OnNodeChanged(object sender, XmlNodeChangedEventArgs e)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnNodeRemoved(object sender, XmlNodeChangedEventArgs e)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnNodeInserted(object sender, XmlNodeChangedEventArgs e)
|
|
|
|
{
|
2001-06-28 04:19:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void FlushPreferencesFile(String aFile)
|
|
|
|
{
|
|
|
|
UTF8Encoding enc = new UTF8Encoding();
|
|
|
|
XmlTextWriter writer = new XmlTextWriter(aFile, enc);
|
|
|
|
writer.Formatting = Formatting.Indented;
|
|
|
|
mPrefsDocument.WriteTo(writer);
|
|
|
|
writer.Flush();
|
|
|
|
}
|
2001-06-30 05:11:21 +04:00
|
|
|
|
|
|
|
//
|
|
|
|
// The Manticore preferences file takes the following (XML) format:
|
|
|
|
//
|
|
|
|
// <preferences>
|
|
|
|
// <foopy>
|
|
|
|
// <noopy type="int" value="42">
|
|
|
|
// <noo type="bool" value="true"/>
|
|
|
|
// <goo type="string" value="goats"/>
|
|
|
|
// </noopy>
|
|
|
|
// </foopy>
|
|
|
|
// </preferences>
|
|
|
|
//
|
|
|
|
// where this maps to preferences called:
|
|
|
|
// foopy.noopy (int pref, value 42)
|
|
|
|
// foopy.noopy.noo (bool pref, value true);
|
|
|
|
// foopy.noopy.goo (string pref, value "goats");
|
|
|
|
//
|
|
|
|
private XmlElement CreateBranch(String aPrefName)
|
|
|
|
{
|
|
|
|
String[] names = aPrefName.Split('.');
|
|
|
|
XmlElement elt = mPrefsDocument.DocumentElement;
|
|
|
|
for (int i = 0; i < names.Length; ++i)
|
|
|
|
elt = CreateBranch(names[i], elt);
|
|
|
|
return elt;
|
|
|
|
}
|
2001-06-28 04:19:25 +04:00
|
|
|
|
2001-06-30 05:11:21 +04:00
|
|
|
private XmlElement CreateBranch(String aBranchName, XmlElement aRoot)
|
2001-06-28 04:19:25 +04:00
|
|
|
{
|
2001-06-30 05:11:21 +04:00
|
|
|
XmlElement elt = GetBranch(aBranchName, aRoot);
|
|
|
|
if (elt == null) {
|
|
|
|
elt = mPrefsDocument.CreateElement(aBranchName);
|
|
|
|
aRoot.AppendChild(elt);
|
|
|
|
}
|
|
|
|
return elt;
|
2001-06-28 04:19:25 +04:00
|
|
|
}
|
|
|
|
|
2001-07-03 10:14:01 +04:00
|
|
|
private XmlElement GetBranchElement(String aBranchName)
|
2001-06-28 04:19:25 +04:00
|
|
|
{
|
2001-06-30 05:11:21 +04:00
|
|
|
String[] names = aBranchName.Split('.');
|
|
|
|
XmlElement elt = mPrefsDocument.DocumentElement;
|
|
|
|
for (int i = 0; i < names.Length && elt != null; ++i)
|
|
|
|
elt = GetBranch(names[i], elt);
|
|
|
|
|
|
|
|
// The preference wasn't found in the user preferences
|
|
|
|
// file, look in the default preferences file.
|
|
|
|
if (elt == null) {
|
|
|
|
elt = mDefaultsDocument.DocumentElement;
|
|
|
|
for (int i = 0; i < names.Length; ++i)
|
|
|
|
elt = GetBranch(names[i], elt);
|
2001-06-28 04:19:25 +04:00
|
|
|
}
|
2001-06-30 05:11:21 +04:00
|
|
|
|
|
|
|
return elt;
|
|
|
|
}
|
|
|
|
|
|
|
|
private XmlElement GetBranch(String aBranchName, XmlElement aRoot)
|
|
|
|
{
|
|
|
|
// First, check to see if the specified root already has a branch
|
|
|
|
// specified. If it exists, hand that root back.
|
|
|
|
int childCount = aRoot.ChildNodes.Count;
|
|
|
|
for (int i = 0; i < childCount; ++i) {
|
|
|
|
if (aRoot.ChildNodes[i].LocalName == aBranchName)
|
|
|
|
return aRoot.ChildNodes[i] as XmlElement;
|
2001-06-28 04:19:25 +04:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2001-07-03 10:14:01 +04:00
|
|
|
public PrefBranch GetBranch(String aBranchName)
|
2001-06-30 05:11:21 +04:00
|
|
|
{
|
2001-07-03 10:14:01 +04:00
|
|
|
return new PrefBranch(aBranchName, GetBranchElement(aBranchName));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void RemoveBranch(String aBranchName)
|
|
|
|
{
|
|
|
|
XmlElement elt = GetBranchElement(aBranchName);
|
2001-06-30 05:11:21 +04:00
|
|
|
XmlNode parent = elt.ParentNode;
|
|
|
|
parent.RemoveChild(elt);
|
2001-07-03 10:14:01 +04:00
|
|
|
while (parent != null &&
|
|
|
|
parent != (mPrefsDocument.DocumentElement as XmlNode)) {
|
|
|
|
if (!parent.HasChildNodes) {
|
|
|
|
parent.ParentNode.RemoveChild(parent);
|
2001-06-30 05:11:21 +04:00
|
|
|
parent = parent.ParentNode;
|
2001-07-03 10:14:01 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
2001-06-30 05:11:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-06-28 04:19:25 +04:00
|
|
|
public bool GetBoolPref(String aPrefName)
|
|
|
|
{
|
2001-07-03 10:14:01 +04:00
|
|
|
XmlElement elt = GetBranchElement(aPrefName);
|
2001-06-30 05:11:21 +04:00
|
|
|
return elt != null ? elt.GetAttribute("value") == "true" : false;
|
2001-06-28 04:19:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetBoolPref(String aPrefName, bool aPrefValue)
|
|
|
|
{
|
2001-06-30 05:11:21 +04:00
|
|
|
XmlElement childElt = CreateBranch(aPrefName);
|
|
|
|
if (childElt != null)
|
|
|
|
childElt.SetAttribute("value", aPrefValue ? "true" : "false");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void RemovePref(String aPrefName)
|
|
|
|
{
|
|
|
|
RemoveBranch(aPrefName);
|
2001-06-28 04:19:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public int GetIntPref(String aPrefName)
|
|
|
|
{
|
2001-07-03 10:14:01 +04:00
|
|
|
XmlElement elt = GetBranchElement(aPrefName);
|
2001-06-28 04:19:25 +04:00
|
|
|
return elt != null ? Int32.Parse(elt.GetAttribute("value")) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetIntPref(String aPrefName, int aPrefValue)
|
|
|
|
{
|
2001-06-30 05:11:21 +04:00
|
|
|
XmlElement elt = CreateBranch(aPrefName);
|
2001-06-28 04:19:25 +04:00
|
|
|
if (elt != null) {
|
|
|
|
Object o = aPrefValue;
|
|
|
|
elt.SetAttribute("value", o.ToString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String GetStringPref(String aPrefName)
|
|
|
|
{
|
2001-07-03 10:14:01 +04:00
|
|
|
XmlElement elt = GetBranchElement(aPrefName);
|
2001-06-28 04:19:25 +04:00
|
|
|
return elt != null ? elt.GetAttribute("value") : "";
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetStringPref(String aPrefName, String aPrefValue)
|
|
|
|
{
|
2001-06-30 05:11:21 +04:00
|
|
|
XmlElement elt = CreateBranch(aPrefName);
|
|
|
|
if (elt != null)
|
2001-06-28 04:19:25 +04:00
|
|
|
elt.SetAttribute("value", aPrefValue);
|
|
|
|
}
|
2001-07-03 10:14:01 +04:00
|
|
|
|
|
|
|
public static String ResolvePref(XmlElement aElement)
|
|
|
|
{
|
|
|
|
String rv = aElement.LocalName;
|
|
|
|
XmlElement temp = aElement;
|
|
|
|
while (true) {
|
|
|
|
temp = temp.ParentNode as XmlElement;
|
|
|
|
if (temp == null || temp.LocalName == "preferences")
|
|
|
|
break;
|
|
|
|
rv = temp.LocalName + "." + rv;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
2001-06-28 04:19:25 +04:00
|
|
|
}
|
|
|
|
|
2001-07-03 10:14:01 +04:00
|
|
|
public class PrefBranch : IEnumerator
|
2001-06-30 05:11:21 +04:00
|
|
|
{
|
|
|
|
//private XmlElement mRoot;
|
2001-07-03 10:14:01 +04:00
|
|
|
private String mBranchName;
|
|
|
|
private XmlElement mRoot;
|
|
|
|
private XmlElement mCurrent = null;
|
|
|
|
|
|
|
|
public PrefBranch(String aPrefBranchName, XmlElement aBranchRoot)
|
|
|
|
{
|
|
|
|
mBranchName = aPrefBranchName;
|
|
|
|
mRoot = aBranchRoot;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Object Current
|
|
|
|
{
|
2001-08-01 07:48:08 +04:00
|
|
|
get
|
|
|
|
{
|
2001-07-03 10:14:01 +04:00
|
|
|
return Preferences.ResolvePref(mCurrent) as Object;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool MoveNext()
|
|
|
|
{
|
2001-08-01 07:48:08 +04:00
|
|
|
if (mCurrent != null)
|
|
|
|
{
|
2001-07-03 10:14:01 +04:00
|
|
|
if (mCurrent.NextSibling != null)
|
|
|
|
mCurrent = mCurrent.NextSibling as XmlElement;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
mCurrent = mRoot.FirstChild as XmlElement;
|
|
|
|
return true;
|
|
|
|
}
|
2001-06-30 05:11:21 +04:00
|
|
|
|
2001-07-03 10:14:01 +04:00
|
|
|
public void Reset()
|
2001-06-30 05:11:21 +04:00
|
|
|
{
|
2001-07-03 10:14:01 +04:00
|
|
|
mCurrent = mRoot.FirstChild as XmlElement;
|
2001-06-30 05:11:21 +04:00
|
|
|
}
|
|
|
|
}
|
2001-06-28 04:19:25 +04:00
|
|
|
}
|