1/*
2
3PrintTransport
4
5Copyright (c) 2004 OpenBeOS.
6
7Authors:
8	Philippe Houdoin
9	Michael Pfeiffer
10
11Permission is hereby granted, free of charge, to any person obtaining a copy of
12this software and associated documentation files (the "Software"), to deal in
13the Software without restriction, including without limitation the rights to
14use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
15of the Software, and to permit persons to whom the Software is furnished to do
16so, subject to the following conditions:
17
18The above copyright notice and this permission notice shall be included in all
19copies or substantial portions of the Software.
20
21THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27THE SOFTWARE.
28
29*/
30
31#include "PrintTransport.h"
32
33#include <Directory.h>
34#include <File.h>
35#include <FindDirectory.h>
36#include <Path.h>
37#include <String.h>
38
39// implementation of class PrintTransport
40PrintTransport::PrintTransport()
41	: fDataIO(NULL)
42	, fAddOnID(-1)
43	, fExitProc(NULL)
44{
45}
46
47PrintTransport::~PrintTransport()
48{
49	if (fExitProc) {
50		(*fExitProc)();
51		fExitProc = NULL;
52	}
53
54	if (fAddOnID >= 0) {
55		unload_add_on(fAddOnID);
56		fAddOnID = -1;
57	}
58}
59
60status_t PrintTransport::Open(BNode* printerFolder)
61{
62	// already opened?
63	if (fDataIO != NULL) {
64		return B_ERROR;
65	}
66
67	// retrieve transport add-on name from printer folder attribute
68	BString transportName;
69	if (printerFolder->ReadAttrString("transport", &transportName) != B_OK) {
70		return B_ERROR;
71	}
72
73	const directory_which paths[] = {
74		B_USER_ADDONS_DIRECTORY,
75		B_COMMON_ADDONS_DIRECTORY,
76		B_SYSTEM_ADDONS_DIRECTORY,
77	};
78	BPath path;
79	for (uint32 i = 0; i < sizeof(paths) / sizeof(paths[0]); ++i) {
80		if (find_directory(paths[i], &path) != B_OK)
81			continue;
82	path.Append("Print/transport");
83	path.Append(transportName.String());
84	fAddOnID = load_add_on(path.Path());
85		if (fAddOnID >= 0)
86			break;
87	}
88
89	if (fAddOnID < 0) {
90		// failed to load transport add-on
91		return B_ERROR;
92	}
93
94	// get init & exit proc
95	BDataIO* (*initProc)(BMessage*);
96	get_image_symbol(fAddOnID, "init_transport", B_SYMBOL_TYPE_TEXT, (void **) &initProc);
97	get_image_symbol(fAddOnID, "exit_transport", B_SYMBOL_TYPE_TEXT, (void **) &fExitProc);
98
99	if (initProc == NULL || fExitProc == NULL) {
100		// transport add-on has not the proper interface
101		return B_ERROR;
102	}
103
104	// now, initialize the transport add-on
105	node_ref   ref;
106	BDirectory dir;
107
108	printerFolder->GetNodeRef(&ref);
109	dir.SetTo(&ref);
110
111	if (path.SetTo(&dir, NULL) != B_OK) {
112		return B_ERROR;
113	}
114
115	// request BDataIO object from transport add-on
116	BMessage input('TRIN');
117	input.AddString("printer_file", path.Path());
118	fDataIO = (*initProc)(&input);
119	return B_OK;
120}
121
122BDataIO*
123PrintTransport::GetDataIO()
124{
125	return fDataIO;
126}
127
128bool PrintTransport::IsPrintToFileCanceled() const
129{
130	// The BeOS "Print To File" transport add-on returns a non-NULL BDataIO *
131	// even after user filepanel cancellation!
132	BFile* file = dynamic_cast<BFile*>(fDataIO);
133	return fDataIO == NULL || (file != NULL && file->InitCheck() != B_OK);
134}
135