Debian Codegraph @

src/settings/NoteSettings.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 class DesktopFolder.NoteSettings : PositionSettings {
19 private string _name;
20 public string name {
21 get {
22 return _name;
23 }
24 set {
25 if (_name != value) {
26 _name = value; flagChanged = true;
27 }
28 }
29 }
30 private string _bgcolor;
31 public string bgcolor {
32 get {
33 return _bgcolor;
34 }
35 set {
36 if (_bgcolor != value) {
37 _bgcolor = value; flagChanged = true;
38 }
39 }
40 }
41
42 private string _fgcolor;
43 public string fgcolor {
44 get {
45 return _fgcolor;
46 }
47 set {
48 if (_fgcolor != value) {
49 _fgcolor = value; flagChanged = true;
50 }
51 }
52 }
53 private int _clipcolor;
54 public int clipcolor {
55 get {
56 return _clipcolor;
57 }
58 set {
59 if (_clipcolor != value) {
60 _clipcolor = value; flagChanged = true;
61 }
62 }
63 }
64 private string _texture;
65 public string texture {
66 get {
67 return _texture;
68 }
69 set {
70 if (_texture != value) {
71 _texture = value; flagChanged = true;
72 }
73 }
74 }
75 private string _text;
76 public string text {
77 get {
78 return _text;
79 }
80 set {
81 if (_text != value) {
82 _text = value; flagChanged = true;
83 }
84 }
85 }
86 private bool _on_top;
87 public bool on_top {
88 get {
89 return _on_top;
90 }
91 set {
92 if (_on_top != value) {
93 _on_top = value; flagChanged = true;
94 }
95 }
96 }
97 private bool _on_back;
98 public bool on_back {
99 get {
100 return _on_back;
101 }
102 set {
103 if (_on_back != value) {
104 _on_back = value; flagChanged = true;
105 }
106 }
107 }
108
109 // util value to know the settings versions
110 private int _version;
111 public int version {
112 get {
113 return _version;
114 }
115 set {
116 if (_version != value) {
117 _version = value; flagChanged = true;
118 }
119 }
120 }
121
122 private bool _edit_label_on_creation;
123 public bool edit_label_on_creation {
124 get {
125 return _edit_label_on_creation;
126 }
127 set {
128 if (_edit_label_on_creation != value) {
129 _edit_label_on_creation = value; flagChanged = true;
130 }
131 }
132 }
133
134 private File file;
135
136 public NoteSettings (string name) {
137 this.x = 110;
138 this.y = 110;
139 this.w = 300;
140 this.h = 300;
141 this.bgcolor = "df_yellow";
142 this.fgcolor = "df_dark";
143 this.texture = "";
144 this.clipcolor = Random.int_range (1, 6);
145 this.name = name;
146 this.text = "Lorem Ipsum";
147 this.on_top = false;
148 this.on_back = true;
149 this.version = DesktopFolder.SETTINGS_VERSION;
150 }
151
152 /**
153 * @name save
154 * @description persist the changes to the filesystem. The File is the same as the saved initially.
155 */
156 public void save () {
157 this.save_to_file (this.file);
158 }
159
160 /**
161 * @name save_to_file
162 * @description persist the changes to the filesystem. The file used is passed to the function, and saved for following saves.
163 * @param file File the file to be saved
164 */
165 public void save_to_file (File file) {
166 if (!flagChanged) {
167 return;
168 }
169
170 flagChanged = false;
171 this.file = file;
172
173 store_resolution_position ();
174
175 // string data = Json.gobject_to_data (this, null);
176 Json.Node root = Json.gobject_serialize (this);
177
178 // To string: (see gobject_to_data)
179 Json.Generator generator = new Json.Generator ();
180 generator.set_root (root);
181 string data = generator.to_data (null);
182
183 try {
184 // an output file in the current working directory
185 if (file.query_exists ()) {
186 file.delete ();
187 }
188
189 // creating a file and a DataOutputStream to the file
190 /*
191 Use BufferedOutputStream to increase write speed:
192 var dos = new DataOutputStream (new BufferedOutputStream.sized (file.create (FileCreateFlags.REPLACE_DESTINATION), 65536));
193 */
194 var dos = new DataOutputStream (file.create (FileCreateFlags.REPLACE_DESTINATION));
195 // writing a short string to the stream
196 dos.put_string (data);
197
198 } catch (Error e) {
199 stderr.printf ("%s\n", e.message);
200 }
201 }
202
203 /**
204 * @name read_settings
205 * @description read the settings from a file to create a Note Settings object
206 * @param file File the file where the settings are persisted
207 * @param name string the name of the note
208 * @return NoteSettings the NoteSettings created
209 */
210 public static NoteSettings read_settings (File file, string name) {
211 NoteSettings result = _read_settings (file, name);
212 if (result == null) {
213 // some error occurred, lets delete the settings and create again
214 try {
215 file.trash ();
216 } catch (Error e) {
217 }
218 NoteSettings new_settings = new NoteSettings (name);
219 new_settings.save_to_file (file);
220 return _read_settings (file, name);
221 }
222 return result;
223 }
224
225 public static NoteSettings _read_settings (File file, string name) {
226 try {
227 string content = "";
228 var dis = new DataInputStream (file.read ());
229 string line;
230 // Read lines until end of file (null) is reached
231 while ((line = dis.read_line (null)) != null) {
232 content = content + line;
233 }
234 NoteSettings existent = Json.gobject_from_data (typeof (NoteSettings), content) as NoteSettings;
235 existent.file = file;
236 existent.name = name;
237
238 // regression for classes, now must have a df_ prefix
239 if (existent.bgcolor.length > 0 && !existent.bgcolor.has_prefix ("df_")) {
240 if (!existent.bgcolor.has_prefix ("rgb")) {
241 existent.bgcolor = "df_" + existent.bgcolor;
242 existent.texture = "square_paper";
243 }
244 }
245 if (existent.fgcolor.length > 0 && !existent.fgcolor.has_prefix ("df_")) {
246 // backward compability
247 existent.fgcolor = "df_" + existent.fgcolor;
248 existent.texture = "square_paper";
249 }
250
251 // regression for on top and back
252 if (existent.version == 0) {
253 existent.version = DesktopFolder.SETTINGS_VERSION;
254 existent.on_top = false;
255 existent.on_back = true;
256 }
257
258 // the properties have not changed, just loaded
259 existent.flagChanged = false;
260
261 return existent;
262 } catch (Error e) {
263 stderr.printf ("Error: %s\n", e.message);
264 return null as NoteSettings;
265 }
266 }
267
268 }