xref: /haiku/headers/os/mail/MailComponent.h (revision 6aff37d1c79e20748c683ae224bd629f88a5b0be)
1 #ifndef ZOIDBERG_MAIL_COMPONENT_H
2 #define ZOIDBERG_MAIL_COMPONENT_H
3 /* (Text)Component - message component base class and plain text
4 **
5 ** Copyright 2001 Dr. Zoidberg Enterprises. All rights reserved.
6 */
7 
8 
9 #include <UTF8.h>
10 #include <Message.h>
11 #include <String.h>
12 
13 #include <mail_encoding.h>
14 
15 class BMimeType;
16 
17 extern const char *kHeaderCharsetString;
18 extern const char *kHeaderEncodingString;
19 // Special field names in the headers which specify the character set (int32)
20 // and encoding (int8) to use when converting the headers from UTF-8 to the
21 // output e-mail format (rfc2047).  For use with SetHeaderField when you pass
22 // it a structured header in a BMessage.
23 
24 
25 enum component_type {
26 	B_MAIL_PLAIN_TEXT_BODY = 0,
27 	B_MAIL_SIMPLE_ATTACHMENT,
28 	B_MAIL_ATTRIBUTED_ATTACHMENT,
29 	B_MAIL_MULTIPART_CONTAINER
30 };
31 
32 class BMailComponent {
33 	public:
34 		BMailComponent(uint32 defaultCharSet = B_MAIL_NULL_CONVERSION);
35 		virtual ~BMailComponent();
36 
37 		//------Info on this component
38 		uint32 ComponentType();
39 		BMailComponent *WhatIsThis();
40 			// Takes any generic MailComponent, and returns an instance
41 			// of a MailComponent subclass that applies to this case,
42 			// ready for instantiation. Note that you still have to
43 			// Instantiate() it yourself.
44 		bool IsAttachment();
45 			// Returns true if this component is an attachment, false
46 			// otherwise
47 
48 		void SetHeaderField(
49 			const char *key, const char *value,
50 			uint32 charset = B_MAIL_NULL_CONVERSION,
51 			mail_encoding encoding = null_encoding,
52 			bool replace_existing = true);
53 			// If you want to delete a header, pass in a zero length or NULL
54 			// string for the value field, or use RemoveHeader.
55 		void SetHeaderField(
56 			const char *key, BMessage *structured_header,
57 			bool replace_existing = true);
58 
59 		const char *HeaderAt(int32 index) const;
60 		const char *HeaderField(const char *key, int32 index = 0) const;
61 		status_t	HeaderField(const char *key, BMessage *structured_header, int32 index = 0) const;
62 
63 		status_t	RemoveHeader(const char *key);
64 
65 		virtual status_t GetDecodedData(BPositionIO *data);
66 		virtual status_t SetDecodedData(BPositionIO *data);
67 
68 		virtual status_t SetToRFC822(BPositionIO *data, size_t length, bool parse_now = false);
69 		virtual status_t RenderToRFC822(BPositionIO *render_to);
70 
71 		virtual status_t MIMEType(BMimeType *mime);
72 
73 	protected:
74 		uint32 _charSetForTextDecoding;
75 			// This is the character set to be used for decoding text
76 			// components, or if it is B_MAIL_NULL_CONVERSION then the character
77 			// set will be determined automatically.  Since we can't use a
78 			// global variable (different messages might have different values
79 			// of this), and since sub-components can't find their parents,
80 			// this is passed down during construction to some (just Component,
81 			// Container, Message, MIME, Text) child components and ends up
82 			// being used in the text components.
83 
84 	private:
85 		virtual void _ReservedComponent1();
86 		virtual void _ReservedComponent2();
87 		virtual void _ReservedComponent3();
88 		virtual void _ReservedComponent4();
89 		virtual void _ReservedComponent5();
90 
91 		BMessage headers;
92 
93 		uint32 _reserved[5];
94 };
95 
96 
97 class BTextMailComponent : public BMailComponent {
98 	public:
99 		BTextMailComponent(const char *text = NULL, uint32 defaultCharSet = B_MAIL_NULL_CONVERSION);
100 		virtual ~BTextMailComponent();
101 
102 		void SetEncoding(mail_encoding encoding, int32 charset);
103 			// encoding: you should always use quoted_printable, base64 is strongly not
104 			//		recommended, see rfc 2047 for the reasons why
105 			// charset: use Conversion flavor constants from UTF8.h
106 
107 		void SetText(const char *text);
108 		void AppendText(const char *text);
109 
110 		const char *Text();
111 		BString *BStringText();
112 
113 		void Quote(const char *message = NULL,
114 				   const char *quote_style = "> ");
115 
116 		virtual status_t GetDecodedData(BPositionIO *data);
117 		virtual status_t SetDecodedData(BPositionIO *data);
118 
119 		virtual status_t SetToRFC822(BPositionIO *data, size_t length, bool parse_now = false);
120 		virtual status_t RenderToRFC822(BPositionIO *render_to);
121 
122 	private:
123 		virtual void _ReservedText1();
124 		virtual void _ReservedText2();
125 
126 		BString text;
127 		BString decoded;
128 
129 		mail_encoding encoding;
130 		uint32 charset; // This character set is used for encoding, not decoding.
131 
132 		status_t ParseRaw();
133 		BPositionIO *raw_data;
134 		size_t raw_length;
135 		off_t raw_offset;
136 
137 		uint32 _reserved[5];
138 };
139 
140 #endif	/* ZOIDBERG_MAIL_COMPONENT_H */
141