xref: /haiku/src/add-ons/mail_daemon/inbound_protocols/imap/IMAPFolder.h (revision eba458b9407056235aeb184a038e202b2d7bfe5e)
1 /*
2  * Copyright 2012-2013, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef IMAP_FOLDER_H
6 #define IMAP_FOLDER_H
7 
8 
9 #include <hash_map>
10 #include <sys/stat.h>
11 
12 #include <Entry.h>
13 #include <Node.h>
14 #include <Handler.h>
15 #include <String.h>
16 
17 
18 class IMAPProtocol;
19 
20 
21 struct MessageToken {
22 	BString	mailboxName;
23 	uint32	uidValidity;
24 	uint32	uid;
25 };
26 
27 
28 class FolderListener {
29 public:
30 	virtual	uint32				MessageAdded(const MessageToken& fromToken,
31 									const entry_ref& ref) = 0;
32 	virtual void				MessageDeleted(const MessageToken& token) = 0;
33 
34 	virtual void				MessageFlagsChanged(const MessageToken& token,
35 									const entry_ref& ref, uint32 oldFlags,
36 									uint32 newFlags) = 0;
37 };
38 
39 
40 class IMAPFolder : public BHandler {
41 public:
42 								IMAPFolder(IMAPProtocol& protocol,
43 									const BString& mailboxName,
44 									const entry_ref& ref);
45 	virtual						~IMAPFolder();
46 
47 			const BString&		MailboxName() const { return fMailboxName; }
48 
49 			void				SetListener(FolderListener* listener);
50 			void				SetUIDValidity(uint32 uidValidity);
51 
52 			status_t			StoreMessage(BFile& file, uint32 fetchFlags,
53 									BDataIO& stream, size_t& length,
54 									entry_ref& ref);
55 			void				MessageStored(entry_ref& ref, BFile& file,
56 									uint32 fetchFlags, uint32 uid,
57 									uint32 flags);
58 
59 			void				DeleteMessage(uint32 uid);
60 			void				SetMessageFlags(uint32 uid, uint32 flags);
61 
62 	virtual void				MessageReceived(BMessage* message);
63 
64 private:
65 			void				_InitializeFolderState();
66 			const MessageToken	_Token(uint32 uid) const;
67 			uint32				_ReadUniqueID(BNode& node);
68 			status_t			_WriteUniqueID(BNode& node, uint32 uid);
69 			uint32				_ReadFlags(BNode& node);
70 			status_t			_WriteFlags(BNode& node, uint32 flags);
71 
72 private:
73 	typedef std::hash_map<uint32, uint32> UIDToFlagsMap;
74 	typedef std::hash_map<uint32, entry_ref> UIDToRefMap;
75 
76 			IMAPProtocol&		fProtocol;
77 			const entry_ref		fRef;
78 			BString				fMailboxName;
79 			uint32				fUIDValidity;
80 			FolderListener*		fListener;
81 			UIDToRefMap			fRefMap;
82 			UIDToFlagsMap		fUIDMap;
83 };
84 
85 
86 #endif	// IMAP_FOLDER_H
87