-
Notifications
You must be signed in to change notification settings - Fork 41
/
FolderSettings.vala
424 lines (390 loc) · 13.3 KB
/
FolderSettings.vala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
* Copyright (c) 2017 José Amuedo (https://github.com/spheras)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
/**
* @class
* Desktop Folder Settings
*/
public class DesktopFolder.FolderSettings : PositionSettings {
private string _name;
public string name {
get {
return _name;
}
set {
if (_name != value) {
_name = value; flagChanged = true;
}
}
}
private string _bgcolor;
public string bgcolor {
get {
return _bgcolor;
}
set {
if (_bgcolor != value) {
_bgcolor = value; flagChanged = true;
}
}
}
private string _fgcolor;
public string fgcolor {
get {
return _fgcolor;
}
set {
if (_fgcolor != value) {
_fgcolor = value; flagChanged = true;
}
}
}
private bool _textbold;
public bool textbold {
get {
return _textbold;
}
set {
if (_textbold != value) {
_textbold = value; flagChanged = true;
}
}
}
private bool _textshadow;
public bool textshadow {
get {
return _textshadow;
}
set {
if (_textshadow != value) {
_textshadow = value; flagChanged = true;
}
}
}
private bool _lockitems;
public bool lockitems {
get {
return _lockitems;
}
set {
if (_lockitems != value) {
_lockitems = value; flagChanged = true;
}
}
}
private bool _lockpanel;
public bool lockpanel {
get {
return _lockpanel;
}
set {
if (_lockpanel != value) {
_lockpanel = value; flagChanged = true;
}
}
}
private bool _align_to_grid;
public bool align_to_grid {
get {
return _align_to_grid;
}
set {
if (_align_to_grid != value) {
_align_to_grid = value; flagChanged = true;
}
}
}
private string[] _items = new string[0];
public string[] items {
get {
return _items;
}
set {
bool different = false;
if ((value == null && _items != null) || (_items == null && value != null) || value.length != _items.length) {
different = true;
}
if (!different && _items != null) {
for (int i = 0; i < _items.length; i++) {
if (_items[i] != value[i]) {
different = true;
break;
}
}
}
if (different) {
_items = value; flagChanged = true;
}
}
}
// util value to know the settings versions
private int _version;
public int version {
get {
return _version;
}
set {
if (_version != value) {
_version = value; flagChanged = true;
}
}
}
// default json seralization implementation only support primitive types
private File file;
/**
* @Constructor
* @param string name the name of the folder
*/
public FolderSettings (string name) {
this.reset ();
}
/**
* @name reset
* @description reset the properties
*/
public void reset () {
this.x = 100;
this.y = 100;
this.w = 300;
this.h = 300;
this.bgcolor = "df_black";
this.fgcolor = "df_light";
this.textbold = true;
this.textshadow = true;
this.align_to_grid = false;
this.lockitems = false;
this.lockpanel = false;
this.name = name;
this.items = new string[0];
this.version = DesktopFolder.SETTINGS_VERSION;
check_off_screen ();
}
/**
* @name set_item
* @description replace the current settings for a certain item with other new info
* @param ItemSettings item the new settings for the item with the same name
*/
public void set_item (ItemSettings item) {
// first, we create the list of itemsettings, and replace the old with the new one content
List <ItemSettings> all = new List <ItemSettings> ();
for (int i = 0; i < this.items.length; i++) {
ItemSettings is = ItemSettings.parse (this.items[i]);
if (is.name == item.name) {
is = item;
}
all.append (is);
}
// finally, we recreate the string[]
string[] str_result = new string[all.length ()];
for (int i = 0; i < all.length (); i++) {
ItemSettings element = all.nth_data (i);
var str = element.to_string ();
str_result[i] = str;
}
this.items = str_result;
}
/**
* @name rename
* @description rename an item on this folder.
* @param oldName string the old name of the item
* @param newName string the new name of the item
*/
public void rename (string oldName, string newName) {
// first, we create the list of itemsettings, and replace the old with the new one content
List <ItemSettings> all = new List <ItemSettings> ();
for (int i = 0; i < this.items.length; i++) {
ItemSettings is = ItemSettings.parse (this.items[i]);
if (is.name == oldName) {
is.name = newName;
}
all.append (is);
}
// finally, we recreate the string[]
string[] str_result = new string[all.length ()];
for (int i = 0; i < all.length (); i++) {
ItemSettings element = all.nth_data (i);
var str = element.to_string ();
str_result[i] = str;
}
this.items = str_result;
}
/**
* @name add_item
* @description add an item setting to the list of items of this folder settings
* @param item ItemSettings the ItemSettings to be added
*/
public void add_item (ItemSettings item) {
int length = this.items.length;
// i don't know why this can't compile
// this.items.resize(length+1);
// this.items[this.items.length-1]=item.to_string();
// alternative, copying it manually?!! :(
string[] citems = new string[length + 1];
for (int i = 0; i < length; i++) {
citems[i] = this.items[i];
}
citems[length] = item.to_string ();
this.items = citems;
}
/**
* @name get_item
* @description get an ItemSettings of an existent item inside this folder
* @param name string the name to find the item
* @return ItemSettings the ItemSettings found
*/
public ItemSettings get_item (string name) {
for (int i = 0; i < this.items.length; i++) {
ItemSettings is = ItemSettings.parse (this.items[i]);
if (is.name == name) {
return is;
}
}
return (ItemSettings) null;
}
/**
* @name save
* @description persist the changes to the filesystem. The File is the same as the saved initially.
*/
public void save () {
this.save_to_file (this.file);
}
/**
* @name save_to_file
* @description persist the changes to the filesystem. The file used is passed to the function, and saved for following saves.
* @param file File the file to be saved
*/
public void save_to_file (File file) {
if (!flagChanged) {
return;
} else {
//debug ("saving settings!!");
}
flagChanged = false;
this.file = file;
store_resolution_position ();
// string data = Json.gobject_to_data (this, null);
Json.Node root = Json.gobject_serialize (this);
// To string: (see gobject_to_data)
Json.Generator generator = new Json.Generator ();
generator.set_root (root);
string data = generator.to_data (null);
// debug ("the json generated is:\n%s\n", data);
try {
// an output file in the current working directory
if (file.query_exists ()) {
file.delete ();
}
// creating a file and a DataOutputStream to the file
/*
Use BufferedOutputStream to increase write speed:
var dos = new DataOutputStream (new BufferedOutputStream.sized (file.create (FileCreateFlags.REPLACE_DESTINATION), 65536));
*/
var dos = new DataOutputStream (file.create (FileCreateFlags.REPLACE_DESTINATION));
// writing a short string to the stream
dos.put_string (data);
} catch (Error e) {
stderr.printf ("%s\n", e.message);
}
}
/**
* @name read_settings
* @description read the settings from a file to create a Folder Settings object
* @param file File the file where the settings are persisted
* @param name string the name of the folder
* @return FolderSettings the FolderSettings created
*/
public static FolderSettings read_settings (File file, string name) {
FolderSettings result = _read_settings (file, name);
if (result == null) {
// some error occurred, lets delete the settings and create again
try {
file.trash ();
} catch (Error e) {
}
FolderSettings new_folder_settings = new FolderSettings (name);
new_folder_settings.save_to_file (file);
return _read_settings (file, name);
}
return result;
}
private static FolderSettings _read_settings (File file, string name) {
try {
string content = "";
var dis = new DataInputStream (file.read ());
string line;
// Read lines until end of file (null) is reached
while ((line = dis.read_line (null)) != null) {
content = content + line;
}
FolderSettings existent = Json.gobject_from_data (typeof (FolderSettings), content) as FolderSettings;
existent.file = file;
existent.name = name;
// regression for classes, now must have a df_ prefix
if (existent.bgcolor.length > 0 && !existent.bgcolor.has_prefix ("df_")) {
if (!existent.bgcolor.has_prefix ("rgb")) {
existent.bgcolor = "df_" + existent.bgcolor;
}
}
if (existent.fgcolor.length > 0 && !existent.fgcolor.has_prefix ("df_")) {
existent.fgcolor = "df_" + existent.fgcolor;
}
// regression for on top and back
if (existent.version == 0) {
existent.version = DesktopFolder.SETTINGS_VERSION;
}
//the properties have not changed, just loaded
existent.flagChanged = false;
//after flag changed to false because the check could modify some properties
existent.check_all ();
return existent;
} catch (Error e) {
stderr.printf ("Error: %s\n", e.message);
return (FolderSettings) null;
}
}
/**
* @name check_all
* @description check if all the items described exists fisically, and fixes problems
*/
public void check_all () {
List <ItemSettings> all = new List <ItemSettings> ();
for (int i = 0; i < this.items.length; i++) {
ItemSettings is = ItemSettings.parse (this.items[i]);
var basePath = Environment.get_home_dir () + "/Desktop/" + this.name;
var filepath = basePath + "/" + is.name;
// debug("checking:"+filepath);
File f = File.new_for_path (filepath);
if (f.query_exists ()) {
all.append (is);
} else {
debug ("Alert! doesnt exist: %s", filepath);
// doesn't exist, we must remove the entry
}
}
// finally, we recreate the string[]
string[] str_result = new string[all.length ()];
for (int i = 0; i < all.length (); i++) {
ItemSettings element = all.nth_data (i);
var str = element.to_string ();
str_result[i] = str;
}
this.items = str_result;
// and we finally resave it
this.save ();
}
}