xref: /haiku/src/apps/webpositive/CredentialsStorage.h (revision 040a81419dda83d1014e9dc94936a4cb3f027303)
1 /*
2  * Copyright (C) 2010 Stephan Aßmus <superstippi@gmx.de>
3  *
4  * All rights reserved. Distributed under the terms of the MIT License.
5  */
6 #ifndef CREDENTIAL_STORAGE_H
7 #define CREDENTIAL_STORAGE_H
8 
9 #include "HashKeys.h"
10 #include "HashMap.h"
11 #include <Locker.h>
12 #include <String.h>
13 
14 
15 class BFile;
16 class BMessage;
17 class BString;
18 
19 
20 class Credentials {
21 public:
22 								Credentials();
23 								Credentials(const BString& username,
24 									const BString& password);
25 								Credentials(
26 									const Credentials& other);
27 								Credentials(const BMessage* archive);
28 								~Credentials();
29 
30 			status_t			Archive(BMessage* archive) const;
31 
32 			Credentials&		operator=(const Credentials& other);
33 
34 			bool				operator==(const Credentials& other) const;
35 			bool				operator!=(const Credentials& other) const;
36 
37 			const BString&		Username() const;
38 			const BString&		Password() const;
39 
40 private:
41 			BString				fUsername;
42 			BString				fPassword;
43 };
44 
45 
46 class CredentialsStorage : public BLocker {
47 public:
48 	static	CredentialsStorage*	SessionInstance();
49 	static	CredentialsStorage*	PersistentInstance();
50 
51 			bool				Contains(const HashKeyString& key);
52 			status_t			PutCredentials(const HashKeyString& key,
53 									const Credentials& credentials);
54 			Credentials			GetCredentials(const HashKeyString& key);
55 
56 private:
57 								CredentialsStorage(bool persistent);
58 	virtual						~CredentialsStorage();
59 
60 			void				_LoadSettings();
61 			void				_SaveSettings() const;
62 			bool				_OpenSettingsFile(BFile& file,
63 									uint32 mode) const;
64 
65 private:
66 			typedef HashMap<HashKeyString, Credentials> CredentialMap;
67 			CredentialMap		fCredentialMap;
68 
69 	static	CredentialsStorage	sPersistentInstance;
70 	static	CredentialsStorage	sSessionInstance;
71 			bool				fSettingsLoaded;
72 			bool				fPersistent;
73 };
74 
75 
76 #endif // CREDENTIAL_STORAGE_H
77 
78