mtp_device_object_enumerator.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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 3// found in the LICENSE file. 4 5#include "chrome/browser/media_galleries/linux/mtp_device_object_enumerator.h" 6 7#include "base/logging.h" 8 9namespace chrome { 10 11MTPDeviceObjectEnumerator::MTPDeviceObjectEnumerator( 12 const std::vector<MtpFileEntry>& entries) 13 : file_entries_(entries), 14 index_(0U), 15 is_index_ready_(false) { 16} 17 18MTPDeviceObjectEnumerator::~MTPDeviceObjectEnumerator() { 19} 20 21base::FilePath MTPDeviceObjectEnumerator::Next() { 22 if (IsIndexReadyAndInRange()) 23 ++index_; // Normal traversal. 24 else if (!is_index_ready_) 25 is_index_ready_ = true; // First time calling Next(). 26 27 if (!HasMoreEntries()) 28 return base::FilePath(); 29 return base::FilePath(file_entries_[index_].file_name()); 30} 31 32int64 MTPDeviceObjectEnumerator::Size() { 33 if (!IsIndexReadyAndInRange()) 34 return 0; 35 return file_entries_[index_].file_size(); 36} 37 38bool MTPDeviceObjectEnumerator::IsDirectory() { 39 if (!IsIndexReadyAndInRange()) 40 return false; 41 return file_entries_[index_].file_type() == MtpFileEntry::FILE_TYPE_FOLDER; 42} 43 44base::Time MTPDeviceObjectEnumerator::LastModifiedTime() { 45 if (!IsIndexReadyAndInRange()) 46 return base::Time(); 47 return base::Time::FromTimeT(file_entries_[index_].modification_time()); 48} 49 50bool MTPDeviceObjectEnumerator::GetEntryId(uint32_t* entry_id) const { 51 DCHECK(entry_id); 52 if (!IsIndexReadyAndInRange()) 53 return false; 54 55 *entry_id = file_entries_[index_].item_id(); 56 return true; 57} 58 59bool MTPDeviceObjectEnumerator::HasMoreEntries() const { 60 return index_ < file_entries_.size(); 61} 62 63bool MTPDeviceObjectEnumerator::IsIndexReadyAndInRange() const { 64 return is_index_ready_ && HasMoreEntries(); 65} 66 67} // namespace chrome 68