Library- and NotificationConnectors in GlobalBase
This commit is contained in:
parent
11461051f3
commit
8f309fcfd7
@ -1,4 +1,7 @@
|
|||||||
using Logging;
|
using Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Tranga.LibraryConnectors;
|
||||||
|
using Tranga.NotificationConnectors;
|
||||||
|
|
||||||
namespace Tranga;
|
namespace Tranga;
|
||||||
|
|
||||||
@ -6,17 +9,23 @@ public abstract class GlobalBase
|
|||||||
{
|
{
|
||||||
protected Logger? logger { get; init; }
|
protected Logger? logger { get; init; }
|
||||||
protected TrangaSettings settings { get; init; }
|
protected TrangaSettings settings { get; init; }
|
||||||
|
private HashSet<NotificationConnector> notificationConnectors { get; init; }
|
||||||
|
private HashSet<LibraryConnector> libraryConnectors { get; init; }
|
||||||
|
|
||||||
public GlobalBase(GlobalBase clone)
|
protected GlobalBase(GlobalBase clone)
|
||||||
{
|
{
|
||||||
this.logger = clone.logger;
|
this.logger = clone.logger;
|
||||||
this.settings = clone.settings;
|
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.logger = logger;
|
||||||
this.settings = settings;
|
this.settings = settings;
|
||||||
|
this.notificationConnectors = settings.LoadNotificationConnectors();
|
||||||
|
this.libraryConnectors = settings.LoadLibraryConnectors();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void Log(string message)
|
protected void Log(string message)
|
||||||
@ -29,6 +38,38 @@ public abstract class GlobalBase
|
|||||||
Log(string.Format(fStr, replace));
|
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)
|
protected bool IsFileInUse(string filePath)
|
||||||
{
|
{
|
||||||
if (!File.Exists(filePath))
|
if (!File.Exists(filePath))
|
||||||
|
@ -6,7 +6,7 @@ public class DownloadChapter : Job
|
|||||||
{
|
{
|
||||||
public Chapter chapter { get; init; }
|
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;
|
this.chapter = chapter;
|
||||||
}
|
}
|
||||||
@ -16,6 +16,8 @@ public class DownloadChapter : Job
|
|||||||
Task downloadTask = new(delegate
|
Task downloadTask = new(delegate
|
||||||
{
|
{
|
||||||
mangaConnector.DownloadChapter(chapter, this.progressToken);
|
mangaConnector.DownloadChapter(chapter, this.progressToken);
|
||||||
|
UpdateLibraries();
|
||||||
|
SendNotifications("Chapter downloaded", $"{chapter.parentPublication.sortName} - {chapter.chapterNumber}");
|
||||||
});
|
});
|
||||||
downloadTask.Start();
|
downloadTask.Start();
|
||||||
return Array.Empty<Job>();
|
return Array.Empty<Job>();
|
||||||
|
@ -6,7 +6,7 @@ public class DownloadNewChapters : Job
|
|||||||
{
|
{
|
||||||
public Publication publication { get; init; }
|
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;
|
this.publication = publication;
|
||||||
}
|
}
|
||||||
@ -18,7 +18,7 @@ public class DownloadNewChapters : Job
|
|||||||
List<Job> subJobs = new();
|
List<Job> subJobs = new();
|
||||||
foreach (Chapter chapter in chapters)
|
foreach (Chapter chapter in chapters)
|
||||||
{
|
{
|
||||||
DownloadChapter downloadChapterJob = new(this.mangaConnector, chapter);
|
DownloadChapter downloadChapterJob = new(this, this.mangaConnector, chapter);
|
||||||
subJobs.Add(downloadChapterJob);
|
subJobs.Add(downloadChapterJob);
|
||||||
}
|
}
|
||||||
progressToken.Complete();
|
progressToken.Complete();
|
||||||
|
@ -10,6 +10,9 @@ public class TrangaSettings
|
|||||||
public string downloadLocation { get; private set; }
|
public string downloadLocation { get; private set; }
|
||||||
public string workingDirectory { get; init; }
|
public string workingDirectory { get; init; }
|
||||||
[JsonIgnore] public string settingsFilePath => Path.Join(workingDirectory, "settings.json");
|
[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 tasksFilePath => Path.Join(workingDirectory, "tasks.json");
|
||||||
[JsonIgnore] public string coverImageCache => Path.Join(workingDirectory, "imageCache");
|
[JsonIgnore] public string coverImageCache => Path.Join(workingDirectory, "imageCache");
|
||||||
public ushort? version { get; set; }
|
public ushort? version { get; set; }
|
||||||
@ -24,23 +27,32 @@ public class TrangaSettings
|
|||||||
this.downloadLocation = downloadLocation;
|
this.downloadLocation = downloadLocation;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TrangaSettings LoadSettings(string importFilePath, Logger? logger)
|
public HashSet<LibraryConnector> LoadLibraryConnectors()
|
||||||
{
|
{
|
||||||
if (!File.Exists(importFilePath))
|
if (!File.Exists(libraryConnectorsFilePath))
|
||||||
return new TrangaSettings();
|
return new HashSet<LibraryConnector>();
|
||||||
|
return JsonConvert.DeserializeObject<HashSet<LibraryConnector>>(File.ReadAllText(libraryConnectorsFilePath),
|
||||||
string toRead = File.ReadAllText(importFilePath);
|
new JsonSerializerSettings()
|
||||||
TrangaSettings? settings = JsonConvert.DeserializeObject<TrangaSettings>(File.ReadAllText(importFilePath),
|
|
||||||
new JsonSerializerSettings
|
|
||||||
{
|
{
|
||||||
Converters =
|
Converters =
|
||||||
{
|
{
|
||||||
new NotificationManagerJsonConverter(),
|
|
||||||
new LibraryManagerJsonConverter()
|
new LibraryManagerJsonConverter()
|
||||||
}
|
}
|
||||||
});
|
})!;
|
||||||
return settings ?? new TrangaSettings();
|
}
|
||||||
|
|
||||||
|
public HashSet<NotificationConnector> LoadNotificationConnectors()
|
||||||
|
{
|
||||||
|
if (!File.Exists(notificationConnectorsFilePath))
|
||||||
|
return new HashSet<NotificationConnector>();
|
||||||
|
return JsonConvert.DeserializeObject<HashSet<NotificationConnector>>(File.ReadAllText(libraryConnectorsFilePath),
|
||||||
|
new JsonSerializerSettings()
|
||||||
|
{
|
||||||
|
Converters =
|
||||||
|
{
|
||||||
|
new NotificationManagerJsonConverter()
|
||||||
|
}
|
||||||
|
})!;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ExportSettings()
|
public void ExportSettings()
|
||||||
|
Loading…
Reference in New Issue
Block a user