Debian Codegraph @

src/DocumentView.vala

raw
1 /* DocumentView.vala
2 *
3 * Copyright 2023 Diego Iván <[email protected]>
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 [GtkTemplate (ui = "/io/github/diegoivan/pdf_metadata_editor/gtk/document-view.ui")]
22 public class PaperClip.DocumentView : Adw.Bin {
23 [GtkChild]
24 private unowned Adw.EntryRow title_row;
25 [GtkChild]
26 private unowned Adw.EntryRow author_row;
27 [GtkChild]
28 private unowned Adw.EntryRow creator_row;
29 [GtkChild]
30 private unowned Adw.EntryRow subject_row;
31 [GtkChild]
32 private unowned Adw.EntryRow producer_row;
33 [GtkChild]
34 private unowned DateRow creation_row;
35 [GtkChild]
36 private unowned DateRow modification_row;
37 [GtkChild]
38 private unowned KeywordList keyword_list;
39 [GtkChild]
40 private unowned Gtk.Label title_label;
41 [GtkChild]
42 private unowned DetailsList details_list;
43 [GtkChild]
44 private unowned DocumentThumbnail document_thumbnail;
45 [GtkChild]
46 private unowned Gtk.ScrolledWindow scrolled_window;
47
48 private BindingGroup document_bindings = new BindingGroup ();
49 private Gtk.FileLauncher file_launcher;
50
51 private Document _document;
52 public Document document {
53 get {
54 return _document;
55 }
56 set {
57 _document = value;
58 document_bindings.source = document;
59 details_list.document = document;
60 document_thumbnail.document = document;
61 keyword_list.document = document;
62
63 document.bind_property ("title", root, "title", SYNC_CREATE);
64
65 measure_filename ();
66 title_label.label = document.original_file.get_basename ();
67
68 title_row.grab_focus_without_selecting ();
69 scrolled_window.vadjustment.value = 0;
70 }
71 }
72
73 construct {
74 file_launcher = new Gtk.FileLauncher (null) {
75 always_ask = true
76 };
77 var action_group = new SimpleActionGroup ();
78 insert_action_group ("editor", action_group);
79
80 setup_bindings ();
81 }
82
83 private void setup_bindings () {
84 // Bind Rows
85 document_bindings.bind ("title", title_row, "text",
86 SYNC_CREATE | BIDIRECTIONAL);
87 document_bindings.bind ("author", author_row, "text",
88 SYNC_CREATE | BIDIRECTIONAL);
89 document_bindings.bind ("creator", creator_row, "text",
90 SYNC_CREATE | BIDIRECTIONAL);
91 document_bindings.bind ("subject", subject_row, "text",
92 SYNC_CREATE | BIDIRECTIONAL);
93 document_bindings.bind ("producer", producer_row, "text",
94 SYNC_CREATE | BIDIRECTIONAL);
95 document_bindings.bind ("creation-date", creation_row, "date",
96 SYNC_CREATE | BIDIRECTIONAL);
97 document_bindings.bind ("modification-date", modification_row, "date",
98 SYNC_CREATE | BIDIRECTIONAL);
99 document_bindings.bind ("original-file", file_launcher, "file",
100 SYNC_CREATE);
101 }
102
103 private void measure_filename () {
104 string? basename = document.original_file.get_basename ();
105 if (basename == null) {
106 return;
107 }
108
109 title_label.remove_css_class ("long-filename");
110 title_label.ellipsize = NONE;
111
112 // A long file name that does contain spaces in the name, we will assign an special
113 // class that will reduce the size of the label
114 if (basename.length > 45 && " " in basename) {
115 title_label.add_css_class ("long-filename");
116 return;
117 }
118
119 // Now, we have to check for files with long filenames but that do not have any spaces
120 if (basename.length > 21 && !(" " in basename)) {
121 title_label.ellipsize = END;
122 }
123 }
124
125 public async void open_on_app () {
126 try {
127 bool result = yield file_launcher.launch ((Gtk.Window) root, null);
128 if (!result) {
129 debug ("File was not opened by the user");
130 }
131 }
132 catch (Error e) {
133 critical (e.message);
134 }
135 }
136 }