2023-06-03 15:02:15 +02:00
|
|
|
|
using Logging;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
public string downloadLocation { get; set; }
|
|
|
|
|
public string workingDirectory { get; set; }
|
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");
|
|
|
|
|
[JsonIgnore] public string knownPublicationsPath => Path.Join(workingDirectory, "knownPublications.json");
|
2023-05-25 14:23:33 +02:00
|
|
|
|
[JsonIgnore] public string coverImageCache => Path.Join(workingDirectory, "imageCache");
|
2023-06-03 16:24:54 +02:00
|
|
|
|
public HashSet<LibraryManager> libraryManagers { get; }
|
2023-06-15 18:25:32 +02:00
|
|
|
|
public HashSet<NotificationManager> notificationManagers { get; }
|
2023-05-22 00:13:24 +02:00
|
|
|
|
|
2023-06-15 18:50:50 +02:00
|
|
|
|
public TrangaSettings(string downloadLocation, string workingDirectory, HashSet<LibraryManager>? libraryManagers,
|
|
|
|
|
HashSet<NotificationManager>? notificationManagers)
|
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-06-15 18:50:50 +02:00
|
|
|
|
this.libraryManagers = libraryManagers??new();
|
|
|
|
|
this.notificationManagers = notificationManagers??new();
|
2023-05-22 00:13:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-03 15:02:15 +02:00
|
|
|
|
public static TrangaSettings LoadSettings(string importFilePath, Logger? logger)
|
2023-05-22 00:13:24 +02:00
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(importFilePath))
|
2023-06-15 18:25:32 +02:00
|
|
|
|
return new TrangaSettings(Path.Join(Directory.GetCurrentDirectory(), "Downloads"),
|
|
|
|
|
Directory.GetCurrentDirectory(), new HashSet<LibraryManager>(), new HashSet<NotificationManager>());
|
2023-05-22 00:13:24 +02:00
|
|
|
|
|
|
|
|
|
string toRead = File.ReadAllText(importFilePath);
|
2023-06-15 18:25:32 +02:00
|
|
|
|
TrangaSettings settings = JsonConvert.DeserializeObject<TrangaSettings>(toRead,
|
|
|
|
|
new JsonSerializerSettings { Converters = { new NotificationManager.NotificationManagerJsonConverter(), new LibraryManager.LibraryManagerJsonConverter() } })!;
|
|
|
|
|
if (logger is not null)
|
|
|
|
|
foreach (LibraryManager lm in settings.libraryManagers)
|
2023-06-03 15:17:08 +02:00
|
|
|
|
lm.AddLogger(logger);
|
2023-05-22 00:13:24 +02:00
|
|
|
|
|
|
|
|
|
return settings;
|
|
|
|
|
}
|
2023-06-15 18:25:32 +02:00
|
|
|
|
|
|
|
|
|
public void UpdateSettings(UpdateField field, Logger? logger = null, params string[] values)
|
|
|
|
|
{
|
|
|
|
|
switch (field)
|
|
|
|
|
{
|
|
|
|
|
case UpdateField.DownloadLocation:
|
|
|
|
|
if (values.Length != 1)
|
|
|
|
|
return;
|
|
|
|
|
this.downloadLocation = values[0];
|
|
|
|
|
break;
|
|
|
|
|
case UpdateField.Komga:
|
|
|
|
|
if (values.Length != 2)
|
|
|
|
|
return;
|
|
|
|
|
libraryManagers.RemoveWhere(lm => lm.GetType() == typeof(Komga));
|
|
|
|
|
libraryManagers.Add(new Komga(values[0], values[1], logger));
|
|
|
|
|
break;
|
|
|
|
|
case UpdateField.Kavita:
|
|
|
|
|
if (values.Length != 3)
|
|
|
|
|
return;
|
|
|
|
|
libraryManagers.RemoveWhere(lm => lm.GetType() == typeof(Kavita));
|
|
|
|
|
libraryManagers.Add(new Kavita(values[0], values[1], values[2], logger));
|
|
|
|
|
break;
|
|
|
|
|
case UpdateField.Gotify:
|
|
|
|
|
if (values.Length != 2)
|
|
|
|
|
return;
|
|
|
|
|
notificationManagers.RemoveWhere(nm => nm.GetType() == typeof(Gotify));
|
|
|
|
|
notificationManagers.Add(new Gotify(values[0], values[1], logger));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum UpdateField { DownloadLocation, Komga, Kavita, Gotify}
|
2023-05-22 00:13:24 +02:00
|
|
|
|
}
|