Debian Codegraph @

src/logic/NoteManager.vala

raw
1 /*
2 * Copyright (c) 2017-2019 José Amuedo (https://github.com/spheras)
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 public errordomain NoteManagerIOError {
19 FILE_EXISTS,
20 MOVE_ERROR
21 }
22
23 /**
24 * @class
25 * Item Manager that represents an Icon of a File or Folder
26 */
27 public class DesktopFolder.NoteManager : Object {
28 /** parent application */
29 private DesktopFolderApp application;
30 /** the File object associated with the item */
31 private File file;
32 /** the view associated with this manager */
33 private NoteWindow view;
34 /** name of the note */
35 private string note_name = null;
36 /** Note Settings of this note */
37 private NoteSettings settings = null;
38 /** flag to get the validity of the note */
39 private bool flag_valid = true;
40 /**
41 * @constructor
42 * @param DesktopFolderApp application the application of this note
43 * @param string note_name the name of the note
44 * @param File file the GLib File object for the file associated with this item
45 */
46 public NoteManager (DesktopFolderApp application, string note_name, File file) {
47 this.note_name = note_name;
48 this.file = file;
49
50 // Let's load the settings of the folder (if exist or a new one)
51 if (!this.load_note_settings ()) {
52 // removing the settings file
53 if (this.file.query_exists ()) {
54 try {
55 this.file.trash ();
56 } catch (Error error) {
57 stderr.printf ("Error: %s\n", error.message);
58 Util.show_error_dialog ("Error", error.message);
59 }
60 }
61 this.flag_valid = false;
62 } else {
63 // First we create a Note Window above the desktop
64 this.application = application;
65 this.view = new NoteWindow (this);
66 this.application.add_window (this.view);
67 this.view.show ();
68 }
69 }
70
71 /**
72 * @name on_screen_size_changed
73 * @description detecting screen size changes
74 */
75 public void on_screen_size_changed (Gdk.Screen screen) {
76 this.settings.calculate_current_position ();
77 this.view.reload_settings ();
78 }
79
80 /**
81 * @name is_valid
82 * @description return the validity of the note widget
83 * @return {bool} true->yes, it is valid
84 */
85 public bool is_valid () {
86 return this.flag_valid;
87 }
88
89 /**
90 * @name load_note_settings
91 * @description load the settings of this note.
92 * The note/settings file contains all the info needed to create the note position, size, etc.. and the text itself
93 */
94 private bool load_note_settings () {
95 // let's search the folder settings file
96 var abs_path = this.get_absolute_path ();
97 debug ("loading note settings...%s", abs_path);
98 if (!this.file.query_exists ()) {
99 warning ("note file does not exist!");
100 return false;
101 } else {
102 NoteSettings existent = NoteSettings.read_settings (this.file, this.get_note_name ());
103 if (existent == null) {
104 // something bad occurred, we must delete this note widget
105 return false;
106 } else {
107 this.settings = existent;
108 }
109 }
110 return true;
111 }
112
113 /**
114 * @name reopen
115 * @description close the current view and reopen it again
116 * (mainly to reposition the window on top or back, and most important, the icon over the status bar)
117 */
118 public void reopen () {
119 this.view.save_current_position_and_size ();
120 this.get_settings ().save ();
121
122 // closing
123 this.application.remove_window (this.view);
124 this.view.close ();
125 // reopening
126 this.view = new NoteWindow (this);
127 this.application.add_window (this.view);
128 this.view.show ();
129 }
130
131 /**
132 * @name on_text_change
133 * @description the text change event was produced
134 * @param {string} text the new text of the note
135 */
136 public void on_text_change (string text) {
137 this.settings.text = text;
138 this.settings.save ();
139 }
140
141 /**
142 * @name get_settings
143 * @description return the settings of this note
144 * @return FolderSettings the settings of this note
145 */
146 public NoteSettings get_settings () {
147 return this.settings;
148 }
149
150 /**
151 * @name get_note_name
152 * @description return the note name
153 * @return string the note name
154 */
155 public string get_note_name () {
156 return this.note_name;
157 }
158
159 /**
160 * @name get_application
161 * @description return the desktop folder application
162 * @return DesktopFolderApp
163 */
164 public DesktopFolderApp get_application () {
165 return this.application;
166 }
167
168 /**
169 * @name get_view
170 * @description return the view of this manager
171 * @return ItemView
172 */
173 public NoteWindow get_view () {
174 return this.view;
175 }
176
177 /**
178 * @name show_view
179 * @description show the folder
180 */
181 public void show_view () {
182 // setting opacity to stop the folder window flashing at startup
183 this.view.opacity = 1;
184 this.view.show_all ();
185 this.view.fade_in ();
186 }
187
188 /**
189 * @name hide_view
190 * @description hide the folder
191 */
192 public void hide_view () {
193 this.view.fade_out ();
194 Timeout.add (160, () => {
195 // ditto
196 this.view.opacity = 0;
197 this.view.hide ();
198 return false;
199 });
200 }
201
202 /**
203 * @name get_file
204 * @description return the Glib.File associated
205 * @return File the file associated
206 */
207 public GLib.File get_file () {
208 return this.file;
209 }
210
211 /**
212 * @name get_absolute_path
213 * @description return the absolute path to this item
214 * @return string the absolute path
215 */
216 public string get_absolute_path () {
217 return this.get_file ().get_path ();
218 }
219
220 /**
221 * @name close
222 * @description close the item manager and its view
223 */
224 public void close () {
225 this.view.close ();
226 }
227
228 /**
229 * @name trash
230 * @description trash the file or folder associated
231 */
232 public void trash () {
233 try {
234 File file = File.new_for_path (this.get_absolute_path ());
235 file.trash ();
236 } catch (Error e) {
237 stderr.printf ("Error: %s\n", e.message);
238 Util.show_error_dialog ("Error", e.message);
239 }
240 }
241
242 /**
243 * @name set_new_shape
244 * @description set a new shape (position and size) of the view
245 */
246 public void set_new_shape (int x, int y, int width, int height) {
247 this.settings.x = x;
248 this.settings.y = y;
249 this.settings.w = width;
250 this.settings.h = height;
251 this.settings.save ();
252 }
253
254 /**
255 * @name save_head_color
256 * @description save a head color to the settings file
257 * @param color string the color for the head to be saved
258 */
259 public void save_head_color (string color) {
260 this.settings.fgcolor = color;
261 this.settings.save ();
262 }
263
264 /**
265 * @name save_body_color
266 * @description save a body color to the settings file
267 * @param color string the color for the body to be saved
268 */
269 public void save_body_color (string color) {
270 this.settings.bgcolor = color;
271 this.settings.save ();
272 }
273
274 /**
275 * @name rename
276 * @description renaming myself
277 * @param string name the new name
278 * @return bool true->everything is ok, false->something failed, rollback
279 */
280 public bool rename (string new_name) {
281 string sanitized_name = DesktopFolder.Util.sanitize_name (new_name);
282 if (!DesktopFolder.Util.check_name (sanitized_name)) {
283 DesktopFolder.Util.show_invalid_name_error_dialog (this.view, new_name);
284 return false;
285 }
286 string old_name = this.note_name;
287 string old_path = this.get_absolute_path ();
288 var old_file = this.file;
289 this.note_name = sanitized_name;
290 string new_path = DesktopFolderApp.get_app_folder () + "/" + sanitized_name + "." + DesktopFolder.NEW_NOTE_EXTENSION;
291 var new_file = File.new_for_path (new_path);
292 try {
293 if (new_file.query_exists ()) {
294 DesktopFolder.Util.show_file_exists_error_dialog (this.view, sanitized_name, _("Note"), null);
295 throw new NoteManagerIOError.FILE_EXISTS ("File already exists");
296 }
297 NoteSettings note_settings = this.get_settings ();
298 note_settings.name = sanitized_name;
299
300 FileUtils.rename (old_path, new_path);
301 this.file = File.new_for_path (new_path);
302 if (this.file.query_exists ()) {
303 note_settings.save_to_file (this.file);
304 return true;
305 } else {
306 throw new NoteManagerIOError.MOVE_ERROR ("Failed to rename note");
307 }
308
309 } catch (Error error) {
310 warning (error.message);
311 // Revert changes
312 this.note_name = old_name;
313 NoteSettings note_settings = this.get_settings ();
314 note_settings.name = old_name;
315 this.file = old_file;
316 note_settings.save ();
317 return false;
318 }
319 }
320
321 }