2 * Copyright (C) 2010 Stephan Aßmus <superstippi@gmx.de>
4 * All rights reserved. Distributed under the terms of the MIT License.
7 #include "CredentialsStorage.h"
15 #include <FindDirectory.h>
19 #include "BrowserApp.h"
22 Credentials::Credentials()
30 Credentials::Credentials(const BString
& username
, const BString
& password
)
38 Credentials::Credentials(const Credentials
& other
)
44 Credentials::Credentials(const BMessage
* archive
)
48 archive
->FindString("username", &fUsername
);
49 archive
->FindString("password", &fPassword
);
53 Credentials::~Credentials()
59 Credentials::Archive(BMessage
* archive
) const
63 status_t status
= archive
->AddString("username", fUsername
);
65 status
= archive
->AddString("password", fPassword
);
71 Credentials::operator=(const Credentials
& other
)
76 fUsername
= other
.fUsername
;
77 fPassword
= other
.fPassword
;
84 Credentials::operator==(const Credentials
& other
) const
89 return fUsername
== other
.fUsername
&& fPassword
== other
.fPassword
;
94 Credentials::operator!=(const Credentials
& other
) const
96 return !(*this == other
);
101 Credentials::Username() const
108 Credentials::Password() const
114 // #pragma mark - CredentialsStorage
118 CredentialsStorage::sPersistentInstance(true);
122 CredentialsStorage::sSessionInstance(false);
125 CredentialsStorage::CredentialsStorage(bool persistent
)
127 BLocker(persistent
? "persistent credential storage"
128 : "credential storage"),
130 fSettingsLoaded(false),
131 fPersistent(persistent
)
136 CredentialsStorage::~CredentialsStorage()
142 /*static*/ CredentialsStorage
*
143 CredentialsStorage::SessionInstance()
145 return &sSessionInstance
;
149 /*static*/ CredentialsStorage
*
150 CredentialsStorage::PersistentInstance()
152 if (sPersistentInstance
.Lock()) {
153 sPersistentInstance
._LoadSettings();
154 sPersistentInstance
.Unlock();
156 return &sPersistentInstance
;
161 CredentialsStorage::Contains(const HashKeyString
& key
)
165 return fCredentialMap
.ContainsKey(key
);
170 CredentialsStorage::PutCredentials(const HashKeyString
& key
,
171 const Credentials
& credentials
)
175 return fCredentialMap
.Put(key
, credentials
);
180 CredentialsStorage::GetCredentials(const HashKeyString
& key
)
184 return fCredentialMap
.Get(key
);
188 // #pragma mark - private
192 CredentialsStorage::_LoadSettings()
194 if (!fPersistent
|| fSettingsLoaded
)
197 fSettingsLoaded
= true;
200 if (_OpenSettingsFile(settingsFile
, B_READ_ONLY
)) {
201 BMessage settingsArchive
;
202 settingsArchive
.Unflatten(&settingsFile
);
203 BMessage credentialsArchive
;
204 for (int32 i
= 0; settingsArchive
.FindMessage("credentials", i
,
205 &credentialsArchive
) == B_OK
; i
++) {
207 if (credentialsArchive
.FindString("key", &key
) == B_OK
) {
208 Credentials
credentials(&credentialsArchive
);
209 fCredentialMap
.Put(key
, credentials
);
217 CredentialsStorage::_SaveSettings() const
220 if (_OpenSettingsFile(settingsFile
,
221 B_CREATE_FILE
| B_ERASE_FILE
| B_WRITE_ONLY
)) {
222 BMessage settingsArchive
;
223 BMessage credentialsArchive
;
224 CredentialMap::Iterator iterator
= fCredentialMap
.GetIterator();
225 while (iterator
.HasNext()) {
226 const CredentialMap::Entry
& entry
= iterator
.Next();
227 if (entry
.value
.Archive(&credentialsArchive
) != B_OK
228 || credentialsArchive
.AddString("key",
229 entry
.key
.value
) != B_OK
) {
232 if (settingsArchive
.AddMessage("credentials",
233 &credentialsArchive
) != B_OK
) {
236 credentialsArchive
.MakeEmpty();
238 settingsArchive
.Flatten(&settingsFile
);
244 CredentialsStorage::_OpenSettingsFile(BFile
& file
, uint32 mode
) const
247 if (find_directory(B_USER_SETTINGS_DIRECTORY
, &path
) != B_OK
248 || path
.Append(kApplicationName
) != B_OK
249 || path
.Append("CredentialsStorage") != B_OK
) {
252 return file
.SetTo(path
.Path(), mode
) == B_OK
;