2011-09-28 13:16:26 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
2010-09-30 21:08:22 +00:00
|
|
|
const Gio = imports.gi.Gio;
|
|
|
|
const GLib = imports.gi.GLib;
|
|
|
|
|
|
|
|
function listDirAsync(file, callback) {
|
|
|
|
let allFiles = [];
|
|
|
|
file.enumerate_children_async(Gio.FILE_ATTRIBUTE_STANDARD_NAME,
|
|
|
|
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 21:10:50 +00:00
|
|
|
|
|
|
|
function deleteGFile(file) {
|
|
|
|
// Work around 'delete' being a keyword in JS.
|
|
|
|
return file['delete'](null);
|
|
|
|
}
|
|
|
|
|
2012-06-27 00:47:44 +00:00
|
|
|
function recursivelyDeleteDir(dir, deleteParent) {
|
2011-09-10 21:10:50 +00: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-12-01 02:46:52 +00:00
|
|
|
else if (type == Gio.FileType.DIRECTORY)
|
2012-06-27 00:47:44 +00:00
|
|
|
recursivelyDeleteDir(child, true);
|
2011-09-10 21:10:50 +00:00
|
|
|
}
|
|
|
|
|
2012-06-27 00:47:44 +00: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());
|
|
|
|
log([srcChild.get_path(), destChild.get_path()]);
|
|
|
|
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 21:10:50 +00:00
|
|
|
}
|