3 * Copyright 1999-2000 Y.Takagi. All Rights Reserved.
4 * Copyright 2004 Michael Pfeiffer.
7 #include "PrinterDriver.h"
9 #include <fs_attr.h> // for attr_info
11 #include <FindDirectory.h>
16 #include <memory> // for auto_ptr
19 #include "AddPrinterDlg.h"
22 #include "GraphicsDriver.h"
23 #include "PrinterCap.h"
24 #include "PrinterData.h"
27 #include "PrintUtils.h"
30 // Implementation of PrinterDriver
32 PrinterDriver::PrinterDriver(BNode
* spoolFolder
)
34 fSpoolFolder(spoolFolder
),
41 PrinterDriver::~PrinterDriver()
43 delete fGraphicsDriver
;
44 fGraphicsDriver
= NULL
;
55 PrinterDriver::InstantiatePrinterData(BNode
* node
)
57 return new PrinterData(node
);
61 PrinterDriver::InitPrinterDataAndCap() {
62 fPrinterData
= InstantiatePrinterData(fSpoolFolder
);
64 // NOTE: moved the load above from the constructor of PrinterData as
65 // we're inheriting from PrinterData and want our overridden versions
66 // of load to be called
67 fPrinterCap
= InstantiatePrinterCap(fPrinterData
);
71 PrinterDriver::About()
74 copyright
= "libprint Copyright © 1999-2000 Y.Takagi\n";
75 copyright
<< GetCopyright();
76 copyright
<< "All Rights Reserved.";
78 AboutBox
app(GetSignature(), GetDriverName(), GetVersion(), copyright
.String());
83 PrinterDriver::AddPrinter(char* printerName
)
85 // print_server has created a spool folder with name printerName in
86 // folder B_USER_PRINTERS_DIRECTORY. It can be used to store
87 // settings in the folder attributes.
88 DBGMSG((">%s: add_printer\n", GetDriverName()));
89 DBGMSG(("\tprinter_name: %s\n", printerName
));
90 DBGMSG(("<%s: add_printer\n", GetDriverName()));
92 if (fPrinterCap
->Supports(PrinterCap::kProtocolClass
)) {
93 if (fPrinterCap
->CountCap(PrinterCap::kProtocolClass
) > 1) {
94 AddPrinterDlg
*dialog
;
95 dialog
= new AddPrinterDlg(fPrinterData
, fPrinterCap
);
96 if (dialog
->Go() != B_OK
) {
101 const ProtocolClassCap
* pcCap
;
102 pcCap
= (const ProtocolClassCap
*)fPrinterCap
->GetDefaultCap(
103 PrinterCap::kProtocolClass
);
105 fPrinterData
->SetProtocolClass(pcCap
->fProtocolClass
);
106 fPrinterData
->Save();
114 PrinterDriver::ConfigPage(BMessage
* settings
)
116 DBGMSG((">%s: config_page\n", GetDriverName()));
117 DUMP_BMESSAGE(settings
);
118 DUMP_BNODE(fSpoolFolder
);
120 BMessage
pageSettings(*settings
);
121 _MergeWithPreviousSettings(kAttrPageSettings
, &pageSettings
);
122 UIDriver
uiDriver(&pageSettings
, fPrinterData
, fPrinterCap
);
123 BMessage
*result
= uiDriver
.ConfigPage();
124 _WriteSettings(kAttrPageSettings
, result
);
126 DUMP_BMESSAGE(result
);
127 DBGMSG(("<%s: config_page\n", GetDriverName()));
132 PrinterDriver::ConfigJob(BMessage
* settings
)
134 DBGMSG((">%s: config_job\n", GetDriverName()));
135 DUMP_BMESSAGE(settings
);
136 DUMP_BNODE(fSpoolFolder
);
138 BMessage
jobSettings(*settings
);
139 _MergeWithPreviousSettings(kAttrJobSettings
, &jobSettings
);
140 UIDriver
uiDriver(&jobSettings
, fPrinterData
, fPrinterCap
);
141 BMessage
*result
= uiDriver
.ConfigJob();
142 _WriteSettings(kAttrJobSettings
, result
);
144 DUMP_BMESSAGE(result
);
145 DBGMSG(("<%s: config_job\n", GetDriverName()));
150 PrinterDriver::TakeJob(BFile
* printJob
, BMessage
* settings
)
152 DBGMSG((">%s: take_job\n", GetDriverName()));
153 DUMP_BMESSAGE(settings
);
154 DUMP_BNODE(fSpoolFolder
);
156 fGraphicsDriver
= InstantiateGraphicsDriver(settings
, fPrinterData
, fPrinterCap
);
157 const JobData
* jobData
= fGraphicsDriver
->GetJobData(printJob
);
158 if (jobData
!= NULL
&& jobData
->GetShowPreview()) {
159 off_t offset
= printJob
->Position();
160 PreviewWindow
*preview
= new PreviewWindow(printJob
, true);
161 if (preview
->Go() != B_OK
) {
162 return new BMessage('okok');
164 printJob
->Seek(offset
, SEEK_SET
);
166 BMessage
*result
= fGraphicsDriver
->TakeJob(printJob
);
168 DUMP_BMESSAGE(result
);
169 DBGMSG(("<%s: take_job\n", GetDriverName()));
173 // read settings from spool folder attribute
175 PrinterDriver::_ReadSettings(const char* attrName
, BMessage
* settings
)
181 settings
->MakeEmpty();
183 if (fSpoolFolder
->GetAttrInfo(attrName
, &info
) == B_OK
&& info
.size
> 0) {
184 data
= new char[info
.size
];
185 auto_ptr
<char> _data(data
);
186 size
= fSpoolFolder
->ReadAttr(attrName
, B_MESSAGE_TYPE
, 0, data
, info
.size
);
187 if (size
== info
.size
&& settings
->Unflatten(data
) == B_OK
) {
194 // write settings to spool folder attribute
196 PrinterDriver::_WriteSettings(const char* attrName
, BMessage
* settings
)
198 if (settings
== NULL
|| settings
->what
!= 'okok') return;
203 size
= settings
->FlattenedSize();
204 data
= new char[size
];
205 auto_ptr
<char> _data(data
);
207 if (data
!= NULL
&& settings
->Flatten(data
, size
) == B_OK
) {
208 fSpoolFolder
->WriteAttr(attrName
, B_MESSAGE_TYPE
, 0, data
, size
);
212 // read settings from spool folder attribute and merge them to current settings
214 PrinterDriver::_MergeWithPreviousSettings(const char* attrName
, BMessage
* settings
)
216 if (settings
== NULL
) return;
219 if (_ReadSettings(attrName
, &stored
)) {
220 AddFields(&stored
, settings
);
225 // Implementation of PrinterDriverInstance
227 class PrinterDriverInstance
230 PrinterDriverInstance(BNode
* spoolFolder
= NULL
);
231 ~PrinterDriverInstance();
232 PrinterDriver
* GetPrinterDriver() { return fInstance
; }
235 PrinterDriver
* fInstance
;
238 PrinterDriverInstance::PrinterDriverInstance(BNode
* spoolFolder
)
240 fInstance
= instantiate_printer_driver(spoolFolder
);
241 if (fInstance
!= NULL
) {
242 fInstance
->InitPrinterDataAndCap();
246 PrinterDriverInstance::~PrinterDriverInstance()
253 // printer driver add-on functions
255 char *add_printer(char *printerName
)
259 BNode
* spoolFolder
= NULL
;
261 if (find_directory(B_USER_PRINTERS_DIRECTORY
, &path
) == B_OK
&&
262 path
.Append(printerName
) == B_OK
&&
263 folder
.SetTo(path
.Path()) == B_OK
) {
264 spoolFolder
= &folder
;
267 PrinterDriverInstance
instance(spoolFolder
);
268 return instance
.GetPrinterDriver()->AddPrinter(printerName
);
271 BMessage
*config_page(BNode
*spoolFolder
, BMessage
*settings
)
273 PrinterDriverInstance
instance(spoolFolder
);
274 return instance
.GetPrinterDriver()->ConfigPage(settings
);
277 BMessage
*config_job(BNode
*spoolFolder
, BMessage
*settings
)
279 PrinterDriverInstance
instance(spoolFolder
);
280 return instance
.GetPrinterDriver()->ConfigJob(settings
);
283 BMessage
*take_job(BFile
*printJob
, BNode
*spoolFolder
, BMessage
*settings
)
285 PrinterDriverInstance
instance(spoolFolder
);
286 return instance
.GetPrinterDriver()->TakeJob(printJob
, settings
);
289 // main entry if printer driver is launched directly
291 int main(int argc
, char* argv
[])
293 PrinterDriverInstance instance
;
294 instance
.GetPrinterDriver()->About();