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.
5 #include "chrome/service/cloud_print/cdd_conversion_win.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "components/cloud_devices/common/printer_description.h"
9 #include "printing/backend/win_helper.h"
11 namespace cloud_print
{
13 bool IsValidCjt(const std::string
& print_ticket_data
) {
14 cloud_devices::CloudDeviceDescription description
;
15 return description
.InitFromString(print_ticket_data
);
18 scoped_ptr
<DEVMODE
, base::FreeDeleter
> CjtToDevMode(
19 const base::string16
& printer_name
,
20 const std::string
& print_ticket
) {
21 scoped_ptr
<DEVMODE
, base::FreeDeleter
> dev_mode
;
23 cloud_devices::CloudDeviceDescription description
;
24 if (!description
.InitFromString(print_ticket
))
25 return dev_mode
.Pass();
27 using namespace cloud_devices::printer
;
28 printing::ScopedPrinterHandle printer
;
29 if (!printer
.OpenPrinter(printer_name
.c_str()))
30 return dev_mode
.Pass();
33 ColorTicketItem color
;
34 if (color
.LoadFrom(description
)) {
35 bool is_color
= color
.value().type
== STANDARD_COLOR
;
36 dev_mode
= printing::CreateDevModeWithColor(printer
.Get(), printer_name
,
39 dev_mode
= printing::CreateDevMode(printer
.Get(), NULL
);
44 return dev_mode
.Pass();
46 ColorTicketItem color
;
47 DuplexTicketItem duplex
;
48 OrientationTicketItem orientation
;
49 MarginsTicketItem margins
;
51 FitToPageTicketItem fit_to_page
;
52 MediaTicketItem media
;
53 CopiesTicketItem copies
;
54 PageRangeTicketItem page_range
;
55 CollateTicketItem collate
;
56 ReverseTicketItem reverse
;
58 if (orientation
.LoadFrom(description
)) {
59 dev_mode
->dmFields
|= DM_ORIENTATION
;
60 if (orientation
.value() == LANDSCAPE
) {
61 dev_mode
->dmOrientation
= DMORIENT_LANDSCAPE
;
63 dev_mode
->dmOrientation
= DMORIENT_PORTRAIT
;
67 if (color
.LoadFrom(description
)) {
68 dev_mode
->dmFields
|= DM_COLOR
;
69 if (color
.value().type
== STANDARD_MONOCHROME
) {
70 dev_mode
->dmColor
= DMCOLOR_MONOCHROME
;
71 } else if (color
.value().type
== STANDARD_COLOR
) {
72 dev_mode
->dmColor
= DMCOLOR_COLOR
;
78 if (duplex
.LoadFrom(description
)) {
79 dev_mode
->dmFields
|= DM_DUPLEX
;
80 if (duplex
.value() == NO_DUPLEX
) {
81 dev_mode
->dmDuplex
= DMDUP_SIMPLEX
;
82 } else if (duplex
.value() == LONG_EDGE
) {
83 dev_mode
->dmDuplex
= DMDUP_VERTICAL
;
84 } else if (duplex
.value() == SHORT_EDGE
) {
85 dev_mode
->dmDuplex
= DMDUP_HORIZONTAL
;
91 if (copies
.LoadFrom(description
)) {
92 dev_mode
->dmFields
|= DM_COPIES
;
93 dev_mode
->dmCopies
= copies
.value();
96 if (dpi
.LoadFrom(description
)) {
97 if (dpi
.value().horizontal
> 0) {
98 dev_mode
->dmFields
|= DM_PRINTQUALITY
;
99 dev_mode
->dmPrintQuality
= dpi
.value().horizontal
;
101 if (dpi
.value().vertical
> 0) {
102 dev_mode
->dmFields
|= DM_YRESOLUTION
;
103 dev_mode
->dmYResolution
= dpi
.value().vertical
;
107 if (collate
.LoadFrom(description
)) {
108 dev_mode
->dmFields
|= DM_COLLATE
;
109 dev_mode
->dmCollate
= (collate
.value() ? DMCOLLATE_TRUE
: DMCOLLATE_FALSE
);
112 if (media
.LoadFrom(description
)) {
113 static const size_t kFromUm
= 100; // Windows uses 0.1mm.
114 int width
= media
.value().width_um
/ kFromUm
;
115 int height
= media
.value().height_um
/ kFromUm
;
117 if (base::StringToUint(media
.value().vendor_id
, &id
) && id
) {
118 dev_mode
->dmFields
|= DM_PAPERSIZE
;
119 dev_mode
->dmPaperSize
= static_cast<short>(id
);
120 } else if (width
> 0 && height
> 0) {
121 dev_mode
->dmFields
|= DM_PAPERWIDTH
;
122 dev_mode
->dmPaperWidth
= width
;
123 dev_mode
->dmFields
|= DM_PAPERLENGTH
;
124 dev_mode
->dmPaperLength
= height
;
128 return printing::CreateDevMode(printer
.Get(), dev_mode
.get());
131 } // namespace cloud_print