| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2010 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 "chrome/browser/chromeos/dom_ui/system_settings_provider.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/time.h" |
| 10 #include "base/string_util.h" |
| 11 #include "base/stl_util-inl.h" |
| 12 #include "base/time.h" |
| 13 #include "base/utf_string_conversions.h" |
| 14 #include "base/values.h" |
| 15 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 16 #include "chrome/browser/chromeos/cros_settings.h" |
| 17 #include "chrome/browser/chromeos/cros_settings_names.h" |
| 18 |
| 19 namespace chromeos { |
| 20 |
| 21 static const char* kTimeZonesUtf8[] = { |
| 22 "Pacific/Samoa", |
| 23 "US/Hawaii", |
| 24 "US/Alaska", |
| 25 "US/Pacific", |
| 26 "US/Mountain", |
| 27 "US/Central", |
| 28 "US/Eastern", |
| 29 "America/Santiago", |
| 30 "America/Sao_Paulo", |
| 31 "Atlantic/South_Georgia", |
| 32 "Atlantic/Cape_Verde", |
| 33 "Europe/London", |
| 34 "Europe/Rome", |
| 35 "Europe/Helsinki", |
| 36 "Europe/Moscow", |
| 37 "Asia/Dubai", |
| 38 "Asia/Karachi", |
| 39 "Asia/Dhaka", |
| 40 "Asia/Bangkok", |
| 41 "Asia/Hong_Kong", |
| 42 "Asia/Tokyo", |
| 43 "Australia/Sydney", |
| 44 "Asia/Magadan", |
| 45 "Pacific/Auckland" }; |
| 46 |
| 47 SystemSettingsProvider::SystemSettingsProvider() { |
| 48 // TODO(chocobo): For now, add all the GMT timezones. |
| 49 // We may eventually want to use icu::TimeZone::createEnumeration() |
| 50 // to list all the timezones and pick the ones we want to show. |
| 51 // NOTE: This currently does not handle daylight savings properly |
| 52 // b/c this is just a manually selected list of timezones that |
| 53 // happen to span the GMT-11 to GMT+12 Today. When daylight savings |
| 54 // kick in, this list might have more than one timezone in the same |
| 55 // GMT bucket. |
| 56 for (size_t i = 0; i < arraysize(kTimeZonesUtf8); i++) { |
| 57 timezones_.push_back(icu::TimeZone::createTimeZone( |
| 58 icu::UnicodeString::fromUTF8(kTimeZonesUtf8[i]))); |
| 59 } |
| 60 CrosLibrary::Get()->GetSystemLibrary()->AddObserver(this); |
| 61 |
| 62 } |
| 63 |
| 64 SystemSettingsProvider::~SystemSettingsProvider() { |
| 65 CrosLibrary::Get()->GetSystemLibrary()->RemoveObserver(this); |
| 66 STLDeleteElements(&timezones_); |
| 67 } |
| 68 |
| 69 void SystemSettingsProvider::Set(const std::string& path, Value* in_value) { |
| 70 if (path == kSystemTimezone) { |
| 71 string16 value; |
| 72 if (!in_value || !in_value->IsType(Value::TYPE_STRING) || |
| 73 !in_value->GetAsString(&value)) |
| 74 return; |
| 75 const icu::TimeZone* timezone = GetTimezone(value); |
| 76 if (!timezone) |
| 77 return; |
| 78 CrosLibrary::Get()->GetSystemLibrary()->SetTimezone(timezone); |
| 79 } |
| 80 } |
| 81 |
| 82 bool SystemSettingsProvider::Get(const std::string& path, |
| 83 Value** out_value) const { |
| 84 if (path == kSystemTimezone) { |
| 85 *out_value = Value::CreateStringValue( |
| 86 GetTimezoneID(CrosLibrary::Get()->GetSystemLibrary()->GetTimezone())); |
| 87 return true; |
| 88 } |
| 89 return false; |
| 90 } |
| 91 |
| 92 bool SystemSettingsProvider::HandlesSetting(const std::string& path) { |
| 93 return ::StartsWithASCII(path, std::string("cros.system."), true); |
| 94 } |
| 95 |
| 96 void SystemSettingsProvider::TimezoneChanged(const icu::TimeZone& timezone) { |
| 97 // Fires system setting change notification. |
| 98 CrosSettings::Get()->FireObservers(kSystemTimezone); |
| 99 } |
| 100 |
| 101 ListValue* SystemSettingsProvider::GetTimezoneList() { |
| 102 ListValue* timezoneList = new ListValue(); |
| 103 for (std::vector<icu::TimeZone*>::iterator iter = timezones_.begin(); |
| 104 iter != timezones_.end(); ++iter) { |
| 105 const icu::TimeZone* timezone = *iter; |
| 106 ListValue* option = new ListValue(); |
| 107 option->Append(Value::CreateStringValue(GetTimezoneID(*timezone))); |
| 108 option->Append(Value::CreateStringValue(GetTimezoneName(*timezone))); |
| 109 timezoneList->Append(option); |
| 110 } |
| 111 return timezoneList; |
| 112 } |
| 113 |
| 114 string16 SystemSettingsProvider::GetTimezoneName( |
| 115 const icu::TimeZone& timezone) { |
| 116 icu::UnicodeString name; |
| 117 timezone.getDisplayName(name); |
| 118 string16 output(name.getBuffer(), name.length()); |
| 119 int hour_offset = timezone.getRawOffset() / 3600000; |
| 120 const wchar_t* format; |
| 121 if (hour_offset == 0) |
| 122 format = L"(GMT) "; |
| 123 else |
| 124 format = L"(GMT%+d) "; |
| 125 std::wstring offset = StringPrintf(format, hour_offset); |
| 126 return WideToUTF16(offset) + output; |
| 127 } |
| 128 |
| 129 string16 SystemSettingsProvider::GetTimezoneID( |
| 130 const icu::TimeZone& timezone) { |
| 131 icu::UnicodeString id; |
| 132 timezone.getID(id); |
| 133 return string16(id.getBuffer(), id.length()); |
| 134 } |
| 135 |
| 136 const icu::TimeZone* SystemSettingsProvider::GetTimezone( |
| 137 const string16& timezone_id) { |
| 138 for (std::vector<icu::TimeZone*>::iterator iter = timezones_.begin(); |
| 139 iter != timezones_.end(); ++iter) { |
| 140 const icu::TimeZone* timezone = *iter; |
| 141 if (GetTimezoneID(*timezone) == timezone_id) { |
| 142 return timezone; |
| 143 } |
| 144 } |
| 145 return NULL; |
| 146 } |
| 147 |
| 148 } // namespace chromeos |
| OLD | NEW |