2023-06-03 15:02:15 +02:00
|
|
|
|
using Logging;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Tranga.LibraryManagers;
|
2023-05-22 00:13:24 +02:00
|
|
|
|
|
|
|
|
|
namespace Tranga;
|
|
|
|
|
|
|
|
|
|
public class TrangaSettings
|
|
|
|
|
{
|
|
|
|
|
public string downloadLocation { get; set; }
|
|
|
|
|
public string workingDirectory { get; set; }
|
2023-05-22 02:06:49 +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-05-22 00:13:24 +02:00
|
|
|
|
|
2023-06-03 15:17:08 +02:00
|
|
|
|
public TrangaSettings(string downloadLocation, string workingDirectory, HashSet<LibraryManager> libraryManagers)
|
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-03 15:17:08 +02:00
|
|
|
|
this.libraryManagers = libraryManagers;
|
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-03 15:17:08 +02:00
|
|
|
|
return new TrangaSettings(Path.Join(Directory.GetCurrentDirectory(), "Downloads"), Directory.GetCurrentDirectory(), new HashSet<LibraryManager>());
|
2023-05-22 00:13:24 +02:00
|
|
|
|
|
|
|
|
|
string toRead = File.ReadAllText(importFilePath);
|
2023-06-03 15:32:54 +02:00
|
|
|
|
TrangaSettings settings = JsonConvert.DeserializeObject<TrangaSettings>(toRead, new JsonSerializerSettings() { Converters = { new LibraryManager.LibraryManagerJsonConverter()} })!;
|
2023-06-03 15:17:08 +02:00
|
|
|
|
if(logger is not null)
|
|
|
|
|
foreach(LibraryManager lm in settings.libraryManagers)
|
|
|
|
|
lm.AddLogger(logger);
|
2023-05-22 00:13:24 +02:00
|
|
|
|
|
|
|
|
|
return settings;
|
|
|
|
|
}
|
|
|
|
|
}
|