Move MIME DB code back from registrar to src/kits/storage/mime

* Together with database_{access,support}.cpp it is built into a static
  library.
* Add new interfaces MimeSniffer and Database::NotificationListener for
  plugging in registrar specific functionality (the sniffer add-on
  support and the notification mechanism).
This commit is contained in:
Ingo Weinhold
2013-05-07 17:54:29 +02:00
parent a625f557c5
commit 9cda9c9905
25 changed files with 255 additions and 91 deletions
@@ -0,0 +1,61 @@
/*
* Copyright 2002-2007, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef _MIME_ASSOCIATED_TYPES_H
#define _MIME_ASSOCIATED_TYPES_H
#include <SupportDefs.h>
#include <map>
#include <set>
#include <string>
class BMessage;
class BString;
struct entry_ref;
namespace BPrivate {
namespace Storage {
namespace Mime {
class MimeSniffer;
class AssociatedTypes {
public:
AssociatedTypes(MimeSniffer* mimeSniffer);
~AssociatedTypes();
status_t GetAssociatedTypes(const char *extension, BMessage *types);
status_t GuessMimeType(const char *filename, BString *result);
status_t GuessMimeType(const entry_ref *ref, BString *result);
status_t SetFileExtensions(const char *type, const BMessage *extensions);
status_t DeleteFileExtensions(const char *type);
void PrintToStream() const;
private:
status_t AddAssociatedType(const char *extension, const char *type);
status_t RemoveAssociatedType(const char *extension, const char *type);
status_t BuildAssociatedTypesTable();
status_t ProcessType(const char *type);
std::string PrepExtension(const char *extension) const;
std::map<std::string, std::set<std::string> > fFileExtensions; // mime type => set of associated file extensions
std::map<std::string, std::set<std::string> > fAssociatedTypes; // file extension => set of associated mime types
private:
MimeSniffer* fMimeSniffer;
bool fHaveDoneFullBuild;
};
} // namespace Mime
} // namespace Storage
} // namespace BPrivate
#endif // _MIME_ASSOCIATED_TYPES_H
+192
View File
@@ -0,0 +1,192 @@
/*
* Copyright 2002-2006, Haiku.
* Distributed under the terms of the MIT License.
*
* Authors:
* Tyler Dauwalder
*/
#ifndef _MIME_DATABASE_H
#define _MIME_DATABASE_H
#include <map>
#include <set>
#include <string>
#include <List.h>
#include <Locker.h>
#include <Mime.h>
#include <Messenger.h>
#include <StorageDefs.h>
#include <mime/AssociatedTypes.h>
#include <mime/database_access.h>
#include <mime/InstalledTypes.h>
#include <mime/SnifferRules.h>
#include <mime/SupportingApps.h>
class BNode;
class BBitmap;
class BMessage;
class BString;
struct entry_ref;
namespace BPrivate {
namespace Storage {
namespace Mime {
class MimeSniffer;
// types of mime update functions that may be run asynchronously
typedef enum {
B_REG_UPDATE_MIME_INFO,
B_REG_CREATE_APP_META_MIME,
} mime_update_function;
class Database {
public:
class NotificationListener;
public:
Database(MimeSniffer* mimeSniffer,
NotificationListener* notificationListener);
~Database();
status_t InitCheck() const;
// Type management
status_t Install(const char *type);
status_t Delete(const char *type);
// Set()
status_t SetAppHint(const char *type, const entry_ref *ref);
status_t SetAttrInfo(const char *type, const BMessage *info);
status_t SetShortDescription(const char *type, const char *description);
status_t SetLongDescription(const char *type, const char *description);
status_t SetFileExtensions(const char *type, const BMessage *extensions);
status_t SetIcon(const char *type, const void *data, size_t dataSize,
icon_size which);
status_t SetIcon(const char *type, const void *data, size_t dataSize);
status_t SetIconForType(const char *type, const char *fileType,
const void *data, size_t dataSize, icon_size which);
status_t SetIconForType(const char *type, const char *fileType,
const void *data, size_t dataSize);
status_t SetPreferredApp(const char *type, const char *signature,
app_verb verb = B_OPEN);
status_t SetSnifferRule(const char *type, const char *rule);
status_t SetSupportedTypes(const char *type, const BMessage *types, bool fullSync);
// Non-atomic Get()
status_t GetInstalledSupertypes(BMessage *super_types);
status_t GetInstalledTypes(BMessage *types);
status_t GetInstalledTypes(const char *super_type,
BMessage *subtypes);
status_t GetSupportingApps(const char *type, BMessage *signatures);
status_t GetAssociatedTypes(const char *extension, BMessage *types);
// Sniffer
status_t GuessMimeType(const entry_ref *file, BString *result);
status_t GuessMimeType(const void *buffer, int32 length, BString *result);
status_t GuessMimeType(const char *filename, BString *result);
// Monitor
status_t StartWatching(BMessenger target);
status_t StopWatching(BMessenger target);
// Delete()
status_t DeleteAppHint(const char *type);
status_t DeleteAttrInfo(const char *type);
status_t DeleteShortDescription(const char *type);
status_t DeleteLongDescription(const char *type);
status_t DeleteFileExtensions(const char *type);
status_t DeleteIcon(const char *type, icon_size size);
status_t DeleteIcon(const char *type);
status_t DeleteIconForType(const char *type, const char *fileType,
icon_size which);
status_t DeleteIconForType(const char *type, const char *fileType);
status_t DeletePreferredApp(const char *type, app_verb verb = B_OPEN);
status_t DeleteSnifferRule(const char *type);
status_t DeleteSupportedTypes(const char *type, bool fullSync);
// deferred notifications
void DeferInstallNotification(const char* type);
void UndeferInstallNotification(const char* type);
private:
struct DeferredInstallNotification {
char type[B_MIME_TYPE_LENGTH];
bool notify;
};
status_t _SetStringValue(const char *type, int32 what,
const char* attribute, type_code attributeType,
size_t maxLength, const char *value);
// Functions to send monitor notifications
status_t _SendInstallNotification(const char *type);
status_t _SendDeleteNotification(const char *type);
status_t _SendMonitorUpdate(int32 which, const char *type,
const char *extraType, bool largeIcon, int32 action);
status_t _SendMonitorUpdate(int32 which, const char *type,
const char *extraType, int32 action);
status_t _SendMonitorUpdate(int32 which, const char *type,
bool largeIcon, int32 action);
status_t _SendMonitorUpdate(int32 which, const char *type,
int32 action);
status_t _SendMonitorUpdate(BMessage &msg);
DeferredInstallNotification* _FindDeferredInstallNotification(
const char* type, bool remove = false);
bool _CheckDeferredInstallNotification(int32 which, const char* type);
private:
status_t fStatus;
NotificationListener* fNotificationListener;
std::set<BMessenger> fMonitorMessengers;
AssociatedTypes fAssociatedTypes;
InstalledTypes fInstalledTypes;
SnifferRules fSnifferRules;
SupportingApps fSupportingApps;
BLocker fDeferredInstallNotificationsLocker;
BList fDeferredInstallNotifications;
};
class InstallNotificationDeferrer {
public:
InstallNotificationDeferrer(Database* database, const char* type)
:
fDatabase(database),
fType(type)
{
if (fDatabase != NULL && fType != NULL)
fDatabase->DeferInstallNotification(fType);
}
~InstallNotificationDeferrer()
{
if (fDatabase != NULL && fType != NULL)
fDatabase->UndeferInstallNotification(fType);
}
private:
Database* fDatabase;
const char* fType;
};
class Database::NotificationListener {
public:
virtual ~NotificationListener();
virtual status_t Notify(BMessage* message,
const BMessenger& target) = 0;
};
} // namespace Mime
} // namespace Storage
} // namespace BPrivate
#endif // _MIME_DATABASE_H
@@ -0,0 +1,42 @@
/*
* Copyright 2013, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Ingo Weinhold <[email protected]>
*/
#ifndef DATABASE_DIRECTORY_H
#define DATABASE_DIRECTORY_H
#include <MergedDirectory.h>
namespace BPrivate {
namespace Storage {
namespace Mime {
class DatabaseDirectory : public BMergedDirectory {
public:
DatabaseDirectory();
virtual ~DatabaseDirectory();
status_t Init(const char* superType = NULL);
protected:
virtual bool ShallPreferFirstEntry(const entry_ref& entry1,
int32 index1, const entry_ref& entry2,
int32 index2);
private:
bool _IsValidMimeTypeEntry(const entry_ref& entry);
};
} // namespace Mime
} // namespace Storage
} // namespace BPrivate
#endif // DATABASE_DIRECTORY_H
@@ -0,0 +1,68 @@
/*
* Copyright 2002-2006, Haiku.
* Distributed under the terms of the MIT License.
*
* Authors:
* Tyler Dauwalder
* Ingo Weinhold, [email protected]
* Axel Dörfler, [email protected]
*/
#ifndef _MIME_INSTALLED_TYPES_H
#define _MIME_INSTALLED_TYPES_H
#include <map>
#include <string>
#include <SupportDefs.h>
#include <mime/Supertype.h>
class BMessage;
namespace BPrivate {
namespace Storage {
namespace Mime {
class InstalledTypes {
public:
InstalledTypes();
~InstalledTypes();
status_t GetInstalledTypes(BMessage *types);
status_t GetInstalledTypes(const char *supertype, BMessage *types);
status_t GetInstalledSupertypes(BMessage *types);
status_t AddType(const char *type);
status_t RemoveType(const char *type);
private:
status_t _AddSupertype(const char *super,
std::map<std::string, Supertype>::iterator &i);
status_t _AddSubtype(const char *super, const char *sub);
status_t _AddSubtype(Supertype &super, const char *sub);
status_t _RemoveSupertype(const char *super);
status_t _RemoveSubtype(const char *super, const char *sub);
void _Unset();
void _ClearCachedMessages();
status_t _CreateMessageWithTypes(BMessage **result) const;
status_t _CreateMessageWithSupertypes(BMessage **result) const;
void _FillMessageWithSupertypes(BMessage *msg);
status_t _BuildInstalledTypesList();
std::map<std::string, Supertype> fSupertypes;
BMessage *fCachedMessage;
BMessage *fCachedSupertypesMessage;
bool fHaveDoneFullBuild;
};
} // namespace Mime
} // namespace Storage
} // namespace BPrivate
#endif // _MIME_INSTALLED_TYPES_H
@@ -0,0 +1,43 @@
/*
* Copyright 2013, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Ingo Weinhold <[email protected]>
*/
#ifndef _MIME_MIME_SNIFFER_H
#define _MIME_MIME_SNIFFER_H
#include <SupportDefs.h>
class BFile;
class BMimeType;
namespace BPrivate {
namespace Storage {
namespace Mime {
class MimeSniffer {
public:
virtual ~MimeSniffer();
virtual size_t MinimalBufferSize() = 0;
virtual float GuessMimeType(const char* fileName,
BMimeType* type) = 0;
virtual float GuessMimeType(BFile* file,
const void* buffer, int32 length,
BMimeType* type) = 0;
};
} // namespace Mime
} // namespace Storage
} // namespace BPrivate
#endif // _MIME_MIME_SNIFFER_H
@@ -0,0 +1,71 @@
/*
* Copyright 2002-2007, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef _MIME_SNIFFER_RULES_H
#define _MIME_SNIFFER_RULES_H
#include <SupportDefs.h>
#include <list>
#include <string>
class BFile;
class BString;
struct entry_ref;
namespace BPrivate {
namespace Storage {
namespace Sniffer {
class Rule;
}
namespace Mime {
class MimeSniffer;
class SnifferRules {
public:
SnifferRules(MimeSniffer* mimeSniffer);
~SnifferRules();
status_t GuessMimeType(const entry_ref *ref, BString *type);
status_t GuessMimeType(const void *buffer, int32 length, BString *type);
status_t SetSnifferRule(const char *type, const char *rule);
status_t DeleteSnifferRule(const char *type);
void PrintToStream() const;
struct sniffer_rule {
std::string type; // The mime type that own the rule
std::string rule_string; // The unparsed string version of the rule
BPrivate::Storage::Sniffer::Rule *rule; // The parsed rule
sniffer_rule(BPrivate::Storage::Sniffer::Rule *rule = NULL);
~sniffer_rule();
};
private:
status_t BuildRuleList();
status_t GuessMimeType(BFile* file, const void *buffer, int32 length,
BString *type);
ssize_t MaxBytesNeeded();
status_t ProcessType(const char *type, ssize_t *bytesNeeded);
std::list<sniffer_rule> fRuleList;
private:
MimeSniffer* fMimeSniffer;
ssize_t fMaxBytesNeeded;
bool fHaveDoneFullBuild;
};
} // namespace Mime
} // namespace Storage
} // namespace BPrivate
#endif // _MIME_SNIFFER_H
+50
View File
@@ -0,0 +1,50 @@
//----------------------------------------------------------------------
// This software is part of the OpenBeOS distribution and is covered
// by the OpenBeOS license.
//---------------------------------------------------------------------
/*!
\file Supertype.h
Supertype class declarations
*/
#ifndef _MIME_SUPERTYPE_H
#define _MIME_SUPERTYPE_H
#include <SupportDefs.h>
#include <string>
#include <set>
class BMessage;
namespace BPrivate {
namespace Storage {
namespace Mime {
class Supertype {
public:
Supertype(const char *super = NULL);
~Supertype();
status_t GetInstalledSubtypes(BMessage *types);
status_t AddSubtype(const char *sub);
status_t RemoveSubtype(const char *sub);
void SetName(const char *super);
const char* GetName();
status_t FillMessageWithTypes(BMessage &msg) const;
private:
status_t CreateMessageWithTypes(BMessage **result) const;
std::set<std::string> fSubtypes;
BMessage *fCachedMessage;
std::string fName;
};
} // namespace Mime
} // namespace Storage
} // namespace BPrivate
#endif // _MIME_SUPERTYPE_H
@@ -0,0 +1,51 @@
//----------------------------------------------------------------------
// This software is part of the OpenBeOS distribution and is covered
// by the OpenBeOS license.
//---------------------------------------------------------------------
/*!
\file SupportingApps.h
SupportingApps class declarations
*/
#ifndef _MIME_SUPPORTING_APPS_H
#define _MIME_SUPPORTING_APPS_H
#include <SupportDefs.h>
#include <map>
#include <set>
#include <string>
class BMessage;
namespace BPrivate {
namespace Storage {
namespace Mime {
class SupportingApps {
public:
SupportingApps();
~SupportingApps();
status_t GetSupportingApps(const char *type, BMessage *apps);
status_t SetSupportedTypes(const char *app, const BMessage *types, bool fullSync);
status_t DeleteSupportedTypes(const char *app, bool fullSync);
private:
status_t AddSupportingApp(const char *type, const char *app);
status_t RemoveSupportingApp(const char *type, const char *app);
status_t BuildSupportingAppsTable();
std::map<std::string, std::set<std::string> > fSupportedTypes; // app sig => set of supported types
std::map<std::string, std::set<std::string> > fSupportingApps; // mime type => set of supporting apps
std::map<std::string, std::set<std::string> > fStrandedTypes; // app sig => set of no longer supported types for whom the
// given app is still listed as a supporting app
bool fHaveDoneFullBuild;
};
} // namespace Mime
} // namespace Storage
} // namespace BPrivate
#endif // _MIME_SUPPORTING_APPS_H