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	{ 0 } // terminate list
30};
31
32
33void
34Transport::HandleScriptingCommand(BMessage* msg)
35{
36	status_t rc = B_ERROR;
37	BString propName;
38	BString result;
39	BMessage spec;
40	int32 idx;
41
42	if ((rc=msg->GetCurrentSpecifier(&idx,&spec)) == B_OK
43		&& (rc=spec.FindString("property",&propName)) == B_OK) {
44		switch(msg->what) {
45			case B_GET_PROPERTY:
46				if (propName == "Name")
47					result = Name();
48				else if (propName == "Ports") {
49					// Need to duplicate messaging code, as our result is a
50					// complex bmessage, not a string :(
51					BMessage reply(B_REPLY);
52					rc = ListAvailablePorts(&reply);
53					reply.AddInt32("error", rc);
54					msg->SendReply(&reply);
55					break;
56				} else {
57					// If unknown scripting request, let superclas handle it
58					Inherited::MessageReceived(msg);
59					break;
60				}
61
62				BMessage reply(B_REPLY);
63				reply.AddString("result", result);
64				reply.AddInt32("error", rc);
65				msg->SendReply(&reply);
66				break;
67		}
68	} else {
69		// If GetSpecifier failed
70		if (idx == -1) {
71			BMessage reply(B_REPLY);
72			reply.AddMessenger("result", BMessenger(this));
73			reply.AddInt32("error", B_OK);
74			msg->SendReply(&reply);
75		}
76	}
77}
78
79
80BHandler*
81Transport::ResolveSpecifier(BMessage* msg, int32 index, BMessage* spec,
82	int32 form, const char* prop)
83{
84	BPropertyInfo prop_info(prop_list);
85	BHandler* rc = this;
86
87	int32 idx;
88	switch (idx=prop_info.FindMatch(msg,0,spec,form,prop)) {
89		case B_ERROR:
90			rc = Inherited::ResolveSpecifier(msg,index,spec,form,prop);
91			break;
92	}
93
94	return rc;
95}
96
97
98status_t
99Transport::GetSupportedSuites(BMessage* msg)
100{
101	msg->AddString("suites", "application/x-vnd.OpenBeOS-transport");
102
103	static bool localized = false;
104	if (!localized) {
105		localized = true;
106		for (int i = 0; prop_list[i].name != NULL; i ++)
107			prop_list[i].usage = B_TRANSLATE_NOCOLLECT(prop_list[i].usage);
108	}
109
110	BPropertyInfo prop_info(prop_list);
111	msg->AddFlat("messages", &prop_info);
112
113	return Inherited::GetSupportedSuites(msg);
114}
115