// AggieConfig.cs
//
// Holds the configuration class that controls Aggie's behavior.
//
// Code written for use by Aggie.
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace Bitworking{
public class AggieConfig { // TODO: Handle templates/template @name/dynaskin pattern
#region Configuration data
[XmlStringSerialize("//AggieConfig/SiteListFileName", "myChannels.opml")]
public string siteListFileName_;
[XmlBoolSerialize( "//AggieConfig/ShowOldItems", false )]
public bool showOldItems_;
[XmlStringSerialize( "//AggieConfig/Proxy", "" )]
public string proxy_;
[XmlStringSerialize( "//AggieConfig/Skin", "Default" )]
public string skin_;
[XmlStringSerialize( "//AggieConfig/WeblogUrl", "" )]
public string weblogUrl_;
[XmlIntSerialize( "//AggieConfig/MaxThreads", 3 )]
public int maxThreads_;
public SmtpConfigInfo smtpConfig_;
#endregion
#region Class constants
private const string defaultConfigFile = "Aggie.xfg";
private const string defaultFactoryConfigFile = "Aggie.xfg.orig";
private const string defaultSiteListFileName = "myChannels.opml";
private const string defaultSkin = "Default";
#endregion
#region Private data (not serialized)
private string loadedFromFile; // File we were loaded from
#endregion
#region Creation (deserialize)
private AggieConfig() {
smtpConfig_ = new SmtpConfigInfo();
}
static public AggieConfig Create() {
return Create( defaultConfigFile, defaultFactoryConfigFile );
} // Create()
static public AggieConfig Create( string configFile, string factoryConfigFile ) {
// If first time after install, create a new config
// file from the factory default
if ( !File.Exists( configFile ) ) {
File.Copy( factoryConfigFile, configFile );
}
// Load the configuration information from the file
AggieConfig config = Load( configFile );
// Mandatory configuration values, without which
// nothing could work
config.PostLoad();
return config;
} // AggieConfig( string, string )
private void PostLoad() {
// Any field which is empty but shouldn't be is set here
siteListFileName_ = StringUtils.StringOrFallback( siteListFileName_, defaultSiteListFileName );
skin_ = StringUtils.StringOrFallback( skin_, defaultSkin );
// Ensure existence of a skin
// 1. If any file of the skin is missing, we revert to the skin "Default"
string skindir = "templates/" + skin_;
if ( !Directory.Exists( skindir ) ||
!File.Exists( skindir + "/skin.xsl" ) ||
!File.Exists( skindir + "/About.xml" )
) {
skin_ = defaultSkin;
skindir = "templates/" + skin_;
if ( !Directory.Exists( skindir ) ) {
Directory.CreateDirectory( skindir );
}
if ( !File.Exists( skindir + "/skin.xsl" ) ) {
File.Copy( "Aggie.xsl.orig", skindir + "/skin.xsl" );
}
if ( !File.Exists( skindir + "/About.xml" ) ) {
File.Copy( "about.xml.orig", skindir + "/About.xml" );
}
}
} // PostLoad()
static private AggieConfig Load( string file ) {
AggieConfig config = new AggieConfig();
SerializeByAttributes.Read(config, file);
SerializeByAttributes.Read(config.smtpConfig_, file);
config.loadedFromFile = file; // Save so we could use this name when writing
return config;
}
public void RevertToDefaultChannelList() {
siteListFileName_ = defaultSiteListFileName;
}
#endregion
#region Serialize
public void Save() { // Save to the same file you were loaded from
Save( loadedFromFile );
}
public void Save( string file ) {
SerializeByAttributes.Write(this, file);
SerializeByAttributes.Write(this.smtpConfig_, file);
}
#endregion
} // class AggieConfig
} // namespace Bitworking
|