xref: /haiku/src/libs/print/libprint/JobData.cpp (revision 746cac055adc6ac3308c7bc2d29040fb95689cc9)
1 /*
2  * JobData.cpp
3  * Copyright 1999-2000 Y.Takagi. All Rights Reserved.
4  */
5 
6 #include <InterfaceDefs.h>
7 #include <Message.h>
8 #include "JobData.h"
9 #include "PrinterCap.h"
10 #include "DbgMsg.h"
11 
12 static const char *kJDXRes                  = "xres";
13 static const char *kJDYRes                  = "yres";
14 static const char *kJDCopies                = "copies";
15 static const char *kJDOrientation           = "orientation";
16 static const char *kJDScaling               = "scale";
17 static const char *kJDScaledPaperRect       = "paper_rect";
18 static const char *kJDScaledPrintableRect   = "printable_rect";
19 static const char *kJDFirstPage             = "first_page";
20 static const char *kJDLastPage              = "last_page";
21 
22 static const char *kJDShowPreview           = "JJJJ_showPreview";
23 static const char *kJDPaper                 = "JJJJ_paper";
24 static const char *kJDNup                   = "JJJJ_nup";
25 static const char *kJDGamma                 = "JJJJ_gamma";
26 static const char *kJDInkDensity            = "JJJJ_ink_density";
27 static const char *kJDPaperSource           = "JJJJ_paper_source";
28 static const char *kJDCollate               = "JJJJ_collate";
29 static const char *kJDReverse               = "JJJJ_reverse";
30 static const char *kJDPrintStyle            = "JJJJ_print_style";
31 static const char *kJDBindingLocation       = "JJJJ_binding_location";
32 static const char *kJDPageOrder             = "JJJJ_page_order";
33 static const char *kJDColor                 = "JJJJ_color";
34 static const char *kJDDitherType            = "JJJJ_dither_type";
35 static const char *kJDPaperRect             = "JJJJ_paper_rect";
36 static const char* kJDPrintableRect         = "JJJJ_printable_rect";
37 static const char* kJDPageSelection         = "JJJJ_page_selection";
38 static const char* kJDMarginUnit            = "JJJJ_margin_unit";
39 static const char* kJDPhysicalRect          = "JJJJ_physical_rect";
40 static const char* kJDScaledPhysicalRect    = "JJJJ_scaled_physical_rect";
41 
42 JobData::JobData(BMessage *msg, const PrinterCap *cap, Settings settings)
43 {
44 	load(msg, cap, settings);
45 }
46 
47 JobData::~JobData()
48 {
49 }
50 
51 JobData::JobData(const JobData &job_data)
52 {
53 	fShowPreview           = job_data.fShowPreview;
54 	fPaper                 = job_data.fPaper;
55 	fXRes                  = job_data.fXRes;
56 	fYRes                  = job_data.fYRes;
57 	fOrientation           = job_data.fOrientation;
58 	fScaling               = job_data.fScaling;
59 	fPaperRect             = job_data.fPaperRect;
60 	fScaledPaperRect       = job_data.fScaledPaperRect;
61 	fPrintableRect         = job_data.fPrintableRect;
62 	fScaledPrintableRect   = job_data.fScaledPrintableRect;
63 	fNup                   = job_data.fNup;
64 	fFirstPage             = job_data.fFirstPage;
65 	fLastPage              = job_data.fLastPage;
66 	fGamma                 = job_data.fGamma;
67 	fInkDensity            = job_data.fInkDensity;
68 	fPaperSource           = job_data.fPaperSource;
69 	fCopies                = job_data.fCopies;
70 	fCollate               = job_data.fCollate;
71 	fReverse               = job_data.fReverse;
72 	fPrintStyle            = job_data.fPrintStyle;
73 	fBindingLocation       = job_data.fBindingLocation;
74 	fPageOrder             = job_data.fPageOrder;
75 	fSettings              = job_data.fSettings;
76 	fMsg                   = job_data.fMsg;
77 	fColor                 = job_data.fColor;
78 	fDitherType            = job_data.fDitherType;
79 	fPageSelection         = job_data.fPageSelection;
80 	fMarginUnit            = job_data.fMarginUnit;
81 	fPhysicalRect          = job_data.fPhysicalRect;
82 	fScaledPhysicalRect    = job_data.fScaledPhysicalRect;
83 }
84 
85 JobData &JobData::operator = (const JobData &job_data)
86 {
87 	fShowPreview           = job_data.fShowPreview;
88 	fPaper                 = job_data.fPaper;
89 	fXRes                  = job_data.fXRes;
90 	fYRes                  = job_data.fYRes;
91 	fOrientation           = job_data.fOrientation;
92 	fScaling               = job_data.fScaling;
93 	fPaperRect             = job_data.fPaperRect;
94 	fScaledPaperRect       = job_data.fScaledPaperRect;
95 	fPrintableRect         = job_data.fPrintableRect;
96 	fScaledPrintableRect   = job_data.fScaledPrintableRect;
97 	fNup                   = job_data.fNup;
98 	fFirstPage             = job_data.fFirstPage;
99 	fLastPage              = job_data.fLastPage;
100 	fGamma                 = job_data.fGamma;
101 	fInkDensity            = job_data.fInkDensity;
102 	fPaperSource           = job_data.fPaperSource;
103 	fCopies                = job_data.fCopies;
104 	fCollate               = job_data.fCollate;
105 	fReverse               = job_data.fReverse;
106 	fPrintStyle            = job_data.fPrintStyle;
107 	fBindingLocation       = job_data.fBindingLocation;
108 	fPageOrder             = job_data.fPageOrder;
109 	fSettings              = job_data.fSettings;
110 	fMsg                   = job_data.fMsg;
111 	fColor                 = job_data.fColor;
112 	fDitherType            = job_data.fDitherType;
113 	fPageSelection         = job_data.fPageSelection;
114 	fMarginUnit            = job_data.fMarginUnit;
115 	fPhysicalRect          = job_data.fPhysicalRect;
116 	fScaledPhysicalRect    = job_data.fScaledPhysicalRect;
117 	return *this;
118 }
119 
120 void JobData::load(BMessage *msg, const PrinterCap *cap, Settings settings)
121 {
122 	fMsg = msg;
123 	fSettings = settings;
124 
125 	const PaperCap *paperCap = NULL;
126 
127  	if (msg->HasBool(kJDShowPreview))
128  		fShowPreview = msg->FindBool(kJDShowPreview);
129  	else
130  		fShowPreview = false;
131 
132 	if (msg->HasInt32(kJDPaper))
133 		fPaper = (Paper)msg->FindInt32(kJDPaper);
134 	else if (cap->isSupport(PrinterCap::kPaper)) {
135 		paperCap = (const PaperCap *)cap->getDefaultCap(PrinterCap::kPaper);
136 		fPaper = paperCap->paper;
137 	} else
138 		fPaper = kA4;
139 
140 	if (msg->HasInt64(kJDXRes)) {
141 		int64 xres64;
142 		msg->FindInt64(kJDXRes, &xres64);
143 		fXRes = xres64;
144 	} else if (cap->isSupport(PrinterCap::kResolution)) {
145 		fXRes = ((const ResolutionCap *)cap->getDefaultCap(PrinterCap::kResolution))->xres;
146 	} else {
147 		fXRes = 300;
148 	}
149 
150 	if (msg->HasInt64(kJDYRes)) {
151 		int64 yres64;
152 		msg->FindInt64(kJDYRes, &yres64);
153 		fYRes = yres64;
154 	} else if (cap->isSupport(PrinterCap::kResolution)) {
155 		fYRes = ((const ResolutionCap *)cap->getDefaultCap(PrinterCap::kResolution))->yres;
156 	} else {
157 		fYRes = 300;
158 	}
159 
160 	if (msg->HasInt32(kJDOrientation))
161 		fOrientation = (Orientation)msg->FindInt32(kJDOrientation);
162 	else if (cap->isSupport(PrinterCap::kOrientation))
163 		fOrientation = ((const OrientationCap *)cap->getDefaultCap(PrinterCap::kOrientation))->orientation;
164 	else
165 		fOrientation = kPortrait;
166 
167 	if (msg->HasFloat(kJDScaling))
168 		fScaling = msg->FindFloat(kJDScaling);
169 	else
170 		fScaling = 100.0f;
171 
172 	if (msg->HasRect(kJDPaperRect)) {
173 		fPaperRect = msg->FindRect(kJDPaperRect);
174 	} else if (paperCap != NULL) {
175 		fPaperRect = paperCap->paper_rect;
176 	}
177 
178 	if (msg->HasRect(kJDScaledPaperRect)) {
179 		fScaledPaperRect = msg->FindRect(kJDScaledPaperRect);
180 	} else {
181 		fScaledPaperRect = fPaperRect;
182 	}
183 
184 	if (msg->HasRect(kJDPrintableRect)) {
185 		fPrintableRect = msg->FindRect(kJDPrintableRect);
186 	} else if (paperCap != NULL) {
187 		fPrintableRect = paperCap->physical_rect;
188 	}
189 
190 	if (msg->HasRect(kJDScaledPrintableRect)) {
191 		fScaledPrintableRect = msg->FindRect(kJDScaledPrintableRect);
192 	} else {
193 		fScaledPrintableRect = fPrintableRect;
194 	}
195 
196 	if (msg->HasRect(kJDPhysicalRect)) {
197 		fPhysicalRect = msg->FindRect(kJDPhysicalRect);
198 	} else if (paperCap != NULL) {
199 		fPhysicalRect = paperCap->physical_rect;
200 	}
201 
202 	if (msg->HasRect(kJDScaledPhysicalRect)) {
203 		fScaledPhysicalRect = msg->FindRect(kJDScaledPhysicalRect);
204 	} else {
205 		fScaledPhysicalRect = fPhysicalRect;
206 	}
207 
208 	if (msg->HasInt32(kJDFirstPage))
209 		fFirstPage = msg->FindInt32(kJDFirstPage);
210 	else
211 		fFirstPage = 1;
212 
213 	if (msg->HasInt32(kJDLastPage))
214 		fLastPage = msg->FindInt32(kJDLastPage);
215 	else
216 		fLastPage = -1;
217 
218 	if (msg->HasInt32(kJDNup))
219 		fNup = msg->FindInt32(kJDNup);
220 	else
221 		fNup = 1;
222 
223 	if (msg->HasFloat(kJDGamma))
224 		fGamma = fMsg->FindFloat(kJDGamma);
225 	else
226 		fGamma = 0.25f;
227 
228 	if (msg->HasFloat(kJDInkDensity))
229 		fInkDensity = fMsg->FindFloat(kJDInkDensity);
230 	else
231 		fInkDensity = 0.0f;
232 
233 	if (msg->HasInt32(kJDPaperSource))
234 		fPaperSource = (PaperSource)fMsg->FindInt32(kJDPaperSource);
235 	else if (cap->isSupport(PrinterCap::kPaperSource))
236 		fPaperSource = ((const PaperSourceCap *)cap->getDefaultCap(PrinterCap::kPaperSource))->paper_source;
237 	else
238 		fPaperSource = kAuto;
239 
240 	if (msg->HasInt32(kJDCopies))
241 		fCopies = msg->FindInt32(kJDCopies);
242 	else
243 		fCopies = 1;
244 
245 	if (msg->HasBool(kJDCollate))
246 		fCollate = msg->FindBool(kJDCollate);
247 	else
248 		fCollate = false;
249 
250 	if (msg->HasBool(kJDReverse))
251 		fReverse = msg->FindBool(kJDReverse);
252 	else
253 		fReverse = false;
254 
255 	if (msg->HasInt32(kJDPrintStyle))
256 		fPrintStyle = (PrintStyle)msg->FindInt32(kJDPrintStyle);
257 	else if (cap->isSupport(PrinterCap::kPrintStyle))
258 		fPrintStyle = ((const PrintStyleCap *)cap->getDefaultCap(PrinterCap::kPrintStyle))->print_style;
259 	else
260 		fPrintStyle = kSimplex;
261 
262 	if (msg->HasInt32(kJDBindingLocation))
263 		fBindingLocation = (BindingLocation)msg->FindInt32(kJDBindingLocation);
264 	else if (cap->isSupport(PrinterCap::kBindingLocation))
265 		fBindingLocation = ((const BindingLocationCap *)cap->getDefaultCap(PrinterCap::kBindingLocation))->binding_location;
266 	else
267 		fBindingLocation = kLongEdgeLeft;
268 
269 	if (msg->HasInt32(kJDPageOrder))
270 		fPageOrder = (PageOrder)msg->FindInt32(kJDPageOrder);
271 	else
272 		fPageOrder = kAcrossFromLeft;
273 
274 	if (msg->HasInt32(kJDColor))
275 		fColor = (Color)msg->FindInt32(kJDColor);
276 	else if (cap->isSupport(PrinterCap::kColor))
277 		fColor = ((const ColorCap *)cap->getDefaultCap(PrinterCap::kColor))->color;
278 	else
279 		fColor = kMonochrome;
280 
281 	if (msg->HasInt32(kJDDitherType))
282 		fDitherType = (Halftone::DitherType)msg->FindInt32(kJDDitherType);
283 	else
284 		fDitherType = Halftone::kTypeFloydSteinberg;
285 
286 	if (msg->HasInt32(kJDPageSelection))
287 		fPageSelection = (PageSelection)msg->FindInt32(kJDPageSelection);
288 	else
289 		fPageSelection = kAllPages;
290 
291 	if (msg->HasInt32(kJDMarginUnit))
292 		fMarginUnit = (MarginUnit)msg->FindInt32(kJDMarginUnit);
293 	else
294 		fMarginUnit = kUnitInch;
295 }
296 
297 void JobData::save(BMessage *msg)
298 {
299 	if (msg == NULL) {
300 		msg = fMsg;
301 	}
302 
303 	// page settings
304 	msg->RemoveName(kJDPaper);
305 	msg->AddInt32(kJDPaper, fPaper);
306 
307 	msg->RemoveName(kJDXRes);
308 	msg->AddInt64(kJDXRes, fXRes);
309 
310 	msg->RemoveName(kJDYRes);
311 	msg->AddInt64(kJDYRes, fYRes);
312 
313 	msg->RemoveName(kJDOrientation);
314 	msg->AddInt32(kJDOrientation, fOrientation);
315 
316 	msg->RemoveName(kJDScaling);
317 	msg->AddFloat(kJDScaling, fScaling);
318 
319 	msg->RemoveName(kJDPaperRect);
320 	msg->AddRect(kJDPaperRect, fPaperRect);
321 
322 	msg->RemoveName(kJDScaledPaperRect);
323 	msg->AddRect(kJDScaledPaperRect, fScaledPaperRect);
324 
325 	msg->RemoveName(kJDPrintableRect);
326 	msg->AddRect(kJDPrintableRect, fPrintableRect);
327 
328 	msg->RemoveName(kJDScaledPrintableRect);
329 	msg->AddRect(kJDScaledPrintableRect, fScaledPrintableRect);
330 
331 	msg->RemoveName(kJDPhysicalRect);
332 	msg->AddRect(kJDPhysicalRect, fPhysicalRect);
333 
334 	msg->RemoveName(kJDScaledPhysicalRect);
335 	msg->AddRect(kJDScaledPhysicalRect, fScaledPhysicalRect);
336 
337 	msg->RemoveName(kJDMarginUnit);
338 	msg->AddInt32(kJDMarginUnit, fMarginUnit);
339 
340 	// page settings end here
341 
342 	// job settings
343 
344 	// make sure job settings are not present in page settings
345 	msg->RemoveName(kJDShowPreview);
346 	if (fSettings == kJobSettings)
347 		msg->AddBool(kJDShowPreview, fShowPreview);
348 
349 	msg->RemoveName(kJDNup);
350 	if (fSettings == kJobSettings)
351 		msg->AddInt32(kJDNup, fNup);
352 
353 	msg->RemoveName(kJDFirstPage);
354 	if (fSettings == kJobSettings)
355 		msg->AddInt32(kJDFirstPage, fFirstPage);
356 
357 	msg->RemoveName(kJDLastPage);
358 	if (fSettings == kJobSettings)
359 		msg->AddInt32(kJDLastPage, fLastPage);
360 
361 	msg->RemoveName(kJDGamma);
362 	if (fSettings == kJobSettings)
363 		msg->AddFloat(kJDGamma, fGamma);
364 
365 	msg->RemoveName(kJDInkDensity);
366 	if (fSettings == kJobSettings)
367 		msg->AddFloat(kJDInkDensity, fInkDensity);
368 
369 	msg->RemoveName(kJDPaperSource);
370 	if (fSettings == kJobSettings)
371 		msg->AddInt32(kJDPaperSource, fPaperSource);
372 
373 	msg->RemoveName(kJDCopies);
374 	if (fSettings == kJobSettings)
375 		msg->AddInt32(kJDCopies, fCopies);
376 
377 	msg->RemoveName(kJDCollate);
378 	if (fSettings == kJobSettings)
379 		msg->AddBool(kJDCollate, fCollate);
380 
381 	msg->RemoveName(kJDReverse);
382 	if (fSettings == kJobSettings)
383 		msg->AddBool(kJDReverse, fReverse);
384 
385 	msg->RemoveName(kJDPrintStyle);
386 	if (fSettings == kJobSettings)
387 		msg->AddInt32(kJDPrintStyle, fPrintStyle);
388 
389 	msg->RemoveName(kJDBindingLocation);
390 	if (fSettings == kJobSettings)
391 		msg->AddInt32(kJDBindingLocation, fBindingLocation);
392 
393 	msg->RemoveName(kJDPageOrder);
394 	if (fSettings == kJobSettings)
395 		msg->AddInt32(kJDPageOrder, fPageOrder);
396 
397 	msg->RemoveName(kJDColor);
398 	if (fSettings == kJobSettings)
399 		msg->AddInt32(kJDColor, fColor);
400 
401 	msg->RemoveName(kJDDitherType);
402 	if (fSettings == kJobSettings)
403 		msg->AddInt32(kJDDitherType, fDitherType);
404 
405 	msg->RemoveName(kJDPageSelection);
406 	if (fSettings == kJobSettings)
407 		msg->AddInt32(kJDPageSelection, fPageSelection);
408 }
409