2020-09-06 16:39:56 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using Gtk;
|
2021-06-15 00:34:11 +00:00
|
|
|
|
2021-06-15 00:20:46 +00:00
|
|
|
// ReSharper disable UnusedMember.Local
|
|
|
|
// ReSharper disable UnusedParameter.Local
|
2020-09-06 16:39:56 +00:00
|
|
|
|
|
|
|
namespace Buypeeb {
|
2021-06-15 00:20:46 +00:00
|
|
|
internal class SettingsWindow : Window {
|
|
|
|
private readonly Builder builder;
|
2021-10-23 17:07:24 +00:00
|
|
|
private readonly List<Switch> generalSwitches = new();
|
|
|
|
|
|
|
|
private readonly List<string> generalSwitchNames =
|
|
|
|
new() {"ShowSecondsInListView", "Autosave", "ShowFavouritesAtTopOfList"};
|
2020-09-06 16:39:56 +00:00
|
|
|
|
2021-10-23 17:07:24 +00:00
|
|
|
private readonly Settings settings;
|
|
|
|
private readonly List<Entry> updateIntervalEntries = new();
|
2021-06-15 00:34:11 +00:00
|
|
|
|
2021-10-23 17:07:24 +00:00
|
|
|
private readonly List<string> updateIntervalEntryNames = new()
|
2021-06-15 00:34:11 +00:00
|
|
|
{"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) {
|
2021-06-15 00:20:46 +00:00
|
|
|
Title = "Buypeeb - Settings";
|
2020-09-06 16:39:56 +00:00
|
|
|
this.settings = settings;
|
|
|
|
this.builder = builder;
|
|
|
|
builder.Autoconnect(this);
|
|
|
|
|
2021-06-15 00:20:46 +00:00
|
|
|
foreach (var name in generalSwitchNames) {
|
2021-06-15 00:34:11 +00:00
|
|
|
var s = (Switch) builder.GetObject($"Switch{name}");
|
2021-06-15 00:20:46 +00:00
|
|
|
generalSwitches.Add(s);
|
|
|
|
s.Active = GetSetting<bool>(PropertyName(name));
|
2020-09-06 16:39:56 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 00:20:46 +00:00
|
|
|
foreach (var name in updateIntervalEntryNames) {
|
2021-06-15 00:34:11 +00:00
|
|
|
var e = (Entry) builder.GetObject($"Entry{name}");
|
2021-06-15 00:20:46 +00:00
|
|
|
updateIntervalEntries.Add(e);
|
|
|
|
e.Text = GetSetting<int>(PropertyName(name)).ToString();
|
2020-09-06 16:39:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private T GetSetting<T>(string property) {
|
2021-10-23 17:07:24 +00:00
|
|
|
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) {
|
2021-10-23 17:07:24 +00:00
|
|
|
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
|
2021-06-15 00:20:46 +00:00
|
|
|
var failed = false;
|
|
|
|
foreach (var name in updateIntervalEntryNames) {
|
2021-06-15 00:34:11 +00:00
|
|
|
var e = (Entry) builder.GetObject($"Entry{name}");
|
2021-06-15 00:20:46 +00:00
|
|
|
if (!int.TryParse(e.Text, out var result)) {
|
2020-09-06 16:39:56 +00:00
|
|
|
failed = true;
|
2021-06-15 00:34:11 +00:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-15 00:20:46 +00:00
|
|
|
if (!failed) {
|
|
|
|
continue;
|
2020-09-06 16:39:56 +00:00
|
|
|
}
|
2021-06-15 00:20:46 +00:00
|
|
|
|
2021-06-15 00:34:11 +00:00
|
|
|
var md = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Ok,
|
|
|
|
"Update intervals must be a whole number between 30 and 6000.");
|
2021-06-15 00:20:46 +00:00
|
|
|
md.Run();
|
|
|
|
md.Dispose();
|
|
|
|
return;
|
2020-09-06 16:39:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// validation success!
|
|
|
|
|
2021-06-15 00:20:46 +00:00
|
|
|
foreach (var name in updateIntervalEntryNames) {
|
|
|
|
SetSetting(PropertyName(name), int.Parse(((Entry) builder.GetObject($"Entry{name}")).Text));
|
2020-09-06 16:39:56 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 00:20:46 +00:00
|
|
|
foreach (var name in generalSwitchNames) {
|
|
|
|
SetSetting(PropertyName(name), ((Switch) builder.GetObject($"Switch{name}")).Active);
|
2020-09-06 16:39:56 +00:00
|
|
|
}
|
2021-06-15 00:34:11 +00:00
|
|
|
|
2021-06-15 00:20:46 +00:00
|
|
|
Dispose();
|
2020-09-06 16:39:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void ButtonCancelClicked(object sender, EventArgs args) {
|
2021-06-15 00:20:46 +00:00
|
|
|
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
|
|
|
}
|
2021-06-15 00:34:11 +00:00
|
|
|
}
|