xref: /haiku/headers/os/translation/TranslatorRoster.h (revision d67f94ca9dc693a2734456fa13111d2036bc7297)
1 /*****************************************************************************/
2 //               File: TranslatorRoster.h
3 //              Class: BTranslatorRoster
4 //   Reimplemented by: Michael Wilber, Translation Kit Team
5 //   Reimplementation: 2002-06-11
6 //
7 // Description: This class is the guts of the translation kit, it makes the
8 //              whole thing happen. It bridges the applications using this
9 //              object with the translators that the apps need to access.
10 //
11 //
12 // Copyright (c) 2002 OpenBeOS Project
13 //
14 // Original Version: Copyright 1998, Be Incorporated, All Rights Reserved.
15 //                   Copyright 1995-1997, Jon Watte
16 //
17 // Permission is hereby granted, free of charge, to any person obtaining a
18 // copy of this software and associated documentation files (the "Software"),
19 // to deal in the Software without restriction, including without limitation
20 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
21 // and/or sell copies of the Software, and to permit persons to whom the
22 // Software is furnished to do so, subject to the following conditions:
23 //
24 // The above copyright notice and this permission notice shall be included
25 // in all copies or substantial portions of the Software.
26 //
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
28 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33 // DEALINGS IN THE SOFTWARE.
34 /*****************************************************************************/
35 
36 #ifndef _TRANSLATOR_ROSTER_H
37 #define _TRANSLATOR_ROSTER_H
38 
39 #include <TranslationDefs.h>
40 #include <OS.h>
41 #include <StorageDefs.h>
42 #include <InterfaceDefs.h>
43 #include <Archivable.h>
44 #include <TranslatorFormats.h>
45 #include <TranslationDefs.h>
46 #include <TranslationErrors.h>
47 #include <Translator.h>
48 #include <String.h>
49 #include <image.h>
50 #include <File.h>
51 #include <Directory.h>
52 #include <Volume.h>
53 #include <string.h>
54 #include <OS.h>
55 #include <stdlib.h>
56 #include <stdio.h>
57 #include <dirent.h>
58 #include <unistd.h>
59 #include <sys/stat.h>
60 #include <alloca.h>
61 
62 struct translation_format;
63 
64 class BBitmap;
65 class BView;
66 class BPositionIO;
67 class BQuery;
68 class BMessage;
69 
70 const char kgDefaultTranslatorPath[] = "/boot/home/config/add-ons/Translators:/boot/home/config/add-ons/Datatypes:/system/add-ons/Translators";
71 
72 // used by BTranslatorRoster to store the list of translators
73 struct translator_node {
74 	BTranslator *translator;
75 	char *path;
76 	image_id image;
77 	translator_id id;
78 	translator_node *next;
79 };
80 
81 class BTranslatorRoster : public BArchivable {
82 	// private, unimplemented functions
83 	// (I'm not sure if there is a need for them anyway)
84 	BTranslatorRoster(const BTranslatorRoster &);
85 	BTranslatorRoster & operator=(const BTranslatorRoster &);
86 
87 public:
88 	BTranslatorRoster();
89 		// initializes the object with no translators
90 
91 	BTranslatorRoster(BMessage *model);
92 		// initializes the object and loads all translators from the
93 		// "be:translator_path" string array in model
94 
95 	~BTranslatorRoster();
96 		// frees memory used by this object
97 
98 	virtual status_t Archive(BMessage *into, bool deep = true) const;
99 		// store the BTranslatorRoster in into so it can later be
100 		// used with Instantiate or the BMessage constructor
101 
102 	static BArchivable *Instantiate(BMessage *from);
103 		// creates a BTranslatorRoster object from the from archive
104 
105 	static const char *Version(int32 *outCurVersion, int32 *outMinVersion,
106 		int32 inAppVersion = B_TRANSLATION_CURRENT_VERSION);
107 		// returns the version of BTranslator roster as a string
108 		// and through the int32 pointers; inAppVersion appears
109 		// to be ignored
110 
111 	static BTranslatorRoster *Default();
112 		// returns the "default" set of translators, they are translators
113 		// that are loaded from the default paths
114 		//	/boot/home/config/add-ons/Translators,
115 		//	/boot/home/config/add-ons/Datatypes,
116 		//	/system/add-ons/Translators or the paths in the
117 		// TRANSLATORS environment variable, if it exists
118 
119 	status_t AddTranslators(const char *load_path = NULL);
120 		// this adds a translator, all of the translators in a folder or
121 		// the default translators if the path is NULL
122 
123 	status_t AddTranslator(BTranslator * translator);
124 		// adds a translator that you've created yourself to the
125 		// BTranslatorRoster; this is useful if you have translators
126 		// that you don't want to make public or don't want loaded as
127 		// an add-on
128 
129 	virtual status_t Identify(BPositionIO *inSource, BMessage *ioExtension,
130 		translator_info *outInfo, uint32 inHintType = 0,
131 		const char *inHintMIME = NULL, uint32 inWantType = 0);
132 		// identifies the translator that can handle the data in inSource
133 
134 	virtual status_t GetTranslators(BPositionIO *inSource,
135 		BMessage *ioExtension, translator_info **outInfo, int32 *outNumInfo,
136 		uint32 inHintType = 0, const char *inHintMIME = NULL,
137 		uint32 inWantType = 0);
138 		// finds all translators for the given type
139 
140 	virtual status_t GetAllTranslators(translator_id **outList,
141 		int32 *outCount);
142 		// returns all translators that this object contains
143 
144 	virtual	status_t GetTranslatorInfo(translator_id forTranslator,
145 		const char **outName, const char **outInfo, int32 *outVersion);
146 		// gets user visible info about a specific translator
147 
148 	virtual status_t GetInputFormats(translator_id forTranslator,
149 		const translation_format **outFormats, int32 *outNumFormats);
150 		// finds all input formats for a translator;
151 		// note that translators don't always publish the formats
152 		// that they support
153 
154 	virtual	status_t GetOutputFormats(translator_id forTranslator,
155 		const translation_format **outFormats, int32 *outNumFormats);
156 		// finds all output formats for a translator;
157 		// note that translators don't always publish the formats
158 		// that they support
159 
160 	virtual	status_t Translate(BPositionIO *inSource,
161 		const translator_info *inInfo, BMessage *ioExtension,
162 		BPositionIO *outDestination, uint32 inWantOutType,
163 		uint32 inHintType = 0, const char *inHintMIME = NULL);
164 		// the whole point of this entire kit boils down to this function;
165 		// this translates the data in inSource into outDestination
166 		// using the format inWantOutType
167 
168 	virtual	status_t Translate(translator_id inTranslator,
169 		BPositionIO *inSource, BMessage *ioExtension,
170 		BPositionIO *outDestination, uint32 inWantOutType);
171 		// the whole point of this entire kit boils down to this function;
172 		// this translates the data in inSource into outDestination
173 		// using the format inWantOutType and the translator inTranslator
174 
175 	virtual status_t MakeConfigurationView(translator_id forTranslator,
176 		BMessage *ioExtension, BView **outView, BRect *outExtent);
177 		// create the view for forTranslator that allows the user
178 		// to configure it;
179 		// NOTE: translators are not required to support this function
180 
181 	virtual	status_t GetConfigurationMessage(translator_id forTranslator,
182 		BMessage *ioExtension);
183 		// this is used to save the settings for a translator so that
184 		// they can be written to disk or used in an Indentify or
185 		// Translate call
186 
187 	status_t GetRefFor(translator_id translator, entry_ref *out_ref);
188 		// I'm guessing that this returns the entry_ref for the
189 		// translator add-on file for the translator_id translator
190 
191 private:
192 	void Initialize();
193 		// class initialization code used by all constructors
194 
195 	translator_node *FindTranslatorNode(translator_id id);
196 		// used to find the translator_node for the translator_id id
197 		// returns NULL if the id is not valid
198 
199 	status_t LoadTranslator(const char *path);
200 		// loads the translator add-on specified by path
201 
202 	void LoadDir(const char *path, int32 &loadErr, int32 &nLoaded);
203 		// loads all of the translator add-ons from path and
204 		// returns error status in loadErr and the number of
205 		// translators loaded in nLoaded
206 
207 	bool CheckFormats(const translation_format *inputFormats,
208 		int32 inputFormatsCount, uint32 hintType, const char *hintMIME,
209 		const translation_format **outFormat);
210 		// determines how the Identify function is called
211 
212 	// adds the translator to the list of translators
213 	// that this object maintains
214 	status_t AddTranslatorToList(BTranslator *translator);
215 	status_t AddTranslatorToList(BTranslator *translator,
216 			const char *path, image_id image, bool acquire);
217 
218 	static	BTranslatorRoster *fspDefaultTranslators;
219 		// object that contains the default translators
220 
221 	// list of translators maintained by this object
222 	translator_node *fpTranslators;
223 	translator_node *fpLastTranslator;
224 
225 	sem_id fSem;
226 		// semaphore used to lock this object
227 
228 	// used to maintain binary combatibility with
229 	// past and future versions of this object
230 	int32 fUnused[4];
231 	virtual	void ReservedTranslatorRoster1();
232 	virtual	void ReservedTranslatorRoster2();
233 	virtual	void ReservedTranslatorRoster3();
234 	virtual	void ReservedTranslatorRoster4();
235 	virtual	void ReservedTranslatorRoster5();
236 	virtual	void ReservedTranslatorRoster6();
237 };
238 
239 #endif	/* _TRANSLATOR_ROSTER_H */
240 
241