Imported ViewState.cpp|h 1.2 from OpenTracker CVS.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@18996 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2006-10-04 13:06:39 +00:00
parent 1f9f5e4e63
commit f974492c2d
2 changed files with 211 additions and 201 deletions
+125 -123
View File
@@ -32,9 +32,6 @@ names are registered trademarks or trademarks of their respective holders.
All rights reserved.
*/
#include <string.h>
#include <stdlib.h>
#include <Debug.h>
#include <AppDefs.h>
#include <InterfaceDefs.h>
@@ -45,6 +42,11 @@ All rights reserved.
#include "Utilities.h"
#include "ViewState.h"
#include <new>
#include <string.h>
#include <stdlib.h>
const char *kColumnVersionName = "BColumn:version";
const char *kColumnTitleName = "BColumn:fTitle";
const char *kColumnOffsetName = "BColumn:fOffset";
@@ -56,25 +58,41 @@ const char *kColumnAttrTypeName = "BColumn:fAttrType";
const char *kColumnStatFieldName = "BColumn:fStatField";
const char *kColumnEditableName = "BColumn:fEditable";
BColumn::BColumn(const char *title, float offset, float width, alignment a,
const char *attributeName, uint32 attr_type, bool stat_field,
bool editable)
: fTitle(title),
fAttrName(attributeName)
const char *kViewStateVersionName = "ViewState:version";
const char *kViewStateViewModeName = "ViewState:fViewMode";
const char *kViewStateLastIconModeName = "ViewState:fLastIconMode";
const char *kViewStateListOriginName = "ViewState:fListOrigin";
const char *kViewStateIconOriginName = "ViewState:fIconOrigin";
const char *kViewStatePrimarySortAttrName = "ViewState:fPrimarySortAttr";
const char *kViewStatePrimarySortTypeName = "ViewState:fPrimarySortType";
const char *kViewStateSecondarySortAttrName = "ViewState:fSecondarySortAttr";
const char *kViewStateSecondarySortTypeName = "ViewState:fSecondarySortType";
const char *kViewStateReverseSortName = "ViewState:fReverseSort";
const char *kViewStateIconSizeName = "ViewState:fIconSize";
BColumn::BColumn(const char *title, float offset, float width, alignment align,
const char *attributeName, uint32 attrType, bool statField,
bool editable)
:
fTitle(title),
fAttrName(attributeName)
{
fOffset = offset;
fWidth = width;
fAlignment = a;
fAttrHash = AttrHashString(attributeName, attr_type);
fAttrType = attr_type;
fStatField = stat_field;
fAlignment = align;
fAttrHash = AttrHashString(attributeName, attrType);
fAttrType = attrType;
fStatField = statField;
fEditable = editable;
}
BColumn::~BColumn()
{
}
BColumn::BColumn(BMallocIO *stream, bool endianSwap)
{
StringFromStream(&fTitle, stream, endianSwap);
@@ -98,6 +116,7 @@ BColumn::BColumn(BMallocIO *stream, bool endianSwap)
}
}
BColumn::BColumn(const BMessage &message, int32 index)
{
message.FindString(kColumnTitleName, index, &fTitle);
@@ -111,6 +130,7 @@ BColumn::BColumn(const BMessage &message, int32 index)
message.FindBool(kColumnEditableName, index, &fEditable);
}
BColumn *
BColumn::InstantiateFromStream(BMallocIO *stream, bool endianSwap)
{
@@ -118,7 +138,6 @@ BColumn::InstantiateFromStream(BMallocIO *stream, bool endianSwap)
uint32 key = AttrHashString("BColumn", B_OBJECT_TYPE);
int32 version = kColumnStateArchiveVersion;
if (endianSwap) {
key = SwapUInt32(key);
version = SwapInt32(version);
@@ -129,29 +148,10 @@ BColumn::InstantiateFromStream(BMallocIO *stream, bool endianSwap)
return 0;
// PRINT(("instantiating column, %s\n", endianSwap ? "endian swapping," : ""));
BColumn *result = new BColumn(stream, endianSwap);
// sanity-check the resulting column
if (result->fTitle.Length() > 500
|| result->fOffset < 0
|| result->fOffset > 10000
|| result->fWidth < 0
|| result->fWidth > 10000
|| (int32)result->fAlignment < B_ALIGN_LEFT
|| (int32)result->fAlignment > B_ALIGN_CENTER
|| result->fAttrName.Length() > 500) {
PRINT(("column data not valid\n"));
delete result;
return 0;
}
#if DEBUG
else if (endianSwap)
PRINT(("Instantiated foreign column ok\n"));
#endif
return result;
return _Sanitize(new (std::nothrow) BColumn(stream, endianSwap));
}
BColumn *
BColumn::InstantiateFromMessage(const BMessage &message, int32 index)
{
@@ -164,24 +164,10 @@ BColumn::InstantiateFromMessage(const BMessage &message, int32 index)
if (version != messageVersion)
return NULL;
BColumn *result = new BColumn(message, index);
// sanity-check the resulting column
if (result->fTitle.Length() > 500
|| result->fOffset < 0
|| result->fOffset > 10000
|| result->fWidth < 0
|| result->fWidth > 10000
|| (int32)result->fAlignment < B_ALIGN_LEFT
|| (int32)result->fAlignment > B_ALIGN_CENTER
|| result->fAttrName.Length() > 500) {
PRINT(("column data not valid\n"));
delete result;
return NULL;
}
return result;
return _Sanitize(new (std::nothrow) BColumn(message, index));
}
void
BColumn::ArchiveToStream(BMallocIO *stream) const
{
@@ -204,6 +190,7 @@ BColumn::ArchiveToStream(BMallocIO *stream) const
stream->Write(&fEditable, sizeof(bool));
}
void
BColumn::ArchiveToMessage(BMessage &message) const
{
@@ -220,17 +207,37 @@ BColumn::ArchiveToMessage(BMessage &message) const
message.AddBool(kColumnEditableName, fEditable);
}
const char *kViewStateVersionName = "ViewState:version";
const char *kViewStateViewModeName = "ViewState:fViewMode";
const char *kViewStateLastIconModeName = "ViewState:fLastIconMode";
const char *kViewStateListOriginName = "ViewState:fListOrigin";
const char *kViewStateIconOriginName = "ViewState:fIconOrigin";
const char *kViewStatePrimarySortAttrName = "ViewState:fPrimarySortAttr";
const char *kViewStatePrimarySortTypeName = "ViewState:fPrimarySortType";
const char *kViewStateSecondarySortAttrName = "ViewState:fSecondarySortAttr";
const char *kViewStateSecondarySortTypeName = "ViewState:fSecondarySortType";
const char *kViewStateReverseSortName = "ViewState:fReverseSort";
const char *kViewStateIconSizeName = "ViewState:fIconSize";
BColumn *
BColumn::_Sanitize(BColumn *column)
{
if (column == NULL)
return NULL;
// sanity-check the resulting column
if (column->fTitle.Length() > 500
|| column->fOffset < 0
|| column->fOffset > 10000
|| column->fWidth < 0
|| column->fWidth > 10000
|| (int32)column->fAlignment < B_ALIGN_LEFT
|| (int32)column->fAlignment > B_ALIGN_CENTER
|| column->fAttrName.Length() > 500) {
PRINT(("column data not valid\n"));
delete column;
return NULL;
}
#if DEBUG
else if (endianSwap)
PRINT(("Instantiated foreign column ok\n"));
#endif
return column;
}
// #pragma mark -
BViewState::BViewState()
{
@@ -247,6 +254,7 @@ BViewState::BViewState()
fStateNeedsSaving = false;
}
BViewState::BViewState(BMallocIO *stream, bool endianSwap)
{
stream->Read(&fViewMode, sizeof(uint32));
@@ -259,7 +267,7 @@ BViewState::BViewState(BMallocIO *stream, bool endianSwap)
stream->Read(&fSecondarySortType, sizeof(uint32));
stream->Read(&fReverseSort, sizeof(bool));
stream->Read(&fIconSize, sizeof(uint32));
if (endianSwap) {
PRINT(("endian swapping view state\n"));
fViewMode = B_SWAP_INT32(fViewMode);
@@ -273,15 +281,12 @@ BViewState::BViewState(BMallocIO *stream, bool endianSwap)
fSecondarySortType = B_SWAP_INT32(fSecondarySortType);
}
// assure a sane state
if (fIconSize < 16)
fIconSize = 16;
if (fIconSize > 64)
fIconSize = 64;
fStateNeedsSaving = false;
_Sanitize(this, true);
}
BViewState::BViewState(const BMessage &message)
{
message.FindInt32(kViewStateViewModeName, (int32 *)&fViewMode);
@@ -293,17 +298,12 @@ BViewState::BViewState(const BMessage &message)
message.FindInt32(kViewStateSecondarySortAttrName, (int32 *)&fSecondarySortAttr);
message.FindInt32(kViewStateSecondarySortTypeName, (int32 *)&fSecondarySortType);
message.FindBool(kViewStateReverseSortName, &fReverseSort);
message.FindInt32(kViewStateIconSizeName, (int32 *)&fIconSize);
// assure a sane state
if (fIconSize < 16)
fIconSize = 16;
if (fIconSize > 64)
fIconSize = 64;
fStateNeedsSaving = false;
_Sanitize(this, true);
}
void
BViewState::ArchiveToStream(BMallocIO *stream) const
{
@@ -344,14 +344,13 @@ BViewState::ArchiveToMessage(BMessage &message) const
}
BViewState *
BViewState::InstantiateFromStream(BMallocIO *stream, bool endianSwap)
{
// compare stream header in canonical form
uint32 key = AttrHashString("BViewState", B_OBJECT_TYPE);
int32 version = kViewStateArchiveVersion;
if (endianSwap) {
key = SwapUInt32(key);
version = SwapInt32(version);
@@ -360,65 +359,68 @@ BViewState::InstantiateFromStream(BMallocIO *stream, bool endianSwap)
if (!ValidateStream(stream, key, version))
return NULL;
BViewState *result = new BViewState(stream, endianSwap);
// do a sanity check here
if ((result->fViewMode != kListMode
&& result->fViewMode != kIconMode
&& result->fViewMode != kMiniIconMode
&& result->fViewMode != kScaleIconMode
&& result->fViewMode != 0)
|| (result->fLastIconMode != kListMode
&& result->fLastIconMode != kIconMode
&& result->fLastIconMode != kMiniIconMode
&& result->fLastIconMode != kScaleIconMode
&& result->fLastIconMode != 0)) {
PRINT(("Bad data instantiating ViewState, view mode %x, lastIconMode %x\n",
result->fViewMode, result->fLastIconMode));
delete result;
return NULL;
}
#if DEBUG
else if (endianSwap)
PRINT(("Instantiated foreign view state ok\n"));
#endif
return result;
return _Sanitize(new (std::nothrow) BViewState(stream, endianSwap));
}
BViewState *
BViewState::InstantiateFromMessage(const BMessage &message)
{
int32 version = kViewStateArchiveVersion;
int32 messageVersion;
int32 messageVersion;
if (message.FindInt32(kViewStateVersionName, &messageVersion) != B_OK)
return NULL;
if (version != messageVersion)
return NULL;
BViewState *result = new BViewState(message);
// do a sanity check here
if ((result->fViewMode != kListMode
&& result->fViewMode != kIconMode
&& result->fViewMode != kMiniIconMode
&& result->fViewMode != kScaleIconMode
&& result->fViewMode != 0)
|| (result->fLastIconMode != kListMode
&& result->fLastIconMode != kIconMode
&& result->fLastIconMode != kMiniIconMode
&& result->fLastIconMode != kScaleIconMode
&& result->fLastIconMode != 0)) {
PRINT(("Bad data instantiating ViewState, view mode %x, lastIconMode %x\n",
result->fViewMode, result->fLastIconMode));
return _Sanitize(new (std::nothrow) BViewState(message));
}
delete result;
BViewState *
BViewState::_Sanitize(BViewState *state, bool fixOnly)
{
if (state == NULL)
return NULL;
if (state->fViewMode == kListMode) {
if (state->fListOrigin.x < 0)
state->fListOrigin.x = 0;
if (state->fListOrigin.y < 0)
state->fListOrigin.y = 0;
}
if (state->fIconSize < 16)
state->fIconSize = 16;
if (state->fIconSize > 64)
state->fIconSize = 64;
if (fixOnly)
return state;
// do a sanity check here
if ((state->fViewMode != kListMode
&& state->fViewMode != kIconMode
&& state->fViewMode != kMiniIconMode
&& state->fViewMode != kScaleIconMode
&& state->fViewMode != 0)
|| (state->fLastIconMode != kListMode
&& state->fLastIconMode != kIconMode
&& state->fLastIconMode != kMiniIconMode
&& state->fLastIconMode != kScaleIconMode
&& state->fLastIconMode != 0)) {
PRINT(("Bad data instantiating ViewState, view mode %x, lastIconMode %x\n",
state->fViewMode, state->fLastIconMode));
delete state;
return NULL;
}
return result;
#if DEBUG
else if (endianSwap)
PRINT(("Instantiated foreign view state ok\n"));
#endif
return state;
}
+86 -78
View File
@@ -31,55 +31,60 @@ of Be Incorporated in the United States and other countries. Other brand product
names are registered trademarks or trademarks of their respective holders.
All rights reserved.
*/
#ifndef _VIEW_STATE_H
#define _VIEW_STATE_H
#include <DataIO.h>
#include <String.h>
namespace BPrivate {
const int32 kColumnStateArchiveVersion = 21;
// bump version when layout or size changes
class BColumn {
public:
BColumn(const char *title, float offset, float width,
alignment, const char *attributeName, uint32 attr_type,
bool stat_field, bool editable);
~BColumn();
public:
BColumn(const char *title, float offset, float width,
alignment align, const char *attributeName, uint32 attrType,
bool statField, bool editable);
~BColumn();
BColumn(BMallocIO *stream, bool endianSwap = false);
BColumn(const BMessage &, int32 index = 0);
static BColumn *InstantiateFromStream(BMallocIO *stream, bool endianSwap = false);
static BColumn *InstantiateFromMessage(const BMessage &, int32 index = 0);
void ArchiveToStream(BMallocIO *stream) const;
void ArchiveToMessage(BMessage &) const;
BColumn(BMallocIO *stream, bool endianSwap = false);
BColumn(const BMessage &, int32 index = 0);
static BColumn *InstantiateFromStream(BMallocIO *stream,
bool endianSwap = false);
static BColumn *InstantiateFromMessage(const BMessage &archive,
int32 index = 0);
void ArchiveToStream(BMallocIO *stream) const;
void ArchiveToMessage(BMessage &) const;
const char *Title() const;
float Offset() const;
float Width() const;
alignment Alignment() const;
const char *AttrName() const;
uint32 AttrType() const;
uint32 AttrHash() const;
bool StatField() const;
bool Editable() const;
void SetOffset(float);
void SetWidth(float);
const char *Title() const;
float Offset() const;
float Width() const;
alignment Alignment() const;
const char *AttrName() const;
uint32 AttrType() const;
uint32 AttrHash() const;
bool StatField() const;
bool Editable() const;
private:
BString fTitle;
float fOffset;
float fWidth;
alignment fAlignment;
BString fAttrName;
uint32 fAttrHash;
uint32 fAttrType;
bool fStatField;
bool fEditable;
void SetOffset(float);
void SetWidth(float);
private:
static BColumn *_Sanitize(BColumn *column);
BString fTitle;
float fOffset;
float fWidth;
alignment fAlignment;
BString fAttrName;
uint32 fAttrHash;
uint32 fAttrType;
bool fStatField;
bool fEditable;
};
@@ -88,54 +93,57 @@ const int32 kViewStateArchiveVersion = 10;
class BViewState {
public:
BViewState();
BViewState(BMallocIO *stream, bool endianSwap = false);
BViewState(const BMessage &message);
static BViewState *InstantiateFromStream(BMallocIO *stream, bool endianSwap = false);
static BViewState *InstantiateFromMessage(const BMessage &message);
void ArchiveToStream(BMallocIO *stream) const;
void ArchiveToMessage(BMessage &message) const;
BViewState();
uint32 ViewMode() const;
uint32 LastIconMode() const;
uint32 IconSize() const;
BPoint ListOrigin() const;
BPoint IconOrigin() const;
uint32 PrimarySort() const;
uint32 SecondarySort() const;
uint32 PrimarySortType() const;
uint32 SecondarySortType() const;
bool ReverseSort() const;
void SetViewMode(uint32);
void SetLastIconMode(uint32);
void SetIconSize(uint32);
void SetListOrigin(BPoint);
void SetIconOrigin(BPoint);
void SetPrimarySort(uint32);
void SetSecondarySort(uint32);
void SetPrimarySortType(uint32);
void SetSecondarySortType(uint32);
void SetReverseSort(bool);
bool StateNeedsSaving();
void MarkSaved();
BViewState(BMallocIO *stream, bool endianSwap = false);
BViewState(const BMessage &message);
static BViewState *InstantiateFromStream(BMallocIO *stream, bool endianSwap = false);
static BViewState *InstantiateFromMessage(const BMessage &message);
void ArchiveToStream(BMallocIO *stream) const;
void ArchiveToMessage(BMessage &message) const;
private:
uint32 fViewMode;
uint32 fLastIconMode;
uint32 fIconSize;
BPoint fListOrigin;
BPoint fIconOrigin;
uint32 fPrimarySortAttr;
uint32 fSecondarySortAttr;
uint32 fPrimarySortType;
uint32 fSecondarySortType;
bool fReverseSort;
bool fStateNeedsSaving;
uint32 ViewMode() const;
uint32 LastIconMode() const;
uint32 IconSize() const;
BPoint ListOrigin() const;
BPoint IconOrigin() const;
uint32 PrimarySort() const;
uint32 SecondarySort() const;
uint32 PrimarySortType() const;
uint32 SecondarySortType() const;
bool ReverseSort() const;
void SetViewMode(uint32);
void SetLastIconMode(uint32);
void SetIconSize(uint32);
void SetListOrigin(BPoint);
void SetIconOrigin(BPoint);
void SetPrimarySort(uint32);
void SetSecondarySort(uint32);
void SetPrimarySortType(uint32);
void SetSecondarySortType(uint32);
void SetReverseSort(bool);
bool StateNeedsSaving();
void MarkSaved();
private:
static BViewState *_Sanitize(BViewState *state, bool fixOnly = false);
uint32 fViewMode;
uint32 fLastIconMode;
uint32 fIconSize;
BPoint fListOrigin;
BPoint fIconOrigin;
uint32 fPrimarySortAttr;
uint32 fSecondarySortAttr;
uint32 fPrimarySortType;
uint32 fSecondarySortType;
bool fReverseSort;
bool fStateNeedsSaving;
};
inline const char *
BColumn::Title() const
{