Debian Codegraph @

src/settings/FolderSettings.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 interface FolderSettingsInfoProvider {
19
20 /**
21 * @name get_parent_default_arrangement_orientation_setting
22 * @description provide the default orientation setting to store
23 * @return {int} the default orientation setting. See ARRANGEMENT_ORIENTATION_VERTICAL and ARRANGEMENT_ORIENTATION_HORIZONTAL
24 */
25 public abstract int get_parent_default_arrangement_orientation_setting ();
26
27 }
28
29 /**
30 * @class
31 * Desktop Folder Settings
32 */
33 public class DesktopFolder.FolderSettings : PositionSettings {
34 public static int ARRANGEMENT_ORIENTATION_VERTICAL = 1;
35 public static int ARRANGEMENT_ORIENTATION_HORIZONTAL = 2;
36
37 private string _name;
38 public string name {
39 get {
40 return _name;
41 }
42 set {
43 if (_name != value) {
44 _name = value; flagChanged = true;
45 }
46 }
47 }
48 private string _bgcolor;
49 public string bgcolor {
50 get {
51 return _bgcolor;
52 }
53 set {
54 if (_bgcolor != value) {
55 _bgcolor = value; flagChanged = true;
56 }
57 }
58 }
59 private string _fgcolor;
60 public string fgcolor {
61 get {
62 return _fgcolor;
63 }
64 set {
65 if (_fgcolor != value) {
66 _fgcolor = value; flagChanged = true;
67 }
68 }
69 }
70 private bool _textbold;
71 public bool textbold {
72 get {
73 return _textbold;
74 }
75 set {
76 if (_textbold != value) {
77 _textbold = value; flagChanged = true;
78 }
79 }
80 }
81 private bool _textshadow;
82 public bool textshadow {
83 get {
84 return _textshadow;
85 }
86 set {
87 if (_textshadow != value) {
88 _textshadow = value; flagChanged = true;
89 }
90 }
91 }
92 private bool _lockitems;
93 public bool lockitems {
94 get {
95 return _lockitems;
96 }
97 set {
98 if (_lockitems != value) {
99 _lockitems = value; flagChanged = true;
100 }
101 }
102 }
103 private bool _lockpanel;
104 public bool lockpanel {
105 get {
106 return _lockpanel;
107 }
108 set {
109 if (_lockpanel != value) {
110 _lockpanel = value; flagChanged = true;
111 }
112 }
113 }
114
115 // @deprecated since version 4
116 private bool _align_to_grid;
117 public bool align_to_grid {
118 get {
119 return _align_to_grid;
120 }
121 set {
122 if (_align_to_grid != value) {
123 _align_to_grid = value; flagChanged = true;
124 }
125 }
126 }
127
128 private int _arrangement_type;
129 public int arrangement_type {
130 get {
131 return _arrangement_type;
132 }
133 set {
134 if (_arrangement_type != value) {
135 _arrangement_type = value; flagChanged = true;
136 }
137 }
138 }
139
140 private int _arrangement_padding;
141 public int arrangement_padding {
142 get {
143 return _arrangement_padding;
144 }
145 set {
146 if (_arrangement_padding != value) {
147 _arrangement_padding = value; flagChanged = true;
148 }
149 }
150 }
151
152 private int _sort_by_type;
153 public int sort_by_type {
154 get {
155 return _sort_by_type;
156 }
157 set {
158 if (_sort_by_type != value) {
159 _sort_by_type = value; flagChanged = true;
160 }
161 }
162 }
163
164 private bool _sort_reverse;
165 public bool sort_reverse {
166 get {
167 return _sort_reverse;
168 }
169 set {
170 if (_sort_reverse != value) {
171 _sort_reverse = value; flagChanged = true;
172 }
173 }
174 }
175
176 private int _arrangement_orientation;
177 public int arrangement_orientation {
178 get {
179 return _arrangement_orientation;
180 }
181 set {
182 if (_arrangement_orientation != value) {
183 _arrangement_orientation = value; flagChanged = true;
184 }
185 }
186 }
187
188 private string[] _items = new string[0];
189 public string[] items {
190 get {
191 return _items;
192 }
193
194 set {
195 bool different = false;
196
197 if ((value == null && _items != null) || (_items == null && value != null) || value.length != _items.length) {
198 different = true;
199 }
200 if (!different && _items != null) {
201 for (int i = 0; i < _items.length; i++) {
202 if (_items[i] != value[i]) {
203 different = true;
204 break;
205 }
206 }
207 }
208
209 if (different) {
210 _items = value; flagChanged = true;
211 }
212 }
213 }
214
215 // util value to know the settings versions
216 private int _version;
217 public int version {
218 get {
219 return _version;
220 }
221 set {
222 if (_version != value) {
223 _version = value; flagChanged = true;
224 }
225 }
226 }
227
228 private bool _recently_created;
229 public bool recently_created {
230 get {
231 return _recently_created;
232 }
233 set {
234 if (_recently_created != value) {
235 _recently_created = value; flagChanged = true;
236 }
237 }
238 }
239 // default json seralization implementation only support primitive types
240
241 private File file;
242
243 /**
244 * @Constructor
245 * @param string name the name of the folder
246 */
247 public FolderSettings (string name) {
248 this.reset ();
249 }
250
251 /**
252 * @name reset
253 * @description reset the properties
254 */
255 public void reset () {
256 this.x = 100;
257 this.y = 100;
258 this.w = 300;
259 this.h = 300;
260 this.bgcolor = "df_black";
261 this.fgcolor = "df_light";
262 this.textbold = true;
263 this.textshadow = true;
264 this.align_to_grid = false;
265 this.lockitems = false;
266 this.lockpanel = false;
267 this.arrangement_padding = FolderArrangement.DEFAULT_PADDING;
268 this.arrangement_type = FolderArrangement.ARRANGEMENT_TYPE_FREE;
269 this.name = name;
270 this.items = new string[0];
271 this.version = DesktopFolder.SETTINGS_VERSION;
272 this.recently_created = false;
273 check_off_screen ();
274 }
275
276 /**
277 * @name set_item
278 * @description replace the current settings for a certain item with other new info
279 * @param ItemSettings item the new settings for the item with the same name
280 */
281 public void set_item (ItemSettings item) {
282 // first, we create the list of itemsettings, and replace the old with the new one content
283 List <ItemSettings> all = new List <ItemSettings> ();
284 for (int i = 0; i < this.items.length; i++) {
285 ItemSettings is = ItemSettings.parse (this.items[i]);
286 if (is.name == item.name) {
287 is = item;
288 }
289 all.append (is);
290 }
291
292 // finally, we recreate the string[]
293 this.serialize_list (all);
294 }
295
296 /**
297 * @name serialize
298 * @description serialize a list of itemsettings to the string result
299 * @param {List<ItemSettings} all the list of item settings to serialize in the settings
300 */
301 public void serialize_list (List <ItemSettings> all) {
302 string[] str_result = new string[all.length ()];
303 for (int i = 0; i < all.length (); i++) {
304 ItemSettings element = all.nth_data (i);
305 var str = element.to_string ();
306 str_result[i] = str;
307 }
308 this.items = str_result;
309 }
310
311 /**
312 * @name serialize
313 * @description serialize an array of itemsettings to the string result
314 * @param {ItemSettings[]} all the array of item settings to serialize in the settings
315 */
316 public void serialize_array (ItemSettings[] all) {
317 string[] str_result = new string[all.length];
318 for (int i = 0; i < all.length; i++) {
319 ItemSettings element = all[i];
320 var str = element.to_string ();
321 str_result[i] = str;
322 }
323 this.items = str_result;
324 }
325
326 /**
327 * @name rename
328 * @description rename an item on this folder.
329 * @param oldName string the old name of the item
330 * @param newName string the new name of the item
331 */
332 public void rename (string oldName, string newName) {
333 // first, we create the list of itemsettings, and replace the old with the new one content
334 List <ItemSettings> all = new List <ItemSettings> ();
335 for (int i = 0; i < this.items.length; i++) {
336 ItemSettings is = ItemSettings.parse (this.items[i]);
337 if (is.name == oldName) {
338 is.name = newName;
339 }
340 all.append (is);
341 }
342
343 // finally, we recreate the string[]
344 string[] str_result = new string[all.length ()];
345 for (int i = 0; i < all.length (); i++) {
346 ItemSettings element = all.nth_data (i);
347 var str = element.to_string ();
348 str_result[i] = str;
349 }
350 this.items = str_result;
351 }
352
353 /**
354 * @name add_item
355 * @description add an item setting to the list of items of this folder settings
356 * @param item ItemSettings the ItemSettings to be added
357 */
358 public void add_item (ItemSettings item) {
359 this._items += item.to_string ();
360 this.flagChanged = true;
361
362 /*
363 int length = this.items.length;
364 this._items.resize (length + 1);
365 this._items[this._items.length - 1] = item.to_string ();
366 this.flagChanged=true;
367 */
368
369
370 /*
371 // alternative, copying it manually?!! :(
372 int length = this.items.length;
373 string[] citems = new string[length + 1];
374 for (int i = 0; i < length; i++) {
375 citems[i] = this.items[i];
376 }
377 citems[length] = item.to_string ();
378 this.items = citems;
379 */
380 }
381
382 /**
383 * @name get_item
384 * @description get an ItemSettings of an existent item inside this folder
385 * @param name string the name to find the item
386 * @return ItemSettings the ItemSettings found
387 */
388 public ItemSettings get_item (string name) {
389 for (int i = 0; i < this.items.length; i++) {
390 ItemSettings is = ItemSettings.parse (this.items[i]);
391 if (is.name == name) {
392 return is;
393 }
394 }
395 return (ItemSettings) null;
396 }
397
398 /**
399 * @name get_items_parsed
400 * @description return the list of item settings managed by this folder settings
401 * @return {List<ItemSettings>} the list of managed item settings
402 */
403 public Gee.HashMap <string, ItemSettings> get_items_parsed () {
404 // debug("0---get_items_parsed");
405 Gee.HashMap <string, ItemSettings> result = new Gee.HashMap <string, ItemSettings>();
406 for (int i = 0; i < this.items.length; i++) {
407 // debug(">>>>>>>>>>> %s",this.items[i]);
408 ItemSettings is = ItemSettings.parse (this.items[i]);
409 result.set (is.name, is);
410 }
411 return result;
412 }
413
414 /**
415 * @name save
416 * @description persist the changes to the filesystem. The File is the same as the saved initially.
417 */
418 public void save () {
419 this.save_to_file (this.file);
420 }
421
422 /**
423 * @name save_to_file
424 * @description persist the changes to the filesystem. The file used is passed to the function, and saved for following saves.
425 * @param file File the file to be saved
426 */
427 public void save_to_file (File file) {
428 if (!flagChanged) {
429 return;
430 } else {
431 // debug ("saving settings!!");
432 }
433
434 flagChanged = false;
435 this.file = file;
436
437 store_resolution_position ();
438
439 // string data = Json.gobject_to_data (this, null);
440 Json.Node root = Json.gobject_serialize (this);
441
442 // To string: (see gobject_to_data)
443 Json.Generator generator = new Json.Generator ();
444 generator.set_root (root);
445 string data = generator.to_data (null);
446
447 // debug ("the json generated is:\n%s\n", data);
448 try {
449 // an output file in the current working directory
450 if (file.query_exists ()) {
451 file.delete ();
452 }
453
454 // creating a file and a DataOutputStream to the file
455 /*
456 Use BufferedOutputStream to increase write speed:
457 var dos = new DataOutputStream (new BufferedOutputStream.sized (file.create (FileCreateFlags.REPLACE_DESTINATION), 65536));
458 */
459 var dos = new DataOutputStream (file.create (FileCreateFlags.REPLACE_DESTINATION));
460 // writing a short string to the stream
461 dos.put_string (data);
462
463 } catch (Error e) {
464 stderr.printf ("%s\n", e.message);
465 }
466 }
467
468 /**
469 * @name read_settings
470 * @description read the settings from a file to create a Folder Settings object
471 * @param file File the file where the settings are persisted
472 * @param name string the name of the folder
473 * @return FolderSettings the FolderSettings created
474 */
475 public static FolderSettings read_settings (File file, string name, FolderSettingsInfoProvider provider) {
476 FolderSettings result = _read_settings (file, name, provider);
477 if (result == null) {
478 // some error occurred, lets delete the settings and create again
479 try {
480 file.trash ();
481 } catch (Error e) {
482 }
483 FolderSettings new_folder_settings = new FolderSettings (name);
484 new_folder_settings.save_to_file (file);
485 return _read_settings (file, name, provider);
486 }
487 return result;
488 }
489
490 private static FolderSettings _read_settings (File file, string name, FolderSettingsInfoProvider provider) {
491 try {
492 string content = "";
493 var dis = new DataInputStream (file.read ());
494 string line;
495 // Read lines until end of file (null) is reached
496 while ((line = dis.read_line (null)) != null) {
497 content = content + line;
498 }
499 FolderSettings existent = Json.gobject_from_data (typeof (FolderSettings), content) as FolderSettings;
500 existent.file = file;
501 existent.name = name;
502
503 // regression for classes, now must have a df_ prefix
504 if (existent.bgcolor.length > 0 && !existent.bgcolor.has_prefix ("df_")) {
505 if (!existent.bgcolor.has_prefix ("rgb")) {
506 existent.bgcolor = "df_" + existent.bgcolor;
507 }
508 }
509 if (existent.fgcolor.length > 0 && !existent.fgcolor.has_prefix ("df_")) {
510 existent.fgcolor = "df_" + existent.fgcolor;
511 }
512
513 // align_to_grid is deprecated from version 4
514 if (existent.align_to_grid == true) {
515 existent.arrangement_type = FolderArrangement.ARRANGEMENT_TYPE_GRID;
516 existent.align_to_grid = false;
517 } else if (existent.arrangement_type == 0) {
518 existent.arrangement_type = FolderArrangement.ARRANGEMENT_TYPE_FREE;
519 }
520
521 // regression for on top and back
522 if (existent.version == 0) {
523 existent.version = DesktopFolder.SETTINGS_VERSION;
524 }
525
526 if (existent.arrangement_orientation == 0) {
527 existent.arrangement_orientation = provider.get_parent_default_arrangement_orientation_setting ();
528 }
529
530 // the properties have not changed, just loaded
531 existent.flagChanged = false;
532
533 // after flag changed to false because the check could modify some properties
534 existent.check_all ();
535
536 return existent;
537 } catch (Error e) {
538 stderr.printf ("Error: %s\n", e.message);
539 return (FolderSettings) null;
540 }
541 }
542
543 /**
544 * @name check_all
545 * @description check if all the items described exists fisically, and fixes problems
546 */
547 public void check_all () {
548 List <ItemSettings> all = new List <ItemSettings> ();
549 for (int i = 0; i < this.items.length; i++) {
550 ItemSettings is = ItemSettings.parse (this.items[i]);
551 var basePath = Environment.get_home_dir () + "/Desktop/" + this.name;
552 var filepath = basePath + "/" + is.name;
553 // debug("checking:"+filepath);
554 File f = File.new_for_path (filepath);
555 if (f.query_exists ()) {
556 all.append (is);
557 } else {
558 debug ("Alert! does not exist: %s", filepath);
559 // doesn't exist, we must remove the entry
560 }
561 }
562
563 // finally, we recreate the string[]
564 string[] str_result = new string[all.length ()];
565 for (int i = 0; i < all.length (); i++) {
566 ItemSettings element = all.nth_data (i);
567 var str = element.to_string ();
568 str_result[i] = str;
569 }
570 this.items = str_result;
571
572 // and we finally resave it
573 this.save ();
574 }
575
576 }