Skip to content

TagSpaces is an offline, open source, document manager with tagging support

Adding creatingDirectoryTree to the mozilla io API

uggrock1203cf17b5ea
diff --git a/data/js/ioapi.chrome.js b/data/js/ioapi.chrome.js
index 7978d7150..59005cdeb 100644
--- a/data/js/ioapi.chrome.js
+++ b/data/js/ioapi.chrome.js
@@ -2,7 +2,7 @@
* Use of this source code is governed by a AGPL3 license that
* can be found in the LICENSE file. */
-// TODO use this http://developer.chrome.com/extensions/fileBrowserHandler.html
+// TODO use eventually this http://developer.chrome.com/extensions/fileBrowserHandler.html
// Activating browser specific IOAPI modul
console.debug("Loading IOapiChrome.js..");
@@ -165,4 +165,47 @@ IOAPI.createDirectoryIndex = function(dirPath) {
// TODO implement createDirectoryIndex
console.debug("Directory indexing functionality not implemented on chrome yet!");
alert("Directory indexing functionality not implemented on chrome yet!");
-}
\ No newline at end of file
+}
+
+IOAPI.createDirectoryTree = function(dirPath) {
+ console.debug("Creating directory index for: "+dirPath);
+ console.debug("Creating Directory Tree functionality not implemented on chrome yet!");
+ // TODO implement createDirectoryTree
+ UIAPI.ViewManager.updateTreeData();
+}
+
+/* Needed for createDirectoryTree
+function directoryTree(dirPath) {
+ try {
+ var tree = {};
+ var dirList = filesIO.list(dirPath);
+ var directory = Cc['@mozilla.org/file/local;1'].createInstance(Ci.nsIFile);
+ directory.initWithPath(dirPath);
+ console.log("Directory "+JSON.stringify(directory));
+ tree["name"] = directory.leafName;
+ tree["type"] = "directory";
+ tree["lmdt"] = directory.lastModifiedTime;
+ tree["path"] = dirPath;
+ tree["children"] = [];
+
+ for (var i=0; i < dirList.length; i++) {
+ var path = filesIO.join(dirPath,dirList[i]);
+ var file = Cc['@mozilla.org/file/local;1'].createInstance(Ci.nsIFile);
+ file.initWithPath(path);
+ if (file.isFile()) {
+ tree["children"].push({
+ "name": file.leafName,
+ "type": "file",
+ "size": file.fileSize,
+ "lmdt": file.lastModifiedTime,
+ "path": path
+ });
+ } else {
+ tree["children"].push( directoryTree(path) );
+ }
+ }
+ return tree;
+ } catch(ex) {
+ console.error("Creating directory index failed "+ex);
+ }
+}*/
\ No newline at end of file
diff --git a/data/js/ioapi.mozilla.js b/data/js/ioapi.mozilla.js
index 2bef875ad..c7aa20e5f 100644
--- a/data/js/ioapi.mozilla.js
+++ b/data/js/ioapi.mozilla.js
@@ -156,4 +156,15 @@ IOAPI.createDirectoryIndex = function(dirPath) {
"path": dirPath
}});
document.documentElement.dispatchEvent(event);
+}
+
+IOAPI.createDirectoryTree = function(dirPath) {
+ console.debug("Creating directory tree for: "+dirPath);
+ UIAPI.showLoadingAnimation();
+ var event = document.createEvent('CustomEvent');
+ event.initCustomEvent("addon-message", true, true, {"detail":{
+ "command": "createDirectoryTree",
+ "path": dirPath
+ }});
+ document.documentElement.dispatchEvent(event);
}
\ No newline at end of file
diff --git a/data/js/messaging.mozilla.js b/data/js/messaging.mozilla.js
index b8b531b0c..2ca8136e9 100644
--- a/data/js/messaging.mozilla.js
+++ b/data/js/messaging.mozilla.js
@@ -7,7 +7,7 @@ define(function(require, exports, module) {
console.debug("Loading messaging.mozilla.js..");
document.documentElement.addEventListener("addon-message1", function(event) {
- console.debug("Message received in page script from content script: "+JSON.stringify(event.detail));
+ console.debug("Message received in page script from content script"); //+JSON.stringify(event.detail));
UIAPI.hideLoadingAnimation();
var message = event.detail;
switch (message.command) {
@@ -82,12 +82,20 @@ document.documentElement.addEventListener("addon-message1", function(event) {
break;
case "indexDirectory":
if(message.success){
- console.debug("Directory Index: "+JSON.stringify(message.content));
+ //console.debug("Directory Index: "+JSON.stringify(message.content));
UIAPI.ViewManager.updateIndexData(message.content);
} else {
UIAPI.updateLogger("Indexing directory failed");
}
break;
+ case "createDirectoryTree":
+ if(message.success){
+ //console.debug("Directory tree: "+JSON.stringify(message.content));
+ UIAPI.ViewManager.updateTreeData(message.content);
+ } else {
+ UIAPI.updateLogger("Indexing directory failed");
+ }
+ break;
case "getSubdirs":
if(message.success){
var dirListing = [];
diff --git a/lib/ioutils.js b/lib/ioutils.js
index 0a592b437..9efd350e1 100644
--- a/lib/ioutils.js
+++ b/lib/ioutils.js
@@ -37,7 +37,6 @@ exports.searchDirectory = function searchDirectory(dirPath, keewords, worker) {
console.debug("Searching directory: "+dirPath+" for: "+keewords);
var resultIndex = [];
resultIndex = searchDir(dirPath, resultIndex, keewords);
-// IOUtils.saveTextFile("/media/truecrypt1/searchresult.js", JSON.stringify(resultIndex));
// console.debug("Index: "+JSON.stringify(resultIndex));
worker.postMessage({
"command": "indexDirectory",
@@ -50,7 +49,6 @@ exports.createDirectoryIndex = function createDirectoryIndex(dirPath, worker) {
console.debug("Creating index for directory: "+dirPath);
var directoryIndex = [];
directoryIndex = scanDirectory(dirPath, directoryIndex);
- // IOUtils.saveTextFile("/media/truecrypt1/myoffice.js", JSON.stringify(directoryIndex), worker);
// console.debug("Index: "+JSON.stringify(directoryIndex));
worker.postMessage({
"command": "indexDirectory",
@@ -59,25 +57,80 @@ exports.createDirectoryIndex = function createDirectoryIndex(dirPath, worker) {
});
}
+exports.createDirectoryTree = function createDirectoryTree(dirPath, worker) {
+ console.debug("Creating directory tree: "+dirPath);
+ var dirTree = directoryTree(dirPath);
+// console.debug("Index: "+JSON.stringify(dirTree));
+ worker.postMessage({
+ "command": "createDirectoryTree",
+ "success": true,
+ "content": dirTree
+ });
+}
+
+/*
+ "leafName":"2013",
+ "permissions":438,
+ "permissionsOfLink":438,
+ "lastModifiedTime":1350309208000,
+ "lastModifiedTimeOfLink":1350309208000,
+ "fileSize":0,
+ "fileSizeOfLink":0,
+ "target":"Z:\\Chronique\\2013",
+ "path":"Z:\\Chronique\\2013",
+ "parent":{}
+*/
+
+function directoryTree(dirPath) {
+ try {
+ var tree = {};
+ var dirList = filesIO.list(dirPath);
+ var directory = Cc['@mozilla.org/file/local;1'].createInstance(Ci.nsIFile);
+ directory.initWithPath(dirPath);
+ console.log("Directory "+JSON.stringify(directory));
+ tree["name"] = directory.leafName;
+ tree["type"] = "directory";
+ tree["lmdt"] = directory.lastModifiedTime;
+ tree["path"] = dirPath;
+ tree["children"] = [];
+
+ for (var i=0; i < dirList.length; i++) {
+ var path = filesIO.join(dirPath,dirList[i]);
+ var file = Cc['@mozilla.org/file/local;1'].createInstance(Ci.nsIFile);
+ file.initWithPath(path);
+ if (file.isFile()) {
+ tree["children"].push({
+ "name": file.leafName,
+ "type": "file",
+ "size": file.fileSize,
+ "lmdt": file.lastModifiedTime,
+ "path": path
+ });
+ } else {
+ tree["children"].push( directoryTree(path) );
+ }
+ }
+ return tree;
+ } catch(ex) {
+ console.error("Creating directory index failed "+ex);
+ }
+}
+
function scanDirectory(dirPath, index) {
try {
var dirList = filesIO.list(dirPath);
- var path = "";
for (var i=0; i < dirList.length; i++) {
- path = filesIO.join(dirPath,dirList[i]);
-
+ var path = filesIO.join(dirPath,dirList[i]);
var file = Cc['@mozilla.org/file/local;1'].createInstance(Ci.nsIFile);
- var currentPath = filesIO.join(dirPath,dirList[i])
- file.initWithPath(currentPath);
+ file.initWithPath(path);
index.push({
"name": file.leafName,
"type": file.isFile()?"file":"directory",
"size": file.fileSize,
"lmdt": file.lastModifiedTime,
- "path": currentPath
- });
-
- if(!file.isFile()) {
+ "path": path
+ });
+ if (!file.isFile()) {
scanDirectory(path, index);
}
}
diff --git a/lib/main.js b/lib/main.js
index 0e5aaab13..f7d4f2ab3 100644
--- a/lib/main.js
+++ b/lib/main.js
@@ -158,6 +158,9 @@ function handleMessage(msg, worker) {
break;
case "createDirectoryIndex":
ioutils.createDirectoryIndex(msg.detail.path,worker);
+ break;
+ case "createDirectoryTree":
+ ioutils.createDirectoryTree(msg.detail.path,worker);
break;
default:
break;