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