Skip to content

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

Added some changes in the rename functionality

uggrock1cbd4d3edfd4
diff --git a/data/js/ioapi.mozilla.js b/data/js/ioapi.mozilla.js
index 363fc6e17..73b8b7031 100644
--- a/data/js/ioapi.mozilla.js
+++ b/data/js/ioapi.mozilla.js
@@ -27,17 +27,17 @@ define(function(require, exports, module) {
TSCORE.generateTagGroups();
} catch (ex) {
- console.log("Exception while getting setting from firefox failed "+ex)
+ console.log("Exception while getting setting from firefox failed "+ex);
}
} else {
- console.log("Getting setting from firefox failed")
+ console.log("Getting setting from firefox failed");
}
break;
case "saveSettings":
if(message.success) {
- console.log("Saving setting as native mozilla preference successfull!")
+ console.log("Saving setting as native mozilla preference successfull!");
} else {
- console.log("Saving setting as native mozilla preference failed!")
+ console.log("Saving setting as native mozilla preference failed!");
}
break;
case "rename":
@@ -137,7 +137,7 @@ define(function(require, exports, module) {
"content": content
}});
document.documentElement.dispatchEvent(event);
- }
+ };
exports.loadSettings = function() {
console.log("Loading setting from firefox preferences...");
@@ -147,7 +147,7 @@ define(function(require, exports, module) {
"command": "loadSettings"
}});
document.documentElement.dispatchEvent(event);
- }
+ };
exports.createDirectory = function(dirPath) {
console.log("Directory "+dirPath+" created.");
@@ -158,7 +158,7 @@ define(function(require, exports, module) {
"path": dirPath
}});
document.documentElement.dispatchEvent(event);
- }
+ };
exports.loadTextFile = function(filePath) {
console.log("Loading file: "+filePath);
@@ -169,11 +169,11 @@ define(function(require, exports, module) {
"path": filePath
}});
document.documentElement.dispatchEvent(event);
- }
+ };
exports.renameFile = function(filePath, newFilePath) {
console.log("Renaming "+filePath+" to "+newFilePath);
- TSCORE.showLoadingAnimation();
+ TSCORE.showLoadingAnimation();
var event = document.createEvent('CustomEvent');
event.initCustomEvent("addon-message", true, true, {"detail":{
"command": "rename",
@@ -181,7 +181,7 @@ define(function(require, exports, module) {
"newPath": newFilePath
}});
document.documentElement.dispatchEvent(event);
- }
+ };
exports.saveTextFile = function(filePath,content) {
console.log("Saving file: "+filePath);
@@ -193,7 +193,7 @@ define(function(require, exports, module) {
"content": content
}});
document.documentElement.dispatchEvent(event);
- }
+ };
exports.listDirectory = function(dirPath) {
console.log("Listing directory: "+dirPath);
@@ -204,7 +204,7 @@ define(function(require, exports, module) {
"path": dirPath
}});
document.documentElement.dispatchEvent(event);
- }
+ };
exports.getSubdirs = function(dirPath) {
console.log("Getting subdirs: "+dirPath);
@@ -215,7 +215,7 @@ define(function(require, exports, module) {
"path": dirPath
}});
document.documentElement.dispatchEvent(event);
- }
+ };
exports.deleteElement = function(path) {
console.log("Deleting: "+path);
@@ -226,11 +226,11 @@ define(function(require, exports, module) {
"path": path
}});
document.documentElement.dispatchEvent(event);
- }
+ };
exports.checkAccessFileURLAllowed = function() {
console.log("checkAccessFileURLAllowed function not relevant for node..");
- }
+ };
exports.selectDirectory = function() {
console.log("Selecting directory...");
@@ -239,7 +239,7 @@ define(function(require, exports, module) {
"command": "selectDirectory",
}});
document.documentElement.dispatchEvent(event);
- }
+ };
exports.selectFile = function() {
console.log("Selecting file...");
@@ -248,7 +248,7 @@ define(function(require, exports, module) {
"command": "selectFile"
}});
document.documentElement.dispatchEvent(event);
- }
+ };
exports.openDirectory = function(dirPath) {
console.log("Opening directory: "+dirPath);
@@ -258,7 +258,7 @@ define(function(require, exports, module) {
"path": dirPath,
}});
document.documentElement.dispatchEvent(event);
- }
+ };
exports.openExtensionsDirectory = function() {
console.log("Opening extensions directory...");
@@ -267,7 +267,7 @@ define(function(require, exports, module) {
"command": "openExtensionsDirectory"
}});
document.documentElement.dispatchEvent(event);
- }
+ };
exports.createDirectoryIndex = function(dirPath) {
console.log("Creating directory index for: "+dirPath);
@@ -278,7 +278,7 @@ define(function(require, exports, module) {
"path": dirPath
}});
document.documentElement.dispatchEvent(event);
- }
+ };
exports.createDirectoryTree = function(dirPath) {
console.log("Creating directory tree for: "+dirPath);
@@ -289,7 +289,7 @@ define(function(require, exports, module) {
"path": dirPath
}});
document.documentElement.dispatchEvent(event);
- }
+ };
exports.checkNewVersion = function() {
console.log("Checking for new version...");
@@ -299,6 +299,6 @@ define(function(require, exports, module) {
"command": "checkNewVersion",
}});
document.documentElement.dispatchEvent(event);
- }
+ };
});
diff --git a/data/libs/requirecss/normalize.js b/data/libs/requirecss/normalize.js
index a66e72396..4d81eb1cb 100644
--- a/data/libs/requirecss/normalize.js
+++ b/data/libs/requirecss/normalize.js
@@ -32,11 +32,32 @@
define(['require', 'module'], function(require, module) {
+ // regular expression for removing double slashes
+ // eg http://www.example.com//my///url/here -> http://www.example.com/my/url/here
+ var slashes = /([^:])\/+/g
+ var removeDoubleSlashes = function(uri) {
+ return uri.replace(slashes, '$1/');
+ }
+
+ // given a relative URI, and two absolute base URIs, convert it from one base to another
+ var protocolRegEx = /[^\:\/]*:\/\/([^\/])*/
function convertURIBase(uri, fromBase, toBase) {
+ if(uri.indexOf("data:") === 0)
+ return uri;
+ uri = removeDoubleSlashes(uri);
// absolute urls are left in tact
- if (uri.match(/^\/|([^\:\/]*:)/))
+ if (uri.match(/^\//) || uri.match(protocolRegEx))
return uri;
- return relativeURI(absoluteURI(uri, fromBase), toBase);
+ // if toBase specifies a protocol path, ensure this is the same protocol as fromBase, if not
+ // use absolute path at fromBase
+ var toBaseProtocol = toBase.match(protocolRegEx);
+ var fromBaseProtocol = fromBase.match(protocolRegEx);
+ if (fromBaseProtocol && (!toBaseProtocol || toBaseProtocol[1] != fromBaseProtocol[1] || toBaseProtocol[2] != fromBaseProtocol[2]))
+ return absoluteURI(uri, fromBase);
+
+ else {
+ return relativeURI(absoluteURI(uri, fromBase), toBase);
+ }
};
// given a relative URI, calculate the absolute URI
@@ -88,28 +109,26 @@ define(['require', 'module'], function(require, module) {
return out.substr(0, out.length - 1);
};
- var normalizeCSS = function(source, fromBase, toBase) {
-
- var urlRegEx = /(url\(\s*"(.*)"\s*\))|(url\(\s*'(.*)'\s*\))|(url\(\s*(.*)\s*\))/g;
+ var normalizeCSS = function(source, fromBase, toBase, cssBase) {
+
+ fromBase = removeDoubleSlashes(fromBase);
+ toBase = removeDoubleSlashes(toBase);
+
+ var urlRegEx = /@import\s*("([^"]*)"|'([^']*)')|url\s*\(\s*(\s*"([^"]*)"|'([^']*)'|[^\)]*\s*)\s*\)/ig;
var result, url, source;
while (result = urlRegEx.exec(source)) {
- url = result[2] || result[4] || result[6];
- var newUrl = convertURIBase(url, fromBase, toBase);
- var quoteLen = result[2] || result[4] ? 1 : 0;
+ url = result[3] || result[2] || result[5] || result[6] || result[4];
+ var newUrl;
+ if (cssBase && url.substr(0, 1) == '/')
+ newUrl = cssBase + url;
+ else
+ newUrl = convertURIBase(url, fromBase, toBase);
+ var quoteLen = result[5] || result[6] ? 1 : 0;
source = source.substr(0, urlRegEx.lastIndex - url.length - quoteLen - 1) + newUrl + source.substr(urlRegEx.lastIndex - quoteLen - 1);
urlRegEx.lastIndex = urlRegEx.lastIndex + (newUrl.length - url.length);
}
- var importRegEx = /(@import\s*'(.*)')|(@import\s*"(.*)")/g;
-
- while (result = importRegEx.exec(source)) {
- url = result[2] || result[4];
- var newUrl = convertURIBase(url, fromBase, toBase);
- source = source.substr(0, importRegEx.lastIndex - url.length - 1) + newUrl + source.substr(importRegEx.lastIndex - 1);
- importRegEx.lastIndex = importRegEx.lastIndex + (newUrl.length - url.length);
- }
-
return source;
};
diff --git a/lib/ioutils.js b/lib/ioutils.js
index d63b07007..af7c43487 100644
--- a/lib/ioutils.js
+++ b/lib/ioutils.js
@@ -41,7 +41,7 @@ function searchDir(dirPath, index, keewords) {
"name": dirList[i],
"type": isFile?"file":"directory",
"path": path
- })
+ });
}
if(!isFile) {
searchDir(path, index, keewords);
@@ -63,7 +63,7 @@ exports.searchDirectory = function searchDirectory(dirPath, keewords, worker) {
"success": true,
"content": directoryIndex
});
-}
+};
exports.createDirectoryIndex = function createDirectoryIndex(dirPath, worker) {
console.log("Creating index for directory: "+dirPath);
@@ -75,7 +75,7 @@ exports.createDirectoryIndex = function createDirectoryIndex(dirPath, worker) {
"success": true,
"content": directoryIndex
});
-}
+};
exports.createDirectoryTree = function createDirectoryTree(dirPath, worker) {
console.log("Creating directory tree: "+dirPath);
@@ -86,7 +86,7 @@ exports.createDirectoryTree = function createDirectoryTree(dirPath, worker) {
"success": true,
"content": dirTree
});
-}
+};
/*
"leafName":"2013",
@@ -204,7 +204,7 @@ exports.listDirectory = function listDirectory(dirPath, worker) {
"success": false
});
}
-}
+};
exports.createDirectory = function createDirectory(filePath, worker) {
console.log("Creating directory: "+filePath);
@@ -222,7 +222,7 @@ exports.createDirectory = function createDirectory(filePath, worker) {
});
console.error("Creating failed "+ex);
}
-}
+};
exports.loadTextFile = function loadTextFile(filePath, worker) {
console.log("Loading text file: "+filePath);
@@ -241,7 +241,7 @@ exports.loadTextFile = function loadTextFile(filePath, worker) {
});
console.error("Loading file failed "+ex);
}
-}
+};
exports.getSubdirs = function getSubdirs(dirPath, worker) {
console.log("Getting subdirectories: "+dirPath);
@@ -279,7 +279,7 @@ exports.getSubdirs = function getSubdirs(dirPath, worker) {
"success": false
});
}
-}
+};
exports.deleteElement = function deleteElement(path, worker) {
console.log("Deleting: "+path);
@@ -306,58 +306,80 @@ exports.deleteElement = function deleteElement(path, worker) {
});
console.error("Deleting file failed "+ex);
}
-}
+};
exports.rename = function rename(filePath, newFilePath, worker) {
console.log("Renaming file: "+filePath+" to "+newFilePath);
// Checks if the file already exists.
- if(!filesIO.exists(newFilePath)) {
- try {
- var targetDir;
- var targetFileName;
- // Detecting WINNT os
- console.log("Input filePath: "+filePath+" newFilePath: "+newFilePath);
- if(runtime.OS.toLowerCase().indexOf("win")!=-1) {
- targetDir = newFilePath.substring(0,newFilePath.lastIndexOf("\\"));
- targetFileName = newFilePath.substring(newFilePath.lastIndexOf("\\")+1,newFilePath.length);
- } else {
- targetDir = newFilePath.substring(0,newFilePath.lastIndexOf("/"));
- targetFileName = newFilePath.substring(newFilePath.lastIndexOf("/")+1,newFilePath.length);
- }
- console.log("Target dir: "+targetDir+" filename: "+targetFileName);
- var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
- file.initWithPath(filePath);
-
- //let FileUtils = Cu.import('resource://gre/modules/FileUtils.jsm').FileUtils;
- //var targetDirFile = new FileUtils.File(targetDir);
-
- var targetDirFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
- targetDirFile.initWithPath(filesIO.join(targetDir,""));
- //targetDirFile.initWithPath(targetDir);
-
- console.log("After init: "+file.path+" targetdir: "+targetDirFile.path+" targetfilename: "+targetFileName);
- file.moveTo(targetDirFile,targetFileName);
- var targetPath = filesIO.join(targetDir,targetFileName);
- worker.postMessage({
- "command": "rename",
- "success": true,
- "content": [filePath, targetPath]
- });
+ if(!filesIO.exists(newFilePath)) {
+ try {
+ // TODO Remove the OSX custom code after https://bugzilla.mozilla.org/show_bug.cgi?id=913663 is fixed
+ if(runtime.OS.toLowerCase().indexOf("mac")>=0) {
+ console.log("Renaming under macos");
+
+ //var url = Cc["@mozilla.org/file/local;1"].newURI(filePath, null, null);
+ //if (!url || !url.schemeIs("file")) throw "Expected a file URL.";
+ //var binaryFile = url.QueryInterface(Ci.nsIFileURL).file;
+
+ var binaryFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
+ binaryFile.initWithPath( filePath );
+
+ var istream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream);
+ istream.init(binaryFile, -1, -1, false);
+ var bstream = Cc["@mozilla.org/binaryinputstream;1"].createInstance(Ci.nsIBinaryInputStream);
+ bstream.setInputStream(istream);
+
+ var aFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
+ aFile.initWithPath( newFilePath );
+ aFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0600);
+
+ var stream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(Ci.nsIFileOutputStream);
+ stream.init(aFile, 0x04 | 0x08 | 0x20, 0600, 0); // readwrite, create, truncate
+ stream.write(bstream,bstream.length);
+ if (stream instanceof Ci.nsISafeOutputStream) {
+ stream.finish();
+ } else {
+ stream.close();
+ }
+
+ filesIO.remove(filePath);
+ worker.postMessage({ "command": "rename", "success": true, "content": [filePath, newFilePath] });
+ } else {
+ var targetDir;
+ var targetFileName;
+ // Detecting WINNT os
+ if(runtime.OS.toLowerCase().indexOf("win")!=-1) {
+ targetDir = newFilePath.substring(0,newFilePath.lastIndexOf("\\"));
+ targetFileName = newFilePath.substring(newFilePath.lastIndexOf("\\")+1,newFilePath.length);
+ } else {
+ targetDir = newFilePath.substring(0,newFilePath.lastIndexOf("/"));
+ targetFileName = newFilePath.substring(newFilePath.lastIndexOf("/")+1,newFilePath.length);
+ }
+ console.log("Target dir: "+targetDir+" filename: "+targetFileName);
+ var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
+ file.initWithPath(filePath);
+
+ //let FileUtils = Cu.import('resource://gre/modules/FileUtils.jsm').FileUtils;
+ //var targetDirFile = new FileUtils.File(targetDir);
+
+ var targetDirFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
+ targetDirFile.initWithPath(filesIO.join(targetDir,""));
+ //targetDirFile.initWithPath(targetDir);
+
+ console.log("After init: "+file.path+" targetdir: "+targetDirFile.path+" targetfilename: "+targetFileName);
+ file.moveTo(targetDirFile,targetFileName);
+ var targetPath = filesIO.join(targetDir,targetFileName);
+ worker.postMessage({ "command": "rename", "success": true, "content": [filePath, targetPath] });
+ }
} catch(ex) {
- worker.postMessage({
- "command": "rename",
- "success": false
- });
+ worker.postMessage({ "command": "rename", "success": false });
console.error("Renaming failed "+ex);
}
- } else {
- worker.postMessage({
- "command": "rename",
- "success": false
- });
- console.error("Renaming failed. Target filepath already exists.");
- }
-}
+ } else {
+ worker.postMessage({ "command": "rename", "success": false });
+ console.error("Renaming failed. Target filepath already exists.");
+ }
+};
exports.saveTextFile = function saveTextFile(filePath, content, worker) {
console.log("Saving file: "+filePath);
@@ -385,7 +407,7 @@ exports.saveTextFile = function saveTextFile(filePath, content, worker) {
});
console.error("Save failed "+ex);
}
-}
+};
/**
* Returning a path to a file selected from the user
@@ -461,7 +483,7 @@ exports.openDirectory = function openDirectory(dirPath, worker) {
} catch(ex) {
console.error("Opening directory failed "+ex);
}
-}
+};
/**
* Open the directory where the user can install tagspaces extensions
@@ -481,4 +503,4 @@ exports.openExtensionsDirectory = function openExtensionsDirectory(worker) {
} catch(ex) {
console.error("Opening extensions directory failed "+ex);
} */
-}
+};