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