Store settings in settings.xml instead of registry.

This commit is contained in:
Frank Skare
2021-05-24 03:09:23 +02:00
parent bba71c3782
commit 9d27465492
13 changed files with 137 additions and 107 deletions

View File

@@ -19,7 +19,7 @@ namespace mpvnet
public static string LightTheme { get; set; } = "light";
public static string StartSize { get; set; } = "height-session";
public static bool RememberPosition { get; set; }
public static bool RememberWindowPosition { get; set; }
public static bool DebugMode { get; set; }
public static bool IsStartedFromTerminal { get; } = Environment.GetEnvironmentVariable("_started_from_console") == "yes";
public static bool RememberVolume { get; set; } = true;
@@ -39,6 +39,17 @@ namespace mpvnet
get => (DarkMode == "system" && Sys.IsDarkTheme) || DarkMode == "always";
}
static AppSettings _Settings;
public static AppSettings Settings {
get {
if (_Settings == null)
_Settings = SettingsManager.Load();
return _Settings;
}
}
public static void Init()
{
string dummy = Core.ConfigFolder;
@@ -132,18 +143,17 @@ namespace mpvnet
{
if (RememberVolume)
{
Core.set_property_int("volume", RegistryHelp.GetInt("volume", 70));
Core.set_property_string("mute", RegistryHelp.GetString("mute", "no"));
Core.set_property_int("volume", Settings.Volume);
Core.set_property_string("mute", Settings.Mute);
}
}
static void Shutdown()
{
if (RememberVolume)
{
RegistryHelp.SetInt("volume", Core.get_property_int("volume"));
RegistryHelp.SetString("mute", Core.get_property_string("mute"));
}
Settings.Volume = Core.get_property_int("volume");
Settings.Mute = Core.get_property_string("mute");
SettingsManager.Save(Settings);
}
static Dictionary<string, string> _Conf;
@@ -167,7 +177,7 @@ namespace mpvnet
{
switch (name)
{
case "remember-position": RememberPosition = value == "yes"; return true;
case "remember-window-position": RememberWindowPosition = value == "yes"; return true;
case "debug-mode": DebugMode = value == "yes"; return true;
case "remember-volume": RememberVolume = value == "yes"; return true;
case "queue": Queue = value == "yes"; return true;
@@ -190,23 +200,5 @@ namespace mpvnet
return false;
}
}
public static void ShowSetup()
{
int value = RegistryHelp.GetInt("location: " + Folder.Startup);
if (value != 1)
{
if (Msg.ShowQuestion("Would you like to setup mpv.net?",
"The setup allows to create a start menu shortcut, file associations and " +
"adding mpv.net to the Path environment variable.") == DialogResult.OK)
Commands.Execute("show-setup-dialog");
else
Msg.ShowInfo("The setup dialog can be found at:\n\nContext Menu > Tools > Setup");
RegistryHelp.SetInt("location: " + Folder.Startup, 1);
}
}
}
}

1407
src/Misc/CorePlayer.cs Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -6,8 +6,6 @@ using System.Collections.Generic;
using System.Threading;
using System.Diagnostics;
using static mpvnet.Global;
namespace mpvnet
{
static class Program
@@ -23,9 +21,6 @@ namespace mpvnet
if (App.IsStartedFromTerminal)
Native.AttachConsole(-1 /*ATTACH_PARENT_PROCESS*/);
if (Core.ConfigFolder == "")
return;
string[] args = Environment.GetCommandLineArgs().Skip(1).ToArray();
if (args.Length >= 2 && args[0] == "--reg-file-assoc")

56
src/Misc/Settings.cs Normal file
View File

@@ -0,0 +1,56 @@

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using static mpvnet.Global;
namespace mpvnet
{
[Serializable()]
public class AppSettings
{
public int LastUpdateCheck;
public int Volume = 70;
public List<string> RecentFiles = new List<string>();
public Point WindowLocation;
public Point WindowPosition;
public Size WindowSize;
public string ConfigEditorSearch = "";
public string Mute = "no";
public string UpdateCheckVersion = "";
}
class SettingsManager
{
public static string SettingsFile {
get => Core.ConfigFolder + "settings.xml";
}
public static AppSettings Load()
{
if (!File.Exists(SettingsFile))
return new AppSettings();
XmlSerializer serializer = new XmlSerializer(typeof(AppSettings));
using (FileStream fs = new FileStream(SettingsFile, FileMode.Open))
return (AppSettings)serializer.Deserialize(fs);
}
public static void Save(object obj)
{
using (XmlTextWriter writer = new XmlTextWriter(SettingsFile, Encoding.UTF8))
{
writer.Formatting = Formatting.Indented;
writer.Indentation = 4;
XmlSerializer serializer = new XmlSerializer(obj.GetType());
serializer.Serialize(writer, obj);
}
}
}
}

View File

@@ -15,9 +15,7 @@ namespace mpvnet
{
public static void DailyCheck()
{
if (App.UpdateCheck && RegistryHelp.GetInt("last-update-check")
!= DateTime.Now.DayOfYear)
if (App.UpdateCheck && App.Settings.LastUpdateCheck != DateTime.Now.DayOfYear)
CheckOnline();
}
@@ -27,7 +25,7 @@ namespace mpvnet
{
using (HttpClient client = new HttpClient())
{
RegistryHelp.SetValue("last-update-check", DateTime.Now.DayOfYear);
App.Settings.LastUpdateCheck = DateTime.Now.DayOfYear;
client.DefaultRequestHeaders.Add("User-Agent", "mpv.net");
var response = await client.GetAsync("https://api.github.com/repos/stax76/mpv.net/releases/latest");
response.EnsureSuccessStatusCode();
@@ -51,8 +49,8 @@ namespace mpvnet
return;
}
if ((RegistryHelp.GetString("update-check-version")
!= onlineVersion.ToString() || showUpToDateMessage) && Msg.ShowQuestion(
if ((App.Settings.UpdateCheckVersion != onlineVersion.ToString() ||
showUpToDateMessage) && Msg.ShowQuestion(
$"New version {onlineVersion} is available, update now?") == DialogResult.OK)
{
string url = $"https://github.com/stax76/mpv.net/releases/download/{onlineVersion}/mpv.net-{onlineVersion}-portable.zip";
@@ -73,7 +71,7 @@ namespace mpvnet
Core.command("quit");
}
RegistryHelp.SetValue("update-check-version", onlineVersion.ToString());
App.Settings.UpdateCheckVersion = onlineVersion.ToString();
}
}
catch (Exception ex)