Debian Codegraph @

src/models/info.js

raw
1 /* exported FlatpakInfoModel getDefault */
2
3 /* info.js
4 *
5 * Copyright 2020 Martin Abente Lahaye
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 const {GObject, GLib} = imports.gi;
22
23
24 var FlatpakInfoModel = GObject.registerClass({
25 GTypeName: 'FlatpakInfoModel',
26 }, class FlatpakInfoModel extends GObject.Object {
27 _init() {
28 super._init({});
29 this._version = null;
30 }
31
32 static _getInfoPath() {
33 const path = GLib.getenv('FLATPAK_INFO_PATH');
34 if (path)
35 return path;
36
37 return GLib.build_filenamev([
38 GLib.DIR_SEPARATOR_S, '.flatpak-info',
39 ]);
40 }
41
42 _parseVersion() {
43 const keyFile = new GLib.KeyFile();
44
45 try {
46 keyFile.load_from_file(this.constructor._getInfoPath(), GLib.KeyFileFlags.NONE);
47 return keyFile.get_value('Instance', 'flatpak-version');
48 } catch (err) {
49 return null;
50 }
51 }
52
53 getVersion() {
54 if (this._version === null)
55 this._version = this._parseVersion();
56
57 return this._version;
58 }
59
60 supports(target) {
61 const version = this.getVersion();
62
63 if (version === null)
64 return true;
65
66 const versions = version.split('.');
67 const targets = target.split('.');
68 const components = Math.max(versions.length, targets.length);
69
70 for (let index = 0; index < components; index++) {
71 const _version = parseInt(versions[index] || 0, 10);
72 const _target = parseInt(targets[index] || 0, 10);
73
74 if (_version < _target)
75 return false;
76 if (_version > _target)
77 return true;
78 }
79
80 return true;
81 }
82
83 reload() {
84 this._version = this._parseVersion();
85 }
86 });
87
88
89 var getDefault = (function() {
90 let instance;
91 return function() {
92 if (typeof instance === 'undefined')
93 instance = new FlatpakInfoModel();
94 return instance;
95 };
96 }());