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/chromeos/session/logout_confirmation_dialog.h"
6
7#include "ash/shell.h"
8#include "ash/system/chromeos/session/logout_confirmation_controller.h"
9#include "ash/system/tray/tray_constants.h"
10#include "base/location.h"
11#include "base/time/tick_clock.h"
12#include "grit/ash_strings.h"
13#include "ui/base/l10n/l10n_util.h"
14#include "ui/base/l10n/time_format.h"
15#include "ui/base/ui_base_types.h"
16#include "ui/gfx/text_constants.h"
17#include "ui/views/border.h"
18#include "ui/views/controls/label.h"
19#include "ui/views/layout/fill_layout.h"
20#include "ui/views/widget/widget.h"
21
22namespace ash {
23namespace {
24
25const int kCountdownUpdateIntervalMs = 1000;  // 1 second.
26
27const int kHalfSecondInMs = 500;  // Half a second.
28
29}  // namespace
30
31LogoutConfirmationDialog::LogoutConfirmationDialog(
32    LogoutConfirmationController* controller,
33    base::TimeTicks logout_time)
34    : controller_(controller),
35      logout_time_(logout_time) {
36  SetLayoutManager(new views::FillLayout());
37
38  label_ = new views::Label;
39  label_->SetBorder(views::Border::CreateEmptyBorder(
40      0, kTrayPopupPaddingHorizontal, 0, kTrayPopupPaddingHorizontal));
41  label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
42  label_->SetMultiLine(true);
43  AddChildView(label_);
44
45  UpdateLabel();
46
47  CreateDialogWidget(this, ash::Shell::GetPrimaryRootWindow(), NULL);
48  GetWidget()->Show();
49
50  update_timer_.Start(
51      FROM_HERE,
52      base::TimeDelta::FromMilliseconds(kCountdownUpdateIntervalMs),
53      this,
54      &LogoutConfirmationDialog::UpdateLabel);
55}
56
57LogoutConfirmationDialog::~LogoutConfirmationDialog() {
58}
59
60void LogoutConfirmationDialog::Update(base::TimeTicks logout_time) {
61  logout_time_ = logout_time;
62  UpdateLabel();
63}
64
65bool LogoutConfirmationDialog::Accept() {
66  logout_time_ = controller_->clock()->NowTicks();
67  UpdateLabel();
68  controller_->OnLogoutConfirmed();
69  return true;
70}
71
72ui::ModalType LogoutConfirmationDialog::GetModalType() const {
73  return ui::MODAL_TYPE_SYSTEM;
74}
75
76base::string16 LogoutConfirmationDialog::GetWindowTitle() const {
77  return l10n_util::GetStringUTF16(IDS_ASH_LOGOUT_CONFIRMATION_TITLE);
78}
79
80base::string16 LogoutConfirmationDialog::GetDialogButtonLabel(
81    ui::DialogButton button) const {
82  if (button == ui::DIALOG_BUTTON_OK)
83    return l10n_util::GetStringUTF16(IDS_ASH_LOGOUT_CONFIRMATION_BUTTON);
84  return views::DialogDelegateView::GetDialogButtonLabel(button);
85}
86
87void LogoutConfirmationDialog::OnClosed() {
88  update_timer_.Stop();
89  controller_->OnDialogClosed();
90}
91
92void LogoutConfirmationDialog::DeleteDelegate() {
93  delete this;
94}
95
96void LogoutConfirmationDialog::UpdateLabel() {
97  const base::TimeDelta time_remaining =
98      logout_time_ - controller_->clock()->NowTicks();
99  if (time_remaining >= base::TimeDelta::FromMilliseconds(kHalfSecondInMs)) {
100    label_->SetText(l10n_util::GetStringFUTF16(
101        IDS_ASH_LOGOUT_CONFIRMATION_WARNING,
102        ui::TimeFormat::Detailed(ui::TimeFormat::FORMAT_DURATION,
103                                 ui::TimeFormat::LENGTH_LONG,
104                                 10,
105                                 time_remaining)));
106  } else {
107    label_->SetText(l10n_util::GetStringUTF16(
108        IDS_ASH_LOGOUT_CONFIRMATION_WARNING_NOW));
109    update_timer_.Stop();
110  }
111}
112
113}  // namespace ash
114