1/*
2 * Copyright 2002-2007, Haiku.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Michael Pfeiffer
7 */
8
9#include <stdio.h>
10#include <PrintJob.h>
11
12#include "PicturePrinter.h"
13#include "PrintJobReader.h"
14
15void printArguments(const char* application) {
16	printf("Usage: %s [--pictures] files\n", application);
17	printf("Prints the contents of the specified spool files to stdout.\n"
18		"Arguments:\n"
19		"  --pictures  print contents of pictures\n");
20}
21
22int main(int argc, char* argv[]) {
23	BApplication app("application/x-vnd.Haiku.dump-print-job");
24
25	bool printPicture = false;
26	bool hasFiles = false;
27
28	for (int i = 1; i < argc; i ++) {
29		const char* arg = argv[i];
30		if (strcmp(arg, "--pictures") == 0) {
31			printPicture = true;
32			continue;
33		}
34
35		hasFiles = true;
36		BFile jobFile(arg, B_READ_WRITE);
37		if (jobFile.InitCheck() != B_OK) {
38			fprintf(stderr, "Error opening file %s!\n", arg);
39			continue;
40		}
41
42		PrintJobReader reader(&jobFile);
43		if (reader.InitCheck() != B_OK) {
44			fprintf(stderr, "Error reading spool file %s!", arg);
45			continue;
46		}
47
48		printf("Spool file: %s\n", arg);
49		printf("Job settings message:\n");
50		reader.JobSettings()->PrintToStream();
51
52		int32 pages = reader.NumberOfPages();
53		printf("Number of pages: %d\n", (int)pages);
54		for (int page = 0; page < pages; page ++) {
55			printf("Page: %d\n", page+1);
56			PrintJobPage pjp;
57			if (reader.GetPage(page, pjp) != B_OK) {
58				fprintf(stderr, "Error reading page!\n");
59				break;
60			}
61
62			BPicture picture;
63			BPoint pos;
64			BRect rect;
65			printf("Number of pictures: %ld\n", pjp.NumberOfPictures());
66			while (pjp.NextPicture(picture, pos, rect) == B_OK) {
67				printf("Picture position = (%f, %f) bounds = [l: %f t: %f r: %f b: %f]\n",
68					pos.x, pos.y,
69					rect.left, rect.top, rect.right, rect.bottom);
70
71				if (printPicture) {
72					printf("Picture:\n");
73					PicturePrinter printer;
74					printer.Iterate(&picture);
75				}
76			}
77		}
78	}
79
80	if (!hasFiles)
81		printArguments(argv[0]);
82}
83