diff --git a/Tranga-CLI/Tranga_Cli.cs b/Tranga-CLI/Tranga_Cli.cs index 1340903..221009d 100644 --- a/Tranga-CLI/Tranga_Cli.cs +++ b/Tranga-CLI/Tranga_Cli.cs @@ -14,16 +14,19 @@ public static class Tranga_Cli { public static void Main(string[] args) { + string applicationFolderPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Tranga"); + string logMessageFolderPath = Path.Join(applicationFolderPath, "logs"); + string settingsFilePath = Path.Join(applicationFolderPath, "data.json"); + Logger logger = new(new[] { Logger.LoggerType.FileLogger }, null, null, - Path.Join(Directory.GetCurrentDirectory(), $"log-{DateTime.Now:dd-M-yyyy-HH-mm-ss}.txt")); + Path.Join(logMessageFolderPath, $"log-{DateTime.Now:dd-M-yyyy-HH-mm-ss}.txt")); - logger.WriteLine("Tranga_CLI", "Loading Settings."); + logger.WriteLine("Tranga_CLI", "Loading Taskmanager."); TaskManager.SettingsData settings; - string settingsPath = Path.Join(Directory.GetCurrentDirectory(), "data.json"); - if (File.Exists(settingsPath)) - settings = TaskManager.LoadData(Directory.GetCurrentDirectory()); + if (File.Exists(settingsFilePath)) + settings = TaskManager.LoadData(settingsFilePath); else - settings = new TaskManager.SettingsData(Directory.GetCurrentDirectory(), null, new HashSet()); + settings = new TaskManager.SettingsData(Directory.GetCurrentDirectory(), settingsFilePath, null, new HashSet()); logger.WriteLine("Tranga_CLI", "User Input"); @@ -76,7 +79,7 @@ public static class Tranga_Cli { TaskManager taskManager = new (settings, logger); ConsoleKey selection = ConsoleKey.EraseEndOfFile; - PrintMenu(taskManager, settings.downloadLocation, logger); + PrintMenu(taskManager, taskManager.settings.downloadLocation, logger); while (selection != ConsoleKey.Q) { int taskCount = taskManager.GetAllTasks().Length; @@ -97,7 +100,7 @@ public static class Tranga_Cli Console.ReadKey(); break; case ConsoleKey.C: - CreateTask(taskManager, settings, logger); + CreateTask(taskManager, taskManager.settings, logger); Console.WriteLine("Press any key."); Console.ReadKey(); break; @@ -145,7 +148,7 @@ public static class Tranga_Cli Console.ReadKey(); break; } - PrintMenu(taskManager, settings.downloadLocation, logger); + PrintMenu(taskManager, taskManager.settings.downloadLocation, logger); } Thread.Sleep(1000); } diff --git a/Tranga/TaskManager.cs b/Tranga/TaskManager.cs index 38cc17e..063ebe5 100644 --- a/Tranga/TaskManager.cs +++ b/Tranga/TaskManager.cs @@ -15,27 +15,31 @@ public class TaskManager private bool _continueRunning = true; private readonly Connector[] _connectors; private readonly Dictionary> _taskQueue = new(); - private string downloadLocation { get; } + public SettingsData settings { get; } private Logger? logger { get; } public Komga? komga { get; } - /// Local path to save data (Manga) to + /// Local path to save data (Manga) to + /// Path to the settings file (data.json) /// The Url of the Komga-instance that you want to update /// The Komga username /// The Komga password /// - public TaskManager(string folderPath, string? komgaBaseUrl = null, string? komgaUsername = null, string? komgaPassword = null, Logger? logger = null) + public TaskManager(string downloadFolderPath, string? settingsFilePath = null, string? komgaBaseUrl = null, string? komgaUsername = null, string? komgaPassword = null, Logger? logger = null) { this.logger = logger; - this.downloadLocation = folderPath; + _allTasks = new HashSet(); + if (komgaBaseUrl != null && komgaUsername != null && komgaPassword != null) + this.komga = new Komga(komgaBaseUrl, komgaUsername, komgaPassword, logger); + + this.settings = new SettingsData(downloadFolderPath, settingsFilePath, this.komga, this._allTasks); if (komgaBaseUrl != null && komgaUsername != null && komgaPassword != null) this.komga = new Komga(komgaBaseUrl, komgaUsername, komgaPassword, logger); - this._connectors = new Connector[]{ new MangaDex(folderPath, logger) }; + this._connectors = new Connector[]{ new MangaDex(downloadFolderPath, logger) }; foreach(Connector cConnector in this._connectors) _taskQueue.Add(cConnector, new List()); - _allTasks = new HashSet(); Thread taskChecker = new(TaskCheckerThread); taskChecker.Start(); @@ -45,9 +49,9 @@ public class TaskManager { this.logger = logger; this._connectors = new Connector[]{ new MangaDex(settings.downloadLocation, logger) }; + this.settings = settings; foreach(Connector cConnector in this._connectors) _taskQueue.Add(cConnector, new List()); - this.downloadLocation = settings.downloadLocation; this.komga = settings.komga; _allTasks = settings.allTasks; Thread taskChecker = new(TaskCheckerThread); @@ -67,7 +71,7 @@ public class TaskManager foreach (KeyValuePair> connectorTaskQueue in _taskQueue) { if(connectorTaskQueue.Value.RemoveAll(task => task.state == TrangaTask.ExecutionState.Waiting) > 0) - ExportData(Directory.GetCurrentDirectory()); + ExportData(); if (connectorTaskQueue.Value.Count > 0 && connectorTaskQueue.Value.All(task => task.state is TrangaTask.ExecutionState.Enqueued)) ExecuteTaskNow(connectorTaskQueue.Value.First()); @@ -154,7 +158,7 @@ public class TaskManager } } logger?.WriteLine(this.GetType().ToString(), $"Added new Task {newTask.ToString()}"); - ExportData(Directory.GetCurrentDirectory()); + ExportData(); return newTask; } @@ -184,7 +188,7 @@ public class TaskManager else logger?.WriteLine(this.GetType().ToString(), $"No Task {task} {publication?.sortName} {publication?.downloadUrl} could be found."); } - ExportData(Directory.GetCurrentDirectory()); + ExportData(); } /// @@ -252,7 +256,7 @@ public class TaskManager { logger?.WriteLine(this.GetType().ToString(), $"Shutting down (forced={force})"); _continueRunning = false; - ExportData(Directory.GetCurrentDirectory()); + ExportData(); if(force) Environment.Exit(_allTasks.Count(task => task.state is TrangaTask.ExecutionState.Enqueued or TrangaTask.ExecutionState.Running)); @@ -272,7 +276,7 @@ public class TaskManager { string importPath = Path.Join(importFolderPath, "data.json"); if (!File.Exists(importPath)) - return new SettingsData("", null, new HashSet()); + return new SettingsData(Directory.GetCurrentDirectory(), null, null, new HashSet()); string toRead = File.ReadAllText(importPath); SettingsData data = JsonConvert.DeserializeObject(toRead)!; @@ -283,26 +287,26 @@ public class TaskManager /// /// Exports data (settings, tasks) to file /// - /// Folder path, filename will be data.json - private void ExportData(string exportFolderPath) + private void ExportData() { logger?.WriteLine(this.GetType().ToString(), $"Exporting data to data.json"); - SettingsData data = new SettingsData(this.downloadLocation, this.komga, this._allTasks); - string exportPath = Path.Join(exportFolderPath, "data.json"); - string serializedData = JsonConvert.SerializeObject(data); - File.Delete(exportPath); - File.WriteAllText(exportPath, serializedData); + string serializedData = JsonConvert.SerializeObject(settings); + File.WriteAllText(settings.settingsFilePath, serializedData); } public class SettingsData { public string downloadLocation { get; set; } + public string settingsFilePath { get; } public Komga? komga { get; set; } public HashSet allTasks { get; } - public SettingsData(string downloadLocation, Komga? komga, HashSet allTasks) + public SettingsData(string downloadLocation, string? settingsFilePath, Komga? komga, HashSet allTasks) { + this.settingsFilePath = settingsFilePath ?? + Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), + "Tranga", "data.json"); this.downloadLocation = downloadLocation; this.komga = komga; this.allTasks = allTasks;