download_item_model.cc revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
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
17using base::TimeDelta;
18
19// -----------------------------------------------------------------------------
20// DownloadItemModel
21
22DownloadItemModel::DownloadItemModel(DownloadItem* download)
23    : BaseDownloadItemModel(download) {
24}
25
26void DownloadItemModel::CancelTask() {
27  download_->Cancel(true /* update history service */);
28}
29
30string16 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_->state() == DownloadItem::IN_PROGRESS &&
48      download_->is_paused()) {
49    simple_time = l10n_util::GetStringUTF16(IDS_DOWNLOAD_PROGRESS_PAUSED);
50  } else if (download_->TimeRemaining(&remaining)) {
51    simple_time = download_->open_when_complete() ?
52                      TimeFormat::TimeRemainingShort(remaining) :
53                      TimeFormat::TimeRemaining(remaining);
54  }
55
56  string16 status_text;
57  switch (download_->state()) {
58    case DownloadItem::IN_PROGRESS:
59      if (download_->open_when_complete()) {
60        if (simple_time.empty()) {
61          status_text =
62              l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_OPEN_WHEN_COMPLETE);
63        } else {
64          status_text = l10n_util::GetStringFUTF16(IDS_DOWNLOAD_STATUS_OPEN_IN,
65                                                   simple_time);
66        }
67      } else {
68        if (simple_time.empty()) {
69          // Instead of displaying "0 B" we keep the "Starting..." string.
70          status_text = (size == 0) ?
71              l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_STARTING) :
72              FormatBytes(size, GetByteDisplayUnits(size), true);
73        } else {
74          status_text = l10n_util::GetStringFUTF16(
75              IDS_DOWNLOAD_STATUS_IN_PROGRESS, simple_size, simple_total,
76              simple_time);
77        }
78      }
79      break;
80    case DownloadItem::COMPLETE:
81      status_text.clear();
82      break;
83    case DownloadItem::CANCELLED:
84      status_text = l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_CANCELED);
85      break;
86    case DownloadItem::REMOVING:
87      break;
88    default:
89      NOTREACHED();
90  }
91
92  return status_text;
93}
94
95// -----------------------------------------------------------------------------
96// SavePageModel
97
98SavePageModel::SavePageModel(SavePackage* save, DownloadItem* download)
99    : BaseDownloadItemModel(download),
100      save_(save) {
101}
102
103void SavePageModel::CancelTask() {
104  save_->Cancel(true);
105}
106
107string16 SavePageModel::GetStatusText() {
108  int64 size = download_->received_bytes();
109  int64 total_size = download_->total_bytes();
110
111  string16 status_text;
112  switch (download_->state()) {
113    case DownloadItem::IN_PROGRESS:
114      status_text = l10n_util::GetStringFUTF16(
115          IDS_SAVE_PAGE_PROGRESS,
116          base::FormatNumber(size),
117          base::FormatNumber(total_size));
118      break;
119    case DownloadItem::COMPLETE:
120      status_text = l10n_util::GetStringUTF16(IDS_SAVE_PAGE_STATUS_COMPLETED);
121      break;
122    case DownloadItem::CANCELLED:
123      status_text = l10n_util::GetStringUTF16(IDS_SAVE_PAGE_STATUS_CANCELED);
124      break;
125    case DownloadItem::REMOVING:
126      break;
127    default:
128      NOTREACHED();
129  }
130
131  return status_text;
132}
133