1 // Copyright 2014 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 #include "chrome/common/cloud_print/cloud_print_cdd_conversion.h"
6
7 #include "base/strings/string_number_conversions.h"
8 #include "components/cloud_devices/common/printer_description.h"
9 #include "printing/backend/print_backend.h"
10
11 namespace cloud_print {
12
PrinterSemanticCapsAndDefaultsToCdd(const printing::PrinterSemanticCapsAndDefaults & semantic_info)13 scoped_ptr<base::DictionaryValue> PrinterSemanticCapsAndDefaultsToCdd(
14 const printing::PrinterSemanticCapsAndDefaults& semantic_info) {
15 using namespace cloud_devices::printer;
16 cloud_devices::CloudDeviceDescription description;
17
18 ContentTypesCapability content_types;
19 content_types.AddOption("application/pdf");
20 content_types.SaveTo(&description);
21
22 if (semantic_info.collate_capable) {
23 CollateCapability collate;
24 collate.set_default_value(semantic_info.collate_default);
25 collate.SaveTo(&description);
26 }
27
28 if (semantic_info.copies_capable) {
29 CopiesCapability copies;
30 copies.SaveTo(&description);
31 }
32
33 if (semantic_info.duplex_capable) {
34 DuplexCapability duplex;
35 duplex.AddDefaultOption(
36 NO_DUPLEX, semantic_info.duplex_default == printing::SIMPLEX);
37 duplex.AddDefaultOption(
38 LONG_EDGE, semantic_info.duplex_default == printing::LONG_EDGE);
39 duplex.AddDefaultOption(
40 SHORT_EDGE, semantic_info.duplex_default == printing::SHORT_EDGE);
41 duplex.SaveTo(&description);
42 }
43
44 ColorCapability color;
45 if (semantic_info.color_default || semantic_info.color_changeable) {
46 Color standard_color(STANDARD_COLOR);
47 standard_color.vendor_id = base::IntToString(semantic_info.color_model);
48 color.AddDefaultOption(standard_color, semantic_info.color_default);
49 }
50 if (!semantic_info.color_default || semantic_info.color_changeable) {
51 Color standard_monochrome(STANDARD_MONOCHROME);
52 standard_monochrome.vendor_id = base::IntToString(semantic_info.bw_model);
53 color.AddDefaultOption(standard_monochrome, !semantic_info.color_default);
54 }
55 color.SaveTo(&description);
56
57 if (!semantic_info.papers.empty()) {
58 Media default_media(semantic_info.default_paper.display_name,
59 semantic_info.default_paper.vendor_id,
60 semantic_info.default_paper.size_um.width(),
61 semantic_info.default_paper.size_um.height());
62 default_media.MatchBySize();
63
64 MediaCapability media;
65 bool is_default_set = false;
66 for (size_t i = 0; i < semantic_info.papers.size(); ++i) {
67 gfx::Size paper_size = semantic_info.papers[i].size_um;
68 if (paper_size.width() > paper_size.height())
69 paper_size.SetSize(paper_size.height(), paper_size.width());
70 Media new_media(semantic_info.papers[i].display_name,
71 semantic_info.papers[i].vendor_id,
72 paper_size.width(),
73 paper_size.height());
74 new_media.MatchBySize();
75 if (new_media.IsValid() && !media.Contains(new_media)) {
76 if (!default_media.IsValid())
77 default_media = new_media;
78 media.AddDefaultOption(new_media, new_media == default_media);
79 is_default_set = is_default_set || (new_media == default_media);
80 }
81 }
82 if (!is_default_set && default_media.IsValid())
83 media.AddDefaultOption(default_media, true);
84
85 if (media.IsValid()) {
86 media.SaveTo(&description);
87 } else {
88 NOTREACHED();
89 }
90 }
91
92 if (!semantic_info.dpis.empty()) {
93 DpiCapability dpi;
94 Dpi default_dpi(semantic_info.default_dpi.width(),
95 semantic_info.default_dpi.height());
96 bool is_default_set = false;
97 for (size_t i = 0; i < semantic_info.dpis.size(); ++i) {
98 Dpi new_dpi(semantic_info.dpis[i].width(),
99 semantic_info.dpis[i].height());
100 if (new_dpi.IsValid() && !dpi.Contains(new_dpi)) {
101 if (!default_dpi.IsValid())
102 default_dpi = new_dpi;
103 dpi.AddDefaultOption(new_dpi, new_dpi == default_dpi);
104 is_default_set = is_default_set || (new_dpi == default_dpi);
105 }
106 }
107 if (!is_default_set && default_dpi.IsValid())
108 dpi.AddDefaultOption(default_dpi, true);
109 if (dpi.IsValid()) {
110 dpi.SaveTo(&description);
111 } else {
112 NOTREACHED();
113 }
114 }
115
116 OrientationCapability orientation;
117 orientation.AddDefaultOption(PORTRAIT, true);
118 orientation.AddOption(LANDSCAPE);
119 orientation.AddOption(AUTO_ORIENTATION);
120 orientation.SaveTo(&description);
121
122 return scoped_ptr<base::DictionaryValue>(description.root().DeepCopy());
123 }
124
125 } // namespace cloud_print
126