* cleanup, no functional change

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26307 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Karsten Heimrich
2008-07-07 19:39:10 +00:00
parent 30cf4e6a54
commit a836d47d56
+133 -100
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2001-2006, Haiku. All rights reserved. * Copyright 2001-2008, Haiku. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
@@ -29,7 +29,7 @@
SpoolFolder::SpoolFolder(BLocker*locker, BLooper* looper, const BDirectory& spoolDir) SpoolFolder::SpoolFolder(BLocker*locker, BLooper* looper, const BDirectory& spoolDir)
: Folder(locker, looper, spoolDir) : Folder(locker, looper, spoolDir)
{ {
} }
@@ -40,7 +40,7 @@ void SpoolFolder::Notify(Job* job, int kind)
if ((kind == kJobAdded || kind == kJobAttrChanged) && if ((kind == kJobAdded || kind == kJobAttrChanged) &&
job->IsValid() && job->IsWaiting()) { job->IsValid() && job->IsWaiting()) {
be_app_messenger.SendMessage(PSRV_PRINT_SPOOLED_JOB); be_app_messenger.SendMessage(PSRV_PRINT_SPOOLED_JOB);
} }
} }
@@ -74,11 +74,12 @@ Printer* Printer::Find(const BString& name)
return sPrinters.ItemAt(idx); return sPrinters.ItemAt(idx);
} }
} }
// None found, so return NULL // None found, so return NULL
return NULL; return NULL;
} }
Printer* Printer::Find(node_ref* node) Printer* Printer::Find(node_ref* node)
{ {
node_ref n; node_ref n;
@@ -86,19 +87,23 @@ Printer* Printer::Find(node_ref* node)
for (int32 idx=0; idx < sPrinters.CountItems(); idx++) { for (int32 idx=0; idx < sPrinters.CountItems(); idx++) {
Printer* printer = sPrinters.ItemAt(idx); Printer* printer = sPrinters.ItemAt(idx);
printer->SpoolDir()->GetNodeRef(&n); printer->SpoolDir()->GetNodeRef(&n);
if (n == *node) return printer; if (n == *node)
return printer;
} }
// None found, so return NULL // None found, so return NULL
return NULL; return NULL;
} }
Printer* Printer::At(int32 idx) Printer* Printer::At(int32 idx)
{ {
return sPrinters.ItemAt(idx); return sPrinters.ItemAt(idx);
} }
void Printer::Remove(Printer* printer) {
void Printer::Remove(Printer* printer)
{
sPrinters.RemoveItem(printer); sPrinters.RemoveItem(printer);
} }
@@ -107,6 +112,7 @@ int32 Printer::CountPrinters()
return sPrinters.CountItems(); return sPrinters.CountItems();
} }
// --------------------------------------------------------------- // ---------------------------------------------------------------
// Printer [constructor] // Printer [constructor]
// //
@@ -133,14 +139,16 @@ Printer::Printer(const BDirectory* node, Resource* res)
if (SpoolDir()->ReadAttrString(PSRV_PRINTER_ATTR_PRT_NAME, &name) == B_OK) if (SpoolDir()->ReadAttrString(PSRV_PRINTER_ATTR_PRT_NAME, &name) == B_OK)
SetName(name.String()); SetName(name.String());
if (name == "Preview") fSinglePrintThread = false; if (name == "Preview")
fSinglePrintThread = false;
// Add us to the global list of known printer definitions // Add us to the global list of known printer definitions
sPrinters.AddItem(this); sPrinters.AddItem(this);
ResetJobStatus(); ResetJobStatus();
} }
Printer::~Printer() Printer::~Printer()
{ {
((PrintServerApp*)be_app)->NotifyPrinterDeletion(this); ((PrintServerApp*)be_app)->NotifyPrinterDeletion(this);
@@ -157,10 +165,11 @@ status_t Printer::Remove()
path.Append(Name()); path.Append(Name());
rc = rmdir(path.Path()); rc = rmdir(path.Path());
} }
return rc; return rc;
} }
// --------------------------------------------------------------- // ---------------------------------------------------------------
// ConfigurePrinter // ConfigurePrinter
// //
@@ -174,24 +183,24 @@ status_t Printer::Remove()
// --------------------------------------------------------------- // ---------------------------------------------------------------
status_t Printer::ConfigurePrinter() status_t Printer::ConfigurePrinter()
{ {
image_id id;
status_t rc; status_t rc;
image_id id;
if ((rc=LoadPrinterAddon(id)) == B_OK) { if ((rc = LoadPrinterAddon(id)) == B_OK) {
// Addon was loaded, so try and get the add_printer symbol // Addon was loaded, so try and get the add_printer symbol
add_printer_func_t func; add_printer_func_t func;
if (get_image_symbol(id, "add_printer", B_SYMBOL_TYPE_TEXT,
if (get_image_symbol(id, "add_printer", B_SYMBOL_TYPE_TEXT, (void**)&func) == B_OK) { (void**)&func) == B_OK) {
// call the function and check its result // call the function and check its result
rc = ((*func)(Name()) == NULL) ? B_ERROR : B_OK; rc = ((*func)(Name()) == NULL) ? B_ERROR : B_OK;
} }
::unload_add_on(id); ::unload_add_on(id);
} }
return rc; return rc;
} }
// --------------------------------------------------------------- // ---------------------------------------------------------------
// ConfigurePage // ConfigurePage
// //
@@ -207,30 +216,31 @@ status_t Printer::ConfigurePrinter()
// --------------------------------------------------------------- // ---------------------------------------------------------------
status_t Printer::ConfigurePage(BMessage& settings) status_t Printer::ConfigurePage(BMessage& settings)
{ {
image_id id;
status_t rc; status_t rc;
image_id id;
if ((rc=LoadPrinterAddon(id)) == B_OK) { if ((rc = LoadPrinterAddon(id)) == B_OK) {
// Addon was loaded, so try and get the config_page symbol // Addon was loaded, so try and get the config_page symbol
config_func_t func; config_func_t func;
if ((rc=get_image_symbol(id, "config_page", B_SYMBOL_TYPE_TEXT,
if ((rc=get_image_symbol(id, "config_page", B_SYMBOL_TYPE_TEXT, (void**)&func)) == B_OK) { (void**)&func)) == B_OK) {
// call the function and check its result // call the function and check its result
BMessage* new_settings = (*func)(SpoolDir(), &settings); BMessage* new_settings = (*func)(SpoolDir(), &settings);
if (new_settings != NULL && new_settings->what != 'baad') { if (new_settings != NULL && new_settings->what != 'baad') {
settings = *new_settings; settings = *new_settings;
AddCurrentPrinter(&settings); AddCurrentPrinter(&settings);
} else } else {
rc = B_ERROR; rc = B_ERROR;
}
delete new_settings; delete new_settings;
} }
::unload_add_on(id); ::unload_add_on(id);
} }
return rc; return rc;
} }
// --------------------------------------------------------------- // ---------------------------------------------------------------
// ConfigureJob // ConfigureJob
// //
@@ -246,27 +256,27 @@ status_t Printer::ConfigurePage(BMessage& settings)
// --------------------------------------------------------------- // ---------------------------------------------------------------
status_t Printer::ConfigureJob(BMessage& settings) status_t Printer::ConfigureJob(BMessage& settings)
{ {
image_id id;
status_t rc; status_t rc;
image_id id;
if ((rc=LoadPrinterAddon(id)) == B_OK) { if ((rc = LoadPrinterAddon(id)) == B_OK) {
// Addon was loaded, so try and get the config_job symbol // Addon was loaded, so try and get the config_job symbol
config_func_t func; config_func_t func;
if ((rc = get_image_symbol(id, "config_job", B_SYMBOL_TYPE_TEXT,
if ((rc=get_image_symbol(id, "config_job", B_SYMBOL_TYPE_TEXT, (void**)&func)) == B_OK) { (void**)&func)) == B_OK) {
// call the function and check its result // call the function and check its result
BMessage* new_settings = (*func)(SpoolDir(), &settings); BMessage* new_settings = (*func)(SpoolDir(), &settings);
if ((new_settings != NULL) && (new_settings->what != 'baad')) { if ((new_settings != NULL) && (new_settings->what != 'baad')) {
settings = *new_settings; settings = *new_settings;
AddCurrentPrinter(&settings); AddCurrentPrinter(&settings);
} else } else {
rc = B_ERROR; rc = B_ERROR;
}
delete new_settings; delete new_settings;
} }
::unload_add_on(id); ::unload_add_on(id);
} }
return rc; return rc;
} }
@@ -276,13 +286,15 @@ status_t Printer::ConfigureJob(BMessage& settings)
// //
// Print spooled jobs in a new thread. // Print spooled jobs in a new thread.
// --------------------------------------------------------------- // ---------------------------------------------------------------
void Printer::HandleSpooledJob() { void Printer::HandleSpooledJob()
{
BAutolock lock(gLock); BAutolock lock(gLock);
if (lock.IsLocked() && (!fSinglePrintThread || fProcessing == 0) && FindSpooledJob()) { if (lock.IsLocked()
&& (!fSinglePrintThread || fProcessing == 0) && FindSpooledJob()) {
StartPrintThread(); StartPrintThread();
} }
} }
// --------------------------------------------------------------- // ---------------------------------------------------------------
// GetDefaultSettings // GetDefaultSettings
@@ -295,37 +307,38 @@ void Printer::HandleSpooledJob() {
// Returns: // Returns:
// B_OK if successful or errorcode otherwise. // B_OK if successful or errorcode otherwise.
// --------------------------------------------------------------- // ---------------------------------------------------------------
status_t Printer::GetDefaultSettings(BMessage& settings) { status_t Printer::GetDefaultSettings(BMessage& settings)
image_id id; {
status_t rc; status_t rc;
image_id id;
if ((rc=LoadPrinterAddon(id)) == B_OK) { if ((rc = LoadPrinterAddon(id)) == B_OK) {
// Addon was loaded, so try and get the default_settings symbol // Addon was loaded, so try and get the default_settings symbol
default_settings_t func; default_settings_t func;
if ((rc = get_image_symbol(id, "default_settings", B_SYMBOL_TYPE_TEXT,
if ((rc=get_image_symbol(id, "default_settings", B_SYMBOL_TYPE_TEXT, (void**)&func)) == B_OK) { (void**)&func)) == B_OK) {
// call the function and check its result // call the function and check its result
BMessage* new_settings = (*func)(SpoolDir()); BMessage* new_settings = (*func)(SpoolDir());
if (new_settings) { if (new_settings) {
settings = *new_settings; settings = *new_settings;
AddCurrentPrinter(&settings); AddCurrentPrinter(&settings);
} else { } else {
rc = B_ERROR; rc = B_ERROR;
} }
delete new_settings; delete new_settings;
} }
::unload_add_on(id); ::unload_add_on(id);
} }
return rc; return rc;
} }
void Printer::AbortPrintThread() { void Printer::AbortPrintThread()
{
fAbort = true; fAbort = true;
} }
// --------------------------------------------------------------- // ---------------------------------------------------------------
// LoadPrinterAddon // LoadPrinterAddon
// //
@@ -339,25 +352,27 @@ void Printer::AbortPrintThread() {
// --------------------------------------------------------------- // ---------------------------------------------------------------
status_t Printer::LoadPrinterAddon(image_id& id) status_t Printer::LoadPrinterAddon(image_id& id)
{ {
BString drName;
status_t rc; status_t rc;
BPath path; BString drName;
if ((rc = SpoolDir()->ReadAttrString(PSRV_PRINTER_ATTR_DRV_NAME, &drName)) == B_OK) {
if ((rc=SpoolDir()->ReadAttrString(PSRV_PRINTER_ATTR_DRV_NAME, &drName)) == B_OK) { // try to locate the driver
// try to locate the driver BPath path;
if ((rc=::TestForAddonExistence(drName.String(), B_USER_ADDONS_DIRECTORY, "Print", path)) != B_OK) { if ((rc= ::TestForAddonExistence(drName.String(), B_USER_ADDONS_DIRECTORY,
if ((rc=::TestForAddonExistence(drName.String(), B_COMMON_ADDONS_DIRECTORY, "Print", path)) != B_OK) { "Print", path)) != B_OK) {
rc = ::TestForAddonExistence(drName.String(), B_BEOS_ADDONS_DIRECTORY, "Print", path); if ((rc = ::TestForAddonExistence(drName.String(), B_COMMON_ADDONS_DIRECTORY,
"Print", path)) != B_OK) {
rc = ::TestForAddonExistence(drName.String(), B_BEOS_ADDONS_DIRECTORY,
"Print", path);
} }
} }
// If the driver was found // If the driver was found
if (rc == B_OK) { if (rc == B_OK) {
// If we cannot load the addon // If we cannot load the addon
if ((id=::load_add_on(path.Path())) < 0) if ((id=::load_add_on(path.Path())) < 0)
rc = id; rc = id;
} }
} }
return rc; return rc;
} }
@@ -401,12 +416,13 @@ void Printer::MessageReceived(BMessage* msg)
case B_EXECUTE_PROPERTY: case B_EXECUTE_PROPERTY:
HandleScriptingCommand(msg); HandleScriptingCommand(msg);
break; break;
default: default:
Inherited::MessageReceived(msg); Inherited::MessageReceived(msg);
} }
} }
// --------------------------------------------------------------- // ---------------------------------------------------------------
// GetName // GetName
// //
@@ -415,27 +431,32 @@ void Printer::MessageReceived(BMessage* msg)
// Parameters: // Parameters:
// name - the name of the printer. // name - the name of the printer.
// --------------------------------------------------------------- // ---------------------------------------------------------------
void Printer::GetName(BString& name) { void Printer::GetName(BString& name)
{
if (B_OK != SpoolDir()->ReadAttrString(PSRV_PRINTER_ATTR_PRT_NAME, &name)) if (B_OK != SpoolDir()->ReadAttrString(PSRV_PRINTER_ATTR_PRT_NAME, &name))
name = "Unknown Printer"; name = "Unknown Printer";
} }
// --------------------------------------------------------------- // ---------------------------------------------------------------
// ResetJobStatus // ResetJobStatus
// //
// Reset status of "processing" jobs to "waiting" at print_server start. // Reset status of "processing" jobs to "waiting" at print_server start.
// --------------------------------------------------------------- // ---------------------------------------------------------------
void Printer::ResetJobStatus() { void Printer::ResetJobStatus()
{
if (fPrinter.Lock()) { if (fPrinter.Lock()) {
const int32 n = fPrinter.CountJobs(); const int32 n = fPrinter.CountJobs();
for (int32 i = 0; i < n; i ++) { for (int32 i = 0; i < n; i ++) {
Job* job = fPrinter.JobAt(i); Job* job = fPrinter.JobAt(i);
if (job->Status() == kProcessing) job->SetStatus(kWaiting); if (job->Status() == kProcessing)
job->SetStatus(kWaiting);
} }
fPrinter.Unlock(); fPrinter.Unlock();
} }
} }
// --------------------------------------------------------------- // ---------------------------------------------------------------
// HasCurrentPrinter // HasCurrentPrinter
// //
@@ -447,16 +468,18 @@ void Printer::ResetJobStatus() {
// Returns: // Returns:
// true if successful. // true if successful.
// --------------------------------------------------------------- // ---------------------------------------------------------------
bool Printer::HasCurrentPrinter(BString& name) { bool Printer::HasCurrentPrinter(BString& name)
{
BMessage settings; BMessage settings;
// read settings from spool file and get printer name // read settings from spool file and get printer name
BFile jobFile(&fJob->EntryRef(), B_READ_WRITE); BFile jobFile(&fJob->EntryRef(), B_READ_WRITE);
return jobFile.InitCheck() == B_OK && return jobFile.InitCheck() == B_OK
jobFile.Seek(sizeof(print_file_header), SEEK_SET) == sizeof(print_file_header) && && jobFile.Seek(sizeof(print_file_header), SEEK_SET) == sizeof(print_file_header)
settings.Unflatten(&jobFile) == B_OK && && settings.Unflatten(&jobFile) == B_OK
settings.FindString(PSRV_FIELD_CURRENT_PRINTER, &name) == B_OK; && settings.FindString(PSRV_FIELD_CURRENT_PRINTER, &name) == B_OK;
} }
// --------------------------------------------------------------- // ---------------------------------------------------------------
// MoveJob // MoveJob
// //
@@ -468,32 +491,36 @@ bool Printer::HasCurrentPrinter(BString& name) {
// Returns: // Returns:
// true if successful. // true if successful.
// --------------------------------------------------------------- // ---------------------------------------------------------------
bool Printer::MoveJob(const BString& name) { bool Printer::MoveJob(const BString& name)
{
BPath file(&fJob->EntryRef()); BPath file(&fJob->EntryRef());
BPath path; BPath path;
file.GetParent(&path); file.GetParent(&path);
path.Append(".."); path.Append(name.String()); path.Append("..");
path.Append(name.String());
BDirectory dir(path.Path()); BDirectory dir(path.Path());
BEntry entry(&fJob->EntryRef()); BEntry entry(&fJob->EntryRef());
// try to move job file to proper directory // try to move job file to proper directory
return entry.MoveTo(&dir) == B_OK; return entry.MoveTo(&dir) == B_OK;
} }
// --------------------------------------------------------------- // ---------------------------------------------------------------
// FindSpooledJob // FindSpooledJob
// //
// Looks if there is a job waiting to be processed and moves // Looks if there is a job waiting to be processed and moves
// jobs to the proper printer folder. // jobs to the proper printer folder.
// //
// Note: // Note:
// Our implementation of BPrintJob moves jobs to the // Our implementation of BPrintJob moves jobs to the
// proper printer folder. // proper printer folder.
// //
// //
// Returns: // Returns:
// true if there is a job present in fJob. // true if there is a job present in fJob.
// --------------------------------------------------------------- // ---------------------------------------------------------------
bool Printer::FindSpooledJob() { bool Printer::FindSpooledJob()
{
BString name2; BString name2;
GetName(name2); GetName(name2);
do { do {
@@ -514,6 +541,7 @@ bool Printer::FindSpooledJob() {
return false; return false;
} }
// --------------------------------------------------------------- // ---------------------------------------------------------------
// PrintSpooledJob // PrintSpooledJob
// //
@@ -528,30 +556,28 @@ bool Printer::FindSpooledJob() {
// --------------------------------------------------------------- // ---------------------------------------------------------------
status_t Printer::PrintSpooledJob(BFile* spoolFile) status_t Printer::PrintSpooledJob(BFile* spoolFile)
{ {
take_job_func_t func;
image_id id;
status_t rc; status_t rc;
image_id id;
if ((rc=LoadPrinterAddon(id)) == B_OK) { if ((rc = LoadPrinterAddon(id)) == B_OK) {
// Addon was loaded, so try and get the take_job symbol take_job_func_t func;
if ((rc=get_image_symbol(id, "take_job", B_SYMBOL_TYPE_TEXT, (void**)&func)) == B_OK) { // Addon was loaded, so try and get the take_job symbol
// This seems to be required for legacy? if ((rc = get_image_symbol(id, "take_job", B_SYMBOL_TYPE_TEXT,
// HP PCL3 add-on crashes without it! (void**)&func)) == B_OK) {
// This seems to be required for legacy?
// HP PCL3 add-on crashes without it!
BMessage params(B_REFS_RECEIVED); BMessage params(B_REFS_RECEIVED);
params.AddInt32("file", (int32)spoolFile); params.AddInt32("file", (int32)spoolFile);
params.AddInt32("printer", (int32)SpoolDir()); params.AddInt32("printer", (int32)SpoolDir());
// call the function and check its result // call the function and check its result
BMessage* result = (*func)(spoolFile, SpoolDir(), &params); BMessage* result = (*func)(spoolFile, SpoolDir(), &params);
if (result == NULL || result->what != 'okok') if (result == NULL || result->what != 'okok')
rc = B_ERROR; rc = B_ERROR;
delete result; delete result;
} }
::unload_add_on(id); ::unload_add_on(id);
} }
return rc; return rc;
} }
@@ -565,12 +591,13 @@ status_t Printer::PrintSpooledJob(BFile* spoolFile)
// Parameters: // Parameters:
// job - the spool job. // job - the spool job.
// --------------------------------------------------------------- // ---------------------------------------------------------------
void Printer::PrintThread(Job* job) { void Printer::PrintThread(Job* job)
// Wait until resource is available {
// Wait until resource is available
fResource->Lock(); fResource->Lock();
bool failed = true; bool failed = true;
// Can we continue? // Can we continue?
if (!fAbort) { if (!fAbort) {
BFile jobFile(&job->EntryRef(), B_READ_WRITE); BFile jobFile(&job->EntryRef(), B_READ_WRITE);
// Tell the printer to print the spooled job // Tell the printer to print the spooled job
if (jobFile.InitCheck() == B_OK && PrintSpooledJob(&jobFile) == B_OK) { if (jobFile.InitCheck() == B_OK && PrintSpooledJob(&jobFile) == B_OK) {
@@ -579,15 +606,17 @@ void Printer::PrintThread(Job* job) {
} }
} }
// Set status of spooled job on error // Set status of spooled job on error
if (failed) job->SetStatus(kFailed); if (failed)
job->SetStatus(kFailed);
fResource->Unlock(); fResource->Unlock();
job->Release(); job->Release();
atomic_add(&fProcessing, -1); atomic_add(&fProcessing, -1);
Release(); Release();
// Notify print_server to process next spooled job // Notify print_server to process next spooled job
be_app_messenger.SendMessage(PSRV_PRINT_SPOOLED_JOB); be_app_messenger.SendMessage(PSRV_PRINT_SPOOLED_JOB);
} }
// --------------------------------------------------------------- // ---------------------------------------------------------------
// print_thread // print_thread
// //
@@ -599,27 +628,31 @@ void Printer::PrintThread(Job* job) {
// Returns: // Returns:
// 0 always. // 0 always.
// --------------------------------------------------------------- // ---------------------------------------------------------------
status_t Printer::print_thread(void* data) { status_t Printer::print_thread(void* data)
{
Job* job = static_cast<Job*>(data); Job* job = static_cast<Job*>(data);
job->GetPrinter()->PrintThread(job); job->GetPrinter()->PrintThread(job);
return 0; return 0;
} }
// --------------------------------------------------------------- // ---------------------------------------------------------------
// StartPrintThread // StartPrintThread
// //
// Sets the status of the current spool job to "processing" and // Sets the status of the current spool job to "processing" and
// starts the print_thread. // starts the print_thread.
// --------------------------------------------------------------- // ---------------------------------------------------------------
void Printer::StartPrintThread() { void Printer::StartPrintThread()
{
Acquire(); Acquire();
thread_id tid = spawn_thread(print_thread, "print", B_NORMAL_PRIORITY, (void*)fJob); thread_id tid = spawn_thread(print_thread, "print", B_NORMAL_PRIORITY, (void*)fJob);
if (tid > 0) { if (tid > 0) {
fJob->SetStatus(kProcessing); fJob->SetStatus(kProcessing);
atomic_add(&fProcessing, 1); atomic_add(&fProcessing, 1);
resume_thread(tid); resume_thread(tid);
} else { } else {
fJob->Release(); Release(); fJob->Release();
Release();
} }
} }