xref: /haiku/src/add-ons/translators/rtf/RTFTranslator.cpp (revision 1d9d47fc72028bb71b5f232a877231e59cfe2438)
1 /*
2  * Copyright 2004-2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "RTFTranslator.h"
8 #include "ConfigView.h"
9 #include "RTF.h"
10 #include "convert.h"
11 
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 
16 
17 #define READ_BUFFER_SIZE 2048
18 #define DATA_BUFFER_SIZE 64
19 
20 
21 // The input formats that this translator supports.
22 translation_format sInputFormats[] = {
23 	{
24 		RTF_TEXT_FORMAT,
25 		B_TRANSLATOR_TEXT,
26 		RTF_IN_QUALITY,
27 		RTF_IN_CAPABILITY,
28 		"text/rtf",
29 		"RichTextFormat file"
30 	},
31 };
32 
33 // The output formats that this translator supports.
34 translation_format sOutputFormats[] = {
35 	{
36 		B_TRANSLATOR_TEXT,
37 		B_TRANSLATOR_TEXT,
38 		TEXT_OUT_QUALITY,
39 		TEXT_OUT_CAPABILITY,
40 		"text/plain",
41 		"Plain text file"
42 	},
43 	{
44 		B_STYLED_TEXT_FORMAT,
45 		B_TRANSLATOR_TEXT,
46 		STXT_OUT_QUALITY,
47 		STXT_OUT_CAPABILITY,
48 		"text/x-vnd.Be-stxt",
49 		"Be styled text file"
50 	}
51 };
52 
53 
54 RTFTranslator::RTFTranslator()
55 {
56 	char info[256];
57 	sprintf(info, "Rich Text Format Translator v%d.%d.%d %s",
58 		int(B_TRANSLATION_MAJOR_VERSION(RTF_TRANSLATOR_VERSION)),
59 		int(B_TRANSLATION_MINOR_VERSION(RTF_TRANSLATOR_VERSION)),
60 		int(B_TRANSLATION_REVISION_VERSION(RTF_TRANSLATOR_VERSION)),
61 		__DATE__);
62 
63 	fInfo = strdup(info);
64 }
65 
66 
67 RTFTranslator::~RTFTranslator()
68 {
69 	free(fInfo);
70 }
71 
72 
73 const char *
74 RTFTranslator::TranslatorName() const
75 {
76 	return "RTF Text Files";
77 }
78 
79 
80 const char *
81 RTFTranslator::TranslatorInfo() const
82 {
83 	return "Rich Text Format Translator";
84 }
85 
86 
87 int32
88 RTFTranslator::TranslatorVersion() const
89 {
90 	return RTF_TRANSLATOR_VERSION;
91 }
92 
93 
94 const translation_format *
95 RTFTranslator::InputFormats(int32 *_outCount) const
96 {
97 	if (_outCount == NULL)
98 		return NULL;
99 
100 	*_outCount = sizeof(sInputFormats) / sizeof(translation_format);
101 	return sInputFormats;
102 }
103 
104 
105 const translation_format *
106 RTFTranslator::OutputFormats(int32 *_outCount) const
107 {
108 	*_outCount = sizeof(sOutputFormats) / sizeof(translation_format);
109 	return sOutputFormats;
110 }
111 
112 
113 status_t
114 RTFTranslator::Identify(BPositionIO *stream,
115 	const translation_format *format, BMessage *ioExtension,
116 	translator_info *info, uint32 outType)
117 {
118 	if (!outType)
119 		outType = B_TRANSLATOR_TEXT;
120 	if (outType != B_TRANSLATOR_TEXT && outType != B_STYLED_TEXT_FORMAT)
121 		return B_NO_TRANSLATOR;
122 
123 	RTF::Parser parser(*stream);
124 
125 	status_t status = parser.Identify();
126 	if (status != B_OK)
127 		return B_NO_TRANSLATOR;
128 
129 	// return information about the data in the stream
130 	info->type = B_TRANSLATOR_TEXT; //RTF_TEXT_FORMAT;
131 	info->group = B_TRANSLATOR_TEXT;
132 	info->quality = RTF_IN_QUALITY;
133 	info->capability = RTF_IN_CAPABILITY;
134 	strcpy(info->name, "RichTextFormat file");
135 	strcpy(info->MIME, "text/rtf");
136 
137 	return B_OK;
138 }
139 
140 
141 status_t
142 RTFTranslator::Translate(BPositionIO *source,
143 	const translator_info *inInfo, BMessage *ioExtension,
144 	uint32 outType, BPositionIO *target)
145 {
146 	if (target == NULL || source == NULL)
147 		return B_BAD_VALUE;
148 
149 	if (!outType)
150 		outType = B_TRANSLATOR_TEXT;
151 	if (outType != B_TRANSLATOR_TEXT && outType != B_STYLED_TEXT_FORMAT)
152 		return B_NO_TRANSLATOR;
153 
154 	RTF::Parser parser(*source);
155 
156 	RTF::Header header;
157 	status_t status = parser.Parse(header);
158 	if (status != B_OK)
159 		return status;
160 
161 	// we support two different output formats
162 
163 	if (outType == B_TRANSLATOR_TEXT)
164 		return convert_to_plain_text(header, *target);
165 
166 	return convert_to_stxt(header, *target);
167 }
168 
169 
170 status_t
171 RTFTranslator::MakeConfigurationView(BMessage *ioExtension, BView **_view, BRect *_extent)
172 {
173 	if (_view == NULL || _extent == NULL)
174 		return B_BAD_VALUE;
175 
176 	BView *view = new ConfigView(BRect(0, 0, 225, 175));
177 	if (view == NULL)
178 		return BTranslator::MakeConfigurationView(ioExtension, _view, _extent);
179 
180 	*_view = view;
181 	*_extent = view->Bounds();
182 	return B_OK;
183 }
184 
185 
186 //	#pragma mark -
187 
188 
189 BTranslator *
190 make_nth_translator(int32 n, image_id you, uint32 flags, ...)
191 {
192 	if (n != 0)
193 		return NULL;
194 
195 	return new RTFTranslator();
196 }
197 
198