| OLD | NEW |
| (Empty) |
| 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 | |
| 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 PrintSettings::RequestedMedia requested_media; | |
| 55 const base::DictionaryValue* media_size_value = NULL; | |
| 56 if (job_settings.GetDictionary(kSettingMediaSize, &media_size_value)) { | |
| 57 int width_microns = 0; | |
| 58 int height_microns = 0; | |
| 59 if (media_size_value->GetInteger(kSettingMediaSizeWidthMicrons, | |
| 60 &width_microns) && | |
| 61 media_size_value->GetInteger(kSettingMediaSizeHeightMicrons, | |
| 62 &height_microns)) { | |
| 63 requested_media.size_microns = gfx::Size(width_microns, height_microns); | |
| 64 } | |
| 65 std::string vendor_id; | |
| 66 if (media_size_value->GetString(kSettingMediaSizeVendorId, &vendor_id) && | |
| 67 !vendor_id.empty()) { | |
| 68 requested_media.vendor_id = vendor_id; | |
| 69 } | |
| 70 } | |
| 71 settings->set_requested_media(requested_media); | |
| 72 | |
| 73 int margin_type = DEFAULT_MARGINS; | |
| 74 if (!job_settings.GetInteger(kSettingMarginsType, &margin_type) || | |
| 75 (margin_type != DEFAULT_MARGINS && | |
| 76 margin_type != NO_MARGINS && | |
| 77 margin_type != CUSTOM_MARGINS && | |
| 78 margin_type != PRINTABLE_AREA_MARGINS)) { | |
| 79 margin_type = DEFAULT_MARGINS; | |
| 80 } | |
| 81 settings->set_margin_type(static_cast<MarginType>(margin_type)); | |
| 82 | |
| 83 if (margin_type == CUSTOM_MARGINS) { | |
| 84 PageSizeMargins page_size_margins; | |
| 85 GetCustomMarginsFromJobSettings(job_settings, &page_size_margins); | |
| 86 | |
| 87 PageMargins margins_in_points; | |
| 88 margins_in_points.Clear(); | |
| 89 margins_in_points.top = page_size_margins.margin_top; | |
| 90 margins_in_points.bottom = page_size_margins.margin_bottom; | |
| 91 margins_in_points.left = page_size_margins.margin_left; | |
| 92 margins_in_points.right = page_size_margins.margin_right; | |
| 93 | |
| 94 settings->SetCustomMargins(margins_in_points); | |
| 95 } | |
| 96 | |
| 97 settings->set_ranges(ranges); | |
| 98 | |
| 99 int color = 0; | |
| 100 bool landscape = false; | |
| 101 int duplex_mode = 0; | |
| 102 base::string16 device_name; | |
| 103 bool collate = false; | |
| 104 int copies = 1; | |
| 105 | |
| 106 if (!job_settings.GetBoolean(kSettingCollate, &collate) || | |
| 107 !job_settings.GetInteger(kSettingCopies, &copies) || | |
| 108 !job_settings.GetInteger(kSettingColor, &color) || | |
| 109 !job_settings.GetInteger(kSettingDuplexMode, &duplex_mode) || | |
| 110 !job_settings.GetBoolean(kSettingLandscape, &landscape) || | |
| 111 !job_settings.GetString(kSettingDeviceName, &device_name)) { | |
| 112 return false; | |
| 113 } | |
| 114 | |
| 115 settings->set_collate(collate); | |
| 116 settings->set_copies(copies); | |
| 117 settings->SetOrientation(landscape); | |
| 118 settings->set_device_name(device_name); | |
| 119 settings->set_duplex_mode(static_cast<DuplexMode>(duplex_mode)); | |
| 120 settings->set_color(static_cast<ColorModel>(color)); | |
| 121 | |
| 122 return true; | |
| 123 } | |
| 124 | |
| 125 } // namespace printing | |
| OLD | NEW |