2011-09-28 09:16:26 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
2010-09-30 17:08:22 -04:00
|
|
|
const Gio = imports.gi.Gio;
|
|
|
|
const GLib = imports.gi.GLib;
|
2012-11-30 10:36:12 -05:00
|
|
|
const Lang = imports.lang;
|
|
|
|
const Params = imports.misc.params;
|
2010-09-30 17:08:22 -04:00
|
|
|
|
|
|
|
function listDirAsync(file, callback) {
|
|
|
|
let allFiles = [];
|
2012-12-07 19:42:09 -05:00
|
|
|
file.enumerate_children_async('standard::name,standard::type',
|
2010-09-30 17:08:22 -04:00
|
|
|
Gio.FileQueryInfoFlags.NONE,
|
|
|
|
GLib.PRIORITY_LOW, null, function (obj, res) {
|
|
|
|
let enumerator = obj.enumerate_children_finish(res);
|
|
|
|
function onNextFileComplete(obj, res) {
|
|
|
|
let files = obj.next_files_finish(res);
|
|
|
|
if (files.length) {
|
|
|
|
allFiles = allFiles.concat(files);
|
|
|
|
enumerator.next_files_async(100, GLib.PRIORITY_LOW, null, onNextFileComplete);
|
|
|
|
} else {
|
|
|
|
enumerator.close(null);
|
|
|
|
callback(allFiles);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
enumerator.next_files_async(100, GLib.PRIORITY_LOW, null, onNextFileComplete);
|
|
|
|
});
|
|
|
|
}
|
2011-09-10 17:10:50 -04:00
|
|
|
|
2012-11-30 10:36:12 -05:00
|
|
|
function _collectFromDirectoryAsync(dir, loadState) {
|
2012-12-05 19:28:50 -05:00
|
|
|
function done() {
|
|
|
|
loadState.numLoading--;
|
|
|
|
if (loadState.loadedCallback &&
|
|
|
|
loadState.numLoading == 0)
|
|
|
|
loadState.loadedCallback(loadState.data);
|
|
|
|
}
|
|
|
|
|
2012-12-07 19:42:09 -05:00
|
|
|
dir.query_info_async('standard::type', Gio.FileQueryInfoFlags.NONE,
|
2012-11-30 10:36:12 -05:00
|
|
|
GLib.PRIORITY_DEFAULT, null, function(object, res) {
|
|
|
|
try {
|
|
|
|
object.query_info_finish(res);
|
|
|
|
} catch (e) {
|
|
|
|
if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.NOT_FOUND))
|
|
|
|
log(e.message);
|
2012-12-05 19:28:50 -05:00
|
|
|
done();
|
2012-11-30 10:36:12 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
listDirAsync(dir, Lang.bind(this, function(infos) {
|
|
|
|
for (let i = 0; i < infos.length; i++)
|
|
|
|
loadState.processFile(dir.get_child(infos[i].get_name()),
|
|
|
|
infos[i], loadState.data);
|
2012-12-05 19:28:50 -05:00
|
|
|
done();
|
2012-11-30 10:36:12 -05:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function collectFromDatadirsAsync(subdir, params) {
|
|
|
|
params = Params.parse(params, { includeUserDir: false,
|
|
|
|
processFile: null,
|
|
|
|
loadedCallback: null,
|
|
|
|
data: null });
|
|
|
|
let loadState = { data: params.data,
|
|
|
|
numLoading: 0,
|
|
|
|
loadedCallback: params.loadedCallback,
|
|
|
|
processFile: params.processFile };
|
|
|
|
|
|
|
|
if (params.processFile == null) {
|
|
|
|
if (params.loadedCallback)
|
|
|
|
params.loadedCallback(params.data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let dataDirs = GLib.get_system_data_dirs();
|
|
|
|
if (params.includeUserDir)
|
|
|
|
dataDirs.unshift(GLib.get_user_data_dir());
|
2012-12-05 19:28:50 -05:00
|
|
|
loadState.numLoading = dataDirs.length;
|
2012-11-30 10:36:12 -05:00
|
|
|
|
|
|
|
for (let i = 0; i < dataDirs.length; i++) {
|
|
|
|
let path = GLib.build_filenamev([dataDirs[i], 'gnome-shell', subdir]);
|
|
|
|
let dir = Gio.File.new_for_path(path);
|
|
|
|
|
|
|
|
_collectFromDirectoryAsync(dir, loadState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-10 17:10:50 -04:00
|
|
|
function deleteGFile(file) {
|
|
|
|
// Work around 'delete' being a keyword in JS.
|
|
|
|
return file['delete'](null);
|
|
|
|
}
|
|
|
|
|
2012-06-26 20:47:44 -04:00
|
|
|
function recursivelyDeleteDir(dir, deleteParent) {
|
2011-09-10 17:10:50 -04:00
|
|
|
let children = dir.enumerate_children('standard::name,standard::type',
|
|
|
|
Gio.FileQueryInfoFlags.NONE, null);
|
|
|
|
|
|
|
|
let info, child;
|
|
|
|
while ((info = children.next_file(null)) != null) {
|
|
|
|
let type = info.get_file_type();
|
|
|
|
let child = dir.get_child(info.get_name());
|
|
|
|
if (type == Gio.FileType.REGULAR)
|
|
|
|
deleteGFile(child);
|
2011-11-30 21:46:52 -05:00
|
|
|
else if (type == Gio.FileType.DIRECTORY)
|
2012-06-26 20:47:44 -04:00
|
|
|
recursivelyDeleteDir(child, true);
|
2011-09-10 17:10:50 -04:00
|
|
|
}
|
|
|
|
|
2012-06-26 20:47:44 -04:00
|
|
|
if (deleteParent)
|
|
|
|
deleteGFile(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
function recursivelyMoveDir(srcDir, destDir) {
|
|
|
|
let children = srcDir.enumerate_children('standard::name,standard::type',
|
|
|
|
Gio.FileQueryInfoFlags.NONE, null);
|
|
|
|
|
|
|
|
if (!destDir.query_exists(null))
|
|
|
|
destDir.make_directory_with_parents(null);
|
|
|
|
|
|
|
|
let info, child;
|
|
|
|
while ((info = children.next_file(null)) != null) {
|
|
|
|
let type = info.get_file_type();
|
|
|
|
let srcChild = srcDir.get_child(info.get_name());
|
|
|
|
let destChild = destDir.get_child(info.get_name());
|
|
|
|
if (type == Gio.FileType.REGULAR)
|
|
|
|
srcChild.move(destChild, Gio.FileCopyFlags.NONE, null, null);
|
|
|
|
else if (type == Gio.FileType.DIRECTORY)
|
|
|
|
recursivelyMoveDir(srcChild, destChild);
|
|
|
|
}
|
2011-09-10 17:10:50 -04:00
|
|
|
}
|