From 8f309fcfd7d822ad08b0ec235cb9d0cf590e8d03 Mon Sep 17 00:00:00 2001 From: glax Date: Thu, 24 Aug 2023 13:33:23 +0200 Subject: [PATCH] Library- and NotificationConnectors in GlobalBase --- Tranga/GlobalBase.cs | 45 ++++++++++++++++++++++++++++-- Tranga/Jobs/DownloadChapter.cs | 4 ++- Tranga/Jobs/DownloadNewChapters.cs | 4 +-- Tranga/TrangaSettings.cs | 32 ++++++++++++++------- 4 files changed, 70 insertions(+), 15 deletions(-) diff --git a/Tranga/GlobalBase.cs b/Tranga/GlobalBase.cs index 9b05fdd..cf2b365 100644 --- a/Tranga/GlobalBase.cs +++ b/Tranga/GlobalBase.cs @@ -1,4 +1,7 @@ using Logging; +using Newtonsoft.Json; +using Tranga.LibraryConnectors; +using Tranga.NotificationConnectors; namespace Tranga; @@ -6,17 +9,23 @@ public abstract class GlobalBase { protected Logger? logger { get; init; } protected TrangaSettings settings { get; init; } + private HashSet notificationConnectors { get; init; } + private HashSet libraryConnectors { get; init; } - public GlobalBase(GlobalBase clone) + protected GlobalBase(GlobalBase clone) { this.logger = clone.logger; this.settings = clone.settings; + this.notificationConnectors = clone.notificationConnectors; + this.libraryConnectors = clone.libraryConnectors; } - public GlobalBase(Logger? logger, TrangaSettings settings) + protected GlobalBase(Logger? logger, TrangaSettings settings) { this.logger = logger; this.settings = settings; + this.notificationConnectors = settings.LoadNotificationConnectors(); + this.libraryConnectors = settings.LoadLibraryConnectors(); } protected void Log(string message) @@ -29,6 +38,38 @@ public abstract class GlobalBase Log(string.Format(fStr, replace)); } + protected void SendNotifications(string title, string text) + { + foreach (NotificationConnector nc in notificationConnectors) + nc.SendNotification(title, text); + } + + protected void AddNotificationConnector(NotificationConnector notificationConnector) + { + notificationConnectors.RemoveWhere(nc => nc.GetType() == notificationConnector.GetType()); + notificationConnectors.Add(notificationConnector); + + while(IsFileInUse(settings.notificationConnectorsFilePath)) + Thread.Sleep(100); + File.WriteAllText(settings.notificationConnectorsFilePath, JsonConvert.SerializeObject(notificationConnectors)); + } + + protected void UpdateLibraries() + { + foreach(LibraryConnector lc in libraryConnectors) + lc.UpdateLibrary(); + } + + protected void AddLibraryConnector(LibraryConnector libraryConnector) + { + libraryConnectors.RemoveWhere(lc => lc.GetType() == libraryConnector.GetType()); + libraryConnectors.Add(libraryConnector); + + while(IsFileInUse(settings.libraryConnectorsFilePath)) + Thread.Sleep(100); + File.WriteAllText(settings.libraryConnectorsFilePath, JsonConvert.SerializeObject(libraryConnectors)); + } + protected bool IsFileInUse(string filePath) { if (!File.Exists(filePath)) diff --git a/Tranga/Jobs/DownloadChapter.cs b/Tranga/Jobs/DownloadChapter.cs index 9110af9..61048bd 100644 --- a/Tranga/Jobs/DownloadChapter.cs +++ b/Tranga/Jobs/DownloadChapter.cs @@ -6,7 +6,7 @@ public class DownloadChapter : Job { public Chapter chapter { get; init; } - public DownloadChapter(MangaConnector connector, Chapter chapter) : base(connector) + public DownloadChapter(GlobalBase clone, MangaConnector connector, Chapter chapter) : base(clone, connector) { this.chapter = chapter; } @@ -16,6 +16,8 @@ public class DownloadChapter : Job Task downloadTask = new(delegate { mangaConnector.DownloadChapter(chapter, this.progressToken); + UpdateLibraries(); + SendNotifications("Chapter downloaded", $"{chapter.parentPublication.sortName} - {chapter.chapterNumber}"); }); downloadTask.Start(); return Array.Empty(); diff --git a/Tranga/Jobs/DownloadNewChapters.cs b/Tranga/Jobs/DownloadNewChapters.cs index a31ff6a..1baeeb4 100644 --- a/Tranga/Jobs/DownloadNewChapters.cs +++ b/Tranga/Jobs/DownloadNewChapters.cs @@ -6,7 +6,7 @@ public class DownloadNewChapters : Job { public Publication publication { get; init; } - public DownloadNewChapters(MangaConnector connector, Publication publication, bool recurring = false) : base (connector, recurring) + public DownloadNewChapters(GlobalBase clone, MangaConnector connector, Publication publication, bool recurring = false) : base (clone, connector, recurring) { this.publication = publication; } @@ -18,7 +18,7 @@ public class DownloadNewChapters : Job List subJobs = new(); foreach (Chapter chapter in chapters) { - DownloadChapter downloadChapterJob = new(this.mangaConnector, chapter); + DownloadChapter downloadChapterJob = new(this, this.mangaConnector, chapter); subJobs.Add(downloadChapterJob); } progressToken.Complete(); diff --git a/Tranga/TrangaSettings.cs b/Tranga/TrangaSettings.cs index 2eda41d..009cc74 100644 --- a/Tranga/TrangaSettings.cs +++ b/Tranga/TrangaSettings.cs @@ -10,6 +10,9 @@ public class TrangaSettings public string downloadLocation { get; private set; } public string workingDirectory { get; init; } [JsonIgnore] public string settingsFilePath => Path.Join(workingDirectory, "settings.json"); + [JsonIgnore] public string libraryConnectorsFilePath => Path.Join(workingDirectory, "libraryConnectors.json"); + + [JsonIgnore] public string notificationConnectorsFilePath => Path.Join(workingDirectory, "notificationConnectors.json"); [JsonIgnore] public string tasksFilePath => Path.Join(workingDirectory, "tasks.json"); [JsonIgnore] public string coverImageCache => Path.Join(workingDirectory, "imageCache"); public ushort? version { get; set; } @@ -24,23 +27,32 @@ public class TrangaSettings this.downloadLocation = downloadLocation; } - public static TrangaSettings LoadSettings(string importFilePath, Logger? logger) + public HashSet LoadLibraryConnectors() { - if (!File.Exists(importFilePath)) - return new TrangaSettings(); - - string toRead = File.ReadAllText(importFilePath); - TrangaSettings? settings = JsonConvert.DeserializeObject(File.ReadAllText(importFilePath), - new JsonSerializerSettings + if (!File.Exists(libraryConnectorsFilePath)) + return new HashSet(); + return JsonConvert.DeserializeObject>(File.ReadAllText(libraryConnectorsFilePath), + new JsonSerializerSettings() { Converters = { - new NotificationManagerJsonConverter(), new LibraryManagerJsonConverter() } - }); - return settings ?? new TrangaSettings(); + })!; + } + public HashSet LoadNotificationConnectors() + { + if (!File.Exists(notificationConnectorsFilePath)) + return new HashSet(); + return JsonConvert.DeserializeObject>(File.ReadAllText(libraryConnectorsFilePath), + new JsonSerializerSettings() + { + Converters = + { + new NotificationManagerJsonConverter() + } + })!; } public void ExportSettings()