Skip to content

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

Added functionality for opening a directory, selecting a file and a directory from the file system

uggrock22217ba1b6ae
diff --git a/data/js/directories.ui.js b/data/js/directories.ui.js
index fb39936b4..5aaf60f75 100644
--- a/data/js/directories.ui.js
+++ b/data/js/directories.ui.js
@@ -176,6 +176,12 @@ DirectoriesUI.initDialogs = function() {
height:240,
modal: true,
buttons: {
+ "Select directory": function() {
+ IOAPI.selectDirectory();
+ },
+ "Select directory": function() {
+ IOAPI.selectDirectory();
+ },
"Create": function() {
TSSETTINGS.createFavorite($("#favoriteName").val(), $("#favoriteLocation").val());
DirectoriesUI.initFavorites();
diff --git a/data/js/ioapi.mozilla.js b/data/js/ioapi.mozilla.js
index f5fe4ad2b..2e6c039ee 100644
--- a/data/js/ioapi.mozilla.js
+++ b/data/js/ioapi.mozilla.js
@@ -108,4 +108,32 @@ IOAPI.deleteElement = function(path) {
"path": path
}});
document.documentElement.dispatchEvent(event);
-}
+}
+
+IOAPI.selectDirectory = function() {
+ console.debug("Selecting directory...");
+ var event = document.createEvent('CustomEvent');
+ event.initCustomEvent("addon-message", true, true, {"detail":{
+ "command": "selectDirectory"
+ }});
+ document.documentElement.dispatchEvent(event);
+}
+
+IOAPI.selectFile = function() {
+ console.debug("Selecting file...");
+ var event = document.createEvent('CustomEvent');
+ event.initCustomEvent("addon-message", true, true, {"detail":{
+ "command": "selectFile"
+ }});
+ document.documentElement.dispatchEvent(event);
+}
+
+IOAPI.openDirectory = function(dirPath) {
+ console.debug("Opening directory: "+dirPath);
+ var event = document.createEvent('CustomEvent');
+ event.initCustomEvent("addon-message", true, true, {"detail":{
+ "command": "openDirectory",
+ "path": dirPath
+ }});
+ document.documentElement.dispatchEvent(event);
+}
diff --git a/data/js/messaging.mozilla.js b/data/js/messaging.mozilla.js
index 0f612dfe3..e51101403 100644
--- a/data/js/messaging.mozilla.js
+++ b/data/js/messaging.mozilla.js
@@ -102,6 +102,14 @@ document.documentElement.addEventListener("addon-message1", function(event) {
UIAPI.updateLogger("Delete failed");
}
break;
+ case "selectDirectory":
+ if(message.success){
+ // TODO make the use of this function more general
+ $("#favoriteLocation").val(message.content);
+ } else {
+ UIAPI.updateLogger("Selecting directory failed.");
+ }
+ break;
default:
break;
}
diff --git a/lib/ioutils.js b/lib/ioutils.js
index 6742c5c0b..b1549ef12 100644
--- a/lib/ioutils.js
+++ b/lib/ioutils.js
@@ -130,7 +130,6 @@ exports.listDirectory = function listDirectory(dirPath, worker) {
var anotatedDirList = [];
for (var i=0; i < dirList.length; i++) {
var type = "directory";
- // TODO Add last changed timestamp to the annotation data
var file = Cc['@mozilla.org/file/local;1'].createInstance(Ci.nsIFile);
file.initWithPath(filesIO.join(dirPath,dirList[i]));
anotatedDirList.push({
@@ -283,3 +282,78 @@ exports.saveTextFile = function saveTextFile(filePath, content, worker) {
console.error("Save failed "+ex);
}
}
+
+/**
+ * Returning a path to a file selected from the user
+ * documentation is https://developer.mozilla.org/en/NsIFilePicker
+ */
+exports.promptFileOpenPicker = function promptFileOpenPicker(worker) {
+ console.debug("Opening file selection dialog...");
+ try {
+ var window = require("window-utils").activeWindow;
+ var fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
+ fp.init(window, "PDF Open", Ci.nsIFilePicker.modeOpen);
+ fp.appendFilter("PDF files", "*.pdf");
+ fp.appendFilters(Ci.nsIFilePicker.filterAll);
+ fp.filterIndex = 0;
+ var result = fp.show();
+ if (result === Ci.nsIFilePicker.returnOK || result === Ci.nsIFilePicker.returnReplace) {
+ worker.postMessage({
+ "command": "selectFile",
+ "success": true,
+ "content": fp.file.path
+ });
+ }
+ } catch(ex) {
+ worker.postMessage({
+ "command": "selectFile",
+ "success": false
+ });
+ console.error("File selection failed "+ex);
+ }
+};
+
+/**
+ * Returning a path to a directory selected from the user
+ * documentation is https://developer.mozilla.org/en/NsIFilePicker
+ */
+exports.promptDirectorySelector = function promptDirectorySelector(worker) {
+ console.debug("Opening directory selection dialog...");
+ try {
+ var window = require("window-utils").activeWindow;
+ var fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
+ fp.init(window, "Directory Selection", Ci.nsIFilePicker.modeGetFolder);
+ var result = fp.show();
+ if (result === Ci.nsIFilePicker.returnOK) {
+ worker.postMessage({
+ "command": "selectDirectory",
+ "success": true,
+ "content": fp.file.path
+ });
+ }
+ } catch(ex) {
+ worker.postMessage({
+ "command": "selectDirectory",
+ "success": false
+ });
+ console.error("Directory selection failed "+ex);
+ }
+};
+
+/**
+ * Open a directory in the default directory viewer (e.g. windows explorer)
+ * http://stackoverflow.com/questions/9453786
+ */
+exports.openDirectory = function openDirectory(dirPath, worker) {
+ console.debug("Opening directory: "+dirPath);
+ dirPath = "file://"+dirPath;
+ try {
+ var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
+ var uri = ioService.newURI(dirPath, null, null);
+ if (uri instanceof Ci.nsIFileURL && uri.file.isDirectory()) {
+ uri.file.QueryInterface(Ci.nsILocalFile).launch();
+ }
+ } catch(ex) {
+ console.error("Opening directory failed "+ex);
+ }
+}
\ No newline at end of file
diff --git a/lib/main.js b/lib/main.js
index d9fdbbcd0..b56515a0f 100644
--- a/lib/main.js
+++ b/lib/main.js
@@ -19,7 +19,7 @@ exports.main = function(options, callbacks) {
var mainUIMod = require("page-mod");
mainUIMod.PageMod({
- include: data.url("index.html"), //"resource://jid1-fbamkxtiftsahq-at-jetpack/tagspaces/data/index.html",
+ include: data.url("index.html"),
contentScript: ''+
'self.on("message", function(message) {'+
'console.debug("Message received in content script from addon: "+JSON.stringify(message));'+
@@ -65,8 +65,9 @@ exports.main = function(options, callbacks) {
}
});
- // Adding menuitem to the tools menu
- var menuitem = require("menuitems").Menuitem({
+ // Adding menuitem to the tools menu
+ //TODO Enable menuitem
+/* var menuitem = require("menuitems").Menuitem({
id: "TagSpacesMenuItem",
menuid: "menu_ToolsPopup",
label: "TagSpaces",
@@ -78,7 +79,7 @@ exports.main = function(options, callbacks) {
});
},
insertbefore: "menu_pageInfo"
- });
+ }); */
};
exports.onUnload = function(reason) {
@@ -120,8 +121,17 @@ function handleMessage(msg, worker) {
break;
case "delete":
ioutils.deleteElement(msg.detail.path,worker);
+ break;
+ case "selectFile":
+ ioutils.promptFileOpenPicker(worker);
+ break;
+ case "selectDirectory":
+ ioutils.promptDirectorySelector(worker);
+ break;
+ case "openDirectory":
+ ioutils.openDirectory(msg.detail.path,worker);
break;
default:
break;
}
-}
+}
\ No newline at end of file