diff --git a/.gitignore b/.gitignore index fb7962a..0ddb942 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ -build -builddir +build* .atom-dbg.cson .vscode *~ diff --git a/src/Application.vala b/src/Application.vala index 2716dcc..aa48bd4 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -9,6 +9,8 @@ const string APP_ID = "io.github.wpkelso.slate"; public class Application : Gtk.Application { public static uint created_documents = 1; + public static string data_dir_path = Environment.get_user_data_dir () + "/slate"; + public Application () { Object ( @@ -70,22 +72,29 @@ public class Application : Gtk.Application { } this.quit (); }); + + debug ("Datadir path: %s", data_dir_path); } protected override void activate () { // Reopen all the unsaved documents we have in datadir - Utils.check_if_datadir (); - var datadir = Environment.get_user_data_dir (); + Utils.check_if_data_dir (); + try { - var pile_unsaved_documents = Dir.open (datadir); + var pile_unsaved_documents = Dir.open (data_dir_path); string? unsaved_doc = null; while ((unsaved_doc = pile_unsaved_documents.read_name ()) != null) { print (unsaved_doc); - string path = Path.build_filename (datadir, unsaved_doc); + string path = Path.build_filename (data_dir_path, unsaved_doc); File file = File.new_for_path (path); - open_file (file); + + bool ret = open_window_with_file (file); + if (!ret) { + continue; + } + created_documents++; } @@ -103,27 +112,23 @@ public class Application : Gtk.Application { protected override void open (File[] files, string hint) { foreach (var file in files) { debug ("Creating window with file: %s", file.get_basename ()); - open_file (file); + open_window_with_file (file); } } - public void open_file (File file) { - var new_window = new AppWindow (file); - add_window (new_window); - new_window.present (); - } - public static int main (string[] args) { return new Application ().run (args); } /* ---------------- HANDLERS ---------------- */ public void on_new_document () { + var name = Utils.get_new_document_name (); - var path = Path.build_filename (Environment.get_user_data_dir (), name); + var path = Path.build_filename (data_dir_path, name); var file = File.new_for_path (path); - Utils.check_if_datadir (); + Utils.check_if_data_dir (); + try { file.create_readwrite (GLib.FileCreateFlags.REPLACE_DESTINATION); @@ -131,16 +136,28 @@ public class Application : Gtk.Application { warning ("Failed to prepare target file %s\n", e.message); } - open_file (file); + open_window_with_file (file); } + public bool open_window_with_file (File file) { + if (file.query_file_type (FileQueryInfoFlags.NONE) != FileType.REGULAR) { + warning ("Couldn't open, not a regular file."); + return false; + } + + var new_window = new AppWindow (file); + add_window (new_window); + new_window.present (); + return true; + } + public void on_open_document () { var open_dialog = new Gtk.FileDialog (); open_dialog.open.begin (this.active_window, null, (obj, res) => { try { var file = open_dialog.open.end (res); - open_file (file); + open_window_with_file (file); } catch (Error err) { warning ("Failed to select file to open: %s", err.message); } diff --git a/src/Utils.vala b/src/Utils.vala index e5755a2..30aa647 100644 --- a/src/Utils.vala +++ b/src/Utils.vala @@ -19,16 +19,18 @@ return name; } - public static void check_if_datadir () { - debug ("do we have a data directory?"); - - var data_directory = File.new_for_path (Environment.get_user_data_dir ()); + public static void check_if_data_dir () { + debug ("Do we have a data directory?"); + var data_directory = File.new_for_path (Application.data_dir_path); try { if (!data_directory.query_exists ()) { data_directory.make_directory (); + debug ("No, creating data directory"); + } else { + debug ("Yes, data directory exists!"); } - } catch (Error e) { - warning ("Failed to prepare target data directory %s\n", e.message); + } catch (Error err) { + warning ("Failed to prepare target data directory %s\n", err.message); } } } diff --git a/src/Window.vala b/src/Window.vala index d259a41..db32c3c 100644 --- a/src/Window.vala +++ b/src/Window.vala @@ -112,8 +112,8 @@ public class AppWindow : Gtk.Window { } public void save_file (File file = this.file) { - if (Environment.get_user_data_dir () in this.file.get_path ()) { - Utils.check_if_datadir (); + if (Application.data_dir_path in this.file.get_path ()) { + Utils.check_if_data_dir (); } try { @@ -140,7 +140,7 @@ public class AppWindow : Gtk.Window { debug ("Save event!"); File oldfile = this.file; - bool is_unsaved_doc = (Environment.get_user_data_dir () in this.file.get_path ()); + bool is_unsaved_doc = (Application.data_dir_path in this.file.get_path ()); var save_dialog = new Gtk.FileDialog () { initial_name = (is_unsaved_doc ? file_name + ".txt" : file_name) @@ -187,7 +187,7 @@ public class AppWindow : Gtk.Window { public bool on_close () { debug ("Close event!"); - bool is_unsaved_doc = (Environment.get_user_data_dir () in this.file.get_path ()); + bool is_unsaved_doc = (Application.data_dir_path in this.file.get_path ()); // We want to delete empty unsaved documents if ((is_unsaved_doc) && (buf.text == "")) {