Fix some interleaving issues in EmbeddedWorkerInstance.
[chromium-blink-merge.git] / chrome / utility / media_galleries / picasa_albums_indexer.cc
blob7eded27db44bfe3654efd4ab982ded5d57cccf52
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.
5 #include "chrome/utility/media_galleries/picasa_albums_indexer.h"
7 #include <algorithm>
8 #include <utility>
9 #include <vector>
11 #include "base/logging.h"
12 #include "base/strings/string_split.h"
13 #include "base/strings/stringprintf.h"
14 #include "chrome/common/ini_parser.h"
16 namespace picasa {
18 namespace {
20 const char kAlbumSectionHeader[] = ".album:";
21 const char kAlbumsKey[] = "albums";
22 const int kMaxDedupeNumber = 1000; // Chosen arbitrarily.
24 class PicasaINIParser : public INIParser {
25 public:
26 PicasaINIParser(
27 const base::FilePath& folder_path, AlbumImagesMap* albums_images)
28 : folder_path_(folder_path),
29 albums_images_(albums_images) {
31 ~PicasaINIParser() override {}
33 private:
34 void HandleTriplet(const std::string& section,
35 const std::string& key,
36 const std::string& value) override {
37 if (key != kAlbumsKey)
38 return;
40 // [.album:*] sections ignored as we get that data from the PMP files.
41 if (section.find(kAlbumSectionHeader) == 0)
42 return;
44 for (const std::string& album : base::SplitString(
45 value, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) {
46 AlbumImagesMap::iterator album_map_it = albums_images_->find(album);
48 // Ignore entry if the album uid is not listed among in |album_uids|
49 // in the constructor. Happens if the PMP and INI files are inconsistent.
50 if (album_map_it == albums_images_->end())
51 continue;
53 base::FilePath filename = base::FilePath::FromUTF8Unsafe(section);
54 AlbumImages& album_images = album_map_it->second;
56 // If filename is first of its name in album, simply add.
57 if (album_images.insert(
58 std::make_pair(section, folder_path_.Append(filename))).second) {
59 continue;
62 // Otherwise, de-dupe by appending a number starting at (1).
63 for (int i = 1; i < kMaxDedupeNumber; ++i) {
64 std::string deduped_filename =
65 filename.InsertBeforeExtensionASCII(base::StringPrintf(" (%d)", i))
66 .AsUTF8Unsafe();
68 // Attempt to add the de-duped name.
69 if (album_images.insert(
70 std::make_pair(deduped_filename, folder_path_.Append(filename)))
71 .second) {
72 break;
78 const base::FilePath folder_path_;
79 AlbumImagesMap* const albums_images_;
82 } // namespace
84 PicasaAlbumsIndexer::PicasaAlbumsIndexer(const AlbumUIDSet& album_uids) {
85 // Create an entry in the map for the valid album uids.
86 for (AlbumUIDSet::const_iterator it = album_uids.begin();
87 it != album_uids.end(); ++it) {
88 albums_images_[*it] = AlbumImages();
92 PicasaAlbumsIndexer::~PicasaAlbumsIndexer() {}
94 void PicasaAlbumsIndexer::ParseFolderINI(
95 const std::vector<picasa::FolderINIContents>& folders_inis) {
96 // Make a copy for sorting
97 std::vector<picasa::FolderINIContents> folders_inis_sorted = folders_inis;
99 // Sort here so image names are deduplicated in a stable ordering.
100 std::sort(folders_inis_sorted.begin(), folders_inis_sorted.end());
102 for (std::vector<picasa::FolderINIContents>::const_iterator it =
103 folders_inis_sorted.begin();
104 it != folders_inis_sorted.end();
105 ++it) {
106 PicasaINIParser parser(it->folder_path, &albums_images_);
107 parser.Parse(it->ini_contents);
111 } // namespace picasa