1 #include "PrintAddOnServer.h"
2
3 #include <Entry.h>
4 #include <Roster.h>
5
6 #include "PrinterDriverAddOn.h"
7 #include "PrintAddOnServerProtocol.h"
8
9 static const bigtime_t kSeconds = 1000000L;
10 static const bigtime_t kDeliveryTimeout = 30 * kSeconds;
11 static const bigtime_t kReplyTimeout = B_INFINITE_TIMEOUT;
12
13
PrintAddOnServer(const char * driver)14 PrintAddOnServer::PrintAddOnServer(const char* driver)
15 :
16 fDriver(driver)
17 {
18 fLaunchStatus = Launch(fMessenger);
19 }
20
21
~PrintAddOnServer()22 PrintAddOnServer::~PrintAddOnServer()
23 {
24 if (fLaunchStatus == B_OK)
25 Quit();
26 }
27
28
29 status_t
AddPrinter(const char * spoolFolderName)30 PrintAddOnServer::AddPrinter(const char* spoolFolderName)
31 {
32 BMessage message(kMessageAddPrinter);
33 message.AddString(kPrinterDriverAttribute, Driver());
34 message.AddString(kPrinterNameAttribute, spoolFolderName);
35
36 BMessage reply;
37 status_t result = SendRequest(message, reply);
38 if (result != B_OK)
39 return result;
40
41 return GetResult(reply);
42 }
43
44
45 status_t
ConfigPage(BDirectory * spoolFolder,BMessage * settings)46 PrintAddOnServer::ConfigPage(BDirectory* spoolFolder,
47 BMessage* settings)
48 {
49 BMessage message(kMessageConfigPage);
50 message.AddString(kPrinterDriverAttribute, Driver());
51 AddDirectory(message, kPrinterFolderAttribute, spoolFolder);
52 message.AddMessage(kPrintSettingsAttribute, settings);
53
54 BMessage reply;
55 status_t result = SendRequest(message, reply);
56 if (result != B_OK)
57 return result;
58
59 return GetResultAndUpdateSettings(reply, settings);
60 }
61
62
63 status_t
ConfigJob(BDirectory * spoolFolder,BMessage * settings)64 PrintAddOnServer::ConfigJob(BDirectory* spoolFolder,
65 BMessage* settings)
66 {
67 BMessage message(kMessageConfigJob);
68 message.AddString(kPrinterDriverAttribute, Driver());
69 AddDirectory(message, kPrinterFolderAttribute, spoolFolder);
70 message.AddMessage(kPrintSettingsAttribute, settings);
71
72 BMessage reply;
73 status_t result = SendRequest(message, reply);
74 if (result != B_OK)
75 return result;
76
77 return GetResultAndUpdateSettings(reply, settings);
78 }
79
80
81 status_t
DefaultSettings(BDirectory * spoolFolder,BMessage * settings)82 PrintAddOnServer::DefaultSettings(BDirectory* spoolFolder,
83 BMessage* settings)
84 {
85 BMessage message(kMessageDefaultSettings);
86 message.AddString(kPrinterDriverAttribute, Driver());
87 AddDirectory(message, kPrinterFolderAttribute, spoolFolder);
88
89 BMessage reply;
90 status_t result = SendRequest(message, reply);
91 if (result != B_OK)
92 return result;
93
94 return GetResultAndUpdateSettings(reply, settings);
95 }
96
97
98 status_t
TakeJob(const char * spoolFile,BDirectory * spoolFolder)99 PrintAddOnServer::TakeJob(const char* spoolFile,
100 BDirectory* spoolFolder)
101 {
102 BMessage message(kMessageTakeJob);
103 message.AddString(kPrinterDriverAttribute, Driver());
104 message.AddString(kPrintJobFileAttribute, spoolFile);
105 AddDirectory(message, kPrinterFolderAttribute, spoolFolder);
106
107 BMessage reply;
108 status_t result = SendRequest(message, reply);
109 if (result != B_OK)
110 return result;
111
112 return GetResult(reply);
113 }
114
115
116 status_t
FindPathToDriver(const char * driver,BPath * path)117 PrintAddOnServer::FindPathToDriver(const char* driver, BPath* path)
118 {
119 return PrinterDriverAddOn::FindPathToDriver(driver, path);
120 }
121
122
123 const char*
Driver() const124 PrintAddOnServer::Driver() const
125 {
126 return fDriver.String();
127 }
128
129
130 status_t
Launch(BMessenger & messenger)131 PrintAddOnServer::Launch(BMessenger& messenger)
132 {
133 team_id team;
134 status_t result =
135 be_roster->Launch(kPrintAddOnServerApplicationSignature,
136 (BMessage*)NULL, &team);
137 if (result != B_OK) {
138 return result;
139 }
140
141 fMessenger = BMessenger(kPrintAddOnServerApplicationSignature, team);
142 return result;
143 }
144
145
146 bool
IsLaunched()147 PrintAddOnServer::IsLaunched()
148 {
149 return fLaunchStatus == B_OK;
150 }
151
152
153 void
Quit()154 PrintAddOnServer::Quit()
155 {
156 if (fLaunchStatus == B_OK) {
157 fMessenger.SendMessage(B_QUIT_REQUESTED);
158 fLaunchStatus = B_ERROR;
159 }
160 }
161
162
163 void
AddDirectory(BMessage & message,const char * name,BDirectory * directory)164 PrintAddOnServer::AddDirectory(BMessage& message, const char* name,
165 BDirectory* directory)
166 {
167 BEntry entry;
168 status_t result = directory->GetEntry(&entry);
169 if (result != B_OK)
170 return;
171
172 BPath path;
173 if (entry.GetPath(&path) != B_OK)
174 return;
175
176 message.AddString(name, path.Path());
177 }
178
179
180 void
AddEntryRef(BMessage & message,const char * name,const entry_ref * entryRef)181 PrintAddOnServer::AddEntryRef(BMessage& message, const char* name,
182 const entry_ref* entryRef)
183 {
184 BPath path(entryRef);
185 if (path.InitCheck() != B_OK)
186 return;
187
188 message.AddString(name, path.Path());
189 }
190
191
192 status_t
SendRequest(BMessage & request,BMessage & reply)193 PrintAddOnServer::SendRequest(BMessage& request, BMessage& reply)
194 {
195 if (!IsLaunched())
196 return fLaunchStatus;
197
198 return fMessenger.SendMessage(&request, &reply, kDeliveryTimeout,
199 kReplyTimeout);
200 }
201
202
203 status_t
GetResult(BMessage & reply)204 PrintAddOnServer::GetResult(BMessage& reply)
205 {
206 int32 status;
207 status_t result = reply.FindInt32(kPrintAddOnServerStatusAttribute,
208 &status);
209 if (result != B_OK)
210 return result;
211 return static_cast<status_t>(status);
212 }
213
214
215 status_t
GetResultAndUpdateSettings(BMessage & reply,BMessage * settings)216 PrintAddOnServer::GetResultAndUpdateSettings(BMessage& reply, BMessage* settings)
217 {
218 BMessage newSettings;
219 if (reply.FindMessage(kPrintSettingsAttribute, &newSettings) == B_OK)
220 *settings = newSettings;
221
222 settings->PrintToStream();
223
224 return GetResult(reply);
225 }
226