downloadOverlay.js (6914B)
1 /* 2 ***** BEGIN LICENSE BLOCK ***** 3 4 Copyright © 2011 Center for History and New Media 5 George Mason University, Fairfax, Virginia, USA 6 http://zotero.org 7 8 This file is part of Zotero. 9 10 Zotero is free software: you can redistribute it and/or modify 11 it under the terms of the GNU Affero General Public License as published by 12 the Free Software Foundation, either version 3 of the License, or 13 (at your option) any later version. 14 15 Zotero is distributed in the hope that it will be useful, 16 but WITHOUT ANY WARRANTY; without even the implied warranty of 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 GNU Affero General Public License for more details. 19 20 You should have received a copy of the GNU Affero General Public License 21 along with Zotero. If not, see <http://www.gnu.org/licenses/>. 22 23 ***** END LICENSE BLOCK ***** 24 */ 25 var Zotero_DownloadOverlay = new function() { 26 const PDF_MIME_TYPE = "application/pdf"; 27 const ALLOW_LIST = [ 28 "application/pdf", 29 "application/postscript", 30 "application/xhtml+xml", 31 "text/html", 32 "text/plain", 33 /^audio\//, 34 /^image\//, 35 /^video\//, 36 /^application\/vnd\.oasis\.opendocument\./, 37 /^application\/vnd\.ms-/, 38 /^application\/vnd\.openxmlformats-officedocument/, 39 /^application\/vnd\.lotus-/, 40 /^application\/vnd\.wolfram\./, 41 "application/vnd.wordperfect", 42 "application/wordperfect5.1", 43 "application/msword", 44 "application/x-latex" 45 ]; 46 47 /** 48 * Saves this item, if we are supposed to save it. 49 * 50 * @return {Boolean} True if an item was saved, false if we were not supposed to save 51 */ 52 this.handleSave = Zotero.Promise.coroutine(function* () { 53 if(!document.getElementById('zotero-radio').selected) return false; 54 55 var retrieveMetadata = document.getElementById('zotero-recognizePDF').selected; 56 57 var url = dialog.mLauncher.source.spec; 58 Zotero.debug("Zotero_DownloadOverlay: Downloading from "+url); 59 60 // set up progress window 61 var win = Components.classes["@mozilla.org/appshell/window-mediator;1"] 62 .getService(Components.interfaces.nsIWindowMediator) 63 .getMostRecentWindow("navigator:browser"); 64 var libraryID, collection; 65 try { 66 let itemGroup = win.ZoteroPane.getCollectionTreeRow(); 67 if (itemGroup.filesEditable && !itemGroup.isPublications()) { 68 libraryID = win.ZoteroPane.getSelectedLibraryID(); 69 collection = win.ZoteroPane.getSelectedCollection(); 70 } 71 // TODO: Just show an error instead? 72 else { 73 Zotero.debug("Cannot save files to library " + itemGroup.ref.libraryID 74 + " -- saving to My Library instead", 2); 75 libraryID = Zotero.Libraries.userLibraryID; 76 } 77 } catch(e) { 78 Zotero.debug(e, 1); 79 }; 80 81 var recognizePDF = document.getElementById('zotero-recognizePDF').checked 82 && !document.getElementById('zotero-recognizePDF').hidden 83 && !document.getElementById('zotero-recognizePDF').disabled; 84 var contentType = dialog.mLauncher.MIMEInfo.MIMEType; 85 86 // mimic dialog cancellation 87 dialog.onCancel(); 88 89 // show progress dialog 90 var progressWin = new Zotero.ProgressWindow(); 91 progressWin.changeHeadline(Zotero.getString("save.link")); 92 progressWin.show(); 93 94 // perform import 95 try { 96 var item = yield Zotero.Attachments.importFromURL({ 97 libraryID, 98 url, 99 collections: collection ? [collection.id] : [], 100 contentType 101 }); 102 } 103 catch (e) { 104 if (!win) return; 105 progressWin.addDescription(Zotero.getString("save.link.error")); 106 progressWin.startCloseTimer(8000); 107 Zotero.logError(e); 108 return false; 109 } 110 111 if(!win) return; 112 113 progressWin.addLines([item.getDisplayTitle()], [item.getImageSrc()]); 114 progressWin.startCloseTimer(); 115 if (collection) { 116 yield collection.addItem(item.id); 117 } 118 119 yield win.ZoteroPane.selectItem(item.id); 120 121 if(recognizePDF) { 122 var timer = Components.classes["@mozilla.org/timer;1"] 123 .createInstance(Components.interfaces.nsITimer); 124 timer.init(function() { 125 try { 126 if (item && item.getFile()) { 127 timer.cancel(); 128 Zotero.RecognizePDF.recognizeItems([item]); 129 } 130 } catch(e) { dump(e.toSource()) }; 131 }, 1000, Components.interfaces.nsITimer.TYPE_REPEATING_SLACK); 132 } 133 134 return true; 135 }); 136 137 /** 138 * Called when mode in dialog has been changed 139 */ 140 this.modeChanged = function() { 141 var zoteroSelected = document.getElementById('zotero-radio').selected; 142 143 // don't allow user to remember Zotero option for file type; i'm not sure anyone wants this 144 // to happen automatically 145 if(zoteroSelected) document.getElementById('rememberChoice').selected = false; 146 document.getElementById('rememberChoice').disabled = zoteroSelected; 147 document.getElementById('zotero-recognizePDF').disabled = !zoteroSelected; 148 149 Zotero_DownloadOverlay.updateLibraryNote(); 150 }; 151 152 /** 153 * Determines whether the note stating that the item will be saved to "My Library" is shown 154 */ 155 this.updateLibraryNote = function() { 156 var zoteroSelected = document.getElementById('zotero-radio').selected; 157 var zp = Zotero.getActiveZoteroPane(), canSave = true; 158 try { 159 canSave = zp.getCollectionTreeRow().filesEditable; 160 } catch(e) { 161 Zotero.logError(e); 162 }; 163 document.getElementById('zotero-saveToLibrary-description').hidden = !zoteroSelected || canSave; 164 window.sizeToContent(); 165 } 166 167 /** 168 * Called when the save dialog is opened 169 */ 170 this.init = function() { 171 if(Zotero.isConnector) return; 172 173 // Disable for filetypes people probably don't want to save 174 var show = false; 175 var mimeType = dialog.mLauncher.MIMEInfo.MIMEType.toLowerCase(); 176 for (let elem of ALLOW_LIST) { 177 if(typeof elem === "string") { 178 if(elem === mimeType) { 179 document.getElementById('zotero-container').hidden = false; 180 document.getElementById('zotero-radio').disabled = false; 181 break; 182 } 183 } else if(elem.test(mimeType)) { 184 document.getElementById('zotero-container').hidden = false; 185 document.getElementById('zotero-radio').disabled = false; 186 break; 187 } 188 } 189 190 // Hook in event listener to ondialogaccept 191 document.documentElement.setAttribute('ondialogaccept', 192 'Zotero_DownloadOverlay.handleSave().then(function (saved) { if (!saved) {' 193 + document.documentElement.getAttribute('ondialogaccept') 194 +'}})'); 195 196 // Hook in event listener for mode change 197 var radios = document.getElementById('mode'). 198 addEventListener("command", Zotero_DownloadOverlay.modeChanged, false); 199 200 // Set label on retrieve PDF option 201 if(mimeType === PDF_MIME_TYPE) { 202 var recognizePDF = document.getElementById('zotero-recognizePDF'); 203 recognizePDF.label = Zotero.getString("pane.items.menu.recognizePDF"); 204 recognizePDF.hidden = false; 205 recognizePDF.disabled = true; 206 } 207 }; 208 } 209 210 window.addEventListener("load", Zotero_DownloadOverlay.init, false); 211 window.addEventListener("activate", Zotero_DownloadOverlay.updateLibraryNote, false);