xref: /haiku/src/apps/webpositive/CredentialsStorage.h (revision fc7456e9b1ec38c941134ed6d01c438cf289381e)
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 
10 #include <Locker.h>
11 #include <String.h>
12 
13 #include "HashMap.h"
14 #include "HashString.h"
15 
16 
17 class BFile;
18 class BMessage;
19 class BString;
20 
21 
22 class Credentials {
23 public:
24 								Credentials();
25 								Credentials(const BString& username,
26 									const BString& password);
27 								Credentials(
28 									const Credentials& other);
29 								Credentials(const BMessage* archive);
30 								~Credentials();
31 
32 			status_t			Archive(BMessage* archive) const;
33 
34 			Credentials&		operator=(const Credentials& other);
35 
36 			bool				operator==(const Credentials& other) const;
37 			bool				operator!=(const Credentials& other) const;
38 
39 			const BString&		Username() const;
40 			const BString&		Password() const;
41 
42 private:
43 			BString				fUsername;
44 			BString				fPassword;
45 };
46 
47 
48 class CredentialsStorage : public BLocker {
49 public:
50 	static	CredentialsStorage*	SessionInstance();
51 	static	CredentialsStorage*	PersistentInstance();
52 
53 			bool				Contains(const BPrivate::HashString& key);
54 			status_t			PutCredentials(const HashString& key,
55 									const Credentials& credentials);
56 			Credentials			GetCredentials(const HashString& key);
57 
58 private:
59 								CredentialsStorage(bool persistent);
60 	virtual						~CredentialsStorage();
61 
62 			void				_LoadSettings();
63 			void				_SaveSettings() const;
64 			bool				_OpenSettingsFile(BFile& file,
65 									uint32 mode) const;
66 
67 private:
68 			typedef HashMap<HashString, Credentials> CredentialMap;
69 			CredentialMap		fCredentialMap;
70 
71 	static	CredentialsStorage	sPersistentInstance;
72 	static	CredentialsStorage	sSessionInstance;
73 			bool				fSettingsLoaded;
74 			bool				fPersistent;
75 };
76 
77 
78 #endif // CREDENTIAL_STORAGE_H
79