xref: /haiku/src/servers/print/PrintServerApp.Scripting.cpp (revision 7120e97489acbf17d86d3f33e3b2e68974fd4b23)
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 "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 	{ "UseConfigWindow", { B_GET_PROPERTY, B_SET_PROPERTY }, { B_DIRECT_SPECIFIER },
59 		"Show configuration window" },
60 	{ 0 } // terminate list
61 };
62 
63 void
64 PrintServerApp::HandleScriptingCommand(BMessage* msg)
65 {
66 	BString propName;
67 	BMessage spec;
68 	int32 idx;
69 
70 	if (msg->GetCurrentSpecifier(&idx,&spec) == B_OK &&
71 		spec.FindString("property",&propName) == B_OK) {
72 		switch(msg->what) {
73 			case B_GET_PROPERTY:
74 				if (propName == "ActivePrinter") {
75 					BMessage reply(B_REPLY);
76 					reply.AddString("result", fDefaultPrinter ? fDefaultPrinter->Name() : "");
77 					reply.AddInt32("error", B_OK);
78 					msg->SendReply(&reply);
79 				} else if (propName == "UseConfigWindow") {
80 					BMessage reply(B_REPLY);
81 					reply.AddString("result", fUseConfigWindow ? "true" : "false");
82 					reply.AddInt32("error", B_OK);
83 					msg->SendReply(&reply);
84 				}
85 				break;
86 
87 			case B_SET_PROPERTY:
88 				if (propName == "ActivePrinter") {
89 					BString newActivePrinter;
90 					if (msg->FindString("data", &newActivePrinter) == B_OK) {
91 						BMessage reply(B_REPLY);
92 						reply.AddInt32("error", SelectPrinter(newActivePrinter.String()));
93 						msg->SendReply(&reply);
94 					}
95 				} else if (propName == "UseConfigWindow") {
96 					bool useConfigWindow;
97 					if (msg->FindBool("data", &useConfigWindow) == B_OK) {
98 						fUseConfigWindow = useConfigWindow;
99 						BMessage reply(B_REPLY);
100 						reply.AddInt32("error", fUseConfigWindow);
101 						msg->SendReply(&reply);
102 					}
103 				}
104 				break;
105 
106 			case B_CREATE_PROPERTY:
107 				if (propName == "Printer") {
108 					BString name, driver, transport, config;
109 
110 					if (msg->FindString("name", &name) == B_OK &&
111 						msg->FindString("driver", &driver) == B_OK &&
112 						msg->FindString("transport", &transport) == B_OK &&
113 						msg->FindString("config", &config) == B_OK) {
114 						BMessage reply(B_REPLY);
115 						reply.AddInt32("error", CreatePrinter(name.String(), driver.String(),
116 													"Local", transport.String(), config.String()));
117 						msg->SendReply(&reply);
118 					}
119 				}
120 				break;
121 
122 			case B_DELETE_PROPERTY: {
123 					Printer* printer = GetPrinterFromSpecifier(&spec);
124 					status_t rc = B_BAD_VALUE;
125 
126 					if (printer != NULL) {
127 						rc=printer->Remove();
128 					}
129 
130 					BMessage reply(B_REPLY);
131 					reply.AddInt32("error", rc);
132 					msg->SendReply(&reply);
133 				}
134 				break;
135 
136 			case B_COUNT_PROPERTIES:
137 				if (propName == "Printers") {
138 					BMessage reply(B_REPLY);
139 					reply.AddInt32("result", Printer::CountPrinters());
140 					reply.AddInt32("error", B_OK);
141 					msg->SendReply(&reply);
142 				}
143 				break;
144 		}
145 	}
146 }
147 
148 Printer* PrintServerApp::GetPrinterFromSpecifier(BMessage* msg)
149 {
150 	switch(msg->what) {
151 		case B_NAME_SPECIFIER: {
152 			BString name;
153 			if (msg->FindString("name", &name) == B_OK) {
154 				return Printer::Find(name.String());
155 			}
156 			break;
157 		}
158 
159 		case B_INDEX_SPECIFIER: {
160 			int32 idx;
161 			if (msg->FindInt32("index", &idx) == B_OK) {
162 				return Printer::At(idx);
163 			}
164 			break;
165 		}
166 
167 		case B_REVERSE_INDEX_SPECIFIER: {
168 			int32 idx;
169 			if (msg->FindInt32("index", &idx) == B_OK) {
170 				return Printer::At(Printer::CountPrinters() - idx);
171 			}
172 			break;
173 		}
174 	}
175 
176 	return NULL;
177 }
178 
179 BHandler*
180 PrintServerApp::ResolveSpecifier(BMessage* msg, int32 index, BMessage* spec,
181 								int32 form, const char* prop)
182 {
183 	BPropertyInfo prop_info(prop_list);
184 	BHandler* rc = NULL;
185 
186 	int32 idx;
187 	switch( idx=prop_info.FindMatch(msg,0,spec,form,prop) ) {
188 		case B_ERROR:
189 			rc = Inherited::ResolveSpecifier(msg,index,spec,form,prop);
190 
191 			// GET Printer [arg]
192 		case 1:
193 			if ((rc=GetPrinterFromSpecifier(spec)) == NULL) {
194 				BMessage reply(B_REPLY);
195 				reply.AddInt32("error", B_BAD_INDEX);
196 				msg->SendReply(&reply);
197 			}
198 			else
199 				msg->PopSpecifier();
200 			break;
201 
202 		default:
203 			rc = this;
204 	}
205 
206 	return rc;
207 }
208 
209 status_t
210 PrintServerApp::GetSupportedSuites(BMessage* msg)
211 {
212 	msg->AddString("suites", "suite/vnd.OpenBeOS-printserver");
213 
214 	BPropertyInfo prop_info(prop_list);
215 	msg->AddFlat("messages", &prop_info);
216 
217 	return Inherited::GetSupportedSuites(msg);
218 }
219