1// Copyright 2014 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/date_default_view.h"
6
7#include "ash/metrics/user_metrics_recorder.h"
8#include "ash/session/session_state_delegate.h"
9#include "ash/shell.h"
10#include "ash/system/date/date_view.h"
11#include "ash/system/tray/special_popup_row.h"
12#include "ash/system/tray/system_tray_delegate.h"
13#include "ash/system/tray/tray_constants.h"
14#include "ash/system/tray/tray_popup_header_button.h"
15#include "grit/ash_resources.h"
16#include "grit/ash_strings.h"
17#include "ui/base/l10n/l10n_util.h"
18#include "ui/views/border.h"
19#include "ui/views/controls/button/button.h"
20#include "ui/views/layout/fill_layout.h"
21#include "ui/views/view.h"
22
23namespace {
24
25const int kPaddingVertical = 19;
26
27}  // namespace
28
29namespace ash {
30
31DateDefaultView::DateDefaultView(ash::user::LoginStatus login)
32    : help_(NULL),
33      shutdown_(NULL),
34      lock_(NULL),
35      date_view_(NULL) {
36  SetLayoutManager(new views::FillLayout);
37
38  date_view_ = new tray::DateView();
39  date_view_->SetBorder(views::Border::CreateEmptyBorder(
40      kPaddingVertical, ash::kTrayPopupPaddingHorizontal, 0, 0));
41  SpecialPopupRow* view = new SpecialPopupRow();
42  view->SetContent(date_view_);
43  AddChildView(view);
44
45  bool userAddingRunning = ash::Shell::GetInstance()
46                               ->session_state_delegate()
47                               ->IsInSecondaryLoginScreen();
48
49  if (login == user::LOGGED_IN_LOCKED ||
50      login == user::LOGGED_IN_NONE || userAddingRunning)
51    return;
52
53  date_view_->SetAction(TrayDate::SHOW_DATE_SETTINGS);
54
55  help_ = new TrayPopupHeaderButton(this,
56                                    IDR_AURA_UBER_TRAY_HELP,
57                                    IDR_AURA_UBER_TRAY_HELP,
58                                    IDR_AURA_UBER_TRAY_HELP_HOVER,
59                                    IDR_AURA_UBER_TRAY_HELP_HOVER,
60                                    IDS_ASH_STATUS_TRAY_HELP);
61  help_->SetTooltipText(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_HELP));
62  view->AddButton(help_);
63
64#if !defined(OS_WIN)
65  if (login != ash::user::LOGGED_IN_LOCKED &&
66      login != ash::user::LOGGED_IN_RETAIL_MODE) {
67    shutdown_ = new TrayPopupHeaderButton(this,
68                                          IDR_AURA_UBER_TRAY_SHUTDOWN,
69                                          IDR_AURA_UBER_TRAY_SHUTDOWN,
70                                          IDR_AURA_UBER_TRAY_SHUTDOWN_HOVER,
71                                          IDR_AURA_UBER_TRAY_SHUTDOWN_HOVER,
72                                          IDS_ASH_STATUS_TRAY_SHUTDOWN);
73    shutdown_->SetTooltipText(
74        l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_SHUTDOWN));
75    view->AddButton(shutdown_);
76  }
77
78  if (ash::Shell::GetInstance()->session_state_delegate()->CanLockScreen()) {
79    lock_ = new TrayPopupHeaderButton(this,
80                                      IDR_AURA_UBER_TRAY_LOCKSCREEN,
81                                      IDR_AURA_UBER_TRAY_LOCKSCREEN,
82                                      IDR_AURA_UBER_TRAY_LOCKSCREEN_HOVER,
83                                      IDR_AURA_UBER_TRAY_LOCKSCREEN_HOVER,
84                                      IDS_ASH_STATUS_TRAY_LOCK);
85    lock_->SetTooltipText(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_LOCK));
86    view->AddButton(lock_);
87  }
88#endif  // !defined(OS_WIN)
89}
90
91DateDefaultView::~DateDefaultView() {
92}
93
94views::View* DateDefaultView::GetHelpButtonView() {
95  return help_;
96}
97
98tray::DateView* DateDefaultView::GetDateView() {
99  return date_view_;
100}
101
102const tray::DateView* DateDefaultView::GetDateView() const {
103  return date_view_;
104}
105
106void DateDefaultView::ButtonPressed(views::Button* sender,
107                                    const ui::Event& event) {
108  ash::Shell* shell = ash::Shell::GetInstance();
109  ash::SystemTrayDelegate* tray_delegate = shell->system_tray_delegate();
110  if (sender == help_) {
111    shell->metrics()->RecordUserMetricsAction(ash::UMA_TRAY_HELP);
112    tray_delegate->ShowHelp();
113  } else if (sender == shutdown_) {
114    shell->metrics()->RecordUserMetricsAction(ash::UMA_TRAY_SHUT_DOWN);
115    tray_delegate->ShutDown();
116  } else if (sender == lock_) {
117    shell->metrics()->RecordUserMetricsAction(ash::UMA_TRAY_LOCK_SCREEN);
118    tray_delegate->RequestLockScreen();
119  } else {
120    NOTREACHED();
121  }
122}
123
124}  // namespace ash
125