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