xref: /haiku/src/add-ons/print/transports/lpr/LprSetupDlg.cpp (revision 1214ef1b2100f2b3299fc9d8d6142e46f70a4c3f)
1 // Sun, 18 Jun 2000
2 // Y.Takagi
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 
11 #include <algorithm>
12 
13 #include "LpsClient.h"
14 #include "LprSetupDlg.h"
15 #include "LprDefs.h"
16 #include "DbgMsg.h"
17 
18 #define	DLG_WIDTH		370
19 #define DLG_HEIGHT		100
20 
21 #define BUTTON_WIDTH	70
22 #define BUTTON_HEIGHT	20
23 
24 #define SERVER_H		10
25 #define SERVER_V		10
26 #define SERVER_WIDTH	(DLG_WIDTH - SERVER_H - SERVER_H)
27 #define SERVER_HEIGHT	20
28 #define SERVER_TEXT		"Server name"
29 
30 #define QUEUE_H			10
31 #define QUEUE_V			SERVER_V + SERVER_HEIGHT + 2
32 #define QUEUE_WIDTH		(DLG_WIDTH - QUEUE_H - QUEUE_H)
33 #define QUEUE_HEIGHT	20
34 #define QUEUE_TEXT		"Queue name"
35 
36 #define OK_H			(DLG_WIDTH  - BUTTON_WIDTH  - 11)
37 #define OK_V			(DLG_HEIGHT - BUTTON_HEIGHT - 11)
38 #define OK_TEXT			"OK"
39 
40 #define CANCEL_H		(OK_H - BUTTON_WIDTH - 12)
41 #define CANCEL_V		OK_V
42 #define CANCEL_TEXT		"Cancel"
43 
44 const BRect SERVER_RECT(
45 	SERVER_H,
46 	SERVER_V,
47 	SERVER_H + SERVER_WIDTH,
48 	SERVER_V + SERVER_HEIGHT);
49 
50 const BRect QUEUE_RECT(
51 	QUEUE_H,
52 	QUEUE_V,
53 	QUEUE_H + QUEUE_WIDTH,
54 	QUEUE_V + QUEUE_HEIGHT);
55 
56 const BRect OK_RECT(
57 	OK_H,
58 	OK_V,
59 	OK_H + BUTTON_WIDTH,
60 	OK_V + BUTTON_HEIGHT);
61 
62 const BRect CANCEL_RECT(
63 	CANCEL_H,
64 	CANCEL_V,
65 	CANCEL_H + BUTTON_WIDTH,
66 	CANCEL_V + BUTTON_HEIGHT);
67 
68 enum MSGS {
69 	M_CANCEL = 1,
70 	M_OK
71 };
72 
73 
74 class LprSetupView : public BView {
75 public:
76 	LprSetupView(BRect, BDirectory *);
77 	~LprSetupView() {}
78 	virtual void AttachedToWindow();
79 	bool UpdateViewData();
80 
81 private:
82 	BTextControl *server;
83 	BTextControl *queue;
84 	BDirectory   *dir;
85 };
86 
87 LprSetupView::LprSetupView(BRect frame, BDirectory *d)
88 	: BView(frame, "", B_FOLLOW_ALL, B_WILL_DRAW), dir(d)
89 {
90 	SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
91 }
92 
93 void LprSetupView::AttachedToWindow()
94 {
95 	float width = max(StringWidth(SERVER_TEXT), StringWidth(QUEUE_TEXT)) + 10;
96 
97 	/* server name box */
98 
99     // TODO remember previous value
100 	server = new BTextControl(SERVER_RECT, "", SERVER_TEXT, "192.168.0.0", NULL);
101 	AddChild(server);
102 	server->SetDivider(width);
103 
104 	/* queue name box */
105 
106     // TODO remember previous value
107 	queue = new BTextControl(QUEUE_RECT, "", QUEUE_TEXT, "LPT1_PASSTHRU", NULL);
108 	AddChild(queue);
109 	queue->SetDivider(width);
110 
111 	/* cancel */
112 
113 	BButton *button = new BButton(CANCEL_RECT, "", CANCEL_TEXT, new BMessage(M_CANCEL));
114 	AddChild(button);
115 
116 	/* ok */
117 
118 	button = new BButton(OK_RECT, "", OK_TEXT, new BMessage(M_OK));
119 	AddChild(button);
120 	button->MakeDefault(true);
121 }
122 
123 bool LprSetupView::UpdateViewData()
124 {
125 	if (*server->Text() && *queue->Text()) {
126 
127 		try {
128 			LpsClient lpr(server->Text());
129 			lpr.connect();
130 		}
131 
132 		catch (LPSException &err) {
133 			BAlert *alert = new BAlert("", err.what(), "OK");
134 			alert->Go();
135 			return false;
136 		}
137 
138 		dir->WriteAttr(LPR_SERVER_NAME, B_STRING_TYPE, 0, server->Text(), strlen(server->Text()) + 1);
139 		dir->WriteAttr(LPR_QUEUE_NAME,  B_STRING_TYPE, 0, queue->Text(),  strlen(queue->Text())  + 1);
140 		return true;
141 	}
142 
143 	BAlert *alert = new BAlert("", "please input parameters.", "OK");
144 	alert->Go();
145 	return false;
146 }
147 
148 LprSetupDlg::LprSetupDlg(BDirectory *dir)
149 	: BWindow(BRect(100, 100, 100 + DLG_WIDTH, 100 + DLG_HEIGHT),
150 		"LPR Setup", B_TITLED_WINDOW_LOOK, B_MODAL_APP_WINDOW_FEEL,
151 		B_NOT_RESIZABLE | B_NOT_MINIMIZABLE | B_NOT_ZOOMABLE)
152 {
153 	result = 0;
154 
155 	Lock();
156 	LprSetupView *view = new LprSetupView(Bounds(), dir);
157 	AddChild(view);
158 	Unlock();
159 
160 	semaphore = create_sem(0, "lprSetupSem");
161 }
162 
163 bool LprSetupDlg::QuitRequested()
164 {
165 	result = B_ERROR;
166 	release_sem(semaphore);
167 	return true;
168 }
169 
170 void LprSetupDlg::MessageReceived(BMessage *msg)
171 {
172 	bool success;
173 
174 	switch (msg->what) {
175 	case M_OK:
176 		Lock();
177 		success = ((LprSetupView *)ChildAt(0))->UpdateViewData();
178 		Unlock();
179 		if (success) {
180 			result = B_NO_ERROR;
181 			release_sem(semaphore);
182 		}
183 		break;
184 
185 	case M_CANCEL:
186 		result = B_ERROR;
187 		release_sem(semaphore);
188 		break;
189 
190 	default:
191 		BWindow::MessageReceived(msg);
192 		break;
193 	}
194 }
195 
196 int LprSetupDlg::Go()
197 {
198 	Show();
199 	acquire_sem(semaphore);
200 	delete_sem(semaphore);
201 	int value = result;
202 	Lock();
203 	Quit();
204 	return value;
205 }
206