1/*
2 * Copyright 2001 Dr. Zoidberg Enterprises. All rights reserved.
3 */
4
5
6//!	Config views for the account, protocols, and filters.
7
8
9#include "ConfigViews.h"
10#include "CenterContainer.h"
11
12#include <TextControl.h>
13#include <ListView.h>
14#include <PopUpMenu.h>
15#include <MenuField.h>
16#include <MenuItem.h>
17#include <Button.h>
18#include <Looper.h>
19#include <Path.h>
20#include <Alert.h>
21#include <Entry.h>
22#include <FindDirectory.h>
23#include <Directory.h>
24#include <Catalog.h>
25#include <Locale.h>
26
27#include <string.h>
28
29#include <MailSettings.h>
30
31#include "FilterConfigView.h"
32
33
34#undef B_TRANSLATION_CONTEXT
35#define B_TRANSLATION_CONTEXT "Config Views"
36
37
38// AccountConfigView
39const uint32 kMsgAccountNameChanged = 'anmc';
40
41// ProtocolsConfigView
42const uint32 kMsgProtocolChanged = 'prch';
43
44
45BView*
46CreateConfigView(entry_ref addon, MailAddonSettings& settings,
47	BMailAccountSettings& accountSettings, image_id* image)
48{
49	BView* (*instantiate_config)(MailAddonSettings& settings,
50		BMailAccountSettings& accountSettings);
51	BPath path(&addon);
52	*image = load_add_on(path.Path());
53	if (image < 0)
54		return NULL;
55
56	if (get_image_symbol(*image, "instantiate_config_panel", B_SYMBOL_TYPE_TEXT,
57		(void **)&instantiate_config) != B_OK) {
58		unload_add_on(*image);
59		*image = -1;
60		return NULL;
61	}
62
63	BView* view = (*instantiate_config)(settings, accountSettings);
64	return view;
65}
66
67
68AccountConfigView::AccountConfigView(BRect rect, BMailAccountSettings* account)
69	:
70	BBox(rect),
71	fAccount(account)
72{
73	SetLabel(B_TRANSLATE("Account settings"));
74
75	rect = Bounds().InsetByCopy(8, 8);
76	rect.top += 10;
77	CenterContainer *view = new CenterContainer(rect, false);
78	view->SetSpacing(5);
79
80	// determine font height
81	font_height fontHeight;
82	view->GetFontHeight(&fontHeight);
83	int32 height = (int32)(fontHeight.ascent + fontHeight.descent
84		+ fontHeight.leading) + 5;
85
86	rect = view->Bounds();
87	rect.bottom = height + 5;
88
89	float labelWidth = view->StringWidth(B_TRANSLATE("Account name:")) + 6;
90
91	view->AddChild(fNameControl = new BTextControl(rect, NULL,
92		B_TRANSLATE("Account name:"), NULL,
93		new BMessage(kMsgAccountNameChanged)));
94	fNameControl->SetDivider(labelWidth);
95	view->AddChild(fRealNameControl = new BTextControl(rect, NULL,
96		B_TRANSLATE("Real name:"), NULL, NULL));
97	fRealNameControl->SetDivider(labelWidth);
98	view->AddChild(fReturnAddressControl = new BTextControl(rect, NULL,
99		B_TRANSLATE("Return address:"), NULL, NULL));
100	fReturnAddressControl->SetDivider(labelWidth);
101//			control->TextView()->HideTyping(true);
102
103	float w, h;
104	view->GetPreferredSize(&w, &h);
105	ResizeTo(w + 15, h + 22);
106	view->ResizeTo(w, h);
107
108	AddChild(view);
109}
110
111
112void
113AccountConfigView::DetachedFromWindow()
114{
115	fAccount->SetName(fNameControl->Text());
116	fAccount->SetRealName(fRealNameControl->Text());
117	fAccount->SetReturnAddress(fReturnAddressControl->Text());
118}
119
120
121void
122AccountConfigView::AttachedToWindow()
123{
124	UpdateViews();
125	fNameControl->SetTarget(this);
126}
127
128
129void
130AccountConfigView::MessageReceived(BMessage *msg)
131{
132	switch (msg->what) {
133		case kMsgAccountNameChanged:
134			fAccount->SetName(fNameControl->Text());
135			break;
136
137		default:
138			BView::MessageReceived(msg);
139	}
140}
141
142
143void
144AccountConfigView::UpdateViews()
145{
146	fNameControl->SetText(fAccount->Name());
147	fRealNameControl->SetText(fAccount->RealName());
148	fReturnAddressControl->SetText(fAccount->ReturnAddress());
149}
150
151
152//	#pragma mark -
153
154
155InProtocolsConfigView::InProtocolsConfigView(BMailAccountSettings* account)
156	:
157	BBox(BRect(0, 0, 100, 100)),
158	fAccount(account),
159	fConfigView(NULL)
160{
161	BString label = "Can't find protocol.";
162	entry_ref protocol = fAccount->InboundPath();
163	MailAddonSettings& inboundSettings = fAccount->InboundSettings();
164
165	fConfigView = CreateConfigView(protocol, inboundSettings, *account,
166		&fImageID);
167
168	if (fConfigView) {
169		float w = fConfigView->Bounds().Width();
170		float h = fConfigView->Bounds().Height();
171		fConfigView->MoveTo(3, 13);
172		ResizeTo(w + 6, h + 16);
173		AddChild(fConfigView);
174
175		fConfigView->MoveTo(3, 21);
176		ResizeBy(0, 8);
177		if (CenterContainer *container
178			= dynamic_cast<CenterContainer *>(Parent())) {
179			container->Layout();
180		}
181		label = protocol.name;
182
183		fConfigView->MoveTo(3, 21);
184		ResizeBy(0, 8);
185	}
186	SetLabel(label);
187}
188
189
190void
191InProtocolsConfigView::DetachedFromWindow()
192{
193	if (fConfigView == NULL)
194		return;
195	BMessage settings;
196	if (fConfigView->Archive(&settings) != B_OK)
197		return;
198	fAccount->InboundSettings().EditSettings() = settings;
199
200	RemoveChild(fConfigView);
201	delete fConfigView;
202	fConfigView = NULL;
203	unload_add_on(fImageID);
204}
205
206
207OutProtocolsConfigView::OutProtocolsConfigView(BMailAccountSettings* account)
208	:
209	BBox(BRect(0, 0, 100, 100)),
210	fAccount(account),
211	fConfigView(NULL)
212{
213	BString label = "Can't find protocol.";
214	entry_ref protocol = fAccount->OutboundPath();
215	MailAddonSettings& outboundSettings = fAccount->OutboundSettings();
216	fConfigView = CreateConfigView(protocol, outboundSettings, *account,
217		&fImageID);
218
219	if (fConfigView) {
220		float w = fConfigView->Bounds().Width();
221		float h = fConfigView->Bounds().Height();
222		fConfigView->MoveTo(3, 13);
223		ResizeTo(w + 6, h + 16);
224		AddChild(fConfigView);
225
226		fConfigView->MoveTo(3, 21);
227		ResizeBy(0, 8);
228		if (CenterContainer *container
229			= dynamic_cast<CenterContainer *>(Parent())) {
230			container->Layout();
231		}
232		label = protocol.name;
233
234		fConfigView->MoveTo(3, 21);
235		ResizeBy(0, 8);
236	}
237
238	SetLabel(label);
239}
240
241
242void
243OutProtocolsConfigView::DetachedFromWindow()
244{
245	if (fConfigView == NULL)
246		return;
247	BMessage settings;
248	if (fConfigView->Archive(&settings) != B_OK)
249		return;
250	fAccount->OutboundSettings().EditSettings() = settings;
251
252	RemoveChild(fConfigView);
253	delete fConfigView;
254	fConfigView = NULL;
255	unload_add_on(fImageID);
256}
257