xref: /haiku/src/servers/print/Printer.Scripting.cpp (revision 5d9e40fe9252c8f9c5e5e41594545bfa4419fcc7)
1 /*****************************************************************************/
2 // print_server Background Application.
3 //
4 // Version: 1.0.0d1
5 //
6 // The print_server manages the communication between applications and the
7 // printer and transport drivers.
8 //
9 // Author
10 //   Ithamar R. Adema
11 //
12 // This application and all source files used in its construction, except
13 // where noted, are licensed under the MIT License, and have been written
14 // and are:
15 //
16 // Copyright (c) 2001, 2002 OpenBeOS Project
17 //
18 // Permission is hereby granted, free of charge, to any person obtaining a
19 // copy of this software and associated documentation files (the "Software"),
20 // to deal in the Software without restriction, including without limitation
21 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
22 // and/or sell copies of the Software, and to permit persons to whom the
23 // Software is furnished to do so, subject to the following conditions:
24 //
25 // The above copyright notice and this permission notice shall be included
26 // in all copies or substantial portions of the Software.
27 //
28 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
29 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
31 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
33 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
34 // DEALINGS IN THE SOFTWARE.
35 /*****************************************************************************/
36 
37 #include "Printer.h"
38 
39 
40 #include "pr_server.h"
41 
42 	// BeOS API
43 #include <PropertyInfo.h>
44 #include <Messenger.h>
45 #include <Message.h>
46 #include <AppDefs.h>
47 
48 static property_info prop_list[] = {
49 	{ "Name", { B_GET_PROPERTY }, { B_DIRECT_SPECIFIER },
50 		"Get name of printer" },
51 	{ "TransportAddon", { B_GET_PROPERTY }, { B_DIRECT_SPECIFIER },
52 		"Get name of the transport add-on used for this printer" },
53 	{ "TransportConfig", { B_GET_PROPERTY }, { B_DIRECT_SPECIFIER },
54 		"Get the transport configuration for this printer" },
55 	{ "PrinterAddon", { B_GET_PROPERTY }, { B_DIRECT_SPECIFIER },
56 		"Get name of the printer add-on used for this printer" },
57 	{ "Comments", { B_GET_PROPERTY }, { B_DIRECT_SPECIFIER },
58 		"Get comments about this printer" },
59 	{ 0 } // terminate list
60 };
61 
62 void Printer::HandleScriptingCommand(BMessage* msg)
63 {
64 	status_t rc = B_ERROR;
65 	BString propName;
66 	BString result;
67 	BMessage spec;
68 	int32 idx;
69 
70 	if ((rc=msg->GetCurrentSpecifier(&idx,&spec)) == B_OK &&
71 		(rc=spec.FindString("property",&propName)) == B_OK) {
72 		switch(msg->what) {
73 			case B_GET_PROPERTY:
74 				if (propName == "Name")
75 					rc = SpoolDir()->ReadAttrString(PSRV_PRINTER_ATTR_PRT_NAME, &result);
76 				else if (propName == "TransportAddon")
77 					rc = SpoolDir()->ReadAttrString(PSRV_PRINTER_ATTR_TRANSPORT, &result);
78 				else if (propName == "TransportConfig")
79 					rc = SpoolDir()->ReadAttrString(PSRV_PRINTER_ATTR_TRANSPORT_ADDR, &result);
80 				else if (propName == "PrinterAddon")
81 					rc = SpoolDir()->ReadAttrString(PSRV_PRINTER_ATTR_DRV_NAME, &result);
82 				else if (propName == "Comments")
83 					rc = SpoolDir()->ReadAttrString(PSRV_PRINTER_ATTR_COMMENTS, &result);
84 				else { // If unknown scripting request, let superclas handle it
85 					Inherited::MessageReceived(msg);
86 					break;
87 				}
88 
89 				BMessage reply(B_REPLY);
90 				reply.AddString("result", result);
91 				reply.AddInt32("error", rc);
92 				msg->SendReply(&reply);
93 				break;
94 		}
95 	}
96 	else {
97 			// If GetSpecifier failed
98 		if (idx == -1) {
99 			BMessage reply(B_REPLY);
100 			reply.AddMessenger("result", BMessenger(this));
101 			reply.AddInt32("error", B_OK);
102 			msg->SendReply(&reply);
103 		}
104 	}
105 }
106 
107 BHandler* Printer::ResolveSpecifier(BMessage* msg, int32 index, BMessage* spec,
108 	int32 form, const char* prop)
109 {
110 	BPropertyInfo prop_info(prop_list);
111 	BHandler* rc = this;
112 
113 	int32 idx;
114 	switch( idx=prop_info.FindMatch(msg,0,spec,form,prop) ) {
115 		case B_ERROR:
116 			rc = Inherited::ResolveSpecifier(msg,index,spec,form,prop);
117 			break;
118 	}
119 
120 	return rc;
121 }
122 
123 status_t Printer::GetSupportedSuites(BMessage* msg)
124 {
125 	msg->AddString("suites", "application/x-vnd.OpenBeOS-printer");
126 
127 	BPropertyInfo prop_info(prop_list);
128 	msg->AddFlat("messages", &prop_info);
129 
130 	return Inherited::GetSupportedSuites(msg);
131 }
132