1/*
2 * Copyright 2004-2015, Haiku, Inc. All rights reserved.
3 * Copyright 2001, Dr. Zoidberg Enterprises. All rights reserved.
4 * Copyright 2011, Clemens Zeidler <haiku@clemens-zeidler.de>
5 *
6 * Distributed under the terms of the MIT License.
7 */
8
9
10//!	Notifies incoming e-mail
11
12
13#include <Alert.h>
14#include <Application.h>
15#include <Beep.h>
16#include <Catalog.h>
17#include <Message.h>
18#include <Path.h>
19#include <String.h>
20#include <StringFormat.h>
21
22#include <MailFilter.h>
23
24#include "NotifierConfigView.h"
25
26
27#undef B_TRANSLATION_CONTEXT
28#define B_TRANSLATION_CONTEXT "NotifierFilter"
29
30
31class NotifyFilter : public BMailFilter {
32public:
33								NotifyFilter(BMailProtocol& protocol,
34									const BMailAddOnSettings& settings);
35
36			BMailFilterAction	HeaderFetched(entry_ref& ref,
37									BFile& file, BMessage& attributes);
38			void				MailboxSynchronized(status_t status);
39
40private:
41			int32				fStrategy;
42			int32				fNNewMessages;
43};
44
45
46NotifyFilter::NotifyFilter(BMailProtocol& protocol,
47	const BMailAddOnSettings& settings)
48	:
49	BMailFilter(protocol, &settings),
50	fNNewMessages(0)
51{
52	fStrategy = settings.FindInt32("notification_method");
53}
54
55
56BMailFilterAction
57NotifyFilter::HeaderFetched(entry_ref& ref, BFile& file,
58	BMessage& attributes)
59{
60	// TODO: do not use MAIL:status here!
61	char statusString[256];
62	if (file.ReadAttr("MAIL:status", B_STRING_TYPE, 0, statusString, 256) < 0)
63		return B_NO_MAIL_ACTION;
64	if (BString(statusString).Compare("Read") != 0)
65		fNNewMessages++;
66	return B_NO_MAIL_ACTION;
67}
68
69
70void
71NotifyFilter::MailboxSynchronized(status_t status)
72{
73	if (fNNewMessages == 0)
74		return;
75
76	if ((fStrategy & NOTIFY_BEEP) != 0)
77		system_beep("New E-mail");
78
79	if ((fStrategy & NOTIFY_ALERT) != 0) {
80		BStringFormat format(B_TRANSLATE(
81			"You have {0, plural, one{one new message} other{# new messages}} "
82			"for %account."));
83
84		BString text;
85		format.Format(text, fNNewMessages);
86		text.ReplaceFirst("%account", fMailProtocol.AccountSettings().Name());
87
88		BAlert *alert = new BAlert(B_TRANSLATE("New messages"), text.String(),
89			B_TRANSLATE("OK"), NULL, NULL, B_WIDTH_AS_USUAL);
90		alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
91		alert->SetFeel(B_NORMAL_WINDOW_FEEL);
92		alert->Go(NULL);
93	}
94
95	if ((fStrategy & NOTIFY_BLINK_LEDS) != 0)
96		be_app->PostMessage('mblk');
97
98	if ((fStrategy & NOTIFY_CENTRAL_BEEP) != 0)
99		be_app->PostMessage('mcbp');
100
101	if ((fStrategy & NOTIFY_CENTRAL_ALERT) != 0) {
102		BMessage msg('numg');
103		msg.AddInt32("num_messages", fNNewMessages);
104		msg.AddString("name", fMailProtocol.AccountSettings().Name());
105
106		be_app->PostMessage(&msg);
107	}
108
109	if ((fStrategy & NOTIFY_NOTIFICATION) != 0) {
110		BStringFormat format(B_TRANSLATE("{0, plural, "
111			"one{One new message} other{# new messages}}"));
112
113		BString message;
114		format.Format(message, fNNewMessages);
115		fMailProtocol.ShowMessage(message.String());
116	}
117
118	fNNewMessages = 0;
119}
120
121
122// #pragma mark -
123
124
125BString
126filter_name(const BMailAccountSettings& accountSettings,
127	const BMailAddOnSettings* addOnSettings)
128{
129	return B_TRANSLATE("New mails notification");
130}
131
132
133BMailFilter*
134instantiate_filter(BMailProtocol& protocol, const BMailAddOnSettings& settings)
135{
136	return new NotifyFilter(protocol, settings);
137}
138
139