2010-05-19 13:05:30 -05:00
|
|
|
/*
|
|
|
|
* Copyright 2010, Oliver Ruiz Dorantes. All rights reserved.
|
2012-03-24 09:56:00 -05:00
|
|
|
* Copyright 2012, Casalinuovo Dario. All rights reserved.
|
2010-05-19 13:05:30 -05:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
|
|
|
#ifndef _PREFERENCES_CONTAINER_H
|
|
|
|
#define _PREFERENCES_CONTAINER_H
|
|
|
|
|
|
|
|
#include <libsupport/Singleton.h>
|
|
|
|
|
2010-05-22 09:17:02 -05:00
|
|
|
#include <Directory.h>
|
|
|
|
#include <File.h>
|
|
|
|
#include <FindDirectory.h>
|
|
|
|
#include <Path.h>
|
|
|
|
|
2012-03-24 09:56:00 -05:00
|
|
|
enum {
|
2021-06-20 12:44:20 -05:00
|
|
|
APP_PREFERENCES_TYPE = 'CPTY'
|
2012-03-24 09:56:00 -05:00
|
|
|
};
|
|
|
|
|
2010-05-19 13:05:30 -05:00
|
|
|
// TODO: added to main singleton class?
|
|
|
|
template<typename T> T* Singleton<T>::fInstance = 0;
|
|
|
|
|
|
|
|
|
2021-06-20 12:44:20 -05:00
|
|
|
template<class AppPreferencesData>
|
2012-03-24 09:56:00 -05:00
|
|
|
class PreferencesContainer
|
2021-06-20 12:44:20 -05:00
|
|
|
: public Singleton<PreferencesContainer<AppPreferencesData> > {
|
2010-05-19 13:05:30 -05:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2021-06-20 12:44:20 -05:00
|
|
|
static AppPreferencesData*
|
2010-05-19 13:05:30 -05:00
|
|
|
Item()
|
|
|
|
{
|
2021-06-20 12:44:20 -05:00
|
|
|
return &(Singleton<PreferencesContainer<AppPreferencesData> >
|
2012-03-24 09:56:00 -05:00
|
|
|
::Get()->fSettings);
|
2010-05-19 13:05:30 -05:00
|
|
|
}
|
|
|
|
|
2010-05-22 09:17:02 -05:00
|
|
|
|
|
|
|
status_t Load()
|
|
|
|
{
|
|
|
|
if (fPreferencesFile.SetTo(&fDirectory, fFilename,
|
|
|
|
B_READ_WRITE | B_FAIL_IF_EXISTS) == B_OK) {
|
|
|
|
|
2021-06-20 12:44:20 -05:00
|
|
|
return fSettings.Unflatten(APP_PREFERENCES_TYPE,
|
2012-03-24 09:56:00 -05:00
|
|
|
&fPreferencesFile);
|
2010-05-22 09:17:02 -05:00
|
|
|
}
|
|
|
|
return B_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t Save()
|
|
|
|
{
|
|
|
|
if (fPreferencesFile.SetTo(&fDirectory, fFilename,
|
|
|
|
B_READ_WRITE | B_CREATE_FILE | B_ERASE_FILE) == B_OK) {
|
|
|
|
|
2012-03-24 09:56:00 -05:00
|
|
|
return fSettings.Flatten(&fPreferencesFile);
|
2010-05-22 09:17:02 -05:00
|
|
|
}
|
|
|
|
return B_ERROR;
|
|
|
|
}
|
2010-05-19 13:05:30 -05:00
|
|
|
|
|
|
|
private:
|
2021-06-20 12:44:20 -05:00
|
|
|
PreferencesContainer<AppPreferencesData>()
|
|
|
|
: Singleton<PreferencesContainer<AppPreferencesData> >()
|
2010-05-22 09:17:02 -05:00
|
|
|
{
|
|
|
|
BPath path;
|
|
|
|
|
|
|
|
find_directory(B_USER_SETTINGS_DIRECTORY, &path);
|
|
|
|
path.Append(fFolder);
|
|
|
|
fDirectory.SetTo(path.Path());
|
|
|
|
|
|
|
|
Load();
|
|
|
|
}
|
|
|
|
|
2021-06-20 12:44:20 -05:00
|
|
|
AppPreferencesData fSettings;
|
2012-03-24 09:56:00 -05:00
|
|
|
BFile fPreferencesFile;
|
|
|
|
BDirectory fDirectory;
|
2010-05-22 09:17:02 -05:00
|
|
|
|
2012-03-24 09:56:00 -05:00
|
|
|
static const char* fFilename;
|
|
|
|
static const char* fFolder;
|
2010-05-19 13:05:30 -05:00
|
|
|
|
2021-06-20 12:44:20 -05:00
|
|
|
friend class Singleton<PreferencesContainer<AppPreferencesData> >;
|
2010-05-19 13:05:30 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif // _PREFERENCES_CONTAINER_H
|