xref: /haiku/src/add-ons/mail_daemon/inbound_protocols/imap/IMAPConnectionWorker.h (revision 12dba4e70f831d6d27a7f769cc9dab19c19a155d)
1 /*
2  * Copyright 2011-2016, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef IMAP_CONNECTION_WORKER_H
6 #define IMAP_CONNECTION_WORKER_H
7 
8 
9 #include <Locker.h>
10 #include <String.h>
11 
12 #include "Protocol.h"
13 
14 
15 class IMAPFolder;
16 class IMAPMailbox;
17 class IMAPProtocol;
18 class Settings;
19 class WorkerCommand;
20 class WorkerPrivate;
21 
22 typedef BObjectList<WorkerCommand> WorkerCommandList;
23 
24 
25 class IMAPConnectionWorker : public IMAP::ExistsListener,
26 	IMAP::ExpungeListener {
27 public:
28 								IMAPConnectionWorker(IMAPProtocol& owner,
29 									const Settings& settings,
30 									bool main = false);
31 	virtual						~IMAPConnectionWorker();
32 
33 			bool				HasMailboxes() const;
34 			uint32				CountMailboxes() const;
35 			void				AddMailbox(IMAPFolder* folder);
36 			void				RemoveAllMailboxes();
37 
38 			IMAPProtocol&		Owner() const { return fOwner; }
39 			bool				IsMain() const { return fMain; }
40 			bool				UsesIdle() const { return fIdle; }
41 
42 			status_t			Run();
43 			thread_id			Thread() const { return fThread; }
44 			void				Quit();
45 
46 			status_t			EnqueueCheckSubscribedFolders();
47 			status_t			EnqueueCheckMailboxes();
48 			status_t			EnqueueFetchBody(IMAPFolder& folder,
49 									uint32 uid, const BMessenger& replyTo);
50 			status_t			EnqueueUpdateFlags(IMAPFolder& folder,
51 									uint32 uid, uint32 flags);
52 
53 	// Handler listener
54 	virtual	void				MessageExistsReceived(uint32 index);
55 	virtual	void				MessageExpungeReceived(uint32 index);
56 
57 private:
58 			status_t			_Worker();
59 			status_t			_EnqueueCommand(WorkerCommand* command);
60 			void				_WaitForCommands();
61 
62 			status_t			_SelectMailbox(IMAPFolder& folder,
63 									uint32* _nextUID);
64 			IMAPMailbox*		_MailboxFor(IMAPFolder& folder);
65 			IMAPFolder*			_Selected() const { return fSelectedBox; }
66 			void				_SyncCommandDone();
67 			uint32				_MessagesExist() const
68 									{ return fMessagesExist; }
69 			bool				_IsQuitPending();
70 
71 			status_t			_Connect();
72 			void				_Disconnect();
73 
74 	static	status_t			_Worker(void* self);
75 
76 private:
77 	typedef std::map<IMAPFolder*, IMAPMailbox*> MailboxMap;
78 	friend class WorkerPrivate;
79 
80 			IMAPProtocol&		fOwner;
81 			const Settings&		fSettings;
82 			IMAP::Protocol		fProtocol;
83 			sem_id				fPendingCommandsSemaphore;
84 			WorkerCommandList	fPendingCommands;
85 			uint32				fSyncPending;
86 
87 			IMAP::ExistsHandler	fExistsHandler;
88 			IMAP::ExpungeHandler fExpungeHandler;
89 
90 			IMAPFolder*			fIdleBox;
91 			IMAPFolder*			fSelectedBox;
92 			MailboxMap			fMailboxes;
93 			uint32				fMessagesExist;
94 
95 			BLocker				fLocker;
96 			BLocker				fQueueLocker;
97 			thread_id			fThread;
98 			bool				fMain;
99 			bool				fStopped;
100 			bool				fIdle;
101 };
102 
103 
104 #endif	// IMAP_CONNECTION_WORKER_H
105