Skip to content

Commit

Permalink
Note is saved on quit
Browse files Browse the repository at this point in the history
  • Loading branch information
lainsce committed May 27, 2017
1 parent 667116a commit 4292947
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 48 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ add_definitions(${DEPS_CFLAGS})
link_libraries(${DEPS_LIBRARIES})
link_directories(${DEPS_LIBRARY_DIRS})

# use config to surface app build details
configure_file (${CMAKE_SOURCE_DIR}/cmake/config.h.cmake ${CMAKE_BINARY_DIR}/config.h)
add_definitions (-DGETTEXT_PACKAGE=\"${GETTEXT_PACKAGE}\")

# make sure we have vala
find_package(Vala REQUIRED)
# make sure we use vala
Expand All @@ -64,6 +68,9 @@ PACKAGES
gtksourceview-3.0

# some extra options
CUSTOM_VAPIS
vapi/config.vapi

OPTIONS
${GRANITE_OPTIONS}
)
Expand Down
12 changes: 12 additions & 0 deletions cmake/config.h.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef CONFIG_H
#define CONFIG_H

#cmakedefine DATADIR "@DATADIR@"
#cmakedefine PKGDATADIR "@PKGDATADIR@"
#cmakedefine GETTEXT_PACKAGE "@GETTEXT_PACKAGE@"
#cmakedefine RELEASE_NAME "@RELEASE_NAME@"
#cmakedefine VERSION "@VERSION@"
#cmakedefine VERSION_INFO "@VERSION_INFO@"
#cmakedefine PLUGINDIR "@PLUGINDIR@"

#endif // CONFIG_H
7 changes: 4 additions & 3 deletions src/Constants/Stylesheet.vala
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ namespace Notejot.Stylesheet {
@define-color colorPrimary #fff1b9;
@define-color textColorPrimary #656565;
notejot-window .background {
.notejot-window {
background-color: #fdf6e3;
}
GtkSourceView {
.notejot-note {
background-color: #fdf6e3;
font-size: 11px;
}
GtkSourceView:selected {
.notejot-note:selected {
background-color: #93a1a1;
color: #fdf6e3;
}
Expand Down
6 changes: 5 additions & 1 deletion src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ namespace Notejot {

construct {
this.get_style_context ().add_class ("rounded");
this.get_style_context ().add_class ("notejot-window");
this.toolbar = new Widgets.Toolbar ();
var header_context = toolbar.get_style_context ();
header_context.add_class ("notejot-window");

this.window_position = Gtk.WindowPosition.CENTER;
this.set_titlebar (toolbar);
this.show_all ();
Expand All @@ -51,6 +53,8 @@ namespace Notejot {
this.add (scroll);
this.view = new Widgets.SourceView ();
scroll.add (view);

Utils.FileUtils.load_tmp_file ();
}
}
}
21 changes: 2 additions & 19 deletions src/Utils/DialogUtils.vala
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@ 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);
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);
}
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 @@ -44,19 +40,6 @@ namespace Notejot.Utils.DialogUtils {
return chooser;
}

/**
* Display an open dialog and return the selected file. Returned file should exist.
*/
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 ();
chooser.destroy();
return file;
}

/**
* Display a save dialog and return the selected file.
*/
Expand Down
50 changes: 41 additions & 9 deletions src/Utils/FileUtils.vala
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,56 @@

namespace Notejot.Utils.FileUtils {

File tmp_file;

// Save a buffer to a file.
public void save_file (File file, uint8[] buffer) throws Error {
var output = new DataOutputStream (file.create
(FileCreateFlags.REPLACE_DESTINATION));
(FileCreateFlags.NONE));
long written = 0;
while (written < buffer.length)
written += output.write (buffer[written:buffer.length]);
// No close method? This is scary, GLib. Very scary.
}

// Read a file and get the contents.
public string open_file (File file) throws Error {
string content = "";
var input = new DataInputStream (file.read ());
string line;
while ((line = input.read_line (null)) != null)
content += line + "\n";
return content;
private void load_tmp_file () {
Granite.Services.Paths.initialize ("notejot", Build.PKGDATADIR);
Granite.Services.Paths.ensure_directory_exists (Granite.Services.Paths.user_data_folder);

tmp_file = Granite.Services.Paths.user_data_folder.get_child ("temp");

if ( !tmp_file.query_exists () ) {
try {
tmp_file.create (FileCreateFlags.NONE);
} catch (Error e) {
error ("Error: %s\n", e.message);
}
}

try {
string text;
string filename = tmp_file.get_path ();

GLib.FileUtils.get_contents (filename, out text);
Widgets.SourceView.buffer.text = text;
} catch (Error e) {
error ("%s", e.message);
}
}

private void save_tmp_file () {
if ( tmp_file.query_exists () ) {
try {
tmp_file.delete();
} catch (Error e) {
error ("Error: %s\n", e.message);
}
}

Gtk.TextIter start, end;
Widgets.SourceView.buffer.get_bounds (out start, out end);
string buffer = Widgets.SourceView.buffer.get_text (start, end, true);
uint8[] binbuffer = buffer.data;
save_file(tmp_file, binbuffer);
}
}
8 changes: 6 additions & 2 deletions src/Widgets/SourceView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@

namespace Notejot.Widgets {
public class SourceView : Gtk.SourceView {
public static Gtk.SourceBuffer buffer;
public static new Gtk.SourceBuffer buffer;
public static bool is_modified;

public SourceView () {
construct {
var context = this.get_style_context ();
context.add_class ("notejot-note");

buffer = new Gtk.SourceBuffer (null);
this.set_buffer (buffer);

Expand All @@ -38,6 +41,7 @@ namespace Notejot.Widgets {
}

public void on_text_modified () {
Utils.FileUtils.save_tmp_file ();
if (!is_modified) {
is_modified = true;
}
Expand Down
22 changes: 8 additions & 14 deletions src/Widgets/Toolbar.vala
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace Notejot.Widgets {

var save_item = new Gtk.MenuItem.with_label (_("Save as…"));
save_item.activate.connect(() => {
button_pressed ();
save_button_pressed ();
});

var about_item = new Gtk.MenuItem.with_label (_("About"));
Expand Down Expand Up @@ -78,40 +78,34 @@ namespace Notejot.Widgets {
});
}

public void button_pressed () {
debug ("Button pressed.");
public void save_button_pressed () {
debug ("Save button pressed.");

if (Widgets.SourceView.is_modified = true) {
try {
bool was_saved = save_document ();
if (!was_saved) {
debug ("Cancelling new document too.");
return;
}
debug ("Saving file...");
save_document ();
} catch (Error e) {
error ("Unexpected error during save: " + e.message);
}
}

debug ("Clearing buffer.");
Widgets.SourceView.buffer.set_text ("");
file = null;
Widgets.SourceView.is_modified = false;
}

private bool save_document () throws Error {
public bool save_document () throws Error {
// If it's a new file, ask the user for a valid location.
if (file == null) {
debug ("This is a new file. Asking the user where to save.");
debug ("Asking the user where to save.");
file = Utils.DialogUtils.display_save_dialog ();
// If file is still null, then user aborted save operation.
if (file == null) {
debug ("User cancelled operation. Aborting operation.");
debug ("User cancelled operation. Aborting.");
return false;
}
}

debug ("Attempting to save file: " + file.get_path ());
if (file.query_exists ())
file.delete ();

Expand Down
10 changes: 10 additions & 0 deletions vapi/config.vapi
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[CCode (cprefix = "", lower_case_cprefix = "", cheader_filename = "config.h")]
namespace Build {
public const string DATADIR;
public const string PKGDATADIR;
public const string GETTEXT_PACKAGE;
public const string RELEASE_NAME;
public const string VERSION;
public const string VERSION_INFO;
public const string PLUGINDIR;
}

0 comments on commit 4292947

Please sign in to comment.