Chromium Code Reviews| Index: webkit/fileapi/media/picasa/picasa_album_data_reader.cc |
| diff --git a/webkit/fileapi/media/picasa/picasa_album_data_reader.cc b/webkit/fileapi/media/picasa/picasa_album_data_reader.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7667343a35830e10810b0af2802618b4b4eaeaa5 |
| --- /dev/null |
| +++ b/webkit/fileapi/media/picasa/picasa_album_data_reader.cc |
| @@ -0,0 +1,125 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "webkit/fileapi/media/picasa/picasa_album_data_reader.h" |
| + |
| +#include <vector> |
| + |
| +#include "base/utf_string_conversions.h" |
| +#include "webkit/fileapi/media/picasa/pmp_column_reader.h" |
| +#include "webkit/fileapi/media/picasa/pmp_constants.h" |
| +#include "webkit/fileapi/media/picasa/pmp_table_reader.h" |
| + |
| +namespace picasaimport { |
| + |
| +namespace { |
| + |
| +const uint16 kAlbumCategoryUserAlbum = 0; |
| +const uint16 kAlbumCategoryFolder = 2; |
| +const uint16 kAlbumCategoryInvalid = 0xffff; // Sentinel value. |
| + |
| +const int kPmpVariantTimeEpochYear = 1899; |
| +const int kPmpVariantTimeEpochMonth = 12; |
| +const int kPmpVariantTimeEpochDay = 30; |
| + |
| +// |variant_time| is specified as the number of days from Dec 30, 1899. |
| +base::Time TimeFromMicrosoftVariantTime(double variant_time) { |
| + base::Time::Exploded variant_epoch; |
| + |
| + variant_epoch.year = kPmpVariantTimeEpochYear; |
| + variant_epoch.month = kPmpVariantTimeEpochMonth; |
| + variant_epoch.day_of_month = kPmpVariantTimeEpochDay; |
| + |
| + base::TimeDelta variant_delta = base::TimeDelta::FromMicroseconds( |
| + static_cast<int64>(variant_time * base::Time::kMicrosecondsPerDay)); |
| + |
| + return base::Time::FromLocalExploded(variant_epoch) + variant_delta; |
| +} |
| + |
| +} // namespace |
| + |
| +AlbumInfo::AlbumInfo(const std::string& name, const base::Time& timestamp, |
| + const std::string& uid) |
| + : name(name), |
| + timestamp(timestamp), |
| + uid(uid) {} |
| + |
| +FolderInfo::FolderInfo(const std::string& name, const base::Time& timestamp, |
| + const base::FilePath& path) |
| + : name(name), |
| + timestamp(timestamp), |
| + path(path) {} |
| + |
| +PicasaAlbumDataReader::PicasaAlbumDataReader() {} |
| + |
| +PicasaAlbumDataReader::~PicasaAlbumDataReader() {} |
| + |
| +bool PicasaAlbumDataReader::Init(const base::FilePath& directory_path) { |
| + const std::string kAlbumTokenPrefix = "]album:"; |
| + |
| + PmpTableReader pmp_reader("albumdata", directory_path); |
| + |
| + const PmpColumnReader* category_column = |
| + pmp_reader.AddColumn("category", PMP_TYPE_UINT32); |
| + const PmpColumnReader* date_column = |
| + pmp_reader.AddColumn("date", PMP_TYPE_DOUBLE64); |
| + const PmpColumnReader* filename_column = |
| + pmp_reader.AddColumn("filename", PMP_TYPE_STRING); |
| + const PmpColumnReader* name_column = |
| + pmp_reader.AddColumn("name", PMP_TYPE_STRING); |
| + const PmpColumnReader* token_column = |
| + pmp_reader.AddColumn("token", PMP_TYPE_STRING); |
| + const PmpColumnReader* uid_column = |
| + pmp_reader.AddColumn("uid", PMP_TYPE_STRING); |
| + |
| + if (pmp_reader.Columns().size() != 6) |
| + return false; |
| + |
| + for (uint32 i = 0; i < pmp_reader.RowCount(); i++) { |
| + uint32 category = kAlbumCategoryInvalid; |
| + double date = 0; |
| + std::string name; |
| + if (!category_column->ReadUInt32(i, &category) || |
| + !date_column->ReadDouble64(i, &date) || |
| + !name_column->ReadString(i, &name) || name.empty()) { |
| + continue; |
| + } |
| + |
| + base::Time timestamp = TimeFromMicrosoftVariantTime(date); |
| + |
| + switch (category) { |
| + case kAlbumCategoryUserAlbum: { |
| + std::string token, uid; |
| + if (!token_column->ReadString(i, &token) || token.empty() || |
| + !uid_column->ReadString(i, &uid) || uid.empty() || |
| + token.substr(0, kAlbumTokenPrefix.size()) != kAlbumTokenPrefix) { |
| + continue; |
| + } |
| + |
| + user_albums_.push_back(AlbumInfo(name, timestamp, uid)); |
| + break; |
| + } |
| + case kAlbumCategoryFolder: { |
| + std::string filename; |
| + if (!filename_column->ReadString(i, &filename) || filename.empty()) |
| + continue; |
| + |
| +#if defined(OS_WIN) |
|
vandebo (ex-Chrome)
2013/04/11 20:29:22
FromUTF8Unsafe
tommycli
2013/04/11 20:51:28
Done.
|
| + base::FilePath path = base::FilePath(UTF8ToUTF16(filename)); |
| +#else |
| + base::FilePath path = base::FilePath(filename); |
| +#endif |
| + folders_.push_back(FolderInfo(name, timestamp, path)); |
| + break; |
| + } |
| + default: { |
| + break; |
| + } |
| + } |
| + } |
| + |
| + return true; |
| +} |
| + |
| +} // namespace picasaimport |