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 "chrome/browser/ui/views/download/download_in_progress_dialog_view.h"
6
7#include <algorithm>
8
9#include "base/strings/string_number_conversions.h"
10#include "chrome/browser/ui/views/constrained_window_views.h"
11#include "chrome/grit/chromium_strings.h"
12#include "chrome/grit/generated_resources.h"
13#include "ui/base/l10n/l10n_util.h"
14#include "ui/gfx/size.h"
15#include "ui/views/border.h"
16#include "ui/views/controls/message_box_view.h"
17#include "ui/views/layout/grid_layout.h"
18#include "ui/views/widget/widget.h"
19
20// static
21void DownloadInProgressDialogView::Show(
22    gfx::NativeWindow parent,
23    int download_count,
24    Browser::DownloadClosePreventionType dialog_type,
25    bool app_modal,
26    const base::Callback<void(bool)>& callback) {
27  DownloadInProgressDialogView* window = new DownloadInProgressDialogView(
28      download_count, dialog_type, app_modal, callback);
29  CreateBrowserModalDialogViews(window, parent)->Show();
30}
31
32DownloadInProgressDialogView::DownloadInProgressDialogView(
33    int download_count,
34    Browser::DownloadClosePreventionType dialog_type,
35    bool app_modal,
36    const base::Callback<void(bool)>& callback)
37    : app_modal_(app_modal),
38      callback_(callback),
39      message_box_view_(NULL) {
40  base::string16 explanation_text;
41  switch (dialog_type) {
42    case Browser::DOWNLOAD_CLOSE_BROWSER_SHUTDOWN:
43      if (download_count == 1) {
44        title_text_ = l10n_util::GetStringUTF16(
45            IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_TITLE);
46        explanation_text = l10n_util::GetStringUTF16(
47            IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_EXPLANATION);
48      } else {
49        title_text_ = l10n_util::GetStringUTF16(
50            IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_TITLE);
51        explanation_text = l10n_util::GetStringUTF16(
52            IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_EXPLANATION);
53      }
54      ok_button_text_ = l10n_util::GetStringUTF16(
55          IDS_DOWNLOAD_REMOVE_CONFIRM_OK_BUTTON_LABEL);
56      break;
57    case Browser::DOWNLOAD_CLOSE_LAST_WINDOW_IN_INCOGNITO_PROFILE:
58      if (download_count == 1) {
59        title_text_ = l10n_util::GetStringUTF16(
60            IDS_SINGLE_INCOGNITO_DOWNLOAD_REMOVE_CONFIRM_TITLE);
61        explanation_text = l10n_util::GetStringUTF16(
62            IDS_SINGLE_INCOGNITO_DOWNLOAD_REMOVE_CONFIRM_EXPLANATION);
63      } else {
64        title_text_ = l10n_util::GetStringUTF16(
65            IDS_MULTIPLE_INCOGNITO_DOWNLOADS_REMOVE_CONFIRM_TITLE);
66        explanation_text = l10n_util::GetStringUTF16(
67            IDS_MULTIPLE_INCOGNITO_DOWNLOADS_REMOVE_CONFIRM_EXPLANATION);
68      }
69      ok_button_text_ = l10n_util::GetStringUTF16(
70          IDS_INCOGNITO_DOWNLOAD_REMOVE_CONFIRM_OK_BUTTON_LABEL);
71      break;
72    default:
73      // This dialog should have been created within the same thread invocation
74      // as the original test that lead to us, so it should always not be ok
75      // to close.
76      NOTREACHED();
77  }
78  cancel_button_text_ = l10n_util::GetStringUTF16(
79      IDS_DOWNLOAD_REMOVE_CONFIRM_CANCEL_BUTTON_LABEL);
80
81  message_box_view_ = new views::MessageBoxView(
82      views::MessageBoxView::InitParams(explanation_text));
83}
84
85DownloadInProgressDialogView::~DownloadInProgressDialogView() {}
86
87int DownloadInProgressDialogView::GetDefaultDialogButton() const {
88  return ui::DIALOG_BUTTON_CANCEL;
89}
90
91base::string16 DownloadInProgressDialogView::GetDialogButtonLabel(
92    ui::DialogButton button) const {
93  return (button == ui::DIALOG_BUTTON_OK) ?
94      ok_button_text_ : cancel_button_text_;
95}
96
97bool DownloadInProgressDialogView::Cancel() {
98  callback_.Run(false);
99  return true;
100}
101
102bool DownloadInProgressDialogView::Accept() {
103  callback_.Run(true);
104  return true;
105}
106
107ui::ModalType DownloadInProgressDialogView::GetModalType() const {
108  return app_modal_ ? ui::MODAL_TYPE_SYSTEM : ui::MODAL_TYPE_WINDOW;
109}
110
111base::string16 DownloadInProgressDialogView::GetWindowTitle() const {
112  return title_text_;
113}
114
115void DownloadInProgressDialogView::DeleteDelegate() {
116  delete this;
117}
118
119views::Widget* DownloadInProgressDialogView::GetWidget() {
120  return message_box_view_->GetWidget();
121}
122
123const views::Widget* DownloadInProgressDialogView::GetWidget() const {
124  return message_box_view_->GetWidget();
125}
126
127views::View* DownloadInProgressDialogView::GetContentsView() {
128  return message_box_view_;
129}
130