#include <Message.h>
#include <PropertyInfo.h>
#include "ContainerWindow.h"
#include "FSUtils.h"
#include "Tracker.h"
#define kPropertyTrash "Trash"
#define kPropertyFolder "Folder"
#define kPropertyPreferences "Preferences"
const property_info kTrackerPropertyList[] = {
{
kPropertyTrash,
{ B_DELETE_PROPERTY },
{ B_DIRECT_SPECIFIER },
"delete Trash # Empties the Trash",
0,
{},
{},
{}
},
{
kPropertyFolder,
{ B_CREATE_PROPERTY, B_GET_PROPERTY, B_EXECUTE_PROPERTY },
{ B_DIRECT_SPECIFIER },
"create Folder to path # creates a new folder\n"
"get Folder to path # get Tracker window pointing to folder\n"
"execute Folder to path # opens Tracker window pointing to folder\n",
0,
{ B_REF_TYPE },
{},
{}
},
{
kPropertyPreferences,
{ B_EXECUTE_PROPERTY },
{ B_DIRECT_SPECIFIER },
"shows Tracker preferences",
0,
{},
{},
{}
},
{ 0 }
};
status_t
TTracker::GetSupportedSuites(BMessage* data)
{
data->AddString("suites", kTrackerSuites);
BPropertyInfo propertyInfo(const_cast<property_info*>(
kTrackerPropertyList));
data->AddFlat("messages", &propertyInfo);
return _inherited::GetSupportedSuites(data);
}
BHandler*
TTracker::ResolveSpecifier(BMessage* message, int32 index, BMessage* specifier,
int32 form, const char* property)
{
BPropertyInfo propertyInfo(const_cast<property_info*>(
kTrackerPropertyList));
int32 result = propertyInfo.FindMatch(message, index, specifier, form,
property);
if (result < 0) {
return _inherited::ResolveSpecifier(message, index, specifier,
form, property);
}
return this;
}
bool
TTracker::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 = NULL;
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(message, 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(message, form, property, &reply);
break;
}
if (handled) {
message->SendReply(&reply);
}
return handled;
}
bool
TTracker::CreateProperty(BMessage* message, BMessage*, int32 form,
const char* property, BMessage* reply)
{
bool handled = false;
status_t error = B_OK;
if (strcmp(property, kPropertyFolder) == 0) {
if (form != B_DIRECT_SPECIFIER)
return false;
entry_ref ref;
for (int32 index = 0;
message->FindRef("data", index, &ref) == B_OK; index++) {
BEntry entry(&ref);
if (!entry.Exists())
error = FSCreateNewFolder(&ref);
if (error != B_OK)
break;
}
handled = true;
}
if (error != B_OK)
reply->AddInt32("error", error);
return handled;
}
bool
TTracker::DeleteProperty(BMessage*, int32 form, const char* property, BMessage*)
{
if (strcmp(property, kPropertyTrash) == 0) {
if (form != B_DIRECT_SPECIFIER) {
return false;
}
FSEmptyTrash();
return true;
}
return false;
}
bool
TTracker::ExecuteProperty(BMessage* message, int32 form, const char* property,
BMessage* reply)
{
if (strcmp(property, kPropertyPreferences) == 0) {
if (form != B_DIRECT_SPECIFIER) {
return false;
}
ShowSettingsWindow();
return true;
}
if (strcmp(property, kPropertyFolder) == 0) {
message->PrintToStream();
if (form != B_DIRECT_SPECIFIER)
return false;
entry_ref ref;
for (int32 index = 0;
message->FindRef("data", index, &ref) == B_OK; index++) {
status_t error = OpenRef(&ref, NULL, NULL, kOpen, NULL);
if (error == B_OK) {
reply->AddMessenger("window",
BMessenger(FindContainerWindow(&ref)));
} else {
reply->AddInt32("error", error);
break;
}
}
return true;
}
return false;
}
bool
TTracker::CountProperty(BMessage*, int32, const char*, BMessage*)
{
return false;
}
bool
TTracker::GetProperty(BMessage* message, int32 form, const char* property,
BMessage* reply)
{
if (strcmp(property, kPropertyFolder) == 0) {
message->PrintToStream();
if (form != B_DIRECT_SPECIFIER)
return false;
entry_ref ref;
for (int32 index = 0;
message->FindRef("data", index, &ref) == B_OK; index++) {
BHandler* window = FindContainerWindow(&ref);
if (window != NULL)
reply->AddMessenger("window", BMessenger(window));
else {
reply->AddInt32("error", B_NAME_NOT_FOUND);
break;
}
}
return true;
}
return false;
}
bool
TTracker::SetProperty(BMessage*, BMessage*, int32, const char*, BMessage*)
{
return false;
}