/*
* $RCSfile: Configuration.cs,v $
* Copyright (C) 2006 Rob Loach (http://www.robloach.net)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
using System;
using System.Xml;
using System.Xml.Serialization;
using System.Reflection;
using System.Collections;
using System.IO;
using System.Windows.Forms;
namespace ScreenCap
{
public class Config
{
private static XmlDictionary m_Dictionary = new XmlDictionary("ScreenCap.xml", true);
public static void Save()
{
m_Dictionary.Save();
}
public static string SaveDirectory
{
get
{
if (m_Dictionary.ContainsKey("SaveDirectory"))
return m_Dictionary["SaveDirectory"].ToString();
else
return System.Windows.Forms.Application.StartupPath;
}
set{ m_Dictionary["SaveDirectory"] = value; }
}
public static string FilenameFormat
{
get
{
if (m_Dictionary.ContainsKey("FilenameFormat"))
return m_Dictionary["FilenameFormat"].ToString();
else
return "screenshot_{1:000}_({2:hh-mm-ss}).{0}";
}
set
{
m_Dictionary["FilenameFormat"] = value;
}
}
public enum ImageFormat
{
JPEG,BMP,PNG,GIF,TIFF
}
public static ImageFormat FileFormat
{
get
{
if (m_Dictionary.ContainsKey("FileFormat"))
return (ImageFormat)Enum.Parse(typeof(ImageFormat), m_Dictionary["FileFormat"].ToString(), true);
else
return ImageFormat.BMP;
}
set
{
m_Dictionary["FileFormat"] = value.ToString();
}
}
public static bool StartupSound
{
get
{
if (m_Dictionary.ContainsKey("StartupSound"))
return bool.Parse(m_Dictionary["StartupSound"].ToString());
else
return true;
}
set
{
m_Dictionary["StartupSound"] = value.ToString();
}
}
public static bool SnapshotSound
{
get
{
if (m_Dictionary.ContainsKey("SnapshotSound"))
return bool.Parse(m_Dictionary["SnapshotSound"].ToString());
else
return true;
}
set
{
m_Dictionary["SnapshotSound"] = value.ToString();
}
}
public static Keys CaptureActiveWindow
{
get
{
if (m_Dictionary.ContainsKey("CaptureActiveWindow"))
return (Keys)Enum.Parse(typeof(Keys),m_Dictionary["CaptureActiveWindow"].ToString(), true);
else
return Keys.ControlKey;
}
set
{
m_Dictionary["CaptureActiveWindow"] = value.ToString();
}
}
public static Keys CaptureActiveClientArea
{
get
{
if (m_Dictionary.ContainsKey("CaptureActiveClientArea"))
return (Keys)Enum.Parse(typeof(Keys), m_Dictionary["CaptureActiveClientArea"].ToString(), true);
else
return Keys.ShiftKey;
}
set
{
m_Dictionary["CaptureActiveClientArea"] = value.ToString();
}
}
public static bool GenerateThumbnails
{
get
{
if (m_Dictionary.ContainsKey("GenerateThumbnails"))
return bool.Parse(m_Dictionary["GenerateThumbnails"].ToString());
else
return false;
}
set
{
m_Dictionary["GenerateThumbnails"] = value.ToString();
}
}
public static bool SetMaxWidth
{
get
{
if (m_Dictionary.ContainsKey("SetMaxWidth"))
return bool.Parse(m_Dictionary["SetMaxWidth"].ToString());
else
return true;
}
set
{
m_Dictionary["SetMaxWidth"] = value.ToString();
}
}
public static bool SetMaxHeight
{
get
{
if (m_Dictionary.ContainsKey("SetMaxHeight"))
return bool.Parse(m_Dictionary["SetMaxHeight"].ToString());
else
return true;
}
set
{
m_Dictionary["SetMaxHeight"] = value.ToString();
}
}
public static int MaxWidth
{
get
{
if (m_Dictionary.ContainsKey("MaxWidth"))
return int.Parse(m_Dictionary["MaxWidth"].ToString());
else
return 100;
}
set
{
m_Dictionary["MaxWidth"] = value.ToString();
}
}
public static int MaxHeight
{
get
{
if (m_Dictionary.ContainsKey("MaxHeight"))
return int.Parse(m_Dictionary["MaxHeight"].ToString());
else
return 75;
}
set
{
m_Dictionary["MaxHeight"] = value.ToString();
}
}
public static string ThumbnailFileFormat
{
get
{
if (m_Dictionary.ContainsKey("ThumbnailFileFormat"))
return m_Dictionary["ThumbnailFileFormat"].ToString();
else
return "thumbnail_{1:000}_({2:hh-mm-ss}).{0}";
}
set
{
m_Dictionary["ThumbnailFileFormat"] = value;
}
}
public static ImageFormat ThumbnailImageFormat
{
get
{
if (m_Dictionary.ContainsKey("ThumbnailImageFormat"))
return (ImageFormat)Enum.Parse(typeof(ImageFormat), m_Dictionary["ThumbnailImageFormat"].ToString(), true);
else
return ImageFormat.JPEG;
}
set
{
m_Dictionary["ThumbnailImageFormat"] = value.ToString();
}
}
public static bool NotifyStartup
{
get
{
if (m_Dictionary.ContainsKey("NotifyStartup"))
return bool.Parse(m_Dictionary["NotifyStartup"].ToString());
else
return true;
}
set
{
m_Dictionary["NotifyStartup"] = value.ToString();
}
}
public static bool NotifySnapshot
{
get
{
if (m_Dictionary.ContainsKey("NotifySnapshot"))
return bool.Parse(m_Dictionary["NotifySnapshot"].ToString());
else
return false;
}
set
{
m_Dictionary["NotifySnapshot"] = value.ToString();
}
}
public static string FtpServer
{
get
{
if (m_Dictionary.ContainsKey("FtpServer"))
return m_Dictionary["FtpServer"].ToString();
else
return "localhost";
}
set
{
m_Dictionary["FtpServer"] = value;
}
}
public static string FtpUsername
{
get
{
if (m_Dictionary.ContainsKey("FtpUsername"))
return m_Dictionary["FtpUsername"].ToString();
else
return "";
}
set
{
m_Dictionary["FtpUsername"] = value;
}
}
public static string FtpPath
{
get
{
if (m_Dictionary.ContainsKey("FtpPath"))
return m_Dictionary["FtpPath"].ToString();
else
return "";
}
set
{
m_Dictionary["FtpPath"] = value;
}
}
#region XmlDictionary
///
/// A collection of key-and-value pairs that can be saved and loaded as an XML document.
///
///
/// The following example saves some data as an XML document and then loads it and displays the values.
///
/// // Put some data in an XmlDictionary
/// XmlDictionary data = new XmlDictionary("data.xml");
/// data["Width"] = 640;
/// data["Height"] = 480;
/// data["Title"] = "Hello World!";
///
/// // Save it to disk
/// data.Save();
///
/// // Load the data from the disk
/// XmlDictionary loadedData = new XmlDictionary();
/// loadedData.Load("data.xml");
///
/// // Display some data from it...
/// System.Console.WriteLine(data["Title"]);
/// System.Console.WriteLine("Width: " + data["Width"]);
/// System.Console.WriteLine("Height: " + data["Height"]);
///
///
///
/// You can also store customized classes if you make them interface from .
///
private class XmlDictionary : DictionaryBase, ICollection, IXmlDictionaryElement
{
#region Constructors
///
/// Creates an empty XmlDictionary.
///
public XmlDictionary()
{
}
///
/// Creates an empty XmlDictionary with a set filename.
///
/// The filename to save and load from.
/// Doesn't load on creation.
public XmlDictionary(string file)
{
m_File = file;
}
///
/// Creates a XmlDictionary with a filename.
///
/// The filename to save and load from.
/// True to automatically load the data on creation, false otherwise.
public XmlDictionary(string file, bool autoLoad)
{
m_File = file;
if (autoLoad)
Load();
}
#endregion
#region Properties
///
/// The file to save and load from.
///
private string m_File = "XmlDictionary.xml";
///
/// Gets and sets the filename to save and load from when calling the Save and Load functions.
///
/// This is automatically changed when calling Save(filename) and Load(filename) methods.
public string Filename
{
get
{
return m_File;
}
set
{
m_File = value;
}
}
#endregion
#region Functions
#region Contains
///
/// Checks if the dictionary contains the specified key.
///
/// The key to check exists.
/// True if the dictionary contains the element, false otherwise.
public bool Contains(string key)
{
return Dictionary.Contains(key);
}
///
/// Checks if the dictionary contains the specified key.
///
/// The key to check exists.
/// True if the dictionary contains the element, false otherwise.
public bool ContainsKey(string key)
{
return Dictionary.Contains(key);
}
///
/// Checks if the dictionary already contains the given value.
///
/// The value to check exists.
/// True if the value already exists in the dictionary.
public bool ContainsValue(object val)
{
IDictionaryEnumerator enumerator = Dictionary.GetEnumerator();
while (enumerator.MoveNext())
if (enumerator.Value == val)
return true;
return false;
}
#endregion
#region Dictionary Related
///
/// Gets and sets the value within the dictionary provided the key.
///
public object this[string key]
{
get
{
return this.Dictionary[key];
}
set
{
Dictionary[key] = value;
}
}
///
/// Gets a value from the dictionary.
///
/// The key of the value to get.
/// The value from within the dictionary.
public object GetValue(string key)
{
return Dictionary[key];
}
///
/// Sets a value in the dictionary.
///
/// The key of the value to set.
/// The value to set.
public void SetValue(string key, object val)
{
Dictionary[key] = val;
}
#endregion
#region XML Related
///
/// Converts the given node to an Object and adds it to the Dictionary.
///
///
private void NodeToItem(XmlNode node)
{
string key = node.Name;
if (node.Attributes["Type"] != null)
{
string strType = node.Attributes["Type"].Value;
Type type = Type.GetType(strType, false, true);
if (type != null)
{
object dynObj;
try
{
dynObj = Activator.CreateInstance(type);
}
catch (System.Exception e)
{
NullReferenceException err = new NullReferenceException(@"Error creating object of type """ + strType + @""" for XML element """ + key + @""": " + e.Message, e);
err.Source = strType;
throw err;
}
if (dynObj is IXmlDictionaryElement)
{
((IXmlDictionaryElement)dynObj).FromXml(node);
Dictionary[key] = dynObj;
return;
}
else
{
InvalidCastException err = new InvalidCastException(@"The type """ + strType + @""" is not of type IXmlDictionaryElement or IXmlSerializable for XML element """ + key + @""".");
err.Source = key;
throw err;
}
}
else
{
InvalidCastException err = new InvalidCastException(@"The desired type for """ + key + @""" (" + strType + ") does not exist.");
err.Source = key;
throw err;
}
}
if (node.InnerText == "NULL")
Dictionary[key] = null;
else
Dictionary[key] = node.InnerText;
}
///
/// Loads the dictionary data from the provided XML document.
///
/// The document from which to load the dictionary data.
private void LoadDocument(XmlDocument doc)
{
XmlElement root = doc.DocumentElement;
IEnumerator ienum = root.GetEnumerator();
while (ienum.MoveNext())
{
XmlNode node = (XmlNode)ienum.Current;
NodeToItem(node);
}
}
///
/// Writes the item information to the XML document
///
///
///
///
private XmlElement EnumeratorToXml(XmlDocument doc, IDictionaryEnumerator enumerator/*, XmlElement element*/)
{
XmlElement element = doc.CreateElement("", (string)enumerator.Key, "");
if (enumerator.Value == null)
{
element.AppendChild(doc.CreateTextNode("NULL"));
}
else if (enumerator.Value is IXmlDictionaryElement)
{
IXmlDictionaryElement val = (IXmlDictionaryElement)enumerator.Value;
element.SetAttribute("Type", enumerator.Value.GetType().ToString());
val.ToXml(element);
}
else
{
XmlText text = doc.CreateTextNode(enumerator.Value.ToString());
element.AppendChild(text);
}
return element;
}
///
/// Returns a XmlDocument containing the dictionary's data.
///
///
public XmlDocument GetDocument()
{
XmlDocument doc = new XmlDocument();
XmlNode node = doc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
doc.AppendChild(node);
XmlElement root = doc.CreateElement("", "XmlDictionary", "");
IDictionaryEnumerator enumerator = Dictionary.GetEnumerator();
while (enumerator.MoveNext())
root.AppendChild(EnumeratorToXml(doc, enumerator));
doc.AppendChild(root);
return doc;
}
#endregion
#region Load
///
/// Loads the dictionary data from specified XML file.
///
/// The XML file to load from.
public void Load(string xmlFile)
{
m_File = xmlFile;
Load();
}
///
/// Loads the dictionary data from the specified XmlReader.
///
/// The reader to load from.
public void Load(XmlReader reader)
{
XmlDocument doc = new XmlDocument();
doc.Load(reader);
LoadDocument(doc);
}
///
/// Loads the dictionary data from a TextReader.
///
/// The text reader containing the XML document.
public void Load(TextReader reader)
{
XmlDocument doc = new XmlDocument();
doc.Load(reader);
LoadDocument(doc);
}
///
/// Loads the dictionary data from a stream.
///
/// The stream containing the XML document to load.
public void Load(Stream inStream)
{
XmlDocument doc = new XmlDocument();
doc.Load(inStream);
LoadDocument(doc);
}
///
/// Loads the XML document from this XmlDictionary's filename.
///
public void Load()
{
try
{
using (XmlTextReader reader = new XmlTextReader(m_File))
{
XmlDocument doc = new XmlDocument();
doc.Load(reader);
LoadDocument(doc);
}
}
catch
{
}
}
#endregion
#region Save
///
/// Saves the dictionary data in the XmlDictionary's filename.
///
public void Save()
{
using (XmlTextWriter writer = new XmlTextWriter(m_File, null))
{
writer.Formatting = Formatting.Indented;
GetDocument().Save(writer);
}
}
///
/// Saves the dictionary data through a stream.
///
/// The stream to save the dictionary data as XML.
public void Save(System.IO.Stream stream)
{
GetDocument().Save(stream);
}
///
/// Saves the dictionary data as XML at the given filename.
///
/// The file to save the XML document containing the dictionary information.
public void Save(string filename)
{
m_File = filename;
Save();
}
///
/// Saves the dictionary data as XML on the given TextWriter.
///
/// The TextWriter to which you want to save the XML document.
public void Save(System.IO.TextWriter writer)
{
GetDocument().Save(writer);
}
#endregion
#endregion
#region Interface Implementations
#region ICollection Members
///
/// Gets a value saying whether this dictionary is thread-safe.
///
public bool IsSynchronized
{
get
{
return Dictionary.IsSynchronized;
}
}
///
/// Gets an object that can be used to synchronize the collection.
///
public object SyncRoot
{
get
{
return Dictionary.SyncRoot;
}
}
#endregion
#region IXmlDictionaryElement Members
///
/// Translates the dictionary to XML within the given XmlElement.
///
///
public void ToXml(XmlElement element)
{
IDictionaryEnumerator enumer = Dictionary.GetEnumerator();
while (enumer.MoveNext())
{
element.AppendChild(EnumeratorToXml(element.OwnerDocument, enumer));
}
}
///
/// Gets the dictionary information from the given XML node.
///
///
public void FromXml(XmlNode node)
{
IEnumerator ienum = node.GetEnumerator();
while (ienum.MoveNext())
{
XmlNode nodeitem = (XmlNode)ienum.Current;
NodeToItem(nodeitem);
}
}
#endregion
#endregion
}
///
/// An interface used by XmlDictionary that converts the object to and from XML.
///
///
/// This is an example of a Person class implementing IXmlDictionaryElement:
///
/// public class Person : IXmlDictionaryElement
/// {
/// public string Name;
/// public int Age;
/// public string Description;
///
/// #region IXmlDictionaryElement Members
///
/// public virtual void ToXml(System.Xml.XmlElement element)
/// {
/// element.SetAttribute("Name", Name);
/// element.SetAttribute("Age", Age.ToString());
/// element.InnerText = Description;
/// }
///
/// public virtual void FromXml(System.Xml.XmlNode node)
/// {
/// Name = node.Attributes["Name"].Value;
/// Age = int.Parse(node.Attributes["Age"].Value);
/// Description = node.InnerText;
/// }
///
/// #endregion
/// }
///
public interface IXmlDictionaryElement
{
///
/// When implemented in a derived class, it adds the current objects information to the given element.
///
/// The XML Element to add the data to.
void ToXml(XmlElement element);
///
/// When implemented in a derived class, it gets the current object's information from the given XML node.
///
/// The node from which to get information.
void FromXml(XmlNode node);
}
#endregion XmlDictionary
}
}