1/*
2 * Copyright 2008, Haiku. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Ithamar R. Adema
7 */
8
9
10#include "Transport.h"
11
12#include <AppDefs.h>
13#include <Catalog.h>
14#include <Locale.h>
15#include <Message.h>
16#include <Messenger.h>
17#include <PropertyInfo.h>
18
19
20#undef B_TRANSLATION_CONTEXT
21#define B_TRANSLATION_CONTEXT "Transport Scripting"
22
23
24static property_info prop_list[] = {
25	{ "Name", { B_GET_PROPERTY }, { B_DIRECT_SPECIFIER },
26		B_TRANSLATE_MARK("Get name of transport") },
27	{ "Ports", { B_GET_PROPERTY }, { B_DIRECT_SPECIFIER },
28		B_TRANSLATE_MARK("Get currently available ports/devices") },
29
30	{ 0 } // terminate list
31};
32
33
34void
35Transport::HandleScriptingCommand(BMessage* msg)
36{
37	status_t rc = B_ERROR;
38	BString propName;
39	BString result;
40	BMessage spec;
41	int32 idx;
42
43	if ((rc=msg->GetCurrentSpecifier(&idx,&spec)) == B_OK
44		&& (rc=spec.FindString("property",&propName)) == B_OK) {
45		switch(msg->what) {
46			case B_GET_PROPERTY:
47				if (propName == "Name")
48					result = Name();
49				else if (propName == "Ports") {
50					// Need to duplicate messaging code, as our result is a
51					// complex bmessage, not a string :(
52					BMessage reply(B_REPLY);
53					rc = ListAvailablePorts(&reply);
54					reply.AddInt32("error", rc);
55					msg->SendReply(&reply);
56					break;
57				} else {
58					// If unknown scripting request, let superclas handle it
59					Inherited::MessageReceived(msg);
60					break;
61				}
62
63				BMessage reply(B_REPLY);
64				reply.AddString("result", result);
65				reply.AddInt32("error", rc);
66				msg->SendReply(&reply);
67				break;
68		}
69	} else {
70		// If GetSpecifier failed
71		if (idx == -1) {
72			BMessage reply(B_REPLY);
73			reply.AddMessenger("result", BMessenger(this));
74			reply.AddInt32("error", B_OK);
75			msg->SendReply(&reply);
76		}
77	}
78}
79
80
81BHandler*
82Transport::ResolveSpecifier(BMessage* msg, int32 index, BMessage* spec,
83	int32 form, const char* prop)
84{
85	BPropertyInfo prop_info(prop_list);
86	BHandler* rc = this;
87
88	int32 idx;
89	switch (idx=prop_info.FindMatch(msg,0,spec,form,prop)) {
90		case B_ERROR:
91			rc = Inherited::ResolveSpecifier(msg,index,spec,form,prop);
92			break;
93	}
94
95	return rc;
96}
97
98
99status_t
100Transport::GetSupportedSuites(BMessage* msg)
101{
102	msg->AddString("suites", "application/x-vnd.OpenBeOS-transport");
103
104	static bool localized = false;
105	if (!localized) {
106		localized = true;
107		for (int i = 0; prop_list[i].name != NULL; i ++)
108			prop_list[i].usage = B_TRANSLATE_NOCOLLECT(prop_list[i].usage);
109	}
110
111	BPropertyInfo prop_info(prop_list);
112	msg->AddFlat("messages", &prop_info);
113
114	return Inherited::GetSupportedSuites(msg);
115}
116