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 24 class MailNotifier { 25 public: 26 virtual ~MailNotifier() {} 27 28 virtual MailNotifier* Clone() = 0; 29 30 virtual void ShowError(const char* error) = 0; 31 virtual void ShowMessage(const char* message) = 0; 32 33 virtual void SetTotalItems(int32 items) = 0; 34 virtual void SetTotalItemsSize(int32 size) = 0; 35 virtual void ReportProgress(int bytes, int messages, 36 const char* message = NULL) = 0; 37 virtual void ResetProgress(const char* message = NULL) = 0; 38 }; 39 40 41 class MailProtocol; 42 43 44 class MailFilter { 45 public: 46 MailFilter(MailProtocol& protocol, 47 AddonSettings* settings); 48 virtual ~MailFilter(); 49 50 //! Message hooks if filter is installed to a inbound protocol 51 virtual void HeaderFetched(const entry_ref& ref, 52 BFile* file); 53 virtual void BodyFetched(const entry_ref& ref, BFile* file); 54 virtual void MailboxSynced(status_t status); 55 56 //! Message hooks if filter is installed to a outbound protocol 57 virtual void MessageReadyToSend(const entry_ref& ref, 58 BFile* file); 59 virtual void MessageSent(const entry_ref& ref, 60 BFile* file); 61 protected: 62 MailProtocol& fMailProtocol; 63 AddonSettings* fAddonSettings; 64 }; 65 66 67 class MailProtocolThread; 68 69 70 class MailProtocol { 71 public: 72 MailProtocol(BMailAccountSettings* settings); 73 virtual ~MailProtocol(); 74 75 virtual void SetStopNow() {} 76 77 BMailAccountSettings& AccountSettings(); 78 79 void SetProtocolThread( 80 MailProtocolThread* protocolThread); 81 virtual void AddedToLooper() {} 82 MailProtocolThread* Looper(); 83 /*! Add handler to the handler list. The handler is installed / 84 removed to the according BLooper automatically. */ 85 bool AddHandler(BHandler* handler); 86 //! Does not delete handler 87 bool RemoveHandler(BHandler* handler); 88 89 void SetMailNotifier(MailNotifier* mailNotifier); 90 91 virtual void ShowError(const char* error); 92 virtual void ShowMessage(const char* message); 93 virtual void SetTotalItems(int32 items); 94 virtual void SetTotalItemsSize(int32 size); 95 virtual void ReportProgress(int bytes, int messages, 96 const char* message = NULL); 97 virtual void ResetProgress(const char* message = NULL); 98 99 //! MailProtocol takes ownership of the filters 100 bool AddFilter(MailFilter* filter); 101 int32 CountFilter(); 102 MailFilter* FilterAt(int32 index); 103 MailFilter* RemoveFilter(int32 index); 104 bool RemoveFilter(MailFilter* filter); 105 106 void NotifyNewMessagesToFetch(int32 nMessages); 107 void NotifyHeaderFetched(const entry_ref& ref, 108 BFile* mail); 109 void NotifyBodyFetched(const entry_ref& ref, 110 BFile* mail); 111 void NotifyMessageReadyToSend(const entry_ref& ref, 112 BFile* mail); 113 void NotifyMessageSent(const entry_ref& ref, 114 BFile* mail); 115 116 //! mail storage operations 117 virtual status_t MoveMessage(const entry_ref& ref, 118 BDirectory& dir); 119 virtual status_t DeleteMessage(const entry_ref& ref); 120 121 virtual void FileRenamed(const entry_ref& from, 122 const entry_ref& to); 123 virtual void FileDeleted(const node_ref& node); 124 125 protected: 126 void LoadFilters(MailAddonSettings& settings); 127 128 BMailAccountSettings fAccountSettings; 129 MailNotifier* fMailNotifier; 130 131 private: 132 MailFilter* _LoadFilter(AddonSettings* filterSettings); 133 134 MailProtocolThread* fProtocolThread; 135 BObjectList<BHandler> fHandlerList; 136 BObjectList<MailFilter> fFilterList; 137 std::map<entry_ref, image_id> fFilterImages; 138 }; 139 140 141 class InboundProtocol : public MailProtocol { 142 public: 143 InboundProtocol(BMailAccountSettings* settings); 144 virtual ~InboundProtocol(); 145 146 virtual status_t SyncMessages() = 0; 147 virtual status_t FetchBody(const entry_ref& ref) = 0; 148 virtual status_t MarkMessageAsRead(const entry_ref& ref, 149 read_flags flag = B_READ); 150 virtual status_t DeleteMessage(const entry_ref& ref) = 0; 151 virtual status_t AppendMessage(const entry_ref& ref); 152 }; 153 154 155 class OutboundProtocol : public MailProtocol { 156 public: 157 OutboundProtocol( 158 BMailAccountSettings* settings); 159 virtual ~OutboundProtocol(); 160 161 virtual status_t SendMessages(const std::vector<entry_ref>& 162 mails, size_t totalBytes) = 0; 163 }; 164 165 166 class MailProtocolThread : public BLooper { 167 public: 168 MailProtocolThread(MailProtocol* protocol); 169 virtual void MessageReceived(BMessage* message); 170 171 MailProtocol* Protocol() { return fMailProtocol; } 172 173 void SetStopNow(); 174 /*! These function post a message to the loop to trigger the action. 175 */ 176 void TriggerFileMove(const entry_ref& ref, 177 BDirectory& dir); 178 void TriggerFileDeletion(const entry_ref& ref); 179 180 void TriggerFileRenamed(const entry_ref& from, 181 const entry_ref& to); 182 void TriggerFileDeleted(const node_ref& node); 183 private: 184 MailProtocol* fMailProtocol; 185 }; 186 187 188 class InboundProtocolThread : public MailProtocolThread { 189 public: 190 InboundProtocolThread( 191 InboundProtocol* protocol); 192 ~InboundProtocolThread(); 193 194 void MessageReceived(BMessage* message); 195 196 void SyncMessages(); 197 void FetchBody(const entry_ref& ref, 198 BMessenger* listener = NULL); 199 void MarkMessageAsRead(const entry_ref& ref, 200 read_flags flag = B_READ); 201 void DeleteMessage(const entry_ref& ref); 202 void AppendMessage(const entry_ref& ref); 203 private: 204 void _NotiyMailboxSynced(status_t status); 205 206 InboundProtocol* fProtocol; 207 }; 208 209 210 class OutboundProtocolThread : public MailProtocolThread { 211 public: 212 OutboundProtocolThread( 213 OutboundProtocol* protocol); 214 ~OutboundProtocolThread(); 215 216 void MessageReceived(BMessage* message); 217 218 void SendMessages(const std::vector<entry_ref>& 219 mails, size_t totalBytes); 220 221 private: 222 OutboundProtocol* fProtocol; 223 }; 224 225 226 #endif // MAIL_PROTOCOL_H 227