Jackett/src/Jackett/Models/ConfigurationData.cs

117 lines
3.3 KiB
C#
Raw Normal View History

2015-07-19 17:18:54 +00:00
using Jackett.Utils;
using Newtonsoft.Json.Linq;
2015-04-13 06:25:21 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2015-07-19 00:27:41 +00:00
namespace Jackett.Models
2015-04-13 06:25:21 +00:00
{
public abstract class ConfigurationData
{
public enum ItemType
{
InputString,
InputBool,
DisplayImage,
DisplayInfo,
HiddenData
}
public void LoadValuesFromJson(JToken json)
{
// todo: match up ids with items and fill values
IDictionary<string, JToken> dictionary = (JObject)json;
foreach (var item in GetItems())
{
switch (item.ItemType)
{
case ItemType.InputString:
((StringItem)item).Value = (string)dictionary[item.ID];
break;
case ItemType.InputBool:
((BoolItem)item).Value = (bool)dictionary[item.ID];
break;
}
}
}
public JToken ToJson()
{
var items = GetItems();
var jArray = new JArray();
foreach (var item in items)
{
var jObject = new JObject();
jObject["id"] = item.ID;
jObject["type"] = item.ItemType.ToString().ToLower();
jObject["name"] = item.Name;
switch (item.ItemType)
{
case ItemType.InputString:
case ItemType.HiddenData:
case ItemType.DisplayInfo:
jObject["value"] = ((StringItem)item).Value;
break;
case ItemType.InputBool:
jObject["value"] = ((BoolItem)item).Value;
break;
case ItemType.DisplayImage:
2015-07-19 17:18:54 +00:00
string dataUri = DataUrlUtils.BytesToDataUrl(((ImageItem)item).Value, "image/jpeg");
2015-04-13 06:25:21 +00:00
jObject["value"] = dataUri;
break;
}
jArray.Add(jObject);
}
return jArray;
}
public class Item
{
public ItemType ItemType { get; set; }
public string Name { get; set; }
public string ID { get { return Name.Replace(" ", "").ToLower(); } }
}
2015-04-18 06:05:30 +00:00
public class DisplayItem : StringItem
{
public DisplayItem(string value)
{
Value = value;
ItemType = ItemType.DisplayInfo;
}
}
2015-04-13 06:25:21 +00:00
public class StringItem : Item
{
public string Value { get; set; }
2015-04-18 06:05:30 +00:00
public StringItem()
{
ItemType = ConfigurationData.ItemType.InputString;
}
2015-04-13 06:25:21 +00:00
}
public class BoolItem : Item
{
public bool Value { get; set; }
2015-04-18 06:05:30 +00:00
public BoolItem()
{
ItemType = ConfigurationData.ItemType.InputBool;
}
2015-04-13 06:25:21 +00:00
}
public class ImageItem : Item
{
public byte[] Value { get; set; }
2015-04-18 06:05:30 +00:00
public ImageItem()
{
ItemType = ConfigurationData.ItemType.DisplayImage;
}
2015-04-13 06:25:21 +00:00
}
public abstract Item[] GetItems();
}
}