88 lines
2.6 KiB
C#
88 lines
2.6 KiB
C#
using System.Text.Json;
|
|
using LehrerApp.Sync.Crypto;
|
|
|
|
namespace LehrerApp.Desktop.Services;
|
|
|
|
internal sealed class AnnualPlanSettingsConfig
|
|
{
|
|
public bool Enabled { get; set; }
|
|
public string? EncryptedIcalUrl { get; set; }
|
|
public DateTime? LastSyncAt { get; set; }
|
|
public string LastSyncStatus { get; set; } = "";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gerätebezogene Einstellungen für den externen Schuljahresplan. Der eingebettete Feed-Schlüssel
|
|
/// wird wie die WebUntis-URL verschlüsselt in einer separaten Datei gespeichert.
|
|
/// </summary>
|
|
public sealed class AnnualPlanSettingsService
|
|
{
|
|
private readonly string _configPath;
|
|
private readonly byte[] _urlKey;
|
|
private AnnualPlanSettingsConfig _config;
|
|
|
|
public bool Enabled => _config.Enabled;
|
|
public bool IsConfigured => _config.EncryptedIcalUrl is not null;
|
|
public DateTime? LastSyncAt => _config.LastSyncAt;
|
|
public string LastSyncStatus => _config.LastSyncStatus;
|
|
|
|
public AnnualPlanSettingsService(string appDataPath)
|
|
{
|
|
_configPath = Path.Combine(appDataPath, "annual-plan-settings.json");
|
|
var keyPath = Path.Combine(appDataPath, "annual-plan-url.key");
|
|
_urlKey = SyncCrypto.LoadKey(keyPath) ?? GenerateAndSaveKey(keyPath);
|
|
_config = Load();
|
|
}
|
|
|
|
public void SetIcalUrl(string url)
|
|
{
|
|
_config.EncryptedIcalUrl = SyncCrypto.EncryptObject(url, _urlKey);
|
|
Save();
|
|
}
|
|
|
|
public string? GetIcalUrl() => _config.EncryptedIcalUrl is null
|
|
? null
|
|
: SyncCrypto.DecryptObject<string>(_config.EncryptedIcalUrl, _urlKey);
|
|
|
|
public void SetEnabled(bool enabled)
|
|
{
|
|
_config.Enabled = enabled;
|
|
Save();
|
|
}
|
|
|
|
public void ClearIcalUrl()
|
|
{
|
|
_config.EncryptedIcalUrl = null;
|
|
_config.Enabled = false;
|
|
Save();
|
|
}
|
|
|
|
public void SetLastSync(DateTime at, string status)
|
|
{
|
|
_config.LastSyncAt = at;
|
|
_config.LastSyncStatus = status;
|
|
Save();
|
|
}
|
|
|
|
private byte[] GenerateAndSaveKey(string keyPath)
|
|
{
|
|
var key = SyncCrypto.GenerateKey();
|
|
SyncCrypto.SaveKey(key, keyPath);
|
|
return key;
|
|
}
|
|
|
|
private AnnualPlanSettingsConfig Load()
|
|
{
|
|
try
|
|
{
|
|
if (File.Exists(_configPath))
|
|
return JsonSerializer.Deserialize<AnnualPlanSettingsConfig>(File.ReadAllText(_configPath))
|
|
?? new AnnualPlanSettingsConfig();
|
|
}
|
|
catch { /* beschädigte lokale Konfiguration -> Standardwerte */ }
|
|
return new AnnualPlanSettingsConfig();
|
|
}
|
|
|
|
private void Save() => File.WriteAllText(_configPath, JsonSerializer.Serialize(_config));
|
|
}
|