Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,47 @@ export default class AppImageManagerExtension extends Extension {

let monitoredDirectory = this._settingsManager.getMonitoredDirectory();

await this._appImageManager.rescan(monitoredDirectory);
try {
await this._appImageManager.rescan(monitoredDirectory);
} catch (e) {
logError(`Rescan failed: ${e?.message ?? e}`);
}

this._fileMonitor.startMonitoring(
const started = this._fileMonitor.startMonitoring(
monitoredDirectory,
(filePath) => {
this._appImageManager.addAppImage(filePath);
this._appImageManager.addAppImage(filePath).catch(err => logError(err));
},
(filePath) => {
this._appImageManager.removeAppImage(filePath);
this._appImageManager.removeAppImage(filePath).catch(err => logError(err));
}
);
if (!started)
logError(`Failed to monitor directory: ${monitoredDirectory}`);
}

disable() {
log(`Disabling ${this.metadata.name} extension`);
this._fileMonitor.stopMonitoring();
this._fileMonitor?.stopMonitoring();

let monitoredDirectory = this._settingsManager.getMonitoredDirectory();
let dir = Gio.File.new_for_path(monitoredDirectory);
if (dir.query_exists(null)) {
let enumerator = dir.enumerate_children('standard::name,standard::type', Gio.FileQueryInfoFlags.NONE, null);
let fileInfo;
while ((fileInfo = enumerator.next_file(null)) !== null) {
let child = dir.get_child(fileInfo.get_name());
if (fileInfo.get_file_type() === Gio.FileType.REGULAR && this._appImageManager.isAppImage(child.get_path())) {
let fileName = GLib.path_get_basename(child.get_path());
let appImageName = fileName.replace(/\.AppImage$/, '');
this._launcherService.deleteLauncher(appImageName);
try {
let enumerator = dir.enumerate_children('standard::name,standard::type', Gio.FileQueryInfoFlags.NONE, null);
let fileInfo;
while ((fileInfo = enumerator.next_file(null)) !== null) {
let child = dir.get_child(fileInfo.get_name());
if (fileInfo.get_file_type() === Gio.FileType.REGULAR && this._appImageManager.isAppImage(child.get_path())) {
let fileName = GLib.path_get_basename(child.get_path());
let appImageName = fileName.replace(/\.AppImage$/, '');
this._launcherService.deleteLauncher(appImageName);
}
}
enumerator.close(null);
} catch (e) {
logError(`Failed to cleanup launchers: ${e?.message ?? e}`);
}
enumerator.close(null);
}

this._launcherService = null;
Expand Down
43 changes: 34 additions & 9 deletions src/appImageManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,16 +330,41 @@ export class AppImageManager {

async rescan(directory) {
const directoryFile = Gio.File.new_for_path(directory);
const enumerator = directoryFile.enumerate_children('standard::name', Gio.FileQueryInfoFlags.NONE, null);
let fileInfo;
if (!directoryFile.query_exists(null)) {
try {
directoryFile.make_directory_with_parents(null);
log(`Created monitored directory: ${directory}`);
} catch (e) {
logError(`Failed to create monitored directory ${directory}: ${e.message}`);
return;
}
}

let enumerator;
try {
enumerator = directoryFile.enumerate_children('standard::name', Gio.FileQueryInfoFlags.NONE, null);
} catch (e) {
logError(`Failed to enumerate directory ${directory}: ${e.message}`);
return;
}

const filesInDirectory = new Set();

while ((fileInfo = enumerator.next_file(null)) !== null) {
const fileName = fileInfo.get_name();
const filePath = GLib.build_pathv('/', [directory, fileName]);
filesInDirectory.add(filePath);
if (this.isAppImage(filePath)) {
await this.addAppImage(filePath);
try {
let fileInfo;
while ((fileInfo = enumerator.next_file(null)) !== null) {
const fileName = fileInfo.get_name();
const filePath = GLib.build_pathv('/', [directory, fileName]);
filesInDirectory.add(filePath);
if (this.isAppImage(filePath)) {
await this.addAppImage(filePath);
}
}
} finally {
try {
enumerator.close(null);
} catch {
// ignore
}
}

Expand All @@ -350,4 +375,4 @@ export class AppImageManager {
}
}
}
}
}
7 changes: 6 additions & 1 deletion src/fileMonitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ export class FileMonitor {
this._onFileAdded = onFileAdded;
this._onFileRemoved = onFileRemoved;

this._monitor = this._directory.monitor_directory(Gio.FileMonitorFlags.NONE, null);
try {
this._monitor = this._directory.monitor_directory(Gio.FileMonitorFlags.NONE, null);
} catch (e) {
logError(`Failed to monitor directory ${directoryPath}: ${e.message}`);
return false;
}
this._monitor.connect('changed', (monitor, file, otherFile, eventType) => {
log(`File monitor event: ${eventType}`);
switch (eventType) {
Expand Down