Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / print_preview / data / destination.js
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.
4
5 cr.define('print_preview', function() {
6   'use strict';
7
8   /**
9    * Print destination data object that holds data for both local and cloud
10    * destinations.
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
14    *     destination.
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>,
20    *          isOwned: ?boolean,
21    *          lastAccessTime: ?number,
22    *          isTosAccepted: ?boolean,
23    *          cloudID: ?string}=} opt_params Optional parameters for the
24    *     destination.
25    * @constructor
26    */
27   function Destination(id, type, origin, displayName, isRecent,
28                        connectionStatus, opt_params) {
29     /**
30      * ID of the destination.
31      * @type {string}
32      * @private
33      */
34     this.id_ = id;
35
36     /**
37      * Type of the destination.
38      * @type {!print_preview.Destination.Type}
39      * @private
40      */
41     this.type_ = type;
42
43     /**
44      * Origin of the destination.
45      * @type {!print_preview.Destination.Origin}
46      * @private
47      */
48     this.origin_ = origin;
49
50     /**
51      * Display name of the destination.
52      * @type {string}
53      * @private
54      */
55     this.displayName_ = displayName;
56
57     /**
58      * Whether the destination has been used recently.
59      * @type {boolean}
60      * @private
61      */
62     this.isRecent_ = isRecent;
63
64     /**
65      * Tags associated with the destination.
66      * @type {!Array.<string>}
67      * @private
68      */
69     this.tags_ = (opt_params && opt_params.tags) || [];
70
71     /**
72      * Print capabilities of the destination.
73      * @type {print_preview.Cdd}
74      * @private
75      */
76     this.capabilities_ = null;
77
78     /**
79      * Whether the destination is owned by the user.
80      * @type {boolean}
81      * @private
82      */
83     this.isOwned_ = (opt_params && opt_params.isOwned) || false;
84
85     /**
86      * Cache of destination location fetched from tags.
87      * @type {?string}
88      * @private
89      */
90     this.location_ = null;
91
92     /**
93      * Connection status of the destination.
94      * @type {!print_preview.Destination.ConnectionStatus}
95      * @private
96      */
97     this.connectionStatus_ = connectionStatus;
98
99     /**
100      * Number of milliseconds since the epoch when the printer was last
101      * accessed.
102      * @type {number}
103      * @private
104      */
105     this.lastAccessTime_ = (opt_params && opt_params.lastAccessTime) ||
106                            Date.now();
107
108     /**
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.
112      * @type {?boolean}
113      * @private
114      */
115     this.isTosAccepted_ = (opt_params && opt_params.isTosAccepted) || false;
116
117     /**
118      * Cloud ID for privet printers
119      * @type {?string}
120      * @private
121      */
122     this.cloudID_ = (opt_params && opt_params.cloudID) || '';
123   };
124
125   /**
126    * Prefix of the location destination tag.
127    * @type {string}
128    * @const
129    */
130   Destination.LOCATION_TAG_PREFIX = '__cp__printer-location=';
131
132   /**
133    * Enumeration of Google-promoted destination IDs.
134    * @enum {string}
135    */
136   Destination.GooglePromotedId = {
137     DOCS: '__google__docs',
138     FEDEX: '__google__fedex',
139     SAVE_AS_PDF: 'Save as PDF'
140   };
141
142   /**
143    * Enumeration of the types of destinations.
144    * @enum {string}
145    */
146   Destination.Type = {
147     GOOGLE: 'google',
148     LOCAL: 'local',
149     MOBILE: 'mobile'
150   };
151
152   /**
153    * Enumeration of the origin types for cloud destinations.
154    * @enum {string}
155    */
156   Destination.Origin = {
157     LOCAL: 'local',
158     COOKIES: 'cookies',
159     PROFILE: 'profile',
160     DEVICE: 'device',
161     PRIVET: 'privet'
162   };
163
164   /**
165    * Enumeration of the connection statuses of printer destinations.
166    * @enum {string}
167    */
168   Destination.ConnectionStatus = {
169     DORMANT: 'DORMANT',
170     OFFLINE: 'OFFLINE',
171     ONLINE: 'ONLINE',
172     UNKNOWN: 'UNKNOWN',
173     UNREGISTERED: 'UNREGISTERED'
174   };
175
176   /**
177    * Enumeration of relative icon URLs for various types of destinations.
178    * @enum {string}
179    * @private
180    */
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'
191   };
192
193   Destination.prototype = {
194     /** @return {string} ID of the destination. */
195     get id() {
196       return this.id_;
197     },
198
199     /** @return {!print_preview.Destination.Type} Type of the destination. */
200     get type() {
201       return this.type_;
202     },
203
204     /**
205      * @return {!print_preview.Destination.Origin} Origin of the destination.
206      */
207     get origin() {
208       return this.origin_;
209     },
210
211     /** @return {string} Display name of the destination. */
212     get displayName() {
213       return this.displayName_;
214     },
215
216     /** @return {boolean} Whether the destination has been used recently. */
217     get isRecent() {
218       return this.isRecent_;
219     },
220
221     /**
222      * @param {boolean} isRecent Whether the destination has been used recently.
223      */
224     set isRecent(isRecent) {
225       this.isRecent_ = isRecent;
226     },
227
228     /**
229      * @return {boolean} Whether the user owns the destination. Only applies to
230      *     cloud-based destinations.
231      */
232     get isOwned() {
233       return this.isOwned_;
234     },
235
236     /** @return {boolean} Whether the destination is local or cloud-based. */
237     get isLocal() {
238       return this.origin_ == Destination.Origin.LOCAL ||
239              (this.origin_ == Destination.Origin.PRIVET &&
240               this.connectionStatus_ !=
241               Destination.ConnectionStatus.UNREGISTERED);
242     },
243
244     /** @return {boolean} Whether the destination is a privet local printer */
245     get isPrivet() {
246       return this.origin_ == Destination.Origin.PRIVET;
247     },
248
249     /**
250      * @return {string} The location of the destination, or an empty string if
251      *     the location is unknown.
252      */
253     get location() {
254       if (this.location_ == null) {
255         this.location_ = '';
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) || '';
260             break;
261           }
262         }
263       }
264       return this.location_;
265     },
266
267     /** @return {!Array.<string>} Tags associated with the destination. */
268     get tags() {
269       return this.tags_.slice(0);
270     },
271
272     /** @return {string} Cloud ID associated with the destination */
273     get cloudID() {
274       return this.cloudID_;
275     },
276
277     /** @return {print_preview.Cdd} Print capabilities of the destination. */
278     get capabilities() {
279       return this.capabilities_;
280     },
281
282     /**
283      * @param {!print_preview.Cdd} capabilities Print capabilities of the
284      *     destination.
285      */
286     set capabilities(capabilities) {
287       this.capabilities_ = capabilities;
288     },
289
290     /**
291      * @return {!print_preview.Destination.ConnectionStatus} Connection status
292      *     of the print destination.
293      */
294     get connectionStatus() {
295       return this.connectionStatus_;
296     },
297
298     /**
299      * @param {!print_preview.Destination.ConnectionStatus} status Connection
300      *     status of the print destination.
301      */
302     set connectionStatus(status) {
303       this.connectionStatus_ = status;
304     },
305
306     /** @return {boolean} Whether the destination is considered offline. */
307     get isOffline() {
308       return arrayContains([print_preview.Destination.ConnectionStatus.OFFLINE,
309                             print_preview.Destination.ConnectionStatus.DORMANT],
310                             this.connectionStatus_);
311     },
312
313     /** @return {string} Human readable status for offline destination. */
314     get offlineStatusText() {
315       if (!this.isOffline) {
316         return '';
317       }
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';
326       } else {
327         offlineMessageId = 'offline';
328       }
329       return localStrings.getString(offlineMessageId);
330     },
331
332     /**
333      * @return {number} Number of milliseconds since the epoch when the printer
334      *     was last accessed.
335      */
336     get lastAccessTime() {
337       return this.lastAccessTime_;
338     },
339
340     /** @return {string} Relative URL of the destination's icon. */
341     get iconUrl() {
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;
356       } else {
357         return Destination.IconUrl_.CLOUD_SHARED;
358       }
359     },
360
361     /**
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
364      *     apply.
365      */
366     get isTosAccepted() {
367       return this.isTosAccepted_;
368     },
369
370     /**
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
373      *     apply.
374      */
375     set isTosAccepted(isTosAccepted) {
376       this.isTosAccepted_ = isTosAccepted;
377     },
378
379     /**
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.
384      */
385     matches: function(query) {
386       return this.displayName_.toLowerCase().indexOf(
387           query.toLowerCase().trim()) != -1;
388     }
389   };
390
391   /**
392    * The CDD (Cloud Device Description) describes the capabilities of a print
393    * destination.
394    *
395    * @typedef {{
396    *   version: string,
397    *   printer: {
398    *     vendor_capability: !Array.<{Object}>,
399    *     collate: {default: boolean=}=,
400    *     color: {
401    *       option: !Array.<{
402    *         type: string=,
403    *         vendor_id: string=,
404    *         custom_display_name: string=,
405    *         is_default: boolean=
406    *       }>
407    *     }=,
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=}>
412    *     }=
413    *   }
414    * }}
415    */
416   var Cdd = Object;
417
418   // Export
419   return {
420     Destination: Destination,
421     Cdd: Cdd
422   };
423 });