Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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/printing_context.h" |
| 6 | |
| 7 | #include "base/logging.h" |
| 8 | #include "base/values.h" |
| 9 | #include "printing/page_setup.h" |
| 10 | #include "printing/page_size_margins.h" |
| 11 | #include "printing/print_settings_initializer.h" |
| 12 | |
| 13 | namespace printing { |
| 14 | |
| 15 | PrintingContext::PrintingContext(const std::string& app_locale) |
| 16 | : dialog_box_dismissed_(false), |
| 17 | in_print_job_(false), |
| 18 | abort_printing_(false), |
| 19 | app_locale_(app_locale) { |
| 20 | } |
| 21 | |
| 22 | PrintingContext::~PrintingContext() { |
| 23 | } |
| 24 | |
| 25 | void PrintingContext::set_margin_type(MarginType type) { |
| 26 | DCHECK(type != CUSTOM_MARGINS); |
| 27 | settings_.margin_type = type; |
| 28 | } |
| 29 | |
| 30 | void PrintingContext::ResetSettings() { |
| 31 | ReleaseContext(); |
| 32 | |
| 33 | settings_.Clear(); |
| 34 | |
| 35 | in_print_job_ = false; |
| 36 | dialog_box_dismissed_ = false; |
| 37 | abort_printing_ = false; |
| 38 | } |
| 39 | |
| 40 | PrintingContext::Result PrintingContext::OnError() { |
| 41 | ResetSettings(); |
| 42 | return abort_printing_ ? CANCEL : FAILED; |
| 43 | } |
| 44 | |
| 45 | PrintingContext::Result PrintingContext::UpdatePrintSettings( |
| 46 | const base::DictionaryValue& job_settings, |
| 47 | const PageRanges& ranges) { |
| 48 | ResetSettings(); |
| 49 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 50 | job_settings.GetBoolean(kSettingHeaderFooterEnabled, |
| 51 | &settings_.display_header_footer); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 52 | |
| 53 | int margin_type = DEFAULT_MARGINS; |
| 54 | if (!job_settings.GetInteger(kSettingMarginsType, &margin_type) || |
| 55 | (margin_type != DEFAULT_MARGINS && |
| 56 | margin_type != NO_MARGINS && |
| 57 | margin_type != CUSTOM_MARGINS && |
| 58 | margin_type != PRINTABLE_AREA_MARGINS)) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 59 | margin_type = DEFAULT_MARGINS; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 60 | } |
| 61 | settings_.margin_type = static_cast<MarginType>(margin_type); |
| 62 | |
| 63 | if (margin_type == CUSTOM_MARGINS) { |
| 64 | printing::PageSizeMargins page_size_margins; |
| 65 | GetCustomMarginsFromJobSettings(job_settings, &page_size_margins); |
| 66 | |
| 67 | PageMargins margins_in_points; |
| 68 | margins_in_points.Clear(); |
| 69 | margins_in_points.top = page_size_margins.margin_top; |
| 70 | margins_in_points.bottom = page_size_margins.margin_bottom; |
| 71 | margins_in_points.left = page_size_margins.margin_left; |
| 72 | margins_in_points.right = page_size_margins.margin_right; |
| 73 | |
| 74 | settings_.SetCustomMargins(margins_in_points); |
| 75 | } |
| 76 | |
| 77 | PrintingContext::Result result = UpdatePrinterSettings(job_settings, ranges); |
| 78 | PrintSettingsInitializer::InitHeaderFooterStrings(job_settings, &settings_); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 79 | |
| 80 | job_settings.GetBoolean(kSettingShouldPrintBackgrounds, |
| 81 | &settings_.should_print_backgrounds); |
| 82 | job_settings.GetBoolean(kSettingShouldPrintSelectionOnly, |
| 83 | &settings_.selection_only); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 84 | return result; |
| 85 | } |
| 86 | |
| 87 | } // namespace printing |