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