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.
4
5#include "ash/system/locale/locale_notification_controller.h"
6
7#include "ash/shell.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"
19
20using message_center::Notification;
21
22namespace ash {
23namespace {
24
25const char kLocaleChangeNotificationId[] = "chrome://settings/locale";
26
27class LocaleNotificationDelegate : public message_center::NotificationDelegate {
28 public:
29  explicit LocaleNotificationDelegate(LocaleObserver::Delegate* delegate);
30
31 protected:
32  virtual ~LocaleNotificationDelegate();
33
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;
41
42 private:
43  LocaleObserver::Delegate* delegate_;
44
45  DISALLOW_COPY_AND_ASSIGN(LocaleNotificationDelegate);
46};
47
48LocaleNotificationDelegate::LocaleNotificationDelegate(
49    LocaleObserver::Delegate* delegate)
50  : delegate_(delegate) {
51  DCHECK(delegate_);
52}
53
54LocaleNotificationDelegate::~LocaleNotificationDelegate() {
55}
56
57void LocaleNotificationDelegate::Display() {
58}
59
60void LocaleNotificationDelegate::Error() {
61}
62
63void LocaleNotificationDelegate::Close(bool by_user) {
64  delegate_->AcceptLocaleChange();
65}
66
67bool LocaleNotificationDelegate::HasClickedListener() {
68  return true;
69}
70
71void LocaleNotificationDelegate::Click() {
72  delegate_->AcceptLocaleChange();
73}
74
75void LocaleNotificationDelegate::ButtonClick(int button_index) {
76  DCHECK_EQ(0, button_index);
77  delegate_->RevertLocaleChange();
78}
79
80}  // namespace
81
82LocaleNotificationController::LocaleNotificationController()
83    : delegate_(NULL) {
84  Shell::GetInstance()->system_tray_notifier()->AddLocaleObserver(this);
85}
86
87LocaleNotificationController::~LocaleNotificationController() {
88  Shell::GetInstance()->system_tray_notifier()->RemoveLocaleObserver(this);
89}
90
91void LocaleNotificationController::OnLocaleChanged(
92    LocaleObserver::Delegate* delegate,
93    const std::string& cur_locale,
94    const std::string& from_locale,
95    const std::string& to_locale) {
96  if (!delegate)
97    return;
98
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);
103
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;
109
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),
122      optional,
123      new LocaleNotificationDelegate(delegate)));
124  message_center::MessageCenter::Get()->AddNotification(notification.Pass());
125}
126
127}  // namespace ash
128