[email protected] | 052ec1a3e | 2011-06-06 14:12:48 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | cdaa865 | 2008-09-13 02:48:59 | [diff] [blame] | 5 | #include "chrome/browser/download/download_item_model.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
[email protected] | d0767cb54 | 2009-10-08 17:38:30 | [diff] [blame] | 7 | #include "base/i18n/number_formatting.h" |
[email protected] | 7cf1b6ce | 2010-03-20 06:37:01 | [diff] [blame] | 8 | #include "base/i18n/rtl.h" |
[email protected] | 328c1ef | 2010-08-12 03:32:53 | [diff] [blame] | 9 | #include "base/string16.h" |
[email protected] | 64048bd | 2010-03-08 23:28:58 | [diff] [blame] | 10 | #include "base/utf_string_conversions.h" |
[email protected] | 8a28271 | 2011-08-23 19:28:00 | [diff] [blame] | 11 | #include "chrome/browser/download/chrome_download_manager_delegate.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 12 | #include "chrome/common/time_format.h" |
[email protected] | 71bf3f5e | 2011-08-15 21:05:22 | [diff] [blame] | 13 | #include "content/browser/download/download_item.h" |
[email protected] | 0ffaa48 | 2011-07-14 23:41:28 | [diff] [blame] | 14 | #include "content/browser/download/save_package.h" |
[email protected] | 34ac8f3 | 2009-02-22 23:03:27 | [diff] [blame] | 15 | #include "grit/generated_resources.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 16 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | 7a3b263 | 2011-06-22 20:40:22 | [diff] [blame] | 17 | #include "ui/base/text/bytes_formatting.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 18 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 19 | using base::TimeDelta; |
| 20 | |
[email protected] | b47da72 | 2009-02-23 23:54:18 | [diff] [blame] | 21 | // ----------------------------------------------------------------------------- |
| 22 | // DownloadItemModel |
| 23 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 24 | DownloadItemModel::DownloadItemModel(DownloadItem* download) |
[email protected] | bcef0dc0 | 2009-02-28 00:35:02 | [diff] [blame] | 25 | : BaseDownloadItemModel(download) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | void DownloadItemModel::CancelTask() { |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 29 | download_->Cancel(true /* update history service */); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 30 | } |
| 31 | |
[email protected] | 42197a2 | 2010-12-28 23:29:42 | [diff] [blame] | 32 | string16 DownloadItemModel::GetStatusText() { |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame^] | 33 | int64 size = download_->GetReceivedBytes(); |
| 34 | int64 total = download_->GetTotalBytes(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 35 | |
[email protected] | 7a3b263 | 2011-06-22 20:40:22 | [diff] [blame] | 36 | ui::DataUnits amount_units = ui::GetByteDisplayUnits(total); |
| 37 | string16 simple_size = ui::FormatBytesWithUnits(size, amount_units, false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 38 | |
| 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] | 7b83a10 | 2010-08-19 23:11:28 | [diff] [blame] | 44 | string16 simple_total = base::i18n::GetDisplayStringInLTRDirectionality( |
[email protected] | 7a3b263 | 2011-06-22 20:40:22 | [diff] [blame] | 45 | ui::FormatBytesWithUnits(total, amount_units, true)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 46 | |
| 47 | TimeDelta remaining; |
[email protected] | 328c1ef | 2010-08-12 03:32:53 | [diff] [blame] | 48 | string16 simple_time; |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame^] | 49 | if (download_->IsInProgress() && download_->IsPaused()) { |
[email protected] | 328c1ef | 2010-08-12 03:32:53 | [diff] [blame] | 50 | simple_time = l10n_util::GetStringUTF16(IDS_DOWNLOAD_PROGRESS_PAUSED); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 51 | } else if (download_->TimeRemaining(&remaining)) { |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame^] | 52 | simple_time = download_->GetOpenWhenComplete() ? |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 53 | TimeFormat::TimeRemainingShort(remaining) : |
| 54 | TimeFormat::TimeRemaining(remaining); |
| 55 | } |
| 56 | |
[email protected] | 328c1ef | 2010-08-12 03:32:53 | [diff] [blame] | 57 | string16 status_text; |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame^] | 58 | switch (download_->GetState()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 59 | case DownloadItem::IN_PROGRESS: |
[email protected] | 8a28271 | 2011-08-23 19:28:00 | [diff] [blame] | 60 | if (ChromeDownloadManagerDelegate::IsExtensionDownload(download_) && |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame^] | 61 | download_->AllDataSaved() && |
| 62 | download_->GetState() == DownloadItem::IN_PROGRESS) { |
[email protected] | 052ec1a3e | 2011-06-06 14:12:48 | [diff] [blame] | 63 | // 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] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame^] | 67 | } else if (download_->GetOpenWhenComplete()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 68 | if (simple_time.empty()) { |
| 69 | status_text = |
[email protected] | 328c1ef | 2010-08-12 03:32:53 | [diff] [blame] | 70 | l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_OPEN_WHEN_COMPLETE); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 71 | } else { |
[email protected] | 328c1ef | 2010-08-12 03:32:53 | [diff] [blame] | 72 | status_text = l10n_util::GetStringFUTF16(IDS_DOWNLOAD_STATUS_OPEN_IN, |
| 73 | simple_time); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 74 | } |
| 75 | } else { |
| 76 | if (simple_time.empty()) { |
| 77 | // Instead of displaying "0 B" we keep the "Starting..." string. |
[email protected] | 7a3b263 | 2011-06-22 20:40:22 | [diff] [blame] | 78 | status_text = (size == 0) |
| 79 | ? l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_STARTING) |
| 80 | : ui::FormatBytes(size); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 81 | } else { |
[email protected] | 328c1ef | 2010-08-12 03:32:53 | [diff] [blame] | 82 | status_text = l10n_util::GetStringFUTF16( |
[email protected] | 70edb86 | 2010-08-18 16:23:18 | [diff] [blame] | 83 | IDS_DOWNLOAD_STATUS_IN_PROGRESS, simple_size, simple_total, |
| 84 | simple_time); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | break; |
| 88 | case DownloadItem::COMPLETE: |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame^] | 89 | if (download_->GetFileExternallyRemoved()) { |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 90 | status_text = l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_REMOVED); |
| 91 | } else { |
| 92 | status_text.clear(); |
| 93 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 94 | break; |
| 95 | case DownloadItem::CANCELLED: |
[email protected] | 328c1ef | 2010-08-12 03:32:53 | [diff] [blame] | 96 | status_text = l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_CANCELED); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 97 | break; |
| 98 | case DownloadItem::REMOVING: |
| 99 | break; |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 100 | case DownloadItem::INTERRUPTED: |
| 101 | status_text = l10n_util::GetStringFUTF16(IDS_DOWNLOAD_STATUS_INTERRUPTED, |
| 102 | simple_size, |
| 103 | simple_total); |
| 104 | break; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 105 | default: |
| 106 | NOTREACHED(); |
| 107 | } |
| 108 | |
[email protected] | 42197a2 | 2010-12-28 23:29:42 | [diff] [blame] | 109 | return status_text; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 110 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 111 | |
[email protected] | b47da72 | 2009-02-23 23:54:18 | [diff] [blame] | 112 | // ----------------------------------------------------------------------------- |
| 113 | // SavePageModel |
| 114 | |
| 115 | SavePageModel::SavePageModel(SavePackage* save, DownloadItem* download) |
[email protected] | bcef0dc0 | 2009-02-28 00:35:02 | [diff] [blame] | 116 | : BaseDownloadItemModel(download), |
| 117 | save_(save) { |
[email protected] | b47da72 | 2009-02-23 23:54:18 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | void SavePageModel::CancelTask() { |
| 121 | save_->Cancel(true); |
| 122 | } |
| 123 | |
[email protected] | 42197a2 | 2010-12-28 23:29:42 | [diff] [blame] | 124 | string16 SavePageModel::GetStatusText() { |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame^] | 125 | int64 size = download_->GetReceivedBytes(); |
| 126 | int64 total_size = download_->GetTotalBytes(); |
[email protected] | b47da72 | 2009-02-23 23:54:18 | [diff] [blame] | 127 | |
[email protected] | 42197a2 | 2010-12-28 23:29:42 | [diff] [blame] | 128 | string16 status_text; |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame^] | 129 | switch (download_->GetState()) { |
[email protected] | b47da72 | 2009-02-23 23:54:18 | [diff] [blame] | 130 | case DownloadItem::IN_PROGRESS: |
[email protected] | 42197a2 | 2010-12-28 23:29:42 | [diff] [blame] | 131 | status_text = l10n_util::GetStringFUTF16( |
[email protected] | d0767cb54 | 2009-10-08 17:38:30 | [diff] [blame] | 132 | IDS_SAVE_PAGE_PROGRESS, |
[email protected] | 42197a2 | 2010-12-28 23:29:42 | [diff] [blame] | 133 | base::FormatNumber(size), |
| 134 | base::FormatNumber(total_size)); |
[email protected] | b47da72 | 2009-02-23 23:54:18 | [diff] [blame] | 135 | break; |
| 136 | case DownloadItem::COMPLETE: |
[email protected] | 42197a2 | 2010-12-28 23:29:42 | [diff] [blame] | 137 | status_text = l10n_util::GetStringUTF16(IDS_SAVE_PAGE_STATUS_COMPLETED); |
[email protected] | b47da72 | 2009-02-23 23:54:18 | [diff] [blame] | 138 | break; |
| 139 | case DownloadItem::CANCELLED: |
[email protected] | 42197a2 | 2010-12-28 23:29:42 | [diff] [blame] | 140 | status_text = l10n_util::GetStringUTF16(IDS_SAVE_PAGE_STATUS_CANCELED); |
[email protected] | b47da72 | 2009-02-23 23:54:18 | [diff] [blame] | 141 | break; |
| 142 | case DownloadItem::REMOVING: |
| 143 | break; |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 144 | 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] | b47da72 | 2009-02-23 23:54:18 | [diff] [blame] | 150 | default: |
| 151 | NOTREACHED(); |
| 152 | } |
| 153 | |
| 154 | return status_text; |
| 155 | } |