OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "printing/print_settings_initializer.h" | 5 #include "printing/print_settings_conversion.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
15 #include "printing/page_size_margins.h" | 15 #include "printing/page_size_margins.h" |
16 #include "printing/print_job_constants.h" | 16 #include "printing/print_job_constants.h" |
17 #include "printing/print_settings.h" | 17 #include "printing/print_settings.h" |
18 #include "printing/units.h" | 18 #include "printing/units.h" |
19 | 19 |
20 namespace printing { | 20 namespace printing { |
21 | 21 |
22 bool PrintSettingsInitializer::InitSettings( | 22 namespace { |
23 const base::DictionaryValue& job_settings, | 23 |
24 const PageRanges& ranges, | 24 void GetCustomMarginsFromJobSettings(const base::DictionaryValue& settings, |
25 PrintSettings* settings) { | 25 PageSizeMargins* page_size_margins) { |
| 26 const base::DictionaryValue* custom_margins; |
| 27 if (!settings.GetDictionary(kSettingMarginsCustom, &custom_margins) || |
| 28 !custom_margins->GetDouble(kSettingMarginTop, |
| 29 &page_size_margins->margin_top) || |
| 30 !custom_margins->GetDouble(kSettingMarginBottom, |
| 31 &page_size_margins->margin_bottom) || |
| 32 !custom_margins->GetDouble(kSettingMarginLeft, |
| 33 &page_size_margins->margin_left) || |
| 34 !custom_margins->GetDouble(kSettingMarginRight, |
| 35 &page_size_margins->margin_right)) { |
| 36 NOTREACHED(); |
| 37 } |
| 38 } |
| 39 |
| 40 void SetMarginsToJobSettings(const std::string& json_path, |
| 41 const PageMargins& margins, |
| 42 base::DictionaryValue* job_settings) { |
| 43 base::DictionaryValue* dict = new base::DictionaryValue; |
| 44 job_settings->Set(json_path, dict); |
| 45 dict->SetInteger(kSettingMarginTop, margins.top); |
| 46 dict->SetInteger(kSettingMarginBottom, margins.bottom); |
| 47 dict->SetInteger(kSettingMarginLeft, margins.left); |
| 48 dict->SetInteger(kSettingMarginRight, margins.right); |
| 49 } |
| 50 |
| 51 void SetSizeToJobSettings(const std::string& json_path, |
| 52 const gfx::Size& size, |
| 53 base::DictionaryValue* job_settings) { |
| 54 base::DictionaryValue* dict = new base::DictionaryValue; |
| 55 job_settings->Set(json_path, dict); |
| 56 dict->SetInteger("width", size.width()); |
| 57 dict->SetInteger("height", size.height()); |
| 58 } |
| 59 |
| 60 void SetRectToJobSettings(const std::string& json_path, |
| 61 const gfx::Rect& rect, |
| 62 base::DictionaryValue* job_settings) { |
| 63 base::DictionaryValue* dict = new base::DictionaryValue; |
| 64 job_settings->Set(json_path, dict); |
| 65 dict->SetInteger("x", rect.x()); |
| 66 dict->SetInteger("y", rect.y()); |
| 67 dict->SetInteger("width", rect.width()); |
| 68 dict->SetInteger("height", rect.height()); |
| 69 } |
| 70 |
| 71 } // namespace |
| 72 |
| 73 bool PrintSettingsFromJobSettings(const base::DictionaryValue& job_settings, |
| 74 PrintSettings* settings) { |
26 bool display_header_footer = false; | 75 bool display_header_footer = false; |
27 if (!job_settings.GetBoolean(kSettingHeaderFooterEnabled, | 76 if (!job_settings.GetBoolean(kSettingHeaderFooterEnabled, |
28 &display_header_footer)) { | 77 &display_header_footer)) { |
29 return false; | 78 return false; |
30 } | 79 } |
31 settings->set_display_header_footer(display_header_footer); | 80 settings->set_display_header_footer(display_header_footer); |
32 | 81 |
33 if (settings->display_header_footer()) { | 82 if (settings->display_header_footer()) { |
34 base::string16 title; | 83 base::string16 title; |
35 base::string16 url; | 84 base::string16 url; |
(...skipping 10 matching lines...) Expand all Loading... |
46 if (!job_settings.GetBoolean(kSettingShouldPrintBackgrounds, &backgrounds) || | 95 if (!job_settings.GetBoolean(kSettingShouldPrintBackgrounds, &backgrounds) || |
47 !job_settings.GetBoolean(kSettingShouldPrintSelectionOnly, | 96 !job_settings.GetBoolean(kSettingShouldPrintSelectionOnly, |
48 &selection_only)) { | 97 &selection_only)) { |
49 return false; | 98 return false; |
50 } | 99 } |
51 settings->set_should_print_backgrounds(backgrounds); | 100 settings->set_should_print_backgrounds(backgrounds); |
52 settings->set_selection_only(selection_only); | 101 settings->set_selection_only(selection_only); |
53 | 102 |
54 int margin_type = DEFAULT_MARGINS; | 103 int margin_type = DEFAULT_MARGINS; |
55 if (!job_settings.GetInteger(kSettingMarginsType, &margin_type) || | 104 if (!job_settings.GetInteger(kSettingMarginsType, &margin_type) || |
56 (margin_type != DEFAULT_MARGINS && | 105 (margin_type != DEFAULT_MARGINS && margin_type != NO_MARGINS && |
57 margin_type != NO_MARGINS && | |
58 margin_type != CUSTOM_MARGINS && | 106 margin_type != CUSTOM_MARGINS && |
59 margin_type != PRINTABLE_AREA_MARGINS)) { | 107 margin_type != PRINTABLE_AREA_MARGINS)) { |
60 margin_type = DEFAULT_MARGINS; | 108 margin_type = DEFAULT_MARGINS; |
61 } | 109 } |
62 settings->set_margin_type(static_cast<MarginType>(margin_type)); | 110 settings->set_margin_type(static_cast<MarginType>(margin_type)); |
63 | 111 |
64 if (margin_type == CUSTOM_MARGINS) { | 112 if (margin_type == CUSTOM_MARGINS) { |
65 PageSizeMargins page_size_margins; | 113 PageSizeMargins page_size_margins; |
66 GetCustomMarginsFromJobSettings(job_settings, &page_size_margins); | 114 GetCustomMarginsFromJobSettings(job_settings, &page_size_margins); |
67 | 115 |
68 PageMargins margins_in_points; | 116 PageMargins margins_in_points; |
69 margins_in_points.Clear(); | 117 margins_in_points.Clear(); |
70 margins_in_points.top = page_size_margins.margin_top; | 118 margins_in_points.top = page_size_margins.margin_top; |
71 margins_in_points.bottom = page_size_margins.margin_bottom; | 119 margins_in_points.bottom = page_size_margins.margin_bottom; |
72 margins_in_points.left = page_size_margins.margin_left; | 120 margins_in_points.left = page_size_margins.margin_left; |
73 margins_in_points.right = page_size_margins.margin_right; | 121 margins_in_points.right = page_size_margins.margin_right; |
74 | 122 |
75 settings->SetCustomMargins(margins_in_points); | 123 settings->SetCustomMargins(margins_in_points); |
76 } | 124 } |
77 | 125 |
78 settings->set_ranges(ranges); | 126 PageRanges new_ranges; |
| 127 const base::ListValue* page_range_array = NULL; |
| 128 if (job_settings.GetList(kSettingPageRange, &page_range_array)) { |
| 129 for (size_t index = 0; index < page_range_array->GetSize(); ++index) { |
| 130 const base::DictionaryValue* dict; |
| 131 if (!page_range_array->GetDictionary(index, &dict)) |
| 132 continue; |
| 133 |
| 134 PageRange range; |
| 135 if (!dict->GetInteger(kSettingPageRangeFrom, &range.from) || |
| 136 !dict->GetInteger(kSettingPageRangeTo, &range.to)) { |
| 137 continue; |
| 138 } |
| 139 |
| 140 // Page numbers are 1-based in the dictionary. |
| 141 // Page numbers are 0-based for the printing context. |
| 142 range.from--; |
| 143 range.to--; |
| 144 new_ranges.push_back(range); |
| 145 } |
| 146 } |
| 147 settings->set_ranges(new_ranges); |
79 | 148 |
80 int color = 0; | 149 int color = 0; |
81 bool landscape = false; | 150 bool landscape = false; |
82 int duplex_mode = 0; | 151 int duplex_mode = 0; |
83 base::string16 device_name; | 152 base::string16 device_name; |
84 bool collate = false; | 153 bool collate = false; |
85 int copies = 1; | 154 int copies = 1; |
86 | 155 |
87 if (!job_settings.GetBoolean(kSettingCollate, &collate) || | 156 if (!job_settings.GetBoolean(kSettingCollate, &collate) || |
88 !job_settings.GetInteger(kSettingCopies, &copies) || | 157 !job_settings.GetInteger(kSettingCopies, &copies) || |
89 !job_settings.GetInteger(kSettingColor, &color) || | 158 !job_settings.GetInteger(kSettingColor, &color) || |
90 !job_settings.GetInteger(kSettingDuplexMode, &duplex_mode) || | 159 !job_settings.GetInteger(kSettingDuplexMode, &duplex_mode) || |
91 !job_settings.GetBoolean(kSettingLandscape, &landscape) || | 160 !job_settings.GetBoolean(kSettingLandscape, &landscape) || |
92 !job_settings.GetString(kSettingDeviceName, &device_name)) { | 161 !job_settings.GetString(kSettingDeviceName, &device_name)) { |
93 return false; | 162 return false; |
94 } | 163 } |
95 | 164 |
96 settings->set_collate(collate); | 165 settings->set_collate(collate); |
97 settings->set_copies(copies); | 166 settings->set_copies(copies); |
98 settings->SetOrientation(landscape); | 167 settings->SetOrientation(landscape); |
99 settings->set_device_name(device_name); | 168 settings->set_device_name(device_name); |
100 settings->set_duplex_mode(static_cast<DuplexMode>(duplex_mode)); | 169 settings->set_duplex_mode(static_cast<DuplexMode>(duplex_mode)); |
101 settings->set_color(static_cast<ColorModel>(color)); | 170 settings->set_color(static_cast<ColorModel>(color)); |
102 | 171 |
103 return true; | 172 return true; |
104 } | 173 } |
105 | 174 |
| 175 void PrintSettingsToJobSettingsDebug(const PrintSettings& settings, |
| 176 base::DictionaryValue* job_settings) { |
| 177 job_settings->SetBoolean(kSettingHeaderFooterEnabled, |
| 178 settings.display_header_footer()); |
| 179 job_settings->SetString(kSettingHeaderFooterTitle, settings.title()); |
| 180 job_settings->SetString(kSettingHeaderFooterURL, settings.url()); |
| 181 job_settings->SetBoolean(kSettingShouldPrintBackgrounds, |
| 182 settings.should_print_backgrounds()); |
| 183 job_settings->SetBoolean(kSettingShouldPrintSelectionOnly, |
| 184 settings.selection_only()); |
| 185 job_settings->SetInteger(kSettingMarginsType, settings.margin_type()); |
| 186 if (!settings.ranges().empty()) { |
| 187 base::ListValue* page_range_array = new base::ListValue; |
| 188 job_settings->Set(kSettingPageRange, page_range_array); |
| 189 for (size_t i = 0; i < settings.ranges().size(); ++i) { |
| 190 base::DictionaryValue* dict = new base::DictionaryValue; |
| 191 page_range_array->Append(dict); |
| 192 dict->SetInteger(kSettingPageRangeFrom, settings.ranges()[i].from + 1); |
| 193 dict->SetInteger(kSettingPageRangeTo, settings.ranges()[i].to + 1); |
| 194 } |
| 195 } |
| 196 |
| 197 job_settings->SetBoolean(kSettingCollate, settings.collate()); |
| 198 job_settings->SetInteger(kSettingCopies, settings.copies()); |
| 199 job_settings->SetInteger(kSettingColor, settings.color()); |
| 200 job_settings->SetInteger(kSettingDuplexMode, settings.duplex_mode()); |
| 201 job_settings->SetBoolean(kSettingLandscape, settings.landscape()); |
| 202 job_settings->SetString(kSettingDeviceName, settings.device_name()); |
| 203 |
| 204 // Following values are not read form JSON by InitSettings, so do not have |
| 205 // common public constants. So just serialize in "debug" section. |
| 206 base::DictionaryValue* debug = new base::DictionaryValue; |
| 207 job_settings->Set("debug", debug); |
| 208 debug->SetDouble("minShrink", settings.min_shrink()); |
| 209 debug->SetDouble("maxShrink", settings.max_shrink()); |
| 210 debug->SetInteger("desiredDpi", settings.desired_dpi()); |
| 211 debug->SetInteger("dpi", settings.dpi()); |
| 212 debug->SetInteger("deviceUnitsPerInch", settings.device_units_per_inch()); |
| 213 debug->SetBoolean("support_alpha_blend", settings.should_print_backgrounds()); |
| 214 |
| 215 SetMarginsToJobSettings("requested_custom_margins_in_points", |
| 216 settings.requested_custom_margins_in_points(), |
| 217 debug); |
| 218 const PageSetup& page_setup = settings.page_setup_device_units(); |
| 219 SetMarginsToJobSettings( |
| 220 "effective_margins", page_setup.effective_margins(), debug); |
| 221 SetSizeToJobSettings("physical_size", page_setup.physical_size(), debug); |
| 222 SetRectToJobSettings("overlay_area", page_setup.overlay_area(), debug); |
| 223 SetRectToJobSettings("content_area", page_setup.content_area(), debug); |
| 224 SetRectToJobSettings("printable_area", page_setup.printable_area(), debug); |
| 225 } |
| 226 |
106 } // namespace printing | 227 } // namespace printing |
OLD | NEW |