1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 cr.define('print_preview', function() {
9 * Print destination data object that holds data for both local and cloud
11 * @param {string} id ID of the destination.
12 * @param {!print_preview.Destination.Type} type Type of the destination.
13 * @param {!print_preview.Destination.Origin} origin Origin of the
15 * @param {string} displayName Display name of the destination.
16 * @param {boolean} isRecent Whether the destination has been used recently.
17 * @param {!print_preview.Destination.ConnectionStatus} connectionStatus
18 * Connection status of the print destination.
19 * @param {{tags: Array.<string>,
21 * lastAccessTime: ?number,
22 * isTosAccepted: ?boolean,
23 * cloudID: ?string}=} opt_params Optional parameters for the
27 function Destination(id, type, origin, displayName, isRecent,
28 connectionStatus, opt_params) {
30 * ID of the destination.
37 * Type of the destination.
38 * @type {!print_preview.Destination.Type}
44 * Origin of the destination.
45 * @type {!print_preview.Destination.Origin}
48 this.origin_ = origin;
51 * Display name of the destination.
55 this.displayName_ = displayName;
58 * Whether the destination has been used recently.
62 this.isRecent_ = isRecent;
65 * Tags associated with the destination.
66 * @type {!Array.<string>}
69 this.tags_ = (opt_params && opt_params.tags) || [];
72 * Print capabilities of the destination.
73 * @type {print_preview.Cdd}
76 this.capabilities_ = null;
79 * Whether the destination is owned by the user.
83 this.isOwned_ = (opt_params && opt_params.isOwned) || false;
86 * Cache of destination location fetched from tags.
90 this.location_ = null;
93 * Connection status of the destination.
94 * @type {!print_preview.Destination.ConnectionStatus}
97 this.connectionStatus_ = connectionStatus;
100 * Number of milliseconds since the epoch when the printer was last
105 this.lastAccessTime_ = (opt_params && opt_params.lastAccessTime) ||
109 * Whether the user has accepted the terms-of-service for the print
110 * destination. Only applies to the FedEx Office cloud-based printer.
111 * {@code} null if terms-of-service does not apply to the print destination.
115 this.isTosAccepted_ = (opt_params && opt_params.isTosAccepted) || false;
118 * Cloud ID for privet printers
122 this.cloudID_ = (opt_params && opt_params.cloudID) || '';
126 * Prefix of the location destination tag.
130 Destination.LOCATION_TAG_PREFIX = '__cp__printer-location=';
133 * Enumeration of Google-promoted destination IDs.
136 Destination.GooglePromotedId = {
137 DOCS: '__google__docs',
138 FEDEX: '__google__fedex',
139 SAVE_AS_PDF: 'Save as PDF'
143 * Enumeration of the types of destinations.
153 * Enumeration of the origin types for cloud destinations.
156 Destination.Origin = {
165 * Enumeration of the connection statuses of printer destinations.
168 Destination.ConnectionStatus = {
173 UNREGISTERED: 'UNREGISTERED'
177 * Enumeration of relative icon URLs for various types of destinations.
181 Destination.IconUrl_ = {
182 CLOUD: 'images/printer.png',
183 CLOUD_SHARED: 'images/printer_shared.png',
184 LOCAL: 'images/printer.png',
185 MOBILE: 'images/mobile.png',
186 MOBILE_SHARED: 'images/mobile_shared.png',
187 THIRD_PARTY: 'images/third_party.png',
188 PDF: 'images/pdf.png',
189 DOCS: 'images/google_doc.png',
190 FEDEX: 'images/third_party_fedex.png'
193 Destination.prototype = {
194 /** @return {string} ID of the destination. */
199 /** @return {!print_preview.Destination.Type} Type of the destination. */
205 * @return {!print_preview.Destination.Origin} Origin of the destination.
211 /** @return {string} Display name of the destination. */
213 return this.displayName_;
216 /** @return {boolean} Whether the destination has been used recently. */
218 return this.isRecent_;
222 * @param {boolean} isRecent Whether the destination has been used recently.
224 set isRecent(isRecent) {
225 this.isRecent_ = isRecent;
229 * @return {boolean} Whether the user owns the destination. Only applies to
230 * cloud-based destinations.
233 return this.isOwned_;
236 /** @return {boolean} Whether the destination is local or cloud-based. */
238 return this.origin_ == Destination.Origin.LOCAL ||
239 (this.origin_ == Destination.Origin.PRIVET &&
240 this.connectionStatus_ !=
241 Destination.ConnectionStatus.UNREGISTERED);
244 /** @return {boolean} Whether the destination is a privet local printer */
246 return this.origin_ == Destination.Origin.PRIVET;
250 * @return {string} The location of the destination, or an empty string if
251 * the location is unknown.
254 if (this.location_ == null) {
256 for (var tag, i = 0; tag = this.tags_[i]; i++) {
257 if (tag.indexOf(Destination.LOCATION_TAG_PREFIX) == 0) {
258 this.location_ = tag.substring(
259 Destination.LOCATION_TAG_PREFIX.length) || '';
264 return this.location_;
267 /** @return {!Array.<string>} Tags associated with the destination. */
269 return this.tags_.slice(0);
272 /** @return {string} Cloud ID associated with the destination */
274 return this.cloudID_;
277 /** @return {print_preview.Cdd} Print capabilities of the destination. */
279 return this.capabilities_;
283 * @param {!print_preview.Cdd} capabilities Print capabilities of the
286 set capabilities(capabilities) {
287 this.capabilities_ = capabilities;
291 * @return {!print_preview.Destination.ConnectionStatus} Connection status
292 * of the print destination.
294 get connectionStatus() {
295 return this.connectionStatus_;
299 * @param {!print_preview.Destination.ConnectionStatus} status Connection
300 * status of the print destination.
302 set connectionStatus(status) {
303 this.connectionStatus_ = status;
306 /** @return {boolean} Whether the destination is considered offline. */
308 return arrayContains([print_preview.Destination.ConnectionStatus.OFFLINE,
309 print_preview.Destination.ConnectionStatus.DORMANT],
310 this.connectionStatus_);
313 /** @return {string} Human readable status for offline destination. */
314 get offlineStatusText() {
315 if (!this.isOffline) {
318 var offlineDurationMs = Date.now() - this.lastAccessTime_;
319 var offlineMessageId;
320 if (offlineDurationMs > 31622400000.0) { // One year.
321 offlineMessageId = 'offlineForYear';
322 } else if (offlineDurationMs > 2678400000.0) { // One month.
323 offlineMessageId = 'offlineForMonth';
324 } else if (offlineDurationMs > 604800000.0) { // One week.
325 offlineMessageId = 'offlineForWeek';
327 offlineMessageId = 'offline';
329 return localStrings.getString(offlineMessageId);
333 * @return {number} Number of milliseconds since the epoch when the printer
336 get lastAccessTime() {
337 return this.lastAccessTime_;
340 /** @return {string} Relative URL of the destination's icon. */
342 if (this.id_ == Destination.GooglePromotedId.DOCS) {
343 return Destination.IconUrl_.DOCS;
344 } else if (this.id_ == Destination.GooglePromotedId.FEDEX) {
345 return Destination.IconUrl_.FEDEX;
346 } else if (this.id_ == Destination.GooglePromotedId.SAVE_AS_PDF) {
347 return Destination.IconUrl_.PDF;
348 } else if (this.isLocal) {
349 return Destination.IconUrl_.LOCAL;
350 } else if (this.type_ == Destination.Type.MOBILE && this.isOwned_) {
351 return Destination.IconUrl_.MOBILE;
352 } else if (this.type_ == Destination.Type.MOBILE) {
353 return Destination.IconUrl_.MOBILE_SHARED;
354 } else if (this.isOwned_) {
355 return Destination.IconUrl_.CLOUD;
357 return Destination.IconUrl_.CLOUD_SHARED;
362 * @return {?boolean} Whether the user has accepted the terms-of-service of
363 * the print destination or {@code null} if a terms-of-service does not
366 get isTosAccepted() {
367 return this.isTosAccepted_;
371 * @param {?boolean} Whether the user has accepted the terms-of-service of
372 * the print destination or {@code null} if a terms-of-service does not
375 set isTosAccepted(isTosAccepted) {
376 this.isTosAccepted_ = isTosAccepted;
380 * Matches a query against the destination.
381 * @param {string} query Query to match against the destination.
382 * @return {boolean} {@code true} if the query matches this destination,
383 * {@code false} otherwise.
385 matches: function(query) {
386 return this.displayName_.toLowerCase().indexOf(
387 query.toLowerCase().trim()) != -1;
392 * The CDD (Cloud Device Description) describes the capabilities of a print
398 * vendor_capability: !Array.<{Object}>,
399 * collate: {default: boolean=}=,
403 * vendor_id: string=,
404 * custom_display_name: string=,
405 * is_default: boolean=
408 * copies: {default: number=, max: number=}=,
409 * duplex: {option: !Array.<{type: string=, is_default: boolean=}>}=,
410 * page_orientation: {
411 * option: !Array.<{type: string=, is_default: boolean=}>
420 Destination: Destination,