66public class AppWindow : Gtk .Window {
77 public File file { get ; set ; }
88 private Gtk . TextBuffer buf;
9- public string file_name { get ; set ; default = " unknown " ; }
10- public bool is_new { get ; set ; default = false ; }
9+ public string ? file_name { get ; set ; default = null ; }
10+ public bool is_new;
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 () {
16+ public AppWindow (File ? document ) {
1717 debug (" Constructing GUI" );
1818
1919 Intl . setlocale ();
@@ -80,7 +80,7 @@ public class AppWindow : Gtk.Window {
8080
8181 // Signal callbacks are heavily derived from similar operations in
8282 // elementary/code
83- save_as_button.clicked.connect (on_save );
83+ save_as_button.clicked.connect (on_save_as );
8484 this.close_request.connect (on_close );
8585 buf.changed.connect (on_buffer_changed );
8686
@@ -89,22 +89,35 @@ public class AppWindow : Gtk.Window {
8989 bind_property ("file_name ", this , "title ");
9090
9191 debug ("Success !");
92+
93+
94+ if (document == null ) {
95+ new_empty_doc ();
96+ }
97+
98+ open_file (document );
9299 }
93100
94101
95102 /* ---------------- FILE OPERATIONS ---------------- */
96- public void open_file (File file = this .file ) {
97- this . file = file;
103+ public void open_file (File ? file = this .file ) {
104+ debug (" Attempting to open file %s " , file. get_basename ());
105+
106+ if (file = null ) {
107+ is_new = true ;
108+
109+ this . file_name = Slate . Utils . get_new_document_name ();
110+ this . file = File . new_for_path (Environment . get_user_data_dir () + ' /' + name);
98111
99- if (this . is_new) {
100112 try {
101- file. create (GLib . FileCreateFlags . REPLACE_DESTINATION );
102- this . file_name = file. get_basename ();
113+ this . file. create (GLib . FileCreateFlags . REPLACE_DESTINATION );
103114 } catch (Error err) {
104- warning (" Couldn't create file: %s " , err. message);
115+ warning (" Couldn't create file: %s " , err. message);
105116 }
117+
106118 } else {
107- debug (" Attempting to open file %s " , file. get_basename ());
119+ this . file = file;
120+
108121 try {
109122 this . file_name = file. get_basename ();
110123 var distream = new DataInputStream (file. read (null ));
@@ -117,6 +130,13 @@ public class AppWindow : Gtk.Window {
117130 }
118131
119132 public void save_file (File file = this .file ) {
133+
134+ // We have to always check if nothing happened to datadir
135+ // This way if the user deleted in the meantime everything, we still can save unsaved docs
136+ if (is_new) {
137+ Slate . Utils . check_if_datadir;
138+ }
139+
120140 try {
121141 debug (" Attempting to save the buffer to disk.." );
122142 DataOutputStream dostream;
@@ -135,9 +155,8 @@ public class AppWindow : Gtk.Window {
135155 }
136156 }
137157
138-
139158 /* ---------------- HANDLERS ---------------- */
140- public void on_save () {
159+ public void on_save_as () {
141160 debug (" Save event!" );
142161 var save_dialog = new Gtk .FileDialog () { initial_name = file_name };
143162
@@ -147,7 +166,7 @@ public class AppWindow : Gtk.Window {
147166 file_name = file. get_basename ();
148167 if (is_new) { is_new = false ; }
149168 save_file (file);
150-
169+
151170 } catch (Error err) {
152171 warning (" Failed to save file: %s " , err. message);
153172 }
0 commit comments