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