12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/media_galleries/linux/mtp_device_object_enumerator.h"
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/logging.h"
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MTPDeviceObjectEnumerator::MTPDeviceObjectEnumerator(
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::vector<MtpFileEntry>& entries)
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    : file_entries_(entries),
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      index_(0U),
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      is_index_ready_(false) {
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MTPDeviceObjectEnumerator::~MTPDeviceObjectEnumerator() {
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)base::FilePath MTPDeviceObjectEnumerator::Next() {
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (IsIndexReadyAndInRange())
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ++index_;  // Normal traversal.
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  else if (!is_index_ready_)
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    is_index_ready_ = true;  // First time calling Next().
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!HasMoreEntries())
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return base::FilePath();
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return base::FilePath(file_entries_[index_].file_name());
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)int64 MTPDeviceObjectEnumerator::Size() {
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!IsIndexReadyAndInRange())
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return 0;
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return file_entries_[index_].file_size();
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool MTPDeviceObjectEnumerator::IsDirectory() {
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!IsIndexReadyAndInRange())
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return false;
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return file_entries_[index_].file_type() == MtpFileEntry::FILE_TYPE_FOLDER;
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)base::Time MTPDeviceObjectEnumerator::LastModifiedTime() {
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!IsIndexReadyAndInRange())
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return base::Time();
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return base::Time::FromTimeT(file_entries_[index_].modification_time());
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
48116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbool MTPDeviceObjectEnumerator::GetEntryId(uint32* entry_id) const {
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(entry_id);
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!IsIndexReadyAndInRange())
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return false;
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  *entry_id = file_entries_[index_].item_id();
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return true;
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool MTPDeviceObjectEnumerator::HasMoreEntries() const {
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return index_ < file_entries_.size();
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool MTPDeviceObjectEnumerator::IsIndexReadyAndInRange() const {
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return is_index_ready_ && HasMoreEntries();
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
64