From 9913c3118fc1c334b39dad26f7c16b1f8c977c8f Mon Sep 17 00:00:00 2001 From: Lynne Date: Sat, 5 Sep 2020 14:15:50 +1000 Subject: [PATCH] implement save as and open buttons --- MainWindow.cs | 71 +++++++++++++++++++++++++++++++++++++++++++++++---- ui/main.glade | 4 +-- 2 files changed, 68 insertions(+), 7 deletions(-) diff --git a/MainWindow.cs b/MainWindow.cs index 7bf879e..4c83136 100755 --- a/MainWindow.cs +++ b/MainWindow.cs @@ -305,12 +305,12 @@ namespace Buypeeb { } } - private MessageDialog OkCancelDialogue(string message) { + private MessageDialog MsgBox(string message, ButtonsType buttonsType = ButtonsType.OkCancel) { var md = new MessageDialog( parent_window: this, flags: DialogFlags.DestroyWithParent | DialogFlags.Modal, type: MessageType.Question, - bt: ButtonsType.OkCancel, + bt: buttonsType, format: message ); md.KeepAbove = true; @@ -393,7 +393,7 @@ namespace Buypeeb { private void ButtonClearEndedClicked(object sender, EventArgs a) { Console.WriteLine("ButtonClearEndedClicked"); - var md = this.OkCancelDialogue("Are you sure you want to remove all ended auctions from the list?"); + var md = this.MsgBox("Are you sure you want to remove all ended auctions from the list?"); var r = (ResponseType)md.Run(); md.Dispose(); if (r != ResponseType.Ok) { @@ -418,12 +418,73 @@ namespace Buypeeb { Console.WriteLine("ButtonClearAllClicked"); } + private void ButtonOpenClicked(object sender, EventArgs a) { + var od = new FileChooserDialog( + title: "Open userdata.json", + parent: this, + action: FileChooserAction.Open, + "Cancel", ResponseType.Cancel, "Open", ResponseType.Accept + ); + + var odf = new FileFilter(); + odf.Name = "JSON files"; + odf.AddMimeType("application/json"); + odf.AddPattern("*.json"); + od.AddFilter(odf); + + if (od.Run() == (int)ResponseType.Accept) { + try { + string j = File.ReadAllText(od.Filename); + this.settings = JsonSerializer.Deserialize(j); + this.RenderList(); + this.UpdateItems(); + } + catch (Exception e) { + Console.WriteLine(e); + MsgBox($"Failed to load {od.Filename}!\n{e.Message}"); + } + } + od.Dispose(); + } + private void ButtonSaveClicked(object sender, EventArgs a) { this.SaveSettings(); } + private void ButtonSaveAsClicked(object sender, EventArgs a) { + var sd = new FileChooserDialog( + title: "Save userdata.json", + parent: this, + action: FileChooserAction.Save, + "Cancel", ResponseType.Cancel, "Save", ResponseType.Accept + ); + sd.CurrentName = "userdata.json"; + + var sdf = new FileFilter(); + sdf.Name = "JSON files"; + sdf.AddMimeType("application/json"); + sdf.AddPattern("*.json"); + sd.AddFilter(sdf); + + if (sd.Run() == (int)ResponseType.Accept) { + try { + if (!File.Exists(sd.Filename)) { + var fs = File.CreateText(sd.Filename); + fs.Close(); + } + File.WriteAllText(sd.Filename, JsonSerializer.Serialize(this.settings)); + } + catch (Exception e) { + Console.WriteLine(e); + MsgBox($"Failed to write {sd.Filename}!\n{e.Message}.", ButtonsType.Ok); + } + } + + sd.Dispose(); + } + private void ButtonQuitClicked(object sender, EventArgs a) { - var md = this.OkCancelDialogue("Are you sure you want to quit?"); + var md = this.MsgBox("Are you sure you want to quit?"); ResponseType response = (ResponseType)md.Run(); md.Dispose(); @@ -448,7 +509,7 @@ namespace Buypeeb { private void ButtonSelectedRemoveClicked(object sender, EventArgs a) { var item = this.selectedItem; - var md = this.OkCancelDialogue($"Are you sure you want to remove the item \"{item.name}\"?"); // TODO: this looks bad being all on one line + var md = this.MsgBox($"Are you sure you want to remove the item \"{item.name}\"?"); // TODO: this looks bad being all on one line ResponseType response = (ResponseType)md.Run(); md.Dispose(); diff --git a/ui/main.glade b/ui/main.glade index 61001ec..6c68989 100644 --- a/ui/main.glade +++ b/ui/main.glade @@ -152,12 +152,12 @@ True - False False Open Open True gtk-open + @@ -184,12 +184,12 @@ True - False False Export as... Export as... True gtk-save-as +