vfs: check userland buffers before reading them.
[haiku.git] / src / libs / print / libprint / JobData.cpp
blob13de2bfbc03fc33b5548d42c7268a17304e230e9
1 /*
2 * JobData.cpp
3 * Copyright 1999-2000 Y.Takagi. All Rights Reserved.
4 */
6 #include "JobData.h"
8 #include <Debug.h>
9 #include <InterfaceDefs.h>
10 #include <Message.h>
12 #include <sstream>
14 #include "PrinterCap.h"
15 #include "DbgMsg.h"
17 static const char *kJDXRes = "xres";
18 static const char *kJDYRes = "yres";
19 static const char *kJDCopies = "copies";
20 static const char *kJDOrientation = "orientation";
21 static const char *kJDScaling = "scale";
22 static const char *kJDScaledPaperRect = "paper_rect";
23 static const char *kJDScaledPrintableRect = "printable_rect";
24 static const char *kJDFirstPage = "first_page";
25 static const char *kJDLastPage = "last_page";
27 static const char *kJDShowPreview = "JJJJ_showPreview";
28 static const char *kJDPaper = "JJJJ_paper";
29 static const char *kJDNup = "JJJJ_nup";
30 static const char *kJDGamma = "JJJJ_gamma";
31 static const char *kJDInkDensity = "JJJJ_ink_density";
32 static const char *kJDPaperSource = "JJJJ_paper_source";
33 static const char *kJDCollate = "JJJJ_collate";
34 static const char *kJDReverse = "JJJJ_reverse";
35 static const char *kJDPrintStyle = "JJJJ_print_style";
36 static const char *kJDBindingLocation = "JJJJ_binding_location";
37 static const char *kJDPageOrder = "JJJJ_page_order";
38 static const char *kJDColor = "JJJJ_color";
39 static const char *kJDDitherType = "JJJJ_dither_type";
40 static const char *kJDPaperRect = "JJJJ_paper_rect";
41 static const char* kJDPrintableRect = "JJJJ_printable_rect";
42 static const char* kJDPageSelection = "JJJJ_page_selection";
43 static const char* kJDMarginUnit = "JJJJ_margin_unit";
44 static const char* kJDPhysicalRect = "JJJJ_physical_rect";
45 static const char* kJDScaledPhysicalRect = "JJJJ_scaled_physical_rect";
46 static const char* kJDResolution = "JJJJ_resolution";
47 static const char* kJDDriverSpecificSettings = "JJJJ_driverSpecificSettings";
50 DriverSpecificSettings::DriverSpecificSettings()
55 DriverSpecificSettings::DriverSpecificSettings(
56 const DriverSpecificSettings& settings)
58 fSettings(settings.fSettings)
63 DriverSpecificSettings &
64 DriverSpecificSettings::operator=(const DriverSpecificSettings &settings)
66 fSettings = settings.fSettings;
67 return *this;
71 void
72 DriverSpecificSettings::MakeEmpty()
74 fSettings.MakeEmpty();
78 bool
79 DriverSpecificSettings::HasString(const char* key) const
81 const char* value;
82 return fSettings.FindString(key, &value) == B_OK;
86 const char*
87 DriverSpecificSettings::GetString(const char* key) const
89 ASSERT(HasString(key));
90 const char* value = NULL;
91 fSettings.FindString(key, &value);
92 return value;
96 void
97 DriverSpecificSettings::SetString(const char* key, const char* value)
99 if (HasString(key))
100 fSettings.ReplaceString(key, value);
101 else
102 fSettings.AddString(key, value);
106 bool
107 DriverSpecificSettings::HasBoolean(const char* key) const
109 bool value;
110 return fSettings.FindBool(key, &value) == B_OK;
114 bool
115 DriverSpecificSettings::GetBoolean(const char* key) const
117 ASSERT(HasBoolean(key));
118 bool value;
119 fSettings.FindBool(key, &value);
120 return value;
124 void
125 DriverSpecificSettings::SetBoolean(const char* key, bool value)
127 if (HasBoolean(key))
128 fSettings.ReplaceBool(key, value);
129 else
130 fSettings.AddBool(key, value);
134 bool
135 DriverSpecificSettings::HasInt(const char* key) const
137 int32 value;
138 return fSettings.FindInt32(key, &value) == B_OK;
142 int32
143 DriverSpecificSettings::GetInt(const char* key) const
145 ASSERT(HasInt(key));
146 int32 value;
147 fSettings.FindInt32(key, &value);
148 return value;
152 void
153 DriverSpecificSettings::SetInt(const char* key, int32 value)
155 if (HasInt(key))
156 fSettings.ReplaceInt32(key, value);
157 else
158 fSettings.AddInt32(key, value);
162 bool
163 DriverSpecificSettings::HasDouble(const char* key) const
165 double value;
166 return fSettings.FindDouble(key, &value) == B_OK;
170 double
171 DriverSpecificSettings::GetDouble(const char* key) const
173 ASSERT(HasDouble(key));
174 double value;
175 fSettings.FindDouble(key, &value);
176 return value;
180 void
181 DriverSpecificSettings::SetDouble(const char* key, double value)
183 if (HasDouble(key))
184 fSettings.ReplaceDouble(key, value);
185 else
186 fSettings.AddDouble(key, value);
190 BMessage&
191 DriverSpecificSettings::Message()
193 return fSettings;
197 JobData::JobData(BMessage *message, const PrinterCap *printerCap, SettingType settingType)
199 Load(message, printerCap, settingType);
203 JobData::~JobData()
208 JobData::JobData(const JobData &jobData)
210 *this = jobData;
214 JobData&
215 JobData::operator=(const JobData &jobData)
217 fShowPreview = jobData.fShowPreview;
218 fPaper = jobData.fPaper;
219 fResolutionID = jobData.fResolutionID;
220 fXRes = jobData.fXRes;
221 fYRes = jobData.fYRes;
222 fOrientation = jobData.fOrientation;
223 fScaling = jobData.fScaling;
224 fPaperRect = jobData.fPaperRect;
225 fScaledPaperRect = jobData.fScaledPaperRect;
226 fPrintableRect = jobData.fPrintableRect;
227 fScaledPrintableRect = jobData.fScaledPrintableRect;
228 fNup = jobData.fNup;
229 fFirstPage = jobData.fFirstPage;
230 fLastPage = jobData.fLastPage;
231 fGamma = jobData.fGamma;
232 fInkDensity = jobData.fInkDensity;
233 fPaperSource = jobData.fPaperSource;
234 fCopies = jobData.fCopies;
235 fCollate = jobData.fCollate;
236 fReverse = jobData.fReverse;
237 fPrintStyle = jobData.fPrintStyle;
238 fBindingLocation = jobData.fBindingLocation;
239 fPageOrder = jobData.fPageOrder;
240 fSettingType = jobData.fSettingType;
241 fMsg = jobData.fMsg;
242 fColor = jobData.fColor;
243 fDitherType = jobData.fDitherType;
244 fPageSelection = jobData.fPageSelection;
245 fMarginUnit = jobData.fMarginUnit;
246 fPhysicalRect = jobData.fPhysicalRect;
247 fScaledPhysicalRect = jobData.fScaledPhysicalRect;
248 fDriverSpecificSettings = jobData.fDriverSpecificSettings;
249 return *this;
253 void
254 JobData::Load(BMessage* message, const PrinterCap* printerCap,
255 SettingType settingType)
257 fMsg = message;
258 fSettingType = settingType;
260 const PaperCap *paperCap = NULL;
262 if (message->HasBool(kJDShowPreview))
263 fShowPreview = message->FindBool(kJDShowPreview);
264 else
265 fShowPreview = false;
267 if (message->HasInt32(kJDPaper))
268 fPaper = (Paper)message->FindInt32(kJDPaper);
269 else if (printerCap->Supports(PrinterCap::kPaper)) {
270 paperCap = (const PaperCap *)printerCap->GetDefaultCap(
271 PrinterCap::kPaper);
272 fPaper = paperCap->fPaper;
273 } else
274 fPaper = kA4;
276 if (message->HasInt32(kJDResolution)) {
277 message->FindInt32(kJDResolution, &fResolutionID);
278 } else if (printerCap->Supports(PrinterCap::kResolution)) {
279 fResolutionID = printerCap->GetDefaultCap(PrinterCap::kResolution)
280 ->ID();
281 } else {
282 // should not reach here!
283 fResolutionID = 0;
286 if (message->HasInt64(kJDXRes)) {
287 int64 xres64;
288 message->FindInt64(kJDXRes, &xres64);
289 fXRes = xres64;
290 } else if (printerCap->Supports(PrinterCap::kResolution)) {
291 fXRes = ((const ResolutionCap *)printerCap->GetDefaultCap(
292 PrinterCap::kResolution))->fXResolution;
293 } else {
294 fXRes = 300;
297 if (message->HasInt64(kJDYRes)) {
298 int64 yres64;
299 message->FindInt64(kJDYRes, &yres64);
300 fYRes = yres64;
301 } else if (printerCap->Supports(PrinterCap::kResolution)) {
302 fYRes = ((const ResolutionCap *)printerCap->GetDefaultCap(
303 PrinterCap::kResolution))->fYResolution;
304 } else {
305 fYRes = 300;
308 if (message->HasInt32(kJDOrientation))
309 fOrientation = (Orientation)message->FindInt32(kJDOrientation);
310 else if (printerCap->Supports(PrinterCap::kOrientation))
311 fOrientation = ((const OrientationCap *)printerCap->GetDefaultCap(
312 PrinterCap::kOrientation))->fOrientation;
313 else
314 fOrientation = kPortrait;
316 if (message->HasFloat(kJDScaling))
317 fScaling = message->FindFloat(kJDScaling);
318 else
319 fScaling = 100.0f;
321 if (message->HasRect(kJDPaperRect)) {
322 fPaperRect = message->FindRect(kJDPaperRect);
323 } else if (paperCap != NULL) {
324 fPaperRect = paperCap->fPaperRect;
327 if (message->HasRect(kJDScaledPaperRect)) {
328 fScaledPaperRect = message->FindRect(kJDScaledPaperRect);
329 } else {
330 fScaledPaperRect = fPaperRect;
333 if (message->HasRect(kJDPrintableRect)) {
334 fPrintableRect = message->FindRect(kJDPrintableRect);
335 } else if (paperCap != NULL) {
336 fPrintableRect = paperCap->fPhysicalRect;
339 if (message->HasRect(kJDScaledPrintableRect)) {
340 fScaledPrintableRect = message->FindRect(kJDScaledPrintableRect);
341 } else {
342 fScaledPrintableRect = fPrintableRect;
345 if (message->HasRect(kJDPhysicalRect)) {
346 fPhysicalRect = message->FindRect(kJDPhysicalRect);
347 } else if (paperCap != NULL) {
348 fPhysicalRect = paperCap->fPhysicalRect;
351 if (message->HasRect(kJDScaledPhysicalRect)) {
352 fScaledPhysicalRect = message->FindRect(kJDScaledPhysicalRect);
353 } else {
354 fScaledPhysicalRect = fPhysicalRect;
357 if (message->HasInt32(kJDFirstPage))
358 fFirstPage = message->FindInt32(kJDFirstPage);
359 else
360 fFirstPage = 1;
362 if (message->HasInt32(kJDLastPage))
363 fLastPage = message->FindInt32(kJDLastPage);
364 else
365 fLastPage = -1;
367 if (message->HasInt32(kJDNup))
368 fNup = message->FindInt32(kJDNup);
369 else
370 fNup = 1;
372 if (message->HasFloat(kJDGamma))
373 fGamma = fMsg->FindFloat(kJDGamma);
374 else
375 fGamma = 0.25f;
377 if (message->HasFloat(kJDInkDensity))
378 fInkDensity = fMsg->FindFloat(kJDInkDensity);
379 else
380 fInkDensity = 0.0f;
382 if (message->HasInt32(kJDPaperSource))
383 fPaperSource = (PaperSource)fMsg->FindInt32(kJDPaperSource);
384 else if (printerCap->Supports(PrinterCap::kPaperSource))
385 fPaperSource = ((const PaperSourceCap *)printerCap->GetDefaultCap(
386 PrinterCap::kPaperSource))->fPaperSource;
387 else
388 fPaperSource = kAuto;
390 if (message->HasInt32(kJDCopies))
391 fCopies = message->FindInt32(kJDCopies);
392 else
393 fCopies = 1;
395 if (message->HasBool(kJDCollate))
396 fCollate = message->FindBool(kJDCollate);
397 else
398 fCollate = false;
400 if (message->HasBool(kJDReverse))
401 fReverse = message->FindBool(kJDReverse);
402 else
403 fReverse = false;
405 if (message->HasInt32(kJDPrintStyle))
406 fPrintStyle = (PrintStyle)message->FindInt32(kJDPrintStyle);
407 else if (printerCap->Supports(PrinterCap::kPrintStyle))
408 fPrintStyle = ((const PrintStyleCap *)printerCap->GetDefaultCap(
409 PrinterCap::kPrintStyle))->fPrintStyle;
410 else
411 fPrintStyle = kSimplex;
413 if (message->HasInt32(kJDBindingLocation))
414 fBindingLocation = (BindingLocation)message->FindInt32(
415 kJDBindingLocation);
416 else if (printerCap->Supports(PrinterCap::kBindingLocation))
417 fBindingLocation = ((const BindingLocationCap *)printerCap->
418 GetDefaultCap(PrinterCap::kBindingLocation))->fBindingLocation;
419 else
420 fBindingLocation = kLongEdgeLeft;
422 if (message->HasInt32(kJDPageOrder))
423 fPageOrder = (PageOrder)message->FindInt32(kJDPageOrder);
424 else
425 fPageOrder = kAcrossFromLeft;
427 if (message->HasInt32(kJDColor))
428 fColor = (Color)message->FindInt32(kJDColor);
429 else if (printerCap->Supports(PrinterCap::kColor))
430 fColor = ((const ColorCap *)printerCap->GetDefaultCap(
431 PrinterCap::kColor))->fColor;
432 else
433 fColor = kMonochrome;
435 if (message->HasInt32(kJDDitherType))
436 fDitherType = (Halftone::DitherType)message->FindInt32(kJDDitherType);
437 else
438 fDitherType = Halftone::kTypeFloydSteinberg;
440 if (message->HasInt32(kJDPageSelection))
441 fPageSelection = (PageSelection)message->FindInt32(kJDPageSelection);
442 else
443 fPageSelection = kAllPages;
445 if (message->HasInt32(kJDMarginUnit))
446 fMarginUnit = (MarginUnit)message->FindInt32(kJDMarginUnit);
447 else
448 fMarginUnit = kUnitInch;
450 if (message->HasMessage(kJDDriverSpecificSettings))
451 message->FindMessage(kJDDriverSpecificSettings,
452 &fDriverSpecificSettings.Message());
456 void
457 JobData::Save(BMessage* message)
459 if (message == NULL) {
460 message = fMsg;
463 // page settings
464 message->RemoveName(kJDPaper);
465 message->AddInt32(kJDPaper, fPaper);
467 message->RemoveName(kJDResolution);
468 message->AddInt32(kJDResolution, fResolutionID);
470 message->RemoveName(kJDXRes);
471 message->AddInt64(kJDXRes, fXRes);
473 message->RemoveName(kJDYRes);
474 message->AddInt64(kJDYRes, fYRes);
476 message->RemoveName(kJDOrientation);
477 message->AddInt32(kJDOrientation, fOrientation);
479 message->RemoveName(kJDScaling);
480 message->AddFloat(kJDScaling, fScaling);
482 message->RemoveName(kJDPaperRect);
483 message->AddRect(kJDPaperRect, fPaperRect);
485 message->RemoveName(kJDScaledPaperRect);
486 message->AddRect(kJDScaledPaperRect, fScaledPaperRect);
488 message->RemoveName(kJDPrintableRect);
489 message->AddRect(kJDPrintableRect, fPrintableRect);
491 message->RemoveName(kJDScaledPrintableRect);
492 message->AddRect(kJDScaledPrintableRect, fScaledPrintableRect);
494 message->RemoveName(kJDPhysicalRect);
495 message->AddRect(kJDPhysicalRect, fPhysicalRect);
497 message->RemoveName(kJDScaledPhysicalRect);
498 message->AddRect(kJDScaledPhysicalRect, fScaledPhysicalRect);
500 message->RemoveName(kJDMarginUnit);
501 message->AddInt32(kJDMarginUnit, fMarginUnit);
503 // page settings end here
505 // job settings
507 // make sure job settings are not present in page settings
508 message->RemoveName(kJDShowPreview);
509 if (fSettingType == kJobSettings)
510 message->AddBool(kJDShowPreview, fShowPreview);
512 message->RemoveName(kJDNup);
513 if (fSettingType == kJobSettings)
514 message->AddInt32(kJDNup, fNup);
516 message->RemoveName(kJDFirstPage);
517 if (fSettingType == kJobSettings)
518 message->AddInt32(kJDFirstPage, fFirstPage);
520 message->RemoveName(kJDLastPage);
521 if (fSettingType == kJobSettings)
522 message->AddInt32(kJDLastPage, fLastPage);
524 message->RemoveName(kJDGamma);
525 if (fSettingType == kJobSettings)
526 message->AddFloat(kJDGamma, fGamma);
528 message->RemoveName(kJDInkDensity);
529 if (fSettingType == kJobSettings)
530 message->AddFloat(kJDInkDensity, fInkDensity);
532 message->RemoveName(kJDPaperSource);
533 if (fSettingType == kJobSettings)
534 message->AddInt32(kJDPaperSource, fPaperSource);
536 message->RemoveName(kJDCopies);
537 if (fSettingType == kJobSettings)
538 message->AddInt32(kJDCopies, fCopies);
540 message->RemoveName(kJDCollate);
541 if (fSettingType == kJobSettings)
542 message->AddBool(kJDCollate, fCollate);
544 message->RemoveName(kJDReverse);
545 if (fSettingType == kJobSettings)
546 message->AddBool(kJDReverse, fReverse);
548 message->RemoveName(kJDPrintStyle);
549 if (fSettingType == kJobSettings)
550 message->AddInt32(kJDPrintStyle, fPrintStyle);
552 message->RemoveName(kJDBindingLocation);
553 if (fSettingType == kJobSettings)
554 message->AddInt32(kJDBindingLocation, fBindingLocation);
556 message->RemoveName(kJDPageOrder);
557 if (fSettingType == kJobSettings)
558 message->AddInt32(kJDPageOrder, fPageOrder);
560 message->RemoveName(kJDColor);
561 if (fSettingType == kJobSettings)
562 message->AddInt32(kJDColor, fColor);
564 message->RemoveName(kJDDitherType);
565 if (fSettingType == kJobSettings)
566 message->AddInt32(kJDDitherType, fDitherType);
568 message->RemoveName(kJDPageSelection);
569 if (fSettingType == kJobSettings)
570 message->AddInt32(kJDPageSelection, fPageSelection);
572 message->RemoveName(kJDDriverSpecificSettings);
573 if (fSettingType == kJobSettings)
575 message->AddMessage(kJDDriverSpecificSettings,
576 &fDriverSpecificSettings.Message());
581 DriverSpecificSettings&
582 JobData::Settings()
584 return fDriverSpecificSettings;
588 const DriverSpecificSettings&
589 JobData::Settings() const
591 return fDriverSpecificSettings;