2023-07-31 02:07:39 +02:00
|
|
|
|
using Newtonsoft.Json;
|
2023-06-03 15:02:15 +02:00
|
|
|
|
using Tranga.LibraryManagers;
|
2023-06-15 18:25:32 +02:00
|
|
|
|
using Tranga.NotificationManagers;
|
2023-05-22 00:13:24 +02:00
|
|
|
|
|
|
|
|
|
namespace Tranga;
|
|
|
|
|
|
|
|
|
|
public class TrangaSettings
|
|
|
|
|
{
|
2023-07-30 17:09:10 +02:00
|
|
|
|
public string downloadLocation { get; private set; }
|
|
|
|
|
public string workingDirectory { get; init; }
|
2023-06-15 18:25:32 +02:00
|
|
|
|
[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");
|
2023-07-20 18:13:22 +02:00
|
|
|
|
public ushort? version { get; set; }
|
2023-05-22 00:13:24 +02:00
|
|
|
|
|
2023-07-30 23:31:25 +02:00
|
|
|
|
public TrangaSettings(string downloadLocation, string workingDirectory)
|
2023-05-22 00:13:24 +02:00
|
|
|
|
{
|
2023-05-30 19:32:22 +02:00
|
|
|
|
if (downloadLocation.Length < 1 || workingDirectory.Length < 1)
|
|
|
|
|
throw new ArgumentException("Download-location and working-directory paths can not be empty!");
|
2023-05-22 02:06:49 +02:00
|
|
|
|
this.workingDirectory = workingDirectory;
|
2023-05-22 00:13:24 +02:00
|
|
|
|
this.downloadLocation = downloadLocation;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-30 23:31:25 +02:00
|
|
|
|
public static TrangaSettings LoadSettings(string importFilePath)
|
2023-05-22 00:13:24 +02:00
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(importFilePath))
|
2023-07-30 23:31:25 +02:00
|
|
|
|
return new TrangaSettings(Path.Join(Directory.GetCurrentDirectory(), "Downloads"), Directory.GetCurrentDirectory());
|
2023-05-22 00:13:24 +02:00
|
|
|
|
|
|
|
|
|
string toRead = File.ReadAllText(importFilePath);
|
2023-07-30 23:31:25 +02:00
|
|
|
|
SettingsJsonObject settings = JsonConvert.DeserializeObject<SettingsJsonObject>(toRead,
|
2023-06-15 18:25:32 +02:00
|
|
|
|
new JsonSerializerSettings { Converters = { new NotificationManager.NotificationManagerJsonConverter(), new LibraryManager.LibraryManagerJsonConverter() } })!;
|
2023-07-30 23:31:25 +02:00
|
|
|
|
return settings.ts ?? new TrangaSettings(Path.Join(Directory.GetCurrentDirectory(), "Downloads"), Directory.GetCurrentDirectory());
|
2023-05-22 00:13:24 +02:00
|
|
|
|
|
|
|
|
|
}
|
2023-06-15 18:25:32 +02:00
|
|
|
|
|
2023-06-21 18:04:12 +02:00
|
|
|
|
public void ExportSettings()
|
|
|
|
|
{
|
2023-07-31 00:01:18 +02:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-31 00:01:18 +02:00
|
|
|
|
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
|
|
|
|
}
|
2023-07-30 23:31:25 +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)
|
2023-06-15 18:25:32 +02:00
|
|
|
|
{
|
|
|
|
|
switch (field)
|
|
|
|
|
{
|
|
|
|
|
case UpdateField.DownloadLocation:
|
|
|
|
|
if (values.Length != 1)
|
|
|
|
|
return;
|
|
|
|
|
this.downloadLocation = values[0];
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-06-21 18:04:12 +02:00
|
|
|
|
ExportSettings();
|
2023-06-15 18:25:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-15 18:57:21 +02:00
|
|
|
|
public enum UpdateField { DownloadLocation, Komga, Kavita, Gotify, LunaSea}
|
2023-07-30 23:31:25 +02:00
|
|
|
|
|
|
|
|
|
internal class SettingsJsonObject
|
|
|
|
|
{
|
2023-07-31 01:45:55 +02:00
|
|
|
|
public TrangaSettings? ts { get; }
|
|
|
|
|
public CommonObjects? co { get; }
|
2023-07-30 23:31:25 +02:00
|
|
|
|
|
|
|
|
|
public SettingsJsonObject(TrangaSettings? ts, CommonObjects? co)
|
|
|
|
|
{
|
|
|
|
|
this.ts = ts;
|
|
|
|
|
this.co = co;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-22 00:13:24 +02:00
|
|
|
|
}
|