1/*
2 * Copyright 2009-2013, Axel Dörfler, axeld@pinc-software.de.
3 * Copyright 2006, Ryan Leavengood, leavengood@gmail.com. All rights reserved.
4 * Distributed under the terms of the MIT License.
5 */
6
7
8#include <E-mail.h>
9#include <Entry.h>
10#include <MailDaemon.h>
11#include <mail_util.h>
12#include <Message.h>
13#include <Node.h>
14#include <String.h>
15
16
17extern "C" void
18process_refs(entry_ref dir, BMessage* message, void* /*reserved*/)
19{
20	entry_ref ref;
21	for (int i = 0; message->FindRef("refs", i, &ref) == B_OK; i++) {
22		BNode node(&ref);
23		BString type;
24
25		if (node.InitCheck() == B_OK
26			&& node.ReadAttrString("BEOS:TYPE", &type) == B_OK
27			&& (type == B_MAIL_TYPE || type == B_PARTIAL_MAIL_TYPE)) {
28			BString previousStatus;
29			BString status("Read");
30			read_flags previousRead;
31			// if we're marking it via the add-on, we haven't really read it
32			// so use B_SEEN instead of B_READ
33			read_flags read = B_SEEN;
34
35			// Update the MAIL:read status to match
36			// Check both B_SEEN and B_READ
37			// (so we don't overwrite B_READ with B_SEEN)
38			if (read_read_attr(node, previousRead) != B_OK
39				|| (previousRead != B_SEEN && previousRead != B_READ)) {
40				int32 account;
41				if (node.ReadAttr(B_MAIL_ATTR_ACCOUNT_ID, B_INT32_TYPE,
42						0LL, &account, sizeof(account)) == sizeof(account))
43					BMailDaemon().MarkAsRead(account, ref, read);
44				else
45					write_read_attr(node, read);
46			}
47
48			// We want to keep the previous behavior of updating the status
49			// string, but write_read_attr will only change the status string
50			// if it's one of "New", "Seen", or "Read" (and not, for example,
51			// "Replied"), so we change the status string here
52			// Only update the attribute if there is an actual change
53			if (node.ReadAttrString(B_MAIL_ATTR_STATUS, &previousStatus) != B_OK
54				|| previousStatus != status)
55				node.WriteAttrString(B_MAIL_ATTR_STATUS, &status);
56		}
57	}
58}
59