summaryrefslogtreecommitdiffstats
path: root/ash/system/locale/locale_notification_controller.cc
blob: 7d66d5b77fb952f02c6c0f68c68ec0415e1f4b2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/system/locale/locale_notification_controller.h"

#include "ash/shell.h"
#include "ash/system/system_notifier.h"
#include "ash/system/tray/system_tray_notifier.h"
#include "base/strings/string16.h"
#include "grit/ash_resources.h"
#include "grit/ash_strings.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/notification.h"
#include "ui/message_center/notification_delegate.h"
#include "ui/message_center/notification_types.h"

using message_center::Notification;

namespace ash {
namespace internal {
namespace {

const char kLocaleChangeNotificationId[] = "chrome://settings/locale";

class LocaleNotificationDelegate : public message_center::NotificationDelegate {
 public:
  explicit LocaleNotificationDelegate(LocaleObserver::Delegate* delegate);

 protected:
  virtual ~LocaleNotificationDelegate();

  // message_center::NotificationDelegate overrides:
  virtual void Display() OVERRIDE;
  virtual void Error() OVERRIDE;
  virtual void Close(bool by_user) OVERRIDE;
  virtual bool HasClickedListener() OVERRIDE;
  virtual void Click() OVERRIDE;
  virtual void ButtonClick(int button_index) OVERRIDE;

 private:
  LocaleObserver::Delegate* delegate_;

  DISALLOW_COPY_AND_ASSIGN(LocaleNotificationDelegate);
};

LocaleNotificationDelegate::LocaleNotificationDelegate(
    LocaleObserver::Delegate* delegate)
  : delegate_(delegate) {
  DCHECK(delegate_);
}

LocaleNotificationDelegate::~LocaleNotificationDelegate() {
}

void LocaleNotificationDelegate::Display() {
}

void LocaleNotificationDelegate::Error() {
}

void LocaleNotificationDelegate::Close(bool by_user) {
  delegate_->AcceptLocaleChange();
}

bool LocaleNotificationDelegate::HasClickedListener() {
  return true;
}

void LocaleNotificationDelegate::Click() {
  delegate_->AcceptLocaleChange();
}

void LocaleNotificationDelegate::ButtonClick(int button_index) {
  DCHECK_EQ(0, button_index);
  delegate_->RevertLocaleChange();
}

}  // namespace

LocaleNotificationController::LocaleNotificationController()
    : delegate_(NULL) {
  Shell::GetInstance()->system_tray_notifier()->AddLocaleObserver(this);
}

LocaleNotificationController::~LocaleNotificationController() {
  Shell::GetInstance()->system_tray_notifier()->RemoveLocaleObserver(this);
}

void LocaleNotificationController::OnLocaleChanged(
    LocaleObserver::Delegate* delegate,
    const std::string& cur_locale,
    const std::string& from_locale,
    const std::string& to_locale) {
  if (!delegate)
    return;

  base::string16 from = l10n_util::GetDisplayNameForLocale(
      from_locale, cur_locale, true);
  base::string16 to = l10n_util::GetDisplayNameForLocale(
      to_locale, cur_locale, true);

  message_center::RichNotificationData optional;
  optional.buttons.push_back(message_center::ButtonInfo(
      l10n_util::GetStringFUTF16(
          IDS_ASH_STATUS_TRAY_LOCALE_REVERT_MESSAGE, from)));
  optional.never_timeout = true;

  ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
  scoped_ptr<Notification> notification(new Notification(
      message_center::NOTIFICATION_TYPE_SIMPLE,
      kLocaleChangeNotificationId,
      base::string16()  /* title */,
      l10n_util::GetStringFUTF16(
          IDS_ASH_STATUS_TRAY_LOCALE_CHANGE_MESSAGE, from, to),
      bundle.GetImageNamed(IDR_AURA_UBER_TRAY_LOCALE),
      base::string16()  /* display_source */,
      message_center::NotifierId(system_notifier::NOTIFIER_LOCALE),
      optional,
      new LocaleNotificationDelegate(delegate)));
  message_center::MessageCenter::Get()->AddNotification(notification.Pass());
}

}  // namespace internal
}  // namespace ash