xref: /haiku/src/add-ons/mail_daemon/inbound_protocols/imap/IMAPConnectionWorker.h (revision 44d19f4d32b8f7e9c01f00294c87ca5cc2e057f7)
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 			void				Quit();
44 
45 			status_t			EnqueueCheckSubscribedFolders();
46 			status_t			EnqueueCheckMailboxes();
47 			status_t			EnqueueFetchBody(IMAPFolder& folder,
48 									uint32 uid, const BMessenger& replyTo);
49 			status_t			EnqueueUpdateFlags(IMAPFolder& folder,
50 									uint32 uid, uint32 flags);
51 
52 	// Handler listener
53 	virtual	void				MessageExistsReceived(uint32 index);
54 	virtual	void				MessageExpungeReceived(uint32 index);
55 
56 private:
57 			status_t			_Worker();
58 			status_t			_EnqueueCommand(WorkerCommand* command);
59 			void				_WaitForCommands();
60 
61 			status_t			_SelectMailbox(IMAPFolder& folder,
62 									uint32* _nextUID);
63 			IMAPMailbox*		_MailboxFor(IMAPFolder& folder);
64 			IMAPFolder*			_Selected() const { return fSelectedBox; }
65 			void				_SyncCommandDone();
66 			uint32				_MessagesExist() const
67 									{ return fMessagesExist; }
68 
69 			status_t			_Connect();
70 			void				_Disconnect();
71 
72 	static	status_t			_Worker(void* self);
73 
74 private:
75 	typedef std::map<IMAPFolder*, IMAPMailbox*> MailboxMap;
76 	friend class WorkerPrivate;
77 
78 			IMAPProtocol&		fOwner;
79 			const Settings&		fSettings;
80 			IMAP::Protocol		fProtocol;
81 			sem_id				fPendingCommandsSemaphore;
82 			WorkerCommandList	fPendingCommands;
83 			uint32				fSyncPending;
84 
85 			IMAP::ExistsHandler	fExistsHandler;
86 			IMAP::ExpungeHandler fExpungeHandler;
87 
88 			IMAPFolder*			fIdleBox;
89 			IMAPFolder*			fSelectedBox;
90 			MailboxMap			fMailboxes;
91 			uint32				fMessagesExist;
92 
93 			BLocker				fLocker;
94 			thread_id			fThread;
95 			bool				fMain;
96 			bool				fStopped;
97 			bool				fIdle;
98 };
99 
100 
101 #endif	// IMAP_CONNECTION_WORKER_H
102