Skip to content

Commit

Permalink
1.0.3 - The ability to open notes.
Browse files Browse the repository at this point in the history
  • Loading branch information
lainsce committed May 28, 2017
1 parent 8509810 commit 37f5b8a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/Utils/DialogUtils.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand All @@ -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.
*/
Expand Down
41 changes: 41 additions & 0 deletions src/Widgets/Toolbar.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.");

Expand All @@ -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) {
Expand Down

0 comments on commit 37f5b8a

Please sign in to comment.