HP JetDirect transport add-on was the only one to not store/retrieve is

address from transport_address attribute but a custom one. Fixed.

Now explicitly shutdown the connection at job end, some printers requires this.

Style cleanup that was lying since months, but this code will be reworked
again soon, as the SetupWindow needs to be rebuild for layout API and locale
translation...


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@39718 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Philippe Houdoin 2010-12-03 14:49:29 +00:00
parent 76108fd7a5
commit b7d73f97ee
5 changed files with 156 additions and 169 deletions

View File

@ -1,35 +1,14 @@
/*****************************************************************************/
// HP JetDirect (TCP/IP only) transport add-on,
//
// Author
// Philippe Houdoin
//
// This application and all source files used in its construction, except
// where noted, are licensed under the MIT License, and have been written
// and are:
//
// Copyright (c) 2001-2003 OpenBeOS Project
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
/*****************************************************************************/
/*
* Copyright 2001-2010 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Philippe Houdoin,
*/
#include "HPJetDirectTransport.h"
#include <stdio.h>
#include <Alert.h>
@ -39,46 +18,53 @@
#include <String.h>
#include <NetEndpoint.h>
#include "HPJetDirectTransport.h"
#include "SetupWindow.h"
// Implementation of HPJetDirectPort
HPJetDirectPort::HPJetDirectPort(BDirectory* printer, BMessage *msg)
:
HPJetDirectPort::HPJetDirectPort(BDirectory* printer, BMessage *msg)
: fHost(""),
fPort(9100),
fEndpoint(NULL),
fReady(B_ERROR)
{
fHost[0] = '\0';
BString address;
const char *spool_path = msg->FindString("printer_file");
if (spool_path && *spool_path) {
BDirectory dir(spool_path);
dir.ReadAttr("hp_jetdirect:host", B_STRING_TYPE, 0, fHost, sizeof(fHost));
if (fHost[0] == '\0') {
SetupWindow *setup = new SetupWindow(&dir);
if (setup->Go() == B_ERROR)
return;
}
dir.ReadAttr("hp_jetdirect:host", B_STRING_TYPE, 0, fHost, sizeof(fHost));
dir.ReadAttr("hp_jetdirect:port", B_UINT16_TYPE, 0, &fPort, sizeof(fPort));
if (printer->ReadAttrString("transport_address", &address) < 0) {
SetupWindow *setup = new SetupWindow(printer);
if (setup->Go() == B_ERROR)
return;
}
if (printer->ReadAttrString("transport_address", &address) < 0)
return;
printf("address = %s\n", address.String());
int32 index = address.FindLast(':');
if (index >= 0) {
fPort = atoi(address.String() + index);
address.MoveInto(fHost, 0, index);
} else
fHost = address;
printf("fHost = %s\n", fHost.String());
printf("fPort = %d\n", fPort);
fEndpoint = new BNetEndpoint(SOCK_STREAM);
if ((fReady = fEndpoint->InitCheck()) != B_OK) {
BAlert *alert = new BAlert("", "Fail to create the NetEndpoint!", "OK");
alert->Go();
return;
}
if (fEndpoint->Connect(fHost, fPort) == B_OK) {
printf("Connected to HP JetDirect printer port at %s:%d\n", fHost, fPort);
printf("Connected to HP JetDirect printer port at %s:%d\n",
fHost.String(), fPort);
fReady = B_OK;
} else {
BAlert *alert = new BAlert("", "Can't connect to HP JetDirect printer port!", "OK");
BAlert *alert = new BAlert("",
"Can't connect to HP JetDirect printer port!", "OK");
alert->Go();
fReady = B_ERROR;
}
@ -87,8 +73,10 @@ HPJetDirectPort::HPJetDirectPort(BDirectory* printer, BMessage *msg)
HPJetDirectPort::~HPJetDirectPort()
{
if (fEndpoint)
if (fEndpoint) {
shutdown(fEndpoint->Socket(), SHUT_WR);
fEndpoint->Close();
}
delete fEndpoint;
}

View File

@ -1,53 +1,32 @@
/*****************************************************************************/
// HP JetDirect (TCP/IP only) transport add-on,
//
// Author
// Philippe Houdoin
//
// This application and all source files used in its construction, except
// where noted, are licensed under the MIT License, and have been written
// and are:
//
// Copyright (c) 2001-2003 OpenBeOS Project
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
/*****************************************************************************/
/*
* Copyright 2001-2010, Haiku.
* Distributed under the terms of the MIT License.
*
* Authors:
* Philippe Houdoin
*/
#ifndef HP_JETDIRECT_TRANSPORT_H
#define HP_JETDIRECT_TRANSPORT_H
#include <Message.h>
#include <Directory.h>
#include <NetEndpoint.h>
#include <DataIO.h>
#include <String.h>
class HPJetDirectPort : public BDataIO {
public:
HPJetDirectPort(BDirectory* printer, BMessage* msg);
~HPJetDirectPort();
class BDirectory;
class BMessage;
class BNetEndpoint;
status_t InitCheck() { return fReady; }
class HPJetDirectPort : public BDataIO {
public:
HPJetDirectPort(BDirectory* printer, BMessage* msg);
~HPJetDirectPort();
ssize_t Read(void* buffer, size_t size);
ssize_t Write(const void* buffer, size_t size);
status_t InitCheck() { return fReady; }
private:
char fHost[256];
ssize_t Read(void* buffer, size_t size);
ssize_t Write(const void* buffer, size_t size);
private:
BString fHost;
uint16 fPort; // default is 9100
BNetEndpoint *fEndpoint;
status_t fReady;

View File

@ -5,10 +5,10 @@ SetSubDirSupportedPlatformsBeOSCompatible ;
SubDirHdrs [ FDirName $(HAIKU_TOP) src add-ons print transports shared ] ;
Addon HP\ JetDirect :
print_transport.cpp
print_transport.cpp
HPJetDirectTransport.cpp
SetupWindow.cpp
: be $(TARGET_NETAPI_LIB) $(TARGET_LIBSUPC++)
: be $(TARGET_NETWORK_LIBS) $(TARGET_NETAPI_LIB) $(TARGET_LIBSUPC++)
;
Package haiku-printingkit-cvs :

View File

@ -1,16 +1,18 @@
#include "SetupWindow.h"
#include <stdio.h>
#include <stdlib.h>
#include <Alert.h>
#include <Button.h>
#include <Directory.h>
#include <NetEndpoint.h>
#include <Rect.h>
#include <String.h>
#include <TextControl.h>
#include <View.h>
#include <Directory.h>
#include <Alert.h>
#include <String.h>
#include <NetEndpoint.h>
#include "SetupWindow.h"
#define DLG_WIDTH 370
#define DLG_HEIGHT 100
@ -22,7 +24,7 @@
#define SERVER_V 10
#define SERVER_WIDTH (DLG_WIDTH - SERVER_H - SERVER_H)
#define SERVER_HEIGHT 20
#define SERVER_TEXT "Printer host name"
#define SERVER_TEXT "Printer address"
#define QUEUE_H 10
#define QUEUE_V SERVER_V + SERVER_HEIGHT + 2
@ -70,42 +72,46 @@ enum MSGS {
class SetupView : public BView {
public:
SetupView(BRect, BDirectory *);
~SetupView() {}
virtual void AttachedToWindow();
bool UpdateViewData();
SetupView(BRect, BDirectory* );
virtual void AttachedToWindow();
bool CheckSetup();
private:
BTextControl *server;
BTextControl *queue;
BDirectory *dir;
BTextControl* fServerAddress;
BTextControl* fQueuePort;
BDirectory* fPrinterDirectory;
};
SetupView::SetupView(BRect frame, BDirectory *d)
: BView(frame, "", B_FOLLOW_ALL, B_WILL_DRAW), dir(d)
SetupView::SetupView(BRect frame, BDirectory* directory)
: BView(frame, "", B_FOLLOW_ALL, B_WILL_DRAW),
fPrinterDirectory(directory)
{
SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
}
void SetupView::AttachedToWindow()
void
SetupView::AttachedToWindow()
{
float width = MAX(StringWidth(SERVER_TEXT), StringWidth(QUEUE_TEXT)) + 10;
/* server name box */
server = new BTextControl(SERVER_RECT, "", SERVER_TEXT, "", NULL);
AddChild(server);
server->SetDivider(width);
fServerAddress = new BTextControl(SERVER_RECT, "", SERVER_TEXT, "<printer's hostname or address>", NULL);
AddChild(fServerAddress);
fServerAddress->SetDivider(width);
/* queue name box */
queue = new BTextControl(QUEUE_RECT, "", QUEUE_TEXT, "9100", NULL); // 9100 is default HP JetDirect port number
AddChild(queue);
queue->SetDivider(width);
fQueuePort = new BTextControl(QUEUE_RECT, "", QUEUE_TEXT, "9100", NULL); // 9100 is default HP JetDirect port number
AddChild(fQueuePort);
fQueuePort->SetDivider(width);
/* cancel */
BButton *button = new BButton(CANCEL_RECT, "", CANCEL_TEXT, new BMessage(M_CANCEL));
BButton* button = new BButton(CANCEL_RECT, "", CANCEL_TEXT, new BMessage(M_CANCEL));
AddChild(button);
/* ok */
@ -115,92 +121,106 @@ void SetupView::AttachedToWindow()
button->MakeDefault(true);
}
bool SetupView::UpdateViewData()
bool
SetupView::CheckSetup()
{
if (*server->Text() && *queue->Text()) {
BNetEndpoint *ep = new BNetEndpoint(SOCK_STREAM);
if (*fServerAddress->Text() && *fQueuePort->Text()) {
BNetEndpoint* ep = new BNetEndpoint(SOCK_STREAM);
if (ep->InitCheck() == B_NO_ERROR) {
uint16 port = atoi(queue->Text());
uint16 port = atoi(fQueuePort->Text());
if (! port)
port = 9100;
if (ep->Connect(server->Text(), atoi(queue->Text())) != B_OK) {
if (ep->Connect(fServerAddress->Text(), port) != B_OK) {
BString text;
text << "Fail to connect to " << server->Text() << ":" << (int) port << "!";
BAlert *alert = new BAlert("", text.String(), "OK");
text << "Fail to connect to " << fServerAddress->Text() << ":" << (int) port << "!";
BAlert* alert = new BAlert("", text.String(), "OK");
alert->Go();
return false;
};
char str[256];
sprintf(str, "%s:%s", server->Text(), queue->Text());
dir->WriteAttr("hp_jetdirect:host", B_STRING_TYPE, 0, server->Text(), strlen(server->Text()) + 1);
dir->WriteAttr("hp_jetdirect:port", B_UINT16_TYPE, 0, &port, sizeof(port));
sprintf(str, "%s:%d", fServerAddress->Text(), port);
fPrinterDirectory->WriteAttr("transport_address", B_STRING_TYPE,
0, str, strlen(str) + 1);
return true;
};
};
BAlert *alert = new BAlert("", "please input parameters.", "OK");
BAlert* alert = new BAlert("", "please input parameters.", "OK");
alert->Go();
return false;
}
SetupWindow::SetupWindow(BDirectory *dir)
// #pragma mark -
SetupWindow::SetupWindow(BDirectory* printerDirectory)
: BWindow(BRect(100, 100, 100 + DLG_WIDTH, 100 + DLG_HEIGHT),
"HP JetDirect Setup", B_TITLED_WINDOW_LOOK, B_MODAL_APP_WINDOW_FEEL,
B_NOT_RESIZABLE | B_NOT_MINIMIZABLE | B_NOT_ZOOMABLE)
{
result = 0;
fResult = 0;
Lock();
SetupView *view = new SetupView(Bounds(), dir);
SetupView* view = new SetupView(Bounds(), printerDirectory);
AddChild(view);
Unlock();
semaphore = create_sem(0, "SetupWindowSem");
fExitSem = create_sem(0, "SetupWindowSem");
}
bool SetupWindow::QuitRequested()
bool
SetupWindow::QuitRequested()
{
result = B_ERROR;
release_sem(semaphore);
fResult = B_ERROR;
release_sem(fExitSem);
return true;
}
void SetupWindow::MessageReceived(BMessage *msg)
void
SetupWindow::MessageReceived(BMessage* msg)
{
bool success;
switch (msg->what) {
case M_OK:
Lock();
success = ((SetupView *)ChildAt(0))->UpdateViewData();
Unlock();
if (success) {
result = B_NO_ERROR;
release_sem(semaphore);
}
break;
case M_OK:
Lock();
success = ((SetupView*)ChildAt(0))->CheckSetup();
Unlock();
if (success) {
fResult = B_NO_ERROR;
release_sem(fExitSem);
}
break;
case M_CANCEL:
result = B_ERROR;
release_sem(semaphore);
break;
case M_CANCEL:
fResult = B_ERROR;
release_sem(fExitSem);
break;
default:
BWindow::MessageReceived(msg);
break;
default:
BWindow::MessageReceived(msg);
break;
}
}
int SetupWindow::Go()
int
SetupWindow::Go()
{
Show();
acquire_sem(semaphore);
delete_sem(semaphore);
int value = result;
acquire_sem(fExitSem);
delete_sem(fExitSem);
int value = fResult;
Lock();
Quit();
return value;
}

View File

@ -7,15 +7,15 @@ class BDirectory;
class SetupWindow : public BWindow {
public:
SetupWindow(BDirectory *);
~SetupWindow() {}
virtual bool QuitRequested();
virtual void MessageReceived(BMessage *message);
int Go();
SetupWindow(BDirectory* printerDirectory);
virtual bool QuitRequested();
virtual void MessageReceived(BMessage* message);
int Go();
private:
int result;
long semaphore;
int fResult;
long fExitSem;
};
#endif // SETUPWINDOW_H