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 ~LocaleNotificationDelegate() override;
34 // message_center::NotificationDelegate overrides:
35 void Close(bool by_user) override;
36 bool HasClickedListener() override;
37 void Click() override;
38 void ButtonClick(int button_index) override;
41 LocaleObserver::Delegate* delegate_;
43 DISALLOW_COPY_AND_ASSIGN(LocaleNotificationDelegate);
46 LocaleNotificationDelegate::LocaleNotificationDelegate(
47 LocaleObserver::Delegate* delegate)
48 : delegate_(delegate) {
52 LocaleNotificationDelegate::~LocaleNotificationDelegate() {
55 void LocaleNotificationDelegate::Close(bool by_user) {
56 delegate_->AcceptLocaleChange();
59 bool LocaleNotificationDelegate::HasClickedListener() {
63 void LocaleNotificationDelegate::Click() {
64 delegate_->AcceptLocaleChange();
67 void LocaleNotificationDelegate::ButtonClick(int button_index) {
68 DCHECK_EQ(0, button_index);
69 delegate_->RevertLocaleChange();
74 LocaleNotificationController::LocaleNotificationController()
76 Shell::GetInstance()->system_tray_notifier()->AddLocaleObserver(this);
79 LocaleNotificationController::~LocaleNotificationController() {
80 Shell::GetInstance()->system_tray_notifier()->RemoveLocaleObserver(this);
83 void LocaleNotificationController::OnLocaleChanged(
84 LocaleObserver::Delegate* delegate,
85 const std::string& cur_locale,
86 const std::string& from_locale,
87 const std::string& to_locale) {
91 base::string16 from = l10n_util::GetDisplayNameForLocale(
92 from_locale, cur_locale, true);
93 base::string16 to = l10n_util::GetDisplayNameForLocale(
94 to_locale, cur_locale, true);
96 message_center::RichNotificationData optional;
97 optional.buttons.push_back(message_center::ButtonInfo(
98 l10n_util::GetStringFUTF16(
99 IDS_ASH_STATUS_TRAY_LOCALE_REVERT_MESSAGE, from)));
100 optional.never_timeout = true;
102 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
103 scoped_ptr<Notification> notification(new Notification(
104 message_center::NOTIFICATION_TYPE_SIMPLE,
105 kLocaleChangeNotificationId,
106 base::string16() /* title */,
107 l10n_util::GetStringFUTF16(
108 IDS_ASH_STATUS_TRAY_LOCALE_CHANGE_MESSAGE, from, to),
109 bundle.GetImageNamed(IDR_AURA_UBER_TRAY_LOCALE),
110 base::string16() /* display_source */,
111 message_center::NotifierId(
112 message_center::NotifierId::SYSTEM_COMPONENT,
113 system_notifier::kNotifierLocale),
115 new LocaleNotificationDelegate(delegate)));
116 message_center::MessageCenter::Get()->AddNotification(notification.Pass());