Chat-O-Matic/application/preferences/AppPreferences.cpp

231 lines
4.6 KiB
C++
Raw Normal View History

/*
* Copyright 2012, Casalinuovo Dario. All rights reserved.
* Distributed under the terms of the MIT License.
*/
2021-06-20 12:44:20 -05:00
#include "AppPreferences.h"
2021-06-22 01:06:00 -05:00
#include "Cardie.h"
2010-05-22 09:18:11 -05:00
#include <string.h>
#include <stdlib.h>
2021-06-22 01:06:00 -05:00
template<> const char* AppPreferences::fFolder = APP_NAME;
2021-06-20 12:44:20 -05:00
template<> const char* AppPreferences::fFilename = "preferences";
2013-04-06 14:59:46 -05:00
/* TODO update _Add* methods to
don't take the BPositionIO argument
and to automatically increase
length counters. For example
the _AddBool() method, should
take the BPositionIO argument from
a private class value, and increase
the the size of another class value
respectively. This way the api looks better
and the possibility of bugs related to
size become very minimal : ).
*/
2021-06-20 12:44:20 -05:00
AppPreferencesData::AppPreferencesData()
:
MoveToCurrentWorkspace(true),
RaiseOnMessageReceived(false),
RaiseUserIsTyping(false),
MarkUnreadWindow(true),
2021-06-20 12:44:20 -05:00
HideDeskbar(false),
DisableReplicant(true),
IgnoreEmoticons(true),
NotifyProtocolStatus(true),
NotifyContactStatus(false),
NotifyNewMessage(true),
HideOffline(true),
DisableQuitConfirm(true)
{
}
2021-06-20 12:44:20 -05:00
AppPreferencesData::~AppPreferencesData()
{
}
bool
2021-06-20 12:44:20 -05:00
AppPreferencesData::IsFixedSize() const
{
return false;
}
type_code
2021-06-20 12:44:20 -05:00
AppPreferencesData::TypeCode() const
{
2021-06-20 12:44:20 -05:00
return APP_PREFERENCES_TYPE;
}
bool
2021-06-20 12:44:20 -05:00
AppPreferencesData::AllowsTypeCode(type_code code) const
{
2021-06-20 12:44:20 -05:00
if (code == APP_PREFERENCES_TYPE)
return true;
return false;
}
ssize_t
2021-06-20 12:44:20 -05:00
AppPreferencesData::FlattenedSize() const
{
// NOTE add the size of every settings
// you added.
ssize_t size = sizeof(bool) * 11;
return size;
}
status_t
2021-06-20 12:44:20 -05:00
AppPreferencesData::Flatten(BPositionIO* flatData) const
{
if (flatData == NULL)
return B_BAD_VALUE;
// Write our type code
2021-06-20 12:44:20 -05:00
type_code code = APP_PREFERENCES_TYPE;
flatData->Write(&code, sizeof(type_code));
// Behaviour
_AddBool(flatData, MoveToCurrentWorkspace);
_AddBool(flatData, RaiseOnMessageReceived);
_AddBool(flatData, RaiseUserIsTyping);
_AddBool(flatData, MarkUnreadWindow);
_AddBool(flatData, NotifyProtocolStatus);
_AddBool(flatData, NotifyContactStatus);
_AddBool(flatData, NotifyNewMessage);
_AddBool(flatData, DisableQuitConfirm);
// Replicant
2021-06-20 12:44:20 -05:00
_AddBool(flatData, HideDeskbar);
_AddBool(flatData, DisableReplicant);
// Chat window
_AddBool(flatData, IgnoreEmoticons);
// Contact list
_AddBool(flatData, HideOffline);
// 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.
return B_OK;
}
status_t
2021-06-20 12:44:20 -05:00
AppPreferencesData::Flatten(void* buffer, ssize_t size) const
{
if (buffer == NULL)
return B_BAD_VALUE;
BMemoryIO flatData(buffer, size);
return Flatten(&flatData, size);
}
status_t
2021-06-20 12:44:20 -05:00
AppPreferencesData::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
2021-06-20 12:44:20 -05:00
AppPreferencesData::Unflatten(type_code code, BPositionIO* flatData)
{
2021-06-20 12:44:20 -05:00
if (code != APP_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);
RaiseOnMessageReceived = _ReadBool(flatData);
RaiseUserIsTyping = _ReadBool(flatData);
MarkUnreadWindow = _ReadBool(flatData);
NotifyProtocolStatus = _ReadBool(flatData);
NotifyContactStatus = _ReadBool(flatData);
NotifyNewMessage = _ReadBool(flatData);
DisableQuitConfirm = _ReadBool(flatData);
// Replicant
2021-06-20 12:44:20 -05:00
HideDeskbar = _ReadBool(flatData);
DisableReplicant = _ReadBool(flatData);
// Chat window
IgnoreEmoticons = _ReadBool(flatData);
// Contact list
HideOffline = _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
2021-06-20 12:44:20 -05:00
AppPreferencesData::_AddBool(BPositionIO* data, bool value) const
{
data->Write(&value, sizeof(value));
}
void
2021-06-20 12:44:20 -05:00
AppPreferencesData::_AddString(BPositionIO* data, const char* value) const
{
size_t len = strlen(value);
data->Write(&len, sizeof(size_t));
data->Write(value, len);
}
bool
2021-06-20 12:44:20 -05:00
AppPreferencesData::_ReadBool(BPositionIO* data)
{
bool ret;
data->Read(&ret, sizeof(bool));
return ret;
}
const char*
2021-06-20 12:44:20 -05:00
AppPreferencesData::_ReadString(BPositionIO* data)
{
size_t len;
data->Read(&len, sizeof(size_t));
char* ret = new char[len];
data->Read(ret, len);
return ret;
}