MailProtocol.h revision f8b5e17a
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
46class BMailProtocol : public BLooper {
47public:
48								BMailProtocol(
49									const BMailAccountSettings& settings);
50	virtual						~BMailProtocol();
51
52			const BMailAccountSettings& AccountSettings() const;
53
54			void				SetMailNotifier(BMailNotifier* mailNotifier);
55			BMailNotifier*		MailNotifier() const;
56
57			//! We take ownership of the filters
58			bool				AddFilter(BMailFilter* filter);
59			int32				CountFilter() const;
60			BMailFilter*		FilterAt(int32 index) const;
61			BMailFilter*		RemoveFilter(int32 index);
62			bool				RemoveFilter(BMailFilter* filter);
63
64	virtual void				MessageReceived(BMessage* message);
65
66			// Mail storage operations
67	virtual	status_t			MoveMessage(const entry_ref& ref,
68									BDirectory& dir);
69	virtual	status_t			DeleteMessage(const entry_ref& ref);
70
71	virtual	void				FileRenamed(const entry_ref& from,
72									const entry_ref& to);
73	virtual	void				FileDeleted(const node_ref& node);
74
75			// Convenience methods that call the BMailNotifier
76			void				ShowError(const char* error);
77			void				ShowMessage(const char* message);
78
79#if __GNUC__ > 2
80			// Unhide virtual base methods
81			using BHandler::AddFilter;
82			using BHandler::RemoveFilter;
83#endif
84
85protected:
86		 	void				SetTotalItems(uint32 items);
87			void				SetTotalItemsSize(uint64 size);
88			void				ReportProgress(uint32 items, uint64 bytes,
89									const char* message = NULL);
90			void				ResetProgress(const char* message = NULL);
91
92			// Filter notifications
93			void				NotifyNewMessagesToFetch(int32 nMessages);
94			void				NotifyHeaderFetched(const entry_ref& ref,
95									BFile* mail);
96			void				NotifyBodyFetched(const entry_ref& ref,
97									BFile* mail);
98			void				NotifyMessageReadyToSend(const entry_ref& ref,
99									BFile* mail);
100			void				NotifyMessageSent(const entry_ref& ref,
101									BFile* mail);
102
103			void				LoadFilters(
104									const BMailProtocolSettings& settings);
105
106private:
107			BMailFilter*		_LoadFilter(const BMailAddOnSettings& settings);
108
109protected:
110			const BMailAccountSettings fAccountSettings;
111			BMailNotifier*		fMailNotifier;
112
113private:
114			BObjectList<BMailFilter> fFilterList;
115			std::map<entry_ref, image_id> fFilterImages;
116};
117
118
119class BInboundMailProtocol : public BMailProtocol {
120public:
121								BInboundMailProtocol(
122									const BMailAccountSettings& settings);
123	virtual						~BInboundMailProtocol();
124
125	virtual void				MessageReceived(BMessage* message);
126
127	virtual	status_t			SyncMessages() = 0;
128	virtual status_t			FetchBody(const entry_ref& ref) = 0;
129	virtual	status_t			MarkMessageAsRead(const entry_ref& ref,
130									read_flags flags = B_READ);
131	virtual	status_t			DeleteMessage(const entry_ref& ref) = 0;
132	virtual	status_t			AppendMessage(const entry_ref& ref);
133
134protected:
135			void				NotiyMailboxSynchronized(status_t status);
136};
137
138
139class BOutboundMailProtocol : public BMailProtocol {
140public:
141								BOutboundMailProtocol(
142									const BMailAccountSettings& settings);
143	virtual						~BOutboundMailProtocol();
144
145	virtual void				MessageReceived(BMessage* message);
146
147	virtual	status_t			SendMessages(const BMessage& message,
148									off_t totalBytes) = 0;
149};
150
151
152// Your protocol needs to export these hooks in order to be picked up
153extern "C" BInboundMailProtocol* instantiate_inbound_protocol(
154	const BMailAccountSettings& settings);
155extern "C" BOutboundMailProtocol* instantiate_outbound_protocol(
156	const BMailAccountSettings& settings);
157
158extern "C" BMailSettingsView* instantiate_protocol_settings_view(
159	const BMailAccountSettings& accountSettings,
160	const BMailProtocolSettings& settings);
161
162
163#endif	// _MAIL_PROTOCOL_H
164