Skip to content

Commit

Permalink
Improved refactoring (#1237)
Browse files Browse the repository at this point in the history
  • Loading branch information
ke1v authored Oct 25, 2024
1 parent 9a83dba commit b9fa5cd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
17 changes: 7 additions & 10 deletions src/main/Core/ImageGenerator/Linux/LinuxAppIconExtractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ export class LinuxAppIconExtractor implements FileIconExtractor {
try {
this.searchCache = new Map();
this.userTheme = await this.getIconThemeName();

if (!this.userTheme) {
throw new Error("Could not determine user's icon theme.");
}

let validThemeDirectories = await this.getExistingThemeDirectories(this.userTheme, this.baseDirectories);

for (let i = 0; i < validThemeDirectories.length; i++) {
Expand Down Expand Up @@ -368,11 +373,7 @@ export class LinuxAppIconExtractor implements FileIconExtractor {
for (const subdir of themeIndex.subdirectories) {
for (const extension of ["png", "svg", "xpm"]) {
const iconThemeSubDir = themeIndex.subdirData.get(subdir);
if (
themeIndex.subdirData.has(subdir) &&
iconThemeSubDir &&
this.directoryMatchesSize(iconThemeSubDir, size, scale)
) {
if (iconThemeSubDir && this.directoryMatchesSize(iconThemeSubDir, size, scale)) {
const filename = `${join(themeIndex.path, subdir, iconName)}.${extension}`;

if (this.fileSystemUtility.existsSync(filename)) {
Expand All @@ -388,17 +389,13 @@ export class LinuxAppIconExtractor implements FileIconExtractor {
for (const themeIndex of this.searchCache.get(theme) ?? []) {
for (const subdir of themeIndex.subdirectories) {
for (const extension of ["png", "svg", "xpm"]) {
if (!themeIndex.subdirData.has(subdir)) {
continue;
}

const filename = `${join(themeIndex.path, subdir, iconName)}.${extension}`;
const iconThemeSubDir = themeIndex.subdirData.get(subdir);

if (!iconThemeSubDir) {
continue;
}

const filename = `${join(themeIndex.path, subdir, iconName)}.${extension}`;
const dist = this.directorySizeDistance(iconThemeSubDir, size, scale);

if (this.fileSystemUtility.existsSync(filename) && dist < minimalSize) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ export class LinuxApplicationRepository implements ApplicationRepository {
.filter((folderPath) => this.fileSystemUtility.isDirectory(folderPath));

const filePaths = await this.getApplicationFilePaths(folderPaths);
const appIcons = await this.fileImageGenerator.getImages(filePaths);

return await this.generateLinuxApplications(filePaths, appIcons);
return await this.generateLinuxApplications(filePaths);
}

private async getApplicationFilePaths(folderPaths: string[]): Promise<string[]> {
Expand All @@ -52,14 +51,9 @@ export class LinuxApplicationRepository implements ApplicationRepository {
return result;
}

private async generateLinuxApplications(
filePaths: string[],
appIcons: Record<string, Image>,
): Promise<LinuxApplication[]> {
private async generateLinuxApplications(filePaths: string[]): Promise<LinuxApplication[]> {
const applicationPromiseResults = await Promise.allSettled(
filePaths.map((filePath) =>
this.generateLinuxApplication(filePath, appIcons[filePath] ?? this.getDefaultAppIcon()),
),
filePaths.map((filePath) => this.generateLinuxApplication(filePath)),
);

const applications: LinuxApplication[] = [];
Expand All @@ -69,7 +63,9 @@ export class LinuxApplicationRepository implements ApplicationRepository {
const promiseReuslt = applicationPromiseResults[i];

if (promiseReuslt.status === "fulfilled") {
applications.push(promiseReuslt.value);
if (promiseReuslt.value) {
applications.push(promiseReuslt.value);
}
} else {
this.logger.warn(`Unable to generate Application for ${filePath}. Reason: ${promiseReuslt.reason}`);
continue;
Expand All @@ -79,28 +75,37 @@ export class LinuxApplicationRepository implements ApplicationRepository {
return applications;
}

private async generateLinuxApplication(filePath: string, appIcon: Image): Promise<LinuxApplication> {
private async generateLinuxApplication(filePath: string): Promise<LinuxApplication | undefined> {
const config: Record<string, string> = this.iniParser.parseIniFileContent(
(await this.fileSystemUtility.readFile(filePath)).toString(),
)["Desktop Entry"];
// https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#recognized-keys
const desktopEnv = this.environmentVariableProvider.get("ORIGINAL_XDG_CURRENT_DESKTOP")?.split(":");

if (!desktopEnv) {
throw new Error("Unable to resolve desktop environment");
if (!config) {
throw new Error(`Unable to parse .desktop at ${filePath}`);
}

const appName = config["Name"] ?? filePath;

// https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#recognized-keys
const desktopEnv = this.environmentVariableProvider.get("ORIGINAL_XDG_CURRENT_DESKTOP")?.split(":") ?? [];

if (
!config ||
!config["Icon"] ||
config.NoDisplay ||
(config.OnlyShowIn !== undefined && !config.OnlyShowIn.split(";").some((i) => desktopEnv.includes(i))) ||
(config.NotShowIn !== undefined && config.NotShowIn.split(";").some((i) => desktopEnv.includes(i)))
(config.OnlyShowIn && !config.OnlyShowIn.split(";").some((i) => desktopEnv.includes(i))) ||
(config.NotShowIn && config.NotShowIn.split(";").some((i) => desktopEnv.includes(i)))
) {
throw new Error("Unexpected .desktop file");
return undefined;
}

let appIcon = this.getDefaultAppIcon();

try {
appIcon = await this.fileImageGenerator.getImage(filePath);
} catch (error) {
this.logger.error(`Using fallback icon for ${appName}. Reason: ${error}`);
}

return new LinuxApplication(config["Name"], filePath, appIcon);
return new LinuxApplication(appName, filePath, appIcon);
}

private getDefaultAppIcon(): Image {
Expand Down

0 comments on commit b9fa5cd

Please sign in to comment.