Skip to content

Commit e6be20e

Browse files
committed
Revert fucky changes and rewrite from scratch
1 parent be8e82e commit e6be20e

File tree

6 files changed

+132
-141
lines changed

6 files changed

+132
-141
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ builddir
88

99
# subprojects/*/
1010
.flatpak-builder
11-
.vscode
11+
.vscode
12+
.flatpak/meson.sh

meson.build

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,8 @@ executable(
4848

4949
subdir('data')
5050
subdir('po')
51+
52+
gnome.post_install(
53+
gtk_update_icon_cache: true,
54+
update_desktop_database: true
55+
)

src/Application.vala

Lines changed: 83 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -43,86 +43,112 @@ public class Application : Gtk.Application {
4343
"new-document",
4444
null
4545
);
46-
47-
new_document_action.activate.connect (() => {
48-
var name = get_new_document_name ();
49-
var path = Path.build_filename (Environment.get_user_data_dir (), name);
50-
var file = File.new_for_path (path);
51-
debug (
52-
"Document is unsaved, creating an save location at: %s",
53-
path
54-
);
55-
56-
var new_window = new AppWindow () {
57-
is_new = true,
58-
};
59-
new_window.open_file (file);
60-
61-
debug ("Created new window with name %s", name);
62-
add_window (new_window);
63-
new_window.present ();
64-
});
46+
new_document_action.activate.connect (on_new_document);
6547
this.add_action (new_document_action);
6648

6749
SimpleAction open_document_action = new SimpleAction (
6850
"open-document",
6951
null
7052
);
71-
open_document_action.activate.connect (() => {
72-
var open_dialog = new Gtk.FileDialog ();
73-
var new_window = new AppWindow ();
74-
75-
open_dialog.open.begin (new_window, null, (obj, res) => {
76-
try {
77-
var file = open_dialog.open.end (res);
78-
new_window.open_file (file);
79-
add_window (new_window);
80-
new_window.present ();
81-
} catch (Error err) {
82-
warning ("Failed to select file to open: %s", err.message);
83-
}
84-
});
85-
});
53+
open_document_action.activate.connect (on_open_document);
8654
this.add_action (open_document_action);
8755
}
8856

8957
protected override void activate () {
9058

91-
Slate.Utils.check_if_datadir ();
92-
saved_unsaved_documents = Environment.get_user_data_dir ();
93-
pile_unsaved_documents = saved_unsaved_documents.open ();
94-
95-
// Conveniently, if there is no unsaved document, we just get NULL
96-
// Which AppWindow will process as a new unsaved doc
97-
foreach (var unsaved_document in pile_unsaved_documents.read_name () ) {
98-
99-
debug (
100-
"Document is unsaved, creating an save location at: %s",
101-
path
102-
);
103-
104-
var new_window = new AppWindow (unsaved_document) {
105-
is_new = true,
106-
};
59+
// Reopen all the unsaved documents we have in datadir
60+
check_if_datadir ();
61+
var datadir = Environment.get_user_data_dir ();
62+
try {
63+
var pile_unsaved_documents = Dir.open (datadir);
64+
65+
string? unsaved_doc = null;
66+
while ((unsaved_doc = pile_unsaved_documents.read_name ()) != null) {
67+
print (unsaved_doc);
68+
string path = Path.build_filename (datadir, unsaved_doc);
69+
File file = File.new_for_path (path);
70+
open_file (file);
71+
created_documents++;
72+
}
73+
74+
} catch (Error e) {
75+
warning ("Cannot read datadir! Is the disk okay? %s\n", e.message);
76+
}
10777

108-
add_window (new_window);
109-
new_window.present ();
78+
// What if there was none ? The loop wouldnt happen at all.
79+
if (created_documents == 1) {
80+
on_new_document ();
11081
}
82+
11183
}
11284

11385
protected override void open (File[] files, string hint) {
11486
foreach (var file in files) {
11587
debug ("Creating window with file: %s", file.get_basename ());
116-
var window = new AppWindow ();
117-
window.open_file (file);
88+
open_file (file);
89+
}
90+
}
91+
92+
string get_new_document_name () {
93+
var name = _("New Document");
94+
if (created_documents > 1) {
95+
name = name + " " + created_documents.to_string ();
96+
}
11897

119-
debug ("Adding new window to application");
120-
add_window (window);
121-
window.present ();
98+
debug ("New document name is: %s",name);
99+
100+
created_documents++;
101+
102+
return name;
103+
}
104+
105+
public void check_if_datadir () {
106+
debug ("do we have a data directory?");
107+
var data_directory = File.new_for_path (Environment.get_user_data_dir ());
108+
try {
109+
if (!data_directory.query_exists ()) {
110+
data_directory.make_directory ();
111+
}
112+
} catch (Error e) {
113+
warning ("Failed to prepare target data directory %s\n", e.message);
122114
}
123115
}
124116

125117
public static int main (string[] args) {
126118
return new Application ().run (args);
127119
}
120+
121+
public void on_new_document () {
122+
var name = get_new_document_name ();
123+
var path = Path.build_filename (Environment.get_user_data_dir (), name);
124+
var file = File.new_for_path (path);
125+
126+
check_if_datadir ();
127+
try {
128+
file.create_readwrite (GLib.FileCreateFlags.REPLACE_DESTINATION);
129+
} catch (Error e) {
130+
warning ("Failed to prepare target file %s\n", e.message);
131+
}
132+
133+
open_file (file);
134+
135+
}
136+
137+
public void open_file (File file) {
138+
var new_window = new AppWindow (file);
139+
add_window (new_window);
140+
new_window.present ();
141+
}
142+
143+
public void on_open_document() {
144+
var open_dialog = new Gtk.FileDialog ();
145+
open_dialog.open.begin (this.active_window, null, (obj, res) => {
146+
try {
147+
var file = open_dialog.open.end (res);
148+
open_file (file);
149+
} catch (Error err) {
150+
warning ("Failed to select file to open: %s", err.message);
151+
}
152+
});
153+
}
128154
}

src/Utils.vala

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/Window.vala

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
public class AppWindow : Gtk.Window {
77
public File file { get; set; }
88
private Gtk.TextBuffer buf;
9-
public string? file_name { get; set; default = null; }
10-
public bool is_new;
9+
public string file_name { get; set; }
10+
public bool is_new { get; set; default = false; }
1111

1212
// Add a debounce so we aren't writing the entire buffer every character input
1313
public int interval = 500; // ms
1414
public uint debounce_timer_id = 0;
1515

16-
public AppWindow (File? document) {
16+
public AppWindow (File file) {
1717
debug ("Constructing GUI");
1818

1919
Intl.setlocale ();
@@ -63,6 +63,7 @@ public class AppWindow : Gtk.Window {
6363
wrap_mode = WORD_CHAR,
6464
};
6565
buf = text_view.buffer;
66+
buf.text = "";
6667

6768

6869
var scrolled_view = new Gtk.ScrolledWindow () {
@@ -90,47 +91,25 @@ public class AppWindow : Gtk.Window {
9091

9192
debug ("Success!");
9293

93-
open_file (document);
94+
open_file (file);
9495
}
9596

96-
/* ---------------- FILE OPERATIONS ---------------- */
97-
public void open_file (File? file = this.file) {
98-
debug ("Attempting to open file %s", file.get_basename ());
99-
100-
if (file = null) {
101-
is_new = true;
102-
103-
this.file_name = Slate.Utils.get_new_document_name ();
104-
this.file = File.new_for_path (Environment.get_user_data_dir () + '/' + name);
105-
106-
try {
107-
this.file.create (GLib.FileCreateFlags.REPLACE_DESTINATION);
108-
} catch (Error err) {
109-
warning ("Couldn't create file: %s", err.message);
110-
}
11197

112-
} else {
113-
this.file = file;
114-
115-
try {
116-
this.file_name = file.get_basename ();
117-
var distream = new DataInputStream (file.read (null));
118-
var contents = distream.read_upto ("", -1, null);
119-
buf.set_text (contents);
120-
} catch (Error err) {
121-
warning ("Couldn't open file: %s", err.message);
122-
}
98+
/* ---------------- FILE OPERATIONS ---------------- */
99+
public void open_file (File file = this.file) {
100+
this.file = file;
101+
debug ("Attempting to open file %s", file.get_basename ());
102+
try {
103+
this.file_name = file.get_basename ();
104+
var distream = new DataInputStream (file.read (null));
105+
var contents = distream.read_upto ("", -1, null);
106+
buf.set_text (contents);
107+
} catch (Error err) {
108+
warning ("Couldn't open file: %s", err.message);
123109
}
124110
}
125111

126112
public void save_file (File file = this.file) {
127-
128-
// We have to always check if nothing happened to datadir
129-
// This way if the user deleted in the meantime everything, we still can save unsaved docs
130-
if (is_new) {
131-
Slate.Utils.check_if_datadir ();
132-
}
133-
134113
try {
135114
debug ("Attempting to save the buffer to disk..");
136115
DataOutputStream dostream;
@@ -149,22 +128,36 @@ public class AppWindow : Gtk.Window {
149128
}
150129
}
151130

131+
152132
/* ---------------- HANDLERS ---------------- */
153133
public void on_save_as () {
154134
debug ("Save event!");
155135
var save_dialog = new Gtk.FileDialog () { initial_name = file_name };
136+
File oldfile = this.file;
137+
bool delete_after = false;
138+
139+
if (Environment.get_user_data_dir () in this.file.get_path ()) {
140+
delete_after = true;
141+
}
156142

157143
save_dialog.save.begin (this, null, (obj, res) => {
158144
try {
145+
146+
159147
file = save_dialog.save.end (res);
160148
file_name = file.get_basename ();
161-
if (is_new) { is_new = false; }
162149
save_file (file);
163150

151+
if ((delete_after) && (oldfile != file)) {
152+
oldfile.delete ();
153+
}
154+
164155
} catch (Error err) {
165156
warning ("Failed to save file: %s", err.message);
166157
}
167158
});
159+
160+
168161
}
169162

170163
public void on_buffer_changed () {
@@ -184,20 +177,24 @@ public class AppWindow : Gtk.Window {
184177

185178
}
186179

187-
public void on_close () {
180+
public bool on_close () {
188181
debug ("Close event!");
189-
save_file ();
190182

191-
if (is_new) {
183+
bool is_unsaved_doc = (Environment.get_user_data_dir () in this.file.get_path ());
184+
185+
// We want to delete empty unsaved documents
186+
if ((is_unsaved_doc) && (buf.text == "")) {
187+
192188
try {
193189
this.file.delete ();
194190
} catch (Error err) {
195-
warning (
196-
"The persistent file couldn't be deleted: %s",
197-
err.message
198-
);
191+
warning ("Failed to delete empty temp file: %s", err.message);
199192
}
193+
194+
} else {
195+
save_file ();
200196
}
197+
201198
return false;
202199
}
203200
}

src/meson.build

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
sources += files(
22
'Window.vala',
33
'Application.vala',
4-
'Utils.vala',
54
)

0 commit comments

Comments
 (0)