2015-08-28 14:20:07 +02:00
|
|
|
var path = require('path');
|
2015-08-30 21:29:19 +02:00
|
|
|
var System = require('./system');
|
2015-09-04 12:10:08 +02:00
|
|
|
var debug = require('./debug')('desktop-apps');
|
|
|
|
var Cache = require('./cache');
|
2015-08-28 14:20:07 +02:00
|
|
|
|
|
|
|
// Constants
|
|
|
|
var ICON_REALPATH_REGEX = /\..+$/;
|
2015-08-30 21:29:19 +02:00
|
|
|
var ICON_THEMES_ROOTDIR = '/usr/share/icons';
|
2015-08-31 12:24:55 +02:00
|
|
|
var PIXMAPS_ICONS_ROOTDIR = '/usr/share/pixmaps';
|
2015-08-28 14:20:07 +02:00
|
|
|
|
2015-08-30 21:29:19 +02:00
|
|
|
/**
|
|
|
|
* Find and load all the desktop files in the subdirectories of given dirs
|
|
|
|
*
|
|
|
|
* @param Array[String] rootDirs
|
|
|
|
* @return Promise
|
|
|
|
*/
|
2015-08-28 14:20:07 +02:00
|
|
|
exports.loadAllDesktopFiles = function(rootDirs) {
|
|
|
|
|
|
|
|
return exports.findAllDesktopFiles(rootDirs)
|
|
|
|
.then(function(filePaths) {
|
|
|
|
|
|
|
|
var promises = filePaths.map(function(path) {
|
|
|
|
return exports.loadDesktopFile(path);
|
|
|
|
});
|
|
|
|
|
|
|
|
return Promise.all(promises)
|
|
|
|
.then(function(contents) {
|
|
|
|
return contents.map(function(content, i) {
|
|
|
|
return { content: content, path: filePaths[i] };
|
|
|
|
});
|
|
|
|
})
|
|
|
|
;
|
|
|
|
|
|
|
|
})
|
|
|
|
;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2015-08-30 21:29:19 +02:00
|
|
|
/**
|
|
|
|
* Find all the desktop files in the subdirectories of given dirs
|
|
|
|
*
|
|
|
|
* @param Array[String] baseDirs
|
|
|
|
* @return Promise
|
|
|
|
*/
|
|
|
|
exports.findAllDesktopFiles = function(baseDirs) {
|
2015-08-28 14:20:07 +02:00
|
|
|
|
2015-08-30 21:29:19 +02:00
|
|
|
if(!Array.isArray(baseDirs)) {
|
|
|
|
baseDirs = [baseDirs];
|
2015-08-28 14:20:07 +02:00
|
|
|
}
|
|
|
|
|
2015-08-30 21:29:19 +02:00
|
|
|
var promises = baseDirs.map(function(baseDir) {
|
|
|
|
return System.findFiles('**/*.desktop', {cwd: baseDir, realpath: true});
|
2015-08-28 14:20:07 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return Promise.all(promises)
|
|
|
|
.then(function(apps) {
|
|
|
|
return uniq(flatten(apps));
|
|
|
|
})
|
|
|
|
;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2015-08-30 21:29:19 +02:00
|
|
|
/**
|
|
|
|
* Load a .desktop file ans return its parsed content
|
|
|
|
*
|
|
|
|
* @param string filePath
|
|
|
|
* @return Promise
|
|
|
|
*/
|
2015-08-28 14:20:07 +02:00
|
|
|
exports.loadDesktopFile = function(filePath) {
|
2015-08-30 21:29:19 +02:00
|
|
|
return System.loadINIFile(filePath);
|
|
|
|
};
|
|
|
|
|
2015-09-04 12:10:08 +02:00
|
|
|
var iconCache = new Cache();
|
|
|
|
|
2015-08-30 21:29:19 +02:00
|
|
|
/**
|
|
|
|
* Find the absolute path of a desktop icon
|
|
|
|
*
|
|
|
|
* @param string iconPath
|
|
|
|
* @return Promise
|
|
|
|
*/
|
|
|
|
exports.findIcon = function(iconName, themeName, size, themeIgnore) {
|
|
|
|
|
2015-09-04 12:10:08 +02:00
|
|
|
var cachedIcon = iconCache.get([iconName, themeName, size]);
|
|
|
|
|
|
|
|
if(cachedIcon) {
|
|
|
|
debug('Icon %s:%s:%s found in cache !', iconName, themeName, size);
|
|
|
|
return Promise.resolve(cachedIcon);
|
|
|
|
}
|
|
|
|
|
2015-08-30 21:29:19 +02:00
|
|
|
themeIgnore = themeIgnore || [];
|
|
|
|
if(themeIgnore.indexOf(themeIgnore) !== -1) {
|
2015-08-31 23:08:56 +02:00
|
|
|
debug('Theme %s already processed, ignoring...', themeName);
|
2015-08-30 21:29:19 +02:00
|
|
|
return Promise.resolve(null);
|
|
|
|
}
|
|
|
|
themeIgnore.push(themeName);
|
|
|
|
|
|
|
|
debug('Finding icon %s:%s:%s...', iconName, themeName, size);
|
|
|
|
|
|
|
|
if( ICON_REALPATH_REGEX.test(iconName) ) {
|
|
|
|
return Promise.resolve(iconName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!themeName) {
|
|
|
|
return exports.findIconThemes()
|
|
|
|
.then(function(themes) {
|
|
|
|
themeIgnore = themeIgnore || [];
|
|
|
|
var promises = themes.map(function(theme) {
|
|
|
|
return exports.findIcon(iconName, theme, size, themeIgnore);
|
|
|
|
});
|
|
|
|
return Promise.all(promises)
|
|
|
|
.then(exports._selectBestIcon)
|
|
|
|
;
|
|
|
|
})
|
2015-09-04 12:10:08 +02:00
|
|
|
.then(_cacheIcon)
|
2015-08-30 21:29:19 +02:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
return exports.findClosestSizeIcon(iconName, themeName, size)
|
|
|
|
.then(function(foundIcon) {
|
|
|
|
|
|
|
|
if(foundIcon) return foundIcon;
|
|
|
|
|
|
|
|
debug('No icon found. Search in parents...');
|
|
|
|
|
2015-08-31 12:24:55 +02:00
|
|
|
return exports.findParentsThemeIcon(iconName, themeName, size, themeIgnore)
|
|
|
|
.then(function(iconPath) {
|
|
|
|
|
|
|
|
if(iconPath) return iconPath;
|
|
|
|
|
|
|
|
return exports.findPixmapsIcon(iconName);
|
|
|
|
|
|
|
|
})
|
|
|
|
;
|
2015-08-30 21:29:19 +02:00
|
|
|
|
|
|
|
})
|
2015-09-04 12:10:08 +02:00
|
|
|
.then(_cacheIcon)
|
2015-08-30 21:29:19 +02:00
|
|
|
;
|
|
|
|
|
2015-09-04 12:10:08 +02:00
|
|
|
|
|
|
|
function _cacheIcon(iconPath) {
|
|
|
|
iconCache.set([iconName, themeName, size], iconPath);
|
|
|
|
return iconPath;
|
|
|
|
}
|
|
|
|
|
2015-08-30 21:29:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.findParentsThemeIcon = function(iconName, themeName, size, themeIgnore) {
|
|
|
|
|
|
|
|
return exports.themeIndexExists(themeName)
|
|
|
|
.then(function(exists) {
|
|
|
|
|
|
|
|
if(!exists) return null;
|
|
|
|
|
|
|
|
return exports.loadThemeIndex(themeName)
|
|
|
|
.then(function(themeIndex) {
|
|
|
|
|
|
|
|
if(!themeIndex || !themeIndex['Icon Theme'].Inherits) return;
|
|
|
|
|
|
|
|
var parents = themeIndex['Icon Theme'].Inherits.split(',');
|
|
|
|
|
|
|
|
debug('Found parents %j', parents);
|
|
|
|
|
|
|
|
var promises = parents.map(function(themeName) {
|
|
|
|
return exports.findIcon(iconName, themeName, size, themeIgnore);
|
|
|
|
});
|
|
|
|
|
|
|
|
return Promise.all(promises)
|
|
|
|
.then(exports._selectBestIcon)
|
|
|
|
;
|
|
|
|
|
|
|
|
})
|
|
|
|
;
|
|
|
|
|
|
|
|
})
|
|
|
|
;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.findClosestSizeIcon = function(iconName, themeName, size) {
|
|
|
|
|
|
|
|
var themePath = path.join(ICON_THEMES_ROOTDIR, themeName);
|
|
|
|
|
|
|
|
var extPattern = '{svg,png}';
|
|
|
|
var filePattern = themeName+'/*/*/'+iconName+'.'+extPattern;
|
|
|
|
|
|
|
|
debug('File pattern %s', filePattern);
|
|
|
|
|
|
|
|
return System.findFiles(filePattern, {cwd: ICON_THEMES_ROOTDIR})
|
|
|
|
.then(function(iconFiles) {
|
|
|
|
|
|
|
|
debug('Found files %j', iconFiles);
|
|
|
|
|
|
|
|
var scalableIcon = iconFiles.reduce(function(scalableIcon, iconPath) {
|
|
|
|
if(iconPath.indexOf('scalable') !== -1) {
|
|
|
|
debug('Found scalable icon %s', iconPath);
|
|
|
|
scalableIcon = iconPath;
|
|
|
|
}
|
|
|
|
return scalableIcon;
|
|
|
|
}, null);
|
|
|
|
|
|
|
|
if(scalableIcon) return scalableIcon;
|
|
|
|
|
|
|
|
if(!size) {
|
|
|
|
size = Math.max.apply(Math, clean(iconFiles.map(sizeFromPath)));
|
2015-08-28 14:20:07 +02:00
|
|
|
}
|
2015-08-30 21:29:19 +02:00
|
|
|
|
|
|
|
var closestIcon = iconFiles.reduce(function(foundIcon, iconPath) {
|
|
|
|
var foundSize = sizeFromPath(iconPath);
|
|
|
|
if( foundSize && Math.abs(foundSize - size) < Math.abs(foundIcon.size - size) ) {
|
|
|
|
foundIcon.path = iconPath;
|
|
|
|
foundIcon.size = foundSize;
|
|
|
|
}
|
|
|
|
return foundIcon;
|
|
|
|
}, {path: null, size: null});
|
|
|
|
|
|
|
|
return closestIcon.path;
|
|
|
|
|
|
|
|
})
|
|
|
|
.then(function(iconPath) {
|
|
|
|
debug('Closest icon %j', iconPath);
|
|
|
|
return iconPath ? path.join(ICON_THEMES_ROOTDIR, iconPath) : null;
|
|
|
|
})
|
|
|
|
;
|
|
|
|
|
|
|
|
function sizeFromPath(iconPath) {
|
|
|
|
var simpleSizeRegex = /\/(\d+)\//;
|
|
|
|
var matches = simpleSizeRegex.exec(iconPath);
|
|
|
|
if(matches && matches[1]) return +matches[1];
|
|
|
|
var doubleSizeRegex = /\/(\d+)x\d+\//;
|
|
|
|
matches = doubleSizeRegex.exec(iconPath);
|
|
|
|
if(matches && matches[1]) return +matches[1];
|
|
|
|
}
|
|
|
|
|
2015-08-28 14:20:07 +02:00
|
|
|
};
|
|
|
|
|
2015-08-31 12:24:55 +02:00
|
|
|
exports.findPixmapsIcon = function(iconName) {
|
|
|
|
var filePattern = iconName+'.{svg,png}';
|
|
|
|
debug('Looking for pixmap icon %s', filePattern);
|
|
|
|
return System.findFiles(filePattern, {cwd: PIXMAPS_ICONS_ROOTDIR})
|
|
|
|
.then(function(iconPaths) {
|
|
|
|
iconPaths = iconPaths.map(function(iconPath) {
|
|
|
|
return path.join(PIXMAPS_ICONS_ROOTDIR, iconPath);
|
|
|
|
});
|
|
|
|
return exports._selectBestIcon(iconPaths);
|
|
|
|
})
|
|
|
|
;
|
|
|
|
};
|
2015-08-30 21:29:19 +02:00
|
|
|
|
|
|
|
exports.findIconThemes = function() {
|
|
|
|
return System.findFiles('*/', {cwd: ICON_THEMES_ROOTDIR, realpath: true})
|
|
|
|
.then(function(files) {
|
2015-10-07 14:25:30 +02:00
|
|
|
return files.map(function(f) {
|
|
|
|
return path.basename(f);
|
|
|
|
});
|
2015-08-30 21:29:19 +02:00
|
|
|
})
|
|
|
|
;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.loadThemeIndex = function(themeName) {
|
|
|
|
var themeIndexPath = path.join(ICON_THEMES_ROOTDIR, themeName, 'index.theme');
|
|
|
|
return System.loadINIFile(themeIndexPath);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.themeIndexExists = function(themeName) {
|
|
|
|
var themeIndexPath = path.join(ICON_THEMES_ROOTDIR, themeName, 'index.theme');
|
|
|
|
return System.exists(themeIndexPath);
|
2015-08-28 14:20:07 +02:00
|
|
|
};
|
|
|
|
|
2015-08-31 12:24:55 +02:00
|
|
|
exports._selectBestIcon = function(iconPaths) {
|
|
|
|
var iconSelection = iconPaths.reduce(function(iconSelection, iconPath) {
|
|
|
|
if(iconPath) {
|
|
|
|
var key = iconPath.indexOf('scalable') !== -1 ? 'scalable' : 'bitmap';
|
|
|
|
iconSelection[key] = iconPath;
|
|
|
|
}
|
|
|
|
return iconSelection;
|
|
|
|
}, {scalable: null, bitmap: null});
|
|
|
|
debug('Icon selection %j', iconSelection);
|
|
|
|
return iconSelection.scalable || iconSelection.bitmap;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-08-28 14:20:07 +02:00
|
|
|
// Array helpers
|
|
|
|
|
2015-08-30 21:29:19 +02:00
|
|
|
function clean(arr) {
|
|
|
|
return arr.filter(function(item) {
|
|
|
|
return item !== null && item !== undefined;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-08-28 14:20:07 +02:00
|
|
|
function flatten(arr) {
|
|
|
|
return arr.reduce(function(result, item) {
|
|
|
|
result = result.concat.apply(result, Array.isArray(item) ? flatten(item) : [item]);
|
|
|
|
return result;
|
|
|
|
}, []);
|
|
|
|
}
|
|
|
|
|
|
|
|
function uniq(arr) {
|
|
|
|
return arr.reduce(function(result, item) {
|
|
|
|
if(result.indexOf(item) === -1) {
|
|
|
|
result.push(item);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}, []);
|
|
|
|
}
|