Debian Codegraph @

src/window.vala

raw
1 /* window.vala
2 *
3 * Copyright 2023-2024 Diego Iván
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: GPL-3.0-or-later
19 */
20
21 namespace PaperClip {
22 [GtkTemplate (ui = "/io/github/diegoivan/pdf_metadata_editor/window.ui")]
23 public class Window : Adw.ApplicationWindow {
24 [GtkChild]
25 private unowned DocumentView doc_view;
26 [GtkChild]
27 private unowned Gtk.Stack view_stack;
28 [GtkChild]
29 private unowned Gtk.MenuButton menu_button;
30 [GtkChild]
31 private unowned Adw.ToastOverlay toast_overlay;
32 [GtkChild]
33 private unowned DropOverlay drop_overlay;
34 [GtkChild]
35 private unowned Gtk.ProgressBar progress_bar;
36
37 public WindowState state { get; set; default = NONE; }
38
39 public Services.DocumentManager document_manager {
40 get;
41 default = new Services.DocumentManager ();
42 }
43
44 private File? dropped_file { get; set; default = null; }
45
46 public Window (Gtk.Application app) {
47 Object (application: app);
48
49 action_set_enabled ("win.save", false);
50 action_set_enabled ("win.save-as", false);
51 action_set_enabled ("win.open-with", false);
52 }
53
54 static construct {
55 typeof (DropOverlay).ensure ();
56 }
57
58 construct {
59 ActionEntry[] entries = {
60 { "open", on_open_action },
61 { "open-with", on_open_with_action },
62 { "save", save_file_action },
63 { "save-as", save_file_as_action },
64 { "main-menu", on_main_menu_action }
65 };
66
67 var action_group = new SimpleActionGroup ();
68 action_group.add_action_entries (entries, this);
69 insert_action_group ("win", action_group);
70
71 // Controller of Drag and Drop
72 var drop_target = new Gtk.DropTarget (typeof(File), COPY);
73
74 drop_target.drop.connect (on_file_dropped);
75 view_stack.add_controller (drop_target);
76 drop_overlay.drop_target = drop_target;
77 }
78
79 private bool on_file_dropped (Value value) {
80 var file = (File) value;
81 try {
82 FileInfo info = file.query_info ("standard::*", NOFOLLOW_SYMLINKS);
83 string? content_type = info.get_content_type ();
84
85 if (content_type != "application/pdf") {
86 critical ("File is not a PDF");
87 return false;
88 }
89
90 dropped_file = file;
91 state = OPENING_DROPPED;
92 open_dropped_file.begin ();
93 }
94 catch (Error e) {
95 critical (e.message);
96 return false;
97 }
98 return true;
99 }
100
101 private async void open_dropped_file () {
102 if (document_manager.changed) {
103 show_unsaved_warning.begin ();
104 }
105 else {
106 load_dropped_file.begin ();
107 }
108 }
109
110 private async void load_dropped_file () {
111 pulse_progress_bar ();
112 yield load_document_to_view (dropped_file);
113
114 hide_progress_bar_animation ();
115 dropped_file = null;
116 state = NONE;
117 }
118
119 public void open_command_line_file (File file)
120 requires (file.query_exists ())
121 {
122 state = OPENING_FILE;
123 dropped_file = file;
124 open_dropped_file.begin ();
125 }
126
127 private void on_open_action () {
128 state = OPENING_FILE;
129 if (document_manager.changed) {
130 show_unsaved_warning.begin ();
131 } else {
132 open_file.begin ();
133 }
134 }
135
136 private async void open_file () {
137 var file_dialog = new Gtk.FileDialog ();
138
139 var pdf_filter = new Gtk.FileFilter () {
140 name = "PDF"
141 };
142 pdf_filter.add_mime_type ("application/pdf");
143
144 var filters = new ListStore (typeof (Gtk.FileFilter));
145 filters.append (pdf_filter);
146
147 file_dialog.filters = filters;
148
149 try {
150 File opened_file = yield file_dialog.open (this, null);
151 pulse_progress_bar ();
152 yield load_document_to_view (opened_file);
153 }
154 catch (Error e) {
155 critical (e.message);
156 }
157 state = NONE;
158 }
159
160 private void on_open_with_action () {
161 doc_view.open_on_app.begin ();
162 }
163
164 private async void load_document_to_view (File file) {
165 try {
166 var doc = yield create_document (file);
167 document_manager.document = doc_view.document = doc;
168 action_set_enabled ("win.save", true);
169 action_set_enabled ("win.save-as", true);
170 action_set_enabled ("win.open-with", true);
171
172 view_stack.visible_child_name = "editor";
173 } catch (Error e) {
174 if (e is Poppler.Error.ENCRYPTED) {
175 var toast = new Adw.Toast ( _("Failed to open the document. The provided password is incorrect.") );
176 toast_overlay.add_toast (toast);
177 }
178 critical (e.message);
179 }
180 hide_progress_bar_animation ();
181 }
182
183 private async Document create_document (File file) throws Error {
184 Document? doc = null;
185 try {
186 doc = yield new Document (file);
187 } catch (Error e) {
188 if (!(e is Poppler.Error.ENCRYPTED)) {
189 throw e;
190 }
191 }
192
193 if (doc != null) {
194 return doc;
195 }
196
197 var dialog = new PasswordDialog ();
198 return yield dialog.decrypt (file, this, null);
199 }
200
201 private void save_file_action () {
202 save_file.begin ();
203 }
204
205 private async void save_file () {
206 if (document_manager.document == null || !document_manager.changed) {
207 return;
208 }
209
210 file_save_animation ();
211 yield document_manager.save (document_manager.document.original_file.get_uri ());
212 proceed_with_state ();
213 state = NONE;
214 }
215
216 private void save_file_as_action () {
217 save_file_as.begin ();
218 }
219
220 private async void save_file_as () {
221 if (document_manager.document == null) {
222 return;
223 }
224
225 var file_dialog = new Gtk.FileDialog () {
226 initial_name = document_manager.document.original_file.get_basename ()
227 };
228
229 var pdf_filter = new Gtk.FileFilter () {
230 name = "PDF"
231 };
232 pdf_filter.add_mime_type ("application/pdf");
233
234 var filters = new ListStore (typeof (Gtk.FileFilter));
235 filters.append (pdf_filter);
236
237 file_dialog.filters = filters;
238
239 try {
240 File file = yield file_dialog.save (this, null);
241 file_save_animation ();
242 yield document_manager.save (file.get_uri ());
243 proceed_with_state ();
244 }
245 catch (Error e) {
246 critical (e.message);
247 }
248 state = NONE;
249 }
250
251 private void file_save_animation () {
252 var property_target = new Adw.PropertyAnimationTarget (progress_bar, "fraction");
253
254 var animation = new Adw.TimedAnimation (progress_bar, 0, 1, 200, property_target) {
255 easing = EASE_IN_OUT_SINE
256 };
257 animation.done.connect (hide_progress_bar_animation);
258
259 progress_bar.fraction = 0;
260 progress_bar.opacity = 1;
261 animation.play ();
262 }
263
264 public void shortcuts () {
265 var builder = new Gtk.Builder.from_resource ("/io/github/diegoivan/pdf_metadata_editor/gtk/shortcut-window.ui");
266 var shortcuts_window = builder.get_object ("shortcut_window") as Gtk.ShortcutsWindow;
267 if (shortcuts_window == null) {
268 critical ("Failed to load shortcuts");
269 return;
270 }
271
272 shortcuts_window.transient_for = this;
273 shortcuts_window.show ();
274 }
275
276 private void on_main_menu_action () {
277 menu_button.popup ();
278 }
279
280 private void hide_progress_bar_animation () {
281 var property_target = new Adw.PropertyAnimationTarget (progress_bar, "opacity");
282 var animation = new Adw.TimedAnimation (progress_bar, 1, 0, 200, property_target) {
283 easing = EASE_IN_OUT_SINE
284 };
285
286 animation.play ();
287 }
288
289 private void pulse_progress_bar () {
290 progress_bar.opacity = 1;
291 progress_bar.pulse ();
292 }
293
294 [GtkCallback]
295 private bool on_close_request () {
296 if (!document_manager.changed || state == CLOSING) {
297 state = NONE;
298 return false;
299 }
300
301 quit_and_save.begin ();
302 return true;
303 }
304
305 private async void quit_and_save () {
306 state = CLOSING;
307 yield show_unsaved_warning ();
308 }
309
310 private async void show_unsaved_warning () {
311 var save_changes_dialog = new SaveChangesDialog () {
312 transient_for = this,
313 modal = true
314 };
315 SaveChangesResponse response = yield save_changes_dialog.ask ({document_manager}, null);
316
317 if (response == CANCEL) {
318 state = NONE;
319 return;
320 }
321
322 if (response == SAVE) {
323 yield save_file ();
324 return;
325 }
326
327 proceed_with_state ();
328 }
329
330 private void proceed_with_state () {
331 switch (state) {
332 case NONE:
333 return;
334 case OPENING_FILE:
335 open_file.begin ();
336 break;
337 case OPENING_DROPPED:
338 load_dropped_file.begin ();
339 break;
340 case CLOSING:
341 this.close ();
342 break;
343 }
344 }
345 }
346 }
347
348 public enum PaperClip.WindowState {
349 NONE,
350 OPENING_FILE,
351 CLOSING,
352 OPENING_DROPPED
353 }