print_server: minor coding style cleanup.

This commit is contained in:
Axel Dörfler
2015-10-13 16:35:47 +02:00
parent aaac25441d
commit 6c3e186ceb
2 changed files with 181 additions and 231 deletions
+119 -180
View File
@@ -1,19 +1,18 @@
/* /*
* Copyright 2001-2010, Haiku, Inc. All rights reserved. * Copyright 2001-2015, Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
* Ithamar R. Adema * Ithamar R. Adema
* Michael Pfeiffer * Michael Pfeiffer
*/ */
#include "PrintServerApp.h" #include "PrintServerApp.h"
#include "BeUtils.h" #include <stdio.h>
#include "Printer.h" #include <unistd.h>
#include "pr_server.h"
#include "Transport.h"
// BeOS API
#include <Alert.h> #include <Alert.h>
#include <Autolock.h> #include <Autolock.h>
#include <Catalog.h> #include <Catalog.h>
@@ -30,11 +29,10 @@
#include <PrintJob.h> #include <PrintJob.h>
#include <String.h> #include <String.h>
// ANSI C #include "BeUtils.h"
#include <stdio.h> #include "Printer.h"
// for printf #include "pr_server.h"
#include <unistd.h> #include "Transport.h"
// for unlink
#undef B_TRANSLATION_CONTEXT #undef B_TRANSLATION_CONTEXT
@@ -46,45 +44,42 @@ typedef struct _printer_data {
} printer_data_t; } printer_data_t;
static const char* kSettingsName = "print_server_settings"; static const char* kSettingsName = "print_server_settings";
BLocker *gLock = NULL; BLocker *gLock = NULL;
/** /*! Main entry point of print_server.
* Main entry point of print_server.
* @returns B_OK if application was started, or an errorcode if
* @returns B_OK if application was started, or an errorcode if the application failed to start.
* application failed to start. */
*/
int int
main() main()
{ {
status_t rc = B_OK;
gLock = new BLocker(); gLock = new BLocker();
PrintServerApp print_server(&rc);
if (rc == B_OK) { status_t status = B_OK;
print_server.Run(); PrintServerApp printServer(&status);
} if (status == B_OK)
printServer.Run();
delete gLock; delete gLock;
return rc; return status;
} }
/** /*! Constructor for print_server's application class. Retrieves the
* Constructor for print_server's application class. Retrieves the name of the default printer from storage, caches the icons for
* name of the default printer from storage, caches the icons for a selected printer.
* a selected printer.
* @param err Pointer to status_t for storing result of application
* @param err Pointer to status_t for storing result of application initialisation.
* initialisation. @see BApplication
* */
* @see BApplication
*/
PrintServerApp::PrintServerApp(status_t* err) PrintServerApp::PrintServerApp(status_t* err)
: Inherited(PSRV_SIGNATURE_TYPE, err), :
Inherited(PSRV_SIGNATURE_TYPE, err),
fDefaultPrinter(NULL), fDefaultPrinter(NULL),
#ifdef HAIKU_TARGET_PLATFORM_HAIKU #ifdef HAIKU_TARGET_PLATFORM_HAIKU
fIconSize(0), fIconSize(0),
@@ -209,7 +204,6 @@ PrintServerApp::RegisterPrinter(BDirectory* printer)
&& printer->ReadAttrString(PSRV_PRINTER_ATTR_CNX, &connection) == B_OK && printer->ReadAttrString(PSRV_PRINTER_ATTR_CNX, &connection) == B_OK
&& printer->ReadAttrString(PSRV_PRINTER_ATTR_STATE, &state) == B_OK && printer->ReadAttrString(PSRV_PRINTER_ATTR_STATE, &state) == B_OK
&& state == "free") { && state == "free") {
BAutolock lock(gLock); BAutolock lock(gLock);
if (lock.IsLocked()) { if (lock.IsLocked()) {
// check if printer is already registered // check if printer is already registered
@@ -221,9 +215,9 @@ PrintServerApp::RegisterPrinter(BDirectory* printer)
return; return;
// register new printer // register new printer
Resource* r = fResourceManager.Allocate(transport.String(), Resource* resource = fResourceManager.Allocate(transport.String(),
address.String(), connection.String()); address.String(), connection.String());
AddHandler(new Printer(printer, r)); AddHandler(new Printer(printer, resource));
Acquire(); Acquire();
} }
} }
@@ -266,7 +260,8 @@ PrintServerApp::EntryRemoved(node_ref* node)
{ {
Printer* printer = Printer::Find(node); Printer* printer = Printer::Find(node);
if (printer) { if (printer) {
if (printer == fDefaultPrinter) fDefaultPrinter = NULL; if (printer == fDefaultPrinter)
fDefaultPrinter = NULL;
UnregisterPrinter(printer); UnregisterPrinter(printer);
} }
} }
@@ -276,44 +271,34 @@ void
PrintServerApp::AttributeChanged(node_ref* node) PrintServerApp::AttributeChanged(node_ref* node)
{ {
BDirectory printer(node); BDirectory printer(node);
if (printer.InitCheck() == B_OK) { if (printer.InitCheck() == B_OK)
RegisterPrinter(&printer); RegisterPrinter(&printer);
}
} }
// --------------------------------------------------------------- /*! This method builds the internal list of printers from disk. It
// SetupPrinterList also installs a node monitor to be sure that the list keeps
// updated with the definitions on disk.
// This method builds the internal list of printers from disk. It
// also installs a node monitor to be sure that the list keeps @return B_OK if successful, or an errorcode if failed.
// updated with the definitions on disk. */
//
// Parameters:
// none.
//
// Returns:
// B_OK if successful, or an errorcode if failed.
// ---------------------------------------------------------------
status_t status_t
PrintServerApp::SetupPrinterList() PrintServerApp::SetupPrinterList()
{ {
status_t rc;
// Find directory containing printer definition nodes // Find directory containing printer definition nodes
BPath path; BPath path;
rc = ::find_directory(B_USER_PRINTERS_DIRECTORY, &path); status_t status = find_directory(B_USER_PRINTERS_DIRECTORY, &path);
if (rc != B_OK) if (status != B_OK)
return rc; return status;
// Directory has to exist in order to watch it // Directory has to exist in order to watch it
mode_t mode = 0777; mode_t mode = 0777;
create_directory(path.Path(), mode); create_directory(path.Path(), mode);
BDirectory dir(path.Path()); BDirectory dir(path.Path());
rc = dir.InitCheck(); status = dir.InitCheck();
if (rc != B_OK) if (status != B_OK)
return rc; return status;
// Register printer definition nodes // Register printer definition nodes
BEntry entry; BEntry entry;
@@ -337,17 +322,11 @@ PrintServerApp::SetupPrinterList()
return B_OK; return B_OK;
} }
// ---------------------------------------------------------------
// void MessageReceived(BMessage* msg) /*! Message handling method for print_server application class.
//
// Message handling method for print_server application class. @param msg Actual message sent to application class.
// */
// Parameters:
// msg - Actual message sent to application class.
//
// Returns:
// void.
// ---------------------------------------------------------------
void void
PrintServerApp::MessageReceived(BMessage* msg) PrintServerApp::MessageReceived(BMessage* msg)
{ {
@@ -378,44 +357,35 @@ PrintServerApp::MessageReceived(BMessage* msg)
} }
// --------------------------------------------------------------- /*! Creates printer definition/spool directory. It sets the
// CreatePrinter(const char* printerName, const char* driverName, attributes of the directory to the values passed and calls
// const char* connection, const char* transportName, the driver's add_printer method to handle any configuration
// const char* transportPath) needed.
//
// Creates printer definition/spool directory. It sets the @param printerName Name of printer to create.
// attributes of the directory to the values passed and calls @param driverName Name of driver to use for this printer.
// the driver's add_printer method to handle any configuration @param connection "Local" or "Network".
// needed. @param transportName Name of transport driver to use.
// @param transportPath Configuration data for transport driver.
// Parameters: */
// printerName - Name of printer to create.
// driverName - Name of driver to use for this printer.
// connection - "Local" or "Network".
// transportName - Name of transport driver to use.
// transportPath - Configuration data for transport driver.
//
// Returns:
// ---------------------------------------------------------------
status_t status_t
PrintServerApp::CreatePrinter(const char* printerName, const char* driverName, PrintServerApp::CreatePrinter(const char* printerName, const char* driverName,
const char* connection, const char* transportName, const char* connection, const char* transportName,
const char* transportPath) const char* transportPath)
{ {
status_t rc;
// Find directory containing printer definitions // Find directory containing printer definitions
BPath path; BPath path;
rc = ::find_directory(B_USER_PRINTERS_DIRECTORY,&path,true,NULL); status_t status = find_directory(B_USER_PRINTERS_DIRECTORY, &path, true,
if (rc != B_OK) NULL);
return rc; if (status != B_OK)
return status;
// Create our printer definition/spool directory // Create our printer definition/spool directory
BDirectory printersDir(path.Path()); BDirectory printersDir(path.Path());
BDirectory printer; BDirectory printer;
rc = printersDir.CreateDirectory(printerName, &printer); status = printersDir.CreateDirectory(printerName, &printer);
if (rc == B_FILE_EXISTS) { if (status == B_FILE_EXISTS) {
printer.SetTo(&printersDir, printerName); printer.SetTo(&printersDir, printerName);
BString info; BString info;
@@ -427,8 +397,8 @@ PrintServerApp::CreatePrinter(const char* printerName, const char* driverName,
if (fDefaultPrinter) { if (fDefaultPrinter) {
// the printer exists, but is not the default printer // the printer exists, but is not the default printer
if (strcmp(fDefaultPrinter->Name(), printerName) != 0) if (strcmp(fDefaultPrinter->Name(), printerName) != 0)
rc = B_OK; status = B_OK;
return rc; return status;
} }
// the printer exists, but no default at all // the printer exists, but no default at all
return B_OK; return B_OK;
@@ -448,10 +418,10 @@ PrintServerApp::CreatePrinter(const char* printerName, const char* driverName,
B_TRANSLATE("Cancel"), B_TRANSLATE("OK")); B_TRANSLATE("Cancel"), B_TRANSLATE("OK"));
alert->SetShortcut(0, B_ESCAPE); alert->SetShortcut(0, B_ESCAPE);
if (alert->Go() == 0) if (alert->Go() == 0)
return rc; return status;
} }
} else if (rc != B_OK) { } else if (status != B_OK) {
return rc; return status;
} }
// Set its type to a printer // Set its type to a printer
@@ -470,46 +440,38 @@ PrintServerApp::CreatePrinter(const char* printerName, const char* driverName,
printer.WriteAttr(PSRV_PRINTER_ATTR_CNX, B_STRING_TYPE, 0, connection, printer.WriteAttr(PSRV_PRINTER_ATTR_CNX, B_STRING_TYPE, 0, connection,
::strlen(connection) + 1); ::strlen(connection) + 1);
rc = Printer::ConfigurePrinter(driverName, printerName); status = Printer::ConfigurePrinter(driverName, printerName);
if (rc == B_OK) { if (status == B_OK) {
// Notify printer driver that a new printer definition node // Notify printer driver that a new printer definition node
// has been created. // has been created.
printer.WriteAttr(PSRV_PRINTER_ATTR_STATE, B_STRING_TYPE, 0, "free", printer.WriteAttr(PSRV_PRINTER_ATTR_STATE, B_STRING_TYPE, 0, "free",
::strlen("free")+1); ::strlen("free")+1);
} }
if (rc != B_OK) { if (status != B_OK) {
BEntry entry; BEntry entry;
if (printer.GetEntry(&entry) == B_OK) if (printer.GetEntry(&entry) == B_OK)
entry.Remove(); entry.Remove();
} }
return rc; return status;
} }
// --------------------------------------------------------------- /*! Makes a new printer the active printer. This is done simply
// SelectPrinter(const char* printerName) by changing our class attribute fDefaultPrinter, and changing
// the icon of the BNode for the printer. Ofcourse, we need to
// Makes a new printer the active printer. This is done simply change the icon of the "old" default printer first back to a
// by changing our class attribute fDefaultPrinter, and changing "non-active" printer icon first.
// the icon of the BNode for the printer. Ofcourse, we need to
// change the icon of the "old" default printer first back to a @param printerName Name of the new active printer.
// "non-active" printer icon first. @return B_OK on success, or error code otherwise.
// */
// Parameters:
// printerName - Name of the new active printer.
//
// Returns:
// B_OK on success, or error code otherwise.
// ---------------------------------------------------------------
status_t status_t
PrintServerApp::SelectPrinter(const char* printerName) PrintServerApp::SelectPrinter(const char* printerName)
{ {
status_t rc;
BNode node;
// Find the node of the "old" default printer // Find the node of the "old" default printer
BNode node;
if (fDefaultPrinter != NULL if (fDefaultPrinter != NULL
&& FindPrinterNode(fDefaultPrinter->Name(), node) == B_OK) { && FindPrinterNode(fDefaultPrinter->Name(), node) == B_OK) {
// and remove the custom icon // and remove the custom icon
@@ -519,8 +481,8 @@ PrintServerApp::SelectPrinter(const char* printerName)
} }
// Find the node for the new default printer // Find the node for the new default printer
rc=FindPrinterNode(printerName, node); status_t status = FindPrinterNode(printerName, node);
if (rc == B_OK) { if (status == B_OK) {
// and add the custom icon // and add the custom icon
BNodeInfo info(&node); BNodeInfo info(&node);
#ifdef HAIKU_TARGET_PLATFORM_HAIKU #ifdef HAIKU_TARGET_PLATFORM_HAIKU
@@ -536,16 +498,11 @@ PrintServerApp::SelectPrinter(const char* printerName)
// update our pref file // update our pref file
be_roster->Broadcast(new BMessage(B_PRINTER_CHANGED)); be_roster->Broadcast(new BMessage(B_PRINTER_CHANGED));
return rc; return status;
} }
// --------------------------------------------------------------- //! Handles calling the printer drivers for printing a spooled job.
// HandleSpooledJobs()
//
// Handles calling the printer drivers for printing a spooled job.
//
// ---------------------------------------------------------------
void void
PrintServerApp::HandleSpooledJobs() PrintServerApp::HandleSpooledJobs()
{ {
@@ -557,18 +514,11 @@ PrintServerApp::HandleSpooledJobs()
} }
// --------------------------------------------------------------- /*! Loads the currently selected printer from a private settings
// RetrieveDefaultPrinter() file.
//
// Loads the currently selected printer from a private settings @return Error code on failore, or B_OK if all went fine.
// file. */
//
// Parameters:
// none.
//
// Returns:
// Error code on failore, or B_OK if all went fine.
// ---------------------------------------------------------------
status_t status_t
PrintServerApp::RetrieveDefaultPrinter() PrintServerApp::RetrieveDefaultPrinter()
{ {
@@ -577,18 +527,11 @@ PrintServerApp::RetrieveDefaultPrinter()
} }
// --------------------------------------------------------------- /*! Stores the currently selected printer in a private settings
// StoreDefaultPrinter() file.
//
// Stores the currently selected printer in a private settings @return Error code on failore, or B_OK if all went fine.
// file. */
//
// Parameters:
// none.
//
// Returns:
// Error code on failore, or B_OK if all went fine.
// ---------------------------------------------------------------
status_t status_t
PrintServerApp::StoreDefaultPrinter() PrintServerApp::StoreDefaultPrinter()
{ {
@@ -600,27 +543,22 @@ PrintServerApp::StoreDefaultPrinter()
} }
// --------------------------------------------------------------- /*! Find the BNode representing the specified printer. It searches
// FindPrinterNode(const char* name, BNode& node) *only* in the users printer definitions.
//
// Find the BNode representing the specified printer. It searches @param name Name of the printer to look for.
// *only* in the users printer definitions. @param node BNode to set to the printer definition node.
// @return B_OK if found, an error code otherwise.
// Parameters: */
// name - Name of the printer to look for.
// node - BNode to set to the printer definition node.
//
// Returns:
// B_OK if found, an error code otherwise.
// ---------------------------------------------------------------
status_t status_t
PrintServerApp::FindPrinterNode(const char* name, BNode& node) PrintServerApp::FindPrinterNode(const char* name, BNode& node)
{ {
// Find directory containing printer definitions // Find directory containing printer definitions
BPath path; BPath path;
status_t rc = ::find_directory(B_USER_PRINTERS_DIRECTORY, &path, true, NULL); status_t status = find_directory(B_USER_PRINTERS_DIRECTORY, &path, true,
if (rc != B_OK) NULL);
return rc; if (status != B_OK)
return status;
path.Append(name); path.Append(name);
return node.SetTo(path.Path()); return node.SetTo(path.Path());
@@ -628,8 +566,7 @@ PrintServerApp::FindPrinterNode(const char* name, BNode& node)
bool bool
PrintServerApp::OpenSettings(BFile& file, const char* name, PrintServerApp::OpenSettings(BFile& file, const char* name, bool forReading)
bool forReading)
{ {
BPath path; BPath path;
uint32 openMode = forReading ? B_READ_ONLY : B_CREATE_FILE | B_ERASE_FILE uint32 openMode = forReading ? B_READ_ONLY : B_CREATE_FILE | B_ERASE_FILE
@@ -641,7 +578,8 @@ PrintServerApp::OpenSettings(BFile& file, const char* name,
void void
PrintServerApp::LoadSettings() { PrintServerApp::LoadSettings()
{
BFile file; BFile file;
if (OpenSettings(file, kSettingsName, true)) { if (OpenSettings(file, kSettingsName, true)) {
fSettings->Load(&file); fSettings->Load(&file);
@@ -651,7 +589,8 @@ PrintServerApp::LoadSettings() {
void void
PrintServerApp::SaveSettings() { PrintServerApp::SaveSettings()
{
BFile file; BFile file;
if (OpenSettings(file, kSettingsName, false)) { if (OpenSettings(file, kSettingsName, false)) {
fSettings->SetUseConfigWindow(fUseConfigWindow); fSettings->SetUseConfigWindow(fUseConfigWindow);
+62 -51
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2001-2010, Haiku, Inc. All rights reserved. * Copyright 2001-2015, Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
@@ -9,6 +9,7 @@
#ifndef _PRINT_SERVER_APP_H #ifndef _PRINT_SERVER_APP_H
#define _PRINT_SERVER_APP_H #define _PRINT_SERVER_APP_H
#include <Application.h> #include <Application.h>
#include <Bitmap.h> #include <Bitmap.h>
#include <Catalog.h> #include <Catalog.h>
@@ -19,81 +20,91 @@
#include "ResourceManager.h" #include "ResourceManager.h"
#include "Settings.h" #include "Settings.h"
class Printer; class Printer;
class Transport; class Transport;
// The global BLocker for synchronisation. // The global BLocker for synchronisation.
extern BLocker *gLock; extern BLocker *gLock;
// The print_server application. // The print_server application.
class PrintServerApp : public BApplication, public FolderListener { class PrintServerApp : public BApplication, public FolderListener {
private: private:
typedef BApplication Inherited; typedef BApplication Inherited;
public: public:
PrintServerApp(status_t *err); PrintServerApp(status_t* error);
~PrintServerApp(); ~PrintServerApp();
void Acquire(); void Acquire();
void Release(); void Release();
bool QuitRequested(); virtual bool QuitRequested();
void MessageReceived(BMessage *msg); virtual void MessageReceived(BMessage* msg);
void NotifyPrinterDeletion(Printer *printer); void NotifyPrinterDeletion(Printer* printer);
// Scripting support, see PrintServerApp.Scripting.cpp // Scripting support, see PrintServerApp.Scripting.cpp
status_t GetSupportedSuites(BMessage *msg); virtual status_t GetSupportedSuites(BMessage* msg);
void HandleScriptingCommand(BMessage *msg); void HandleScriptingCommand(BMessage* msg);
Printer *GetPrinterFromSpecifier(BMessage *msg); Printer* GetPrinterFromSpecifier(BMessage* msg);
Transport *GetTransportFromSpecifier(BMessage *msg); Transport* GetTransportFromSpecifier(BMessage* msg);
BHandler *ResolveSpecifier(BMessage *msg, int32 index, BMessage *spec, virtual BHandler* ResolveSpecifier(BMessage* msg, int32 index,
int32 form, const char *prop); BMessage* specifier, int32 form,
private: const char* property);
bool OpenSettings(BFile &file, const char *name, bool forReading);
void LoadSettings();
void SaveSettings();
status_t SetupPrinterList(); private:
bool OpenSettings(BFile& file, const char* name,
bool forReading);
void LoadSettings();
void SaveSettings();
void HandleSpooledJobs(); status_t SetupPrinterList();
status_t SelectPrinter(const char *printerName); void HandleSpooledJobs();
status_t CreatePrinter(const char *printerName, const char *driverName,
const char *connection, const char *transportName,
const char *transportPath);
void RegisterPrinter(BDirectory *node); status_t SelectPrinter(const char* printerName);
void UnregisterPrinter(Printer *printer); status_t CreatePrinter(const char* printerName,
const char* driverName,
const char* connection,
const char* transportName,
const char* transportPath);
// FolderListener void RegisterPrinter(BDirectory* node);
void EntryCreated(node_ref *node, entry_ref *entry); void UnregisterPrinter(Printer* printer);
void EntryRemoved(node_ref *node);
void AttributeChanged(node_ref *node);
status_t StoreDefaultPrinter(); // FolderListener
status_t RetrieveDefaultPrinter(); void EntryCreated(node_ref* node, entry_ref* entry);
void EntryRemoved(node_ref* node);
void AttributeChanged(node_ref* node);
status_t FindPrinterNode(const char *name, BNode &node); status_t StoreDefaultPrinter();
status_t RetrieveDefaultPrinter();
status_t FindPrinterNode(const char* name, BNode& node);
// "Classic" BeOS R5 support, see PrintServerApp.R5.cpp // "Classic" BeOS R5 support, see PrintServerApp.R5.cpp
static status_t async_thread(void *data); static status_t async_thread(void* data);
void AsyncHandleMessage(BMessage *msg); void AsyncHandleMessage(BMessage* msg);
void Handle_BeOSR5_Message(BMessage *msg); void Handle_BeOSR5_Message(BMessage* msg);
ResourceManager fResourceManager; private:
Printer *fDefaultPrinter; ResourceManager fResourceManager;
Printer* fDefaultPrinter;
#ifdef HAIKU_TARGET_PLATFORM_HAIKU #ifdef HAIKU_TARGET_PLATFORM_HAIKU
size_t fIconSize; size_t fIconSize;
uint8 *fSelectedIcon; uint8* fSelectedIcon;
#else #else
BBitmap *fSelectedIconMini; BBitmap* fSelectedIconMini;
BBitmap *fSelectedIconLarge; BBitmap* fSelectedIconLarge;
#endif #endif
int32 fReferences; int32 fReferences;
sem_id fHasReferences; sem_id fHasReferences;
Settings *fSettings; Settings* fSettings;
bool fUseConfigWindow; bool fUseConfigWindow;
FolderWatcher *fFolder; FolderWatcher* fFolder;
}; };
#endif
#endif // _PRINT_SERVER_APP_H