| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2013 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/fileapi/picasa/picasa_album_table_reade
r.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/path_service.h" |
| 10 #include "base/string_util.h" |
| 11 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/browser/media_galleries/fileapi/picasa/pmp_column_reader.h" |
| 13 #include "chrome/browser/media_galleries/fileapi/picasa/pmp_constants.h" |
| 14 #include "chrome/browser/media_galleries/fileapi/picasa/pmp_table_reader.h" |
| 15 |
| 16 #define FPL(x) FILE_PATH_LITERAL(x) |
| 17 |
| 18 namespace picasaimport { |
| 19 |
| 20 namespace { |
| 21 |
| 22 // |variant_time| is specified as the number of days from Dec 30, 1899. |
| 23 base::Time TimeFromMicrosoftVariantTime(double variant_time) { |
| 24 base::TimeDelta variant_delta = base::TimeDelta::FromMicroseconds( |
| 25 static_cast<int64>(variant_time * base::Time::kMicrosecondsPerDay)); |
| 26 |
| 27 return base::Time::FromLocalExploded(kPicasaVariantTimeEpoch) + variant_delta; |
| 28 } |
| 29 |
| 30 } // namespace |
| 31 |
| 32 AlbumInfo::AlbumInfo(const std::string& name, const base::Time& timestamp, |
| 33 const std::string& uid) |
| 34 : name(name), |
| 35 timestamp(timestamp), |
| 36 uid(uid) {} |
| 37 |
| 38 AlbumInfo::~AlbumInfo() {} |
| 39 |
| 40 FolderInfo::FolderInfo(const std::string& name, const base::Time& timestamp, |
| 41 const std::string& uid, const base::FilePath& path) |
| 42 : name(name), |
| 43 timestamp(timestamp), |
| 44 uid(uid), |
| 45 path(path) {} |
| 46 |
| 47 FolderInfo::~FolderInfo() {} |
| 48 |
| 49 PicasaAlbumTableReader::PicasaAlbumTableReader( |
| 50 const base::FilePath& directory_path) |
| 51 : directory_path_(directory_path), |
| 52 initialized_(false) {} |
| 53 |
| 54 PicasaAlbumTableReader::~PicasaAlbumTableReader() {} |
| 55 |
| 56 base::FilePath PicasaAlbumTableReader::PicasaDB3Dir() { |
| 57 base::FilePath path; |
| 58 |
| 59 #if defined(OS_WIN) |
| 60 if (!PathService::Get(base::DIR_LOCAL_APP_DATA, &path)) |
| 61 return base::FilePath(); |
| 62 #elif defined(OS_MACOSX) |
| 63 if (!PathService::Get(base::DIR_APP_DATA, &path)) |
| 64 return base::FilePath(); |
| 65 #else |
| 66 return base::FilePath(); |
| 67 #endif |
| 68 |
| 69 return path.Append(FPL("Google")).Append(FPL("Picasa2")).Append(FPL("db3")); |
| 70 } |
| 71 |
| 72 const std::vector<FolderInfo>& PicasaAlbumTableReader::folders() const { |
| 73 DCHECK(initialized_); |
| 74 return folders_; |
| 75 } |
| 76 |
| 77 const std::vector<AlbumInfo>& PicasaAlbumTableReader::user_albums() const { |
| 78 DCHECK(initialized_); |
| 79 return user_albums_; |
| 80 } |
| 81 |
| 82 PicasaAlbumTableReader::PicasaAlbumTableReader( |
| 83 const std::vector<FolderInfo>& folders, |
| 84 const std::vector<AlbumInfo>& user_albums) |
| 85 : initialized_(true), |
| 86 folders_(folders), |
| 87 user_albums_(user_albums) {} |
| 88 |
| 89 bool PicasaAlbumTableReader::Init() { |
| 90 if (initialized_) |
| 91 return true; |
| 92 |
| 93 PmpTableReader pmp_reader(kPicasaAlbumTableName, directory_path_); |
| 94 |
| 95 const PmpColumnReader* category_column = |
| 96 pmp_reader.AddColumn("category", PMP_TYPE_UINT32); |
| 97 const PmpColumnReader* date_column = |
| 98 pmp_reader.AddColumn("date", PMP_TYPE_DOUBLE64); |
| 99 const PmpColumnReader* filename_column = |
| 100 pmp_reader.AddColumn("filename", PMP_TYPE_STRING); |
| 101 const PmpColumnReader* name_column = |
| 102 pmp_reader.AddColumn("name", PMP_TYPE_STRING); |
| 103 const PmpColumnReader* token_column = |
| 104 pmp_reader.AddColumn("token", PMP_TYPE_STRING); |
| 105 const PmpColumnReader* uid_column = |
| 106 pmp_reader.AddColumn("uid", PMP_TYPE_STRING); |
| 107 |
| 108 if (pmp_reader.Columns().size() != 6) |
| 109 return false; |
| 110 |
| 111 for (uint32 i = 0; i < pmp_reader.RowCount(); i++) { |
| 112 uint32 category = kAlbumCategoryInvalid; |
| 113 double date = 0; |
| 114 std::string name; |
| 115 std::string uid; |
| 116 if (!category_column->ReadUInt32(i, &category) || |
| 117 !date_column->ReadDouble64(i, &date) || |
| 118 !name_column->ReadString(i, &name) || name.empty() || |
| 119 !uid_column->ReadString(i, &uid) || uid.empty()) { |
| 120 continue; |
| 121 } |
| 122 |
| 123 base::Time timestamp = TimeFromMicrosoftVariantTime(date); |
| 124 |
| 125 switch (category) { |
| 126 case kAlbumCategoryUserAlbum: { |
| 127 std::string token; |
| 128 if (!token_column->ReadString(i, &token) || token.empty() || |
| 129 !StartsWithASCII(token, kAlbumTokenPrefix, false)) { |
| 130 continue; |
| 131 } |
| 132 |
| 133 user_albums_.push_back(AlbumInfo(name, timestamp, uid)); |
| 134 break; |
| 135 } |
| 136 case kAlbumCategoryFolder: { |
| 137 std::string filename; |
| 138 if (!filename_column->ReadString(i, &filename) || filename.empty()) |
| 139 continue; |
| 140 |
| 141 base::FilePath path = |
| 142 base::FilePath(base::FilePath::FromUTF8Unsafe(filename)); |
| 143 |
| 144 folders_.push_back(FolderInfo(name, timestamp, uid, path)); |
| 145 break; |
| 146 } |
| 147 default: { |
| 148 break; |
| 149 } |
| 150 } |
| 151 } |
| 152 |
| 153 initialized_ = true; |
| 154 return true; |
| 155 } |
| 156 |
| 157 } // namespace picasaimport |
| OLD | NEW |