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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 PageMargins margins_in_points; | 136 PageMargins margins_in_points; |
88 margins_in_points.Clear(); | 137 margins_in_points.Clear(); |
89 margins_in_points.top = page_size_margins.margin_top; | 138 margins_in_points.top = page_size_margins.margin_top; |
90 margins_in_points.bottom = page_size_margins.margin_bottom; | 139 margins_in_points.bottom = page_size_margins.margin_bottom; |
91 margins_in_points.left = page_size_margins.margin_left; | 140 margins_in_points.left = page_size_margins.margin_left; |
92 margins_in_points.right = page_size_margins.margin_right; | 141 margins_in_points.right = page_size_margins.margin_right; |
93 | 142 |
94 settings->SetCustomMargins(margins_in_points); | 143 settings->SetCustomMargins(margins_in_points); |
95 } | 144 } |
96 | 145 |
97 settings->set_ranges(ranges); | 146 PageRanges new_ranges; |
| 147 const base::ListValue* page_range_array = NULL; |
| 148 if (job_settings.GetList(kSettingPageRange, &page_range_array)) { |
| 149 for (size_t index = 0; index < page_range_array->GetSize(); ++index) { |
| 150 const base::DictionaryValue* dict; |
| 151 if (!page_range_array->GetDictionary(index, &dict)) |
| 152 continue; |
| 153 |
| 154 PageRange range; |
| 155 if (!dict->GetInteger(kSettingPageRangeFrom, &range.from) || |
| 156 !dict->GetInteger(kSettingPageRangeTo, &range.to)) { |
| 157 continue; |
| 158 } |
| 159 |
| 160 // Page numbers are 1-based in the dictionary. |
| 161 // Page numbers are 0-based for the printing context. |
| 162 range.from--; |
| 163 range.to--; |
| 164 new_ranges.push_back(range); |
| 165 } |
| 166 } |
| 167 settings->set_ranges(new_ranges); |
98 | 168 |
99 int color = 0; | 169 int color = 0; |
100 bool landscape = false; | 170 bool landscape = false; |
101 int duplex_mode = 0; | 171 int duplex_mode = 0; |
102 base::string16 device_name; | 172 base::string16 device_name; |
103 bool collate = false; | 173 bool collate = false; |
104 int copies = 1; | 174 int copies = 1; |
105 | 175 |
106 if (!job_settings.GetBoolean(kSettingCollate, &collate) || | 176 if (!job_settings.GetBoolean(kSettingCollate, &collate) || |
107 !job_settings.GetInteger(kSettingCopies, &copies) || | 177 !job_settings.GetInteger(kSettingCopies, &copies) || |
108 !job_settings.GetInteger(kSettingColor, &color) || | 178 !job_settings.GetInteger(kSettingColor, &color) || |
109 !job_settings.GetInteger(kSettingDuplexMode, &duplex_mode) || | 179 !job_settings.GetInteger(kSettingDuplexMode, &duplex_mode) || |
110 !job_settings.GetBoolean(kSettingLandscape, &landscape) || | 180 !job_settings.GetBoolean(kSettingLandscape, &landscape) || |
111 !job_settings.GetString(kSettingDeviceName, &device_name)) { | 181 !job_settings.GetString(kSettingDeviceName, &device_name)) { |
112 return false; | 182 return false; |
113 } | 183 } |
114 | 184 |
115 settings->set_collate(collate); | 185 settings->set_collate(collate); |
116 settings->set_copies(copies); | 186 settings->set_copies(copies); |
117 settings->SetOrientation(landscape); | 187 settings->SetOrientation(landscape); |
118 settings->set_device_name(device_name); | 188 settings->set_device_name(device_name); |
119 settings->set_duplex_mode(static_cast<DuplexMode>(duplex_mode)); | 189 settings->set_duplex_mode(static_cast<DuplexMode>(duplex_mode)); |
120 settings->set_color(static_cast<ColorModel>(color)); | 190 settings->set_color(static_cast<ColorModel>(color)); |
121 | 191 |
122 return true; | 192 return true; |
123 } | 193 } |
124 | 194 |
| 195 void PrintSettingsToJobSettingsDebug(const PrintSettings& settings, |
| 196 base::DictionaryValue* job_settings) { |
| 197 job_settings->SetBoolean(kSettingHeaderFooterEnabled, |
| 198 settings.display_header_footer()); |
| 199 job_settings->SetString(kSettingHeaderFooterTitle, settings.title()); |
| 200 job_settings->SetString(kSettingHeaderFooterURL, settings.url()); |
| 201 job_settings->SetBoolean(kSettingShouldPrintBackgrounds, |
| 202 settings.should_print_backgrounds()); |
| 203 job_settings->SetBoolean(kSettingShouldPrintSelectionOnly, |
| 204 settings.selection_only()); |
| 205 job_settings->SetInteger(kSettingMarginsType, settings.margin_type()); |
| 206 if (!settings.ranges().empty()) { |
| 207 base::ListValue* page_range_array = new base::ListValue; |
| 208 job_settings->Set(kSettingPageRange, page_range_array); |
| 209 for (size_t i = 0; i < settings.ranges().size(); ++i) { |
| 210 base::DictionaryValue* dict = new base::DictionaryValue; |
| 211 page_range_array->Append(dict); |
| 212 dict->SetInteger(kSettingPageRangeFrom, settings.ranges()[i].from + 1); |
| 213 dict->SetInteger(kSettingPageRangeTo, settings.ranges()[i].to + 1); |
| 214 } |
| 215 } |
| 216 |
| 217 job_settings->SetBoolean(kSettingCollate, settings.collate()); |
| 218 job_settings->SetInteger(kSettingCopies, settings.copies()); |
| 219 job_settings->SetInteger(kSettingColor, settings.color()); |
| 220 job_settings->SetInteger(kSettingDuplexMode, settings.duplex_mode()); |
| 221 job_settings->SetBoolean(kSettingLandscape, settings.landscape()); |
| 222 job_settings->SetString(kSettingDeviceName, settings.device_name()); |
| 223 |
| 224 // Following values are not read form JSON by InitSettings, so do not have |
| 225 // common public constants. So just serialize in "debug" section. |
| 226 base::DictionaryValue* debug = new base::DictionaryValue; |
| 227 job_settings->Set("debug", debug); |
| 228 debug->SetDouble("minShrink", settings.min_shrink()); |
| 229 debug->SetDouble("maxShrink", settings.max_shrink()); |
| 230 debug->SetInteger("desiredDpi", settings.desired_dpi()); |
| 231 debug->SetInteger("dpi", settings.dpi()); |
| 232 debug->SetInteger("deviceUnitsPerInch", settings.device_units_per_inch()); |
| 233 debug->SetBoolean("support_alpha_blend", settings.should_print_backgrounds()); |
| 234 debug->SetString("media_vendor_od", settings.requested_media().vendor_id); |
| 235 SetSizeToJobSettings( |
| 236 "media_size", settings.requested_media().size_microns, debug); |
| 237 SetMarginsToJobSettings("requested_custom_margins_in_points", |
| 238 settings.requested_custom_margins_in_points(), |
| 239 debug); |
| 240 const PageSetup& page_setup = settings.page_setup_device_units(); |
| 241 SetMarginsToJobSettings( |
| 242 "effective_margins", page_setup.effective_margins(), debug); |
| 243 SetSizeToJobSettings("physical_size", page_setup.physical_size(), debug); |
| 244 SetRectToJobSettings("overlay_area", page_setup.overlay_area(), debug); |
| 245 SetRectToJobSettings("content_area", page_setup.content_area(), debug); |
| 246 SetRectToJobSettings("printable_area", page_setup.printable_area(), debug); |
| 247 } |
| 248 |
125 } // namespace printing | 249 } // namespace printing |
OLD | NEW |