xref: /haiku/src/add-ons/mail_daemon/inbound_protocols/imap/IMAPConnectionWorker.h (revision 6d2f2ec177bf615a117a7428d71be4330545b320)
1 /*
2  * Copyright 2011-2013, 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 "Commands.h"
13 #include "Protocol.h"
14 
15 
16 class IMAPFolder;
17 class IMAPMailbox;
18 class IMAPProtocol;
19 class Settings;
20 class WorkerCommand;
21 class WorkerPrivate;
22 
23 
24 typedef BObjectList<WorkerCommand> WorkerCommandList;
25 
26 
27 class IMAPConnectionWorker : public IMAP::ExistsListener,
28 	IMAP::ExpungeListener {
29 public:
30 								IMAPConnectionWorker(IMAPProtocol& owner,
31 									const Settings& settings,
32 									bool main = false);
33 	virtual						~IMAPConnectionWorker();
34 
35 			bool				HasMailboxes() const;
36 			uint32				CountMailboxes() const;
37 			void				AddMailbox(IMAPFolder* folder);
38 			void				RemoveAllMailboxes();
39 
40 			IMAPProtocol&		Owner() const { return fOwner; }
41 			bool				IsMain() const { return fMain; }
42 			bool				UsesIdle() const { return fIdle; }
43 
44 			status_t			Run();
45 			void				Quit();
46 
47 			status_t			EnqueueCheckSubscribedFolders();
48 			status_t			EnqueueCheckMailboxes();
49 			status_t			EnqueueRetrieveMail(entry_ref& ref);
50 
51 	// Handler listener
52 	virtual	void				MessageExistsReceived(uint32 index);
53 	virtual	void				MessageExpungeReceived(uint32 index);
54 
55 private:
56 			status_t			_Worker();
57 			status_t			_EnqueueCommand(WorkerCommand* command);
58 			void				_WaitForCommands();
59 
60 			status_t			_SelectMailbox(IMAPFolder& folder,
61 									uint32* _nextUID);
62 			IMAPMailbox*		_MailboxFor(IMAPFolder& folder);
63 			IMAPFolder*			_Selected() const { return fSelectedBox; }
64 			void				_SyncCommandDone();
65 			uint32				_MessagesExist() const
66 									{ return fMessagesExist; }
67 
68 			status_t			_Connect();
69 			void				_Disconnect();
70 
71 	static	status_t			_Worker(void* self);
72 
73 private:
74 	typedef std::map<IMAPFolder*, IMAPMailbox*> MailboxMap;
75 	friend class WorkerPrivate;
76 
77 			IMAPProtocol&		fOwner;
78 			const Settings&		fSettings;
79 			IMAP::Protocol		fProtocol;
80 			sem_id				fPendingCommandsSemaphore;
81 			WorkerCommandList	fPendingCommands;
82 			uint32				fSyncPending;
83 
84 			IMAP::ExistsHandler	fExistsHandler;
85 			IMAP::ExpungeHandler fExpungeHandler;
86 
87 			IMAPFolder*			fIdleBox;
88 			IMAPFolder*			fSelectedBox;
89 			MailboxMap			fMailboxes;
90 			uint32				fMessagesExist;
91 
92 			BLocker				fLocker;
93 			thread_id			fThread;
94 			bool				fMain;
95 			bool				fStopped;
96 			bool				fIdle;
97 };
98 
99 
100 #endif	// IMAP_CONNECTION_WORKER_H
101