xref: /haiku/src/add-ons/mail_daemon/inbound_protocols/imap/IMAPFolder.h (revision 644fa5a93845dc4a1bc155f1fd0f94ebdf0b47bc)
1 /*
2  * Copyright 2012-2015, 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 BFile;
19 class IMAPProtocol;
20 
21 
22 struct MessageToken {
23 	BString	mailboxName;
24 	uint32	uidValidity;
25 	uint32	uid;
26 };
27 
28 // Additional local only message flags
29 enum FolderMessageFlags {
30 	kPartialMessage = 0x00010000,
31 };
32 
33 
34 class FolderListener {
35 public:
36 	virtual	uint32				MessageAdded(const MessageToken& fromToken,
37 									const entry_ref& ref) = 0;
38 	virtual void				MessageDeleted(const MessageToken& token) = 0;
39 
40 	virtual void				MessageFlagsChanged(const MessageToken& token,
41 									const entry_ref& ref, uint32 oldFlags,
42 									uint32 newFlags) = 0;
43 };
44 
45 
46 class IMAPFolder : public BHandler {
47 public:
48 								IMAPFolder(IMAPProtocol& protocol,
49 									const BString& mailboxName,
50 									const entry_ref& ref);
51 	virtual						~IMAPFolder();
52 
53 			const BString&		MailboxName() const { return fMailboxName; }
54 
55 			void				SetListener(FolderListener* listener);
56 			void				SetUIDValidity(uint32 uidValidity);
57 
58 			uint32				LastUID() const { return fLastUID; }
59 
60 			status_t			GetMessageEntryRef(uint32 uid,
61 									entry_ref& ref) const;
62 			uint32				MessageFlags(uint32 uid) const;
63 
64 			status_t			StoreMessage(uint32 fetchFlags, BDataIO& stream,
65 									size_t& length, entry_ref& ref,
66 									BFile& file);
67 			void				MessageStored(entry_ref& ref, BFile& file,
68 									uint32 fetchFlags, uint32 uid,
69 									uint32 flags);
70 
71 			status_t			StoreBody(uint32 uid, BDataIO& stream,
72 									size_t& length, entry_ref& ref,
73 									BFile& file);
74 			void				BodyStored(entry_ref& ref, BFile& file,
75 									uint32 uid);
76 
77 			void				DeleteMessage(uint32 uid);
78 			void				SetMessageFlags(uint32 uid, uint32 flags);
79 
80 	virtual void				MessageReceived(BMessage* message);
81 
82 private:
83 			void				_InitializeFolderState();
84 			const MessageToken	_Token(uint32 uid) const;
85 			uint32				_ReadUniqueID(BNode& node);
86 			status_t			_WriteUniqueID(BNode& node, uint32 uid);
87 			uint32				_ReadFlags(BNode& node);
88 			status_t			_WriteFlags(BNode& node, uint32 flags);
89 
90 			uint32				_ReadUInt32(BNode& node, const char* attribute);
91 			status_t			_WriteUInt32(BNode& node,
92 									const char* attribute, uint32 value);
93 
94 			status_t			_WriteStream(BFile& file, BDataIO& stream,
95 									size_t& length);
96 
97 private:
98 #if __GNUC__ >= 4
99 	typedef __gnu_cxx::hash_map<uint32, uint32> UIDToFlagsMap;
100 	typedef __gnu_cxx::hash_map<uint32, entry_ref> UIDToRefMap;
101 #else
102 	typedef std::hash_map<uint32, uint32> UIDToFlagsMap;
103 	typedef std::hash_map<uint32, entry_ref> UIDToRefMap;
104 #endif
105 
106 			IMAPProtocol&		fProtocol;
107 			const entry_ref		fRef;
108 			BString				fMailboxName;
109 			uint32				fUIDValidity;
110 			uint32				fLastUID;
111 			FolderListener*		fListener;
112 			UIDToRefMap			fRefMap;
113 			UIDToFlagsMap		fFlagsMap;
114 };
115 
116 
117 #endif	// IMAP_FOLDER_H
118