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/utility/media_galleries/picasa_albums_indexer.h" 6 7 #include <vector> 8 9 #include "base/ini_parser.h" 10 #include "base/logging.h" 11 #include "base/strings/string_split.h" 12 13 namespace picasa { 14 15 namespace { 16 17 const char kAlbumSectionHeader[] = ".album:"; 18 const char kAlbumsKey[] = "albums"; 19 20 class PicasaINIParser : public base::INIParser { 21 public: 22 PicasaINIParser( 23 const base::FilePath& folder_path, AlbumImagesMap* albums_images) 24 : folder_path_(folder_path), 25 albums_images_(albums_images) { 26 } 27 virtual ~PicasaINIParser() {} 28 29 private: 30 virtual void HandleTriplet(const std::string& section, 31 const std::string& key, 32 const std::string& value) OVERRIDE { 33 if (key != kAlbumsKey) 34 return; 35 36 // [.album:*] sections ignored as we get that data from the PMP files. 37 if (section.find(kAlbumSectionHeader) == 0) 38 return; 39 40 std::vector<std::string> containing_albums; 41 base::SplitString(value, ',', &containing_albums); 42 for (std::vector<std::string>::iterator it = containing_albums.begin(); 43 it != containing_albums.end(); ++it) { 44 AlbumImagesMap::iterator album_map_it = albums_images_->find(*it); 45 46 // Ignore entry if the album uid is not listed among in |album_uids| 47 // in the constructor. Happens if the PMP and INI files are inconsistent. 48 if (album_map_it == albums_images_->end()) 49 continue; 50 51 album_map_it->second.insert( 52 folder_path_.Append(base::FilePath::FromUTF8Unsafe(section))); 53 } 54 } 55 56 const base::FilePath folder_path_; 57 AlbumImagesMap* const albums_images_; 58 }; 59 60 } // namespace 61 62 PicasaAlbumsIndexer::PicasaAlbumsIndexer(const AlbumUIDSet& album_uids) { 63 // Create an entry in the map for the valid album uids. 64 for (AlbumUIDSet::const_iterator it = album_uids.begin(); 65 it != album_uids.end(); ++it) { 66 albums_images_[*it] = AlbumImages(); 67 } 68 } 69 70 PicasaAlbumsIndexer::~PicasaAlbumsIndexer() {} 71 72 void PicasaAlbumsIndexer::ParseFolderINI( 73 const base::FilePath& folder_path, const std::string& ini_contents) { 74 PicasaINIParser parser(folder_path, &albums_images_); 75 parser.Parse(ini_contents); 76 } 77 78 } // namespace picasa 79