Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / print_preview / data / local_parsers.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   /** Namespace that contains a method to parse local print destinations. */
9   function LocalDestinationParser() {};
10
11   /**
12    * Parses a local print destination.
13    * @param {!Object} destinationInfo Information describing a local print
14    *     destination.
15    * @return {!print_preview.Destination} Parsed local print destination.
16    */
17   LocalDestinationParser.parse = function(destinationInfo) {
18     var options = {'description': destinationInfo.printerDescription};
19     if (destinationInfo.printerOptions) {
20       // Convert options into cloud print tags format.
21       options.tags = Object.keys(destinationInfo.printerOptions).map(
22           function(key) {return '__cp__' + key + '=' + this[key];},
23           destinationInfo.printerOptions);
24     }
25     return new print_preview.Destination(
26         destinationInfo.deviceName,
27         print_preview.Destination.Type.LOCAL,
28         print_preview.Destination.Origin.LOCAL,
29         destinationInfo.printerName,
30         false /*isRecent*/,
31         print_preview.Destination.ConnectionStatus.ONLINE,
32         options);
33   };
34
35   /** Namespace that contains a method to parse local print capabilities. */
36   function LocalCapabilitiesParser() {};
37
38   /**
39    * Parses local print capabilities.
40    * @param {!Object} settingsInfo Object that describes local print
41    *     capabilities.
42    * @return {!print_preview.Cdd} Parsed local print capabilities.
43    */
44   LocalCapabilitiesParser.parse = function(settingsInfo) {
45     var cdd = {
46       version: '1.0',
47       printer: {
48         collate: {'default': true}
49       }
50     };
51
52     if (!settingsInfo['disableColorOption']) {
53       cdd.printer.color = {
54         option: [
55           {
56             type: 'STANDARD_COLOR',
57             is_default: !!settingsInfo['setColorAsDefault']
58           },
59           {
60             type: 'STANDARD_MONOCHROME',
61             is_default: !settingsInfo['setColorAsDefault']
62           }
63         ]
64       };
65     }
66
67     if (!settingsInfo['disableCopiesOption']) {
68       cdd.printer.copies = {'default': 1};
69     }
70
71     if (settingsInfo['printerDefaultDuplexValue'] !=
72         print_preview.NativeLayer.DuplexMode.UNKNOWN_DUPLEX_MODE) {
73       cdd.printer.duplex = {
74         option: [
75           {type: 'NO_DUPLEX', is_default: !settingsInfo['setDuplexAsDefault']},
76           {type: 'LONG_EDGE', is_default: !!settingsInfo['setDuplexAsDefault']}
77         ]
78       };
79     }
80
81     if (!settingsInfo['disableLandscapeOption']) {
82       cdd.printer.page_orientation = {
83         option: [
84           {type: 'PORTRAIT', is_default: true},
85           {type: 'LANDSCAPE'}
86         ]
87       };
88     }
89
90     return cdd;
91   };
92
93   function PrivetDestinationParser() {}
94
95   /**
96    * Parses a privet destination as one or more local printers.
97    * @param {!Object} destinationInfo Object that describes a privet printer.
98    * @return {!Array.<!print_preview.Destination>} Parsed destination info.
99    */
100   PrivetDestinationParser.parse = function(destinationInfo) {
101     var returnedPrinters = [];
102
103     if (destinationInfo.hasLocalPrinting) {
104        returnedPrinters.push(new print_preview.Destination(
105            destinationInfo.serviceName,
106            print_preview.Destination.Type.LOCAL,
107            print_preview.Destination.Origin.PRIVET,
108            destinationInfo.name,
109            false /*isRecent*/,
110            print_preview.Destination.ConnectionStatus.ONLINE,
111            { cloudID: destinationInfo.cloudID }));
112     }
113
114     if (destinationInfo.isUnregistered) {
115       returnedPrinters.push(new print_preview.Destination(
116           destinationInfo.serviceName,
117           print_preview.Destination.Type.GOOGLE,
118           print_preview.Destination.Origin.PRIVET,
119           destinationInfo.name,
120           false /*isRecent*/,
121           print_preview.Destination.ConnectionStatus.UNREGISTERED));
122     }
123
124     return returnedPrinters;
125   };
126
127   // Export
128   return {
129     LocalCapabilitiesParser: LocalCapabilitiesParser,
130     LocalDestinationParser: LocalDestinationParser,
131     PrivetDestinationParser: PrivetDestinationParser
132   };
133 });