1/*
2
3PrintJobReader
4
5Copyright (c) 2002 OpenBeOS.
6
7Author:
8	Michael Pfeiffer
9
10Permission is hereby granted, free of charge, to any person obtaining a copy of
11this software and associated documentation files (the "Software"), to deal in
12the Software without restriction, including without limitation the rights to
13use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
14of the Software, and to permit persons to whom the Software is furnished to do
15so, subject to the following conditions:
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26THE SOFTWARE.
27
28*/
29
30#include "PrintJobReader.h"
31
32
33#include <stdio.h>
34
35
36#include <Picture.h>
37#include <PrintJob.h>
38
39
40// #pragma mark --- PrintJobPage
41
42
43PrintJobPage::PrintJobPage()
44	: fNextPicture(-1)
45	, fNumberOfPictures(0)
46	, fPicture(0)
47	, fStatus(B_ERROR)
48{
49}
50
51
52PrintJobPage::PrintJobPage(const PrintJobPage& copy)
53	: fJobFile(copy.fJobFile)
54	, fNextPicture(copy.fNextPicture)
55	, fNumberOfPictures(copy.fNumberOfPictures)
56	, fPicture(copy.fPicture)
57	, fStatus(copy.fStatus)
58{
59}
60
61
62PrintJobPage& PrintJobPage::operator=(const PrintJobPage& copy)
63{
64	if (this != &copy) {
65		fJobFile = copy.fJobFile;
66		fNextPicture = copy.fNextPicture;
67		fNumberOfPictures = copy.fNumberOfPictures;
68		fPicture = copy.fPicture;
69		fStatus = copy.fStatus;
70	}
71	return *this;
72}
73
74
75PrintJobPage::PrintJobPage(BFile* jobFile, off_t start)
76	: fJobFile(*jobFile)
77	, fPicture(0)
78	, fStatus(B_ERROR)
79{
80	off_t size;
81	if (fJobFile.GetSize(&size) != B_OK || start > size)
82		return;
83
84	if (fJobFile.Seek(start, SEEK_SET) != start)
85		return;
86
87	if (fJobFile.Read(&fNumberOfPictures, sizeof(int32)) == sizeof(int32)) {
88		// (sizeof(int32) * 10) == padding in _page_header_
89		fJobFile.Seek(sizeof(off_t) + sizeof(int32) * 10, SEEK_CUR);
90		fNextPicture = fJobFile.Position();
91		fStatus = B_OK;
92	}
93}
94
95
96status_t PrintJobPage::InitCheck() const
97{
98	return fStatus;
99}
100
101
102status_t PrintJobPage::NextPicture(BPicture& picture, BPoint& point, BRect& rect)
103{
104	if (fPicture >= fNumberOfPictures)
105		return B_ERROR;
106	fPicture++;
107
108	fJobFile.Seek(fNextPicture, SEEK_SET);
109	fJobFile.Read(&point, sizeof(BPoint));
110	fJobFile.Read(&rect, sizeof(BRect));
111	status_t rc = picture.Unflatten(&fJobFile);
112	fNextPicture = fJobFile.Position();
113
114	if (rc != B_OK)
115		fPicture = fNumberOfPictures;
116
117	return rc;
118}
119
120
121// # pragma mark --- PrintJobReader
122
123
124PrintJobReader::PrintJobReader(BFile* jobFile)
125	: fJobFile(*jobFile)
126	, fNumberOfPages(-1)
127	, fPageIndex(NULL)
128{
129#ifndef B_BEOS_VERSION_DANO
130	print_file_header header;
131#else
132	BPrintJob::print_file_header header;
133#endif
134	fJobFile.Seek(0, SEEK_SET);
135	if (fJobFile.Read(&header, sizeof(header)) == sizeof(header)) {
136		if (fJobSettings.Unflatten(&fJobFile) == B_OK) {
137			fNumberOfPages = header.page_count;
138			fPageIndex = new off_t[fNumberOfPages];
139
140			_BuildPageIndex();
141		}
142	}
143}
144
145
146PrintJobReader::~PrintJobReader()
147{
148	delete[] fPageIndex;
149}
150
151
152status_t PrintJobReader::InitCheck() const
153{
154	return fNumberOfPages > 0 ? B_OK : B_ERROR;
155}
156
157
158void PrintJobReader::_BuildPageIndex()
159{
160	off_t next_page;
161	int32 number_of_pictures;
162	for (int32 page = 0; page < fNumberOfPages; ++page) {
163		fPageIndex[page] = fJobFile.Position();
164		if (fJobFile.Read(&number_of_pictures, sizeof(int32)) == sizeof(int32)
165			&& fJobFile.Read(&next_page, sizeof(off_t)) == sizeof(off_t)
166			&& fPageIndex[page] < next_page) {
167			fJobFile.Seek(next_page, SEEK_SET);
168		} else {
169			fNumberOfPages = 0;
170			delete[] fPageIndex;
171			fPageIndex = NULL;
172			return;
173		}
174	}
175}
176
177
178status_t PrintJobReader::GetPage(int32 page, PrintJobPage& pjp)
179{
180	if (0 <= page && page < fNumberOfPages) {
181		PrintJobPage p(&fJobFile, fPageIndex[page]);
182		if (p.InitCheck() == B_OK) {
183			pjp = p;
184			return B_OK;
185		}
186	}
187	return B_ERROR;
188}
189
190
191int32 PrintJobReader::FirstPage() const
192{
193	int32 firstPage = -1;
194	fJobSettings.FindInt32("first_page", &firstPage);
195	return firstPage;
196}
197
198
199int32 PrintJobReader::LastPage() const
200{
201	int32 lastPage = -1;
202	fJobSettings.FindInt32("last_page", &lastPage);
203	return lastPage;
204}
205
206
207BRect PrintJobReader::PaperRect() const
208{
209	BRect r;
210	fJobSettings.FindRect("paper_rect", &r);
211	return r;
212}
213
214
215BRect PrintJobReader::PrintableRect() const
216{
217	BRect r;
218	fJobSettings.FindRect("printable_rect", &r);
219	return r;
220}
221
222
223void PrintJobReader::GetResolution(int32 *xdpi, int32 *ydpi) const
224{
225	fJobSettings.FindInt32("xres", xdpi);
226	fJobSettings.FindInt32("yres", ydpi);
227}
228
229
230float PrintJobReader::GetScale() const
231{
232	float scale = 1.0;
233	fJobSettings.FindFloat("scale", &scale);
234	return scale;
235}
236