Modified the CayaPreferencesData class to become a BFlattenable, this remove the limit of having a fixed-size buffer for string settings.

This commit is contained in:
barrett 2012-03-24 14:56:00 +00:00
parent b96ea4c6bf
commit 7318f6f28b
3 changed files with 254 additions and 44 deletions

View File

@ -1,4 +1,204 @@
/*
* Copyright 2012, Casalinuovo Dario. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#include "CayaPreferences.h"
#include <string.h>
#include <stdlib.h>
template<> const char* CayaPreferences::fFolder = "Caya";
template<> const char* CayaPreferences::fFilename = "preferences";
CayaPreferencesData::CayaPreferencesData()
:
MoveToCurrentWorkspace(true),
FocusOnMessageReceived(false),
FocusUserIsTyping(false),
HideCayaDeskbar(false),
DisableReplicant(true),
IgnoreEmoticons(false),
NotifyProtocolStatus(true),
NotifyContactStatus(false)
{
}
CayaPreferencesData::~CayaPreferencesData()
{
}
bool
CayaPreferencesData::IsFixedSize() const
{
return false;
}
type_code
CayaPreferencesData::TypeCode() const
{
return CAYA_PREFERENCES_TYPE;
}
bool
CayaPreferencesData::AllowsTypeCode(type_code code) const
{
if (code == CAYA_PREFERENCES_TYPE)
return true;
return false;
}
ssize_t
CayaPreferencesData::FlattenedSize() const
{
// NOTE add the size of every settings
// you added.
ssize_t size = sizeof(bool) * 8;
return size;
}
status_t
CayaPreferencesData::Flatten(BPositionIO* flatData) const
{
if (flatData == NULL)
return B_BAD_VALUE;
// Write our type code
type_code code = CAYA_PREFERENCES_TYPE;
flatData->Write(&code, sizeof(type_code));
// Behaviour
_AddBool(flatData, MoveToCurrentWorkspace);
_AddBool(flatData, FocusOnMessageReceived);
_AddBool(flatData, FocusUserIsTyping);
_AddBool(flatData, NotifyProtocolStatus);
_AddBool(flatData, NotifyContactStatus);
// Replicant
_AddBool(flatData, HideCayaDeskbar);
_AddBool(flatData, DisableReplicant);
// Chat window
_AddBool(flatData, IgnoreEmoticons);
// Usage example for strings :
// _AddString(flatData, yourBString.String());
// NOTE : The order is very important, Unflatten and Flatten
// classes should read/write the values in the same order.
_AddString(flatData, "str");
return B_OK;
}
status_t
CayaPreferencesData::Flatten(void* buffer, ssize_t size) const
{
if (buffer == NULL)
return B_BAD_VALUE;
BMemoryIO flatData(buffer, size);
return Flatten(&flatData, size);
}
status_t
CayaPreferencesData::Unflatten(type_code code, const void* buffer, ssize_t size)
{
if (buffer == NULL)
return B_BAD_VALUE;
BMemoryIO flatData(buffer, size);
return Unflatten(code, &flatData);
}
status_t
CayaPreferencesData::Unflatten(type_code code, BPositionIO* flatData)
{
if (code != CAYA_PREFERENCES_TYPE || flatData == NULL)
return B_BAD_VALUE;
// Reading our type code
type_code typeCode;
flatData->Read(&typeCode, sizeof(type_code));
// checking if the typecode is correct
if (code != typeCode)
return B_BAD_VALUE;
// Behaviour
MoveToCurrentWorkspace = _ReadBool(flatData);
FocusOnMessageReceived = _ReadBool(flatData);
FocusUserIsTyping = _ReadBool(flatData);
NotifyProtocolStatus = _ReadBool(flatData);
NotifyContactStatus = _ReadBool(flatData);
// Replicant
HideCayaDeskbar = _ReadBool(flatData);
DisableReplicant = _ReadBool(flatData);
// Chat window
IgnoreEmoticons = _ReadBool(flatData);
// Usage example for strings :
// const char* str = _ReadString(flatData);
// yourBString.SetTo(str);
// NOTE : The order is very important, Unflatten and Flatten
// classes should read/write the values in the same order.
return B_OK;
}
void
CayaPreferencesData::_AddBool(BPositionIO* data, bool value) const
{
data->Write(&value, sizeof(value));
}
void
CayaPreferencesData::_AddString(BPositionIO* data, const char* value) const
{
size_t len = strlen(value);
data->Write(&len, sizeof(size_t));
data->Write(value, len);
}
bool
CayaPreferencesData::_ReadBool(BPositionIO* data)
{
bool ret;
data->Read(&ret, sizeof(bool));
return ret;
}
const char*
CayaPreferencesData::_ReadString(BPositionIO* data)
{
size_t len;
char* ret;
data->Read(&len, sizeof(size_t));
ret = new char[len];
data->Read(ret, len);
return ret;
}

View File

@ -1,5 +1,6 @@
/*
* Copyright 2010, Oliver Ruiz Dorantes. All rights reserved.
* Copyright 2012, Casalinuovo Dario. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef _CAYA_PREFERENCES_H
@ -7,33 +8,41 @@
#include "PreferencesContainer.h"
typedef struct _CayaPreferencesData
{
bool MoveToCurrentWorkspace;
bool FocusOnMessageReceived;
bool FocusUserIsTyping;
bool HideCayaDeskbar;
bool DisableReplicant;
class CayaPreferencesData : public BFlattenable {
public:
CayaPreferencesData();
virtual ~CayaPreferencesData();
bool IgnoreEmoticons;
virtual bool IsFixedSize() const;
virtual type_code TypeCode() const;
virtual bool AllowsTypeCode(type_code code) const;
virtual ssize_t FlattenedSize() const;
bool NotifyProtocolStatus;
bool NotifyContactStatus;
status_t Flatten(BPositionIO* flatData) const;
virtual status_t Flatten(void* buffer, ssize_t size) const;
virtual status_t Unflatten(type_code code, const void* buffer,
ssize_t size);
status_t Unflatten(type_code code, BPositionIO* flatData);
_CayaPreferencesData()
:
MoveToCurrentWorkspace(true),
FocusOnMessageReceived(false),
FocusUserIsTyping(false),
HideCayaDeskbar(false),
DisableReplicant(true),
IgnoreEmoticons(false),
NotifyProtocolStatus(true),
NotifyContactStatus(false)
{
}
} CayaPreferencesData;
bool MoveToCurrentWorkspace;
bool FocusOnMessageReceived;
bool FocusUserIsTyping;
bool NotifyProtocolStatus;
bool NotifyContactStatus;
bool HideCayaDeskbar;
bool DisableReplicant;
bool IgnoreEmoticons;
private:
void _AddBool(BPositionIO* data, bool value) const;
void _AddString(BPositionIO* data,
const char* value) const;
bool _ReadBool(BPositionIO* data);
const char* _ReadString(BPositionIO* data);
};
typedef PreferencesContainer<CayaPreferencesData> CayaPreferences;

View File

@ -1,5 +1,6 @@
/*
* Copyright 2010, Oliver Ruiz Dorantes. All rights reserved.
* Copyright 2012, Casalinuovo Dario. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef _PREFERENCES_CONTAINER_H
@ -12,19 +13,25 @@
#include <FindDirectory.h>
#include <Path.h>
enum {
CAYA_PREFERENCES_TYPE = 'CPTY'
};
// TODO: added to main singleton class?
template<typename T> T* Singleton<T>::fInstance = 0;
template<class SettingsType>
class PreferencesContainer : public Singleton<PreferencesContainer<SettingsType> > {
template<class CayaPreferencesData>
class PreferencesContainer
: public Singleton<PreferencesContainer<CayaPreferencesData> > {
public:
static SettingsType*
static CayaPreferencesData*
Item()
{
return &(Singleton<PreferencesContainer<SettingsType> >::Get()->fSettings);
return &(Singleton<PreferencesContainer<CayaPreferencesData> >
::Get()->fSettings);
}
@ -33,13 +40,9 @@ public:
if (fPreferencesFile.SetTo(&fDirectory, fFilename,
B_READ_WRITE | B_FAIL_IF_EXISTS) == B_OK) {
// reset the file pointer
fPreferencesFile.Seek(0, SEEK_SET);
if (fPreferencesFile.Read(&fSettings, sizeof(SettingsType)) > 0)
return B_OK;
return fSettings.Unflatten(CAYA_PREFERENCES_TYPE,
&fPreferencesFile);
}
return B_ERROR;
}
@ -49,16 +52,14 @@ public:
if (fPreferencesFile.SetTo(&fDirectory, fFilename,
B_READ_WRITE | B_CREATE_FILE | B_ERASE_FILE) == B_OK) {
if (fPreferencesFile.Write(&fSettings, sizeof(SettingsType)) > 0)
return B_OK;
return fSettings.Flatten(&fPreferencesFile);
}
return B_ERROR;
}
private:
PreferencesContainer<SettingsType>()
: Singleton<PreferencesContainer<SettingsType> >()
PreferencesContainer<CayaPreferencesData>()
: Singleton<PreferencesContainer<CayaPreferencesData> >()
{
BPath path;
@ -69,14 +70,14 @@ private:
Load();
}
friend class Singleton<PreferencesContainer<SettingsType> >;
CayaPreferencesData fSettings;
BFile fPreferencesFile;
BDirectory fDirectory;
SettingsType fSettings;
BFile fPreferencesFile;
BDirectory fDirectory;
static const char* fFilename;
static const char* fFolder;
static const char* fFilename;
static const char* fFolder;
friend class Singleton<PreferencesContainer<CayaPreferencesData> >;
};