btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / kits / print / PrintAddOnServer.cpp
blob90436987b1ab2b2419a83893b7a052e10fe64c50
1 #include "PrintAddOnServer.h"
3 #include <Entry.h>
4 #include <Roster.h>
6 #include "PrinterDriverAddOn.h"
7 #include "PrintAddOnServerProtocol.h"
9 static const bigtime_t kSeconds = 1000000L;
10 static const bigtime_t kDeliveryTimeout = 30 * kSeconds;
11 static const bigtime_t kReplyTimeout = B_INFINITE_TIMEOUT;
14 PrintAddOnServer::PrintAddOnServer(const char* driver)
16 fDriver(driver)
18 fLaunchStatus = Launch(fMessenger);
22 PrintAddOnServer::~PrintAddOnServer()
24 if (fLaunchStatus == B_OK)
25 Quit();
29 status_t
30 PrintAddOnServer::AddPrinter(const char* spoolFolderName)
32 BMessage message(kMessageAddPrinter);
33 message.AddString(kPrinterDriverAttribute, Driver());
34 message.AddString(kPrinterNameAttribute, spoolFolderName);
36 BMessage reply;
37 status_t result = SendRequest(message, reply);
38 if (result != B_OK)
39 return result;
41 return GetResult(reply);
45 status_t
46 PrintAddOnServer::ConfigPage(BDirectory* spoolFolder,
47 BMessage* settings)
49 BMessage message(kMessageConfigPage);
50 message.AddString(kPrinterDriverAttribute, Driver());
51 AddDirectory(message, kPrinterFolderAttribute, spoolFolder);
52 message.AddMessage(kPrintSettingsAttribute, settings);
54 BMessage reply;
55 status_t result = SendRequest(message, reply);
56 if (result != B_OK)
57 return result;
59 return GetResultAndUpdateSettings(reply, settings);
63 status_t
64 PrintAddOnServer::ConfigJob(BDirectory* spoolFolder,
65 BMessage* settings)
67 BMessage message(kMessageConfigJob);
68 message.AddString(kPrinterDriverAttribute, Driver());
69 AddDirectory(message, kPrinterFolderAttribute, spoolFolder);
70 message.AddMessage(kPrintSettingsAttribute, settings);
72 BMessage reply;
73 status_t result = SendRequest(message, reply);
74 if (result != B_OK)
75 return result;
77 return GetResultAndUpdateSettings(reply, settings);
81 status_t
82 PrintAddOnServer::DefaultSettings(BDirectory* spoolFolder,
83 BMessage* settings)
85 BMessage message(kMessageDefaultSettings);
86 message.AddString(kPrinterDriverAttribute, Driver());
87 AddDirectory(message, kPrinterFolderAttribute, spoolFolder);
89 BMessage reply;
90 status_t result = SendRequest(message, reply);
91 if (result != B_OK)
92 return result;
94 return GetResultAndUpdateSettings(reply, settings);
98 status_t
99 PrintAddOnServer::TakeJob(const char* spoolFile,
100 BDirectory* spoolFolder)
102 BMessage message(kMessageTakeJob);
103 message.AddString(kPrinterDriverAttribute, Driver());
104 message.AddString(kPrintJobFileAttribute, spoolFile);
105 AddDirectory(message, kPrinterFolderAttribute, spoolFolder);
107 BMessage reply;
108 status_t result = SendRequest(message, reply);
109 if (result != B_OK)
110 return result;
112 return GetResult(reply);
116 status_t
117 PrintAddOnServer::FindPathToDriver(const char* driver, BPath* path)
119 return PrinterDriverAddOn::FindPathToDriver(driver, path);
123 const char*
124 PrintAddOnServer::Driver() const
126 return fDriver.String();
130 status_t
131 PrintAddOnServer::Launch(BMessenger& messenger)
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;
141 fMessenger = BMessenger(kPrintAddOnServerApplicationSignature, team);
142 return result;
146 bool
147 PrintAddOnServer::IsLaunched()
149 return fLaunchStatus == B_OK;
153 void
154 PrintAddOnServer::Quit()
156 if (fLaunchStatus == B_OK) {
157 fMessenger.SendMessage(B_QUIT_REQUESTED);
158 fLaunchStatus = B_ERROR;
163 void
164 PrintAddOnServer::AddDirectory(BMessage& message, const char* name,
165 BDirectory* directory)
167 BEntry entry;
168 status_t result = directory->GetEntry(&entry);
169 if (result != B_OK)
170 return;
172 BPath path;
173 if (entry.GetPath(&path) != B_OK)
174 return;
176 message.AddString(name, path.Path());
180 void
181 PrintAddOnServer::AddEntryRef(BMessage& message, const char* name,
182 const entry_ref* entryRef)
184 BPath path(entryRef);
185 if (path.InitCheck() != B_OK)
186 return;
188 message.AddString(name, path.Path());
192 status_t
193 PrintAddOnServer::SendRequest(BMessage& request, BMessage& reply)
195 if (!IsLaunched())
196 return fLaunchStatus;
198 return fMessenger.SendMessage(&request, &reply, kDeliveryTimeout,
199 kReplyTimeout);
203 status_t
204 PrintAddOnServer::GetResult(BMessage& reply)
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);
215 status_t
216 PrintAddOnServer::GetResultAndUpdateSettings(BMessage& reply, BMessage* settings)
218 BMessage newSettings;
219 if (reply.FindMessage(kPrintSettingsAttribute, &newSettings) == B_OK)
220 *settings = newSettings;
222 settings->PrintToStream();
224 return GetResult(reply);