buypeeb-cs/SettingsWindow.cs

104 lines
3 KiB
C#
Raw Permalink Normal View History

2020-09-06 16:39:56 +00:00
using System;
using System.Collections.Generic;
using Gtk;
// ReSharper disable UnusedMember.Local
// ReSharper disable UnusedParameter.Local
2020-09-06 16:39:56 +00:00
namespace Buypeeb {
internal class SettingsWindow : Window {
private readonly Builder builder;
private readonly List<Switch> generalSwitches = new();
private readonly List<string> generalSwitchNames =
new() {"ShowSecondsInListView", "Autosave", "ShowFavouritesAtTopOfList"};
2020-09-06 16:39:56 +00:00
private readonly Settings settings;
private readonly List<Entry> updateIntervalEntries = new();
private readonly List<string> updateIntervalEntryNames = new()
{"UpdateInterval", "UpdateIntervalCritical", "FavouriteUpdateInterval", "FavouriteUpdateIntervalCritical"};
2020-09-06 16:39:56 +00:00
2021-10-23 17:35:20 +00:00
public SettingsWindow(Settings settings) : this(new Builder("settings.glade"), settings) { }
2020-09-06 16:39:56 +00:00
private SettingsWindow(Builder builder, Settings settings) : base(builder.GetObject("WindowSettings").Handle) {
Title = "Buypeeb - Settings";
2020-09-06 16:39:56 +00:00
this.settings = settings;
this.builder = builder;
builder.Autoconnect(this);
foreach (var name in generalSwitchNames) {
var s = (Switch) builder.GetObject($"Switch{name}");
generalSwitches.Add(s);
s.Active = GetSetting<bool>(PropertyName(name));
2020-09-06 16:39:56 +00:00
}
foreach (var name in updateIntervalEntryNames) {
var e = (Entry) builder.GetObject($"Entry{name}");
updateIntervalEntries.Add(e);
e.Text = GetSetting<int>(PropertyName(name)).ToString();
2020-09-06 16:39:56 +00:00
}
}
private T GetSetting<T>(string property) {
return (T) settings.GetType().GetProperty(property)?.GetValue(settings, null);
2020-09-06 16:39:56 +00:00
}
private void SetSetting<T>(string property, T value) {
settings.GetType().GetProperty(property)?.SetValue(settings, value);
2020-09-06 16:39:56 +00:00
}
private string PropertyName(string property) {
// replace "PropertyName" with "propertyName";
return property[0].ToString().ToLower() + property.Substring(1);
}
private void ButtonSaveClicked(object sender, EventArgs args) {
// first, validate all the intervals
var failed = false;
foreach (var name in updateIntervalEntryNames) {
var e = (Entry) builder.GetObject($"Entry{name}");
if (!int.TryParse(e.Text, out var result)) {
2020-09-06 16:39:56 +00:00
failed = true;
} else {
2021-06-15 02:19:12 +00:00
if (result is < 30 or > 6000) {
2020-09-06 16:39:56 +00:00
failed = true;
}
}
if (!failed) {
continue;
2020-09-06 16:39:56 +00:00
}
var md = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Ok,
"Update intervals must be a whole number between 30 and 6000.");
md.Run();
md.Dispose();
return;
2020-09-06 16:39:56 +00:00
}
// validation success!
foreach (var name in updateIntervalEntryNames) {
SetSetting(PropertyName(name), int.Parse(((Entry) builder.GetObject($"Entry{name}")).Text));
2020-09-06 16:39:56 +00:00
}
foreach (var name in generalSwitchNames) {
SetSetting(PropertyName(name), ((Switch) builder.GetObject($"Switch{name}")).Active);
2020-09-06 16:39:56 +00:00
}
Dispose();
2020-09-06 16:39:56 +00:00
}
private void ButtonCancelClicked(object sender, EventArgs args) {
Dispose();
2020-09-06 16:39:56 +00:00
}
2021-10-23 17:35:20 +00:00
private void ButtonAboutClicked(object sender, EventArgs args) {
var win = new AboutDialogue();
Application.AddWindow(win);
win.Show();
}
2020-09-06 16:39:56 +00:00
}
}