MailProtocol.h revision 715bf3d1
1/* Protocol - the base class for protocol filters
2 *
3 * Copyright 2001 Dr. Zoidberg Enterprises. All rights reserved.
4 * Copyright 2011 Clemens Zeidler. All rights reserved.
5*/
6#ifndef MAIL_PROTOCOL_H
7#define MAIL_PROTOCOL_H
8
9
10#include <map>
11#include <vector>
12
13#include <Handler.h>
14#include <Looper.h>
15#include <OS.h>
16#include <ObjectList.h>
17#include <Entry.h>
18#include <File.h>
19
20#include <E-mail.h>
21#include <MailSettings.h>
22
23
24class BMailFilter;
25
26
27class BMailNotifier {
28public:
29	virtual						~BMailNotifier() {}
30
31	virtual BMailNotifier*		Clone() = 0;
32
33	virtual	void				ShowError(const char* error) = 0;
34	virtual	void				ShowMessage(const char* message) = 0;
35
36	virtual void				SetTotalItems(uint32 items) = 0;
37	virtual void				SetTotalItemsSize(uint64 size) = 0;
38	virtual	void				ReportProgress(uint32 messages, uint64 bytes,
39									const char* message = NULL) = 0;
40	virtual void				ResetProgress(const char* message = NULL) = 0;
41};
42
43
44class BMailProtocol : BLooper {
45public:
46								BMailProtocol(
47									const BMailAccountSettings& settings);
48	virtual						~BMailProtocol();
49
50			const BMailAccountSettings& AccountSettings() const;
51
52			void				SetMailNotifier(BMailNotifier* mailNotifier);
53			BMailNotifier*		MailNotifier() const;
54
55			//! We take ownership of the filters
56			bool				AddFilter(BMailFilter* filter);
57			int32				CountFilter() const;
58			BMailFilter*		FilterAt(int32 index) const;
59			BMailFilter*		RemoveFilter(int32 index);
60			bool				RemoveFilter(BMailFilter* filter);
61
62	virtual void				MessageReceived(BMessage* message);
63
64			// Mail storage operations
65	virtual	status_t			MoveMessage(const entry_ref& ref,
66									BDirectory& dir);
67	virtual	status_t			DeleteMessage(const entry_ref& ref);
68
69	virtual	void				FileRenamed(const entry_ref& from,
70									const entry_ref& to);
71	virtual	void				FileDeleted(const node_ref& node);
72
73			// Convenience methods that call the BMailNotifier
74			void				ShowError(const char* error);
75			void				ShowMessage(const char* message);
76
77protected:
78		 	void				SetTotalItems(uint32 items);
79			void				SetTotalItemsSize(uint64 size);
80			void				ReportProgress(uint32 messages, uint64 bytes,
81									const char* message = NULL);
82			void				ResetProgress(const char* message = NULL);
83
84			// Filter notifications
85			void				NotifyNewMessagesToFetch(int32 nMessages);
86			void				NotifyHeaderFetched(const entry_ref& ref,
87									BFile* mail);
88			void				NotifyBodyFetched(const entry_ref& ref,
89									BFile* mail);
90			void				NotifyMessageReadyToSend(const entry_ref& ref,
91									BFile* mail);
92			void				NotifyMessageSent(const entry_ref& ref,
93									BFile* mail);
94
95			void				LoadFilters(
96									const BMailProtocolSettings& settings);
97
98private:
99			BMailFilter*		_LoadFilter(BMailAddOnSettings* filterSettings);
100
101protected:
102			const BMailAccountSettings fAccountSettings;
103			BMailNotifier*		fMailNotifier;
104
105private:
106			BObjectList<BMailFilter> fFilterList;
107			std::map<entry_ref, image_id> fFilterImages;
108};
109
110
111class BInboundMailProtocol : public BMailProtocol {
112public:
113								BInboundMailProtocol(
114									const BMailAccountSettings& settings);
115	virtual						~BInboundMailProtocol();
116
117	virtual void				MessageReceived(BMessage* message);
118
119	virtual	status_t			SyncMessages() = 0;
120	virtual status_t			FetchBody(const entry_ref& ref) = 0;
121	virtual	status_t			MarkMessageAsRead(const entry_ref& ref,
122									read_flags flag = B_READ);
123	virtual	status_t			DeleteMessage(const entry_ref& ref) = 0;
124	virtual	status_t			AppendMessage(const entry_ref& ref);
125
126protected:
127			void				NotiyMailboxSynchronized(status_t status);
128};
129
130
131class BOutboundMailProtocol : public BMailProtocol {
132public:
133								BOutboundMailProtocol(
134									const BMailAccountSettings& settings);
135	virtual						~BOutboundMailProtocol();
136
137	virtual void				MessageReceived(BMessage* message);
138
139	virtual	status_t			SendMessages(
140									const std::vector<entry_ref>& mails,
141									size_t totalBytes) = 0;
142};
143
144
145// Your protocol needs to export these hooks in order to be picked up
146extern "C" _EXPORT BInboundMailProtocol* instantiate_inbound_protocol(
147	const BMailAccountSettings& settings);
148extern "C" _EXPORT BOutboundMailProtocol* instantiate_outbound_protocol(
149	const BMailAccountSettings& settings);
150extern "C" _EXPORT BView* instantiate_protocol_config_panel(
151	BMailAccountSettings& settings);
152
153
154#endif // MAIL_PROTOCOL_H
155