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 "CayaPreferences.h"
#include <string.h>
#include <stdlib.h>
template<> const char* CayaPreferences::fFolder = "Caya"; template<> const char* CayaPreferences::fFolder = "Caya";
template<> const char* CayaPreferences::fFilename = "preferences"; 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 2010, Oliver Ruiz Dorantes. All rights reserved.
* Copyright 2012, Casalinuovo Dario. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
#ifndef _CAYA_PREFERENCES_H #ifndef _CAYA_PREFERENCES_H
@ -7,33 +8,41 @@
#include "PreferencesContainer.h" #include "PreferencesContainer.h"
typedef struct _CayaPreferencesData
{
bool MoveToCurrentWorkspace;
bool FocusOnMessageReceived;
bool FocusUserIsTyping;
bool HideCayaDeskbar; class CayaPreferencesData : public BFlattenable {
bool DisableReplicant; 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; status_t Flatten(BPositionIO* flatData) const;
bool NotifyContactStatus; 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() bool MoveToCurrentWorkspace;
: bool FocusOnMessageReceived;
MoveToCurrentWorkspace(true), bool FocusUserIsTyping;
FocusOnMessageReceived(false), bool NotifyProtocolStatus;
FocusUserIsTyping(false), bool NotifyContactStatus;
HideCayaDeskbar(false),
DisableReplicant(true), bool HideCayaDeskbar;
IgnoreEmoticons(false), bool DisableReplicant;
NotifyProtocolStatus(true),
NotifyContactStatus(false) bool IgnoreEmoticons;
{ private:
} void _AddBool(BPositionIO* data, bool value) const;
} CayaPreferencesData; void _AddString(BPositionIO* data,
const char* value) const;
bool _ReadBool(BPositionIO* data);
const char* _ReadString(BPositionIO* data);
};
typedef PreferencesContainer<CayaPreferencesData> CayaPreferences; typedef PreferencesContainer<CayaPreferencesData> CayaPreferences;

View File

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