| 1 |
/* applications.js |
| 2 |
* |
| 3 |
* Copyright 2020 Martin Abente Lahaye |
| 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 |
|
| 19 |
/* exported FlatpakApplicationsModel getDefault */ |
| 20 |
|
| 21 |
const {GObject, GLib, Gio, AppStream} = imports.gi; |
| 22 |
|
| 23 |
const {info} = imports.models; |
| 24 |
|
| 25 |
const SIGNAL_DELAY = 2500; |
| 26 |
const TARGET_EVENTS = [ |
| 27 |
Gio.FileMonitorEvent.CHANGES_DONE_HINT, |
| 28 |
Gio.FileMonitorEvent.DELETED, |
| 29 |
]; |
| 30 |
|
| 31 |
var FlatpakApplicationsModel = GObject.registerClass({ |
| 32 |
GTypeName: 'FlatpakApplicationsModel', |
| 33 |
Signals: { |
| 34 |
changed: {}, |
| 35 |
}, |
| 36 |
}, class FlatpakApplicationsModel extends GObject.Object { |
| 37 |
_init() { |
| 38 |
super._init({}); |
| 39 |
this._setup(); |
| 40 |
} |
| 41 |
|
| 42 |
_setup() { |
| 43 |
this._paths = null; |
| 44 |
this._info = info.getDefault(); |
| 45 |
this._monitors = []; |
| 46 |
this._changedDelayHandlerId = 0; |
| 47 |
|
| 48 |
this._getInstallationsPaths().forEach(path => { |
| 49 |
const file = Gio.File.new_for_path(GLib.build_filenamev([path, 'app'])); |
| 50 |
|
| 51 |
try { |
| 52 |
const monitor = file.monitor_directory(Gio.FileMonitorFlags.NONE, null); |
| 53 |
monitor.connect('changed', this._changedDelayed.bind(this)); |
| 54 |
this._monitors.push(monitor); |
| 55 |
} catch (err) { |
| 56 |
logError(err); |
| 57 |
} |
| 58 |
}); |
| 59 |
} |
| 60 |
|
| 61 |
_changedDelayed(monitor, file, other_file, event) { |
| 62 |
if (!TARGET_EVENTS.includes(event)) |
| 63 |
return; |
| 64 |
|
| 65 |
if (this._changedDelayHandlerId !== 0) |
| 66 |
GLib.Source.remove(this._changedDelayHandlerId); |
| 67 |
|
| 68 |
this._changedDelayHandlerId = GLib.timeout_add( |
| 69 |
GLib.PRIORITY_DEFAULT, SIGNAL_DELAY, this._emitChanged.bind(this)); |
| 70 |
} |
| 71 |
|
| 72 |
_emitChanged() { |
| 73 |
this.emit('changed'); |
| 74 |
this._changedDelayHandlerId = 0; |
| 75 |
return GLib.SOURCE_REMOVE; |
| 76 |
} |
| 77 |
|
| 78 |
static _getSystemPath() { |
| 79 |
const systemPath = GLib.getenv('FLATPAK_SYSTEM_DIR'); |
| 80 |
if (systemPath) |
| 81 |
return systemPath; |
| 82 |
|
| 83 |
return GLib.build_filenamev([ |
| 84 |
GLib.DIR_SEPARATOR_S, 'var', 'lib', 'flatpak', |
| 85 |
]); |
| 86 |
} |
| 87 |
|
| 88 |
_getUserPath() { |
| 89 |
const userPath = GLib.getenv('FLATPAK_USER_DIR'); |
| 90 |
if (userPath) |
| 91 |
return userPath; |
| 92 |
|
| 93 |
let userDataDir = GLib.get_user_data_dir(); |
| 94 |
if (this._info.getVersion() !== null) |
| 95 |
userDataDir = GLib.getenv('HOST_XDG_DATA_HOME'); |
| 96 |
|
| 97 |
if (!userDataDir) { |
| 98 |
userDataDir = GLib.build_filenamev([ |
| 99 |
GLib.get_home_dir(), '.local', 'share', |
| 100 |
]); |
| 101 |
} |
| 102 |
|
| 103 |
return GLib.build_filenamev([userDataDir, 'flatpak']); |
| 104 |
} |
| 105 |
|
| 106 |
_getConfigPath() { |
| 107 |
let configPath = GLib.getenv('FLATPAK_CONFIG_DIR'); |
| 108 |
if (configPath) |
| 109 |
return configPath; |
| 110 |
|
| 111 |
configPath = GLib.build_filenamev([GLib.DIR_SEPARATOR_S, 'etc', 'flatpak']); |
| 112 |
if (this._info.getVersion() !== null) { |
| 113 |
configPath = GLib.build_filenamev([ |
| 114 |
GLib.DIR_SEPARATOR_S, 'run', 'host', 'etc', 'flatpak', |
| 115 |
]); |
| 116 |
} |
| 117 |
|
| 118 |
return configPath; |
| 119 |
} |
| 120 |
|
| 121 |
static _parseCustomInstallation(path) { |
| 122 |
const installations = []; |
| 123 |
|
| 124 |
const keyFile = new GLib.KeyFile(); |
| 125 |
keyFile.load_from_file(path, GLib.KeyFileFlags.NONE); |
| 126 |
|
| 127 |
const [groups] = keyFile.get_groups(); |
| 128 |
groups.forEach(group => { |
| 129 |
const installation = {}; |
| 130 |
|
| 131 |
try { |
| 132 |
installation['path'] = keyFile.get_value(group, 'Path'); |
| 133 |
} catch (err) { |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
try { |
| 138 |
installation['priority'] = keyFile.get_value(group, 'Priority'); |
| 139 |
} catch (err) { |
| 140 |
installation['priority'] = 0; |
| 141 |
} |
| 142 |
|
| 143 |
installations.push(installation); |
| 144 |
}); |
| 145 |
|
| 146 |
return installations; |
| 147 |
} |
| 148 |
|
| 149 |
_getCustomInstallationsPaths() { |
| 150 |
let installations = []; |
| 151 |
|
| 152 |
const configPath = GLib.build_filenamev([ |
| 153 |
this._getConfigPath(), |
| 154 |
'installations.d', |
| 155 |
]); |
| 156 |
|
| 157 |
if (GLib.access(configPath, 0) !== 0) |
| 158 |
return installations; |
| 159 |
|
| 160 |
const directory = Gio.File.new_for_path(configPath); |
| 161 |
const enumerator = directory.enumerate_children('*', Gio.FileQueryInfoFlags.NONE, null); |
| 162 |
let fileInfo = enumerator.next_file(null); |
| 163 |
|
| 164 |
while (fileInfo !== null) { |
| 165 |
const file = enumerator.get_child(fileInfo); |
| 166 |
installations = [ |
| 167 |
...installations, |
| 168 |
...this.constructor._parseCustomInstallation(file.get_path()), |
| 169 |
]; |
| 170 |
fileInfo = enumerator.next_file(null); |
| 171 |
} |
| 172 |
|
| 173 |
return installations |
| 174 |
.sort((a, b) => b.priority - a.priority) |
| 175 |
.map(e => e.path); |
| 176 |
} |
| 177 |
|
| 178 |
_getInstallationsPaths() { |
| 179 |
if (this._paths !== null) |
| 180 |
return this._paths; |
| 181 |
|
| 182 |
/* Installation priority is handled by this list order */ |
| 183 |
this._paths = this._getCustomInstallationsPaths(); |
| 184 |
this._paths.unshift(this._getUserPath()); |
| 185 |
this._paths.push(this.constructor._getSystemPath()); |
| 186 |
|
| 187 |
return this._paths; |
| 188 |
} |
| 189 |
|
| 190 |
_getBundlePathForAppId(appId) { |
| 191 |
return this._getInstallationsPaths() |
| 192 |
.map(p => GLib.build_filenamev([p, 'app', appId, 'current', 'active'])) |
| 193 |
.find(p => GLib.access(p, 0) === 0); |
| 194 |
} |
| 195 |
|
| 196 |
_getIconThemePathForAppId(appId) { |
| 197 |
return GLib.build_filenamev([ |
| 198 |
this._getBundlePathForAppId(appId), 'export', 'share', 'icons', |
| 199 |
]); |
| 200 |
} |
| 201 |
|
| 202 |
/* XXX this only covers cases that follow the flathub convention */ |
| 203 |
static _isBaseApp(appId) { |
| 204 |
return appId.endsWith('.BaseApp'); |
| 205 |
} |
| 206 |
|
| 207 |
_getApplicationsForPath(path) { |
| 208 |
const list = []; |
| 209 |
|
| 210 |
if (GLib.access(path, 0) !== 0) |
| 211 |
return list; |
| 212 |
|
| 213 |
const directory = Gio.File.new_for_path(path); |
| 214 |
const enumerator = directory.enumerate_children('*', Gio.FileQueryInfoFlags.NONE, null); |
| 215 |
let fileInfo = enumerator.next_file(null); |
| 216 |
|
| 217 |
while (fileInfo !== null) { |
| 218 |
const file = enumerator.get_child(fileInfo); |
| 219 |
const appId = GLib.path_get_basename(file.get_path()); |
| 220 |
const activePath = GLib.build_filenamev([file.get_path(), 'current', 'active']); |
| 221 |
|
| 222 |
if (!this.constructor._isBaseApp(appId) && GLib.access(activePath, 0) === 0) |
| 223 |
list.push(appId); |
| 224 |
|
| 225 |
fileInfo = enumerator.next_file(null); |
| 226 |
} |
| 227 |
return list; |
| 228 |
} |
| 229 |
|
| 230 |
static _getApproximateNameForAppId(appId) { |
| 231 |
const name = appId.split('.').pop(); |
| 232 |
return name.replace(/^\w/, c => c.toUpperCase()); |
| 233 |
} |
| 234 |
|
| 235 |
getMetadataForAppId(appId) { |
| 236 |
const data = { |
| 237 |
runtime: _('Unknown'), |
| 238 |
}; |
| 239 |
|
| 240 |
const group = 'Application'; |
| 241 |
const path = this.getMetadataPathForAppId(appId); |
| 242 |
|
| 243 |
if (GLib.access(path, 0) !== 0) |
| 244 |
return data; |
| 245 |
|
| 246 |
const keyFile = new GLib.KeyFile(); |
| 247 |
keyFile.load_from_file(path, 0); |
| 248 |
|
| 249 |
try { |
| 250 |
data.runtime = keyFile.get_value(group, 'runtime'); |
| 251 |
} catch (err) { |
| 252 |
data.runtime = ''; |
| 253 |
} |
| 254 |
|
| 255 |
return data; |
| 256 |
} |
| 257 |
|
| 258 |
getDesktopForAppData(appdata) { |
| 259 |
const desktop = { |
| 260 |
icon: 'application-x-executable-symbolic', |
| 261 |
}; |
| 262 |
|
| 263 |
const path = GLib.build_filenamev([ |
| 264 |
this._getBundlePathForAppId(appdata.appId), |
| 265 |
'export', 'share', 'applications', appdata.launchable, |
| 266 |
]); |
| 267 |
|
| 268 |
const file = Gio.File.new_for_path(path); |
| 269 |
if (!file.query_exists(null)) |
| 270 |
return desktop; |
| 271 |
|
| 272 |
const metadata = new AppStream.Metadata(); |
| 273 |
try { |
| 274 |
metadata.parse_file(file, AppStream.FormatKind.DESKTOP_ENTRY); |
| 275 |
} catch (err) { |
| 276 |
return desktop; |
| 277 |
} |
| 278 |
|
| 279 |
const component = metadata.get_component(); |
| 280 |
if (component === null) |
| 281 |
return desktop; |
| 282 |
|
| 283 |
const icon = component.get_icon_stock(); |
| 284 |
if (icon === null) |
| 285 |
return desktop; |
| 286 |
|
| 287 |
const iconName = icon.get_name(); |
| 288 |
if (iconName !== null) |
| 289 |
desktop.icon = iconName; |
| 290 |
|
| 291 |
return desktop; |
| 292 |
} |
| 293 |
|
| 294 |
getAppDataForAppId(appId) { |
| 295 |
const appdata = { |
| 296 |
appId: appId, |
| 297 |
name: this.constructor._getApproximateNameForAppId(appId), |
| 298 |
author: _('Unknown'), |
| 299 |
version: _('Unknown'), |
| 300 |
date: _('Unknown'), |
| 301 |
launchable: `${appId}.desktop`, |
| 302 |
}; |
| 303 |
|
| 304 |
const bundlePath = this._getBundlePathForAppId(appId); |
| 305 |
|
| 306 |
let path = GLib.build_filenamev([ |
| 307 |
bundlePath, 'files', 'share', 'appdata', `${appId}.appdata.xml`, |
| 308 |
]); |
| 309 |
|
| 310 |
if (!GLib.file_test(path, GLib.FileTest.EXISTS)) { |
| 311 |
path = GLib.build_filenamev([ |
| 312 |
bundlePath, 'files', 'share', 'metainfo', `${appId}.metainfo.xml`, |
| 313 |
]); |
| 314 |
|
| 315 |
if (!GLib.file_test(path, GLib.FileTest.EXISTS)) |
| 316 |
return appdata; |
| 317 |
} |
| 318 |
|
| 319 |
const file = Gio.File.new_for_path(path); |
| 320 |
const metadata = new AppStream.Metadata(); |
| 321 |
try { |
| 322 |
metadata.parse_file(file, AppStream.FormatKind.XML); |
| 323 |
} catch (err) { |
| 324 |
return appdata; |
| 325 |
} |
| 326 |
|
| 327 |
const component = metadata.get_component(); |
| 328 |
if (component === null) |
| 329 |
return appdata; |
| 330 |
|
| 331 |
if (component.get_name()) |
| 332 |
appdata.name = component.get_name(); |
| 333 |
|
| 334 |
const developer = component.get_developer(); |
| 335 |
if (developer && developer.get_name()) |
| 336 |
appdata.author = developer.get_name(); |
| 337 |
|
| 338 |
const launchable = component.get_launchable(AppStream.LaunchableKind.DESKTOP_ID); |
| 339 |
if (launchable && launchable.get_entries()) |
| 340 |
[appdata.launchable] = launchable.get_entries(); |
| 341 |
|
| 342 |
const releaselist = component.get_releases_plain(); |
| 343 |
if (!releaselist) |
| 344 |
return appdata; |
| 345 |
const [release] = releaselist.get_entries(); |
| 346 |
if (!release) |
| 347 |
return appdata; |
| 348 |
|
| 349 |
if (release.get_version() !== null) |
| 350 |
appdata.version = `${release.get_version()}`; |
| 351 |
|
| 352 |
if (release.get_timestamp() !== null) { |
| 353 |
const ts = release.get_timestamp(); |
| 354 |
const date = new Date(ts * 1000); |
| 355 |
appdata.date = date.toISOString().substring(0, 10); |
| 356 |
} |
| 357 |
|
| 358 |
return appdata; |
| 359 |
} |
| 360 |
|
| 361 |
getAll() { |
| 362 |
const installations = this._getInstallationsPaths(); |
| 363 |
let applications = []; |
| 364 |
|
| 365 |
installations.forEach(path => { |
| 366 |
const app = GLib.build_filenamev([path, 'app']); |
| 367 |
applications = [...applications, ...this._getApplicationsForPath(app)]; |
| 368 |
}); |
| 369 |
|
| 370 |
const union = new Set(applications); |
| 371 |
const list = [...union]; |
| 372 |
|
| 373 |
list.sort(); |
| 374 |
|
| 375 |
return list.map(appId => { |
| 376 |
const appdata = this.getAppDataForAppId(appId); |
| 377 |
const desktop = this.getDesktopForAppData(appdata); |
| 378 |
|
| 379 |
return { |
| 380 |
appId: appId, |
| 381 |
appThemePath: this._getIconThemePathForAppId(appId), |
| 382 |
appName: appdata.name, |
| 383 |
appIconName: desktop.icon, |
| 384 |
}; |
| 385 |
}); |
| 386 |
} |
| 387 |
|
| 388 |
getMetadataPathForAppId(appId) { |
| 389 |
return GLib.build_filenamev([this._getBundlePathForAppId(appId), 'metadata']); |
| 390 |
} |
| 391 |
|
| 392 |
get userPath() { |
| 393 |
return this._getUserPath(); |
| 394 |
} |
| 395 |
|
| 396 |
shutdown() { |
| 397 |
if (this._changedDelayHandlerId !== 0) |
| 398 |
GLib.Source.remove(this._changedDelayHandlerId); |
| 399 |
this._changedDelayHandlerId = 0; |
| 400 |
|
| 401 |
this._monitors.forEach(monitor => { |
| 402 |
monitor.cancel(); |
| 403 |
}); |
| 404 |
this._monitors = []; |
| 405 |
} |
| 406 |
|
| 407 |
reload() { |
| 408 |
this.shutdown(); |
| 409 |
this._setup(); |
| 410 |
} |
| 411 |
}); |
| 412 |
|
| 413 |
|
| 414 |
var getDefault = (function() { |
| 415 |
let instance; |
| 416 |
return function() { |
| 417 |
if (typeof instance === 'undefined') |
| 418 |
instance = new FlatpakApplicationsModel(); |
| 419 |
return instance; |
| 420 |
}; |
| 421 |
}()); |