Files
romrepomgr/RomRepoMgr.Settings/Settings.cs

348 lines
15 KiB
C#
Raw Permalink Normal View History

2020-08-22 01:06:14 +01:00
/******************************************************************************
// RomRepoMgr - ROM repository manager
// ----------------------------------------------------------------------------
//
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
2024-11-08 19:13:57 +00:00
// Copyright © 2020-2024 Natalia Portillo
2020-08-22 01:06:14 +01:00
*******************************************************************************/
using System;
using System.IO;
using System.Text.Json;
using Aaru.CommonTypes.Interop;
using Claunia.PropertyList;
using Microsoft.Win32;
using PlatformID = Aaru.CommonTypes.Interop.PlatformID;
2024-11-09 01:37:59 +00:00
namespace RomRepoMgr.Settings;
2020-08-22 01:06:14 +01:00
public enum CompressionType
{
Lzip = 0,
Zstd,
None
}
2024-11-09 01:37:59 +00:00
public sealed class SetSettings
{
2025-07-26 04:17:26 +01:00
public string DatabasePath { get; set; }
public string RepositoryPath { get; set; }
public string TemporaryFolder { get; set; }
public string UnArchiverPath { get; set; }
public CompressionType Compression { get; set; }
public bool UseInternalDecompressor { get; set; }
2024-11-09 01:37:59 +00:00
}
/// <summary>Manages statistics</summary>
public static class Settings
{
const string XDG_CONFIG_HOME = "XDG_CONFIG_HOME";
const string XDG_CONFIG_HOME_RESOLVED = ".config";
/// <summary>Current statistics</summary>
public static SetSettings Current;
public static bool CanDecompress { get; set; }
2024-11-09 01:37:59 +00:00
/// <summary>Loads settings</summary>
public static void LoadSettings()
2020-08-22 01:06:14 +01:00
{
2024-11-09 01:37:59 +00:00
Current = new SetSettings();
PlatformID ptId = DetectOS.GetRealPlatformID();
string homePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
FileStream prefsFs = null;
StreamReader prefsSr = null;
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
try
{
switch(ptId)
2020-08-22 01:06:14 +01:00
{
2024-11-09 01:37:59 +00:00
// In case of macOS or iOS settings will be saved in ~/Library/Preferences/com.claunia.romrepomgr.plist
case PlatformID.MacOSX:
case PlatformID.iOS:
2020-08-22 01:06:14 +01:00
{
2024-11-09 01:37:59 +00:00
string preferencesPath =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
"Library",
"Preferences");
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
string preferencesFilePath = Path.Combine(preferencesPath, "com.claunia.romrepomgr.plist");
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
if(!File.Exists(preferencesFilePath))
{
SetDefaultSettings();
SaveSettings();
}
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
prefsFs = new FileStream(preferencesFilePath, FileMode.Open, FileAccess.Read);
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
var parsedPreferences = (NSDictionary)BinaryPropertyListParser.Parse(prefsFs);
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
if(parsedPreferences != null)
{
NSObject obj;
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
Current.DatabasePath = parsedPreferences.TryGetValue("DatabasePath", out obj)
? ((NSString)obj).ToString()
: null;
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
Current.RepositoryPath = parsedPreferences.TryGetValue("RepositoryPath", out obj)
? ((NSString)obj).ToString()
: null;
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
Current.TemporaryFolder = parsedPreferences.TryGetValue("TemporaryFolder", out obj)
? ((NSString)obj).ToString()
: null;
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
Current.UnArchiverPath = parsedPreferences.TryGetValue("UnArchiverPath", out obj)
? ((NSString)obj).ToString()
: null;
2020-08-22 01:06:14 +01:00
Current.Compression = parsedPreferences.TryGetValue("Compression", out obj)
? (CompressionType)Enum.Parse(typeof(CompressionType),
((NSNumber)obj).ToString())
: CompressionType.Lzip;
2025-07-26 04:17:26 +01:00
Current.UseInternalDecompressor =
parsedPreferences.TryGetValue("UseInternalDecompressor", out obj) &&
((NSNumber)obj).ToBool();
2025-07-26 04:17:26 +01:00
2024-11-09 01:37:59 +00:00
prefsFs.Close();
2020-08-22 01:06:14 +01:00
}
2024-11-09 01:37:59 +00:00
else
2020-08-22 01:06:14 +01:00
{
2024-11-09 01:37:59 +00:00
prefsFs.Close();
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
SetDefaultSettings();
SaveSettings();
}
}
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
break;
#if !NETSTANDARD2_0
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
// In case of Windows settings will be saved in the registry: HKLM/SOFTWARE/Claunia.com/RomRepoMgr
case PlatformID.Win32NT when OperatingSystem.IsWindows():
case PlatformID.Win32S when OperatingSystem.IsWindows():
case PlatformID.Win32Windows when OperatingSystem.IsWindows():
case PlatformID.WinCE when OperatingSystem.IsWindows():
case PlatformID.WindowsPhone when OperatingSystem.IsWindows():
{
RegistryKey parentKey = Registry.CurrentUser.OpenSubKey("SOFTWARE")?.OpenSubKey("Claunia.com");
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
if(parentKey == null)
{
SetDefaultSettings();
SaveSettings();
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
return;
2020-08-22 01:06:14 +01:00
}
2024-11-09 01:37:59 +00:00
RegistryKey key = parentKey.OpenSubKey("RomRepoMgr");
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
if(key == null)
2020-08-22 01:06:14 +01:00
{
2024-11-09 01:37:59 +00:00
SetDefaultSettings();
SaveSettings();
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
return;
}
2020-08-22 01:06:14 +01:00
2025-07-26 04:17:26 +01:00
Current.DatabasePath = key.GetValue("DatabasePath") as string;
Current.RepositoryPath = key.GetValue("RepositoryPath") as string;
Current.TemporaryFolder = key.GetValue("TemporaryFolder") as string;
Current.UnArchiverPath = key.GetValue("UnArchiverPath") as string;
Current.UseInternalDecompressor = key.GetValue("UseInternalDecompressor") as int? > 0;
if(key.GetValue("Compression") is int compression)
Current.Compression = (CompressionType)compression;
else
Current.Compression = CompressionType.Lzip;
2024-11-09 01:37:59 +00:00
}
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
break;
#endif
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
// Otherwise, settings will be saved in ~/.config/RomRepoMgr.json
default:
{
string xdgConfigPath = Path.Combine(homePath,
Environment.GetEnvironmentVariable(XDG_CONFIG_HOME) ??
XDG_CONFIG_HOME_RESOLVED);
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
string settingsPath = Path.Combine(xdgConfigPath, "RomRepoMgr.json");
if(!File.Exists(settingsPath))
{
SetDefaultSettings();
SaveSettings();
return;
2020-08-22 01:06:14 +01:00
}
2024-11-09 01:37:59 +00:00
prefsSr = new StreamReader(settingsPath);
Current = JsonSerializer.Deserialize<SetSettings>(prefsSr.ReadToEnd(),
new JsonSerializerOptions
{
AllowTrailingCommas = true,
PropertyNameCaseInsensitive = true,
ReadCommentHandling =
JsonCommentHandling.Skip,
WriteIndented = true
});
2020-08-22 01:06:14 +01:00
}
2024-11-09 01:37:59 +00:00
break;
2020-08-22 01:06:14 +01:00
}
}
2024-11-09 01:37:59 +00:00
catch
{
prefsFs?.Close();
prefsSr?.Close();
SetDefaultSettings();
SaveSettings();
}
}
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
public static void SaveSettings()
{
try
2020-08-22 01:06:14 +01:00
{
2024-11-09 01:37:59 +00:00
PlatformID ptId = DetectOS.GetRealPlatformID();
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
switch(ptId)
{
// In case of macOS or iOS settings will be saved in ~/Library/Preferences/com.claunia.romrepomgr.plist
case PlatformID.MacOSX:
case PlatformID.iOS:
2020-08-22 01:06:14 +01:00
{
2024-11-09 01:37:59 +00:00
var root = new NSDictionary
2020-08-22 01:06:14 +01:00
{
{
2024-11-09 01:37:59 +00:00
"DatabasePath", Current.DatabasePath
},
{
"RepositoryPath", Current.RepositoryPath
},
{
"TemporaryFolder", Current.TemporaryFolder
},
{
"UnArchiverPath", Current.UnArchiverPath
},
{
"Compression", (NSNumber)(int)Current.Compression
2025-07-26 04:17:26 +01:00
},
{
"UseInternalDecompressor", new NSNumber(Current.UseInternalDecompressor ? 1 : 0)
2024-11-09 01:37:59 +00:00
}
};
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
string preferencesPath =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
"Library",
"Preferences");
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
string preferencesFilePath = Path.Combine(preferencesPath, "com.claunia.romrepomgr.plist");
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
var fs = new FileStream(preferencesFilePath, FileMode.Create);
BinaryPropertyListWriter.Write(fs, root);
fs.Close();
}
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
break;
#if !NETSTANDARD2_0
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
// In case of Windows settings will be saved in the registry: HKLM/SOFTWARE/Claunia.com/RomRepoMgr
case PlatformID.Win32NT when OperatingSystem.IsWindows():
case PlatformID.Win32S when OperatingSystem.IsWindows():
case PlatformID.Win32Windows when OperatingSystem.IsWindows():
case PlatformID.WinCE when OperatingSystem.IsWindows():
case PlatformID.WindowsPhone when OperatingSystem.IsWindows():
{
RegistryKey parentKey =
Registry.CurrentUser.OpenSubKey("SOFTWARE", true)?.CreateSubKey("Claunia.com");
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
RegistryKey key = parentKey?.CreateSubKey("RomRepoMgr");
2020-08-22 01:06:14 +01:00
2025-07-26 04:17:26 +01:00
key?.SetValue("DatabasePath", Current.DatabasePath);
key?.SetValue("RepositoryPath", Current.RepositoryPath);
key?.SetValue("TemporaryFolder", Current.TemporaryFolder);
key?.SetValue("UnArchiverPath", Current.UnArchiverPath);
key?.SetValue("Compression", (int)Current.Compression);
key?.SetValue("UseInternalDecompressor", Current.UseInternalDecompressor ? 1 : 0);
2024-11-09 01:37:59 +00:00
}
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
break;
#endif
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
// Otherwise, settings will be saved in ~/.config/RomRepoMgr.json
default:
{
string homePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
string xdgConfigPath = Path.Combine(homePath,
Environment.GetEnvironmentVariable(XDG_CONFIG_HOME) ??
XDG_CONFIG_HOME_RESOLVED);
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
string settingsPath = Path.Combine(xdgConfigPath, "RomRepoMgr.json");
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
if(!Directory.Exists(xdgConfigPath)) Directory.CreateDirectory(xdgConfigPath);
2020-08-22 01:06:14 +01:00
2024-11-09 01:37:59 +00:00
var prefsSr = new StreamWriter(settingsPath);
prefsSr.Write(JsonSerializer.Serialize(Current,
new JsonSerializerOptions
{
AllowTrailingCommas = true,
PropertyNameCaseInsensitive = true,
ReadCommentHandling = JsonCommentHandling.Skip,
WriteIndented = true
}));
prefsSr.Close();
2020-08-22 01:06:14 +01:00
}
2024-11-09 01:37:59 +00:00
break;
2020-08-22 01:06:14 +01:00
}
}
2024-11-09 01:37:59 +00:00
#pragma warning disable RECS0022 // A catch clause that catches System.Exception and has an empty body
catch
2020-08-22 01:06:14 +01:00
{
2024-11-09 01:37:59 +00:00
// ignored
2020-08-22 01:06:14 +01:00
}
2024-11-09 01:37:59 +00:00
#pragma warning restore RECS0022 // A catch clause that catches System.Exception and has an empty body
}
/// <summary>Sets default settings as all statistics, share everything</summary>
static void SetDefaultSettings()
{
string docsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string dataPath = Path.Combine(docsPath, "RomRepoMgr");
Current = new SetSettings
{
DatabasePath = Path.Combine(dataPath, "romrepo.db"),
RepositoryPath = Path.Combine(dataPath, "repo"),
TemporaryFolder = Path.GetTempPath(),
Compression = CompressionType.Lzip
2024-11-09 01:37:59 +00:00
};
2020-08-22 01:06:14 +01:00
}
}