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