1 /*
2 * Copyright 2010 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 * Michael Pfeiffer
7 */
8 #include "PrintAddOnServerApplication.h"
9
10 #include <PrinterDriverAddOn.h>
11
12 #include <String.h>
13
PrintAddOnServerApplication(const char * signature)14 PrintAddOnServerApplication::PrintAddOnServerApplication(const char* signature)
15 :
16 BApplication(signature)
17 {
18
19 }
20
21
22 void
MessageReceived(BMessage * message)23 PrintAddOnServerApplication::MessageReceived(BMessage* message)
24 {
25 switch (message->what) {
26 case kMessageAddPrinter:
27 AddPrinter(message);
28 break;
29
30 case kMessageConfigPage:
31 ConfigPage(message);
32 break;
33
34 case kMessageConfigJob:
35 ConfigJob(message);
36 break;
37
38 case kMessageDefaultSettings:
39 DefaultSettings(message);
40 break;
41
42 case kMessageTakeJob:
43 TakeJob(message);
44 break;
45
46 default:
47 BApplication::MessageReceived(message);
48 }
49 }
50
51
52 void
AddPrinter(BMessage * message)53 PrintAddOnServerApplication::AddPrinter(BMessage* message)
54 {
55 BString driver;
56 BString name;
57 if (message->FindString(kPrinterDriverAttribute, &driver) != B_OK
58 || message->FindString(kPrinterNameAttribute, &name) != B_OK) {
59 SendReply(message, B_BAD_VALUE);
60 return;
61 }
62
63 status_t status = AddPrinter(driver.String(), name.String());
64 SendReply(message, status);
65 }
66
67
68 status_t
AddPrinter(const char * driver,const char * spoolFolderName)69 PrintAddOnServerApplication::AddPrinter(const char* driver,
70 const char* spoolFolderName)
71 {
72 PrinterDriverAddOn addOn(driver);
73 return addOn.AddPrinter(spoolFolderName);
74 }
75
76
77 void
ConfigPage(BMessage * message)78 PrintAddOnServerApplication::ConfigPage(BMessage* message)
79 {
80 BString driver;
81 BString folder;
82 BMessage settings;
83 if (message->FindString(kPrinterDriverAttribute, &driver) != B_OK
84 || message->FindString(kPrinterFolderAttribute, &folder) != B_OK
85 || message->FindMessage(kPrintSettingsAttribute, &settings) != B_OK) {
86 SendReply(message, B_BAD_VALUE);
87 return;
88 }
89
90 BDirectory spoolFolder(folder.String());
91 status_t status = ConfigPage(driver.String(), &spoolFolder, &settings);
92
93 if (status != B_OK)
94 SendReply(message, status);
95 else {
96 BMessage reply(B_REPLY);
97 reply.AddMessage(kPrintSettingsAttribute, &settings);
98 SendReply(message, &reply);
99 }
100 }
101
102
103 status_t
ConfigPage(const char * driver,BDirectory * spoolFolder,BMessage * settings)104 PrintAddOnServerApplication::ConfigPage(const char* driver,
105 BDirectory* spoolFolder, BMessage* settings)
106 {
107 PrinterDriverAddOn addOn(driver);
108 return addOn.ConfigPage(spoolFolder, settings);
109 }
110
111
112 void
ConfigJob(BMessage * message)113 PrintAddOnServerApplication::ConfigJob(BMessage* message)
114 {
115 BString driver;
116 BString folder;
117 BMessage settings;
118 if (message->FindString(kPrinterDriverAttribute, &driver) != B_OK
119 || message->FindString(kPrinterFolderAttribute, &folder) != B_OK
120 || message->FindMessage(kPrintSettingsAttribute, &settings) != B_OK) {
121 SendReply(message, B_BAD_VALUE);
122 return;
123 }
124
125 BDirectory spoolFolder(folder.String());
126 status_t status = ConfigJob(driver.String(), &spoolFolder, &settings);
127
128 if (status != B_OK)
129 SendReply(message, status);
130 else {
131 BMessage reply(B_REPLY);
132 reply.AddMessage(kPrintSettingsAttribute, &settings);
133 SendReply(message, &reply);
134 }
135 }
136
137
138 status_t
ConfigJob(const char * driver,BDirectory * spoolFolder,BMessage * settings)139 PrintAddOnServerApplication::ConfigJob(const char* driver,
140 BDirectory* spoolFolder,
141 BMessage* settings)
142 {
143 PrinterDriverAddOn addOn(driver);
144 return addOn.ConfigJob(spoolFolder, settings);
145 }
146
147
148 void
DefaultSettings(BMessage * message)149 PrintAddOnServerApplication::DefaultSettings(BMessage* message)
150 {
151 BString driver;
152 BString folder;
153 if (message->FindString(kPrinterDriverAttribute, &driver) != B_OK
154 || message->FindString(kPrinterFolderAttribute, &folder) != B_OK) {
155 SendReply(message, B_BAD_VALUE);
156 return;
157 }
158
159 BMessage settings;
160 BDirectory spoolFolder(folder.String());
161 status_t status = DefaultSettings(driver.String(), &spoolFolder, &settings);
162
163 if (status != B_OK)
164 SendReply(message, status);
165 else {
166 BMessage reply(B_REPLY);
167 reply.AddMessage(kPrintSettingsAttribute, &settings);
168 SendReply(message, &reply);
169 }
170 }
171
172
173 status_t
DefaultSettings(const char * driver,BDirectory * spoolFolder,BMessage * settings)174 PrintAddOnServerApplication::DefaultSettings(const char* driver,
175 BDirectory* spoolFolder, BMessage* settings)
176 {
177 PrinterDriverAddOn addOn(driver);
178 return addOn.DefaultSettings(spoolFolder, settings);
179 }
180
181
182 void
TakeJob(BMessage * message)183 PrintAddOnServerApplication::TakeJob(BMessage* message)
184 {
185 BString driver;
186 BString folder;
187 BString jobFile;
188 if (message->FindString(kPrinterDriverAttribute, &driver) != B_OK
189 || message->FindString(kPrinterFolderAttribute, &folder) != B_OK
190 || message->FindString(kPrintJobFileAttribute, &jobFile) != B_OK) {
191 SendReply(message, B_BAD_VALUE);
192 return;
193 }
194
195 BDirectory spoolFolder(folder.String());
196 status_t status = TakeJob(driver.String(), jobFile.String(),
197 &spoolFolder);
198 SendReply(message, status);
199 }
200
201
202 status_t
TakeJob(const char * driver,const char * spoolFile,BDirectory * spoolFolder)203 PrintAddOnServerApplication::TakeJob(const char* driver, const char* spoolFile,
204 BDirectory* spoolFolder)
205 {
206 PrinterDriverAddOn addOn(driver);
207 return addOn.TakeJob(spoolFile, spoolFolder);
208 }
209
210
211 void
SendReply(BMessage * message,status_t status)212 PrintAddOnServerApplication::SendReply(BMessage* message, status_t status)
213 {
214 BMessage reply(B_REPLY);
215 reply.AddInt32(kPrintAddOnServerStatusAttribute, status);
216 message->SendReply(&reply);
217 }
218
219
220 void
SendReply(BMessage * message,BMessage * reply)221 PrintAddOnServerApplication::SendReply(BMessage* message, BMessage* reply)
222 {
223 reply->AddInt32(kPrintAddOnServerStatusAttribute, B_OK);
224 message->SendReply(reply);
225 }
226
227
228 int
main(int argc,char * argv[])229 main(int argc, char* argv[])
230 {
231 PrintAddOnServerApplication application(
232 kPrintAddOnServerApplicationSignature);
233 application.Run();
234 }
235