1/*
2 * Copyright 2004-2012, Haiku, Inc. All rights reserved.
3 * Copyright 2001, Dr. Zoidberg Enterprises. All rights reserved.
4 *
5 * Distributed under the terms of the MIT License.
6 */
7
8
9#include "NotifierConfigView.h"
10
11#include <Catalog.h>
12#include <CheckBox.h>
13#include <LayoutBuilder.h>
14#include <PopUpMenu.h>
15#include <MenuItem.h>
16#include <MenuField.h>
17#include <String.h>
18#include <Message.h>
19
20#include <MailFilter.h>
21#include <MailSettings.h>
22
23
24#undef B_TRANSLATION_CONTEXT
25#define B_TRANSLATION_CONTEXT "NotifierConfigView"
26
27
28const uint32 kMsgNotifyMethod = 'nomt';
29
30
31NotifierConfigView::NotifierConfigView()
32	:
33	BMailSettingsView("notifier_config")
34{
35	SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
36
37	BPopUpMenu *menu = new BPopUpMenu(B_EMPTY_STRING, false, false);
38
39	const char *notifyMethods[] = {
40		B_TRANSLATE("Beep"),
41		B_TRANSLATE("Alert"),
42		B_TRANSLATE("Keyboard LEDs"),
43		B_TRANSLATE("Central alert"),
44		B_TRANSLATE("Central beep"),
45		B_TRANSLATE("Log window")
46	};
47	for (int32 i = 0, j = 1;i < 6; i++, j *= 2) {
48		menu->AddItem(new BMenuItem(notifyMethods[i],
49			new BMessage(kMsgNotifyMethod)));
50	}
51
52	BLayoutBuilder::Group<>(this).Add(
53		new BMenuField("notify", B_TRANSLATE("Method:"), menu));
54}
55
56
57void
58NotifierConfigView::SetTo(const BMessage *archive)
59{
60	int32 method = archive->FindInt32("notification_method");
61	if (method < 0)
62		method = 1;
63
64	BMenuField *field;
65	if ((field = dynamic_cast<BMenuField *>(FindView("notify"))) == NULL)
66		return;
67
68	for (int32 i = field->Menu()->CountItems(); i-- > 0;) {
69		BMenuItem *item = field->Menu()->ItemAt(i);
70		item->SetMarked((method & (1L << i)) != 0);
71	}
72	_UpdateNotifyText();
73}
74
75
76status_t
77NotifierConfigView::SaveInto(BMailAddOnSettings& settings) const
78{
79	int32 method = 0;
80
81	BMenuField *field;
82	if ((field = dynamic_cast<BMenuField *>(FindView("notify"))) != NULL) {
83		for (int32 i = field->Menu()->CountItems(); i-- > 0;) {
84			BMenuItem *item = field->Menu()->ItemAt(i);
85			if (item->IsMarked())
86				method |= 1L << i;
87		}
88	}
89
90	return settings.SetInt32("notification_method", method);
91}
92
93
94void
95NotifierConfigView::AttachedToWindow()
96{
97	if (BMenuField *field = dynamic_cast<BMenuField *>(FindView("notify")))
98		field->Menu()->SetTargetForItems(this);
99}
100
101
102void
103NotifierConfigView::_UpdateNotifyText()
104{
105	BMenuField *field;
106	if ((field = dynamic_cast<BMenuField *>(FindView("notify"))) == NULL)
107		return;
108
109	BString label;
110	for (int32 i = field->Menu()->CountItems(); i-- > 0;) {
111		BMenuItem *item = field->Menu()->ItemAt(i);
112		if (!item->IsMarked())
113			continue;
114
115		if (label != "")
116			label.Prepend(" + ");
117		label.Prepend(item->Label());
118	}
119	if (label == "")
120		label = B_TRANSLATE("none");
121	field->MenuItem()->SetLabel(label.String());
122}
123
124
125void
126NotifierConfigView::MessageReceived(BMessage *msg)
127{
128	switch (msg->what) {
129		case kMsgNotifyMethod:
130		{
131			BMenuItem *item;
132			if (msg->FindPointer("source",(void **)&item) < B_OK)
133				break;
134
135			item->SetMarked(!item->IsMarked());
136			_UpdateNotifyText();
137			break;
138		}
139		default:
140			BView::MessageReceived(msg);
141	}
142}
143
144
145// #pragma mark -
146
147
148BMailSettingsView*
149instantiate_filter_settings_view(const BMailAccountSettings& accountSettings,
150	const BMailAddOnSettings& settings)
151{
152	NotifierConfigView* view = new NotifierConfigView();
153	view->SetTo(&settings);
154	return view;
155}
156