-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
136 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import path from "node:path"; | ||
import fs from "node:fs"; | ||
|
||
export const getIcon = async (desktopInfo: Record<string, string>, pathname: string, name: string) => { | ||
if (!desktopInfo.Icon) { | ||
return null | ||
} | ||
const themes = [ | ||
'hicolor', | ||
]; | ||
const sizes = ['scalable', '512x512', '256x256', '48x48', '32x32']; | ||
const types = [ | ||
'apps', | ||
]; | ||
const exts = ['.png', '.svg']; | ||
for (const theme of themes) { | ||
for (const size of sizes) { | ||
for (const type of types) { | ||
for (const ext of exts) { | ||
let iconPath = path.join('/usr/share/icons', theme, size, type, desktopInfo.Icon + ext); | ||
if (fs.existsSync(iconPath)) { | ||
return 'file://' + iconPath; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,78 @@ | ||
import {listFiles} from "../util"; | ||
import path from "path"; | ||
import {ConfigLang} from "../../../../../../config/lang"; | ||
import {getIcon} from "./icon"; | ||
import {getAppTitle} from "./title"; | ||
import fs from "node:fs"; | ||
|
||
export const ManagerAppLinux = { | ||
list: async () => { | ||
return lists() | ||
} | ||
} | ||
|
||
const appSet = new Set<string>(); | ||
|
||
const lists = async () => { | ||
appSet.clear() | ||
const files = await listFiles([ | ||
"/usr/share/applications", | ||
"/var/lib/snapd/desktop/applications", | ||
`${process.env.HOME}/.local/share/applications`, | ||
]) | ||
for (const file of files) { | ||
if (path.extname(file.pathname) !== ".desktop") { | ||
const apps = [] | ||
const locale = ConfigLang.getLocale() | ||
for (const f of files) { | ||
if (appSet.has(f.pathname)) { | ||
// console.log('appSet.has', f.pathname) | ||
continue | ||
} | ||
const extname = path.extname(f.pathname); | ||
if (extname !== '.desktop') { | ||
continue | ||
} | ||
const app = { | ||
name: f.name.replace(/\.(desktop)$/, ''), | ||
title: f.name, | ||
pathname: f.pathname, | ||
icon: null, | ||
command: null, | ||
} | ||
const desktopInfo = await parseDesktopFile(app.pathname) | ||
app.icon = await getIcon(desktopInfo, app.pathname, app.name) | ||
app.title = await getAppTitle(desktopInfo, locale, app.pathname, app.name); | ||
if (!app.icon) { | ||
continue | ||
} | ||
if (!desktopInfo.Exec) { | ||
continue | ||
} | ||
//TODO | ||
let command = desktopInfo.Exec | ||
.replace(/ %[A-Za-z]/g, "") | ||
.replace(/"/g, "") | ||
.trim(); | ||
if (desktopInfo.Terminal === 'true') { | ||
command = `gnome-terminal -x ${command}` | ||
} | ||
app.command = command | ||
appSet.add(app.pathname) | ||
apps.push(app) | ||
} | ||
return apps | ||
} | ||
|
||
const parseDesktopFile = async (pathname: string): Promise<Record<string, string>> => { | ||
const content = fs.readFileSync(pathname, 'utf-8'); | ||
const desktop = {}; | ||
for (const line of content.split('\n')) { | ||
if (line.startsWith('[')) { | ||
continue; | ||
} | ||
const [key, value] = line.split('='); | ||
if (!key || !value) { | ||
continue; | ||
} | ||
desktop[key] = value; | ||
} | ||
return [] | ||
return desktop | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const langDirMap = { | ||
'zh-CN': ['zh_CN'], | ||
} | ||
|
||
export const getAppTitle = async (desktopInfo: Record<string, string>, locale: string, pathname: string, name: string) => { | ||
if (locale in langDirMap) { | ||
for (const k of langDirMap[locale]) { | ||
const infoKey = `Name[${k}]` | ||
if (desktopInfo[infoKey]) { | ||
return desktopInfo[infoKey] | ||
} | ||
} | ||
} | ||
if (desktopInfo.Name) { | ||
return desktopInfo.Name | ||
} | ||
return name | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters