3 * Copyright 1999-2000 Y.Takagi. All Rights Reserved.
6 #include <FindDirectory.h>
14 #include "Transport.h"
15 #include "PrinterData.h"
22 TransportException::TransportException(const string
& what
)
30 TransportException::What() const
36 Transport::Transport(const PrinterData
*printerData
)
44 const directory_which paths
[] = {
45 B_USER_NONPACKAGED_ADDONS_DIRECTORY
,
46 B_USER_ADDONS_DIRECTORY
,
47 B_SYSTEM_NONPACKAGED_ADDONS_DIRECTORY
,
48 B_BEOS_ADDONS_DIRECTORY
,
51 for (uint32 i
= 0; i
< sizeof(paths
) / sizeof(paths
[0]); ++i
) {
52 if (find_directory(paths
[i
], &path
) != B_OK
)
54 path
.Append("Print/transport");
55 path
.Append(printerData
->GetTransport().c_str());
56 DBGMSG(("load_add_on: %s\n", path
.Path()));
57 fImage
= load_add_on(path
.Path());
63 SetLastError("cannot load a transport add-on");
67 DBGMSG(("image id = %d\n", (int)fImage
));
69 get_image_symbol(fImage
, "init_transport", B_SYMBOL_TYPE_TEXT
, (void **)&fInitTransport
);
70 get_image_symbol(fImage
, "exit_transport", B_SYMBOL_TYPE_TEXT
, (void **)&fExitTransport
);
72 if (fInitTransport
== NULL
) {
73 SetLastError("get_image_symbol failed.");
74 DBGMSG(("init_transport is NULL\n"));
77 if (fExitTransport
== NULL
) {
78 SetLastError("get_image_symbol failed.");
79 DBGMSG(("exit_transport is NULL\n"));
84 printerData
->GetPath(spool_path
);
85 BMessage
*msg
= new BMessage('TRIN');
86 msg
->AddString("printer_file", spool_path
.c_str());
87 fDataStream
= (*fInitTransport
)(msg
);
89 if (fDataStream
== 0) {
90 SetLastError("init_transport failed.");
96 Transport::~Transport()
102 unload_add_on(fImage
);
108 Transport::CheckAbort() const
110 return fDataStream
== 0;
115 string
&Transport::LastError() const
117 return fLastErrorString
;
122 Transport::IsPrintToFileCanceled() const
124 // The BeOS "Print To File" transport add-on returns a non-NULL BDataIO *
125 // even after user filepanel cancellation!
126 BFile
* file
= dynamic_cast<BFile
*>(fDataStream
);
127 return file
!= NULL
&& file
->InitCheck() != B_OK
;
132 Transport::SetLastError(const char *e
)
134 fLastErrorString
= e
;
140 Transport::Write(const void* buffer
, size_t size
) throw(TransportException
)
143 if (size
== (size_t)fDataStream
->Write(buffer
, size
)) {
146 SetLastError("BDataIO::Write failed.");
148 throw TransportException(LastError());