Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / ash / system / date / tray_date.cc
1 // Copyright (c) 2012 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/date/tray_date.h"
6
7 #include "ash/metrics/user_metrics_recorder.h"
8 #include "ash/session_state_delegate.h"
9 #include "ash/shell.h"
10 #include "ash/shell_delegate.h"
11 #include "ash/system/date/date_view.h"
12 #include "ash/system/tray/system_tray.h"
13 #include "ash/system/tray/system_tray_delegate.h"
14 #include "ash/system/tray/system_tray_notifier.h"
15 #include "ash/system/tray/tray_constants.h"
16 #include "ash/system/tray/tray_item_view.h"
17 #include "ash/system/tray/tray_popup_header_button.h"
18 #include "base/i18n/time_formatting.h"
19 #include "base/strings/stringprintf.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "base/time/time.h"
22 #include "base/timer/timer.h"
23 #include "grit/ash_resources.h"
24 #include "grit/ash_strings.h"
25 #include "third_party/icu/source/i18n/unicode/datefmt.h"
26 #include "third_party/icu/source/i18n/unicode/fieldpos.h"
27 #include "third_party/icu/source/i18n/unicode/fmtable.h"
28 #include "third_party/skia/include/core/SkRect.h"
29 #include "ui/base/l10n/l10n_util.h"
30 #include "ui/base/resource/resource_bundle.h"
31 #include "ui/gfx/image/image.h"
32 #include "ui/gfx/image/image_skia.h"
33 #include "ui/gfx/size.h"
34 #include "ui/views/controls/button/button.h"
35 #include "ui/views/controls/image_view.h"
36 #include "ui/views/controls/label.h"
37 #include "ui/views/layout/box_layout.h"
38 #include "ui/views/layout/fill_layout.h"
39 #include "ui/views/painter.h"
40 #include "ui/views/view.h"
41 #include "ui/views/widget/widget.h"
42
43 #if defined(OS_CHROMEOS)
44 #include "ash/system/chromeos/system_clock_observer.h"
45 #endif
46
47 namespace {
48
49 const int kPaddingVertical = 19;
50
51 }  // namespace
52
53 namespace ash {
54 namespace internal {
55
56 class DateDefaultView : public views::View,
57                         public views::ButtonListener {
58  public:
59   explicit DateDefaultView(ash::user::LoginStatus login)
60       : help_(NULL),
61         shutdown_(NULL),
62         lock_(NULL),
63         date_view_(NULL) {
64     SetLayoutManager(new views::FillLayout);
65
66     date_view_ = new tray::DateView();
67     date_view_->SetBorder(views::Border::CreateEmptyBorder(
68         kPaddingVertical, ash::kTrayPopupPaddingHorizontal, 0, 0));
69     SpecialPopupRow* view = new SpecialPopupRow();
70     view->SetContent(date_view_);
71     AddChildView(view);
72
73     if (login == ash::user::LOGGED_IN_LOCKED ||
74         login == ash::user::LOGGED_IN_NONE)
75       return;
76
77     date_view_->SetActionable(true);
78
79     help_ = new TrayPopupHeaderButton(this,
80         IDR_AURA_UBER_TRAY_HELP,
81         IDR_AURA_UBER_TRAY_HELP,
82         IDR_AURA_UBER_TRAY_HELP_HOVER,
83         IDR_AURA_UBER_TRAY_HELP_HOVER,
84         IDS_ASH_STATUS_TRAY_HELP);
85     help_->SetTooltipText(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_HELP));
86     view->AddButton(help_);
87
88 #if !defined(OS_WIN)
89     if (login != ash::user::LOGGED_IN_LOCKED &&
90         login != ash::user::LOGGED_IN_RETAIL_MODE) {
91       shutdown_ = new TrayPopupHeaderButton(this,
92           IDR_AURA_UBER_TRAY_SHUTDOWN,
93           IDR_AURA_UBER_TRAY_SHUTDOWN,
94           IDR_AURA_UBER_TRAY_SHUTDOWN_HOVER,
95           IDR_AURA_UBER_TRAY_SHUTDOWN_HOVER,
96           IDS_ASH_STATUS_TRAY_SHUTDOWN);
97       shutdown_->SetTooltipText(
98           l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_SHUTDOWN));
99       view->AddButton(shutdown_);
100     }
101
102     if (ash::Shell::GetInstance()->session_state_delegate()->CanLockScreen()) {
103       lock_ = new TrayPopupHeaderButton(this,
104           IDR_AURA_UBER_TRAY_LOCKSCREEN,
105           IDR_AURA_UBER_TRAY_LOCKSCREEN,
106           IDR_AURA_UBER_TRAY_LOCKSCREEN_HOVER,
107           IDR_AURA_UBER_TRAY_LOCKSCREEN_HOVER,
108           IDS_ASH_STATUS_TRAY_LOCK);
109       lock_->SetTooltipText(
110           l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_LOCK));
111       view->AddButton(lock_);
112     }
113 #endif  // !defined(OS_WIN)
114   }
115
116   virtual ~DateDefaultView() {}
117
118   views::View* GetHelpButtonView() const {
119     return help_;
120   }
121
122   tray::DateView* GetDateView() const {
123     return date_view_;
124   }
125
126  private:
127   // Overridden from views::ButtonListener.
128   virtual void ButtonPressed(views::Button* sender,
129                              const ui::Event& event) OVERRIDE {
130     ash::Shell* shell = ash::Shell::GetInstance();
131     ash::SystemTrayDelegate* tray_delegate = shell->system_tray_delegate();
132     if (sender == help_) {
133       shell->metrics()->RecordUserMetricsAction(ash::UMA_TRAY_HELP);
134       tray_delegate->ShowHelp();
135     } else if (sender == shutdown_) {
136       shell->metrics()->RecordUserMetricsAction(ash::UMA_TRAY_SHUT_DOWN);
137       tray_delegate->ShutDown();
138     } else if (sender == lock_) {
139       shell->metrics()->RecordUserMetricsAction(ash::UMA_TRAY_LOCK_SCREEN);
140       tray_delegate->RequestLockScreen();
141     } else {
142       NOTREACHED();
143     }
144   }
145
146   TrayPopupHeaderButton* help_;
147   TrayPopupHeaderButton* shutdown_;
148   TrayPopupHeaderButton* lock_;
149   tray::DateView* date_view_;
150
151   DISALLOW_COPY_AND_ASSIGN(DateDefaultView);
152 };
153
154 TrayDate::TrayDate(SystemTray* system_tray)
155     : SystemTrayItem(system_tray),
156       time_tray_(NULL),
157       default_view_(NULL) {
158 #if defined(OS_CHROMEOS)
159   system_clock_observer_.reset(new SystemClockObserver());
160 #endif
161   Shell::GetInstance()->system_tray_notifier()->AddClockObserver(this);
162 }
163
164 TrayDate::~TrayDate() {
165   Shell::GetInstance()->system_tray_notifier()->RemoveClockObserver(this);
166 }
167
168 views::View* TrayDate::GetHelpButtonView() const {
169   if (!default_view_)
170     return NULL;
171   return default_view_->GetHelpButtonView();
172 }
173
174 views::View* TrayDate::CreateTrayView(user::LoginStatus status) {
175   CHECK(time_tray_ == NULL);
176   ClockLayout clock_layout =
177       (system_tray()->shelf_alignment() == SHELF_ALIGNMENT_BOTTOM ||
178        system_tray()->shelf_alignment() == SHELF_ALIGNMENT_TOP) ?
179           HORIZONTAL_CLOCK : VERTICAL_CLOCK;
180   time_tray_ = new tray::TimeView(clock_layout);
181   views::View* view = new TrayItemView(this);
182   view->AddChildView(time_tray_);
183   return view;
184 }
185
186 views::View* TrayDate::CreateDefaultView(user::LoginStatus status) {
187   default_view_ = new DateDefaultView(status);
188   return default_view_;
189 }
190
191 views::View* TrayDate::CreateDetailedView(user::LoginStatus status) {
192   return NULL;
193 }
194
195 void TrayDate::DestroyTrayView() {
196   time_tray_ = NULL;
197 }
198
199 void TrayDate::DestroyDefaultView() {
200   default_view_ = NULL;
201 }
202
203 void TrayDate::DestroyDetailedView() {
204 }
205
206 void TrayDate::UpdateAfterLoginStatusChange(user::LoginStatus status) {
207 }
208
209 void TrayDate::UpdateAfterShelfAlignmentChange(ShelfAlignment alignment) {
210   if (time_tray_) {
211     ClockLayout clock_layout = (alignment == SHELF_ALIGNMENT_BOTTOM ||
212         alignment == SHELF_ALIGNMENT_TOP) ?
213             HORIZONTAL_CLOCK : VERTICAL_CLOCK;
214     time_tray_->UpdateClockLayout(clock_layout);
215   }
216 }
217
218 void TrayDate::OnDateFormatChanged() {
219   if (time_tray_)
220     time_tray_->UpdateTimeFormat();
221   if (default_view_)
222     default_view_->GetDateView()->UpdateTimeFormat();
223 }
224
225 void TrayDate::OnSystemClockTimeUpdated() {
226   if (time_tray_)
227     time_tray_->UpdateTimeFormat();
228   if (default_view_)
229     default_view_->GetDateView()->UpdateTimeFormat();
230 }
231
232 void TrayDate::Refresh() {
233   if (time_tray_)
234     time_tray_->UpdateText();
235 }
236
237 }  // namespace internal
238 }  // namespace ash