blob: 565fc785a12f53aadccff1355d9110d2942ee39b [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010011#include "base/strings/string_number_conversions.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010012#include "base/strings/utf_string_conversions.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010013#include "base/time/time.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000014#include "base/values.h"
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +000015#include "printing/page_size_margins.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000016#include "printing/print_job_constants.h"
17#include "printing/print_settings.h"
18#include "printing/units.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000019
Torne (Richard Coles)58218062012-11-14 11:43:16 +000020namespace printing {
21
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +000022bool PrintSettingsInitializer::InitSettings(
23 const base::DictionaryValue& job_settings,
24 const PageRanges& ranges,
25 PrintSettings* settings) {
26 bool display_header_footer = false;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000027 if (!job_settings.GetBoolean(kSettingHeaderFooterEnabled,
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +000028 &display_header_footer)) {
29 return false;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000030 }
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +000031 settings->set_display_header_footer(display_header_footer);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000032
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +000033 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);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000042 }
43
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +000044 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;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000104}
105
106} // namespace printing