* More or less rewrote BTranslatorRoster - it now has a private implementation

class, and only wraps around that one.
* Translating is no longer serialized, you can translate more than one object
  at a time now.
* A BTranslator that is released (ie. deleted) will no longer let its BTranslatorRoster
  crash.
* Removed BTranslatorRoster::Version() - this kind of call definitely makes no
  sense at all. It's still exported from the sources, though, for backwards
  compatibility.
* Simplified and improved code.
* Images are now unloaded only once.
* Added new method IsTranslator() that will be used by the DataTranslations preferences
  application.
* Began implementing new methods StartWatching()/StopWatching() that will notify
  you if new translators are installed or old ones removed (this will also be used
  by DataTranslations once it's ready).
* The private BTranslatorRoster class will now add itself to the existing BApplication,
  in order to provide automatic updating of the translators if needed (not yet implemented
  though).
* Not heavily tested yet, there might be some regressions.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@17247 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2006-04-27 18:40:28 +00:00
parent 11add5a5d7
commit f90e454336
9 changed files with 1559 additions and 2768 deletions
+49 -111
View File
@@ -1,127 +1,67 @@
/*****************************************************************************/
// File: Translator.h
// Class: BTranslator
// Reimplemented by: Michael Wilber, Translation Kit Team
// Reimplementation: 2002-06-15
//
// Description: This file contains the BTranslator class, the base class for
// all translators that don't use the BeOS R4/R4.5 add-on method.
//
//
// Copyright (c) 2002 OpenBeOS Project
//
// Original Version: Copyright 1998, Be Incorporated, All Rights Reserved.
// Copyright 1995-1997, Jon Watte
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
/*****************************************************************************/
/*
* Copyright 2002-2006, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef _TRANSLATOR_H
#define _TRANSLATOR_H
#include <TranslationDefs.h>
#include <Archivable.h>
#include <TranslationDefs.h>
#include <TranslatorRoster.h>
class BTranslator : public BArchivable {
public:
BTranslator();
// Sets refcount to 1 (the object is initially Acquired()'d once)
public:
BTranslator();
BTranslator *Acquire();
// return actual object and increment the refcount
BTranslator *Release();
// Decrements the refcount and returns pointer to the object.
// When the refcount is zero, NULL is returned and the object is
// deleted
int32 ReferenceCount();
// returns the refcount, THIS IS NOT THREAD SAFE, ITS ONLY FOR "FUN"
BTranslator* Acquire();
BTranslator* Release();
virtual const char *TranslatorName() const = 0;
// returns the short name of the translator
virtual const char *TranslatorInfo() const = 0;
// returns a verbose name/description for the translator
virtual int32 TranslatorVersion() const = 0;
// returns the version of the translator
int32 ReferenceCount();
// returns the refcount, THIS IS NOT THREAD SAFE, ITS ONLY FOR "FUN"
virtual const translation_format *InputFormats(int32 *out_count)
const = 0;
// returns the input formats and the count of input formats
// that this translator supports
virtual const translation_format *OutputFormats(int32 *out_count)
const = 0;
// returns the output formats and the count of output formats
// that this translator supports
virtual const char *TranslatorName() const = 0;
virtual const char *TranslatorInfo() const = 0;
virtual int32 TranslatorVersion() const = 0;
virtual status_t Identify(BPositionIO *inSource,
const translation_format *inFormat, BMessage *ioExtension,
translator_info *outInfo, uint32 outType) = 0;
// determines whether or not this translator can convert the
// data in inSource to the type outType
virtual const translation_format *InputFormats(int32 *out_count)
const = 0;
virtual const translation_format *OutputFormats(int32 *out_count)
const = 0;
virtual status_t Identify(BPositionIO *inSource,
const translation_format *inFormat, BMessage *ioExtension,
translator_info *outInfo, uint32 outType) = 0;
virtual status_t Translate(BPositionIO *inSource,
const translator_info *inInfo, BMessage *ioExtension,
uint32 outType, BPositionIO *outDestination) = 0;
virtual status_t MakeConfigurationView(BMessage *ioExtension,
BView **outView, BRect *outExtent);
virtual status_t GetConfigurationMessage(BMessage *ioExtension);
virtual status_t Translate(BPositionIO *inSource,
const translator_info *inInfo, BMessage *ioExtension,
uint32 outType, BPositionIO *outDestination) = 0;
// this function is the whole point of the Translation Kit,
// it translates the data in inSource to outDestination
// using the format outType
protected:
virtual ~BTranslator();
// this is protected because the object is deleted by the
// Release() function instead of being deleted directly by
// the user
virtual status_t MakeConfigurationView(BMessage *ioExtension,
BView **outView, BRect *outExtent);
// this function creates a view that allows the user to
// configure the translator, this function is not
// required for all translators
private:
friend class BTranslatorRoster::Private;
virtual status_t GetConfigurationMessage(BMessage *ioExtension);
// puts information about the current configuration of the
// translator into ioExtension so that it can be used with
// Identify or Translate or whatever, this function is
// not required for all translators
virtual status_t _Reserved_Translator_0(int32, void *);
virtual status_t _Reserved_Translator_1(int32, void *);
virtual status_t _Reserved_Translator_2(int32, void *);
virtual status_t _Reserved_Translator_3(int32, void *);
virtual status_t _Reserved_Translator_4(int32, void *);
virtual status_t _Reserved_Translator_5(int32, void *);
virtual status_t _Reserved_Translator_6(int32, void *);
virtual status_t _Reserved_Translator_7(int32, void *);
protected:
virtual ~BTranslator();
// this is protected because the object is deleted by the
// Release() function instead of being deleted directly by
// the user
BTranslatorRoster::Private* fOwningRoster;
translator_id fID;
int32 fRefCount;
private:
int32 fRefCount;
// number of times this object has been referenced
// by the Acquire() function
sem_id fSem;
// used to lock object for Acquire() and Release()
// For binary compatibility with past and future
// version of this class
uint32 fUnused[8];
virtual status_t _Reserved_Translator_0(int32, void *);
virtual status_t _Reserved_Translator_1(int32, void *);
virtual status_t _Reserved_Translator_2(int32, void *);
virtual status_t _Reserved_Translator_3(int32, void *);
virtual status_t _Reserved_Translator_4(int32, void *);
virtual status_t _Reserved_Translator_5(int32, void *);
virtual status_t _Reserved_Translator_6(int32, void *);
virtual status_t _Reserved_Translator_7(int32, void *);
uint32 _reserved[7];
};
// The post-4.5 API suggests implementing this function in your translator
@@ -132,6 +72,4 @@ private:
extern "C" _EXPORT BTranslator *make_nth_translator(int32 n, image_id you,
uint32 flags, ...);
#endif /* _TRANSLATOR_H */
+60 -200
View File
@@ -1,63 +1,13 @@
/*****************************************************************************/
// File: TranslatorRoster.h
// Class: BTranslatorRoster
// Reimplemented by: Michael Wilber, Translation Kit Team
// Reimplementation: 2002-06-11
//
// Description: This class is the guts of the translation kit, it makes the
// whole thing happen. It bridges the applications using this
// object with the translators that the apps need to access.
//
//
// Copyright (c) 2002 OpenBeOS Project
//
// Original Version: Copyright 1998, Be Incorporated, All Rights Reserved.
// Copyright 1995-1997, Jon Watte
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
/*****************************************************************************/
/*
* Copyright 2002-2006, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef _TRANSLATOR_ROSTER_H
#define _TRANSLATOR_ROSTER_H
#include <TranslationDefs.h>
#include <OS.h>
#include <StorageDefs.h>
#include <InterfaceDefs.h>
#include <Archivable.h>
#include <TranslatorFormats.h>
#include <TranslationDefs.h>
#include <TranslationErrors.h>
#include <Translator.h>
#include <String.h>
#include <image.h>
#include <File.h>
#include <Directory.h>
#include <Volume.h>
#include <string.h>
#include <OS.h>
#include <stdlib.h>
#include <stdio.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <alloca.h>
struct translation_format;
@@ -67,174 +17,84 @@ class BPositionIO;
class BQuery;
class BMessage;
const char kgDefaultTranslatorPath[] = "/boot/home/config/add-ons/Translators:/boot/home/config/add-ons/Datatypes:/system/add-ons/Translators";
// used by BTranslatorRoster to store the list of translators
struct translator_node {
BTranslator *translator;
char *path;
image_id image;
translator_id id;
translator_node *next;
};
class BTranslatorRoster : public BArchivable {
// private, unimplemented functions
// (I'm not sure if there is a need for them anyway)
BTranslatorRoster(const BTranslatorRoster &);
BTranslatorRoster & operator=(const BTranslatorRoster &);
public:
BTranslatorRoster();
BTranslatorRoster(BMessage* model);
virtual ~BTranslatorRoster();
public:
BTranslatorRoster();
// initializes the object with no translators
BTranslatorRoster(BMessage *model);
// initializes the object and loads all translators from the
// "be:translator_path" string array in model
~BTranslatorRoster();
// frees memory used by this object
static BTranslatorRoster *Default();
virtual status_t Archive(BMessage *into, bool deep = true) const;
// store the BTranslatorRoster in into so it can later be
// used with Instantiate or the BMessage constructor
static BArchivable *Instantiate(BMessage *from);
// creates a BTranslatorRoster object from the from archive
virtual status_t Archive(BMessage* into, bool deep = true) const;
static BArchivable* Instantiate(BMessage* from);
static const char *Version(int32 *outCurVersion, int32 *outMinVersion,
int32 inAppVersion = B_TRANSLATION_CURRENT_VERSION);
// returns the version of BTranslator roster as a string
// and through the int32 pointers; inAppVersion appears
// to be ignored
status_t AddTranslators(const char* loadPath = NULL);
status_t AddTranslator(BTranslator* translator);
static BTranslatorRoster *Default();
// returns the "default" set of translators, they are translators
// that are loaded from the default paths
// /boot/home/config/add-ons/Translators,
// /boot/home/config/add-ons/Datatypes,
// /system/add-ons/Translators or the paths in the
// TRANSLATORS environment variable, if it exists
virtual status_t Identify(BPositionIO* source, BMessage* ioExtension,
translator_info* _info, uint32 hintType = 0,
const char* hintMIME = NULL, uint32 wantType = 0);
status_t AddTranslators(const char *load_path = NULL);
// this adds a translator, all of the translators in a folder or
// the default translators if the path is NULL
virtual status_t GetTranslators(BPositionIO* source,
BMessage* ioExtension, translator_info** _info, int32* _numInfo,
uint32 hintType = 0, const char* hintMIME = NULL,
uint32 wantType = 0);
status_t AddTranslator(BTranslator * translator);
// adds a translator that you've created yourself to the
// BTranslatorRoster; this is useful if you have translators
// that you don't want to make public or don't want loaded as
// an add-on
virtual status_t GetAllTranslators(translator_id** _list,
int32* _count);
virtual status_t Identify(BPositionIO *inSource, BMessage *ioExtension,
translator_info *outInfo, uint32 inHintType = 0,
const char *inHintMIME = NULL, uint32 inWantType = 0);
// identifies the translator that can handle the data in inSource
virtual status_t GetTranslatorInfo(translator_id translatorID,
const char** _name, const char** _info, int32* _version);
virtual status_t GetTranslators(BPositionIO *inSource,
BMessage *ioExtension, translator_info **outInfo, int32 *outNumInfo,
uint32 inHintType = 0, const char *inHintMIME = NULL,
uint32 inWantType = 0);
// finds all translators for the given type
virtual status_t GetInputFormats(translator_id translatorID,
const translation_format** _formats, int32* _numFormats);
virtual status_t GetAllTranslators(translator_id **outList,
int32 *outCount);
// returns all translators that this object contains
virtual status_t GetOutputFormats(translator_id translatorID,
const translation_format** _formats, int32* _numFormats);
virtual status_t GetTranslatorInfo(translator_id forTranslator,
const char **outName, const char **outInfo, int32 *outVersion);
// gets user visible info about a specific translator
virtual status_t Translate(BPositionIO* source,
const translator_info* info, BMessage* ioExtension,
BPositionIO* destination, uint32 wantOutType,
uint32 hintType = 0, const char* hintMIME = NULL);
virtual status_t GetInputFormats(translator_id forTranslator,
const translation_format **outFormats, int32 *outNumFormats);
// finds all input formats for a translator;
// note that translators don't always publish the formats
// that they support
virtual status_t Translate(translator_id translatorID,
BPositionIO* source, BMessage* ioExtension,
BPositionIO* destination, uint32 wantOutType);
virtual status_t GetOutputFormats(translator_id forTranslator,
const translation_format **outFormats, int32 *outNumFormats);
// finds all output formats for a translator;
// note that translators don't always publish the formats
// that they support
virtual status_t MakeConfigurationView(translator_id translatorID,
BMessage* ioExtension, BView** _view, BRect* _extent);
virtual status_t Translate(BPositionIO *inSource,
const translator_info *inInfo, BMessage *ioExtension,
BPositionIO *outDestination, uint32 inWantOutType,
uint32 inHintType = 0, const char *inHintMIME = NULL);
// the whole point of this entire kit boils down to this function;
// this translates the data in inSource into outDestination
// using the format inWantOutType
virtual status_t GetConfigurationMessage(translator_id translatorID,
BMessage* ioExtension);
virtual status_t Translate(translator_id inTranslator,
BPositionIO *inSource, BMessage *ioExtension,
BPositionIO *outDestination, uint32 inWantOutType);
// the whole point of this entire kit boils down to this function;
// this translates the data in inSource into outDestination
// using the format inWantOutType and the translator inTranslator
status_t GetRefFor(translator_id translatorID, entry_ref* ref);
bool IsTranslator(entry_ref* ref);
virtual status_t MakeConfigurationView(translator_id forTranslator,
BMessage *ioExtension, BView **outView, BRect *outExtent);
// create the view for forTranslator that allows the user
// to configure it;
// NOTE: translators are not required to support this function
status_t StartWatching(BMessenger target);
status_t StopWatching(BMessenger target);
virtual status_t GetConfigurationMessage(translator_id forTranslator,
BMessage *ioExtension);
// this is used to save the settings for a translator so that
// they can be written to disk or used in an Indentify or
// Translate call
private:
// unimplemented
BTranslatorRoster(const BTranslatorRoster& other);
BTranslatorRoster& operator=(const BTranslatorRoster& other);
status_t GetRefFor(translator_id translator, entry_ref *out_ref);
// I'm guessing that this returns the entry_ref for the
// translator add-on file for the translator_id translator
virtual void ReservedTranslatorRoster1();
virtual void ReservedTranslatorRoster2();
virtual void ReservedTranslatorRoster3();
virtual void ReservedTranslatorRoster4();
virtual void ReservedTranslatorRoster5();
virtual void ReservedTranslatorRoster6();
private:
void Initialize();
// class initialization code used by all constructors
translator_node *FindTranslatorNode(translator_id id);
// used to find the translator_node for the translator_id id
// returns NULL if the id is not valid
void _Initialize();
status_t LoadTranslator(const char *path);
// loads the translator add-on specified by path
void LoadDir(const char *path, int32 &loadErr, int32 &nLoaded);
// loads all of the translator add-ons from path and
// returns error status in loadErr and the number of
// translators loaded in nLoaded
bool CheckFormats(const translation_format *inputFormats,
int32 inputFormatsCount, uint32 hintType, const char *hintMIME,
const translation_format **outFormat);
// determines how the Identify function is called
private:
class Private;
// adds the translator to the list of translators
// that this object maintains
status_t AddTranslatorToList(BTranslator *translator);
status_t AddTranslatorToList(BTranslator *translator,
const char *path, image_id image, bool acquire);
Private* fPrivate;
int32 fUnused[6];
static BTranslatorRoster *fspDefaultTranslators;
// object that contains the default translators
// list of translators maintained by this object
translator_node *fpTranslators;
translator_node *fpLastTranslator;
sem_id fSem;
// semaphore used to lock this object
// used to maintain binary combatibility with
// past and future versions of this object
int32 fUnused[4];
virtual void ReservedTranslatorRoster1();
virtual void ReservedTranslatorRoster2();
virtual void ReservedTranslatorRoster3();
virtual void ReservedTranslatorRoster4();
virtual void ReservedTranslatorRoster5();
virtual void ReservedTranslatorRoster6();
static BTranslatorRoster* sDefaultRoster;
};
#endif /* _TRANSLATOR_ROSTER_H */