1/*
2 * Copyright 2010, Haiku Inc. All Rights Reserved.
3 * Copyright 2010 Clemens Zeidler. All rights reserved.
4 *
5 * Distributed under the terms of the MIT License.
6 */
7#ifndef IMAP_STORAGE_H
8#define IMAP_STORAGE_H
9
10
11#include <map>
12#include <string.h>
13
14#include <File.h>
15#include <kernel/OS.h>
16#include <Path.h>
17#include <String.h>
18
19#include "IMAPHandler.h"
20
21
22enum FileFlags {
23	kSeen = 0x01,
24	kAnswered = 0x02,
25	kFlagged = 0x04,
26	kDeleted = 0x08,
27	kDraft = 0x10
28};
29
30
31struct StorageMailEntry {
32	int32		uid;
33	int32		flags;
34	node_ref	nodeRef;
35	BString		fileName;
36};
37
38
39typedef std::map<int32, StorageMailEntry> MailEntryMap;
40
41
42class IMAPStorage {
43public:
44			enum MessageState {
45				kHeaderDownloaded = 0x01,
46				kBodyDownloaded = 0x02
47			};
48
49								IMAPStorage();
50								~IMAPStorage();
51
52			void				SetTo(const char* dir);
53
54			status_t			StartReadDatabase();
55			status_t			WaitForDatabaseRead();
56
57			status_t			AddNewMessage(int32 uid, int32 flags,
58									BPositionIO** file = NULL);
59			status_t			OpenMessage(int32 uid, BPositionIO** file);
60
61			/*! Remove the message from the storage. */
62			status_t			DeleteMessage(int32 uid);
63
64			status_t			SetFlags(int32 uid, int32 flags);
65			int32				GetFlags(int32 uid);
66
67			status_t			SetFileName(int32 uid, const BString& name);
68			status_t			FileRenamed(const entry_ref& from,
69									const entry_ref& to);
70
71	const	MailEntryMap&		GetFiles() { return fMailEntryMap; }
72			bool				HasFile(int32 uid);
73
74			status_t			SetCompleteMessageSize(int32 uid, int32 size);
75
76			bool				BodyFetched(int32 uid);
77
78			StorageMailEntry*	GetEntryForRef(const node_ref& ref);
79			int32				RefToUID(const entry_ref& ref);
80			bool				UIDToRef(int32 uid, entry_ref& ref);
81
82			status_t			ReadUniqueID(BNode& node, int32& uid);
83
84private:
85	static	status_t			_ReadFilesThreadFunction(void* data);
86			status_t			_ReadFiles();
87
88			status_t			_WriteFlags(int32 flags, BNode& node);
89			status_t			_WriteUniqueID(BNode& node, int32 uid);
90
91private:
92			BPath				fMailboxPath;
93
94			sem_id				fLoadDatabaseLock;
95			MailEntryMap		fMailEntryMap;
96};
97
98
99class IMAPMailbox;
100typedef std::vector<int32> MessageNumberList;
101
102class IMAPMailboxSync {
103public:
104			status_t			Sync(IMAPStorage& storage,
105									 IMAPMailbox& mailbox);
106	const	MessageNumberList&	ToFetchList() { return fToFetchList; }
107
108private:
109			MessageNumberList	fToFetchList;
110};
111
112
113#endif // IMAP_STORAGE_H
114