1// Copyright (c) 2011 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 "base/string_number_conversions.h"
8#include "base/utf_string_conversions.h"
9#include "chrome/browser/download/download_manager.h"
10#include "chrome/browser/profiles/profile.h"
11#include "chrome/browser/ui/browser.h"
12#include "grit/chromium_strings.h"
13#include "grit/generated_resources.h"
14#include "grit/locale_settings.h"
15#include "ui/base/l10n/l10n_util.h"
16#include "ui/base/resource/resource_bundle.h"
17#include "ui/gfx/size.h"
18#include "views/border.h"
19#include "views/controls/label.h"
20#include "views/layout/grid_layout.h"
21#include "views/window/window.h"
22
23DownloadInProgressDialogView::DownloadInProgressDialogView(Browser* browser)
24    : browser_(browser),
25      product_name_(l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)) {
26  int download_count = browser->profile()->GetDownloadManager()->
27      in_progress_count();
28
29  std::wstring warning_text;
30  std::wstring explanation_text;
31  if (download_count == 1) {
32    warning_text = UTF16ToWide(l10n_util::GetStringFUTF16(
33        IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_WARNING,
34        product_name_));
35    explanation_text = UTF16ToWide(l10n_util::GetStringFUTF16(
36        IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_EXPLANATION,
37        product_name_));
38    ok_button_text_ = UTF16ToWide(l10n_util::GetStringUTF16(
39        IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_OK_BUTTON_LABEL));
40    cancel_button_text_ = UTF16ToWide(l10n_util::GetStringUTF16(
41        IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_CANCEL_BUTTON_LABEL));
42  } else {
43    warning_text = UTF16ToWide(l10n_util::GetStringFUTF16(
44        IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_WARNING,
45        product_name_,
46        UTF8ToUTF16(base::IntToString(download_count))));
47    explanation_text = UTF16ToWide(l10n_util::GetStringFUTF16(
48        IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_EXPLANATION,
49        product_name_));
50    ok_button_text_ = UTF16ToWide(l10n_util::GetStringUTF16(
51        IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_OK_BUTTON_LABEL));
52    cancel_button_text_ = UTF16ToWide(l10n_util::GetStringUTF16(
53        IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_CANCEL_BUTTON_LABEL));
54  }
55
56  // There are two lines of text: the bold warning label and the text
57  // explanation label.
58  views::GridLayout* layout = new views::GridLayout(this);
59  SetLayoutManager(layout);
60  const int columnset_id = 0;
61  views::ColumnSet* column_set = layout->AddColumnSet(columnset_id);
62  column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::LEADING, 1,
63                        views::GridLayout::USE_PREF, 0, 0);
64
65  gfx::Font bold_font =
66      ResourceBundle::GetSharedInstance().GetFont(
67          ResourceBundle::BaseFont).DeriveFont(0, gfx::Font::BOLD);
68  warning_ = new views::Label(warning_text, bold_font);
69  warning_->SetMultiLine(true);
70  warning_->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
71  warning_->set_border(views::Border::CreateEmptyBorder(10, 10, 10, 10));
72  layout->StartRow(0, columnset_id);
73  layout->AddView(warning_);
74
75  explanation_ = new views::Label(explanation_text);
76  explanation_->SetMultiLine(true);
77  explanation_->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
78  explanation_->set_border(views::Border::CreateEmptyBorder(10, 10, 10, 10));
79  layout->StartRow(0, columnset_id);
80  layout->AddView(explanation_);
81
82  dialog_dimensions_ = views::Window::GetLocalizedContentsSize(
83      IDS_DOWNLOAD_IN_PROGRESS_WIDTH_CHARS,
84      IDS_DOWNLOAD_IN_PROGRESS_MINIMUM_HEIGHT_LINES);
85  const int height =
86      warning_->GetHeightForWidth(dialog_dimensions_.width()) +
87      explanation_->GetHeightForWidth(dialog_dimensions_.width());
88  dialog_dimensions_.set_height(std::max(height,
89                                         dialog_dimensions_.height()));
90}
91
92DownloadInProgressDialogView::~DownloadInProgressDialogView() {}
93
94gfx::Size DownloadInProgressDialogView::GetPreferredSize() {
95  return dialog_dimensions_;
96}
97
98std::wstring DownloadInProgressDialogView::GetDialogButtonLabel(
99    MessageBoxFlags::DialogButton button) const {
100  if (button == MessageBoxFlags::DIALOGBUTTON_OK)
101    return ok_button_text_;
102
103  DCHECK_EQ(MessageBoxFlags::DIALOGBUTTON_CANCEL, button);
104  return cancel_button_text_;
105}
106
107int DownloadInProgressDialogView::GetDefaultDialogButton() const {
108  return MessageBoxFlags::DIALOGBUTTON_CANCEL;
109}
110
111bool DownloadInProgressDialogView::Cancel() {
112  browser_->InProgressDownloadResponse(false);
113  return true;
114}
115
116bool DownloadInProgressDialogView::Accept() {
117  browser_->InProgressDownloadResponse(true);
118  return true;
119}
120
121bool DownloadInProgressDialogView::IsModal() const {
122  return true;
123}
124
125std::wstring DownloadInProgressDialogView::GetWindowTitle() const {
126  return UTF16ToWideHack(product_name_);
127}
128
129views::View* DownloadInProgressDialogView::GetContentsView() {
130  return this;
131}
132