diff --git a/src/Utils/DialogUtils.vala b/src/Utils/DialogUtils.vala index ac270488..cae779a1 100644 --- a/src/Utils/DialogUtils.vala +++ b/src/Utils/DialogUtils.vala @@ -25,8 +25,12 @@ namespace Notejot.Utils.DialogUtils { // Init the FileChooser, based on what the calling method desires. var chooser = new Gtk.FileChooserDialog (title, null, action); chooser.add_button (Gtk.Stock.CANCEL, Gtk.ResponseType.CANCEL); - chooser.add_button (Gtk.Stock.SAVE, Gtk.ResponseType.ACCEPT); - chooser.set_do_overwrite_confirmation (true); + if (action == Gtk.FileChooserAction.OPEN) { + chooser.add_button (Gtk.Stock.OPEN, Gtk.ResponseType.ACCEPT); + } else if (action == Gtk.FileChooserAction.SAVE) { + chooser.add_button (Gtk.Stock.SAVE, Gtk.ResponseType.ACCEPT); + chooser.set_do_overwrite_confirmation (true); + } var filter = new Gtk.FileFilter (); filter.set_filter_name (_("Text files")); @@ -40,6 +44,26 @@ namespace Notejot.Utils.DialogUtils { return chooser; } + public File display_open_dialog () { + var chooser = create_file_chooser (_("Open file"), + Gtk.FileChooserAction.OPEN); + File file = null; + if (chooser.run () == Gtk.ResponseType.ACCEPT) + file = chooser.get_file (); + + var filter = new Gtk.FileFilter (); + filter.set_filter_name (_("Text files")); + filter.add_pattern ("*.txt"); + chooser.add_filter (filter); + filter = new Gtk.FileFilter (); + filter.set_filter_name (_("All files")); + filter.add_pattern ("*"); + chooser.add_filter (filter); + + chooser.destroy(); + return file; + } + /** * Display a save dialog and return the selected file. */ diff --git a/src/Widgets/Toolbar.vala b/src/Widgets/Toolbar.vala index af94fd03..364e6a3e 100644 --- a/src/Widgets/Toolbar.vala +++ b/src/Widgets/Toolbar.vala @@ -54,11 +54,17 @@ namespace Notejot.Widgets { save_button_pressed (); }); + var open_item = new Gtk.MenuItem.with_label (_("Open…")); + open_item.activate.connect(() => { + open_button_pressed (); + }); + var about_item = new Gtk.MenuItem.with_label (_("About")); about_item.activate.connect(() => { show_about_dialog (); }); + menu.add (open_item); menu.add (save_item); menu.add (new Gtk.SeparatorMenuItem ()); menu.add (about_item); @@ -92,6 +98,22 @@ namespace Notejot.Widgets { }); } + public void open_button_pressed () { + debug ("Open button pressed."); + + if (Widgets.SourceView.is_modified = true) { + try { + debug ("Opening file..."); + open_document (); + } catch (Error e) { + error ("Unexpected error during open: " + e.message); + } + } + + file = null; + Widgets.SourceView.is_modified = false; + } + public void save_button_pressed () { debug ("Save button pressed."); @@ -108,6 +130,25 @@ namespace Notejot.Widgets { Widgets.SourceView.is_modified = false; } + public bool open_document () throws Error { + // If it's a file, ask the user for a valid location. + if (file == null) { + debug ("Asking the user what to open."); + file = Utils.DialogUtils.display_open_dialog (); + // If file is still null, then user aborted open operation. + if (file == null) { + debug ("User cancelled operation. Aborting."); + return false; + } + } + + string text; + FileUtils.get_contents (file.get_path(), out text); + + Widgets.SourceView.buffer.text = text; + return true; + } + public bool save_document () throws Error { // If it's a new file, ask the user for a valid location. if (file == null) {