Debian Codegraph @

src/widgets/appInfoViewer.js

raw
1 /* exported FlatsealAppInfoViewer */
2 /* eslint accessor-pairs: */
3
4 /* appInfoViewer.js
5 *
6 * Copyright 2020 Martin Abente Lahaye
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 const {GObject, GLib, Gtk} = imports.gi;
23
24 const {applications} = imports.models;
25
26 const _propFlags = GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT;
27
28 const styles = {
29 NORMAL: 'normal',
30 COMPACT: 'compact',
31 };
32
33 var FlatsealAppInfoViewer = GObject.registerClass({
34 GTypeName: 'FlatsealAppInfoViewer',
35 Template: 'resource:///com/github/tchx84/Flatseal/widgets/appInfoViewer.ui',
36 InternalChildren: ['icon', 'name', 'author', 'version', 'identifier', 'released', 'runtime'],
37 Properties: {
38 compact: GObject.ParamSpec.boolean(
39 'compact',
40 'compact',
41 'compact',
42 _propFlags, false),
43 },
44 }, class FlatsealAppInfoViewer extends Gtk.Box {
45 _init() {
46 super._init();
47 this._appId = '';
48 this._compact = false;
49 this._applications = applications.getDefault();
50 this._validator = new RegExp(/^(\d+)-(\d+)-(\d+)$/);
51 }
52
53 _getFormattedDate(string) {
54 if (!this._validator.test(string))
55 return string;
56
57 const [, year, month, day] = string.match(this._validator);
58 const date = GLib.DateTime.new(GLib.TimeZone.new_local(), year, month, day, 0, 0, 0);
59
60 /* TRANSLATORS: <full-month-name> <day-of-month>, <year-with-century> */
61 return date.format(_('%B %e, %Y'));
62 }
63
64 _setup() {
65 const appdata = this._applications.getAppDataForAppId(this._appId);
66 const desktop = this._applications.getDesktopForAppData(appdata);
67
68 this._name.set_label(appdata.name);
69 this._author.set_label(appdata.author);
70 this._version.set_label(appdata.version);
71 this._identifier.set_label(appdata.appId);
72 this._released.set_label(this._getFormattedDate(appdata.date));
73
74 this._icon.set_from_icon_name(desktop.icon);
75
76 const metadata = this._applications.getMetadataForAppId(this._appId);
77 this._runtime.set_label(metadata.runtime);
78 }
79
80 set appId(id) {
81 this._appId = id;
82 this._setup();
83 }
84
85 set compact(value) {
86 if (typeof this._icon === 'undefined')
87 return;
88 if (this._compact === value)
89 return;
90
91 const orientation = value ? Gtk.Orientation.VERTICAL : Gtk.Orientation.HORIZONTAL;
92 const alignment = value ? Gtk.Align.CENTER : Gtk.Align.START;
93
94 this.set_orientation(orientation);
95 this._icon.halign = alignment;
96 this._name.halign = alignment;
97 this._author.halign = alignment;
98 this.halign = alignment;
99
100 const style = value ? styles.COMPACT : styles.NORMAL;
101 const context = this.get_style_context();
102
103 if (context.has_class(styles.NORMAL))
104 context.remove_class(styles.NORMAL);
105 if (context.has_class(styles.COMPACT))
106 context.remove_class(styles.COMPACT);
107
108 context.add_class(style);
109
110 this._compact = value;
111 }
112 });