Use open with/save as dialog for fallback and download.

This commit is contained in:
Brendan Dahl 2012-05-08 16:11:50 -07:00
parent 982c7a0f7e
commit c1f73b96a4
3 changed files with 29 additions and 39 deletions

View File

@ -44,42 +44,37 @@ function ChromeActions() {
} }
ChromeActions.prototype = { ChromeActions.prototype = {
download: function(data) { download: function(data) {
Services.wm.getMostRecentWindow('navigator:browser').saveURL(data);
},
fallback: function(data) {
let mimeService = Cc['@mozilla.org/mime;1'].getService(Ci.nsIMIMEService); let mimeService = Cc['@mozilla.org/mime;1'].getService(Ci.nsIMIMEService);
var handlerInfo = mimeService. var handlerInfo = mimeService.
getFromTypeAndExtension('application/pdf', 'pdf'); getFromTypeAndExtension('application/pdf', 'pdf');
var uri = NetUtil.newURI(data); var uri = NetUtil.newURI(data);
var filename = Services.wm.getMostRecentWindow('navigator:browser').
getDefaultFileName('document.pdf', uri);
// Create a temporary file to output to.
var file = Cc['@mozilla.org/file/directory_service;1'].
getService(Ci.nsIProperties).
get('TmpD', Ci.nsIFile);
file.append(filename);
file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt('0666', 8));
var ostream = Cc['@mozilla.org/network/file-output-stream;1']. var extHelperAppSvc =
createInstance(Ci.nsIFileOutputStream); Cc['@mozilla.org/uriloader/external-helper-app-service;1'].
ostream.init(file, -1, -1, 0); getService(Ci.nsIExternalHelperAppService);
var frontWindow = Cc['@mozilla.org/embedcomp/window-watcher;1'].
// Fetch the file and once it's ready attempt to open it with the system's getService(Ci.nsIWindowWatcher).activeWindow;
// default pdf handler. var ioService = Services.io;
NetUtil.asyncFetch(uri, function(istream, aResult) { var channel = ioService.newChannel(data, null, null);
if (!Components.isSuccessCode(aResult)) { var listener = {
log('Error: Fetching file failed with code ' + aResult); extListener: null,
return; onStartRequest: function(aRequest, aContext) {
this.extListener = extHelperAppSvc.doContent('application/pdf',
aRequest, frontWindow, false);
this.extListener.onStartRequest(aRequest, aContext);
},
onStopRequest: function(aRequest, aContext, aStatusCode) {
if (this.extListener)
this.extListener.onStopRequest(aRequest, aContext, aStatusCode);
},
onDataAvailable: function(aRequest, aContext, aInputStream, aOffset,
aCount) {
this.extListener.onDataAvailable(aRequest, aContext, aInputStream,
aOffset, aCount);
} }
NetUtil.asyncCopy(istream, ostream, function(aResult) { };
if (!Components.isSuccessCode(aResult)) {
log('Error: Copying file failed with code: ' + aResult); channel.asyncOpen(listener, null);
return;
}
handlerInfo.preferredAction = Ci.nsIHandlerInfo.useSystemDefault;
handlerInfo.launchWithFile(file);
});
});
}, },
setDatabase: function(data) { setDatabase: function(data) {
if (this.inPrivateBrowswing) if (this.inPrivateBrowswing)

View File

@ -101,14 +101,12 @@
<span data-l10n-id="print_label">Print</span> <span data-l10n-id="print_label">Print</span>
</button> </button>
--> -->
<button id="fallback" class="toolbarButton fallback" title="Open with System Default PDF Viewer" onclick="PDFView.fallback();" tabindex="12" data-l10n-id="fallback" hidden="true">F
<span data-l10n-id="fallback_label">Open with System Default PDF Viewer</span> <button id="download" class="toolbarButton download" title="Download" onclick="PDFView.download();" tabindex="12" data-l10n-id="download">
</button>
<button id="download" class="toolbarButton download" title="Download" onclick="PDFView.download();" tabindex="13" data-l10n-id="download">
<span data-l10n-id="download_label">Download</span> <span data-l10n-id="download_label">Download</span>
</button> </button>
<!-- <div class="toolbarButtonSpacer"></div> --> <!-- <div class="toolbarButtonSpacer"></div> -->
<a href="#" id="viewBookmark" class="toolbarButton bookmark" title="Current view (copy or open in new window)" tabindex="14" data-l10n-id="bookmark"><span data-l10n-id="bookmark_label">Current View</span></a> <a href="#" id="viewBookmark" class="toolbarButton bookmark" title="Current view (copy or open in new window)" tabindex="13" data-l10n-id="bookmark"><span data-l10n-id="bookmark_label">Current View</span></a>
</div> </div>
<div class="outerCenter"> <div class="outerCenter">
<div class="innerCenter" id="toolbarViewerMiddle"> <div class="innerCenter" id="toolbarViewerMiddle">

View File

@ -375,9 +375,9 @@ var PDFView = {
}, },
fallback: function pdfViewDownload() { fallback: function pdfViewDownload() {
var url = this.url.split('#')[0];
if (!PDFJS.isFirefoxExtension) if (!PDFJS.isFirefoxExtension)
return; // can't do this with regular viewer return; // can't do this with regular viewer
var url = this.url.split('#')[0];
FirefoxCom.request('fallback', url); FirefoxCom.request('fallback', url);
}, },
@ -1359,9 +1359,6 @@ window.addEventListener('load', function webViewerLoad(evt) {
document.getElementById('fileInput').value = null; document.getElementById('fileInput').value = null;
} }
if (PDFJS.isFirefoxExtension)
document.getElementById('fallback').removeAttribute('hidden');
// Special debugging flags in the hash section of the URL. // Special debugging flags in the hash section of the URL.
var hash = document.location.hash.substring(1); var hash = document.location.hash.substring(1);
var hashParams = PDFView.parseQueryString(hash); var hashParams = PDFView.parseQueryString(hash);