using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Duplicati.Server.Serialization;
namespace Duplicati.Server.Serializable
{
public class JobSettings : IJobSettings
{
///
/// Constructs a new job settings object with default values from xml files
///
public JobSettings()
{
this.Initialize();
Duplicati.Datamodel.ApplicationSettings appset = new Duplicati.Datamodel.ApplicationSettings(Program.DataConnection);
if (appset.UseCommonPassword)
{
//TODO: Set this, but do not transfer to client ?
//this.BackupPassword = appset.CommonPassword;
this.EncryptionModule = appset.CommonPasswordEncryptionModule;
}
ApplyXDoc(XDocument.Load(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(typeof(Program), "backup defaults.xml")));
string extraPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "backup defaults.xml");
if (System.IO.File.Exists(extraPath))
ApplyXDoc(XDocument.Load(extraPath));
//Since this element is not attached to any database element, set the id to -1
this.Id = -1;
}
private void ApplyXDoc(XDocument settings)
{
var el = settings.Element("settings");
if (el == null)
return;
foreach(var p in el.Elements())
{
var prop = this.GetType().GetProperty(p.Name.LocalName, System.Reflection.BindingFlags.IgnoreCase | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
if (prop == null)
continue;
if (prop.PropertyType == typeof(string))
{
prop.SetValue(this, p.Value, null);
}
else if (prop.PropertyType == typeof(bool))
{
prop.SetValue(this, Duplicati.Library.Utility.Utility.ParseBool(p.Value, false), null);
}
else if (prop.PropertyType == typeof(IList))
{
//Use any element name, usually
// x
// y
//but also accept:
//
//
//or any mix of that
prop.SetValue(this, p.Elements().Select(x =>
{
if (x.Attribute("value") != null)
return x.Attribute("value").Value;
else
return x.Value;
}).ToList(), null);
}
else if (prop.PropertyType == typeof(IDictionary))
{
IDictionary props = (IDictionary)prop.GetValue(this, null);
foreach (var pe in p.Elements())
{
//using the format
if (pe.Name.LocalName == "key" && pe.Attribute("name") != null && pe.Attribute("value") != null)
props[pe.Attribute("name").Value] = pe.Attribute("value").Value;
//using the value format
else
props[pe.Name.LocalName] = pe.Value;
}
}
}
}
///
/// Constructs a new job settings object with values reflecting an existing schedule
///
///
public JobSettings(Duplicati.Datamodel.Schedule schedule)
{
this.Initialize();
throw new MissingMethodException();
}
protected void Initialize()
{
this.Labels = new List();
this.Settings = new Dictionary();
this.Overrides = new Dictionary();
this.SchedulerSettings = new Dictionary();
this.BackendSettings = new BackendSettings();
this.CompressionSettings = new Dictionary();
this.EncryptionSettings = new Dictionary();
this.FilterSets = new List();
}
public long Id { get; set; }
public string Name { get; set; }
public IList Labels { get; set; }
public IList SourcePaths { get; set; }
public IList FilterSets { get; set; }
public bool IncludeSetup { get; set; }
public string BackendModule { get; set; }
public string CompressionModule { get; set; }
public string EncryptionModule { get; set; }
public IDictionary Settings { get; set; }
public IDictionary Overrides { get; set; }
public IDictionary SchedulerSettings { get; set; }
public IBackendSettings BackendSettings { get; set; }
public IDictionary CompressionSettings { get; set; }
public IDictionary EncryptionSettings { get; set; }
}
}