btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / apps / webpositive / CredentialsStorage.cpp
blobbd5498b5a7eff7d135030079996a4152b60f0234
1 /*
2 * Copyright (C) 2010 Stephan Aßmus <superstippi@gmx.de>
4 * All rights reserved. Distributed under the terms of the MIT License.
5 */
7 #include "CredentialsStorage.h"
9 #include <new>
10 #include <stdio.h>
12 #include <Autolock.h>
13 #include <Entry.h>
14 #include <File.h>
15 #include <FindDirectory.h>
16 #include <Message.h>
17 #include <Path.h>
19 #include "BrowserApp.h"
22 Credentials::Credentials()
24 fUsername(),
25 fPassword()
30 Credentials::Credentials(const BString& username, const BString& password)
32 fUsername(username),
33 fPassword(password)
38 Credentials::Credentials(const Credentials& other)
40 *this = other;
44 Credentials::Credentials(const BMessage* archive)
46 if (archive == NULL)
47 return;
48 archive->FindString("username", &fUsername);
49 archive->FindString("password", &fPassword);
53 Credentials::~Credentials()
58 status_t
59 Credentials::Archive(BMessage* archive) const
61 if (archive == NULL)
62 return B_BAD_VALUE;
63 status_t status = archive->AddString("username", fUsername);
64 if (status == B_OK)
65 status = archive->AddString("password", fPassword);
66 return status;
70 Credentials&
71 Credentials::operator=(const Credentials& other)
73 if (this == &other)
74 return *this;
76 fUsername = other.fUsername;
77 fPassword = other.fPassword;
79 return *this;
83 bool
84 Credentials::operator==(const Credentials& other) const
86 if (this == &other)
87 return true;
89 return fUsername == other.fUsername && fPassword == other.fPassword;
93 bool
94 Credentials::operator!=(const Credentials& other) const
96 return !(*this == other);
100 const BString&
101 Credentials::Username() const
103 return fUsername;
107 const BString&
108 Credentials::Password() const
110 return fPassword;
114 // #pragma mark - CredentialsStorage
117 CredentialsStorage
118 CredentialsStorage::sPersistentInstance(true);
121 CredentialsStorage
122 CredentialsStorage::sSessionInstance(false);
125 CredentialsStorage::CredentialsStorage(bool persistent)
127 BLocker(persistent ? "persistent credential storage"
128 : "credential storage"),
129 fCredentialMap(),
130 fSettingsLoaded(false),
131 fPersistent(persistent)
136 CredentialsStorage::~CredentialsStorage()
138 _SaveSettings();
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;
160 bool
161 CredentialsStorage::Contains(const HashKeyString& key)
163 BAutolock _(this);
165 return fCredentialMap.ContainsKey(key);
169 status_t
170 CredentialsStorage::PutCredentials(const HashKeyString& key,
171 const Credentials& credentials)
173 BAutolock _(this);
175 return fCredentialMap.Put(key, credentials);
179 Credentials
180 CredentialsStorage::GetCredentials(const HashKeyString& key)
182 BAutolock _(this);
184 return fCredentialMap.Get(key);
188 // #pragma mark - private
191 void
192 CredentialsStorage::_LoadSettings()
194 if (!fPersistent || fSettingsLoaded)
195 return;
197 fSettingsLoaded = true;
199 BFile settingsFile;
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++) {
206 BString key;
207 if (credentialsArchive.FindString("key", &key) == B_OK) {
208 Credentials credentials(&credentialsArchive);
209 fCredentialMap.Put(key, credentials);
216 void
217 CredentialsStorage::_SaveSettings() const
219 BFile settingsFile;
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) {
230 break;
232 if (settingsArchive.AddMessage("credentials",
233 &credentialsArchive) != B_OK) {
234 break;
236 credentialsArchive.MakeEmpty();
238 settingsArchive.Flatten(&settingsFile);
243 bool
244 CredentialsStorage::_OpenSettingsFile(BFile& file, uint32 mode) const
246 BPath path;
247 if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK
248 || path.Append(kApplicationName) != B_OK
249 || path.Append("CredentialsStorage") != B_OK) {
250 return false;
252 return file.SetTo(path.Path(), mode) == B_OK;