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