diff --git a/MainWindow.cs b/MainWindow.cs index da0cf0e..79f60c4 100755 --- a/MainWindow.cs +++ b/MainWindow.cs @@ -513,7 +513,7 @@ namespace Buypeeb { sorted = values.OrderBy(item => item.endDate); } - if (settings.displayFavouritesAtTopOfList) { + if (settings.showFavouritesAtTopOfList) { foreach (var item in sorted.Where(item => item.favourite)) { items.AppendValues(item); } @@ -737,6 +737,13 @@ namespace Buypeeb { } } + private void ButtonSettingsClicked(object sender, EventArgs args) { + var win = new SettingsWindow(this.settings); + Application.AddWindow(win); + win.DeleteEvent += this.WindowSettingsClosed; + win.Show(); + } + private void TreeViewItemsSelectionChanged(object sender, EventArgs a) { this.UpdateSelectionView(); } @@ -784,7 +791,7 @@ namespace Buypeeb { ToggleButton s = (ToggleButton)sender; this.selectedItem.favourite = s.Active; - if (this.settings.displayFavouritesAtTopOfList) { + if (this.settings.showFavouritesAtTopOfList) { var id = this.selectedItem.id; this.RenderList(); } @@ -795,7 +802,6 @@ namespace Buypeeb { this.items.EmitRowChanged(pathAndIter.path, pathAndIter.iter); } } - } private void ButtonSelectedNotesClearClicked(object sender, EventArgs args) { @@ -822,6 +828,10 @@ namespace Buypeeb { this.RenderList(); } + private void WindowSettingsClosed(object sender, EventArgs args) { + this.RenderList(); + } + // timers /// @@ -920,7 +930,7 @@ namespace Buypeeb { ending += end.ToString("MMM d "); } ending += end.ToString("HH:mm"); - if (this.settings.displaySecondsInList) { + if (this.settings.showSecondsInListView) { // add the seconds on to the end ending += end.ToString(":ss"); } diff --git a/Settings.cs b/Settings.cs index 18f6808..b2c14c4 100644 --- a/Settings.cs +++ b/Settings.cs @@ -8,8 +8,9 @@ namespace Buypeeb { public int favouriteUpdateInterval { get; set; } = 5 * 60; public int updateIntervalCritical { get; set; } = 60; public int favouriteUpdateIntervalCritical { get; set; } = 30; - public bool displaySecondsInList { get; set; } = true; - public bool displayFavouritesAtTopOfList { get; set; } = true; + public bool showSecondsInListView { get; set; } = true; + public bool autosave { get; set; } = true; + public bool showFavouritesAtTopOfList { get; set; } = true; public Dictionary watchlist { get; set; diff --git a/SettingsWindow.cs b/SettingsWindow.cs new file mode 100644 index 0000000..8ca7bb8 --- /dev/null +++ b/SettingsWindow.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; +using Gtk; + +namespace Buypeeb { + class SettingsWindow : Window { + private List generalSwitches = new List(); + private List updateIntervalEntries = new List(); + private Settings settings; + private Builder builder; + + private List generalSwitchNames = new List { "ShowSecondsInListView", "Autosave", "ShowFavouritesAtTopOfList" }; + private List updateIntervalEntryNames = new List { "UpdateInterval", "UpdateIntervalCritical", "FavouriteUpdateInterval", "FavouriteUpdateIntervalCritical" }; + + public SettingsWindow(Settings settings) : this(new Builder("settings.glade"), settings) { } + + private SettingsWindow(Builder builder, Settings settings) : base(builder.GetObject("WindowSettings").Handle) { + this.Title = "Buypeeb - Settings"; + this.settings = settings; + this.builder = builder; + builder.Autoconnect(this); + + foreach (var name in this.generalSwitchNames) { + var s = (Switch)builder.GetObject($"Switch{name}"); + this.generalSwitches.Add(s); + s.Active = GetSetting(this.PropertyName(name)); + } + + foreach (var name in this.updateIntervalEntryNames) { + var e = (Entry)builder.GetObject($"Entry{name}"); + this.updateIntervalEntries.Add(e); + e.Text = GetSetting(this.PropertyName(name)).ToString(); + } + } + + private T GetSetting(string property) { + return (T)this.settings.GetType().GetProperty(property).GetValue(this.settings, null); + } + + private void SetSetting(string property, T value) { + this.settings.GetType().GetProperty(property).SetValue(this.settings, value); + } + + 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 + bool failed = false; + foreach (var name in this.updateIntervalEntryNames) { + var e = (Entry)builder.GetObject($"Entry{name}"); + if (!int.TryParse(e.Text, out int result)) { + failed = true; + } + else { + if (result < 30 || result > 6000) { + failed = true; + } + } + + if (failed) { + 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; + } + } + + // validation success! + + foreach (var name in this.updateIntervalEntryNames) { + this.SetSetting(PropertyName(name), int.Parse((builder.GetObject($"Entry{name}") as Entry).Text)); + } + + foreach (var name in this.generalSwitchNames) { + this.SetSetting(PropertyName(name), (builder.GetObject($"Switch{name}") as Switch).Active); + } + this.Dispose(); + } + + private void ButtonCancelClicked(object sender, EventArgs args) { + this.Dispose(); + } + } +} \ No newline at end of file diff --git a/ui/main.glade b/ui/main.glade index 5073c96..1e73756 100644 --- a/ui/main.glade +++ b/ui/main.glade @@ -1,5 +1,5 @@ - + @@ -253,6 +253,9 @@ False 810 500 + + + True @@ -447,12 +450,12 @@ True - False False Settings Settings True gtk-preferences + @@ -1140,8 +1143,5 @@ - - - diff --git a/ui/settings.glade b/ui/settings.glade index 5c89f23..2d631b4 100644 --- a/ui/settings.glade +++ b/ui/settings.glade @@ -2,8 +2,9 @@ - + False + True 440 @@ -29,6 +30,8 @@ False 5 5 + 5 + 5 5 @@ -44,7 +47,7 @@ - + True True end @@ -68,7 +71,7 @@ - + True True end @@ -78,6 +81,30 @@ 1 + + + True + False + start + True + Show favourites at top of list + + + 0 + 2 + + + + + True + True + end + + + 1 + 2 + + @@ -95,7 +122,10 @@ True False 5 + 5 5 + 20 + 3 True @@ -194,13 +224,14 @@ - + True True 5 5 2 2 + 3 5 number @@ -210,13 +241,14 @@ - + True True 5 5 2 2 + 3 5 number @@ -226,13 +258,14 @@ - + True True 5 5 2 2 + 3 5 number @@ -242,13 +275,14 @@ - + True True 5 5 2 2 + 3 5 number @@ -287,7 +321,7 @@ 5 - About + About Buypeeb True True True @@ -300,10 +334,11 @@ - Cancel + Save True True True + False @@ -314,10 +349,11 @@ - Save + Cancel True True True + False