xref: /haiku/src/servers/print/PrintServerApp.Scripting.cpp (revision da199a7e781d23415247118bb116b833b0025337)
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 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 "PrintServerApp.h"
38 
39 #include "Printer.h"
40 
41 	// BeOS API
42 #include <PropertyInfo.h>
43 
44 	// ANSI C
45 #include <stdio.h>
46 
47 static property_info prop_list[] = {
48 	{ "ActivePrinter", { B_GET_PROPERTY, B_SET_PROPERTY }, { B_DIRECT_SPECIFIER },
49 		"Retrieve or select the active printer" },
50 	{ "Printer", { B_GET_PROPERTY }, { B_INDEX_SPECIFIER, B_NAME_SPECIFIER, B_REVERSE_INDEX_SPECIFIER },
51 		"Retrieve a specific printer" },
52 	{ "Printer", { B_CREATE_PROPERTY }, { B_DIRECT_SPECIFIER },
53 		"Create a new printer" },
54 	{ "Printer", { B_DELETE_PROPERTY }, { B_INDEX_SPECIFIER, B_NAME_SPECIFIER, B_REVERSE_INDEX_SPECIFIER },
55 		"Delete a specific printer" },
56 	{ "Printers", { B_COUNT_PROPERTIES }, { B_DIRECT_SPECIFIER },
57 		"Return the number of available printers" },
58 	{ 0 } // terminate list
59 };
60 
61 void
62 PrintServerApp::HandleScriptingCommand(BMessage* msg)
63 {
64 	BString propName;
65 	BMessage spec;
66 	int32 idx;
67 
68 	if (msg->GetCurrentSpecifier(&idx,&spec) == B_OK &&
69 		spec.FindString("property",&propName) == B_OK) {
70 		switch(msg->what) {
71 			case B_GET_PROPERTY:
72 				if (propName == "ActivePrinter") {
73 					BMessage reply(B_REPLY);
74 					reply.AddString("result", fDefaultPrinter ? fDefaultPrinter->Name() : "");
75 					reply.AddInt32("error", B_OK);
76 					msg->SendReply(&reply);
77 				}
78 				break;
79 
80 			case B_SET_PROPERTY:
81 				if (propName == "ActivePrinter") {
82 					BString newActivePrinter;
83 					if (msg->FindString("data", &newActivePrinter) == B_OK) {
84 						BMessage reply(B_REPLY);
85 						reply.AddInt32("error", SelectPrinter(newActivePrinter.String()));
86 						msg->SendReply(&reply);
87 					}
88 				}
89 				break;
90 
91 			case B_CREATE_PROPERTY:
92 				if (propName == "Printer") {
93 					BString name, driver, transport, config;
94 
95 					if (msg->FindString("name", &name) == B_OK &&
96 						msg->FindString("driver", &driver) == B_OK &&
97 						msg->FindString("transport", &transport) == B_OK &&
98 						msg->FindString("config", &config) == B_OK) {
99 						BMessage reply(B_REPLY);
100 						reply.AddInt32("error", CreatePrinter(name.String(), driver.String(),
101 													"Local", transport.String(), config.String()));
102 						msg->SendReply(&reply);
103 					}
104 				}
105 				break;
106 
107 			case B_DELETE_PROPERTY: {
108 					Printer* printer = GetPrinterFromSpecifier(&spec);
109 					status_t rc = B_BAD_VALUE;
110 
111 					if (printer != NULL) {
112 						rc=printer->Remove();
113 					}
114 
115 					BMessage reply(B_REPLY);
116 					reply.AddInt32("error", rc);
117 					msg->SendReply(&reply);
118 				}
119 				break;
120 
121 			case B_COUNT_PROPERTIES:
122 				if (propName == "Printers") {
123 					BMessage reply(B_REPLY);
124 					reply.AddInt32("result", Printer::CountPrinters());
125 					reply.AddInt32("error", B_OK);
126 					msg->SendReply(&reply);
127 				}
128 				break;
129 		}
130 	}
131 }
132 
133 Printer* PrintServerApp::GetPrinterFromSpecifier(BMessage* msg)
134 {
135 	switch(msg->what) {
136 		case B_NAME_SPECIFIER: {
137 			BString name;
138 			if (msg->FindString("name", &name) == B_OK) {
139 				return Printer::Find(name.String());
140 			}
141 			break;
142 		}
143 
144 		case B_INDEX_SPECIFIER: {
145 			int32 idx;
146 			if (msg->FindInt32("index", &idx) == B_OK) {
147 				return Printer::At(idx);
148 			}
149 			break;
150 		}
151 
152 		case B_REVERSE_INDEX_SPECIFIER: {
153 			int32 idx;
154 			if (msg->FindInt32("index", &idx) == B_OK) {
155 				return Printer::At(Printer::CountPrinters() - idx);
156 			}
157 			break;
158 		}
159 	}
160 
161 	return NULL;
162 }
163 
164 BHandler*
165 PrintServerApp::ResolveSpecifier(BMessage* msg, int32 index, BMessage* spec,
166 								int32 form, const char* prop)
167 {
168 	BPropertyInfo prop_info(prop_list);
169 	BHandler* rc = NULL;
170 
171 	int32 idx;
172 	switch( idx=prop_info.FindMatch(msg,0,spec,form,prop) ) {
173 		case B_ERROR:
174 			rc = Inherited::ResolveSpecifier(msg,index,spec,form,prop);
175 
176 			// GET Printer [arg]
177 		case 1:
178 			if ((rc=GetPrinterFromSpecifier(spec)) == NULL) {
179 				BMessage reply(B_REPLY);
180 				reply.AddInt32("error", B_BAD_INDEX);
181 				msg->SendReply(&reply);
182 			}
183 			else
184 				msg->PopSpecifier();
185 			break;
186 
187 		default:
188 			rc = this;
189 	}
190 
191 	return rc;
192 }
193 
194 status_t
195 PrintServerApp::GetSupportedSuites(BMessage* msg)
196 {
197 	msg->AddString("suites", "suite/vnd.OpenBeOS-printserver");
198 
199 	BPropertyInfo prop_info(prop_list);
200 	msg->AddFlat("messages", &prop_info);
201 
202 	return Inherited::GetSupportedSuites(msg);
203 }
204