| 1 |
/* application.vala |
| 2 |
* |
| 3 |
* Copyright 2023 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 |
public class PaperClip.Application : Adw.Application { |
| 22 |
private string? debug_information = null; |
| 23 |
|
| 24 |
public Application () { |
| 25 |
Object (application_id: "io.github.diegoivan.pdf_metadata_editor", |
| 26 |
flags: ApplicationFlags.HANDLES_OPEN); |
| 27 |
|
| 28 |
Intl.setlocale (LocaleCategory.ALL, ""); |
| 29 |
Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.GNOMELOCALEDIR); |
| 30 |
Intl.textdomain (Config.GETTEXT_PACKAGE); |
| 31 |
} |
| 32 |
|
| 33 |
construct { |
| 34 |
ActionEntry[] action_entries = { |
| 35 |
{ "about", this.on_about_action }, |
| 36 |
{ "shortcuts", shortcuts_action }, |
| 37 |
{ "query-quit", on_query_quit_action }, |
| 38 |
{ "new-window", on_new_window_action } |
| 39 |
}; |
| 40 |
this.add_action_entries (action_entries, this); |
| 41 |
set_accels_for_action ("app.shortcuts", { "<Ctrl>question" }); |
| 42 |
set_accels_for_action ("app.query-quit", { "<Ctrl>q" }); |
| 43 |
set_accels_for_action ("app.new-window", { "<Ctrl>n" }); |
| 44 |
|
| 45 |
OptionEntry[] option_entries = { |
| 46 |
{ "new-window", 'w', OptionFlags.NONE, OptionArg.NONE, null, N_("Open a new window"), null }, |
| 47 |
{ "version", 'v', OptionFlags.NONE, OptionArg.NONE, null, N_("Print version information and exit"), null }, |
| 48 |
{} |
| 49 |
}; |
| 50 |
this.add_main_option_entries (option_entries); |
| 51 |
} |
| 52 |
|
| 53 |
public override int handle_local_options (GLib.VariantDict options) |
| 54 |
{ |
| 55 |
if (options.contains ("new-window")) { |
| 56 |
try { |
| 57 |
this.register (); |
| 58 |
} catch (Error e) { |
| 59 |
critical (e.message); |
| 60 |
} |
| 61 |
|
| 62 |
if (this.get_is_remote ()) { |
| 63 |
this.activate_action ("new-window", null); |
| 64 |
return 0; |
| 65 |
} |
| 66 |
} |
| 67 |
if (options.contains ("version")) { |
| 68 |
stderr.printf (@"Paper Clip $(Config.VERSION)\n"); |
| 69 |
return 0; |
| 70 |
} |
| 71 |
return -1; |
| 72 |
} |
| 73 |
|
| 74 |
public override void activate () { |
| 75 |
base.activate (); |
| 76 |
Gtk.Window? win = active_window; |
| 77 |
if (win == null) { |
| 78 |
win = new PaperClip.Window (this); |
| 79 |
} |
| 80 |
win.present (); |
| 81 |
} |
| 82 |
|
| 83 |
public override void startup () { |
| 84 |
base.startup (); |
| 85 |
Xmp.init (); |
| 86 |
} |
| 87 |
|
| 88 |
public override void shutdown () { |
| 89 |
base.shutdown (); |
| 90 |
Xmp.terminate (); |
| 91 |
} |
| 92 |
|
| 93 |
public override void open (File[] files, string hint) { |
| 94 |
if (files.length < 1) { |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
foreach (File file in files) { |
| 99 |
try { |
| 100 |
FileInfo info = file.query_info ("standard::*", |
| 101 |
NOFOLLOW_SYMLINKS); |
| 102 |
string? content_type = info.get_content_type (); |
| 103 |
|
| 104 |
if (content_type != "application/pdf") { |
| 105 |
critical (@"File $(file.get_path ()) is not a PDF"); |
| 106 |
continue; |
| 107 |
} |
| 108 |
|
| 109 |
var window = new Window (this); |
| 110 |
window.open_command_line_file (file); |
| 111 |
window.present (); |
| 112 |
} catch (Error e) { |
| 113 |
critical (e.message); |
| 114 |
} |
| 115 |
} |
| 116 |
activate (); |
| 117 |
} |
| 118 |
|
| 119 |
private void shortcuts_action () { |
| 120 |
var window = (Window) active_window; |
| 121 |
window.shortcuts (); |
| 122 |
} |
| 123 |
|
| 124 |
private void on_new_window_action () { |
| 125 |
var window = new PaperClip.Window (this); |
| 126 |
window.present (); |
| 127 |
} |
| 128 |
|
| 129 |
private void on_query_quit_action () { |
| 130 |
on_query_quit_action_async.begin (); |
| 131 |
} |
| 132 |
|
| 133 |
private async void on_query_quit_action_async () { |
| 134 |
Services.DocumentManager[] managers = {}; |
| 135 |
|
| 136 |
foreach (unowned Gtk.Window toplevel in Gtk.Window.list_toplevels ()) { |
| 137 |
var window = toplevel as Window; |
| 138 |
if (window == null || !window.document_manager.changed) { |
| 139 |
continue; |
| 140 |
} |
| 141 |
managers += window.document_manager; |
| 142 |
} |
| 143 |
|
| 144 |
if (managers.length < 1) { |
| 145 |
quit (); |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
var changes_dialog = new SaveChangesDialog () { |
| 150 |
transient_for = active_window, |
| 151 |
modal = true |
| 152 |
}; |
| 153 |
|
| 154 |
SaveChangesResponse response = yield changes_dialog.ask (managers, null); |
| 155 |
switch (response) { |
| 156 |
case SAVE: |
| 157 |
yield save_unsaved_managers (changes_dialog); |
| 158 |
break; |
| 159 |
case DISCARD: |
| 160 |
quit (); |
| 161 |
break; |
| 162 |
case CANCEL: |
| 163 |
default: |
| 164 |
return; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
private async void save_unsaved_managers (SaveChangesDialog changes_dialog) { |
| 169 |
ListModel unsaved_documents = changes_dialog.selected_documents; |
| 170 |
for (int i = 0; i < unsaved_documents.get_n_items (); i++) { |
| 171 |
var unsaved_manager = (Services.DocumentManager) unsaved_documents.get_item (i); |
| 172 |
yield unsaved_manager.save (); |
| 173 |
} |
| 174 |
quit (); |
| 175 |
} |
| 176 |
|
| 177 |
private void on_about_action () { |
| 178 |
string[] developers = { "Diego Iván M.E" }; |
| 179 |
string[] artists = { "Brage Fuglseth" }; |
| 180 |
var about = new Adw.AboutDialog () { |
| 181 |
application_icon = Config.APP_ID, |
| 182 |
application_name = "Paper Clip", |
| 183 |
copyright = "© 2023-2024 Diego Iván M.E", |
| 184 |
developer_name = "Diego Iván M.E", |
| 185 |
debug_info = generate_debug_info (), |
| 186 |
developers = developers, |
| 187 |
artists = artists, |
| 188 |
issue_url = "https://github.com/Diego-Ivan/Paper-Clip/issues", |
| 189 |
license_type = GPL_3_0, |
| 190 |
// translators: Write your name<email> here :D |
| 191 |
translator_credits = _("translator_credits"), |
| 192 |
version = Config.VERSION, |
| 193 |
website = "https://github.com/Diego-Ivan/Paper-Clip", |
| 194 |
}; |
| 195 |
|
| 196 |
about.present (this.active_window); |
| 197 |
} |
| 198 |
|
| 199 |
private string? generate_debug_info () { |
| 200 |
if (debug_information != null) { |
| 201 |
return debug_information; |
| 202 |
} |
| 203 |
|
| 204 |
bool under_flatpak = FileUtils.test ("/.flatpak-info", EXISTS); |
| 205 |
|
| 206 |
debug_information = |
| 207 |
@"Paper Clip $(Config.VERSION) |
| 208 |
Libraries: |
| 209 |
Gtk $(Gtk.get_major_version ()).$(Gtk.get_minor_version ()).$(Gtk.get_micro_version ()) (Runtime) |
| 210 |
Libadwaita: $(Adw.VERSION_S) (Compile Time) |
| 211 |
Poppler: $(Poppler.get_version ()) (Compile Time) |
| 212 |
Exempi: $(Config.EXEMPI_VERSION) |
| 213 |
GdkPixbuf $(Gdk.PIXBUF_VERSION) (Compile Time) |
| 214 |
|
| 215 |
Running under Flatpak? $under_flatpak |
| 216 |
OS Information? $(Environment.get_os_info (OsInfoKey.PRETTY_NAME))"; |
| 217 |
|
| 218 |
return debug_information; |
| 219 |
} |
| 220 |
} |