xref: /haiku/src/add-ons/print/transports/hp_jetdirect/SetupWindow.cpp (revision 1c09002cbee8e797a0f8bbfc5678dfadd39ee1a7)
1 
2 
3 #include "SetupWindow.h"
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 
8 #include <Alert.h>
9 #include <Button.h>
10 #include <Directory.h>
11 #include <NetEndpoint.h>
12 #include <Rect.h>
13 #include <String.h>
14 #include <TextControl.h>
15 #include <View.h>
16 
17 #define	DLG_WIDTH		370
18 #define DLG_HEIGHT		100
19 
20 #define BUTTON_WIDTH	70
21 #define BUTTON_HEIGHT	20
22 
23 #define SERVER_H		10
24 #define SERVER_V		10
25 #define SERVER_WIDTH	(DLG_WIDTH - SERVER_H - SERVER_H)
26 #define SERVER_HEIGHT	20
27 #define SERVER_TEXT		"Printer address"
28 
29 #define QUEUE_H			10
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"
34 
35 #define OK_H			(DLG_WIDTH  - BUTTON_WIDTH  - 11)
36 #define OK_V			(DLG_HEIGHT - BUTTON_HEIGHT - 11)
37 #define OK_TEXT			"OK"
38 
39 #define CANCEL_H		(OK_H - BUTTON_WIDTH - 12)
40 #define CANCEL_V		OK_V
41 #define CANCEL_TEXT		"Cancel"
42 
43 const BRect SERVER_RECT(
44 	SERVER_H,
45 	SERVER_V,
46 	SERVER_H + SERVER_WIDTH,
47 	SERVER_V + SERVER_HEIGHT);
48 
49 const BRect QUEUE_RECT(
50 	QUEUE_H,
51 	QUEUE_V,
52 	QUEUE_H + QUEUE_WIDTH,
53 	QUEUE_V + QUEUE_HEIGHT);
54 
55 const BRect OK_RECT(
56 	OK_H,
57 	OK_V,
58 	OK_H + BUTTON_WIDTH,
59 	OK_V + BUTTON_HEIGHT);
60 
61 const BRect CANCEL_RECT(
62 	CANCEL_H,
63 	CANCEL_V,
64 	CANCEL_H + BUTTON_WIDTH,
65 	CANCEL_V + BUTTON_HEIGHT);
66 
67 enum MSGS {
68 	M_CANCEL = 1,
69 	M_OK
70 };
71 
72 
73 class SetupView : public BView {
74 public:
75 							SetupView(BRect, BDirectory* );
76 	virtual void 			AttachedToWindow();
77 
78 		bool 				CheckSetup();
79 private:
80 
81 		BTextControl*		fServerAddress;
82 		BTextControl*		fQueuePort;
83 		BDirectory*			fPrinterDirectory;
84 };
85 
86 
87 SetupView::SetupView(BRect frame, BDirectory* directory)
88 	: BView(frame, "", B_FOLLOW_ALL, B_WILL_DRAW),
89 	fPrinterDirectory(directory)
90 {
91 	SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
92 }
93 
94 
95 void
96 SetupView::AttachedToWindow()
97 {
98 	float width = MAX(StringWidth(SERVER_TEXT), StringWidth(QUEUE_TEXT)) + 10;
99 
100 	/* server name box */
101 
102 	fServerAddress = new BTextControl(SERVER_RECT, "", SERVER_TEXT, "<printer's hostname or address>", NULL);
103 	AddChild(fServerAddress);
104 	fServerAddress->SetDivider(width);
105 
106 	/* queue name box */
107 
108 	fQueuePort = new BTextControl(QUEUE_RECT, "", QUEUE_TEXT, "9100", NULL);	// 9100 is default HP JetDirect port number
109 	AddChild(fQueuePort);
110 	fQueuePort->SetDivider(width);
111 
112 	/* cancel */
113 
114 	BButton* button = new BButton(CANCEL_RECT, "", CANCEL_TEXT, new BMessage(M_CANCEL));
115 	AddChild(button);
116 
117 	/* ok */
118 
119 	button = new BButton(OK_RECT, "", OK_TEXT, new BMessage(M_OK));
120 	AddChild(button);
121 	button->MakeDefault(true);
122 }
123 
124 
125 bool
126 SetupView::CheckSetup()
127 {
128 	if (*fServerAddress->Text() && *fQueuePort->Text()) {
129 		BNetEndpoint* ep = new BNetEndpoint(SOCK_STREAM);
130 		if (ep->InitCheck() == B_NO_ERROR) {
131 			uint16 port = atoi(fQueuePort->Text());
132 
133 			if (! port)
134 				port = 9100;
135 
136 			if (ep->Connect(fServerAddress->Text(), port) != B_OK) {
137 				BString text;
138 				text << "Fail to connect to " << fServerAddress->Text() << ":" << (int) port << "!";
139 				BAlert* alert = new BAlert("", text.String(), "OK");
140 				alert->Go();
141 				return false;
142 			};
143 
144 			char str[256];
145 			sprintf(str, "%s:%d", fServerAddress->Text(), port);
146 			fPrinterDirectory->WriteAttr("transport_address", B_STRING_TYPE,
147 				0, str, strlen(str) + 1);
148 			return true;
149 		};
150 	};
151 
152 	BAlert* alert = new BAlert("", "please input parameters.", "OK");
153 	alert->Go();
154 	return false;
155 }
156 
157 
158 // #pragma mark -
159 
160 
161 SetupWindow::SetupWindow(BDirectory* printerDirectory)
162 	: BWindow(BRect(100, 100, 100 + DLG_WIDTH, 100 + DLG_HEIGHT),
163 		"HP JetDirect Setup", B_TITLED_WINDOW_LOOK, B_MODAL_APP_WINDOW_FEEL,
164 		B_NOT_RESIZABLE | B_NOT_MINIMIZABLE | B_NOT_ZOOMABLE
165 			| B_CLOSE_ON_ESCAPE)
166 {
167 	fResult = 0;
168 
169 	Lock();
170 	SetupView* view = new SetupView(Bounds(), printerDirectory);
171 	AddChild(view);
172 	Unlock();
173 
174 	fExitSem = create_sem(0, "SetupWindowSem");
175 }
176 
177 
178 bool
179 SetupWindow::QuitRequested()
180 {
181 	fResult = B_ERROR;
182 	release_sem(fExitSem);
183 	return true;
184 }
185 
186 
187 void
188 SetupWindow::MessageReceived(BMessage* msg)
189 {
190 	bool success;
191 
192 	switch (msg->what) {
193 		case M_OK:
194 			Lock();
195 			success = ((SetupView*)ChildAt(0))->CheckSetup();
196 			Unlock();
197 			if (success) {
198 				fResult = B_NO_ERROR;
199 				release_sem(fExitSem);
200 			}
201 			break;
202 
203 		case M_CANCEL:
204 			fResult = B_ERROR;
205 			release_sem(fExitSem);
206 			break;
207 
208 		default:
209 			BWindow::MessageReceived(msg);
210 			break;
211 	}
212 }
213 
214 
215 int
216 SetupWindow::Go()
217 {
218 	Show();
219 	acquire_sem(fExitSem);
220 	delete_sem(fExitSem);
221 	int value = fResult;
222 	Lock();
223 	Quit();
224 	return value;
225 }
226 
227 
228