162cb58a8SIthamar R. Adema /*
262cb58a8SIthamar R. Adema * Copyright 2008, Haiku. All rights reserved.
362cb58a8SIthamar R. Adema * Distributed under the terms of the MIT License.
462cb58a8SIthamar R. Adema *
562cb58a8SIthamar R. Adema * Authors:
662cb58a8SIthamar R. Adema * Ithamar R. Adema
762cb58a8SIthamar R. Adema */
862cb58a8SIthamar R. Adema
9cc507fa3SWim van der Meer
1062cb58a8SIthamar R. Adema #include "Transport.h"
1162cb58a8SIthamar R. Adema
1262cb58a8SIthamar R. Adema #include <AppDefs.h>
138b0a06adSMichael Pfeiffer #include <Catalog.h>
148b0a06adSMichael Pfeiffer #include <Locale.h>
158b0a06adSMichael Pfeiffer #include <Message.h>
168b0a06adSMichael Pfeiffer #include <Messenger.h>
178b0a06adSMichael Pfeiffer #include <PropertyInfo.h>
188b0a06adSMichael Pfeiffer
198b0a06adSMichael Pfeiffer
20546208a5SOliver Tappe #undef B_TRANSLATION_CONTEXT
21546208a5SOliver Tappe #define B_TRANSLATION_CONTEXT "Transport Scripting"
228b0a06adSMichael Pfeiffer
2362cb58a8SIthamar R. Adema
2462cb58a8SIthamar R. Adema static property_info prop_list[] = {
2562cb58a8SIthamar R. Adema { "Name", { B_GET_PROPERTY }, { B_DIRECT_SPECIFIER },
268b0a06adSMichael Pfeiffer B_TRANSLATE_MARK("Get name of transport") },
27eb5a25caSIthamar R. Adema { "Ports", { B_GET_PROPERTY }, { B_DIRECT_SPECIFIER },
288b0a06adSMichael Pfeiffer B_TRANSLATE_MARK("Get currently available ports/devices") },
29*16af9b4cSHumdinger
3062cb58a8SIthamar R. Adema { 0 } // terminate list
3162cb58a8SIthamar R. Adema };
3262cb58a8SIthamar R. Adema
339abbe87fSMichael Pfeiffer
349abbe87fSMichael Pfeiffer void
HandleScriptingCommand(BMessage * msg)359abbe87fSMichael Pfeiffer Transport::HandleScriptingCommand(BMessage* msg)
3662cb58a8SIthamar R. Adema {
3762cb58a8SIthamar R. Adema status_t rc = B_ERROR;
3862cb58a8SIthamar R. Adema BString propName;
3962cb58a8SIthamar R. Adema BString result;
4062cb58a8SIthamar R. Adema BMessage spec;
4162cb58a8SIthamar R. Adema int32 idx;
4262cb58a8SIthamar R. Adema
43cc507fa3SWim van der Meer if ((rc=msg->GetCurrentSpecifier(&idx,&spec)) == B_OK
44cc507fa3SWim van der Meer && (rc=spec.FindString("property",&propName)) == B_OK) {
4562cb58a8SIthamar R. Adema switch(msg->what) {
4662cb58a8SIthamar R. Adema case B_GET_PROPERTY:
4762cb58a8SIthamar R. Adema if (propName == "Name")
48eb5a25caSIthamar R. Adema result = Name();
49eb5a25caSIthamar R. Adema else if (propName == "Ports") {
50cc507fa3SWim van der Meer // Need to duplicate messaging code, as our result is a
51cc507fa3SWim van der Meer // complex bmessage, not a string :(
52eb5a25caSIthamar R. Adema BMessage reply(B_REPLY);
53eb5a25caSIthamar R. Adema rc = ListAvailablePorts(&reply);
54eb5a25caSIthamar R. Adema reply.AddInt32("error", rc);
55eb5a25caSIthamar R. Adema msg->SendReply(&reply);
56eb5a25caSIthamar R. Adema break;
57cc507fa3SWim van der Meer } else {
58cc507fa3SWim van der Meer // If unknown scripting request, let superclas handle it
5962cb58a8SIthamar R. Adema Inherited::MessageReceived(msg);
6062cb58a8SIthamar R. Adema break;
6162cb58a8SIthamar R. Adema }
6262cb58a8SIthamar R. Adema
6362cb58a8SIthamar R. Adema BMessage reply(B_REPLY);
6462cb58a8SIthamar R. Adema reply.AddString("result", result);
6562cb58a8SIthamar R. Adema reply.AddInt32("error", rc);
6662cb58a8SIthamar R. Adema msg->SendReply(&reply);
6762cb58a8SIthamar R. Adema break;
6862cb58a8SIthamar R. Adema }
69cc507fa3SWim van der Meer } else {
7062cb58a8SIthamar R. Adema // If GetSpecifier failed
7162cb58a8SIthamar R. Adema if (idx == -1) {
7262cb58a8SIthamar R. Adema BMessage reply(B_REPLY);
7362cb58a8SIthamar R. Adema reply.AddMessenger("result", BMessenger(this));
7462cb58a8SIthamar R. Adema reply.AddInt32("error", B_OK);
7562cb58a8SIthamar R. Adema msg->SendReply(&reply);
7662cb58a8SIthamar R. Adema }
7762cb58a8SIthamar R. Adema }
7862cb58a8SIthamar R. Adema }
7962cb58a8SIthamar R. Adema
809abbe87fSMichael Pfeiffer
819abbe87fSMichael Pfeiffer BHandler*
ResolveSpecifier(BMessage * msg,int32 index,BMessage * spec,int32 form,const char * prop)829abbe87fSMichael Pfeiffer Transport::ResolveSpecifier(BMessage* msg, int32 index, BMessage* spec,
8362cb58a8SIthamar R. Adema int32 form, const char* prop)
8462cb58a8SIthamar R. Adema {
8562cb58a8SIthamar R. Adema BPropertyInfo prop_info(prop_list);
8662cb58a8SIthamar R. Adema BHandler* rc = this;
8762cb58a8SIthamar R. Adema
8862cb58a8SIthamar R. Adema int32 idx;
8962cb58a8SIthamar R. Adema switch (idx=prop_info.FindMatch(msg,0,spec,form,prop)) {
9062cb58a8SIthamar R. Adema case B_ERROR:
9162cb58a8SIthamar R. Adema rc = Inherited::ResolveSpecifier(msg,index,spec,form,prop);
9262cb58a8SIthamar R. Adema break;
9362cb58a8SIthamar R. Adema }
9462cb58a8SIthamar R. Adema
9562cb58a8SIthamar R. Adema return rc;
9662cb58a8SIthamar R. Adema }
9762cb58a8SIthamar R. Adema
989abbe87fSMichael Pfeiffer
999abbe87fSMichael Pfeiffer status_t
GetSupportedSuites(BMessage * msg)1009abbe87fSMichael Pfeiffer Transport::GetSupportedSuites(BMessage* msg)
10162cb58a8SIthamar R. Adema {
10262cb58a8SIthamar R. Adema msg->AddString("suites", "application/x-vnd.OpenBeOS-transport");
10362cb58a8SIthamar R. Adema
1048b0a06adSMichael Pfeiffer static bool localized = false;
1058b0a06adSMichael Pfeiffer if (!localized) {
1068b0a06adSMichael Pfeiffer localized = true;
1078b0a06adSMichael Pfeiffer for (int i = 0; prop_list[i].name != NULL; i ++)
1088eff03f5SOliver Tappe prop_list[i].usage = B_TRANSLATE_NOCOLLECT(prop_list[i].usage);
1098b0a06adSMichael Pfeiffer }
1108b0a06adSMichael Pfeiffer
11162cb58a8SIthamar R. Adema BPropertyInfo prop_info(prop_list);
11262cb58a8SIthamar R. Adema msg->AddFlat("messages", &prop_info);
11362cb58a8SIthamar R. Adema
11462cb58a8SIthamar R. Adema return Inherited::GetSupportedSuites(msg);
11562cb58a8SIthamar R. Adema }
116