Debian Codegraph @

src/logic/PhotoManager.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 /**
19 * @class
20 * Item Manager that represents an Icon of a File or Folder
21 */
22 public class DesktopFolder.PhotoManager : Object {
23 /** parent application */
24 private DesktopFolderApp application;
25 /** the File object associated with the item */
26 private File file;
27 /** the view associated with this manager */
28 private PhotoWindow view;
29 /** name of the photo */
30 private string photo_name = null;
31 /** photo Settings of this photo */
32 private PhotoSettings settings = null;
33 /** flag to get the validity of the photo */
34 private bool flag_valid = true;
35
36 /**
37 * @constructor
38 * @param DesktopFolderApp application the application of this photo
39 * @param string photo_name the name of the photo
40 * @param File file the GLib File object for the file associated with this item
41 */
42 public PhotoManager (DesktopFolderApp application, string photo_name, File file) {
43 this.photo_name = photo_name;
44 this.file = file;
45
46 // Let's load the settings of the folder (if exist or a new one)
47 if (!this.load_photo_settings ()) {
48 // removing the settings file
49 if (this.file.query_exists ()) {
50 try {
51 this.file.trash ();
52 } catch (Error e) {
53 }
54 }
55 this.flag_valid = false;
56 } else {
57 // First we create a photo Window above the desktop
58 this.application = application;
59 this.view = new PhotoWindow (this);
60 this.application.add_window (this.view);
61 this.view.show ();
62
63 // trying to put it in front of the rest
64 this.view.set_keep_below (false);
65 this.view.set_keep_above (true);
66 this.view.present ();
67 this.view.set_keep_above (false);
68 this.view.set_keep_below (true);
69 // ---------------------------------------
70 }
71 }
72
73 /**
74 * @name on_screen_size_changed
75 * @description detecting screen size changes
76 */
77 public void on_screen_size_changed (Gdk.Screen screen) {
78 this.settings.calculate_current_position ();
79 this.view.reload_settings ();
80 }
81
82 /**
83 * @name is_valid
84 * @description return the validity of the photo widget
85 * @return {bool} true->yes, it is valid
86 */
87 public bool is_valid () {
88 return this.flag_valid;
89 }
90
91 /**
92 * @name load_photo_settings
93 * @description load the settings of this photo.
94 * The photo/settings file contains all the info needed to create the photo position, size, etc.. and the text itself
95 * @return {bool} true->everything was ok, false->something was bad
96 */
97 private bool load_photo_settings () {
98 // let's search the folder settings file
99 var abs_path = this.get_absolute_path ();
100 debug ("loading photo settings...%s", abs_path);
101 if (!this.file.query_exists ()) {
102 warning ("photo file does not exist!");
103 return false;
104 } else {
105 PhotoSettings existent = PhotoSettings.read_settings (this.file, this.get_photo_name ());
106 if (existent == null) {
107 // something bad occurred, we must delete this photo widget
108 return false;
109 } else {
110 this.settings = existent;
111 }
112 }
113 return true;
114 }
115
116 /**
117 * @name get_settings
118 * @description return the settings of this photo
119 * @return PhotoSettings the settings of this photo
120 */
121 public PhotoSettings get_settings () {
122 return this.settings;
123 }
124
125 /**
126 * @name get_photo_name
127 * @description return the photo name
128 * @return string the photo name
129 */
130 public string get_photo_name () {
131 return this.photo_name;
132 }
133
134 /**
135 * @name get_application
136 * @description return the desktop folder application
137 * @return DesktopFolderApp
138 */
139 public DesktopFolderApp get_application () {
140 return this.application;
141 }
142
143 /**
144 * @name get_view
145 * @description return the view of this manager
146 * @return PhotoWindow
147 */
148 public PhotoWindow get_view () {
149 return this.view;
150 }
151
152 /**
153 * @name show_view
154 * @description show the folder
155 */
156 public void show_view () {
157 // setting opacity to stop the folder window flashing at startup
158 this.view.opacity = 1;
159 this.view.show_all ();
160 this.view.fade_in ();
161 }
162
163 /**
164 * @name hide_view
165 * @description hide the folder
166 */
167 public void hide_view () {
168 this.view.fade_out ();
169 Timeout.add (160, () => {
170 // ditto
171 this.view.opacity = 0;
172 this.view.hide ();
173 return false;
174 });
175 }
176
177 /**
178 * @name get_file
179 * @description return the Glib.File associated
180 * @return File the file associated
181 */
182 public GLib.File get_file () {
183 return this.file;
184 }
185
186 /**
187 * @name get_absolute_path
188 * @description return the absolute path to this item
189 * @return string the absolute path
190 */
191 public string get_absolute_path () {
192 return this.get_file ().get_path ();
193 }
194
195 /**
196 * @name close
197 * @description close the item manager and its view
198 */
199 public void close () {
200 this.view.close ();
201 }
202
203 /**
204 * @name reopen
205 * @description close the current view and reopen it again
206 */
207 public void reopen () {
208 this.view.save_current_position_and_size ();
209 this.get_settings ().save ();
210
211 // closing
212 this.application.remove_window (this.view);
213 this.view.close ();
214 // reopening
215 this.view = new PhotoWindow (this);
216 this.application.add_window (this.view);
217 this.view.show ();
218 }
219
220 /**
221 * @name open
222 * @description open the photo in the default viewer
223 */
224 public void open () {
225 try {
226 var command = "xdg-open \"" + get_settings ().photo_path + "\"";
227 var appinfo = AppInfo.create_from_commandline (command, null, AppInfoCreateFlags.SUPPORTS_URIS);
228 appinfo.launch_uris (null, null);
229 } catch (Error e) {
230 stderr.printf ("Error: %s\n", e.message);
231 Util.show_error_dialog ("Error", e.message);
232 }
233 }
234
235 /**
236 * @name delete
237 * @description delete the file associated
238 */
239 public void delete () {
240 try {
241 File file = File.new_for_path (this.get_absolute_path ());
242 file.delete ();
243 } catch (Error e) {
244 stderr.printf ("Error: %s\n", e.message);
245 Util.show_error_dialog ("Error", e.message);
246 }
247 }
248
249 /**
250 * @name set_new_shape
251 * @description set a new shape (position and size) of the view
252 */
253 public void set_new_shape (int x, int y, int width, int height) {
254 this.settings.x = x;
255 this.settings.y = y;
256 this.settings.w = width;
257 this.settings.h = height;
258 this.settings.save ();
259 }
260
261 /**
262 * @name rename
263 * @description renaming myself
264 * @param string name the new name
265 * @return bool true->everything is ok, false->something failed, rollback
266 */
267 public bool rename (string new_name) {
268 if (new_name.length <= 0) {
269 return false;
270 }
271 string old_name = this.photo_name;
272 string old_path = this.get_absolute_path ();
273 this.photo_name = new_name;
274 string new_path = DesktopFolderApp.get_app_folder () + "/" + new_name + "." + DesktopFolder.NEW_PHOTO_EXTENSION;
275
276 try {
277 PhotoSettings is = this.get_settings ();
278 is.name = new_name;
279
280 FileUtils.rename (old_path, new_path);
281 this.file = File.new_for_path (new_path);
282 is.save_to_file (this.file);
283
284 return true;
285 } catch (Error e) {
286 // we can't rename, undoing
287 this.photo_name = old_name;
288 PhotoSettings is = this.get_settings ();
289 is.name = old_name;
290 is.save ();
291
292 // showing the error
293 stderr.printf ("Error: %s\n", e.message);
294 Util.show_error_dialog ("Error", e.message);
295
296 return false;
297 }
298 }
299
300 }