]> git.parisson.com Git - pdf.js.git/commitdiff
Add fallback option for the extension.
authorBrendan Dahl <brendan.dahl@gmail.com>
Tue, 8 May 2012 20:05:33 +0000 (13:05 -0700)
committerBrendan Dahl <brendan.dahl@gmail.com>
Tue, 8 May 2012 20:05:33 +0000 (13:05 -0700)
extensions/firefox/components/PdfStreamConverter.js
web/viewer.html
web/viewer.js

index af9cf41b8bc858b94068c026ebceeb620b6fecc2..5d7ac69eace44861d0109faacfcf20973d760b64 100644 (file)
@@ -16,6 +16,7 @@ const MAX_DATABASE_LENGTH = 4096;
 
 Cu.import('resource://gre/modules/XPCOMUtils.jsm');
 Cu.import('resource://gre/modules/Services.jsm');
+Cu.import('resource://gre/modules/NetUtil.jsm');
 
 let application = Cc['@mozilla.org/fuel/application;1']
                     .getService(Ci.fuelIApplication);
@@ -45,6 +46,41 @@ ChromeActions.prototype = {
   download: function(data) {
     Services.wm.getMostRecentWindow('navigator:browser').saveURL(data);
   },
+  fallback: function(data) {
+    let mimeService = Cc['@mozilla.org/mime;1'].getService(Ci.nsIMIMEService);
+    var handlerInfo = mimeService.
+                        getFromTypeAndExtension('application/pdf', 'pdf');
+    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'].
+                    createInstance(Ci.nsIFileOutputStream);
+    ostream.init(file, -1, -1, 0);
+
+    // Fetch the file and once it's ready attempt to open it with the system's
+    // default pdf handler.
+    NetUtil.asyncFetch(uri, function(istream, aResult) {
+      if (!Components.isSuccessCode(aResult)) {
+        log('Error: Fetching file failed with code ' + aResult);
+        return;
+      }
+      NetUtil.asyncCopy(istream, ostream, function(aResult) {
+        if (!Components.isSuccessCode(aResult)) {
+          log('Error: Copying file failed with code: ' + aResult);
+          return;
+        }
+        handlerInfo.preferredAction = Ci.nsIHandlerInfo.useSystemDefault;
+        handlerInfo.launchWithFile(file);
+      });
+    });
+  },
   setDatabase: function(data) {
     if (this.inPrivateBrowswing)
       return;
index 0ec05e031943eb650a87d2cce05accbef8ac4f85..fa836fdab75e1ca7c9d905c765b73d57dd334da1 100644 (file)
                   <span data-l10n-id="print_label">Print</span>
                 </button>
                 -->
-
-                <button id="download" class="toolbarButton download" title="Download" onclick="PDFView.download();" tabindex="12" data-l10n-id="download">
+                <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>
+                <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>
                 </button>
                 <!-- <div class="toolbarButtonSpacer"></div> -->
-                <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>
+                <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>
               </div>
               <div class="outerCenter">
                 <div class="innerCenter" id="toolbarViewerMiddle">
index e8eb9ad3b6c762247ea696737915c89311994f0d..b15b123a41fba123d3fb9274776efcff699d7978 100644 (file)
@@ -374,6 +374,13 @@ var PDFView = {
     }
   },
 
+  fallback: function pdfViewDownload() {
+    if (!PDFJS.isFirefoxExtension)
+      return; // can't do this with regular viewer
+    var url = this.url.split('#')[0];
+    FirefoxCom.request('fallback', url);
+  },
+
   navigateTo: function pdfViewNavigateTo(dest) {
     if (typeof dest === 'string')
       dest = this.destinations[dest];
@@ -1352,6 +1359,9 @@ window.addEventListener('load', function webViewerLoad(evt) {
     document.getElementById('fileInput').value = null;
   }
 
+  if (PDFJS.isFirefoxExtension)
+    document.getElementById('fallback').removeAttribute('hidden');
+
   // Special debugging flags in the hash section of the URL.
   var hash = document.location.hash.substring(1);
   var hashParams = PDFView.parseQueryString(hash);