Debian Codegraph @

src/PasswordDialog.vala

raw
1 /* PasswordDialog.vala
2 *
3 * Copyright 2024 Diego Iván <[email protected]>
4 *
5 * SPDX-License-Identifier: GPL-3.0-or-later
6 */
7
8
9 public errordomain PaperClip.PasswordDialogError {
10 CANCELLED
11 }
12
13 [GtkTemplate (ui = "/io/github/diegoivan/pdf_metadata_editor/gtk/password-dialog.ui")]
14 public class PaperClip.PasswordDialog : Adw.Dialog {
15 [GtkChild]
16 private unowned Adw.PasswordEntryRow password_entry;
17 [GtkChild]
18 private unowned Adw.StatusPage status_page;
19
20 private AsyncTask? task = null;
21
22 construct {
23 ActionEntry[] action_entries = {
24 { "success", success_task },
25 { "cancel", cancel_task }
26 };
27 var action_group = new SimpleActionGroup ();
28 action_group.add_action_entries (action_entries, this);
29 this.insert_action_group ("dialog", action_group);
30 }
31
32 public async Document decrypt (File file, Gtk.Window? parent, Cancellable? cancellable = null) throws Error {
33 status_page.description = file.get_basename () ?? "";
34 task = new AsyncTask (decrypt.callback);
35 if (cancellable != null) {
36 cancellable.cancelled.connect (cancel_task);
37 }
38 present (parent);
39 yield;
40 close ();
41
42 if (task.status == CANCELLED) {
43 throw new PasswordDialogError.CANCELLED ("Use cancelled the operation");
44 }
45
46 return yield new Document (file, password_entry.text);
47 }
48
49 [GtkCallback]
50 private void on_closed ()
51 requires (task != null)
52 requires (task.status == RUNNING) {
53 cancel_task ();
54 }
55
56 [GtkCallback]
57 private void success_task ()
58 requires (task != null)
59 requires (task.status == RUNNING) {
60 task.status = SUCCEEDED;
61 Idle.add ((owned) task.callback);
62 }
63
64 private void cancel_task ()
65 requires (task != null)
66 requires (task.status == RUNNING) {
67 task.status = CANCELLED;
68 Idle.add ((owned) task.callback);
69 }
70 }