Added class PrintTransport to simplify the usage of a print transport add-on.

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@6367 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Michael Pfeiffer 2004-01-27 21:19:16 +00:00
parent 3a35412852
commit 393fb10d6e
3 changed files with 195 additions and 0 deletions

View File

@ -0,0 +1,61 @@
/*
PrintTransport
Copyright (c) 2004 OpenBeOS.
Authors:
Michael Pfeiffer
Philippe Houdoin
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef PRINT_TRANSPORT_H
#define PRINT_TRANSPORT_H
#include <image.h>
#include <DataIO.h>
#include <Node.h>
#include <Message.h>
// The class PrintTransport is intended to be used by
// a printer driver.
class PrintTransport
{
public:
PrintTransport();
~PrintTransport();
// opens the transport add-on associated with the printerFolder
status_t Open(BNode* printerFolder);
// returns the output stream created by the transport add-on
BDataIO* GetDataIO();
// returns false if the user has canceled the save to file dialog
// of the "Print To File" transport add-on.
bool IsPrintToFileCanceled() const;
private:
BDataIO* fDataIO;
image_id fAddOnID;
void (*fExitProc)(void);
};
#endif PRINT_TRANSPORT_H

View File

@ -8,6 +8,7 @@ StaticLibrary
PicturePrinter.cpp
PrintJobReader.cpp
PictureIterator.cpp
PrintTransport.cpp
;
StaticLibrary

View File

@ -0,0 +1,133 @@
/*
PrintTransport
Copyright (c) 2004 OpenBeOS.
Authors:
Philippe Houdoin
Michael Pfeiffer
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "PrintTransport.h"
#include <Directory.h>
#include <File.h>
#include <FindDirectory.h>
#include <Path.h>
#include <String.h>
// implementation of class PrintTransport
PrintTransport::PrintTransport()
: fDataIO(NULL)
, fAddOnID(-1)
, fExitProc(NULL)
{
}
PrintTransport::~PrintTransport()
{
if (fExitProc) {
(*fExitProc)();
fExitProc = NULL;
}
if (fAddOnID >= 0) {
unload_add_on(fAddOnID);
fAddOnID = -1;
}
}
status_t PrintTransport::Open(BNode* printerFolder)
{
// already opened?
if (fDataIO != NULL) {
return B_ERROR;
}
// retrieve transport add-on name from printer folder attribute
BString transportName;
if (printerFolder->ReadAttrString("transport", &transportName) != B_OK) {
return B_ERROR;
}
// try first in user add-ons directory
BPath path;
find_directory(B_USER_ADDONS_DIRECTORY, &path);
path.Append("Print/transport");
path.Append(transportName.String());
fAddOnID = load_add_on(path.Path());
if (fAddOnID < 0) {
// on failure try in system add-ons directory
find_directory(B_BEOS_ADDONS_DIRECTORY, &path);
path.Append("Print/transport");
path.Append(transportName.String());
fAddOnID = load_add_on(path.Path());
}
if (fAddOnID < 0) {
// failed to load transport add-on
return B_ERROR;
}
// get init & exit proc
BDataIO* (*initProc)(BMessage*);
get_image_symbol(fAddOnID, "init_transport", B_SYMBOL_TYPE_TEXT, (void **) &initProc);
get_image_symbol(fAddOnID, "exit_transport", B_SYMBOL_TYPE_TEXT, (void **) &fExitProc);
if (initProc == NULL || fExitProc == NULL) {
// transport add-on has not the proper interface
return B_ERROR;
}
// now, initialize the transport add-on
node_ref ref;
BDirectory dir;
printerFolder->GetNodeRef(&ref);
dir.SetTo(&ref);
if (path.SetTo(&dir, NULL) != B_OK) {
return B_ERROR;
}
// request BDataIO object from transport add-on
BMessage input('TRIN');
input.AddString("printer_file", path.Path());
fDataIO = (*initProc)(&input);
return B_OK;
}
BDataIO*
PrintTransport::GetDataIO()
{
return fDataIO;
}
bool PrintTransport::IsPrintToFileCanceled() const
{
// The BeOS "Print To File" transport add-on returns a non-NULL BDataIO *
// even after user filepanel cancellation!
BFile* file = dynamic_cast<BFile*>(fDataIO);
return file != NULL && file->InitCheck() != B_OK;
}