Index: chrome/browser/resources/print_preview.js |
diff --git a/chrome/browser/resources/print_preview.js b/chrome/browser/resources/print_preview.js |
index d6adce83b27dfa34c8e8700583fb262f3ebb0789..12b9e85df4b568480a6f1ae334004fa300fa1038 100644 |
--- a/chrome/browser/resources/print_preview.js |
+++ b/chrome/browser/resources/print_preview.js |
@@ -2,8 +2,13 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
+// require: cr/ui/print_preview_cloud.js |
+ |
var localStrings = new LocalStrings(); |
+var useCloudPrint = false; |
+var maxCloudPrinters = 10; |
+ |
// The total page count of the previewed document regardless of which pages the |
// user has selected. |
var totalPageCount = -1; |
@@ -24,8 +29,12 @@ var lastSelectedPrinterIndex = 0; |
var previewModifiable = false; |
// Destination list special value constants. |
+const ADD_PRINTER = 'addPrinter'; |
+const MANAGE_CLOUD_PRINTERS = 'manageCloudPrinters'; |
+const MANAGE_LOCAL_PRINTERS = 'manageLocalPrinters'; |
+const MORE_PRINTERS = 'morePrinters'; |
+const SIGN_IN = 'signIn'; |
const PRINT_TO_PDF = 'Print To PDF'; |
-const MANAGE_PRINTERS = 'Manage Printers'; |
// State of the print preview settings. |
var printSettings = new PrintSettings(); |
@@ -200,22 +209,56 @@ function updateControlsWithSelectedPrinterCapabilities() { |
var selectedIndex = printerList.selectedIndex; |
if (selectedIndex < 0) |
return; |
- |
+ var skip_refresh = false; |
var selectedValue = printerList.options[selectedIndex].value; |
- if (selectedValue == PRINT_TO_PDF) { |
- updateWithPrinterCapabilities({'disableColorOption': true, |
- 'setColorAsDefault': true, |
- 'disableCopiesOption': true}); |
- } else if (selectedValue == MANAGE_PRINTERS) { |
+ if (printerList.options[selectedIndex].isCloudPrint) { |
+ updateWithCloudPrinterCapabilities(); |
+ skip_refresh = true; |
+ } else if (selectedValue == SIGN_IN || |
+ selectedValue == MANAGE_CLOUD_PRINTERS || |
+ selectedValue == MANAGE_LOCAL_PRINTERS) { |
printerList.selectedIndex = lastSelectedPrinterIndex; |
- chrome.send('managePrinters'); |
+ chrome.send(selectedValue); |
return; |
sanjeevr
2011/06/15 20:24:36
You forgot setting skip_refresh to true here inste
|
+ } else if (selectedValue == PRINT_TO_PDF) { |
+ updateWithPrinterCapabilities({'disableColorOption': true, |
+ 'setColorAsDefault': true, |
+ 'disableCopiesOption': true, |
+ 'disableLandscapeOption': false}); |
} else { |
// This message will call back to 'updateWithPrinterCapabilities' |
// function. |
chrome.send('getPrinterCapabilities', [selectedValue]); |
} |
+ if (!skip_refresh) { |
+ lastSelectedPrinterIndex = selectedIndex; |
+ |
+ // Regenerate the preview data based on selected printer settings. |
+ setDefaultValuesAndRegeneratePreview(); |
+ } |
+} |
+ |
+function updateWithCloudPrinterCapabilities() { |
+ var printerList = $('printer-list'); |
+ var selectedIndex = printerList.selectedIndex; |
+ if (printerList.options[selectedIndex].capabilities) { |
+ doUpdateCloudPrinterCapabilities( |
+ printerList.options[selectedIndex], |
+ printerList.options[selectedIndex].capabilities); |
+ } else { |
+ cloudprint.updatePrinterCaps(printerList.options[selectedIndex], |
+ doUpdateCloudPrinterCapabilities); |
+ } |
+} |
+function doUpdateCloudPrinterCapabilities(printer) { |
+ var settings = {'disableColorOption': !cloudprint.supportsColor(printer), |
+ 'setColorAsDefault': cloudprint.colorIsDefault(printer), |
+ 'disableCopiesOption': true, |
+ 'disableLandscapeOption': true}; |
+ updateWithPrinterCapabilities(settings); |
+ var printerList = $('printer-list'); |
+ var selectedIndex = printerList.selectedIndex; |
lastSelectedPrinterIndex = selectedIndex; |
// Regenerate the preview data based on selected printer settings. |
@@ -230,6 +273,7 @@ function updateWithPrinterCapabilities(settingInfo) { |
var disableColorOption = settingInfo.disableColorOption; |
var disableCopiesOption = settingInfo.disableCopiesOption; |
var setColorAsDefault = settingInfo.setColorAsDefault; |
+ var disableLandscapeOption = settingInfo.disableLandscapeOption; |
var colorOption = $('color'); |
var bwOption = $('bw'); |
@@ -241,6 +285,12 @@ function updateWithPrinterCapabilities(settingInfo) { |
$('hr-before-copies').classList.add('invisible'); |
} |
+ if (disableLandscapeOption) { |
+ fadeOutElement($('landscape-option')); |
+ } else { |
+ fadeInElement($('landscape-option')); |
+ } |
+ |
disableColorOption ? fadeOutElement($('color-options')) : |
fadeInElement($('color-options')); |
@@ -251,6 +301,31 @@ function updateWithPrinterCapabilities(settingInfo) { |
} |
/** |
+ * Turn on the integration of Cloud Print. |
+ * @param {enable} true if cloud print should be used. |
+ */ |
+function setUseCloudPrint(enable, cloudPrintURL) { |
+ useCloudPrint = enable; |
+ cloudprint.setBaseURL(cloudPrintURL); |
+} |
+ |
+/** |
+ * Take the PDF data handed to us and submit it to the cloud, closing the print |
+ * preview tab once the upload is successful. |
+ * @param {pdfData} data to send as the print job. |
+ */ |
+function printToCloud(data) { |
+ cloudprint.printToCloud(data, finishedCloudPrinting); |
+} |
+ |
+/** |
+ * Cloud print upload of the PDF file is finished, time to close the dialog. |
+ */ |
+function finishedCloudPrinting() { |
+ window.location = cloudprint.getBaseURL(); |
+} |
+ |
+/** |
* Validates the copies text field value. |
* NOTE: An empty copies field text is considered valid because the blur event |
* listener of this field will set it back to a default value. |
@@ -341,19 +416,27 @@ function getDuplexMode() { |
* @return {string} JSON string with print job settings. |
*/ |
function getSettingsJSON() { |
+ var printerList = $('printer-list'); |
+ var selectedPrinter = printerList.selectedIndex; |
var printAll = $('all-pages').checked; |
var deviceName = getSelectedPrinterName(); |
var printToPDF = (deviceName == PRINT_TO_PDF); |
- return JSON.stringify({'deviceName': deviceName, |
- 'pageRange': getSelectedPageRanges(), |
- 'printAll': printAll, |
- 'duplex': getDuplexMode(), |
- 'copies': getCopies(), |
- 'collate': isCollated(), |
- 'landscape': isLandscape(), |
- 'color': isColor(), |
- 'printToPDF': printToPDF}); |
+ var settings = {'deviceName': deviceName, |
+ 'pageRange': getSelectedPageRanges(), |
+ 'printAll': printAll, |
+ 'duplex': getDuplexMode(), |
+ 'copies': getCopies(), |
+ 'collate': isCollated(), |
+ 'landscape': isLandscape(), |
+ 'color': isColor(), |
+ 'printToPDF': printToPDF}; |
+ if (printerList.options[selectedPrinter].isCloudPrint) { |
+ settings['cloudPrintID'] = |
+ printerList.options[selectedPrinter].value; |
+ } |
+ |
+ return JSON.stringify(settings); |
} |
/** |
@@ -386,10 +469,12 @@ function printFile() { |
$('cancel-button').classList.add('loading'); |
$('print-summary').innerHTML = localStrings.getString('printing'); |
removeEventListeners(); |
- window.setTimeout(function() { chrome.send('print', [getSettingsJSON()]); }, |
- 1000); |
+ var printerList = $('printer-list') |
+ var printer = printerList[printerList.selectedIndex]; |
+ window.setTimeout(function() { chrome.send('print', |
+ [getSettingsJSON(), cloudprint.getPrintTicketJSON(printer)]); }, 1000); |
} else { |
- chrome.send('print', [getSettingsJSON()]); |
+ chrome.send('print', [getSettingsJSON(), null]); |
} |
} |
@@ -426,6 +511,16 @@ function setDefaultPrinter(printer) { |
*/ |
function setPrinters(printers) { |
var printerList = $('printer-list'); |
+ if (useCloudPrint) { |
+ cloudprint.fetchPrinters(addCloudPrinters); |
+ if (printers.length > 0) { |
+ option = addDestinationListOption('', '', false, true); |
+ option = addDestinationListOption(localStrings.getString('localPrinters'), |
+ '', |
+ false, |
+ true); |
+ } |
+ } |
// If there exists a dummy printer value, then setDefaultPrinter() already |
// requested a preview, so no need to do it again. |
var needPreview = (printerList[0].value == ''); |
@@ -448,9 +543,15 @@ function setPrinters(printers) { |
false); |
addDestinationListOption('', '', false, true); |
- // Add an option to manage printers. |
- addDestinationListOption(localStrings.getString('managePrinters'), |
- MANAGE_PRINTERS, false, false); |
+ // Add options to manage printers. |
+ if (!cr.isChromeOS) { |
+ addDestinationListOption(localStrings.getString('manageLocalPrinters'), |
+ MANAGE_LOCAL_PRINTERS, false, false); |
+ } |
+ if (useCloudPrint) { |
+ addDestinationListOption(localStrings.getString('manageCloudPrinters'), |
+ MANAGE_CLOUD_PRINTERS, false, false); |
+ } |
printerList.disabled = false; |
@@ -464,6 +565,7 @@ function setPrinters(printers) { |
* @param {String} optionValue specifies the option value. |
* @param {boolean} isDefault is true if the option needs to be selected. |
* @param {boolean} isDisabled is true if the option needs to be disabled. |
+ * returns the created option. |
*/ |
function addDestinationListOption(optionText, optionValue, isDefault, |
isDisabled) { |
@@ -473,6 +575,44 @@ function addDestinationListOption(optionText, optionValue, isDefault, |
$('printer-list').add(option); |
option.selected = isDefault; |
option.disabled = isDisabled; |
+ return option; |
+} |
+ |
+/** |
+ * Add cloud printers to the list drop down. |
+ * Called from the cloudprint object on receipt of printer information from the |
+ * cloud print server. |
+ */ |
+function addCloudPrinters(printers) { |
+ addDestinationListOption(localStrings.getString('cloudPrinters'), |
+ '', |
+ false, |
+ true); |
+ if (printers != null) { |
+ var l = printers.length; |
+ if (l > maxCloudPrinters) { |
+ l = maxCloudPrinters; |
+ } |
+ for (var i = 0; i < l; i++) { |
+ var option = addDestinationListOption(printers[i]['name'], |
+ printers[i]['id'], |
+ i == 0, |
+ printers[i]['name'] == defaultOrLastUsedPrinterName); |
+ option.isCloudPrint = true; |
+ } |
+ if (printers.length == 0) { |
+ addDestinationListOption(localStrings.getString('addCloudPrinter'), |
+ ADD_PRINTER, false, false); |
+ } |
+ if (l < printers.length) { |
+ var option = addDestinationListOption('', '', false, true); |
+ addDestinationListOption(localStrings.getString('morePrinters'), |
+ MORE_PRINTERS, false, false); |
+ } |
+ } else { |
+ addDestinationListOption(localStrings.getString('signIn'), |
+ SIGN_IN, false, false); |
+ } |
} |
/** |
@@ -486,6 +626,8 @@ function setColor(color) { |
return; |
} |
pdfViewer.grayscale(!color); |
+ var printerList = $('printer-list') |
+ cloudprint.setColor(printerList[printerList.selectedIndex], color); |
} |
/** |
@@ -1038,3 +1180,4 @@ PrintSettings.prototype.save = function() { |
this.deviceName = getSelectedPrinterName(); |
this.isLandscape = isLandscape(); |
} |
+ |