| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/ui/webui/print_preview/sticky_settings.h" | 5 #include "chrome/browser/ui/webui/print_preview/sticky_settings.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/browser/prefs/pref_service.h" | 10 #include "chrome/browser/prefs/pref_service.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
| 13 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
| 14 #include "printing/page_size_margins.h" | 14 #include "printing/page_size_margins.h" |
| 15 | 15 |
| 16 namespace printing { | 16 namespace printing { |
| 17 | 17 |
| 18 const char kSettingSavePath[] = "savePath"; | 18 const char kSettingSavePath[] = "savePath"; |
| 19 const char kSettingCloudPrintData[] = "cloudPrintData"; | 19 const char kSettingAppState[] = "appState"; |
| 20 | 20 |
| 21 StickySettings::StickySettings() | 21 StickySettings::StickySettings() {} |
| 22 : color_model_(printing::UNKNOWN_COLOR_MODEL), | |
| 23 margins_type_(printing::DEFAULT_MARGINS), | |
| 24 headers_footers_(true), | |
| 25 duplex_mode_(printing::UNKNOWN_DUPLEX_MODE) {} | |
| 26 | 22 |
| 27 StickySettings::~StickySettings() {} | 23 StickySettings::~StickySettings() {} |
| 28 | 24 |
| 29 void StickySettings::GetLastUsedMarginSettings( | 25 void StickySettings::StoreAppState(const std::string& data) { |
| 30 base::DictionaryValue* custom_margins) const { | 26 printer_app_state_.reset(new std::string(data)); |
| 31 custom_margins->SetInteger(printing::kSettingMarginsType, | |
| 32 margins_type_); | |
| 33 if (page_size_margins_.get()) { | |
| 34 custom_margins->SetDouble(printing::kSettingMarginTop, | |
| 35 page_size_margins_->margin_top); | |
| 36 custom_margins->SetDouble(printing::kSettingMarginBottom, | |
| 37 page_size_margins_->margin_bottom); | |
| 38 custom_margins->SetDouble(printing::kSettingMarginLeft, | |
| 39 page_size_margins_->margin_left); | |
| 40 custom_margins->SetDouble(printing::kSettingMarginRight, | |
| 41 page_size_margins_->margin_right); | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 void StickySettings::StoreColorModel( | |
| 46 const base::DictionaryValue& settings) { | |
| 47 int color_model; | |
| 48 if (!settings.GetInteger(printing::kSettingColor, &color_model)) | |
| 49 color_model = printing::GRAY; | |
| 50 color_model_ = static_cast<printing::ColorModels>(color_model); | |
| 51 } | |
| 52 | |
| 53 void StickySettings::StoreMarginSettings( | |
| 54 const base::DictionaryValue& settings) { | |
| 55 int margin_type; | |
| 56 if (!settings.GetInteger(printing::kSettingMarginsType, &margin_type)) | |
| 57 margin_type = printing::DEFAULT_MARGINS; | |
| 58 margins_type_ = static_cast<printing::MarginType>(margin_type); | |
| 59 if (margins_type_ == printing::CUSTOM_MARGINS) { | |
| 60 if (!page_size_margins_.get()) | |
| 61 page_size_margins_.reset(new printing::PageSizeMargins()); | |
| 62 GetCustomMarginsFromJobSettings( | |
| 63 settings, page_size_margins_.get()); | |
| 64 } | |
| 65 } | |
| 66 | |
| 67 void StickySettings::StoreHeadersFooters( | |
| 68 const base::DictionaryValue& settings) { | |
| 69 settings.GetBoolean( | |
| 70 printing::kSettingHeaderFooterEnabled, &headers_footers_); | |
| 71 } | |
| 72 | |
| 73 void StickySettings::Store(const base::DictionaryValue& settings) { | |
| 74 // Storing last used color model. | |
| 75 StoreColorModel(settings); | |
| 76 // Storing last used duplex mode. | |
| 77 StoreDuplexMode(settings); | |
| 78 | |
| 79 bool is_modifiable = false; | |
| 80 settings.GetBoolean(printing::kSettingPreviewModifiable, &is_modifiable); | |
| 81 if (is_modifiable) { | |
| 82 // Storing last used margin settings. | |
| 83 StoreMarginSettings(settings); | |
| 84 // Storing last used header and footer setting. | |
| 85 StoreHeadersFooters(settings); | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 void StickySettings::StoreDuplexMode( | |
| 90 const base::DictionaryValue& settings) { | |
| 91 int duplex_mode = printing::UNKNOWN_DUPLEX_MODE; | |
| 92 settings.GetInteger(printing::kSettingDuplexMode, &duplex_mode); | |
| 93 duplex_mode_ = static_cast<printing::DuplexMode>(duplex_mode); | |
| 94 } | |
| 95 | |
| 96 void StickySettings::StorePrinterName(const std::string& printer_name) { | |
| 97 printer_name_.reset(new std::string(printer_name)); | |
| 98 } | |
| 99 | |
| 100 void StickySettings::StoreCloudPrintData(const std::string& data) { | |
| 101 printer_cloud_print_data_.reset(new std::string(data)); | |
| 102 } | 27 } |
| 103 | 28 |
| 104 void StickySettings::StoreSavePath(const FilePath& path) { | 29 void StickySettings::StoreSavePath(const FilePath& path) { |
| 105 save_path_.reset(new FilePath(path)); | 30 save_path_.reset(new FilePath(path)); |
| 106 } | 31 } |
| 107 | 32 |
| 108 void StickySettings::SaveInPrefs(PrefService* prefs) { | 33 void StickySettings::SaveInPrefs(PrefService* prefs) { |
| 109 if (CommandLine::ForCurrentProcess()->HasSwitch( | |
| 110 switches::kPrintSettingsReset)) | |
| 111 return; | |
| 112 DCHECK(prefs); | 34 DCHECK(prefs); |
| 113 if (prefs) { | 35 if (prefs) { |
| 114 scoped_ptr<DictionaryValue> value(new DictionaryValue); | 36 scoped_ptr<DictionaryValue> value(new DictionaryValue); |
| 115 if (save_path_.get()) | 37 if (save_path_.get()) |
| 116 value->SetString(printing::kSettingSavePath, save_path_->value()); | 38 value->SetString(printing::kSettingSavePath, save_path_->value()); |
| 117 if (printer_name_.get()) | 39 if (printer_app_state_.get()) |
| 118 value->SetString(printing::kSettingPrinterName, *printer_name_); | 40 value->SetString(printing::kSettingAppState, |
| 119 if (printer_cloud_print_data_.get()) | 41 *printer_app_state_); |
| 120 value->SetString(printing::kSettingCloudPrintData, | |
| 121 *printer_cloud_print_data_); | |
| 122 value->SetInteger(printing::kSettingColor, color_model_); | |
| 123 value->SetInteger(printing::kSettingMarginsType, margins_type_); | |
| 124 if (margins_type_ == printing::CUSTOM_MARGINS) { | |
| 125 if (page_size_margins_.get()) { | |
| 126 value->SetDouble(printing::kSettingMarginTop, | |
| 127 page_size_margins_->margin_top); | |
| 128 value->SetDouble(printing::kSettingMarginBottom, | |
| 129 page_size_margins_->margin_bottom); | |
| 130 value->SetDouble(printing::kSettingMarginLeft, | |
| 131 page_size_margins_->margin_left); | |
| 132 value->SetDouble(printing::kSettingMarginRight, | |
| 133 page_size_margins_->margin_right); | |
| 134 } | |
| 135 } | |
| 136 value->SetBoolean(printing::kSettingHeaderFooterEnabled, headers_footers_); | |
| 137 value->SetInteger(printing::kSettingDuplexMode, duplex_mode_); | |
| 138 prefs->Set(prefs::kPrintPreviewStickySettings, *value); | 42 prefs->Set(prefs::kPrintPreviewStickySettings, *value); |
| 139 } | 43 } |
| 140 } | 44 } |
| 141 | 45 |
| 142 void StickySettings::RestoreFromPrefs(PrefService* prefs) { | 46 void StickySettings::RestoreFromPrefs(PrefService* prefs) { |
| 143 if (CommandLine::ForCurrentProcess()->HasSwitch( | |
| 144 switches::kPrintSettingsReset)) | |
| 145 return; | |
| 146 DCHECK(prefs); | 47 DCHECK(prefs); |
| 147 if (prefs) { | 48 if (prefs) { |
| 148 const DictionaryValue* value = | 49 const DictionaryValue* value = |
| 149 prefs->GetDictionary(prefs::kPrintPreviewStickySettings); | 50 prefs->GetDictionary(prefs::kPrintPreviewStickySettings); |
| 150 | 51 |
| 151 FilePath::StringType save_path; | 52 FilePath::StringType save_path; |
| 152 if (value->GetString(printing::kSettingSavePath, &save_path)) | 53 if (value->GetString(printing::kSettingSavePath, &save_path)) |
| 153 save_path_.reset(new FilePath(save_path)); | 54 save_path_.reset(new FilePath(save_path)); |
| 154 std::string buffer; | 55 std::string buffer; |
| 155 if (value->GetString(printing::kSettingPrinterName, &buffer)) | 56 if (value->GetString(printing::kSettingAppState, &buffer)) |
| 156 StorePrinterName(buffer); | 57 printer_app_state_.reset(new std::string(buffer)); |
| 157 if (value->GetString(printing::kSettingCloudPrintData, &buffer)) | |
| 158 StoreCloudPrintData(buffer); | |
| 159 int int_buffer; | |
| 160 if (value->GetInteger(printing::kSettingColor, &int_buffer)) | |
| 161 color_model_ = static_cast<printing::ColorModels>(int_buffer); | |
| 162 if (value->GetInteger(printing::kSettingMarginsType, &int_buffer)) | |
| 163 margins_type_ = static_cast<printing::MarginType>(int_buffer); | |
| 164 if (margins_type_ == printing::CUSTOM_MARGINS) { | |
| 165 if (!page_size_margins_.get()) | |
| 166 page_size_margins_.reset(new PageSizeMargins); | |
| 167 if (page_size_margins_.get()) { | |
| 168 value->GetDouble(printing::kSettingMarginTop, | |
| 169 &page_size_margins_->margin_top); | |
| 170 value->GetDouble(printing::kSettingMarginBottom, | |
| 171 &page_size_margins_->margin_bottom); | |
| 172 value->GetDouble(printing::kSettingMarginLeft, | |
| 173 &page_size_margins_->margin_left); | |
| 174 value->GetDouble(printing::kSettingMarginRight, | |
| 175 &page_size_margins_->margin_right); | |
| 176 } | |
| 177 } | |
| 178 value->GetBoolean(printing::kSettingHeaderFooterEnabled, &headers_footers_); | |
| 179 if (value->GetInteger(printing::kSettingDuplexMode, &int_buffer)) | |
| 180 duplex_mode_ = static_cast<printing::DuplexMode>(int_buffer); | |
| 181 } | 58 } |
| 182 } | 59 } |
| 183 | 60 |
| 184 void StickySettings::RegisterUserPrefs(PrefService* prefs) { | 61 void StickySettings::RegisterUserPrefs(PrefService* prefs) { |
| 185 prefs->RegisterDictionaryPref(prefs::kPrintPreviewStickySettings, | 62 prefs->RegisterDictionaryPref(prefs::kPrintPreviewStickySettings, |
| 186 PrefService::UNSYNCABLE_PREF); | 63 PrefService::UNSYNCABLE_PREF); |
| 187 } | 64 } |
| 188 | 65 |
| 189 std::string* StickySettings::printer_name() { | 66 std::string* StickySettings::printer_app_state() { |
| 190 return printer_name_.get(); | 67 return printer_app_state_.get(); |
| 191 } | |
| 192 | |
| 193 std::string* StickySettings::printer_cloud_print_data() { | |
| 194 return printer_cloud_print_data_.get(); | |
| 195 } | |
| 196 | |
| 197 printing::ColorModels StickySettings::color_model() const { | |
| 198 return color_model_; | |
| 199 } | |
| 200 | |
| 201 bool StickySettings::headers_footers() const { | |
| 202 return headers_footers_; | |
| 203 } | 68 } |
| 204 | 69 |
| 205 FilePath* StickySettings::save_path() { | 70 FilePath* StickySettings::save_path() { |
| 206 return save_path_.get(); | 71 return save_path_.get(); |
| 207 } | 72 } |
| 208 | 73 |
| 209 printing::DuplexMode StickySettings::duplex_mode() const { | |
| 210 return duplex_mode_; | |
| 211 } | |
| 212 | |
| 213 } // namespace printing | 74 } // namespace printing |
| OLD | NEW |