diff options
author | Matěj Cepl <mcepl@redhat.com> | 2010-06-07 16:48:07 +0200 |
---|---|---|
committer | Matěj Cepl <mcepl@redhat.com> | 2010-06-07 16:48:07 +0200 |
commit | ad87ad529947e29adbee0dc2016069b71495a609 (patch) | |
tree | 1e92f8a955fb49cdc20747168021a81869071ad4 /lib/clipboard.js | |
parent | 2b95e7ae6cb53f987931de8f567870b9e69cdf9a (diff) | |
download | bugzilla-triage-ad87ad529947e29adbee0dc2016069b71495a609.tar.gz |
Adding JEP-10 support.
Diffstat (limited to 'lib/clipboard.js')
-rw-r--r-- | lib/clipboard.js | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/lib/clipboard.js b/lib/clipboard.js new file mode 100644 index 0000000..e5b4ac8 --- /dev/null +++ b/lib/clipboard.js @@ -0,0 +1,128 @@ +// Released under the MIT/X11 license +// http://www.opensource.org/licenses/mit-license.php + +/** + * returns content of the system clipboard + * @return string with the content of the clipboard or "" if empty. + * originally from + * https://developer.mozilla.org/en/Using_the_Clipboard + * + */ +// TODO to-be-replaced by +// var contents = jetpack.clipboard.get(); +// http://hsivonen.iki.fi/kesakoodi/clipboard/ +// https://wiki.mozilla.org/Labs/Jetpack/JEP/10 + +function getClipboard() { + const kClipboardContractID = "@mozilla.org/widget/clipboard;1"; + const kClipboardIID = Ci.nsIClipboard; + var clip = Cc[kClipboardContractID].getService(kClipboardIID); + if (!clip) { + throw new Error("No access to the clipboard!"); + } + return clip; +} + +function createTransferable() { + const kTransferableContractID = "@mozilla.org/widget/transferable;1"; + const kTransferableIID = Ci.nsITransferable + var trans = Cc[kTransferableContractID].createInstance(kTransferableIID); + if (!trans) { + throw new Error("No access to the transfer object during the set of clipboard!"); + } + return trans; +} + +var getMethod = exports.get = function getMethod( flavor ) { + var pastetext = "", mimeType = "", stuff = {}; + var len = 0, clipId = 0, clip = {}, trans = {}; + + // flavor argument is optional + if (flavor === undefined) { + flavor = "plain"; + } + + if (flavor === "plain") { + mimeType = "text/unicode"; + } else if (favor === "html") { + mimeType = "text/html"; + } else { + throw new Error("Unsupported flavor '" + flavor + "'!"); + } + + clip = getClipboard(); + + trans = createTransferable(); + + trans.addDataFlavor(mimeType); + clip.getData(trans, clip.kGlobalClipboard); + + var str = {}; + var strLength = {}; + + trans.getTransferData(mimeType, str, strLength); + + if (str) { + str = str.value.QueryInterface(Ci.nsISupportsString); + pastetext = str.data.substring(0, strLength.value / 2); + } + return pastetext; +}; + +var setMethod = exports.set = function setMethod(content, flavor) { + var mimeType = "", stuff = {}; + var len = 0, clipId = 0, clip = {}, trans = {}; + + // flavor argument is optional + if (flavor === undefined) { + flavor = "plain"; + } + + if (flavor === "plain") { + mimeType = "text/unicode"; + } else if (favor === "html") { + mimeType = "text/html"; + } else { + throw new Error("Unsupported flavor '" + flavor + "'!"); + } + + stuff = Cc["@mozilla.org/supports-string;1"]. + createInstance(Ci.nsISupportsString); + if (!stuff) { + return false; + } + stuff.data = content; + len = content.length * 2; + + clip = getClipboard(); + + trans = createTransferable(); + + trans.addDataFlavor(mimeType); + trans.setTransferData(mimeType, stuff, content.length * 2); + + clip.setData(trans, null, clip.kGlobalClipboard); +}; + +function createSupportsWString() { + return Cc["@mozilla.org/supports-wstring;1"]. + createInstance(Ci.nsISupportsWString); +} + +var flavorsMethod = exports.getCurrentFlavors = function flavorsMethod() { + // currently the only possible flavors in Jetpack-prototype are "plain" and + // "html", i.e., "text/plain" (or text/unicode?) and "text/html" (or + // application/xml+xhtml?) + var possibleTypes = { + "text/unicode": "plain", + "text/plain": "plain", + "text/html": "html" + }; + var flavourArray = createSupportsArray(); + + for (mime in possibleTypes) { + var kSuppString = createSupportsWString(); + kSuppString.data = mime; + flavourArray.AppendElement(kSuppString); + } +}; |