xref: /haiku/src/apps/mail/Content.h (revision 5e96d7d537fbec23bad4ae9b4c8e7b02e769f0c6)
1 /*
2 Open Tracker License
3 
4 Terms and Conditions
5 
6 Copyright (c) 1991-2001, Be Incorporated. All rights reserved.
7 
8 Permission is hereby granted, free of charge, to any person obtaining a copy of
9 this software and associated documentation files (the "Software"), to deal in
10 the Software without restriction, including without limitation the rights to
11 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12 of the Software, and to permit persons to whom the Software is furnished to do
13 so, subject to the following conditions:
14 
15 The above copyright notice and this permission notice applies to all licensees
16 and shall be included in all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 
25 Except as contained in this notice, the name of Be Incorporated shall not be
26 used in advertising or otherwise to promote the sale, use or other dealings in
27 this Software without prior written authorization from Be Incorporated.
28 
29 BeMail(TM), Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30 of Be Incorporated in the United States and other countries. Other brand product
31 names are registered trademarks or trademarks of their respective holders.
32 All rights reserved.
33 */
34 #ifndef _CONTENT_H
35 #define _CONTENT_H
36 
37 #include <FilePanel.h>
38 #include <FindDirectory.h>
39 #include <Font.h>
40 #include <fs_attr.h>
41 #include <Point.h>
42 #include <Rect.h>
43 #include <MessageFilter.h>
44 
45 #include "KUndoBuffer.h"
46 
47 #define MESSAGE_TEXT		"Message:"
48 #define MESSAGE_TEXT_H		 16
49 #define MESSAGE_TEXT_V		 5
50 #define MESSAGE_FIELD_H		 59
51 #define MESSAGE_FIELD_V		 11
52 
53 #define CONTENT_TYPE		"content-type: "
54 #define CONTENT_ENCODING	"content-transfer-encoding: "
55 #define CONTENT_DISPOSITION	"Content-Disposition: "
56 #define MIME_TEXT			"text/"
57 #define MIME_MULTIPART		"multipart/"
58 
59 class TMailWindow;
60 class TScrollView;
61 class TTextView;
62 class BFile;
63 class BList;
64 class BPopupMenu;
65 
66 struct text_run_array;
67 
68 typedef struct {
69 	bool header;
70 	bool raw;
71 	bool quote;
72 	bool incoming;
73 	bool close;
74 	bool mime;
75 	TTextView *view;
76 	BEmailMessage *mail;
77 	BList *enclosures;
78 	sem_id *stop_sem;
79 } reader_info;
80 
81 enum ENCLOSURE_TYPE {
82 	TYPE_ENCLOSURE = 100,
83 	TYPE_BE_ENCLOSURE,
84 	TYPE_URL,
85 	TYPE_MAILTO
86 };
87 
88 struct hyper_text {
89 	int32 type;
90 	char *name;
91 	char *content_type;
92 	char *encoding;
93 	int32 text_start;
94 	int32 text_end;
95 	BMailComponent *component;
96 	bool saved;
97 	bool have_ref;
98 	entry_ref ref;
99 	node_ref node;
100 };
101 
102 class TSavePanel;
103 
104 
105 //====================================================================
106 
107 class TContentView : public BView {
108 	public:
109 		TContentView(BRect, bool incoming, BFont*,
110 			bool showHeader, bool coloredQuotes);
111 		virtual void MessageReceived(BMessage *);
112 		void FindString(const char *);
113 		void Focus(bool);
114 		void FrameResized(float, float);
115 
116 		TTextView *fTextView;
117 
118 	private:
119 		bool	fFocus;
120 		bool	fIncoming;
121 		float	fOffset;
122 };
123 
124 //====================================================================
125 
126 enum {
127 	S_CLEAR_ERRORS = 1,
128 	S_SHOW_ERRORS = 2
129 };
130 
131 struct quote_context {
132 	quote_context()
133 		:
134 		level(0),
135 		diff_mode(0),
136 		begin(true),
137 		in_diff(false),
138 		was_diff(false)
139 	{
140 	}
141 
142 	int32	level;
143 	int32	diff_mode;
144 	bool	begin;
145 	bool	in_diff;
146 	bool	was_diff;
147 };
148 
149 class TTextView : public BTextView {
150 	public:
151 								TTextView(BRect, BRect, bool incoming,
152 									TContentView*, BFont*, bool showHeader,
153 									bool coloredQuotes);
154 								~TTextView();
155 
156 		virtual	void AttachedToWindow();
157 		virtual void KeyDown(const char*, int32);
158 		virtual void MakeFocus(bool);
159 		virtual void MessageReceived(BMessage*);
160 		virtual void MouseDown(BPoint);
161 		virtual void MouseMoved(BPoint, uint32, const BMessage*);
162 		virtual void InsertText(const char *text, int32 length, int32 offset,
163 			const text_run_array *runs);
164 		virtual void  DeleteText(int32 start, int32 finish);
165 
166 		void ClearList();
167 		void LoadMessage(BEmailMessage *mail, bool quoteIt, const char *insertText);
168 		void Open(hyper_text*);
169 		status_t Save(BMessage *, bool makeNewFile = true);
170 		void StopLoad();
171 		bool IsReaderThreadRunning();
172 		void AddAsContent(BEmailMessage *mail, bool wrap, uint32 charset, mail_encoding encoding);
173 		void CheckSpelling(int32 start, int32 end,
174 			int32 flags = S_CLEAR_ERRORS | S_SHOW_ERRORS);
175 		void FindSpellBoundry(int32 length, int32 offset, int32 *start,
176 			int32 *end);
177 		void EnableSpellCheck(bool enable);
178 
179 		void AddQuote(int32 start, int32 finish);
180 		void RemoveQuote(int32 start, int32 finish);
181 		void UpdateFont(const BFont* newFont);
182 
183 		void	WindowActivated(bool flag);
184 		void	Undo(BClipboard *clipboard);
185 		void	Redo();
186 
187 		const BFont *Font() const { return &fFont; }
188 
189 		bool fHeader;
190 		bool fColoredQuotes;
191 		bool fReady;
192 
193 	private:
194 		struct { bool replaced, deleted; } fUndoState;
195 		KUndoBuffer	fUndoBuffer;
196 
197 		struct { bool active, replace; } fInputMethodUndoState;
198 		KUndoBuffer	fInputMethodUndoBuffer;
199 			// For handling Input Method changes in undo.
200 
201 		struct spell_mark;
202 
203 		spell_mark *FindSpellMark(int32 start, int32 end, spell_mark **_previousMark = NULL);
204 		void UpdateSpellMarks(int32 offset, int32 length);
205 		status_t AddSpellMark(int32 start, int32 end);
206 		bool RemoveSpellMark(int32 start, int32 end);
207 		void RemoveSpellMarks();
208 
209 		void ContentChanged(void);
210 
211 		int32 LineStart(int32 offset);
212 		int32 PreviousByte(int32 offset) const;
213 
214 		class Reader;
215 		friend class TTextView::Reader;
216 
217 		char *fYankBuffer;
218 		int32 fLastPosition;
219 		BFile *fFile;
220 		BEmailMessage *fMail;
221 			// for incoming/replied/forwarded mails only
222 		BFont fFont;
223 		TContentView *fParent;
224 		sem_id fStopSem;
225 		bool fStopLoading;
226 		thread_id fThread;
227 		BList *fEnclosures;
228 		BPopUpMenu *fEnclosureMenu;
229 		BPopUpMenu *fLinkMenu;
230 		TSavePanel *fPanel;
231 		bool fIncoming;
232 		bool fSpellCheck;
233 		bool fRaw;
234 		bool fCursor;
235 
236 		struct spell_mark {
237 			spell_mark *next;
238 			int32	start;
239 			int32	end;
240 			struct text_run_array *style;
241 		};
242 
243 		spell_mark *fFirstSpellMark;
244 
245 		class Reader {
246 			public:
247 				Reader(bool header, bool raw, bool quote, bool incoming,
248 					bool stripHeaders, bool mime, TTextView* view,
249 					BEmailMessage* mail, BList* list, sem_id sem);
250 
251 				static status_t Run(void* _dummy);
252 
253 			private:
254 				bool ParseMail(BMailContainer* container,
255 					BTextMailComponent* ignore);
256 				bool Process(const char* data, int32 length,
257 					bool isHeader = false);
258 				bool Insert(const char* line, int32 count, bool isHyperLink,
259 					bool isHeader = false);
260 
261 				bool Lock();
262 				status_t Unlock();
263 
264 				bool fHeader;
265 				bool fRaw;
266 				bool fQuote;
267 				quote_context fQuoteContext;
268 				bool fIncoming;
269 				bool fStripHeader;
270 				bool fMime;
271 				TTextView* fView;
272 				BEmailMessage* fMail;
273 				BList *fEnclosures;
274 				sem_id fStopSem;
275 		};
276 };
277 
278 
279 //====================================================================
280 
281 class TSavePanel : public BFilePanel {
282 	public:
283 		TSavePanel(hyper_text*, TTextView*);
284 		virtual void SendMessage(const BMessenger*, BMessage*);
285 		void SetEnclosure(hyper_text*);
286 
287 	private:
288 		hyper_text *fEnclosure;
289 		TTextView *fView;
290 };
291 
292 //====================================================================
293 
294 class TextRunArray {
295 	public:
296 		TextRunArray(size_t entries);
297 		~TextRunArray();
298 
299 		text_run_array &Array() { return *fArray; }
300 		size_t MaxEntries() const { return fNumEntries; }
301 
302 	private:
303 		text_run_array *fArray;
304 		size_t fNumEntries;
305 };
306 
307 extern void FillInQuoteTextRuns(BTextView* view, quote_context* context,
308 	const char* line, int32 length, const BFont& font, text_run_array* style,
309 	int32 maxStyles = 5);
310 
311 #endif	/* #ifndef _CONTENT_H */
312 
313