2 * Copyright 2008 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Julun, <host.haiku@gmx.de
11 #include <FindDirectory.h>
13 #include <NodeMonitor.h>
23 // TODO: remove, after pr_server.h cleanup
26 #define PSRV_PRINTER_MIMETYPE "application/x-vnd.Be.printer"
30 #define PSRV_PRINTER_ATTR_STATE "state"
31 #define PSRV_PRINTER_ATTR_COMMENTS "Comments"
32 #define PSRV_PRINTER_ATTR_TRANSPORT "transport"
33 #define PSRV_PRINTER_ATTR_DRIVER_NAME "Driver Name"
34 #define PSRV_PRINTER_ATTR_PRINTER_NAME "Printer Name"
35 #define PSRV_PRINTER_ATTR_DEFAULT_PRINTER "Default Printer"
36 #define PSRV_PRINTER_ATTR_TRANSPORT_ADDRESS "transport_address"
40 #define PSRV_FIELD_CURRENT_PRINTER "current_printer"
46 memset(&fPrinterEntryRef
, 0, sizeof(entry_ref
));
50 BPrinter::BPrinter(const BEntry
& entry
)
57 BPrinter::BPrinter(const BPrinter
& printer
)
63 BPrinter::BPrinter(const node_ref
& nodeRef
)
70 BPrinter::BPrinter(const entry_ref
& entryRef
)
72 , fPrinterEntryRef(entryRef
)
77 BPrinter::BPrinter(const BDirectory
& directory
)
91 BPrinter::SetTo(const BEntry
& entry
)
94 entry
.GetRef(&fPrinterEntryRef
);
101 BPrinter::SetTo(const node_ref
& nodeRef
)
103 SetTo(BDirectory(&nodeRef
));
109 BPrinter::SetTo(const entry_ref
& entryRef
)
112 fPrinterEntryRef
= entryRef
;
119 BPrinter::SetTo(const BDirectory
& directory
)
124 directory
.GetEntry(&entry
);
125 entry
.GetRef(&fPrinterEntryRef
);
135 memset(&fPrinterEntryRef
, 0, sizeof(entry_ref
));
140 BPrinter::IsValid() const
142 BDirectory
spoolDir(&fPrinterEntryRef
);
143 if (spoolDir
.InitCheck() != B_OK
)
146 BNode
node(spoolDir
);
147 char type
[B_MIME_TYPE_LENGTH
];
148 BNodeInfo(&node
).GetType(type
);
150 if (strcmp(type
, PSRV_PRINTER_MIMETYPE
) != 0)
158 BPrinter::InitCheck() const
160 BDirectory
spoolDir(&fPrinterEntryRef
);
161 return spoolDir
.InitCheck();
166 BPrinter::IsFree() const
168 return (State() == "free");
173 BPrinter::IsDefault() const
175 bool isDefault
= false;
177 BDirectory
spoolDir(&fPrinterEntryRef
);
178 if (spoolDir
.InitCheck() == B_OK
)
179 spoolDir
.ReadAttr(PSRV_PRINTER_ATTR_DEFAULT_PRINTER
, B_BOOL_TYPE
, 0,
180 &isDefault
, sizeof(bool));
187 BPrinter::IsShareable() const
189 if (Name() == "Preview")
197 BPrinter::Name() const
199 return _ReadAttribute(PSRV_PRINTER_ATTR_PRINTER_NAME
);
204 BPrinter::State() const
206 return _ReadAttribute(PSRV_PRINTER_ATTR_STATE
);
211 BPrinter::Driver() const
213 return _ReadAttribute(PSRV_PRINTER_ATTR_DRIVER_NAME
);
218 BPrinter::Comments() const
220 return _ReadAttribute(PSRV_PRINTER_ATTR_COMMENTS
);
225 BPrinter::Transport() const
227 return _ReadAttribute(PSRV_PRINTER_ATTR_TRANSPORT
);
232 BPrinter::TransportAddress() const
234 return _ReadAttribute(PSRV_PRINTER_ATTR_TRANSPORT_ADDRESS
);
239 BPrinter::DefaultSettings(BMessage
& settings
)
241 status_t status
= B_ERROR
;
242 image_id id
= _LoadDriver();
246 typedef BMessage
* (*default_settings_func_t
)(BNode
*);
247 default_settings_func_t default_settings
;
248 if (get_image_symbol(id
, "default_settings", B_SYMBOL_TYPE_TEXT
249 , (void**)&default_settings
) == B_OK
) {
250 BNode
printerNode(&fPrinterEntryRef
);
251 BMessage
*newSettings
= default_settings(&printerNode
);
254 settings
= *newSettings
;
255 _AddPrinterName(settings
);
265 BPrinter::StartWatching(const BMessenger
& listener
)
269 if (!listener
.IsValid())
272 fListener
= new(std::nothrow
) BMessenger(listener
);
277 nodeRef
.device
= fPrinterEntryRef
.device
;
278 nodeRef
.node
= fPrinterEntryRef
.directory
;
280 return watch_node(&nodeRef
, B_WATCH_DIRECTORY
, *fListener
);
285 BPrinter::StopWatching()
288 stop_watching(*fListener
);
296 BPrinter::operator=(const BPrinter
& printer
)
298 if (this != &printer
) {
300 fPrinterEntryRef
= printer
.fPrinterEntryRef
;
301 if (printer
.fListener
)
302 StartWatching(*printer
.fListener
);
309 BPrinter::operator==(const BPrinter
& printer
) const
311 return (fPrinterEntryRef
== printer
.fPrinterEntryRef
);
316 BPrinter::operator!=(const BPrinter
& printer
) const
318 return (fPrinterEntryRef
!= printer
.fPrinterEntryRef
);
323 BPrinter::_Configure() const
325 status_t status
= B_ERROR
;
326 image_id id
= _LoadDriver();
330 BString
printerName(_ReadAttribute(PSRV_PRINTER_ATTR_PRINTER_NAME
));
331 if (printerName
.Length() > 0) {
332 typedef char* (*add_printer_func_t
)(const char*);
333 add_printer_func_t add_printer
;
334 if (get_image_symbol(id
, "add_printer", B_SYMBOL_TYPE_TEXT
335 , (void**)&add_printer
) == B_OK
) {
336 if (add_printer(printerName
.String()) != NULL
)
348 BPrinter::_ConfigureJob(BMessage
& settings
)
350 status_t status
= B_ERROR
;
351 image_id id
= _LoadDriver();
355 typedef BMessage
* (*config_job_func_t
)(BNode
*, const BMessage
*);
356 config_job_func_t configure_job
;
357 if (get_image_symbol(id
, "config_job", B_SYMBOL_TYPE_TEXT
358 , (void**)&configure_job
) == B_OK
) {
359 BNode
printerNode(&fPrinterEntryRef
);
360 BMessage
*newSettings
= configure_job(&printerNode
, &settings
);
361 if (newSettings
&& (newSettings
->what
== 'okok')) {
363 settings
= *newSettings
;
364 _AddPrinterName(settings
);
374 BPrinter::_ConfigurePage(BMessage
& settings
)
376 status_t status
= B_ERROR
;
377 image_id id
= _LoadDriver();
381 typedef BMessage
* (*config_page_func_t
)(BNode
*, const BMessage
*);
382 config_page_func_t configure_page
;
383 if (get_image_symbol(id
, "config_page", B_SYMBOL_TYPE_TEXT
384 , (void**)&configure_page
) == B_OK
) {
385 BNode
printerNode(&fPrinterEntryRef
);
386 BMessage
*newSettings
= configure_page(&printerNode
, &settings
);
387 if (newSettings
&& (newSettings
->what
== 'okok')) {
389 settings
= *newSettings
;
390 _AddPrinterName(settings
);
400 BPrinter::_DriverPath() const
402 BString
driverName(_ReadAttribute(PSRV_PRINTER_ATTR_DRIVER_NAME
));
403 if (driverName
.Length() <= 0)
406 directory_which directories
[] = {
407 B_USER_NONPACKAGED_ADDONS_DIRECTORY
,
408 B_USER_ADDONS_DIRECTORY
,
409 B_SYSTEM_NONPACKAGED_ADDONS_DIRECTORY
,
410 B_SYSTEM_ADDONS_DIRECTORY
414 driverName
.Prepend("Print/");
415 for (int32 i
= 0; i
< sizeof(directories
) / sizeof(directories
[0]); ++i
) {
416 if (find_directory(directories
[i
], &path
) == B_OK
) {
417 path
.Append(driverName
.String());
419 BEntry
driver(path
.Path());
420 if (driver
.InitCheck() == B_OK
&& driver
.Exists() && driver
.IsFile())
429 BPrinter::_LoadDriver() const
431 BPath
driverPath(_DriverPath());
432 if (driverPath
.InitCheck() != B_OK
)
435 return load_add_on(driverPath
.Path());
440 BPrinter::_AddPrinterName(BMessage
& settings
)
442 settings
.RemoveName(PSRV_FIELD_CURRENT_PRINTER
);
443 settings
.AddString(PSRV_FIELD_CURRENT_PRINTER
, Name());
448 BPrinter::_ReadAttribute(const char* attribute
) const
452 BDirectory
spoolDir(&fPrinterEntryRef
);
453 if (spoolDir
.InitCheck() == B_OK
)
454 spoolDir
.ReadAttrString(attribute
, &value
);
461 } // namespace BPrivate