| 1 |
/* Application.vala |
| 2 |
* |
| 3 |
* Copyright (C) 2009-2025 Jerry Casiano |
| 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. |
| 17 |
* |
| 18 |
* If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>. |
| 19 |
*/ |
| 20 |
|
| 21 |
namespace FontManager { |
| 22 |
|
| 23 |
namespace FontViewer { |
| 24 |
|
| 25 |
[DBus (name = "com.github.FontManager.FontViewer")] |
| 26 |
public class Application : Gtk.Application { |
| 27 |
|
| 28 |
[DBus (visible = false)] |
| 29 |
public MainWindow? main_window { get; private set; default = null; } |
| 30 |
[DBus (visible = false)] |
| 31 |
public GLib.Settings? settings { get; private set; default = null; } |
| 32 |
|
| 33 |
uint dbus_id = 0; |
| 34 |
|
| 35 |
const OptionEntry[] options = { |
| 36 |
{ "version", 'v', 0, OptionArg.NONE, null, "Show application version", null }, |
| 37 |
{ "debug", 0, 0, OptionArg.NONE, null, "Enable debug messages", null }, |
| 38 |
{ "", 0, 0, OptionArg.FILENAME_ARRAY, null, null, null }, |
| 39 |
{ null } |
| 40 |
}; |
| 41 |
|
| 42 |
public Application (string app_id, ApplicationFlags app_flags) { |
| 43 |
Object(application_id : app_id, flags : app_flags); |
| 44 |
add_main_option_entries(options); |
| 45 |
} |
| 46 |
|
| 47 |
public bool ready () |
| 48 |
throws DBusError, IOError { |
| 49 |
return main_window != null && main_window.is_visible(); |
| 50 |
} |
| 51 |
|
| 52 |
public void show_uri (string uri) |
| 53 |
throws DBusError, IOError { |
| 54 |
if (main_window == null || !main_window.is_visible()) |
| 55 |
activate(); |
| 56 |
main_window.show_uri(uri); |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
public override void open (File [] files, string hint) { |
| 61 |
if (main_window == null || !main_window.is_visible()) |
| 62 |
activate(); |
| 63 |
main_window.open(files[0]); |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
protected override void activate () { |
| 68 |
register_session = true; |
| 69 |
if (main_window == null) { |
| 70 |
settings = get_gsettings(BUS_ID); |
| 71 |
main_window = new MainWindow(settings); |
| 72 |
add_window(main_window); |
| 73 |
((Gtk.Widget) main_window).realize.connect_after(() => { main_window.restore_state(); }); |
| 74 |
// Why is this needed? |
| 75 |
shutdown.connect(() => { quit(); }); |
| 76 |
} |
| 77 |
main_window.present(); |
| 78 |
main_window.update(); |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
public override int command_line (ApplicationCommandLine cl) { |
| 83 |
hold(); |
| 84 |
VariantDict options = cl.get_options_dict(); |
| 85 |
if (options.contains("debug")) { |
| 86 |
Environment.set_variable("G_MESSAGES_DEBUG", "[font-manager]", true); |
| 87 |
stdout.printf("\n%s %s\n\n", _("Font Viewer"), Config.PACKAGE_VERSION); |
| 88 |
print_os_info(); |
| 89 |
print_library_versions(); |
| 90 |
} |
| 91 |
StringSet? filelist = get_command_line_files(cl); |
| 92 |
if (filelist != null) { |
| 93 |
File [] files = { File.new_for_path(filelist[0]) }; |
| 94 |
open(files, "null"); |
| 95 |
} else { |
| 96 |
activate(); |
| 97 |
} |
| 98 |
release(); |
| 99 |
return 0; |
| 100 |
} |
| 101 |
|
| 102 |
public override int handle_local_options (VariantDict options) { |
| 103 |
|
| 104 |
int exit_status = -1; |
| 105 |
|
| 106 |
if (options.contains("version")) { |
| 107 |
stdout.printf("%s %s\n", _("Font Viewer"), Config.PACKAGE_VERSION); |
| 108 |
return 0; |
| 109 |
} |
| 110 |
|
| 111 |
return exit_status; |
| 112 |
} |
| 113 |
|
| 114 |
public override bool dbus_register (DBusConnection conn, string path) |
| 115 |
throws Error { |
| 116 |
base.dbus_register(conn, path); |
| 117 |
dbus_id = conn.register_object(BUS_PATH, this); |
| 118 |
if (dbus_id == 0) |
| 119 |
critical("Could not register Font Viewer service "); |
| 120 |
return true; |
| 121 |
} |
| 122 |
|
| 123 |
public override void dbus_unregister (DBusConnection conn, string path) { |
| 124 |
if (dbus_id != 0) |
| 125 |
conn.unregister_object(dbus_id); |
| 126 |
base.dbus_unregister(conn, path); |
| 127 |
} |
| 128 |
|
| 129 |
public static int main (string [] args) { |
| 130 |
setup_i18n(); |
| 131 |
Environment.set_application_name(_("Font Viewer")); |
| 132 |
#if HAVE_ADWAITA |
| 133 |
var settings = get_gsettings(BUS_ID); |
| 134 |
if (settings != null) |
| 135 |
if (settings.get_boolean("use-adwaita-stylesheet")) |
| 136 |
Adw.init(); |
| 137 |
#endif |
| 138 |
ApplicationFlags FLAGS = (ApplicationFlags.HANDLES_COMMAND_LINE | |
| 139 |
ApplicationFlags.HANDLES_OPEN); |
| 140 |
return new Application(BUS_ID, FLAGS).run(args); |
| 141 |
} |
| 142 |
|
| 143 |
} |
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
} |
| 148 |
|