overlay.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. window.clipboardAttachment = (function () {
  2. var { classes: Cc, interfaces: Ci, utils: Cu } = Components;
  3. Cu.import('resource://gre/modules/Services.jsm');
  4. Cu.import("resource://gre/modules/NetUtil.jsm");
  5. function getPasteImageType() {
  6. var prefs = Cc["@mozilla.org/preferences-service;1"]
  7. .getService(Ci.nsIPrefService)
  8. .getBranch("clipboard.");
  9. var value = prefs.getIntPref("paste_image_type");
  10. return value;
  11. }
  12. function getOrderedFlavors() {
  13. var flavors = [];
  14. switch (getPasteImageType()) {
  15. case 0:
  16. flavors.push("image/jpeg");
  17. flavors.push("image/jpg");
  18. flavors.push("image/png");
  19. flavors.push("image/gif");
  20. break;
  21. case 1:
  22. default:
  23. flavors.push("image/png");
  24. flavors.push("image/jpeg");
  25. flavors.push("image/jpg");
  26. flavors.push("image/gif");
  27. break;
  28. case 2:
  29. flavors.push("image/gif");
  30. flavors.push("image/jpeg");
  31. flavors.push("image/jpg");
  32. flavors.push("image/png");
  33. break;
  34. }
  35. flavors.push("application/x-moz-file");
  36. flavors.push("text/x-moz-url");
  37. flavors.push("text/uri-list");
  38. flavors.push("text/html");
  39. flavors.push("text/unicode");
  40. return flavors;
  41. }
  42. function getDataFromClipboard() {
  43. var trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
  44. trans.init(null);
  45. var flavors = getOrderedFlavors();
  46. for (i = 0; i < flavors.length; i++) {
  47. trans.addDataFlavor(flavors[i]);
  48. }
  49. window.Services.clipboard.getData(trans, window.Services.clipboard.kGlobalClipboard);
  50. var flavor = {};
  51. var data = {};
  52. var len = {};
  53. trans.getAnyTransferData(flavor, data, len);
  54. if (flavor.value === "text/uri-list") {
  55. // if the flavor is text/uri-list, the data will be garbage
  56. // (UTF-8 interpreted as UTF-16?), so we'll get the string again
  57. // using text/unicode which works.
  58. // We'll still return text/uri-list as flavor, though, so the resulting
  59. // string can be handled as a list of URIs.
  60. trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
  61. trans.init(null);
  62. trans.addDataFlavor("text/unicode");
  63. window.Services.clipboard.getData(trans, window.Services.clipboard.kGlobalClipboard);
  64. trans.getAnyTransferData(flavor, data, len);
  65. return { flavor: "text/uri-list", data: data.value, length: len.value };
  66. }
  67. return { flavor: flavor.value, data: data.value, length: len.value };
  68. }
  69. function createFile(name) {
  70. var file = window.FileUtils.getFile("TmpD", [name]);
  71. file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, window.FileUtils.PERMS_FILE);
  72. return file;
  73. }
  74. function writeString(data, name, callback) {
  75. var file = createFile(name);
  76. var ostream = window.FileUtils.openSafeFileOutputStream(file);
  77. var converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Ci.nsIScriptableUnicodeConverter);
  78. converter.charset = "UTF-8";
  79. var istream = converter.convertToInputStream(data);
  80. window.NetUtil.asyncCopy(istream, ostream, function () {
  81. callback(file);
  82. });
  83. }
  84. function showException(ex) {
  85. var promptService = Cc["@mozilla.org/embedcomp/prompt-service;1"].getService(Ci.nsIPromptService);
  86. var title = WL.messenger.i18n.getMessage("errorTitle");
  87. var msg = WL.messenger.i18n.getMessage("errorMessage") + ": " + ex;
  88. promptService.alert(window, title, msg);
  89. }
  90. function addFileAttachment(file) {
  91. try {
  92. var attachment = window.FileToAttachment(file);
  93. window.AddAttachments([attachment]);
  94. createdFiles.push(file);
  95. } catch (ex) {
  96. showException(ex);
  97. }
  98. }
  99. var createdFiles = [];
  100. var my = {
  101. canAttach: function () {
  102. var clipboard = window.Services.clipboard;
  103. var flavors = getOrderedFlavors();
  104. var hasFlavors = clipboard.hasDataMatchingFlavors(flavors, clipboard.kGlobalClipboard);
  105. return hasFlavors;
  106. },
  107. attachFromClipboard: function () {
  108. try {
  109. if (my.canAttach()) {
  110. var data = getDataFromClipboard();
  111. if (data.flavor.indexOf("image/") === 0) {
  112. var extension = data.flavor === "image/png" ? "png" : (data.flavor === "image/gif" ? "gif" : "jpg");
  113. var file = createFile("image." + extension);
  114. var output = window.FileUtils.openSafeFileOutputStream(file);
  115. window.NetUtil.asyncCopy(data.data, output, function () {
  116. addFileAttachment(file);
  117. });
  118. } else if (data.flavor === "text/html") {
  119. var html = data.data.QueryInterface(Ci.nsISupportsString).data;
  120. writeString(data.data, "document.html", function (file) {
  121. addFileAttachment(file);
  122. });
  123. } else if (data.flavor === "text/x-moz-url") {
  124. var text = data.data.QueryInterface(Ci.nsISupportsString).data;
  125. var lines = text.split('\n');
  126. var attachment = Cc["@mozilla.org/messengercompose/attachment;1"].createInstance(Ci.nsIMsgAttachment);
  127. attachment.url = lines[0];
  128. if (lines.length > 1) attachment.name = lines[1];
  129. if (lines.length > 2) attachment.size = lines[2];
  130. window.AddAttachments([attachment]);
  131. } else if (data.flavor === "text/uri-list") {
  132. var text = data.data.QueryInterface(Ci.nsISupportsString).data;
  133. var lines = text.split('\n');
  134. var attachments = [];
  135. for (i = 0; i < lines.length; i++) {
  136. var attachment = Cc["@mozilla.org/messengercompose/attachment;1"].createInstance(Ci.nsIMsgAttachment);
  137. var fileUri = lines[i];
  138. if (!fileUri.startsWith("file:///")) {
  139. fileUri = window.OS.Path.toFileURI(fileUri);
  140. }
  141. attachment.url = fileUri;
  142. attachments.push(attachment);
  143. }
  144. window.AddAttachments(attachments);
  145. } else if (data.flavor === "text/unicode" || data.flavor === "text/plain") {
  146. var text = data.data.QueryInterface(Ci.nsISupportsString).data;
  147. writeString(data.data, "document.txt", function (file) {
  148. addFileAttachment(file);
  149. });
  150. } else if (data.flavor === "application/x-moz-file") {
  151. var file = data.data.QueryInterface(Ci.nsIFile);
  152. var attachment = window.FileToAttachment(file);
  153. window.AddAttachments([attachment]);
  154. }
  155. }
  156. } catch (ex) {
  157. showException(ex);
  158. }
  159. },
  160. updateCommand: function () {
  161. var canAttach = my.canAttach();
  162. window.goSetCommandEnabled("attachFromClipboardCmd", canAttach);
  163. },
  164. unload: function () {
  165. for (i = 0; i < createdFiles.length; i++) {
  166. createdFiles[i].remove(false);
  167. }
  168. createdFiles = [];
  169. }
  170. };
  171. var menuEditPopup = document.getElementById("menu_EditPopup");
  172. menuEditPopup.addEventListener("popupshowing", my.updateCommand);
  173. var msgComposeWindow = document.getElementById("msgcomposeWindow");
  174. msgComposeWindow.addEventListener("compose-window-close", my.unload);
  175. msgComposeWindow.addEventListener("compose-window-unload", my.unload);
  176. return my;
  177. }());