implement save as and open buttons

This commit is contained in:
Lynne Megido 2020-09-05 14:15:50 +10:00
parent 4544acd01e
commit 9913c3118f
Signed by: lynnesbian
GPG key ID: F0A184B5213D9F90
2 changed files with 68 additions and 7 deletions

View file

@ -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<Settings>(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();

View file

@ -152,12 +152,12 @@
<child>
<object class="GtkToolButton">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">False</property>
<property name="tooltip_text" translatable="yes">Open</property>
<property name="label" translatable="yes">Open</property>
<property name="use_underline">True</property>
<property name="stock_id">gtk-open</property>
<signal name="clicked" handler="ButtonOpenClicked" swapped="no"/>
<accelerator key="o" signal="clicked" modifiers="GDK_CONTROL_MASK"/>
</object>
<packing>
@ -184,12 +184,12 @@
<child>
<object class="GtkToolButton">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">False</property>
<property name="tooltip_text" translatable="yes">Export as...</property>
<property name="label" translatable="yes">Export as...</property>
<property name="use_underline">True</property>
<property name="stock_id">gtk-save-as</property>
<signal name="clicked" handler="ButtonSaveAsClicked" swapped="no"/>
<accelerator key="e" signal="clicked" modifiers="GDK_SHIFT_MASK | GDK_CONTROL_MASK"/>
</object>
<packing>