xref: /haiku/src/add-ons/mail_daemon/inbound_protocols/imap/IMAPConnectionWorker.h (revision 29e5da6f2070ba3c177a8383e9ad3e1c4cdcbd0d)
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 
66 			status_t			_Connect();
67 			void				_Disconnect();
68 
69 	static	status_t			_Worker(void* self);
70 
71 private:
72 	typedef std::map<IMAPFolder*, IMAPMailbox*> MailboxMap;
73 	friend class WorkerPrivate;
74 
75 			IMAPProtocol&		fOwner;
76 			const Settings&		fSettings;
77 			IMAP::Protocol		fProtocol;
78 			sem_id				fPendingCommandsSemaphore;
79 			WorkerCommandList	fPendingCommands;
80 			uint32				fSyncPending;
81 
82 			IMAP::ExistsHandler	fExistsHandler;
83 			IMAP::ExpungeHandler fExpungeHandler;
84 
85 			IMAPFolder*			fIdleBox;
86 			IMAPFolder*			fSelectedBox;
87 			MailboxMap			fMailboxes;
88 
89 			BLocker				fLocker;
90 			thread_id			fThread;
91 			bool				fMain;
92 			bool				fStopped;
93 			bool				fIdle;
94 };
95 
96 
97 #endif	// IMAP_CONNECTION_WORKER_H
98