tray_date.cc revision 5c02ac1a9c1b504631c0a3d2b6e737b5d738bae1
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/shell.h"
8#include "ash/system/date/date_default_view.h"
9#include "ash/system/date/date_view.h"
10#include "ash/system/tray/system_tray.h"
11#include "ash/system/tray/system_tray_notifier.h"
12#include "ash/system/tray/tray_item_view.h"
13
14#if defined(OS_CHROMEOS)
15#include "ash/system/chromeos/system_clock_observer.h"
16#endif
17
18namespace ash {
19
20TrayDate::TrayDate(SystemTray* system_tray)
21    : SystemTrayItem(system_tray),
22      time_tray_(NULL),
23      default_view_(NULL),
24      login_status_(user::LOGGED_IN_NONE) {
25#if defined(OS_CHROMEOS)
26  system_clock_observer_.reset(new SystemClockObserver());
27#endif
28  Shell::GetInstance()->system_tray_notifier()->AddClockObserver(this);
29}
30
31TrayDate::~TrayDate() {
32  Shell::GetInstance()->system_tray_notifier()->RemoveClockObserver(this);
33}
34
35views::View* TrayDate::GetHelpButtonView() const {
36  if (!default_view_)
37    return NULL;
38  return default_view_->GetHelpButtonView();
39}
40
41const tray::TimeView* TrayDate::GetTimeTrayForTesting() const {
42  return time_tray_;
43}
44
45const DateDefaultView* TrayDate::GetDefaultViewForTesting() const {
46  return default_view_;
47}
48
49views::View* TrayDate::CreateDefaultViewForTesting(user::LoginStatus status) {
50  return CreateDefaultView(status);
51}
52
53views::View* TrayDate::CreateTrayView(user::LoginStatus status) {
54  CHECK(time_tray_ == NULL);
55  ClockLayout clock_layout =
56      (system_tray()->shelf_alignment() == SHELF_ALIGNMENT_BOTTOM ||
57       system_tray()->shelf_alignment() == SHELF_ALIGNMENT_TOP) ?
58          HORIZONTAL_CLOCK : VERTICAL_CLOCK;
59  time_tray_ = new tray::TimeView(clock_layout);
60  views::View* view = new TrayItemView(this);
61  view->AddChildView(time_tray_);
62  return view;
63}
64
65views::View* TrayDate::CreateDefaultView(user::LoginStatus status) {
66  default_view_ = new DateDefaultView(status);
67
68#if defined(OS_CHROMEOS)
69  // Save the login status we created the view with.
70  login_status_ = status;
71
72  OnSystemClockCanSetTimeChanged(system_clock_observer_->can_set_time());
73#endif
74  return default_view_;
75}
76
77views::View* TrayDate::CreateDetailedView(user::LoginStatus status) {
78  return NULL;
79}
80
81void TrayDate::DestroyTrayView() {
82  time_tray_ = NULL;
83}
84
85void TrayDate::DestroyDefaultView() {
86  default_view_ = NULL;
87}
88
89void TrayDate::DestroyDetailedView() {
90}
91
92void TrayDate::UpdateAfterLoginStatusChange(user::LoginStatus status) {
93}
94
95void TrayDate::UpdateAfterShelfAlignmentChange(ShelfAlignment alignment) {
96  if (time_tray_) {
97    ClockLayout clock_layout = (alignment == SHELF_ALIGNMENT_BOTTOM ||
98        alignment == SHELF_ALIGNMENT_TOP) ?
99            HORIZONTAL_CLOCK : VERTICAL_CLOCK;
100    time_tray_->UpdateClockLayout(clock_layout);
101  }
102}
103
104void TrayDate::OnDateFormatChanged() {
105  if (time_tray_)
106    time_tray_->UpdateTimeFormat();
107  if (default_view_)
108    default_view_->GetDateView()->UpdateTimeFormat();
109}
110
111void TrayDate::OnSystemClockTimeUpdated() {
112  if (time_tray_)
113    time_tray_->UpdateTimeFormat();
114  if (default_view_)
115    default_view_->GetDateView()->UpdateTimeFormat();
116}
117
118void TrayDate::OnSystemClockCanSetTimeChanged(bool can_set_time) {
119  // Outside of a logged-in session, the date button should launch the set time
120  // dialog if the time can be set.
121  if (default_view_ && login_status_ == user::LOGGED_IN_NONE) {
122    default_view_->GetDateView()->SetAction(
123        can_set_time ? TrayDate::SET_SYSTEM_TIME : TrayDate::NONE);
124  }
125}
126
127void TrayDate::Refresh() {
128  if (time_tray_)
129    time_tray_->UpdateText();
130}
131
132}  // namespace ash
133