OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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_gallery/media_gallery_registry.h" | |
6 | |
7 #include "base/command_line.h" | |
8 #include "base/string_number_conversions.h" | |
9 #include "base/values.h" | |
10 #include "chrome/browser/prefs/pref_service.h" | |
11 #include "chrome/browser/prefs/scoped_user_pref_update.h" | |
12 #include "chrome/browser/profiles/profile.h" | |
13 #include "chrome/common/chrome_switches.h" | |
14 #include "chrome/common/pref_names.h" | |
15 #include "grit/generated_resources.h" | |
16 | |
17 namespace { | |
18 | |
19 const char kMediaGalleryIdKey[] = "id"; | |
20 const char kMediaGalleryPathKey[] = "path"; | |
21 const char kMediaGalleryDisplayNameKey[] = "displayName"; | |
22 | |
23 bool GetId(const DictionaryValue* dict, uint64* value) { | |
24 std::string string_id; | |
25 if (!dict->GetString(kMediaGalleryIdKey, &string_id) || | |
26 !base::StringToUint64(string_id, value)) { | |
27 return false; | |
28 } | |
29 | |
30 return true; | |
31 } | |
32 | |
33 bool PopulateGalleryFromDictionary(const DictionaryValue* dict, | |
34 MediaGallery* out_gallery) { | |
35 uint64 id; | |
36 FilePath::StringType path; | |
37 string16 display_name; | |
38 if (!GetId(dict, &id) || | |
39 !dict->GetString(kMediaGalleryPathKey, &path) || | |
40 !dict->GetString(kMediaGalleryDisplayNameKey, &display_name)) { | |
41 return false; | |
42 } | |
43 | |
44 out_gallery->id = id; | |
45 out_gallery->path = FilePath(path); | |
46 out_gallery->display_name = display_name; | |
47 return true; | |
48 } | |
49 | |
50 DictionaryValue* CreateGalleryDictionary(const MediaGallery& gallery) { | |
51 DictionaryValue* dict = new DictionaryValue(); | |
52 dict->SetString(kMediaGalleryIdKey, base::Uint64ToString(gallery.id)); | |
53 dict->SetString(kMediaGalleryPathKey, gallery.path.value()); | |
54 // TODO(estade): save |path| and |identifier|. | |
55 dict->SetString(kMediaGalleryDisplayNameKey, gallery.display_name); | |
56 return dict; | |
57 } | |
58 | |
59 } // namespace | |
60 | |
61 MediaGallery::MediaGallery() : id(0) {} | |
62 MediaGallery::~MediaGallery() {} | |
63 | |
64 MediaGalleryRegistry::MediaGalleryRegistry(Profile* profile) | |
65 : profile_(profile) { | |
66 DCHECK(UserInteractionIsEnabled()); | |
67 InitFromPrefs(); | |
68 } | |
69 | |
70 MediaGalleryRegistry::~MediaGalleryRegistry() {} | |
71 | |
72 void MediaGalleryRegistry::InitFromPrefs() { | |
73 remembered_galleries_.clear(); | |
74 | |
75 PrefService* prefs = profile_->GetPrefs(); | |
76 const ListValue* list = prefs->GetList( | |
77 prefs::kMediaGalleryRememberedGalleries); | |
78 | |
79 for (ListValue::const_iterator it = list->begin(); it != list->end(); it++) { | |
80 const DictionaryValue* dict = NULL; | |
81 if (!(*it)->GetAsDictionary(&dict)) | |
82 continue; | |
83 | |
84 MediaGallery gallery; | |
85 if (PopulateGalleryFromDictionary(dict, &gallery)) | |
86 remembered_galleries_.push_back(gallery); | |
87 } | |
88 } | |
89 | |
90 void MediaGalleryRegistry::AddGalleryByPath(const FilePath& path) { | |
91 PrefService* prefs = profile_->GetPrefs(); | |
92 | |
93 MediaGallery gallery; | |
94 gallery.id = prefs->GetUint64(prefs::kMediaGalleryUniqueId); | |
95 prefs->SetUint64(prefs::kMediaGalleryUniqueId, gallery.id + 1); | |
96 gallery.display_name = path.BaseName().LossyDisplayName(); | |
97 // TODO(estade): make this relative to base_path. | |
98 gallery.path = path; | |
99 // TODO(estade): populate the rest of the fields. | |
100 | |
101 ListPrefUpdate update(prefs, prefs::kMediaGalleryRememberedGalleries); | |
102 ListValue* list = update.Get(); | |
103 list->Append(CreateGalleryDictionary(gallery)); | |
104 | |
105 remembered_galleries_.push_back(gallery); | |
106 } | |
107 | |
108 void MediaGalleryRegistry::ForgetGalleryById(uint64 id) { | |
109 PrefService* prefs = profile_->GetPrefs(); | |
110 ListPrefUpdate update(prefs, prefs::kMediaGalleryRememberedGalleries); | |
111 ListValue* list = update.Get(); | |
112 | |
113 for (ListValue::iterator iter = list->begin(); iter != list->end(); ++iter) { | |
114 DictionaryValue* dict; | |
115 uint64 iter_id; | |
116 if ((*iter)->GetAsDictionary(&dict) && GetId(dict, &iter_id) && | |
117 id == iter_id) { | |
118 list->Erase(iter, NULL); | |
119 InitFromPrefs(); | |
120 return; | |
121 } | |
122 } | |
123 } | |
124 | |
125 void MediaGalleryRegistry::Shutdown() { | |
126 profile_ = NULL; | |
127 } | |
128 | |
129 // static | |
130 bool MediaGalleryRegistry::UserInteractionIsEnabled() { | |
131 return CommandLine::ForCurrentProcess()->HasSwitch( | |
132 switches::kEnableMediaGalleryUI); | |
133 } | |
134 | |
135 // static | |
136 void MediaGalleryRegistry::RegisterUserPrefs(PrefService* prefs) { | |
137 if (!UserInteractionIsEnabled()) | |
138 return; | |
139 | |
140 prefs->RegisterListPref(prefs::kMediaGalleryRememberedGalleries, | |
141 PrefService::UNSYNCABLE_PREF); | |
142 prefs->RegisterUint64Pref(prefs::kMediaGalleryUniqueId, 0, | |
143 PrefService::UNSYNCABLE_PREF); | |
144 } | |
OLD | NEW |