1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/download/download_shelf_context_menu.h"
#include "chrome/browser/download/download_item.h"
#include "chrome/browser/download/download_item_model.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
DownloadShelfContextMenu::~DownloadShelfContextMenu() {}
DownloadShelfContextMenu::DownloadShelfContextMenu(
BaseDownloadItemModel* download_model)
: download_model_(download_model),
download_item_(download_model->download()) {
}
ui::SimpleMenuModel* DownloadShelfContextMenu::GetMenuModel() {
return download_item_->IsComplete() ? GetFinishedMenuModel()
: GetInProgressMenuModel();
}
bool DownloadShelfContextMenu::IsCommandIdEnabled(int command_id) const {
switch (command_id) {
case SHOW_IN_FOLDER:
case OPEN_WHEN_COMPLETE:
return download_item_->CanShowInFolder();
case ALWAYS_OPEN_TYPE:
return download_item_->CanOpenDownload();
case CANCEL:
return download_item_->IsPartialDownload();
case TOGGLE_PAUSE:
return download_item_->IsInProgress();
default:
return command_id > 0 && command_id < MENU_LAST;
}
}
bool DownloadShelfContextMenu::IsCommandIdChecked(int command_id) const {
switch (command_id) {
case OPEN_WHEN_COMPLETE:
return download_item_->open_when_complete();
case ALWAYS_OPEN_TYPE:
return download_item_->ShouldOpenFileBasedOnExtension();
case TOGGLE_PAUSE:
return download_item_->is_paused();
}
return false;
}
void DownloadShelfContextMenu::ExecuteCommand(int command_id) {
switch (command_id) {
case SHOW_IN_FOLDER:
download_item_->ShowDownloadInShell();
break;
case OPEN_WHEN_COMPLETE:
download_item_->OpenDownload();
break;
case ALWAYS_OPEN_TYPE: {
download_item_->OpenFilesBasedOnExtension(
!IsCommandIdChecked(ALWAYS_OPEN_TYPE));
break;
}
case CANCEL:
download_model_->CancelTask();
break;
case TOGGLE_PAUSE:
// It is possible for the download to complete before the user clicks the
// menu item, recheck if the download is in progress state before toggling
// pause.
if (download_item_->IsPartialDownload())
download_item_->TogglePause();
break;
default:
NOTREACHED();
}
}
bool DownloadShelfContextMenu::GetAcceleratorForCommandId(
int command_id, ui::Accelerator* accelerator) {
return false;
}
bool DownloadShelfContextMenu::IsItemForCommandIdDynamic(int command_id) const {
return command_id == TOGGLE_PAUSE;
}
string16 DownloadShelfContextMenu::GetLabelForCommandId(int command_id) const {
switch (command_id) {
case SHOW_IN_FOLDER:
return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_SHOW);
case OPEN_WHEN_COMPLETE:
if (download_item_->IsInProgress())
return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_OPEN_WHEN_COMPLETE);
return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_OPEN);
case ALWAYS_OPEN_TYPE:
return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_ALWAYS_OPEN_TYPE);
case CANCEL:
return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_CANCEL);
case TOGGLE_PAUSE: {
if (download_item_->is_paused())
return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_RESUME_ITEM);
return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_PAUSE_ITEM);
}
default:
NOTREACHED();
break;
}
return string16();
}
ui::SimpleMenuModel* DownloadShelfContextMenu::GetInProgressMenuModel() {
if (in_progress_download_menu_model_.get())
return in_progress_download_menu_model_.get();
in_progress_download_menu_model_.reset(new ui::SimpleMenuModel(this));
in_progress_download_menu_model_->AddCheckItemWithStringId(
OPEN_WHEN_COMPLETE, IDS_DOWNLOAD_MENU_OPEN_WHEN_COMPLETE);
in_progress_download_menu_model_->AddCheckItemWithStringId(
ALWAYS_OPEN_TYPE, IDS_DOWNLOAD_MENU_ALWAYS_OPEN_TYPE);
in_progress_download_menu_model_->AddSeparator();
in_progress_download_menu_model_->AddItemWithStringId(
TOGGLE_PAUSE, IDS_DOWNLOAD_MENU_PAUSE_ITEM);
in_progress_download_menu_model_->AddItemWithStringId(
SHOW_IN_FOLDER, IDS_DOWNLOAD_MENU_SHOW);
in_progress_download_menu_model_->AddSeparator();
in_progress_download_menu_model_->AddItemWithStringId(
CANCEL, IDS_DOWNLOAD_MENU_CANCEL);
return in_progress_download_menu_model_.get();
}
ui::SimpleMenuModel* DownloadShelfContextMenu::GetFinishedMenuModel() {
if (finished_download_menu_model_.get())
return finished_download_menu_model_.get();
finished_download_menu_model_.reset(new ui::SimpleMenuModel(this));
finished_download_menu_model_->AddItemWithStringId(
OPEN_WHEN_COMPLETE, IDS_DOWNLOAD_MENU_OPEN);
finished_download_menu_model_->AddCheckItemWithStringId(
ALWAYS_OPEN_TYPE, IDS_DOWNLOAD_MENU_ALWAYS_OPEN_TYPE);
finished_download_menu_model_->AddSeparator();
finished_download_menu_model_->AddItemWithStringId(
SHOW_IN_FOLDER, IDS_DOWNLOAD_MENU_SHOW);
finished_download_menu_model_->AddSeparator();
finished_download_menu_model_->AddItemWithStringId(
CANCEL, IDS_DOWNLOAD_MENU_CANCEL);
return finished_download_menu_model_.get();
}
|