#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ByteOrder.h>
#include <Debug.h>
#include <Message.h>
#include <PropertyInfo.h>
#include "Tracker.h"
#include "PoseView.h"
#define kPosesSuites "suite/vnd.Be-TrackerPoses"
#define kPropertyPath "Path"
#if 0
doo Tracker get Suites of Poses of Window test
doo Tracker get Path of Poses of Window test
doo Tracker count Entry of Poses of Window test
doo Tracker get Entry of Poses of Window test
doo Tracker get Entry 2 of Poses of Window test
doo Tracker count Selection of Poses of Window test
doo Tracker get Selection of Poses of Window test
doo Tracker delete Entry 'test/6L6' of Poses of Window test
doo Tracker execute Entry 'test/6L6' of Poses of Window test
doo Tracker execute Entry 2 of Poses of Window test
doo Tracker set Selection of Poses of Window test to [0,2]
doo Tracker set Selection of Poses of Window test to 'test/KT55'
doo Tracker create Selection of Poses of Window test to 'test/EL34'
doo Tracker delete Selection 'test/EL34' of Poses of Window test
#endif
const property_info kPosesPropertyList[] = {
{ kPropertyPath,
{ B_GET_PROPERTY },
{ B_DIRECT_SPECIFIER },
"get Path of ... # returns the path of a Tracker window, "
"error if no path associated",
0,
{ B_REF_TYPE },
{},
{}
},
{ kPropertyEntry,
{ B_COUNT_PROPERTIES },
{ B_DIRECT_SPECIFIER },
"count Entry of ... # count entries in a PoseView",
0,
{ B_INT32_TYPE },
{},
{}
},
{ kPropertyEntry,
{ B_DELETE_PROPERTY },
{ B_ENTRY_SPECIFIER, B_INDEX_SPECIFIER },
"delete Entry {path|index} # deletes specified entries in a PoseView",
0,
{},
{},
{}
},
{ kPropertyEntry,
{ B_GET_PROPERTY },
{ B_DIRECT_SPECIFIER, B_INDEX_SPECIFIER, kPreviousSpecifier,
kNextSpecifier },
"get Entry [next|previous|index] # returns specified entries",
0,
{ B_REF_TYPE },
{},
{}
},
{ kPropertyEntry,
{ B_EXECUTE_PROPERTY },
{ B_ENTRY_SPECIFIER, B_INDEX_SPECIFIER },
"execute Entry {path|index} # opens specified entries",
0,
{ B_REF_TYPE },
{},
{}
},
{ kPropertySelection,
{ B_GET_PROPERTY },
{ B_DIRECT_SPECIFIER, kPreviousSpecifier, kNextSpecifier },
"get Selection [next|previous] # returns the selected entries",
0,
{ B_REF_TYPE },
{},
{}
},
{ kPropertySelection,
{ B_SET_PROPERTY },
{ B_DIRECT_SPECIFIER, kPreviousSpecifier, kNextSpecifier },
"set Selection of ... to {next|previous|entry} # selects specified "
"entries",
0,
{},
{},
{}
},
{ kPropertySelection,
{ B_COUNT_PROPERTIES },
{ B_DIRECT_SPECIFIER },
"count Selection of ... # counts selected items",
0,
{ B_INT32_TYPE },
{},
{}
},
{ kPropertySelection,
{ B_CREATE_PROPERTY },
{ B_DIRECT_SPECIFIER },
"create selection of ... to {entry|index} "
"# adds specified items to a selection in a PoseView",
0,
{},
{},
{}
},
{ kPropertySelection,
{ B_DELETE_PROPERTY },
{ B_ENTRY_SPECIFIER, B_INDEX_SPECIFIER },
"delete selection {path|index} of ... "
"# removes specified items from a selection in a PoseView",
0,
{},
{},
{}
},
{ 0 }
};
status_t
BPoseView::GetSupportedSuites(BMessage* data)
{
data->AddString("suites", kPosesSuites);
BPropertyInfo propertyInfo(
const_cast<property_info*>(kPosesPropertyList));
data->AddFlat("messages", &propertyInfo);
return _inherited::GetSupportedSuites(data);
}
bool
BPoseView::HandleScriptingMessage(BMessage* message)
{
if (message->what != B_GET_PROPERTY
&& message->what != B_SET_PROPERTY
&& message->what != B_CREATE_PROPERTY
&& message->what != B_COUNT_PROPERTIES
&& message->what != B_DELETE_PROPERTY
&& message->what != B_EXECUTE_PROPERTY) {
return false;
}
BMessage reply(B_REPLY);
const char* property = 0;
bool handled = false;
int32 index = 0;
int32 form = 0;
BMessage specifier;
status_t result = message->GetCurrentSpecifier(&index, &specifier,
&form, &property);
if (result != B_OK || index == -1)
return false;
ASSERT(property != NULL);
switch (message->what) {
case B_CREATE_PROPERTY:
handled = CreateProperty(message, &specifier, form, property,
&reply);
break;
case B_GET_PROPERTY:
handled = GetProperty(&specifier, form, property, &reply);
break;
case B_SET_PROPERTY:
handled = SetProperty(message, &specifier, form, property,
&reply);
break;
case B_COUNT_PROPERTIES:
handled = CountProperty(&specifier, form, property, &reply);
break;
case B_DELETE_PROPERTY:
handled = DeleteProperty(&specifier, form, property, &reply);
break;
case B_EXECUTE_PROPERTY:
handled = ExecuteProperty(&specifier, form, property, &reply);
break;
}
if (handled) {
message->SendReply(&reply);
}
return handled;
}
bool
BPoseView::ExecuteProperty(BMessage* specifier, int32 form,
const char* property, BMessage* reply)
{
status_t result = B_OK;
bool handled = false;
if (strcmp(property, kPropertyEntry) == 0) {
BMessage launchMessage(B_REFS_RECEIVED);
if (form == (int32)B_ENTRY_SPECIFIER) {
entry_ref ref;
for (int32 index = 0; specifier->FindRef("refs", index, &ref)
== B_OK; index++)
launchMessage.AddRef("refs", &ref);
} else if (form == (int32)B_INDEX_SPECIFIER) {
int32 specifyingIndex;
for (int32 index = 0; specifier->FindInt32("index", index,
&specifyingIndex) == B_OK; index++) {
BPose* pose = PoseAtIndex(specifyingIndex);
if (pose == NULL) {
result = B_ENTRY_NOT_FOUND;
break;
}
launchMessage.AddRef("refs", pose->TargetModel()->EntryRef());
}
} else
return false;
if (result == B_OK) {
launchMessage.AddMessenger("TrackerViewToken",
BMessenger(this, 0, 0));
if (fSelectionHandler)
fSelectionHandler->PostMessage(&launchMessage);
}
handled = true;
}
if (result != B_OK)
reply->AddInt32("error", result);
return handled;
}
bool
BPoseView::CreateProperty(BMessage* specifier, BMessage*, int32 form,
const char* property, BMessage* reply)
{
status_t result = B_OK;
bool handled = false;
if (strcmp(property, kPropertySelection) == 0) {
if (form != B_DIRECT_SPECIFIER)
return false;
if (specifier->HasRef("data")) {
entry_ref ref;
for (int32 index = 0; specifier->FindRef("data", index, &ref)
== B_OK; index++) {
int32 poseIndex;
BPose* pose = FindPose(&ref, form, &poseIndex);
if (pose == NULL) {
result = B_ENTRY_NOT_FOUND;
handled = true;
break;
}
AddPoseToSelection(pose, poseIndex);
}
handled = true;
} else {
int32 specifyingIndex;
for (int32 index = 0; specifier->FindInt32("data", index,
&specifyingIndex) == B_OK; index++) {
BPose* pose = PoseAtIndex(specifyingIndex);
if (pose == NULL) {
result = B_BAD_INDEX;
handled = true;
break;
}
AddPoseToSelection(pose, specifyingIndex);
}
handled = true;
}
}
if (result != B_OK)
reply->AddInt32("error", result);
return handled;
}
bool
BPoseView::DeleteProperty(BMessage* specifier, int32 form,
const char* property, BMessage* reply)
{
status_t result = B_OK;
bool handled = false;
if (strcmp(property, kPropertySelection) == 0) {
if (form == (int32)B_ENTRY_SPECIFIER) {
entry_ref ref;
for (int32 index = 0; specifier->FindRef("refs", index, &ref)
== B_OK; index++) {
int32 poseIndex;
BPose* pose = FindPose(&ref, form, &poseIndex);
if (pose == NULL) {
result = B_ENTRY_NOT_FOUND;
break;
}
RemovePoseFromSelection(pose);
}
handled = true;
} else if (form == B_INDEX_SPECIFIER) {
int32 specifyingIndex;
for (int32 index = 0; specifier->FindInt32("index", index,
&specifyingIndex) == B_OK; index++) {
BPose* pose = PoseAtIndex(specifyingIndex);
if (pose == NULL) {
result = B_BAD_INDEX;
break;
}
RemovePoseFromSelection(pose);
}
handled = true;
} else
return false;
} else if (strcmp(property, kPropertyEntry) == 0) {
BObjectList<entry_ref, true>* entryList = new BObjectList<entry_ref, true>();
if (form == (int32)B_ENTRY_SPECIFIER) {
entry_ref ref;
for (int32 index = 0; specifier->FindRef("refs", index, &ref)
== B_OK; index++) {
entryList->AddItem(new entry_ref(ref));
}
} else if (form == (int32)B_INDEX_SPECIFIER) {
int32 specifyingIndex;
for (int32 index = 0; specifier->FindInt32("index", index,
&specifyingIndex) == B_OK; index++) {
BPose* pose = PoseAtIndex(specifyingIndex);
if (pose == NULL) {
result = B_BAD_INDEX;
break;
}
entryList->AddItem(
new entry_ref(*pose->TargetModel()->EntryRef()));
}
} else {
delete entryList;
return false;
}
if (result == B_OK)
MoveListToTrash(entryList, false, false);
else {
for (int i = entryList->CountItems() - 1; i >= 0; i--)
delete entryList->ItemAt(i);
delete entryList;
}
handled = true;
}
if (result != B_OK)
reply->AddInt32("error", result);
return handled;
}
bool
BPoseView::CountProperty(BMessage*, int32, const char* property,
BMessage* reply)
{
bool handled = false;
if (strcmp(property, kPropertySelection) == 0) {
reply->AddInt32("result", fSelectionList->CountItems());
handled = true;
} else if (strcmp(property, kPropertyEntry) == 0) {
reply->AddInt32("result", CurrentPoseList()->CountItems());
handled = true;
}
return handled;
}
bool
BPoseView::GetProperty(BMessage* specifier, int32 form,
const char* property, BMessage* reply)
{
bool handled = false;
status_t result = B_OK;
if (strcmp(property, kPropertyPath) == 0) {
if (form == B_DIRECT_SPECIFIER) {
handled = true;
if (TargetModel() == NULL)
result = B_NOT_A_DIRECTORY;
else
reply->AddRef("result", TargetModel()->EntryRef());
}
} else if (strcmp(property, kPropertySelection) == 0) {
int32 count = fSelectionList->CountItems();
switch (form) {
case B_DIRECT_SPECIFIER:
for (int32 index = 0; index < count; index++) {
reply->AddRef("result", fSelectionList->ItemAt(index)->
TargetModel()->EntryRef());
}
handled = true;
break;
case kPreviousSpecifier:
case kNextSpecifier:
{
entry_ref ref;
if (specifier->FindRef("data", &ref) != B_OK)
break;
int32 poseIndex;
BPose* pose = FindPose(&ref, &poseIndex);
for (;;) {
if (form == (int32)kPreviousSpecifier)
pose = PoseAtIndex(--poseIndex);
else if (form == (int32)kNextSpecifier)
pose = PoseAtIndex(++poseIndex);
if (pose == NULL) {
result = B_ENTRY_NOT_FOUND;
break;
}
if (pose->IsSelected()) {
reply->AddRef("result",
pose->TargetModel()->EntryRef());
reply->AddInt32("index", IndexOfPose(pose));
break;
}
}
handled = true;
break;
}
}
} else if (strcmp(property, kPropertyEntry) == 0) {
int32 count = CurrentPoseList()->CountItems();
switch (form) {
case B_DIRECT_SPECIFIER:
{
for (int32 index = 0; index < count; index++) {
reply->AddRef("result",
PoseAtIndex(index)->TargetModel()->EntryRef());
}
handled = true;
break;
}
case B_INDEX_SPECIFIER:
{
int32 index;
if (specifier->FindInt32("index", &index) != B_OK)
break;
if (!PoseAtIndex(index)) {
result = B_BAD_INDEX;
handled = true;
break;
}
reply->AddRef("result",
PoseAtIndex(index)->TargetModel()->EntryRef());
handled = true;
break;
}
case kPreviousSpecifier:
case kNextSpecifier:
{
entry_ref ref;
if (specifier->FindRef("data", &ref) != B_OK)
break;
int32 tmp;
BPose* pose = FindPose(&ref, form, &tmp);
if (pose == NULL) {
result = B_ENTRY_NOT_FOUND;
handled = true;
break;
}
reply->AddRef("result", pose->TargetModel()->EntryRef());
reply->AddInt32("index", IndexOfPose(pose));
handled = true;
break;
}
}
}
if (result != B_OK)
reply->AddInt32("error", result);
return handled;
}
bool
BPoseView::SetProperty(BMessage* message, BMessage*, int32 form,
const char* property, BMessage* reply)
{
status_t result = B_OK;
bool handled = false;
if (strcmp(property, kPropertySelection) == 0) {
entry_ref ref;
switch (form) {
case B_DIRECT_SPECIFIER:
{
int32 selStart;
int32 selEnd;
if (message->FindInt32("data", 0, &selStart) == B_OK
&& message->FindInt32("data", 1, &selEnd) == B_OK) {
if (selStart < 0 || selStart >= CurrentPoseList()->CountItems()
|| selEnd < 0 || selEnd >= CurrentPoseList()->CountItems()) {
result = B_BAD_INDEX;
handled = true;
break;
}
SelectPoses(selStart, selEnd);
handled = true;
break;
}
}
case kPreviousSpecifier:
case kNextSpecifier:
{
bool clearSelection = true;
for (int32 index = 0; message->FindRef("data", index, &ref)
== B_OK; index++) {
int32 poseIndex;
BPose* pose = FindPose(&ref, form, &poseIndex);
if (pose == NULL) {
result = B_ENTRY_NOT_FOUND;
handled = true;
break;
}
if (clearSelection) {
SelectPose(pose, poseIndex);
clearSelection = false;
} else
AddPoseToSelection(pose, poseIndex);
handled = true;
}
break;
}
}
}
if (result != B_OK)
reply->AddInt32("error", result);
return handled;
}
BHandler*
BPoseView::ResolveSpecifier(BMessage* message, int32 index,
BMessage* specifier, int32 form, const char* property)
{
BPropertyInfo propertyInfo(
const_cast<property_info*>(kPosesPropertyList));
int32 result = propertyInfo.FindMatch(message, index, specifier, form,
property);
if (result < 0) {
return _inherited::ResolveSpecifier(message, index, specifier,
form, property);
}
return this;
}
BPose*
BPoseView::FindPose(const entry_ref* ref, int32 specifierForm,
int32* index) const
{
BPose* pose = FindPose(ref, index);
if (specifierForm == (int32)kPreviousSpecifier)
return PoseAtIndex(--*index);
else if (specifierForm == (int32)kNextSpecifier)
return PoseAtIndex(++*index);
else
return pose;
}