1 // Copyright (c) 2011 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 "printing/print_settings_initializer.h"
6
7 #include <algorithm>
8 #include <cmath>
9 #include <string>
10
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/time/time.h"
14 #include "base/values.h"
15 #include "printing/page_size_margins.h"
16 #include "printing/print_job_constants.h"
17 #include "printing/print_settings.h"
18 #include "printing/units.h"
19
20 namespace printing {
21
InitSettings(const base::DictionaryValue & job_settings,const PageRanges & ranges,PrintSettings * settings)22 bool PrintSettingsInitializer::InitSettings(
23 const base::DictionaryValue& job_settings,
24 const PageRanges& ranges,
25 PrintSettings* settings) {
26 bool display_header_footer = false;
27 if (!job_settings.GetBoolean(kSettingHeaderFooterEnabled,
28 &display_header_footer)) {
29 return false;
30 }
31 settings->set_display_header_footer(display_header_footer);
32
33 if (settings->display_header_footer()) {
34 base::string16 title;
35 base::string16 url;
36 if (!job_settings.GetString(kSettingHeaderFooterTitle, &title) ||
37 !job_settings.GetString(kSettingHeaderFooterURL, &url)) {
38 return false;
39 }
40 settings->set_title(title);
41 settings->set_url(url);
42 }
43
44 bool backgrounds = false;
45 bool selection_only = false;
46 if (!job_settings.GetBoolean(kSettingShouldPrintBackgrounds, &backgrounds) ||
47 !job_settings.GetBoolean(kSettingShouldPrintSelectionOnly,
48 &selection_only)) {
49 return false;
50 }
51 settings->set_should_print_backgrounds(backgrounds);
52 settings->set_selection_only(selection_only);
53
54 int margin_type = DEFAULT_MARGINS;
55 if (!job_settings.GetInteger(kSettingMarginsType, &margin_type) ||
56 (margin_type != DEFAULT_MARGINS &&
57 margin_type != NO_MARGINS &&
58 margin_type != CUSTOM_MARGINS &&
59 margin_type != PRINTABLE_AREA_MARGINS)) {
60 margin_type = DEFAULT_MARGINS;
61 }
62 settings->set_margin_type(static_cast<MarginType>(margin_type));
63
64 if (margin_type == CUSTOM_MARGINS) {
65 PageSizeMargins page_size_margins;
66 GetCustomMarginsFromJobSettings(job_settings, &page_size_margins);
67
68 PageMargins margins_in_points;
69 margins_in_points.Clear();
70 margins_in_points.top = page_size_margins.margin_top;
71 margins_in_points.bottom = page_size_margins.margin_bottom;
72 margins_in_points.left = page_size_margins.margin_left;
73 margins_in_points.right = page_size_margins.margin_right;
74
75 settings->SetCustomMargins(margins_in_points);
76 }
77
78 settings->set_ranges(ranges);
79
80 int color = 0;
81 bool landscape = false;
82 int duplex_mode = 0;
83 base::string16 device_name;
84 bool collate = false;
85 int copies = 1;
86
87 if (!job_settings.GetBoolean(kSettingCollate, &collate) ||
88 !job_settings.GetInteger(kSettingCopies, &copies) ||
89 !job_settings.GetInteger(kSettingColor, &color) ||
90 !job_settings.GetInteger(kSettingDuplexMode, &duplex_mode) ||
91 !job_settings.GetBoolean(kSettingLandscape, &landscape) ||
92 !job_settings.GetString(kSettingDeviceName, &device_name)) {
93 return false;
94 }
95
96 settings->set_collate(collate);
97 settings->set_copies(copies);
98 settings->SetOrientation(landscape);
99 settings->set_device_name(device_name);
100 settings->set_duplex_mode(static_cast<DuplexMode>(duplex_mode));
101 settings->set_color(static_cast<ColorModel>(color));
102
103 return true;
104 }
105
106 } // namespace printing
107