Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
build
builddir
build*
.atom-dbg.cson
.vscode
*~
Expand Down
47 changes: 32 additions & 15 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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++;
}

Expand All @@ -103,44 +112,52 @@ 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 file = File.new_for_path (path);

Utils.check_if_datadir ();
Utils.check_if_data_dir ();

try {
file.create_readwrite (GLib.FileCreateFlags.REPLACE_DESTINATION);

} catch (Error e) {
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);
}
Expand Down
14 changes: 8 additions & 6 deletions src/Utils.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
8 changes: 4 additions & 4 deletions src/Window.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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 == "")) {
Expand Down