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() {
75 Shell::GetInstance()->system_tray_notifier()->AddLocaleObserver(this);
78 LocaleNotificationController::~LocaleNotificationController() {
79 Shell::GetInstance()->system_tray_notifier()->RemoveLocaleObserver(this);
82 void LocaleNotificationController::OnLocaleChanged(
83 LocaleObserver::Delegate
* delegate
,
84 const std::string
& cur_locale
,
85 const std::string
& from_locale
,
86 const std::string
& to_locale
) {
90 base::string16 from
= l10n_util::GetDisplayNameForLocale(
91 from_locale
, cur_locale
, true);
92 base::string16 to
= l10n_util::GetDisplayNameForLocale(
93 to_locale
, cur_locale
, true);
95 message_center::RichNotificationData optional
;
96 optional
.buttons
.push_back(message_center::ButtonInfo(
97 l10n_util::GetStringFUTF16(
98 IDS_ASH_STATUS_TRAY_LOCALE_REVERT_MESSAGE
, from
)));
99 optional
.never_timeout
= true;
101 ui::ResourceBundle
& bundle
= ui::ResourceBundle::GetSharedInstance();
102 scoped_ptr
<Notification
> notification(new Notification(
103 message_center::NOTIFICATION_TYPE_SIMPLE
, kLocaleChangeNotificationId
,
104 base::string16() /* title */,
105 l10n_util::GetStringFUTF16(IDS_ASH_STATUS_TRAY_LOCALE_CHANGE_MESSAGE
,
107 bundle
.GetImageNamed(IDR_AURA_UBER_TRAY_LOCALE
),
108 base::string16() /* display_source */, GURL(),
109 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT
,
110 system_notifier::kNotifierLocale
),
111 optional
, new LocaleNotificationDelegate(delegate
)));
112 message_center::MessageCenter::Get()->AddNotification(notification
.Pass());