Home | History | Annotate | Download | only in download
      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/download/download_shelf.h"
      6 
      7 #include "base/file_util.h"
      8 #include "chrome/browser/download/download_item.h"
      9 #include "chrome/browser/download/download_item_model.h"
     10 #include "chrome/browser/download/download_manager.h"
     11 #include "chrome/browser/download/download_util.h"
     12 #include "chrome/browser/metrics/user_metrics.h"
     13 #include "chrome/browser/ui/webui/downloads_ui.h"
     14 #include "chrome/common/url_constants.h"
     15 #include "grit/generated_resources.h"
     16 #include "ui/base/l10n/l10n_util.h"
     17 
     18 // DownloadShelfContextMenu ----------------------------------------------------
     19 
     20 DownloadShelfContextMenu::DownloadShelfContextMenu(
     21     BaseDownloadItemModel* download_model)
     22     : download_(download_model->download()),
     23       model_(download_model) {
     24 }
     25 
     26 DownloadShelfContextMenu::~DownloadShelfContextMenu() {
     27 }
     28 
     29 DownloadItem* DownloadShelfContextMenu::download() const {
     30   return download_;
     31 }
     32 
     33 bool DownloadShelfContextMenu::IsCommandIdChecked(int command_id) const {
     34   switch (command_id) {
     35     case OPEN_WHEN_COMPLETE:
     36       return download_->open_when_complete();
     37     case ALWAYS_OPEN_TYPE:
     38       return download_->ShouldOpenFileBasedOnExtension();
     39     case TOGGLE_PAUSE:
     40       return download_->is_paused();
     41   }
     42   return false;
     43 }
     44 
     45 string16 DownloadShelfContextMenu::GetLabelForCommandId(int command_id) const {
     46   switch (command_id) {
     47     case SHOW_IN_FOLDER:
     48       return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_SHOW);
     49     case OPEN_WHEN_COMPLETE:
     50       if (download_->IsInProgress())
     51         return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_OPEN_WHEN_COMPLETE);
     52       return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_OPEN);
     53     case ALWAYS_OPEN_TYPE:
     54       return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_ALWAYS_OPEN_TYPE);
     55     case CANCEL:
     56       return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_CANCEL);
     57     case TOGGLE_PAUSE: {
     58       if (download_->is_paused())
     59         return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_RESUME_ITEM);
     60       else
     61         return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_PAUSE_ITEM);
     62     }
     63     default:
     64       NOTREACHED();
     65   }
     66   return string16();
     67 }
     68 
     69 bool DownloadShelfContextMenu::IsCommandIdEnabled(int command_id) const {
     70   switch (command_id) {
     71     case SHOW_IN_FOLDER:
     72     case OPEN_WHEN_COMPLETE:
     73       return !download_->IsCancelled();
     74     case ALWAYS_OPEN_TYPE:
     75       return download_->CanOpenDownload();
     76     case CANCEL:
     77       return download_->IsPartialDownload();
     78     case TOGGLE_PAUSE:
     79       return download_->IsInProgress();
     80     default:
     81       return command_id > 0 && command_id < MENU_LAST;
     82   }
     83 }
     84 
     85 void DownloadShelfContextMenu::ExecuteCommand(int command_id) {
     86   switch (command_id) {
     87     case SHOW_IN_FOLDER:
     88       download_->ShowDownloadInShell();
     89       break;
     90     case OPEN_WHEN_COMPLETE:
     91       download_->OpenDownload();
     92       break;
     93     case ALWAYS_OPEN_TYPE: {
     94       download_->OpenFilesBasedOnExtension(
     95           !IsCommandIdChecked(ALWAYS_OPEN_TYPE));
     96       break;
     97     }
     98     case CANCEL:
     99       model_->CancelTask();
    100       break;
    101     case TOGGLE_PAUSE:
    102       // It is possible for the download to complete before the user clicks the
    103       // menu item, recheck if the download is in progress state before toggling
    104       // pause.
    105       if (download_->IsPartialDownload())
    106         download_->TogglePause();
    107       break;
    108     default:
    109       NOTREACHED();
    110   }
    111 }
    112 
    113 bool DownloadShelfContextMenu::GetAcceleratorForCommandId(
    114     int command_id, ui::Accelerator* accelerator) {
    115   return false;
    116 }
    117 
    118 bool DownloadShelfContextMenu::IsItemForCommandIdDynamic(
    119     int command_id) const {
    120   return command_id == TOGGLE_PAUSE;
    121 }
    122 
    123 ui::SimpleMenuModel* DownloadShelfContextMenu::GetInProgressMenuModel() {
    124   if (in_progress_download_menu_model_.get())
    125     return in_progress_download_menu_model_.get();
    126 
    127   in_progress_download_menu_model_.reset(new ui::SimpleMenuModel(this));
    128 
    129   in_progress_download_menu_model_->AddCheckItemWithStringId(
    130       OPEN_WHEN_COMPLETE, IDS_DOWNLOAD_MENU_OPEN_WHEN_COMPLETE);
    131   in_progress_download_menu_model_->AddCheckItemWithStringId(
    132       ALWAYS_OPEN_TYPE, IDS_DOWNLOAD_MENU_ALWAYS_OPEN_TYPE);
    133   in_progress_download_menu_model_->AddSeparator();
    134   in_progress_download_menu_model_->AddItemWithStringId(
    135       TOGGLE_PAUSE, IDS_DOWNLOAD_MENU_PAUSE_ITEM);
    136   in_progress_download_menu_model_->AddItemWithStringId(
    137       SHOW_IN_FOLDER, IDS_DOWNLOAD_MENU_SHOW);
    138   in_progress_download_menu_model_->AddSeparator();
    139   in_progress_download_menu_model_->AddItemWithStringId(
    140       CANCEL, IDS_DOWNLOAD_MENU_CANCEL);
    141 
    142   return in_progress_download_menu_model_.get();
    143 }
    144 
    145 ui::SimpleMenuModel* DownloadShelfContextMenu::GetFinishedMenuModel() {
    146   if (finished_download_menu_model_.get())
    147     return finished_download_menu_model_.get();
    148 
    149   finished_download_menu_model_.reset(new ui::SimpleMenuModel(this));
    150 
    151   finished_download_menu_model_->AddItemWithStringId(
    152       OPEN_WHEN_COMPLETE, IDS_DOWNLOAD_MENU_OPEN);
    153   finished_download_menu_model_->AddCheckItemWithStringId(
    154       ALWAYS_OPEN_TYPE, IDS_DOWNLOAD_MENU_ALWAYS_OPEN_TYPE);
    155   finished_download_menu_model_->AddSeparator();
    156   finished_download_menu_model_->AddItemWithStringId(
    157       SHOW_IN_FOLDER, IDS_DOWNLOAD_MENU_SHOW);
    158   finished_download_menu_model_->AddSeparator();
    159   finished_download_menu_model_->AddItemWithStringId(
    160       CANCEL, IDS_DOWNLOAD_MENU_CANCEL);
    161 
    162   return finished_download_menu_model_.get();
    163 }
    164