1 // Copyright (c) 2010 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_item_model.h"
6
7 #include "base/i18n/number_formatting.h"
8 #include "base/i18n/rtl.h"
9 #include "base/string16.h"
10 #include "base/utf_string_conversions.h"
11 #include "chrome/browser/download/download_item.h"
12 #include "chrome/browser/download/save_package.h"
13 #include "chrome/common/time_format.h"
14 #include "grit/generated_resources.h"
15 #include "ui/base/l10n/l10n_util.h"
16
17 using base::TimeDelta;
18
19 // -----------------------------------------------------------------------------
20 // DownloadItemModel
21
DownloadItemModel(DownloadItem * download)22 DownloadItemModel::DownloadItemModel(DownloadItem* download)
23 : BaseDownloadItemModel(download) {
24 }
25
CancelTask()26 void DownloadItemModel::CancelTask() {
27 download_->Cancel(true /* update history service */);
28 }
29
GetStatusText()30 string16 DownloadItemModel::GetStatusText() {
31 int64 size = download_->received_bytes();
32 int64 total = download_->total_bytes();
33
34 DataUnits amount_units = GetByteDisplayUnits(total);
35 const string16 simple_size = FormatBytes(size, amount_units, false);
36
37 // In RTL locales, we render the text "size/total" in an RTL context. This
38 // is problematic since a string such as "123/456 MB" is displayed
39 // as "MB 123/456" because it ends with an LTR run. In order to solve this,
40 // we mark the total string as an LTR string if the UI layout is
41 // right-to-left so that the string "456 MB" is treated as an LTR run.
42 string16 simple_total = base::i18n::GetDisplayStringInLTRDirectionality(
43 FormatBytes(total, amount_units, true));
44
45 TimeDelta remaining;
46 string16 simple_time;
47 if (download_->IsInProgress() && download_->is_paused()) {
48 simple_time = l10n_util::GetStringUTF16(IDS_DOWNLOAD_PROGRESS_PAUSED);
49 } else if (download_->TimeRemaining(&remaining)) {
50 simple_time = download_->open_when_complete() ?
51 TimeFormat::TimeRemainingShort(remaining) :
52 TimeFormat::TimeRemaining(remaining);
53 }
54
55 string16 status_text;
56 switch (download_->state()) {
57 case DownloadItem::IN_PROGRESS:
58 if (download_->open_when_complete()) {
59 if (simple_time.empty()) {
60 status_text =
61 l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_OPEN_WHEN_COMPLETE);
62 } else {
63 status_text = l10n_util::GetStringFUTF16(IDS_DOWNLOAD_STATUS_OPEN_IN,
64 simple_time);
65 }
66 } else {
67 if (simple_time.empty()) {
68 // Instead of displaying "0 B" we keep the "Starting..." string.
69 status_text = (size == 0) ?
70 l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_STARTING) :
71 FormatBytes(size, GetByteDisplayUnits(size), true);
72 } else {
73 status_text = l10n_util::GetStringFUTF16(
74 IDS_DOWNLOAD_STATUS_IN_PROGRESS, simple_size, simple_total,
75 simple_time);
76 }
77 }
78 break;
79 case DownloadItem::COMPLETE:
80 status_text.clear();
81 break;
82 case DownloadItem::CANCELLED:
83 status_text = l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_CANCELED);
84 break;
85 case DownloadItem::REMOVING:
86 break;
87 case DownloadItem::INTERRUPTED:
88 status_text = l10n_util::GetStringFUTF16(IDS_DOWNLOAD_STATUS_INTERRUPTED,
89 simple_size,
90 simple_total);
91 break;
92 default:
93 NOTREACHED();
94 }
95
96 return status_text;
97 }
98
99 // -----------------------------------------------------------------------------
100 // SavePageModel
101
SavePageModel(SavePackage * save,DownloadItem * download)102 SavePageModel::SavePageModel(SavePackage* save, DownloadItem* download)
103 : BaseDownloadItemModel(download),
104 save_(save) {
105 }
106
CancelTask()107 void SavePageModel::CancelTask() {
108 save_->Cancel(true);
109 }
110
GetStatusText()111 string16 SavePageModel::GetStatusText() {
112 int64 size = download_->received_bytes();
113 int64 total_size = download_->total_bytes();
114
115 string16 status_text;
116 switch (download_->state()) {
117 case DownloadItem::IN_PROGRESS:
118 status_text = l10n_util::GetStringFUTF16(
119 IDS_SAVE_PAGE_PROGRESS,
120 base::FormatNumber(size),
121 base::FormatNumber(total_size));
122 break;
123 case DownloadItem::COMPLETE:
124 status_text = l10n_util::GetStringUTF16(IDS_SAVE_PAGE_STATUS_COMPLETED);
125 break;
126 case DownloadItem::CANCELLED:
127 status_text = l10n_util::GetStringUTF16(IDS_SAVE_PAGE_STATUS_CANCELED);
128 break;
129 case DownloadItem::REMOVING:
130 break;
131 case DownloadItem::INTERRUPTED:
132 status_text = l10n_util::GetStringFUTF16(
133 IDS_SAVE_PAGE_STATUS_INTERRUPTED,
134 base::FormatNumber(size),
135 base::FormatNumber(total_size));
136 break;
137 default:
138 NOTREACHED();
139 }
140
141 return status_text;
142 }
143