MailProtocol.h revision 549949b2
1/*
2 * Copyright 2004-2013, Haiku, Inc. All Rights Reserved.
3 * Copyright 2001 Dr. Zoidberg Enterprises. All rights reserved.
4 * Copyright 2011 Clemens Zeidler. All rights reserved.
5 *
6 * Distributed under the terms of the MIT License.
7 */
8#ifndef _MAIL_PROTOCOL_H
9#define _MAIL_PROTOCOL_H
10
11
12#include <map>
13
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;
25class BMailSettingsView;
26class BView;
27
28
29class BMailNotifier {
30public:
31	virtual						~BMailNotifier() {}
32
33	virtual BMailNotifier*		Clone() = 0;
34
35	virtual	void				ShowError(const char* error) = 0;
36	virtual	void				ShowMessage(const char* message) = 0;
37
38	virtual void				SetTotalItems(uint32 items) = 0;
39	virtual void				SetTotalItemsSize(uint64 size) = 0;
40	virtual	void				ReportProgress(uint32 items, uint64 bytes,
41									const char* message = NULL) = 0;
42	virtual void				ResetProgress(const char* message = NULL) = 0;
43};
44
45
46typedef status_t BMailFilterAction;
47
48#define B_NO_MAIL_ACTION		0
49#define B_MOVE_MAIL_ACTION		1
50#define B_DELETE_MAIL_ACTION	2
51
52
53class BMailProtocol : public BLooper {
54public:
55								BMailProtocol(
56									const BMailAccountSettings& settings);
57	virtual						~BMailProtocol();
58
59			const BMailAccountSettings& AccountSettings() const;
60
61			void				SetMailNotifier(BMailNotifier* mailNotifier);
62			BMailNotifier*		MailNotifier() const;
63
64			//! We take ownership of the filters
65			bool				AddFilter(BMailFilter* filter);
66			int32				CountFilter() const;
67			BMailFilter*		FilterAt(int32 index) const;
68			BMailFilter*		RemoveFilter(int32 index);
69			bool				RemoveFilter(BMailFilter* filter);
70
71	virtual void				MessageReceived(BMessage* message);
72
73			// Mail storage operations
74	virtual	status_t			MoveMessage(const entry_ref& ref,
75									BDirectory& dir);
76	virtual	status_t			DeleteMessage(const entry_ref& ref);
77
78			// Convenience methods that call the BMailNotifier
79			void				ShowError(const char* error);
80			void				ShowMessage(const char* message);
81
82#if __GNUC__ > 2
83			// Unhide virtual base methods
84			using BHandler::AddFilter;
85			using BHandler::RemoveFilter;
86#endif
87
88protected:
89		 	void				SetTotalItems(uint32 items);
90			void				SetTotalItemsSize(uint64 size);
91			void				ReportProgress(uint32 items, uint64 bytes,
92									const char* message = NULL);
93			void				ResetProgress(const char* message = NULL);
94			void				NotifyNewMessagesToFetch(int32 nMessages);
95
96			// Filter notifications
97			BMailFilterAction	ProcessHeaderFetched(entry_ref& ref,
98									BFile& mail);
99			void				NotifyBodyFetched(const entry_ref& ref,
100									BFile& mail);
101			void				NotifyMessageReadyToSend(const entry_ref& ref,
102									BFile& mail);
103			void				NotifyMessageSent(const entry_ref& ref,
104									BFile& mail);
105
106			void				LoadFilters(
107									const BMailProtocolSettings& settings);
108
109private:
110			BMailFilter*		_LoadFilter(const BMailAddOnSettings& settings);
111
112protected:
113			const BMailAccountSettings fAccountSettings;
114			BMailNotifier*		fMailNotifier;
115
116private:
117			BObjectList<BMailFilter> fFilterList;
118			std::map<entry_ref, image_id> fFilterImages;
119};
120
121
122class BInboundMailProtocol : public BMailProtocol {
123public:
124								BInboundMailProtocol(
125									const BMailAccountSettings& settings);
126	virtual						~BInboundMailProtocol();
127
128	virtual void				MessageReceived(BMessage* message);
129
130	virtual	status_t			SyncMessages() = 0;
131	virtual status_t			FetchBody(const entry_ref& ref) = 0;
132	virtual	status_t			MarkMessageAsRead(const entry_ref& ref,
133									read_flags flags = B_READ);
134	virtual	status_t			DeleteMessage(const entry_ref& ref) = 0;
135	virtual	status_t			AppendMessage(const entry_ref& ref);
136
137protected:
138			void				NotiyMailboxSynchronized(status_t status);
139};
140
141
142class BOutboundMailProtocol : public BMailProtocol {
143public:
144								BOutboundMailProtocol(
145									const BMailAccountSettings& settings);
146	virtual						~BOutboundMailProtocol();
147
148	virtual void				MessageReceived(BMessage* message);
149
150	virtual	status_t			SendMessages(const BMessage& message,
151									off_t totalBytes) = 0;
152};
153
154
155// Your protocol needs to export these hooks in order to be picked up
156extern "C" BInboundMailProtocol* instantiate_inbound_protocol(
157	const BMailAccountSettings& settings);
158extern "C" BOutboundMailProtocol* instantiate_outbound_protocol(
159	const BMailAccountSettings& settings);
160
161extern "C" BMailSettingsView* instantiate_protocol_settings_view(
162	const BMailAccountSettings& accountSettings,
163	const BMailProtocolSettings& settings);
164
165
166#endif	// _MAIL_PROTOCOL_H
167