Tranga-Website/Tranga/TrangaSettings.cs

97 lines
3.5 KiB
C#
Raw Normal View History

2023-07-31 02:07:39 +02:00
using Newtonsoft.Json;
using Tranga.LibraryManagers;
using Tranga.NotificationManagers;
namespace Tranga;
public class TrangaSettings
{
2023-07-30 17:09:10 +02:00
public string downloadLocation { get; private set; }
public string workingDirectory { get; init; }
[JsonIgnore] public string settingsFilePath => Path.Join(workingDirectory, "settings.json");
[JsonIgnore] public string tasksFilePath => Path.Join(workingDirectory, "tasks.json");
2023-05-25 14:23:33 +02:00
[JsonIgnore] public string coverImageCache => Path.Join(workingDirectory, "imageCache");
public ushort? version { get; set; }
public TrangaSettings(string downloadLocation, string workingDirectory)
{
if (downloadLocation.Length < 1 || workingDirectory.Length < 1)
throw new ArgumentException("Download-location and working-directory paths can not be empty!");
this.workingDirectory = workingDirectory;
this.downloadLocation = downloadLocation;
}
public static TrangaSettings LoadSettings(string importFilePath)
{
if (!File.Exists(importFilePath))
return new TrangaSettings(Path.Join(Directory.GetCurrentDirectory(), "Downloads"), Directory.GetCurrentDirectory());
string toRead = File.ReadAllText(importFilePath);
SettingsJsonObject settings = JsonConvert.DeserializeObject<SettingsJsonObject>(toRead,
new JsonSerializerSettings { Converters = { new NotificationManager.NotificationManagerJsonConverter(), new LibraryManager.LibraryManagerJsonConverter() } })!;
return settings.ts ?? new TrangaSettings(Path.Join(Directory.GetCurrentDirectory(), "Downloads"), Directory.GetCurrentDirectory());
}
2023-06-21 18:04:12 +02:00
public void ExportSettings()
{
SettingsJsonObject? settings = null;
2023-06-21 18:04:12 +02:00
if (File.Exists(settingsFilePath))
{
bool inUse = true;
while (inUse)
{
try
{
using FileStream stream = new (settingsFilePath, FileMode.Open, FileAccess.Read, FileShare.None);
stream.Close();
inUse = false;
}
catch (IOException)
{
inUse = true;
Thread.Sleep(50);
}
}
string toRead = File.ReadAllText(settingsFilePath);
settings = JsonConvert.DeserializeObject<SettingsJsonObject>(toRead,
new JsonSerializerSettings
{
Converters =
{
new NotificationManager.NotificationManagerJsonConverter(),
new LibraryManager.LibraryManagerJsonConverter()
}
});
2023-06-21 18:04:12 +02:00
}
settings = new SettingsJsonObject(this, settings?.co);
File.WriteAllText(settingsFilePath, JsonConvert.SerializeObject(settings));
2023-06-21 18:04:12 +02:00
}
2023-07-30 17:08:43 +02:00
public void UpdateSettings(UpdateField field, params string[] values)
{
switch (field)
{
case UpdateField.DownloadLocation:
if (values.Length != 1)
return;
this.downloadLocation = values[0];
break;
}
2023-06-21 18:04:12 +02:00
ExportSettings();
}
public enum UpdateField { DownloadLocation, Komga, Kavita, Gotify, LunaSea}
internal class SettingsJsonObject
{
2023-07-31 01:45:55 +02:00
public TrangaSettings? ts { get; }
public CommonObjects? co { get; }
public SettingsJsonObject(TrangaSettings? ts, CommonObjects? co)
{
this.ts = ts;
this.co = co;
}
}
}