Refactor MIME DB access
* Add class DatabaseLocation. It contains a list of the MIME DB
directory paths plus methods to access type files.
* Move all low-level MIME DB access functions from
database_{support,access} to DatabaseLocation. All code that formerly
used those now requires a DatabaseLocation object. In BMimeType and in
the registrar the default object is used, but the low-level classes
can now be reused with different locations.
* Move get_icon_data() from database_access to database_support and
delete the former, which is now empty.
This commit is contained in:
@@ -21,12 +21,15 @@ namespace BPrivate {
|
||||
namespace Storage {
|
||||
namespace Mime {
|
||||
|
||||
|
||||
class DatabaseLocation;
|
||||
class MimeSniffer;
|
||||
|
||||
|
||||
class AssociatedTypes {
|
||||
public:
|
||||
AssociatedTypes(MimeSniffer* mimeSniffer);
|
||||
AssociatedTypes(DatabaseLocation* databaseLocation,
|
||||
MimeSniffer* mimeSniffer);
|
||||
~AssociatedTypes();
|
||||
|
||||
status_t GetAssociatedTypes(const char *extension, BMessage *types);
|
||||
@@ -50,8 +53,9 @@ private:
|
||||
std::map<std::string, std::set<std::string> > fAssociatedTypes; // file extension => set of associated mime types
|
||||
|
||||
private:
|
||||
MimeSniffer* fMimeSniffer;
|
||||
bool fHaveDoneFullBuild;
|
||||
DatabaseLocation* fDatabaseLocation;
|
||||
MimeSniffer* fMimeSniffer;
|
||||
bool fHaveDoneFullBuild;
|
||||
};
|
||||
|
||||
} // namespace Mime
|
||||
|
||||
@@ -19,7 +19,6 @@
|
||||
#include <StorageDefs.h>
|
||||
|
||||
#include <mime/AssociatedTypes.h>
|
||||
#include <mime/database_access.h>
|
||||
#include <mime/InstalledTypes.h>
|
||||
#include <mime/SnifferRules.h>
|
||||
#include <mime/SupportingApps.h>
|
||||
@@ -36,8 +35,11 @@ namespace BPrivate {
|
||||
namespace Storage {
|
||||
namespace Mime {
|
||||
|
||||
|
||||
class DatabaseLocation;
|
||||
class MimeSniffer;
|
||||
|
||||
|
||||
// types of mime update functions that may be run asynchronously
|
||||
typedef enum {
|
||||
B_REG_UPDATE_MIME_INFO,
|
||||
@@ -49,16 +51,18 @@ class Database {
|
||||
class NotificationListener;
|
||||
|
||||
public:
|
||||
Database(MimeSniffer* mimeSniffer,
|
||||
Database(DatabaseLocation* databaseLocation, MimeSniffer* mimeSniffer,
|
||||
NotificationListener* notificationListener);
|
||||
~Database();
|
||||
|
||||
|
||||
status_t InitCheck() const;
|
||||
|
||||
DatabaseLocation* Location() const { return fLocation; }
|
||||
|
||||
// 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);
|
||||
@@ -142,6 +146,7 @@ class Database {
|
||||
|
||||
private:
|
||||
status_t fStatus;
|
||||
DatabaseLocation* fLocation;
|
||||
NotificationListener* fNotificationListener;
|
||||
std::set<BMessenger> fMonitorMessengers;
|
||||
AssociatedTypes fAssociatedTypes;
|
||||
|
||||
@@ -17,12 +17,16 @@ namespace Storage {
|
||||
namespace Mime {
|
||||
|
||||
|
||||
class DatabaseLocation;
|
||||
|
||||
|
||||
class DatabaseDirectory : public BMergedDirectory {
|
||||
public:
|
||||
DatabaseDirectory();
|
||||
virtual ~DatabaseDirectory();
|
||||
|
||||
status_t Init(const char* superType = NULL);
|
||||
status_t Init(DatabaseLocation* databaseLocation,
|
||||
const char* superType = NULL);
|
||||
|
||||
protected:
|
||||
virtual bool ShallPreferFirstEntry(const entry_ref& entry1,
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* Copyright 2013, Haiku, Inc. All Rights Reserved.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*
|
||||
* Authors:
|
||||
* Ingo Weinhold <[email protected]>
|
||||
*/
|
||||
#ifndef _MIME_DATABASE_LOCATION_H
|
||||
#define _MIME_DATABASE_LOCATION_H
|
||||
|
||||
|
||||
#include <Mime.h>
|
||||
#include <StringList.h>
|
||||
|
||||
|
||||
namespace BPrivate {
|
||||
namespace Storage {
|
||||
namespace Mime {
|
||||
|
||||
|
||||
class DatabaseLocation {
|
||||
public:
|
||||
DatabaseLocation();
|
||||
~DatabaseLocation();
|
||||
|
||||
bool AddDirectory(const BString& directory);
|
||||
|
||||
// paths
|
||||
|
||||
const BStringList& Directories() const
|
||||
{ return fDirectories; }
|
||||
BString WritableDirectory() const
|
||||
{ return fDirectories.StringAt(0); }
|
||||
|
||||
BString WritablePathForType(const char* type) const
|
||||
{ return _TypeToFilename(type, 0); }
|
||||
|
||||
// opening type nodes
|
||||
|
||||
status_t OpenType(const char* type, BNode& _node) const;
|
||||
status_t OpenOrCreateType(const char* type,
|
||||
BNode& _node, bool* _didCreate = NULL)
|
||||
const;
|
||||
|
||||
// generic type attributes access
|
||||
|
||||
ssize_t ReadAttribute(const char* type,
|
||||
const char* attribute, void* data,
|
||||
size_t length, type_code datatype) const;
|
||||
status_t ReadMessageAttribute(const char* type,
|
||||
const char* attribute, BMessage& _message)
|
||||
const;
|
||||
status_t ReadStringAttribute(const char* type,
|
||||
const char* attribute, BString& _string)
|
||||
const;
|
||||
|
||||
status_t WriteAttribute(const char* type,
|
||||
const char* attribute, const void* data,
|
||||
size_t length, type_code datatype,
|
||||
bool* _didCreate) const;
|
||||
status_t WriteMessageAttribute(const char* type,
|
||||
const char* attribute,
|
||||
const BMessage& message, bool* _didCreate)
|
||||
const;
|
||||
|
||||
status_t DeleteAttribute(const char* type,
|
||||
const char* attribute) const;
|
||||
|
||||
// type attribute convenience getters
|
||||
|
||||
status_t GetAppHint(const char* type, entry_ref& _ref);
|
||||
status_t GetAttributesInfo(const char* type,
|
||||
BMessage& _info);
|
||||
status_t GetShortDescription(const char* type,
|
||||
char* description);
|
||||
status_t GetLongDescription(const char* type,
|
||||
char* description);
|
||||
status_t GetFileExtensions(const char* type,
|
||||
BMessage& _extensions);
|
||||
status_t GetIcon(const char* type, BBitmap& _icon,
|
||||
icon_size size);
|
||||
status_t GetIcon(const char* type, uint8*& _data,
|
||||
size_t& _size);
|
||||
status_t GetIconForType(const char* type,
|
||||
const char* fileType, BBitmap& _icon,
|
||||
icon_size which);
|
||||
status_t GetIconForType(const char* type,
|
||||
const char* fileType, uint8*& _data,
|
||||
size_t& _size);
|
||||
status_t GetPreferredApp(const char* type,
|
||||
char* signature, app_verb verb);
|
||||
status_t GetSnifferRule(const char* type,
|
||||
BString& _result);
|
||||
status_t GetSupportedTypes(const char* type,
|
||||
BMessage& _types);
|
||||
|
||||
bool IsInstalled(const char* type);
|
||||
|
||||
private:
|
||||
BString _TypeToFilename(const char* type, int32 index)
|
||||
const;
|
||||
status_t _OpenType(const char* type, BNode& _node,
|
||||
int32& _index) const;
|
||||
status_t _CreateTypeNode(const char* type, BNode& _node)
|
||||
const;
|
||||
status_t _CopyTypeNode(BNode& source, const char* type,
|
||||
BNode& _target) const;
|
||||
|
||||
private:
|
||||
BStringList fDirectories;
|
||||
};
|
||||
|
||||
|
||||
} // namespace Mime
|
||||
} // namespace Storage
|
||||
} // namespace BPrivate
|
||||
|
||||
|
||||
#endif // _MIME_DATABASE_LOCATION_H
|
||||
@@ -25,9 +25,13 @@ namespace BPrivate {
|
||||
namespace Storage {
|
||||
namespace Mime {
|
||||
|
||||
|
||||
class DatabaseLocation;
|
||||
|
||||
|
||||
class InstalledTypes {
|
||||
public:
|
||||
InstalledTypes();
|
||||
InstalledTypes(DatabaseLocation* databaseLocation);
|
||||
~InstalledTypes();
|
||||
|
||||
status_t GetInstalledTypes(BMessage *types);
|
||||
@@ -55,6 +59,8 @@ class InstalledTypes {
|
||||
|
||||
status_t _BuildInstalledTypesList();
|
||||
|
||||
private:
|
||||
DatabaseLocation* fDatabaseLocation;
|
||||
std::map<std::string, Supertype> fSupertypes;
|
||||
BMessage *fCachedMessage;
|
||||
BMessage *fCachedSupertypesMessage;
|
||||
|
||||
@@ -25,12 +25,14 @@ namespace Sniffer {
|
||||
|
||||
namespace Mime {
|
||||
|
||||
|
||||
class DatabaseLocation;
|
||||
class MimeSniffer;
|
||||
|
||||
|
||||
class SnifferRules {
|
||||
public:
|
||||
SnifferRules(MimeSniffer* mimeSniffer);
|
||||
SnifferRules(DatabaseLocation* databaseLocation, MimeSniffer* mimeSniffer);
|
||||
~SnifferRules();
|
||||
|
||||
status_t GuessMimeType(const entry_ref *ref, BString *type);
|
||||
@@ -59,9 +61,10 @@ private:
|
||||
std::list<sniffer_rule> fRuleList;
|
||||
|
||||
private:
|
||||
MimeSniffer* fMimeSniffer;
|
||||
ssize_t fMaxBytesNeeded;
|
||||
bool fHaveDoneFullBuild;
|
||||
DatabaseLocation* fDatabaseLocation;
|
||||
MimeSniffer* fMimeSniffer;
|
||||
ssize_t fMaxBytesNeeded;
|
||||
bool fHaveDoneFullBuild;
|
||||
};
|
||||
|
||||
} // namespace Mime
|
||||
|
||||
@@ -22,11 +22,15 @@ namespace BPrivate {
|
||||
namespace Storage {
|
||||
namespace Mime {
|
||||
|
||||
|
||||
class DatabaseLocation;
|
||||
|
||||
|
||||
class SupportingApps {
|
||||
public:
|
||||
SupportingApps();
|
||||
SupportingApps(DatabaseLocation* databaseLocation);
|
||||
~SupportingApps();
|
||||
|
||||
|
||||
status_t GetSupportingApps(const char *type, BMessage *apps);
|
||||
|
||||
status_t SetSupportedTypes(const char *app, const BMessage *types, bool fullSync);
|
||||
@@ -41,6 +45,9 @@ private:
|
||||
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
|
||||
|
||||
private:
|
||||
DatabaseLocation* fDatabaseLocation;
|
||||
bool fHaveDoneFullBuild;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
//----------------------------------------------------------------------
|
||||
// This software is part of the OpenBeOS distribution and is covered
|
||||
// by the OpenBeOS license.
|
||||
//---------------------------------------------------------------------
|
||||
/*!
|
||||
\file database_access.h
|
||||
Mime database atomic read functions and miscellany declarations
|
||||
*/
|
||||
|
||||
#ifndef _MIME_DATABASE_ACCESS_H
|
||||
#define _MIME_DATABASE_ACCESS_H
|
||||
|
||||
#include <Mime.h>
|
||||
|
||||
class BNode;
|
||||
class BBitmap;
|
||||
class BMessage;
|
||||
|
||||
namespace BPrivate {
|
||||
namespace Storage {
|
||||
namespace Mime {
|
||||
|
||||
// Get() functions
|
||||
status_t get_app_hint(const char *type, entry_ref *ref);
|
||||
status_t get_attr_info(const char *type, BMessage *info);
|
||||
status_t get_short_description(const char *type, char *description);
|
||||
status_t get_long_description(const char *type, char *description);
|
||||
status_t get_file_extensions(const char *type, BMessage *extensions);
|
||||
status_t get_icon(const char *type, BBitmap *icon, icon_size size);
|
||||
status_t get_icon(const char *type, uint8** data, size_t* size);
|
||||
status_t get_icon_for_type(const char *type, const char *fileType,
|
||||
BBitmap *icon, icon_size which);
|
||||
status_t get_icon_for_type(const char *type, const char *fileType,
|
||||
uint8** data, size_t* size);
|
||||
status_t get_preferred_app(const char *type, char *signature, app_verb verb);
|
||||
status_t get_sniffer_rule(const char *type, BString *result);
|
||||
status_t get_supported_types(const char *type, BMessage *types);
|
||||
|
||||
bool is_installed(const char *type);
|
||||
|
||||
// Called by BMimeType to get properly formatted icon data ready
|
||||
// to be shipped off to SetIcon*() and written to the database
|
||||
status_t get_icon_data(const BBitmap *icon, icon_size size, void **data, int32 *dataSize);
|
||||
|
||||
} // namespace Mime
|
||||
} // namespace Storage
|
||||
} // namespace BPrivate
|
||||
|
||||
#endif // _MIME_DATABASE_H
|
||||
@@ -1,27 +1,25 @@
|
||||
/*
|
||||
* Copyright 2002-2007, Haiku, Inc. All Rights Reserved.
|
||||
* Copyright 2002-2013, Haiku, Inc. All Rights Reserved.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
#ifndef _MIME_DATABASE_SUPPORT_H
|
||||
#define _MIME_DATABASE_SUPPORT_H
|
||||
|
||||
|
||||
#include <StorageDefs.h>
|
||||
#include <String.h>
|
||||
#include <Mime.h>
|
||||
#include <SupportDefs.h>
|
||||
|
||||
|
||||
class BNode;
|
||||
class BMessage;
|
||||
class BStringList;
|
||||
class BBitmap;
|
||||
|
||||
|
||||
namespace BPrivate {
|
||||
namespace Storage {
|
||||
namespace Mime {
|
||||
|
||||
// Database directory
|
||||
const BStringList& get_database_directories();
|
||||
BString get_writable_database_directory();
|
||||
|
||||
class DatabaseLocation;
|
||||
|
||||
|
||||
// Attribute Prefixes
|
||||
extern const char *kMiniIconAttrPrefix;
|
||||
@@ -75,24 +73,18 @@ extern const char *kMetaMimeType;
|
||||
// Error codes (to be used only by BPrivate::Storage::Mime members)
|
||||
extern const status_t kMimeGuessFailureError;
|
||||
|
||||
BString type_to_writable_filename(const char *type);
|
||||
|
||||
status_t open_type(const char *type, BNode *result);
|
||||
status_t open_or_create_type(const char *type, BNode *result, bool *didCreate);
|
||||
DatabaseLocation* default_database_location();
|
||||
|
||||
ssize_t read_mime_attr(const char *type, const char *attr, void *data,
|
||||
size_t len, type_code datatype);
|
||||
status_t read_mime_attr_message(const char *type, const char *attr, BMessage *msg);
|
||||
status_t read_mime_attr_string(const char *type, const char *attr, BString *str);
|
||||
status_t write_mime_attr(const char *type, const char *attr, const void *data,
|
||||
size_t len, type_code datatype, bool *didCreate);
|
||||
status_t write_mime_attr_message(const char *type, const char *attr,
|
||||
const BMessage *msg, bool *didCreate);
|
||||
// Called by BMimeType to get properly formatted icon data ready
|
||||
// to be shipped off to SetIcon*() and written to the database
|
||||
status_t get_icon_data(const BBitmap *icon, icon_size size, void **data,
|
||||
int32 *dataSize);
|
||||
|
||||
status_t delete_attribute(const char *type, const char *attr);
|
||||
|
||||
} // namespace Mime
|
||||
} // namespace Storage
|
||||
} // namespace BPrivate
|
||||
|
||||
|
||||
#endif // _MIME_DATABASE_SUPPORT_H
|
||||
|
||||
Reference in New Issue
Block a user