summaryrefslogtreecommitdiffstats
path: root/chrome/service/cloud_print/cdd_conversion_win.cc
blob: 2aad275fca25ad06ee3d1bec95908979a5ee9d12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/service/cloud_print/cdd_conversion_win.h"

#include <stddef.h>

#include "base/memory/free_deleter.h"
#include "base/strings/string_number_conversions.h"
#include "components/cloud_devices/common/printer_description.h"
#include "printing/backend/win_helper.h"

namespace cloud_print {

bool IsValidCjt(const std::string& print_ticket_data) {
  cloud_devices::CloudDeviceDescription description;
  return description.InitFromString(print_ticket_data);
}

scoped_ptr<DEVMODE, base::FreeDeleter> CjtToDevMode(
    const base::string16& printer_name,
    const std::string& print_ticket) {
  scoped_ptr<DEVMODE, base::FreeDeleter> dev_mode;

  cloud_devices::CloudDeviceDescription description;
  if (!description.InitFromString(print_ticket))
    return dev_mode;

  using namespace cloud_devices::printer;
  printing::ScopedPrinterHandle printer;
  if (!printer.OpenPrinter(printer_name.c_str()))
    return dev_mode;

  {
    ColorTicketItem color;
    if (color.LoadFrom(description)) {
      bool is_color = color.value().type == STANDARD_COLOR;
      dev_mode = printing::CreateDevModeWithColor(printer.Get(), printer_name,
                                                  is_color);
    } else {
      dev_mode = printing::CreateDevMode(printer.Get(), NULL);
    }
  }

  if (!dev_mode)
    return dev_mode;

  ColorTicketItem color;
  DuplexTicketItem duplex;
  OrientationTicketItem orientation;
  MarginsTicketItem margins;
  DpiTicketItem dpi;
  FitToPageTicketItem fit_to_page;
  MediaTicketItem media;
  CopiesTicketItem copies;
  PageRangeTicketItem page_range;
  CollateTicketItem collate;
  ReverseTicketItem reverse;

  if (orientation.LoadFrom(description)) {
    dev_mode->dmFields |= DM_ORIENTATION;
    if (orientation.value() == LANDSCAPE) {
      dev_mode->dmOrientation = DMORIENT_LANDSCAPE;
    } else {
      dev_mode->dmOrientation = DMORIENT_PORTRAIT;
    }
  }

  if (color.LoadFrom(description)) {
    dev_mode->dmFields |= DM_COLOR;
    if (color.value().type == STANDARD_MONOCHROME) {
      dev_mode->dmColor = DMCOLOR_MONOCHROME;
    } else if (color.value().type == STANDARD_COLOR) {
      dev_mode->dmColor = DMCOLOR_COLOR;
    } else {
      NOTREACHED();
    }
  }

  if (duplex.LoadFrom(description)) {
    dev_mode->dmFields |= DM_DUPLEX;
    if (duplex.value() == NO_DUPLEX) {
      dev_mode->dmDuplex = DMDUP_SIMPLEX;
    } else if (duplex.value() == LONG_EDGE) {
      dev_mode->dmDuplex = DMDUP_VERTICAL;
    } else if (duplex.value() == SHORT_EDGE) {
      dev_mode->dmDuplex = DMDUP_HORIZONTAL;
    } else {
      NOTREACHED();
    }
  }

  if (copies.LoadFrom(description)) {
    dev_mode->dmFields |= DM_COPIES;
    dev_mode->dmCopies = copies.value();
  }

  if (dpi.LoadFrom(description)) {
    if (dpi.value().horizontal > 0) {
      dev_mode->dmFields |= DM_PRINTQUALITY;
      dev_mode->dmPrintQuality = dpi.value().horizontal;
    }
    if (dpi.value().vertical > 0) {
      dev_mode->dmFields |= DM_YRESOLUTION;
      dev_mode->dmYResolution = dpi.value().vertical;
    }
  }

  if (collate.LoadFrom(description)) {
    dev_mode->dmFields |= DM_COLLATE;
    dev_mode->dmCollate = (collate.value() ? DMCOLLATE_TRUE : DMCOLLATE_FALSE);
  }

  if (media.LoadFrom(description)) {
    static const size_t kFromUm = 100;  // Windows uses 0.1mm.
    int width = media.value().width_um / kFromUm;
    int height = media.value().height_um / kFromUm;
    unsigned id = 0;
    if (base::StringToUint(media.value().vendor_id, &id) && id) {
      dev_mode->dmFields |= DM_PAPERSIZE;
      dev_mode->dmPaperSize = static_cast<short>(id);
    } else if (width > 0 && height > 0) {
      dev_mode->dmFields |= DM_PAPERWIDTH;
      dev_mode->dmPaperWidth = width;
      dev_mode->dmFields |= DM_PAPERLENGTH;
      dev_mode->dmPaperLength = height;
    }
  }

  return printing::CreateDevMode(printer.Get(), dev_mode.get());
}

}  // namespace cloud_print