3 #include "SetupWindow.h"
10 #include <Directory.h>
11 #include <NetEndpoint.h>
14 #include <TextControl.h>
18 #define DLG_HEIGHT 100
20 #define BUTTON_WIDTH 70
21 #define BUTTON_HEIGHT 20
25 #define SERVER_WIDTH (DLG_WIDTH - SERVER_H - SERVER_H)
26 #define SERVER_HEIGHT 20
27 #define SERVER_TEXT "Printer address"
30 #define QUEUE_V SERVER_V + SERVER_HEIGHT + 2
31 #define QUEUE_WIDTH (DLG_WIDTH - QUEUE_H - QUEUE_H)
32 #define QUEUE_HEIGHT 20
33 #define QUEUE_TEXT "Port"
35 #define OK_H (DLG_WIDTH - BUTTON_WIDTH - 11)
36 #define OK_V (DLG_HEIGHT - BUTTON_HEIGHT - 11)
39 #define CANCEL_H (OK_H - BUTTON_WIDTH - 12)
41 #define CANCEL_TEXT "Cancel"
43 const BRect
SERVER_RECT(
46 SERVER_H
+ SERVER_WIDTH
,
47 SERVER_V
+ SERVER_HEIGHT
);
49 const BRect
QUEUE_RECT(
52 QUEUE_H
+ QUEUE_WIDTH
,
53 QUEUE_V
+ QUEUE_HEIGHT
);
59 OK_V
+ BUTTON_HEIGHT
);
61 const BRect
CANCEL_RECT(
64 CANCEL_H
+ BUTTON_WIDTH
,
65 CANCEL_V
+ BUTTON_HEIGHT
);
73 class SetupView
: public BView
{
75 SetupView(BRect
, BDirectory
* );
76 virtual void AttachedToWindow();
81 BTextControl
* fServerAddress
;
82 BTextControl
* fQueuePort
;
83 BDirectory
* fPrinterDirectory
;
87 SetupView::SetupView(BRect frame
, BDirectory
* directory
)
88 : BView(frame
, "", B_FOLLOW_ALL
, B_WILL_DRAW
),
89 fPrinterDirectory(directory
)
91 SetViewUIColor(B_PANEL_BACKGROUND_COLOR
);
96 SetupView::AttachedToWindow()
98 float width
= MAX(StringWidth(SERVER_TEXT
), StringWidth(QUEUE_TEXT
)) + 10;
100 /* server name box */
102 fServerAddress
= new BTextControl(SERVER_RECT
, "", SERVER_TEXT
, "<printer's hostname or address>", NULL
);
103 AddChild(fServerAddress
);
104 fServerAddress
->SetDivider(width
);
108 fQueuePort
= new BTextControl(QUEUE_RECT
, "", QUEUE_TEXT
, "9100", NULL
); // 9100 is default HP JetDirect port number
109 AddChild(fQueuePort
);
110 fQueuePort
->SetDivider(width
);
114 BButton
* button
= new BButton(CANCEL_RECT
, "", CANCEL_TEXT
, new BMessage(M_CANCEL
));
119 button
= new BButton(OK_RECT
, "", OK_TEXT
, new BMessage(M_OK
));
121 button
->MakeDefault(true);
126 SetupView::CheckSetup()
128 if (*fServerAddress
->Text() && *fQueuePort
->Text()) {
129 BNetEndpoint
* ep
= new(std::nothrow
) BNetEndpoint(SOCK_STREAM
);
130 if (ep
!= NULL
&& ep
->InitCheck() == B_NO_ERROR
) {
131 uint16 port
= atoi(fQueuePort
->Text());
136 if (ep
->Connect(fServerAddress
->Text(), port
) != B_OK
) {
138 text
<< "Failed to connect to " << fServerAddress
->Text()
139 << ":" << (int) port
<< "!";
140 BAlert
* alert
= new BAlert("", text
.String(), "OK");
141 alert
->SetFlags(alert
->Flags() | B_CLOSE_ON_ESCAPE
);
148 sprintf(str
, "%s:%d", fServerAddress
->Text(), port
);
149 fPrinterDirectory
->WriteAttr("transport_address", B_STRING_TYPE
,
150 0, str
, strlen(str
) + 1);
157 BAlert
* alert
= new BAlert("", "Please input parameters.", "OK");
158 alert
->SetFlags(alert
->Flags() | B_CLOSE_ON_ESCAPE
);
167 SetupWindow::SetupWindow(BDirectory
* printerDirectory
)
168 : BWindow(BRect(100, 100, 100 + DLG_WIDTH
, 100 + DLG_HEIGHT
),
169 "HP JetDirect Setup", B_TITLED_WINDOW_LOOK
, B_MODAL_APP_WINDOW_FEEL
,
170 B_NOT_RESIZABLE
| B_NOT_MINIMIZABLE
| B_NOT_ZOOMABLE
176 SetupView
* view
= new SetupView(Bounds(), printerDirectory
);
180 fExitSem
= create_sem(0, "SetupWindowSem");
185 SetupWindow::QuitRequested()
188 release_sem(fExitSem
);
194 SetupWindow::MessageReceived(BMessage
* msg
)
201 success
= ((SetupView
*)ChildAt(0))->CheckSetup();
204 fResult
= B_NO_ERROR
;
205 release_sem(fExitSem
);
211 release_sem(fExitSem
);
215 BWindow::MessageReceived(msg
);
225 acquire_sem(fExitSem
);
226 delete_sem(fExitSem
);