blob: e7eecc44cabcfb777987086e38d6618bc5d79d9d [file] [log] [blame]
[email protected]052ec1a3e2011-06-06 14:12:481// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]cdaa8652008-09-13 02:48:595#include "chrome/browser/download/download_item_model.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]d0767cb542009-10-08 17:38:307#include "base/i18n/number_formatting.h"
[email protected]7cf1b6ce2010-03-20 06:37:018#include "base/i18n/rtl.h"
[email protected]328c1ef2010-08-12 03:32:539#include "base/string16.h"
[email protected]64048bd2010-03-08 23:28:5810#include "base/utf_string_conversions.h"
[email protected]8a282712011-08-23 19:28:0011#include "chrome/browser/download/chrome_download_manager_delegate.h"
initial.commit09911bf2008-07-26 23:55:2912#include "chrome/common/time_format.h"
[email protected]71bf3f5e2011-08-15 21:05:2213#include "content/browser/download/download_item.h"
[email protected]0ffaa482011-07-14 23:41:2814#include "content/browser/download/save_package.h"
[email protected]34ac8f32009-02-22 23:03:2715#include "grit/generated_resources.h"
[email protected]c051a1b2011-01-21 23:30:1716#include "ui/base/l10n/l10n_util.h"
[email protected]7a3b2632011-06-22 20:40:2217#include "ui/base/text/bytes_formatting.h"
initial.commit09911bf2008-07-26 23:55:2918
[email protected]e1acf6f2008-10-27 20:43:3319using base::TimeDelta;
20
[email protected]b47da722009-02-23 23:54:1821// -----------------------------------------------------------------------------
22// DownloadItemModel
23
initial.commit09911bf2008-07-26 23:55:2924DownloadItemModel::DownloadItemModel(DownloadItem* download)
[email protected]bcef0dc02009-02-28 00:35:0225 : BaseDownloadItemModel(download) {
initial.commit09911bf2008-07-26 23:55:2926}
27
28void DownloadItemModel::CancelTask() {
[email protected]93af2272011-09-21 18:29:1729 download_->Cancel(true /* update history service */);
initial.commit09911bf2008-07-26 23:55:2930}
31
[email protected]42197a22010-12-28 23:29:4232string16 DownloadItemModel::GetStatusText() {
[email protected]c09a8fd2011-11-21 19:54:5033 int64 size = download_->GetReceivedBytes();
34 int64 total = download_->GetTotalBytes();
initial.commit09911bf2008-07-26 23:55:2935
[email protected]7a3b2632011-06-22 20:40:2236 ui::DataUnits amount_units = ui::GetByteDisplayUnits(total);
37 string16 simple_size = ui::FormatBytesWithUnits(size, amount_units, false);
initial.commit09911bf2008-07-26 23:55:2938
39 // In RTL locales, we render the text "size/total" in an RTL context. This
40 // is problematic since a string such as "123/456 MB" is displayed
41 // as "MB 123/456" because it ends with an LTR run. In order to solve this,
42 // we mark the total string as an LTR string if the UI layout is
43 // right-to-left so that the string "456 MB" is treated as an LTR run.
[email protected]7b83a102010-08-19 23:11:2844 string16 simple_total = base::i18n::GetDisplayStringInLTRDirectionality(
[email protected]7a3b2632011-06-22 20:40:2245 ui::FormatBytesWithUnits(total, amount_units, true));
initial.commit09911bf2008-07-26 23:55:2946
47 TimeDelta remaining;
[email protected]328c1ef2010-08-12 03:32:5348 string16 simple_time;
[email protected]c09a8fd2011-11-21 19:54:5049 if (download_->IsInProgress() && download_->IsPaused()) {
[email protected]328c1ef2010-08-12 03:32:5350 simple_time = l10n_util::GetStringUTF16(IDS_DOWNLOAD_PROGRESS_PAUSED);
initial.commit09911bf2008-07-26 23:55:2951 } else if (download_->TimeRemaining(&remaining)) {
[email protected]c09a8fd2011-11-21 19:54:5052 simple_time = download_->GetOpenWhenComplete() ?
initial.commit09911bf2008-07-26 23:55:2953 TimeFormat::TimeRemainingShort(remaining) :
54 TimeFormat::TimeRemaining(remaining);
55 }
56
[email protected]328c1ef2010-08-12 03:32:5357 string16 status_text;
[email protected]c09a8fd2011-11-21 19:54:5058 switch (download_->GetState()) {
initial.commit09911bf2008-07-26 23:55:2959 case DownloadItem::IN_PROGRESS:
[email protected]8a282712011-08-23 19:28:0060 if (ChromeDownloadManagerDelegate::IsExtensionDownload(download_) &&
[email protected]c09a8fd2011-11-21 19:54:5061 download_->AllDataSaved() &&
62 download_->GetState() == DownloadItem::IN_PROGRESS) {
[email protected]052ec1a3e2011-06-06 14:12:4863 // The download is a CRX (app, extension, theme, ...) and it is
64 // being unpacked and validated.
65 status_text = l10n_util::GetStringUTF16(
66 IDS_DOWNLOAD_STATUS_CRX_INSTALL_RUNNING);
[email protected]c09a8fd2011-11-21 19:54:5067 } else if (download_->GetOpenWhenComplete()) {
initial.commit09911bf2008-07-26 23:55:2968 if (simple_time.empty()) {
69 status_text =
[email protected]328c1ef2010-08-12 03:32:5370 l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_OPEN_WHEN_COMPLETE);
initial.commit09911bf2008-07-26 23:55:2971 } else {
[email protected]328c1ef2010-08-12 03:32:5372 status_text = l10n_util::GetStringFUTF16(IDS_DOWNLOAD_STATUS_OPEN_IN,
73 simple_time);
initial.commit09911bf2008-07-26 23:55:2974 }
75 } else {
76 if (simple_time.empty()) {
77 // Instead of displaying "0 B" we keep the "Starting..." string.
[email protected]7a3b2632011-06-22 20:40:2278 status_text = (size == 0)
79 ? l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_STARTING)
80 : ui::FormatBytes(size);
initial.commit09911bf2008-07-26 23:55:2981 } else {
[email protected]328c1ef2010-08-12 03:32:5382 status_text = l10n_util::GetStringFUTF16(
[email protected]70edb862010-08-18 16:23:1883 IDS_DOWNLOAD_STATUS_IN_PROGRESS, simple_size, simple_total,
84 simple_time);
initial.commit09911bf2008-07-26 23:55:2985 }
86 }
87 break;
88 case DownloadItem::COMPLETE:
[email protected]c09a8fd2011-11-21 19:54:5089 if (download_->GetFileExternallyRemoved()) {
[email protected]9fc114672011-06-15 08:17:4890 status_text = l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_REMOVED);
91 } else {
92 status_text.clear();
93 }
initial.commit09911bf2008-07-26 23:55:2994 break;
95 case DownloadItem::CANCELLED:
[email protected]328c1ef2010-08-12 03:32:5396 status_text = l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_CANCELED);
initial.commit09911bf2008-07-26 23:55:2997 break;
98 case DownloadItem::REMOVING:
99 break;
[email protected]bf68a00b2011-04-07 17:28:26100 case DownloadItem::INTERRUPTED:
101 status_text = l10n_util::GetStringFUTF16(IDS_DOWNLOAD_STATUS_INTERRUPTED,
102 simple_size,
103 simple_total);
104 break;
initial.commit09911bf2008-07-26 23:55:29105 default:
106 NOTREACHED();
107 }
108
[email protected]42197a22010-12-28 23:29:42109 return status_text;
initial.commit09911bf2008-07-26 23:55:29110}
license.botbf09a502008-08-24 00:55:55111
[email protected]b47da722009-02-23 23:54:18112// -----------------------------------------------------------------------------
113// SavePageModel
114
115SavePageModel::SavePageModel(SavePackage* save, DownloadItem* download)
[email protected]bcef0dc02009-02-28 00:35:02116 : BaseDownloadItemModel(download),
117 save_(save) {
[email protected]b47da722009-02-23 23:54:18118}
119
120void SavePageModel::CancelTask() {
121 save_->Cancel(true);
122}
123
[email protected]42197a22010-12-28 23:29:42124string16 SavePageModel::GetStatusText() {
[email protected]c09a8fd2011-11-21 19:54:50125 int64 size = download_->GetReceivedBytes();
126 int64 total_size = download_->GetTotalBytes();
[email protected]b47da722009-02-23 23:54:18127
[email protected]42197a22010-12-28 23:29:42128 string16 status_text;
[email protected]c09a8fd2011-11-21 19:54:50129 switch (download_->GetState()) {
[email protected]b47da722009-02-23 23:54:18130 case DownloadItem::IN_PROGRESS:
[email protected]42197a22010-12-28 23:29:42131 status_text = l10n_util::GetStringFUTF16(
[email protected]d0767cb542009-10-08 17:38:30132 IDS_SAVE_PAGE_PROGRESS,
[email protected]42197a22010-12-28 23:29:42133 base::FormatNumber(size),
134 base::FormatNumber(total_size));
[email protected]b47da722009-02-23 23:54:18135 break;
136 case DownloadItem::COMPLETE:
[email protected]42197a22010-12-28 23:29:42137 status_text = l10n_util::GetStringUTF16(IDS_SAVE_PAGE_STATUS_COMPLETED);
[email protected]b47da722009-02-23 23:54:18138 break;
139 case DownloadItem::CANCELLED:
[email protected]42197a22010-12-28 23:29:42140 status_text = l10n_util::GetStringUTF16(IDS_SAVE_PAGE_STATUS_CANCELED);
[email protected]b47da722009-02-23 23:54:18141 break;
142 case DownloadItem::REMOVING:
143 break;
[email protected]bf68a00b2011-04-07 17:28:26144 case DownloadItem::INTERRUPTED:
145 status_text = l10n_util::GetStringFUTF16(
146 IDS_SAVE_PAGE_STATUS_INTERRUPTED,
147 base::FormatNumber(size),
148 base::FormatNumber(total_size));
149 break;
[email protected]b47da722009-02-23 23:54:18150 default:
151 NOTREACHED();
152 }
153
154 return status_text;
155}