OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/storage_monitor/storage_info.h" | 5 #include "chrome/browser/storage_monitor/storage_info.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/strings/utf_string_conversions.h" | |
9 #include "chrome/browser/storage_monitor/media_storage_util.h" | |
10 #include "grit/generated_resources.h" | |
11 #include "ui/base/l10n/l10n_util.h" | |
12 #include "ui/base/text/bytes_formatting.h" | |
8 | 13 |
9 namespace chrome { | 14 namespace chrome { |
10 | 15 |
11 namespace { | 16 namespace { |
12 | 17 |
13 // Prefix constants for different device id spaces. | 18 // Prefix constants for different device id spaces. |
14 const char kRemovableMassStorageWithDCIMPrefix[] = "dcim:"; | 19 const char kRemovableMassStorageWithDCIMPrefix[] = "dcim:"; |
15 const char kRemovableMassStorageNoDCIMPrefix[] = "nodcim:"; | 20 const char kRemovableMassStorageNoDCIMPrefix[] = "nodcim:"; |
16 const char kFixedMassStoragePrefix[] = "path:"; | 21 const char kFixedMassStoragePrefix[] = "path:"; |
17 const char kMtpPtpPrefix[] = "mtp:"; | 22 const char kMtpPtpPrefix[] = "mtp:"; |
18 const char kMacImageCapturePrefix[] = "ic:"; | 23 const char kMacImageCapturePrefix[] = "ic:"; |
19 const char kITunesPrefix[] = "itunes:"; | 24 const char kITunesPrefix[] = "itunes:"; |
20 const char kPicasaPrefix[] = "picasa:"; | 25 const char kPicasaPrefix[] = "picasa:"; |
21 | 26 |
27 string16 GetDisplayNameForDevice(uint64 storage_size_in_bytes, | |
28 const string16& name) { | |
29 DCHECK(!name.empty()); | |
30 return (storage_size_in_bytes == 0) ? | |
31 name : | |
32 ui::FormatBytes(storage_size_in_bytes) + base::ASCIIToUTF16(" ") + name; | |
33 } | |
34 | |
35 // For a device with |device_name| and a relative path |sub_folder|, construct | |
36 // a display name. If |sub_folder| is empty, then just return |device_name|. | |
37 string16 GetDisplayNameForSubFolder(const string16& device_name, | |
38 const base::FilePath& sub_folder) { | |
39 if (sub_folder.empty()) | |
40 return device_name; | |
41 return (sub_folder.BaseName().LossyDisplayName() + | |
42 base::ASCIIToUTF16(" - ") + | |
43 device_name); | |
44 } | |
45 | |
46 string16 GetFullProductName(const string16& vendor_name, | |
47 const string16& model_name) { | |
48 if (vendor_name.empty() && model_name.empty()) | |
49 return string16(); | |
50 | |
51 string16 product_name; | |
52 if (vendor_name.empty()) | |
53 product_name = model_name; | |
54 else if (model_name.empty()) | |
55 product_name = vendor_name; | |
56 else if (!vendor_name.empty() && !model_name.empty()) | |
57 product_name = vendor_name + base::UTF8ToUTF16(", ") + model_name; | |
58 | |
59 return product_name; | |
60 } | |
61 | |
22 } // namespace | 62 } // namespace |
23 | 63 |
24 StorageInfo::StorageInfo() : total_size_in_bytes_(0) { | 64 StorageInfo::StorageInfo() : total_size_in_bytes_(0) { |
25 } | 65 } |
26 | 66 |
27 StorageInfo::StorageInfo(const std::string& device_id_in, | 67 StorageInfo::StorageInfo(const std::string& device_id_in, |
28 const string16& device_name, | |
29 const base::FilePath::StringType& device_location, | 68 const base::FilePath::StringType& device_location, |
30 const string16& label, | 69 const string16& label, |
31 const string16& vendor, | 70 const string16& vendor, |
32 const string16& model, | 71 const string16& model, |
33 uint64 size_in_bytes) | 72 uint64 size_in_bytes) |
34 : device_id_(device_id_in), | 73 : device_id_(device_id_in), |
35 name_(device_name), | |
36 location_(device_location), | 74 location_(device_location), |
37 storage_label_(label), | 75 storage_label_(label), |
38 vendor_name_(vendor), | 76 vendor_name_(vendor), |
39 model_name_(model), | 77 model_name_(model), |
40 total_size_in_bytes_(size_in_bytes) { | 78 total_size_in_bytes_(size_in_bytes) { |
41 } | 79 } |
42 | 80 |
43 StorageInfo::~StorageInfo() { | 81 StorageInfo::~StorageInfo() { |
44 } | 82 } |
45 | 83 |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
135 Type type; | 173 Type type; |
136 return CrackDeviceId(device_id, &type, NULL) && type == ITUNES; | 174 return CrackDeviceId(device_id, &type, NULL) && type == ITUNES; |
137 } | 175 } |
138 | 176 |
139 // static | 177 // static |
140 bool StorageInfo::IsPicasaDevice(const std::string& device_id) { | 178 bool StorageInfo::IsPicasaDevice(const std::string& device_id) { |
141 Type type; | 179 Type type; |
142 return CrackDeviceId(device_id, &type, NULL) && type == PICASA; | 180 return CrackDeviceId(device_id, &type, NULL) && type == PICASA; |
143 } | 181 } |
144 | 182 |
183 base::string16 StorageInfo::GetDisplayName(bool with_size) const { | |
184 return GetDisplayNameWithOverride(base::string16(), with_size); | |
185 } | |
186 | |
187 base::string16 StorageInfo::GetDisplayNameWithOverride( | |
188 const base::string16& override_display_name, bool with_size) const { | |
189 if (!IsRemovableDevice(device_id_)) { | |
190 // For fixed storage, the name is the directory name, or, in the case | |
191 // of a root directory, the root directory name. | |
192 // TODO(gbillock): Using only the BaseName can lead to ambiguity. A tooltip | |
193 // will resolves it. Is that enough? | |
194 if (!override_display_name.empty()) | |
195 return override_display_name; | |
196 base::FilePath path = base::FilePath(location_); | |
197 if (path == path.DirName()) | |
198 return path.LossyDisplayName(); | |
199 return path.BaseName().LossyDisplayName(); | |
200 } | |
201 | |
202 string16 name = override_display_name; | |
203 if (name.empty()) | |
204 name = storage_label_; | |
205 if (name.empty()) | |
206 name = GetFullProductName(vendor_name_, model_name_); | |
207 if (name.empty()) | |
208 name = l10n_util::GetStringUTF16(IDS_MEDIA_GALLERIES_UNLABELED_DEVICE); | |
209 | |
210 if (with_size) | |
211 name = GetDisplayNameForDevice(total_size_in_bytes_, name); | |
212 | |
213 base::FilePath subfolder; | |
214 base::FilePath base_path = MediaStorageUtil::FindDevicePathById(device_id_); | |
215 if (!base_path.empty() && (base_path.value() != location_)) | |
vandebo (ex-Chrome)
2013/07/23 23:28:55
This is kind of ugly. Generally this should happe
Lei Zhang
2013/07/24 05:13:34
Not sure what you mean. Let's chat tomorrow.
Lei Zhang
2013/07/29 20:48:27
Ah, I think you were trying to say StorageInfo's |
| |
216 CHECK(base_path.AppendRelativePath(base::FilePath(location_), &subfolder)); | |
217 return GetDisplayNameForSubFolder(name, subfolder); | |
218 } | |
219 | |
145 } // namespace chrome | 220 } // namespace chrome |
OLD | NEW |