Debian Codegraph @

src/settings/PhotoSettings.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.PhotoSettings : PositionSettings {
19 private int _original_width = 0;
20 public int original_width {
21 get {
22 return _original_width;
23 }
24 set {
25 if (_original_width != value) {
26 _original_width = value; flagChanged = true;
27 }
28 }
29 }
30 private int _original_height = 0;
31 public int original_height {
32 get {
33 return _original_height;
34 }
35 set {
36 if (_original_height != value) {
37 _original_height = value; flagChanged = true;
38 }
39 }
40 }
41 private int _fixocolor = 0;
42 public int fixocolor {
43 get {
44 return _fixocolor;
45 }
46 set {
47 if (_fixocolor != value) {
48 _fixocolor = value; flagChanged = true;
49 }
50 }
51 }
52 private string _name;
53 public string name {
54 get {
55 return _name;
56 }
57 set {
58 if (_name != value) {
59 _name = value; flagChanged = true;
60 }
61 }
62 }
63 private string _photo_path;
64 public string photo_path {
65 get {
66 return _photo_path;
67 }
68 set {
69 if (_photo_path != value) {
70 _photo_path = value; flagChanged = true;
71 }
72 }
73 }
74
75 // util value to know the settings versions
76 private int _version;
77 public int version {
78 get {
79 return _version;
80 }
81 set {
82 if (_version != value) {
83 _version = value; flagChanged = true;
84 }
85 }
86 }
87
88 private File file;
89
90 public PhotoSettings (string photo_path, Gdk.Window window) {
91 this.x = 110;
92 this.y = 110;
93 this.photo_path = photo_path;
94 var file = File.new_for_path (photo_path);
95 this.name = file.get_basename ();
96 this.fixocolor = Random.int_range (1, 6);
97 this.version = DesktopFolder.SETTINGS_VERSION;
98
99 try {
100 // we calculate an aproximated image size
101 var pixbuf = new Gdk.Pixbuf.from_file (photo_path);
102 this.w = pixbuf.get_width ();
103 this.h = pixbuf.get_height ();
104 this.original_width = w;
105 this.original_height = h;
106
107 // max a 30% of the screen
108 Gdk.Screen screen = Gdk.Screen.get_default ();
109 int MAX = (screen.get_display ().get_monitor_at_window (window).get_geometry ().width * 30) / 100;
110
111 if (this.w > MAX) {
112 int newWidth = MAX;
113 int newHeight = (this.h * newWidth) / this.w;
114 this.w = newWidth;
115 this.h = newHeight;
116 } else if (this.h > MAX) {
117 int newHeight = MAX;
118 int newWidth = (this.w * newHeight) / this.h;
119 this.w = newWidth;
120 this.h = newHeight;
121 }
122 } catch (Error e) {
123 // error! ??
124 stderr.printf ("Error: %s\n", e.message);
125 }
126 }
127
128 /**
129 * @name save
130 * @description persist the changes to the filesystem. The File is the same as the saved initially.
131 */
132 public void save () {
133 this.save_to_file (this.file);
134 }
135
136 /**
137 * @name save_to_file
138 * @description persist the changes to the filesystem. The file used is passed to the function, and saved for following saves.
139 * @param file File the file to be saved
140 */
141 public void save_to_file (File file) {
142 if (!flagChanged) {
143 return;
144 }
145
146 flagChanged = false;
147
148 this.file = file;
149
150 store_resolution_position ();
151
152 // string data = Json.gobject_to_data (this, null);
153 Json.Node root = Json.gobject_serialize (this);
154
155 // To string: (see gobject_to_data)
156 Json.Generator generator = new Json.Generator ();
157 generator.set_root (root);
158 string data = generator.to_data (null);
159
160 try {
161 // an output file in the current working directory
162 if (file.query_exists ()) {
163 file.delete ();
164 }
165
166 // creating a file and a DataOutputStream to the file
167 /*
168 Use BufferedOutputStream to increase write speed:
169 var dos = new DataOutputStream (new BufferedOutputStream.sized (file.create (FileCreateFlags.REPLACE_DESTINATION), 65536));
170 */
171 var dos = new DataOutputStream (file.create (FileCreateFlags.REPLACE_DESTINATION));
172 // writing a short string to the stream
173 dos.put_string (data);
174
175 } catch (Error e) {
176 stderr.printf ("%s\n", e.message);
177 }
178 }
179
180 /**
181 * @name read_settings
182 * @description read the settings from a file to create a Photo Settings object
183 * @param file File the file where the settings are persisted
184 * @param name string the name of the photo
185 * @return PhotoSettings the PhotoSettings created
186 */
187 public static PhotoSettings read_settings (File file, string name) {
188 PhotoSettings result = _read_settings (file, name);
189 if (result != null) {
190 // now lets check the existence of the photo linked and if it is valid
191 var photo_file = File.new_for_path (result.photo_path);
192 if (photo_file.query_exists ()) {
193 try {
194 new Gdk.Pixbuf.from_file (result.photo_path);
195 } catch (Error error) {
196 return null as PhotoSettings;
197 }
198 } else {
199 return null as PhotoSettings;
200 }
201 }
202 return result;
203 }
204
205 public static PhotoSettings _read_settings (File file, string name) {
206 try {
207 string content = "";
208 var dis = new DataInputStream (file.read ());
209 string line;
210 // Read lines until end of file (null) is reached
211 while ((line = dis.read_line (null)) != null) {
212 content = content + line;
213 }
214 PhotoSettings existent = Json.gobject_from_data (typeof (PhotoSettings), content) as PhotoSettings;
215 existent.file = file;
216 existent.name = name;
217
218 // old versions don't calculate the original width/height
219 if (existent.original_width <= 0 || existent.original_height <= 0) {
220 // we calculate an aproximated image size
221 var pixbuf = new Gdk.Pixbuf.from_file (existent.photo_path);
222 existent.original_width = pixbuf.get_width ();
223 existent.original_height = pixbuf.get_height ();
224 }
225
226 // regression for on top and back
227 if (existent.version == 0) {
228 existent.version = DesktopFolder.SETTINGS_VERSION;
229 }
230
231 // the properties have not changed, just loaded
232 existent.flagChanged = false;
233
234 return existent;
235 } catch (Error e) {
236 stderr.printf ("Error: %s\n", e.message);
237 return null as PhotoSettings;
238 }
239 }
240
241 }