xref: /haiku/src/libs/print/libprint/JobData.cpp (revision 13581b3d2a71545960b98fefebc5225b5bf29072)
1 /*
2  * JobData.cpp
3  * Copyright 1999-2000 Y.Takagi. All Rights Reserved.
4  */
5 
6 #include "JobData.h"
7 
8 #include <Debug.h>
9 #include <InterfaceDefs.h>
10 #include <Message.h>
11 
12 #include <sstream>
13 
14 #include "PrinterCap.h"
15 #include "DbgMsg.h"
16 
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";
26 
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";
48 
49 
50 DriverSpecificSettings::DriverSpecificSettings()
51 {
52 }
53 
54 
55 DriverSpecificSettings::DriverSpecificSettings(
56 	const DriverSpecificSettings& settings)
57 	:
58 	fSettings(settings.fSettings)
59 {
60 }
61 
62 
63 DriverSpecificSettings &
64 DriverSpecificSettings::operator=(const DriverSpecificSettings &settings)
65 {
66 	fSettings = settings.fSettings;
67 	return *this;
68 }
69 
70 
71 void
72 DriverSpecificSettings::MakeEmpty()
73 {
74 	fSettings.MakeEmpty();
75 }
76 
77 
78 bool
79 DriverSpecificSettings::HasString(const char* key) const
80 {
81 	const char* value;
82 	return fSettings.FindString(key, &value) == B_OK;
83 }
84 
85 
86 const char*
87 DriverSpecificSettings::GetString(const char* key) const
88 {
89 	ASSERT(HasString(key));
90 	const char* value = NULL;
91 	fSettings.FindString(key, &value);
92 	return value;
93 }
94 
95 
96 void
97 DriverSpecificSettings::SetString(const char* key, const char* value)
98 {
99 	if (HasString(key))
100 		fSettings.ReplaceString(key, value);
101 	else
102 		fSettings.AddString(key, value);
103 }
104 
105 
106 bool
107 DriverSpecificSettings::HasBoolean(const char* key) const
108 {
109 	bool value;
110 	return fSettings.FindBool(key, &value) == B_OK;
111 }
112 
113 
114 bool
115 DriverSpecificSettings::GetBoolean(const char* key) const
116 {
117 	ASSERT(HasBoolean(key));
118 	bool value;
119 	fSettings.FindBool(key, &value);
120 	return value;
121 }
122 
123 
124 void
125 DriverSpecificSettings::SetBoolean(const char* key, bool value)
126 {
127 	if (HasBoolean(key))
128 		fSettings.ReplaceBool(key, value);
129 	else
130 		fSettings.AddBool(key, value);
131 }
132 
133 
134 bool
135 DriverSpecificSettings::HasInt(const char* key) const
136 {
137 	int32 value;
138 	return fSettings.FindInt32(key, &value) == B_OK;
139 }
140 
141 
142 int32
143 DriverSpecificSettings::GetInt(const char* key) const
144 {
145 	ASSERT(HasInt(key));
146 	int32 value;
147 	fSettings.FindInt32(key, &value);
148 	return value;
149 }
150 
151 
152 void
153 DriverSpecificSettings::SetInt(const char* key, int32 value)
154 {
155 	if (HasInt(key))
156 		fSettings.ReplaceInt32(key, value);
157 	else
158 		fSettings.AddInt32(key, value);
159 }
160 
161 
162 bool
163 DriverSpecificSettings::HasDouble(const char* key) const
164 {
165 	double value;
166 	return fSettings.FindDouble(key, &value) == B_OK;
167 }
168 
169 
170 double
171 DriverSpecificSettings::GetDouble(const char* key) const
172 {
173 	ASSERT(HasDouble(key));
174 	double value;
175 	fSettings.FindDouble(key, &value);
176 	return value;
177 }
178 
179 
180 void
181 DriverSpecificSettings::SetDouble(const char* key, double value)
182 {
183 	if (HasDouble(key))
184 		fSettings.ReplaceDouble(key, value);
185 	else
186 		fSettings.AddDouble(key, value);
187 }
188 
189 
190 BMessage&
191 DriverSpecificSettings::Message()
192 {
193 	return fSettings;
194 }
195 
196 
197 JobData::JobData(BMessage *message, const PrinterCap *printerCap, SettingType settingType)
198 {
199 	Load(message, printerCap, settingType);
200 }
201 
202 
203 JobData::~JobData()
204 {
205 }
206 
207 
208 JobData::JobData(const JobData &jobData)
209 {
210 	*this = jobData;
211 }
212 
213 
214 JobData&
215 JobData::operator=(const JobData &jobData)
216 {
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;
250 }
251 
252 
253 void
254 JobData::Load(BMessage* message, const PrinterCap* printerCap,
255 	SettingType settingType)
256 {
257 	fMsg = message;
258 	fSettingType = settingType;
259 
260 	const PaperCap *paperCap = NULL;
261 
262 	if (message->HasBool(kJDShowPreview))
263 		fShowPreview = message->FindBool(kJDShowPreview);
264 	else
265 		fShowPreview = false;
266 
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;
275 
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;
284 	}
285 
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;
295 	}
296 
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;
306 	}
307 
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;
315 
316 	if (message->HasFloat(kJDScaling))
317 		fScaling = message->FindFloat(kJDScaling);
318 	else
319 		fScaling = 100.0f;
320 
321 	if (message->HasRect(kJDPaperRect)) {
322 		fPaperRect = message->FindRect(kJDPaperRect);
323 	} else if (paperCap != NULL) {
324 		fPaperRect = paperCap->fPaperRect;
325 	}
326 
327 	if (message->HasRect(kJDScaledPaperRect)) {
328 		fScaledPaperRect = message->FindRect(kJDScaledPaperRect);
329 	} else {
330 		fScaledPaperRect = fPaperRect;
331 	}
332 
333 	if (message->HasRect(kJDPrintableRect)) {
334 		fPrintableRect = message->FindRect(kJDPrintableRect);
335 	} else if (paperCap != NULL) {
336 		fPrintableRect = paperCap->fPhysicalRect;
337 	}
338 
339 	if (message->HasRect(kJDScaledPrintableRect)) {
340 		fScaledPrintableRect = message->FindRect(kJDScaledPrintableRect);
341 	} else {
342 		fScaledPrintableRect = fPrintableRect;
343 	}
344 
345 	if (message->HasRect(kJDPhysicalRect)) {
346 		fPhysicalRect = message->FindRect(kJDPhysicalRect);
347 	} else if (paperCap != NULL) {
348 		fPhysicalRect = paperCap->fPhysicalRect;
349 	}
350 
351 	if (message->HasRect(kJDScaledPhysicalRect)) {
352 		fScaledPhysicalRect = message->FindRect(kJDScaledPhysicalRect);
353 	} else {
354 		fScaledPhysicalRect = fPhysicalRect;
355 	}
356 
357 	if (message->HasInt32(kJDFirstPage))
358 		fFirstPage = message->FindInt32(kJDFirstPage);
359 	else
360 		fFirstPage = 1;
361 
362 	if (message->HasInt32(kJDLastPage))
363 		fLastPage = message->FindInt32(kJDLastPage);
364 	else
365 		fLastPage = -1;
366 
367 	if (message->HasInt32(kJDNup))
368 		fNup = message->FindInt32(kJDNup);
369 	else
370 		fNup = 1;
371 
372 	if (message->HasFloat(kJDGamma))
373 		fGamma = fMsg->FindFloat(kJDGamma);
374 	else
375 		fGamma = 0.25f;
376 
377 	if (message->HasFloat(kJDInkDensity))
378 		fInkDensity = fMsg->FindFloat(kJDInkDensity);
379 	else
380 		fInkDensity = 0.0f;
381 
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;
389 
390 	if (message->HasInt32(kJDCopies))
391 		fCopies = message->FindInt32(kJDCopies);
392 	else
393 		fCopies = 1;
394 
395 	if (message->HasBool(kJDCollate))
396 		fCollate = message->FindBool(kJDCollate);
397 	else
398 		fCollate = false;
399 
400 	if (message->HasBool(kJDReverse))
401 		fReverse = message->FindBool(kJDReverse);
402 	else
403 		fReverse = false;
404 
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;
412 
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;
421 
422 	if (message->HasInt32(kJDPageOrder))
423 		fPageOrder = (PageOrder)message->FindInt32(kJDPageOrder);
424 	else
425 		fPageOrder = kAcrossFromLeft;
426 
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;
434 
435 	if (message->HasInt32(kJDDitherType))
436 		fDitherType = (Halftone::DitherType)message->FindInt32(kJDDitherType);
437 	else
438 		fDitherType = Halftone::kTypeFloydSteinberg;
439 
440 	if (message->HasInt32(kJDPageSelection))
441 		fPageSelection = (PageSelection)message->FindInt32(kJDPageSelection);
442 	else
443 		fPageSelection = kAllPages;
444 
445 	if (message->HasInt32(kJDMarginUnit))
446 		fMarginUnit = (MarginUnit)message->FindInt32(kJDMarginUnit);
447 	else
448 		fMarginUnit = kUnitInch;
449 
450 	if (message->HasMessage(kJDDriverSpecificSettings))
451 		message->FindMessage(kJDDriverSpecificSettings,
452 			&fDriverSpecificSettings.Message());
453 }
454 
455 
456 void
457 JobData::Save(BMessage* message)
458 {
459 	if (message == NULL) {
460 		message = fMsg;
461 	}
462 
463 	// page settings
464 	message->RemoveName(kJDPaper);
465 	message->AddInt32(kJDPaper, fPaper);
466 
467 	message->RemoveName(kJDResolution);
468 	message->AddInt32(kJDResolution, fResolutionID);
469 
470 	message->RemoveName(kJDXRes);
471 	message->AddInt64(kJDXRes, fXRes);
472 
473 	message->RemoveName(kJDYRes);
474 	message->AddInt64(kJDYRes, fYRes);
475 
476 	message->RemoveName(kJDOrientation);
477 	message->AddInt32(kJDOrientation, fOrientation);
478 
479 	message->RemoveName(kJDScaling);
480 	message->AddFloat(kJDScaling, fScaling);
481 
482 	message->RemoveName(kJDPaperRect);
483 	message->AddRect(kJDPaperRect, fPaperRect);
484 
485 	message->RemoveName(kJDScaledPaperRect);
486 	message->AddRect(kJDScaledPaperRect, fScaledPaperRect);
487 
488 	message->RemoveName(kJDPrintableRect);
489 	message->AddRect(kJDPrintableRect, fPrintableRect);
490 
491 	message->RemoveName(kJDScaledPrintableRect);
492 	message->AddRect(kJDScaledPrintableRect, fScaledPrintableRect);
493 
494 	message->RemoveName(kJDPhysicalRect);
495 	message->AddRect(kJDPhysicalRect, fPhysicalRect);
496 
497 	message->RemoveName(kJDScaledPhysicalRect);
498 	message->AddRect(kJDScaledPhysicalRect, fScaledPhysicalRect);
499 
500 	message->RemoveName(kJDMarginUnit);
501 	message->AddInt32(kJDMarginUnit, fMarginUnit);
502 
503 	// page settings end here
504 
505 	// job settings
506 
507 	// make sure job settings are not present in page settings
508 	message->RemoveName(kJDShowPreview);
509 	if (fSettingType == kJobSettings)
510 		message->AddBool(kJDShowPreview, fShowPreview);
511 
512 	message->RemoveName(kJDNup);
513 	if (fSettingType == kJobSettings)
514 		message->AddInt32(kJDNup, fNup);
515 
516 	message->RemoveName(kJDFirstPage);
517 	if (fSettingType == kJobSettings)
518 		message->AddInt32(kJDFirstPage, fFirstPage);
519 
520 	message->RemoveName(kJDLastPage);
521 	if (fSettingType == kJobSettings)
522 		message->AddInt32(kJDLastPage, fLastPage);
523 
524 	message->RemoveName(kJDGamma);
525 	if (fSettingType == kJobSettings)
526 		message->AddFloat(kJDGamma, fGamma);
527 
528 	message->RemoveName(kJDInkDensity);
529 	if (fSettingType == kJobSettings)
530 		message->AddFloat(kJDInkDensity, fInkDensity);
531 
532 	message->RemoveName(kJDPaperSource);
533 	if (fSettingType == kJobSettings)
534 		message->AddInt32(kJDPaperSource, fPaperSource);
535 
536 	message->RemoveName(kJDCopies);
537 	if (fSettingType == kJobSettings)
538 		message->AddInt32(kJDCopies, fCopies);
539 
540 	message->RemoveName(kJDCollate);
541 	if (fSettingType == kJobSettings)
542 		message->AddBool(kJDCollate, fCollate);
543 
544 	message->RemoveName(kJDReverse);
545 	if (fSettingType == kJobSettings)
546 		message->AddBool(kJDReverse, fReverse);
547 
548 	message->RemoveName(kJDPrintStyle);
549 	if (fSettingType == kJobSettings)
550 		message->AddInt32(kJDPrintStyle, fPrintStyle);
551 
552 	message->RemoveName(kJDBindingLocation);
553 	if (fSettingType == kJobSettings)
554 		message->AddInt32(kJDBindingLocation, fBindingLocation);
555 
556 	message->RemoveName(kJDPageOrder);
557 	if (fSettingType == kJobSettings)
558 		message->AddInt32(kJDPageOrder, fPageOrder);
559 
560 	message->RemoveName(kJDColor);
561 	if (fSettingType == kJobSettings)
562 		message->AddInt32(kJDColor, fColor);
563 
564 	message->RemoveName(kJDDitherType);
565 	if (fSettingType == kJobSettings)
566 		message->AddInt32(kJDDitherType, fDitherType);
567 
568 	message->RemoveName(kJDPageSelection);
569 	if (fSettingType == kJobSettings)
570 		message->AddInt32(kJDPageSelection, fPageSelection);
571 
572 	message->RemoveName(kJDDriverSpecificSettings);
573 	if (fSettingType == kJobSettings)
574 	{
575 		message->AddMessage(kJDDriverSpecificSettings,
576 			&fDriverSpecificSettings.Message());
577 	}
578 }
579 
580 
581 DriverSpecificSettings&
582 JobData::Settings()
583 {
584 	return fDriverSpecificSettings;
585 }
586 
587 
588 const DriverSpecificSettings&
589 JobData::Settings() const
590 {
591 	return fDriverSpecificSettings;
592 }
593