1 // Copyright 2013 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.
5 #include "ash/system/locale/locale_notification_controller.h"
8 #include "ash/system/system_notifier.h"
9 #include "ash/system/tray/system_tray_notifier.h"
10 #include "base/strings/string16.h"
11 #include "grit/ash_resources.h"
12 #include "grit/ash_strings.h"
13 #include "ui/base/l10n/l10n_util.h"
14 #include "ui/base/resource/resource_bundle.h"
15 #include "ui/message_center/message_center.h"
16 #include "ui/message_center/notification.h"
17 #include "ui/message_center/notification_delegate.h"
18 #include "ui/message_center/notification_types.h"
20 using message_center::Notification;
25 const char kLocaleChangeNotificationId[] = "chrome://settings/locale";
27 class LocaleNotificationDelegate : public message_center::NotificationDelegate {
29 explicit LocaleNotificationDelegate(LocaleObserver::Delegate* delegate);
32 virtual ~LocaleNotificationDelegate();
34 // message_center::NotificationDelegate overrides:
35 virtual void Display() OVERRIDE;
36 virtual void Error() OVERRIDE;
37 virtual void Close(bool by_user) OVERRIDE;
38 virtual bool HasClickedListener() OVERRIDE;
39 virtual void Click() OVERRIDE;
40 virtual void ButtonClick(int button_index) OVERRIDE;
43 LocaleObserver::Delegate* delegate_;
45 DISALLOW_COPY_AND_ASSIGN(LocaleNotificationDelegate);
48 LocaleNotificationDelegate::LocaleNotificationDelegate(
49 LocaleObserver::Delegate* delegate)
50 : delegate_(delegate) {
54 LocaleNotificationDelegate::~LocaleNotificationDelegate() {
57 void LocaleNotificationDelegate::Display() {
60 void LocaleNotificationDelegate::Error() {
63 void LocaleNotificationDelegate::Close(bool by_user) {
64 delegate_->AcceptLocaleChange();
67 bool LocaleNotificationDelegate::HasClickedListener() {
71 void LocaleNotificationDelegate::Click() {
72 delegate_->AcceptLocaleChange();
75 void LocaleNotificationDelegate::ButtonClick(int button_index) {
76 DCHECK_EQ(0, button_index);
77 delegate_->RevertLocaleChange();
82 LocaleNotificationController::LocaleNotificationController()
84 Shell::GetInstance()->system_tray_notifier()->AddLocaleObserver(this);
87 LocaleNotificationController::~LocaleNotificationController() {
88 Shell::GetInstance()->system_tray_notifier()->RemoveLocaleObserver(this);
91 void LocaleNotificationController::OnLocaleChanged(
92 LocaleObserver::Delegate* delegate,
93 const std::string& cur_locale,
94 const std::string& from_locale,
95 const std::string& to_locale) {
99 base::string16 from = l10n_util::GetDisplayNameForLocale(
100 from_locale, cur_locale, true);
101 base::string16 to = l10n_util::GetDisplayNameForLocale(
102 to_locale, cur_locale, true);
104 message_center::RichNotificationData optional;
105 optional.buttons.push_back(message_center::ButtonInfo(
106 l10n_util::GetStringFUTF16(
107 IDS_ASH_STATUS_TRAY_LOCALE_REVERT_MESSAGE, from)));
108 optional.never_timeout = true;
110 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
111 scoped_ptr<Notification> notification(new Notification(
112 message_center::NOTIFICATION_TYPE_SIMPLE,
113 kLocaleChangeNotificationId,
114 base::string16() /* title */,
115 l10n_util::GetStringFUTF16(
116 IDS_ASH_STATUS_TRAY_LOCALE_CHANGE_MESSAGE, from, to),
117 bundle.GetImageNamed(IDR_AURA_UBER_TRAY_LOCALE),
118 base::string16() /* display_source */,
119 message_center::NotifierId(
120 message_center::NotifierId::SYSTEM_COMPONENT,
121 system_notifier::kNotifierLocale),
123 new LocaleNotificationDelegate(delegate)));
124 message_center::MessageCenter::Get()->AddNotification(notification.Pass());