Files
logofclient/DataStore/Settings.cs
T

306 lines
9.8 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Logof_Client;
public class Settings
{
public static Settings _instance = new();
/// <summary>
/// Schema version - increment this when you make breaking changes to the Settings structure
/// </summary>
public int schemaVersion { get; set; } = SchemaMigration.CURRENT_SCHEMA_VERSION;
public AddressSets addressSets = new();
public Customers customers = new();
public PdfExportSettings pdfExport { get; set; } = new();
// public string settingsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
// "logofclient", "config.json");
public Settings()
{
_instance = this;
}
public static void Save()
{
if (!Directory.Exists(Global._instance.config_path) && !File.Exists((Global._instance.config_path)))
Directory.CreateDirectory(Global._instance.config_path);
// if (!string.IsNullOrEmpty(Global._instance.config_path)) _instance.settingsPath = Global._instance.config_path;
// Always save with current schema version
_instance.schemaVersion = SchemaMigration.CURRENT_SCHEMA_VERSION;
var json = JsonConvert.SerializeObject(_instance, Formatting.Indented);
File.WriteAllText(Path.Combine(Global._instance.config_path,"config.json"), json);
}
public static void Load()
{
//if (!string.IsNullOrEmpty(Global._instance.config_path)) _instance.settingsPath = Global._instance.config_path;
try
{
var contents = File.ReadAllText(Path.Combine(Global._instance.config_path, "config.json"));
// First, parse as JObject to check schema version
var jsonObject = JObject.Parse(contents);
int? loadedVersion = jsonObject["schemaVersion"]?.Value<int>();
// Apply migrations if needed
jsonObject = SchemaMigration.MigrateIfNeeded(jsonObject, loadedVersion);
// Now deserialize the migrated JSON
_instance = jsonObject.ToObject<Settings>();
// Save after migration to persist the upgraded schema
if (loadedVersion != SchemaMigration.CURRENT_SCHEMA_VERSION)
{
Logger.Log("Settings file was migrated to new schema version and saved.", Logger.LogType.Info);
Save();
}
MainWindow._instance.RefreshCustomerItems();
}
catch (Exception ex)
{
Logger.Log($"Error while reading settings. Generating new... {ex.Message}", Logger.LogType.Warning);
_instance = new Settings();
}
}
}
public class PdfExportSettings
{
public double cellPaddingTopMm { get; set; } = 4;
public double cellPaddingBottomMm { get; set; } = 4;
public double cellPaddingLeftMm { get; set; } = 4;
public double cellPaddingRightMm { get; set; } = 4;
public double pageMarginTopMm { get; set; } = 0;
public double pageMarginBottomMm { get; set; } = 0;
public double pageMarginLeftMm { get; set; } = 0;
public double pageMarginRightMm { get; set; } = 0;
public int rowsPerPage { get; set; } = 7;
public int columnsPerPage { get; set; } = 3;
public double fontSize { get; set; } = 9;
public double smallFontSize { get; set; } = 6;
public bool exportRunningSheets { get; set; } = true;
public int rsNumGrouped { get; set; } = 25;
public bool rsPvSt { get; set; } = true;
public string rsKNrAbs { get; set; } = "";
public string rsKNrEmp { get; set; } = "";
public string rsZKZ { get; set; } = "";
public string rsIntVer { get; set; } = "";
public string rsLaufz { get; set; } = "";
public string rsEinldat { get; set; } = "";
public string rsAMANr { get; set; } = "";
public string rsBundGew { get; set; } = "";
public int rsPalNr { get; set; } = 1;
public int rsBundNrPal { get; set; } = 20;
}
public class Global
{
public static Global _instance;
/// <summary>
/// Schema version - increment this when you make breaking changes to the Global structure
/// </summary>
public int schemaVersion { get; set; } = SchemaMigration.CURRENT_SCHEMA_VERSION;
public Global()
{
_instance = this;
}
public string config_path { get; set; } = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"logofclient");
public void SetConfigPath(string path)
{
if (!string.IsNullOrWhiteSpace(path))
config_path = PathUtilities.NormalizeFileSystemPath(path);
}
public string wiki_storage_path { get; set; } = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"logofclient",
"wiki");
public List<Country> countries { get; set; } = new();
public string font_path { get; set; } = Path.Combine(AppContext.BaseDirectory, "assets", "fonts");
public static void Save()
{
if (!Directory.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"logofclient")))
Directory.CreateDirectory(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"logofclient"));
// Always save with current schema version
_instance.schemaVersion = SchemaMigration.CURRENT_SCHEMA_VERSION;
var json = JsonConvert.SerializeObject(_instance, Formatting.Indented);
File.WriteAllText(
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "logofclient",
"global.config"), json);
}
public static void Load()
{
// if (!File.Exists(Path.Combine(
// Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "logofclient",
// "global.config")))
// File.Create(Path.Combine(
// Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "logofclient",
// "global.config"));
try
{
var contents = File.ReadAllText(Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "logofclient",
"global.config"));
// First, parse as JObject to check schema version
var jsonObject = JObject.Parse(contents);
int? loadedVersion = jsonObject["schemaVersion"]?.Value<int>();
// Apply migrations if needed
jsonObject = SchemaMigration.MigrateIfNeeded(jsonObject, loadedVersion);
// Now deserialize the migrated JSON
_instance = jsonObject.ToObject<Global>() ?? new Global();
// Save after migration to persist the upgraded schema
if (loadedVersion != SchemaMigration.CURRENT_SCHEMA_VERSION)
{
Logger.Log("Global settings file was migrated to new schema version and saved.", Logger.LogType.Info);
Save();
}
_instance.NormalizePaths();
}
catch (Exception ex)
{
Logger.Log($"Error while reading global settings. Generating new... {ex.Message}", Logger.LogType.Warning);
_instance = new Global();
Save();
}
}
private void NormalizePaths()
{
config_path = PathUtilities.NormalizeFileSystemPath(config_path);
wiki_storage_path = PathUtilities.NormalizeFileSystemPath(wiki_storage_path);
font_path = PathUtilities.NormalizeFileSystemPath(font_path);
}
}
public class Customers
{
public List<Customer> customers = new();
public Customer current { get; set; } = null;
}
public class Customer
{
public Customer()
{
var highestID = 0;
foreach (var customer in Settings._instance.customers.customers)
if (customer.ID > highestID)
highestID = customer.ID;
ID = highestID + 1;
}
public override string ToString()
{
return name;
}
public string name { get; set; } = "";
public string description { get; set; } = "";
public string sender_address { get; set; } = "";
public AddressPatch patch { get; set; }
public char separator { get; set; } = ',';
public int ID { get; }
public static Customer GetCustomerByID(int id)
{
foreach (var customer in Settings._instance.customers.customers)
{
if (id == customer.ID)
{
return customer;
}
}
return null;
}
// public static int GetIDByCustomerListItem(string item_content)
// {
// var id = item_content.Split(" - ")[0];
// return int.Parse(id);
// }
}
public class AddressSets
{
public List<KasAddressList> addresses = new();
public KasAddressList GetAddressSetByID(int ID)
{
foreach (var i in addresses)
if (i.ID == ID)
return i;
return null;
}
}
public class Country
{
public Country(string name, string translation, List<string> alternatives)
{
this.name = name;
this.translation = translation;
this.alternatives = alternatives;
}
public Country(string name)
{
this.name = name;
translation = "";
alternatives = new List<string>();
}
public Country()
{
}
public string? name { get; set; }
public string translation { get; set; }
public List<string> alternatives { get; set; }
public static Country GetByName(string name)
{
foreach (var country in Global._instance.countries)
if (country.name == name)
return country;
return null;
}
}