-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapplicationMenu.js
285 lines (237 loc) · 7.96 KB
/
applicationMenu.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import { ensureDir } from "./utils.js";
/**
* Retrieves the application menu, potentially from a cache file.
* If the cache is not available, it will generate the menu, save it to the cache, and return it.
* @returns {Object} The application menu as an object.
*/
export function getAppMenu() {
const cachedApplicationMenuDirPath = HOME_DIR + "/.cache/jiffy/";
const cachedApplicationMenuFilePath = cachedApplicationMenuDirPath +
"appsMenu.json";
if (!USER_ARGUMENTS.refresh) {
const cacheFile = STD.loadFile(cachedApplicationMenuFilePath);
if (cacheFile) {
return JSON.parse(cacheFile);
}
}
const appMenu = prepareAppsMenu();
const error = {};
let fd = STD.open(cachedApplicationMenuFilePath, "w+", error);
if (!fd) {
if (error.errno === 2) ensureDir(cachedApplicationMenuDirPath);
fd = STD.open(cachedApplicationMenuFilePath, "w+", error);
if (!fd) {
throw Error(
`Failed to open file "${cachedApplicationMenuFilePath}".\nError code: ${error.errno}`,
);
}
}
const appMenuCache = { Apps: appMenu };
fd.puts(JSON.stringify(appMenuCache));
fd.close();
return appMenuCache;
}
/**
* Prepares application menu by parsing desktop entry files
* @returns {Array} Array of application objects with metadata
*/
function prepareAppsMenu() {
const DESKTOP_DIRS = [
"/usr/share/applications",
`${HOME_DIR}/.local/share/applications`,
];
// Collect and deduplicate desktop files
const desktopFilePaths = collectDesktopFiles(DESKTOP_DIRS);
// Parse desktop files and create application entries
return parseDesktopFiles(desktopFilePaths);
}
/**
* Collects desktop files from specified directories, removing duplicates
* @param {Array<string>} directories - Directories to search for desktop files
* @returns {Array<string>} Deduplicated list of desktop file paths
*/
function collectDesktopFiles(directories) {
const fileNames = new Set();
const filePaths = [];
for (const dir of directories) {
const [files, err] = OS.readdir(dir);
if (err !== 0) continue;
for (const file of files) {
if (!file.endsWith(".desktop") || fileNames.has(file)) continue;
fileNames.add(file);
filePaths.push(`${dir}/${file}`);
}
}
return filePaths;
}
/**
* Parses desktop files and extracts application metadata
* @param {Array<string>} filePaths - Paths to desktop files
* @returns {Array<Object>} Array of application objects
*/
function parseDesktopFiles(filePaths) {
const appMenu = [];
for (const filePath of filePaths) {
const appEntry = parseDesktopFile(filePath);
if (appEntry) {
appMenu.push(appEntry);
}
}
return appMenu;
}
/**
* Parses a single desktop file
* @param {string} filePath - Path to desktop file
* @returns {Object|null} Application metadata object or null if invalid
*/
function parseDesktopFile(filePath) {
const fd = STD.open(filePath, "r");
if (!fd) return null;
try {
const appData = extractDesktopEntryData(fd);
fd.close();
// Validate and return application data
if (Object.keys(appData).length > 0 && !appData.noDisplay && appData.exec) {
delete appData.noDisplay; // Clean up internal property
return { ...appData, path: filePath };
}
return null;
} catch (_) {
print(`Failed to parse: ${filePath}.`);
fd.close();
return null;
}
}
/**
* Extracts data from desktop entry section
* @param {Object} fileDescriptor - Open file descriptor
* @returns {Object} Application metadata
*/
function extractDesktopEntryData(fileDescriptor) {
const appData = { noDisplay: false };
let inDesktopEntry = false;
const FIELD_PARSERS = {
"Name": (value) => appData.name = `• ${value}`,
"Comment": (value) => appData.description = value,
"Exec": (value) => appData.exec = parseExecField(value),
"Icon": (value) => appData.icon = findIconPath(value),
"Terminal": (value) => appData.terminal = value.toLowerCase() === "true",
"NoDisplay": (value) => appData.noDisplay = value.toLowerCase() === "true",
"Categories": (value) => appData.category = parseCategoriesField(value),
"Keywords": (value) => appData.keywords = parseKeywordsField(value),
};
const REQUIRED_FIELDS = 7; // All fields except noDisplay
while (true) {
const line = fileDescriptor.getline();
if (line === null) break;
// Handle section markers
if (line.startsWith("[Desktop Entry]")) {
inDesktopEntry = true;
continue;
}
if (line.startsWith("[") && inDesktopEntry) break; // New section after Desktop Entry
if (!inDesktopEntry) continue;
// Parse field
const separatorIndex = line.indexOf("=");
if (separatorIndex === -1) continue;
const fieldName = line.substring(0, separatorIndex);
const fieldValue = line.substring(separatorIndex + 1);
if (FIELD_PARSERS[fieldName]) {
FIELD_PARSERS[fieldName](fieldValue);
}
// Break early if we have all required fields
if (Object.keys(appData).length >= REQUIRED_FIELDS + 1) break; // +1 for noDisplay
}
return appData;
}
/**
* Parses the Exec field to extract the executable name
* @param {string} execValue - Value of Exec field
* @returns {string} Executable name
*/
function parseExecField(execValue) {
return execValue.split("/").pop().split(" ")[0];
}
/**
* Parses Categories field into formatted string
* @param {string} categoriesValue - Value of Categories field
* @returns {string} Formatted categories string
*/
function parseCategoriesField(categoriesValue) {
return categoriesValue?.split(";")
.filter(Boolean)
.join(" │ ")
.trim() || "";
}
/**
* Parses Keywords field into formatted string
* @param {string} keywordsValue - Value of Keywords field
* @returns {string} Formatted keywords string
*/
function parseKeywordsField(keywordsValue) {
return keywordsValue?.split(";")
.filter((entry) => isAsciiPrintable(entry))
.join(", ")
.trim() || "";
}
/**
* Checks if a string contains only printable ASCII characters
* @param {string} str - String to check
* @returns {boolean} True if string contains only printable ASCII
*/
function isAsciiPrintable(str) {
if (!str) return false;
return str.charCodeAt(0) >= 32 && str.charCodeAt(0) <= 126;
}
function findIconPath(iconName) {
// Icon theme search paths
const ICON_PATHS = [
HOME_DIR + "/.local/share/icons",
"/usr/share/icons",
"/usr/share/pixmaps",
];
// Common icon sizes to search for
const ICON_SIZES = ["48x48", "32x32", "24x24", "16x16", "scalable"];
const ICON_CATEGORIES = ["apps", "applications"];
const ICON_EXTENSIONS = [".svg", ".png", ".xpm"];
// If iconName is an absolute path, verify it exists and return it
if (iconName.startsWith("/")) {
const [_stat, err] = OS.stat(iconName);
if (err === 0) return iconName;
// Continue with theme-based search if absolute path fails
}
// Remove file extension if present
iconName = iconName.replace(/\.[^/.]+$/, "");
// Search in all icon directories
for (const basePath of ICON_PATHS) {
// First check if icon exists directly in pixmaps
if (basePath === "/usr/share/pixmaps") {
for (const ext of ICON_EXTENSIONS) {
const directPath = `${basePath}/${iconName}${ext}`;
const [_stat, err] = OS.stat(directPath);
if (err === 0) return directPath;
}
continue;
}
// Get list of theme directories
let themes;
const [dirs, err] = OS.readdir(basePath);
if (err === 0) themes = dirs;
else continue;
// Search through themes (including hicolor)
for (const theme of ["hicolor", ...themes]) {
for (const size of ICON_SIZES) {
for (const category of ICON_CATEGORIES) {
for (const ext of ICON_EXTENSIONS) {
const iconPath =
`${basePath}/${theme}/${size}/${category}/${iconName}${ext}`;
const [_stat, err] = OS.stat(iconPath);
if (err === 0) return iconPath;
}
}
}
}
}
// Return original icon name if no path found
return iconName;
}