xref: /haiku/src/servers/print/Printer.Scripting.cpp (revision 0562493379cd52eb7103531f895f10bb8e77c085)
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 <PropertyInfo.h>
15 #include <Messenger.h>
16 #include <Message.h>
17 #include <AppDefs.h>
18 
19 static property_info prop_list[] = {
20 	{ "Name", { B_GET_PROPERTY }, { B_DIRECT_SPECIFIER },
21 		"Get name of printer" },
22 	{ "TransportAddon", { B_GET_PROPERTY }, { B_DIRECT_SPECIFIER },
23 		"Get name of the transport add-on used for this printer" },
24 	{ "TransportConfig", { B_GET_PROPERTY }, { B_DIRECT_SPECIFIER },
25 		"Get the transport configuration for this printer" },
26 	{ "PrinterAddon", { B_GET_PROPERTY }, { B_DIRECT_SPECIFIER },
27 		"Get name of the printer add-on used for this printer" },
28 	{ "Comments", { B_GET_PROPERTY }, { B_DIRECT_SPECIFIER },
29 		"Get comments about this printer" },
30 	{ 0 } // terminate list
31 };
32 
33 void Printer::HandleScriptingCommand(BMessage* msg)
34 {
35 	status_t rc = B_ERROR;
36 	BString propName;
37 	BString result;
38 	BMessage spec;
39 	int32 idx;
40 
41 	if ((rc=msg->GetCurrentSpecifier(&idx,&spec)) == B_OK &&
42 		(rc=spec.FindString("property",&propName)) == B_OK) {
43 		switch(msg->what) {
44 			case B_GET_PROPERTY:
45 				if (propName == "Name")
46 					rc = SpoolDir()->ReadAttrString(PSRV_PRINTER_ATTR_PRT_NAME, &result);
47 				else if (propName == "TransportAddon")
48 					rc = SpoolDir()->ReadAttrString(PSRV_PRINTER_ATTR_TRANSPORT, &result);
49 				else if (propName == "TransportConfig")
50 					rc = SpoolDir()->ReadAttrString(PSRV_PRINTER_ATTR_TRANSPORT_ADDR, &result);
51 				else if (propName == "PrinterAddon")
52 					rc = SpoolDir()->ReadAttrString(PSRV_PRINTER_ATTR_DRV_NAME, &result);
53 				else if (propName == "Comments")
54 					rc = SpoolDir()->ReadAttrString(PSRV_PRINTER_ATTR_COMMENTS, &result);
55 				else { // If unknown scripting request, let superclas handle it
56 					Inherited::MessageReceived(msg);
57 					break;
58 				}
59 
60 				BMessage reply(B_REPLY);
61 				reply.AddString("result", result);
62 				reply.AddInt32("error", rc);
63 				msg->SendReply(&reply);
64 				break;
65 		}
66 	}
67 	else {
68 			// If GetSpecifier failed
69 		if (idx == -1) {
70 			BMessage reply(B_REPLY);
71 			reply.AddMessenger("result", BMessenger(this));
72 			reply.AddInt32("error", B_OK);
73 			msg->SendReply(&reply);
74 		}
75 	}
76 }
77 
78 BHandler* Printer::ResolveSpecifier(BMessage* msg, int32 index, BMessage* spec,
79 	int32 form, const char* prop)
80 {
81 	BPropertyInfo prop_info(prop_list);
82 	BHandler* rc = this;
83 
84 	int32 idx;
85 	switch( idx=prop_info.FindMatch(msg,0,spec,form,prop) ) {
86 		case B_ERROR:
87 			rc = Inherited::ResolveSpecifier(msg,index,spec,form,prop);
88 			break;
89 	}
90 
91 	return rc;
92 }
93 
94 status_t Printer::GetSupportedSuites(BMessage* msg)
95 {
96 	msg->AddString("suites", "application/x-vnd.OpenBeOS-printer");
97 
98 	BPropertyInfo prop_info(prop_list);
99 	msg->AddFlat("messages", &prop_info);
100 
101 	return Inherited::GetSupportedSuites(msg);
102 }
103