1/* ConfigView - the configuration view for the Notifier filter
2**
3** Copyright 2001 Dr. Zoidberg Enterprises. All rights reserved.
4*/
5
6
7#include "ConfigView.h"
8
9#include <Catalog.h>
10#include <CheckBox.h>
11#include <PopUpMenu.h>
12#include <MenuItem.h>
13#include <MenuField.h>
14#include <String.h>
15#include <Message.h>
16
17#include <MailAddon.h>
18#include <MailSettings.h>
19
20
21#undef B_TRANSLATION_CONTEXT
22#define B_TRANSLATION_CONTEXT "ConfigView"
23
24
25const uint32 kMsgNotifyMethod = 'nomt';
26
27
28ConfigView::ConfigView()
29	:	BView(BRect(0,0,10,10),"notifier_config",B_FOLLOW_LEFT | B_FOLLOW_TOP,0)
30{
31	SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
32
33	// determine font height
34	font_height fontHeight;
35	GetFontHeight(&fontHeight);
36	float itemHeight = (int32)(fontHeight.ascent + fontHeight.descent
37		+ fontHeight.leading) + 6;
38
39	BRect frame(5,2,250,itemHeight + 2);
40	BPopUpMenu *menu = new BPopUpMenu(B_EMPTY_STRING,false,false);
41
42	const char *notifyMethods[] = {
43		B_TRANSLATE("Beep"),
44		B_TRANSLATE("Alert"),
45		B_TRANSLATE("Keyboard LEDs"),
46		B_TRANSLATE("Central alert"),
47		B_TRANSLATE("Central beep"),
48		B_TRANSLATE("Log window")
49	};
50	for (int32 i = 0,j = 1;i < 6;i++,j *= 2) {
51		menu->AddItem(new BMenuItem(notifyMethods[i],
52			new BMessage(kMsgNotifyMethod)));
53	}
54
55	BMenuField *field = new BMenuField(frame,"notify", B_TRANSLATE("Method:"),
56		menu);
57	field->ResizeToPreferred();
58	field->SetDivider(field->StringWidth(B_TRANSLATE("Method:")) + 6);
59	AddChild(field);
60
61	ResizeToPreferred();
62}
63
64
65void ConfigView::AttachedToWindow()
66{
67	if (BMenuField *field = dynamic_cast<BMenuField *>(FindView("notify")))
68		field->Menu()->SetTargetForItems(this);
69}
70
71
72void ConfigView::SetTo(const BMessage *archive)
73{
74	int32 method = archive->FindInt32("notification_method");
75	if (method < 0)
76		method = 1;
77
78	BMenuField *field;
79	if ((field = dynamic_cast<BMenuField *>(FindView("notify"))) == NULL)
80		return;
81
82	for (int32 i = field->Menu()->CountItems();i-- > 0;)
83	{
84		BMenuItem *item = field->Menu()->ItemAt(i);
85		item->SetMarked((method & (1L << i)) != 0);
86	}
87	UpdateNotifyText();
88}
89
90
91void ConfigView::UpdateNotifyText()
92{
93	BMenuField *field;
94	if ((field = dynamic_cast<BMenuField *>(FindView("notify"))) == NULL)
95		return;
96
97	BString label;
98	for (int32 i = field->Menu()->CountItems();i-- > 0;)
99	{
100		BMenuItem *item = field->Menu()->ItemAt(i);
101		if (!item->IsMarked())
102			continue;
103
104		if (label != "")
105			label.Prepend(" + ");
106		label.Prepend(item->Label());
107	}
108	if (label == "")
109		label = B_TRANSLATE("none");
110	field->MenuItem()->SetLabel(label.String());
111}
112
113
114void ConfigView::MessageReceived(BMessage *msg)
115{
116	switch (msg->what)
117	{
118		case kMsgNotifyMethod:
119		{
120			BMenuItem *item;
121			if (msg->FindPointer("source",(void **)&item) < B_OK)
122				break;
123
124			item->SetMarked(!item->IsMarked());
125			UpdateNotifyText();
126			break;
127		}
128		default:
129			BView::MessageReceived(msg);
130	}
131}
132
133
134status_t ConfigView::Archive(BMessage *into, bool) const
135{
136	int32 method = 0;
137
138	BMenuField *field;
139	if ((field = dynamic_cast<BMenuField *>(FindView("notify"))) != NULL)
140	{
141		for (int32 i = field->Menu()->CountItems();i-- > 0;)
142		{
143			BMenuItem *item = field->Menu()->ItemAt(i);
144			if (item->IsMarked())
145				method |= 1L << i;
146		}
147	}
148
149	if (into->ReplaceInt32("notification_method",method) != B_OK)
150		into->AddInt32("notification_method",method);
151
152	return B_OK;
153}
154
155
156void ConfigView::GetPreferredSize(float *width, float *height)
157{
158	*width = 258;
159	*height = ChildAt(0)->Bounds().Height() + 8;
160}
161
162
163BView*
164instantiate_filter_config_panel(AddonSettings& settings)
165{
166	ConfigView *view = new ConfigView();
167	view->SetTo(&settings.Settings());
168	return view;
169}
170
171
172BString
173descriptive_name()
174{
175	return B_TRANSLATE("New mails notification");
176}
177