TextSnifferAddon: Use MIME DB directly
A DatabaseLocation is passed to the constructor and used to verify that the sniffed MIME type is installed instead of BMimeType::IsInstalled(). This makes the add-on independent of the default MIME DB.
This commit is contained in:
@@ -14,9 +14,13 @@ namespace Storage {
|
|||||||
namespace Mime {
|
namespace Mime {
|
||||||
|
|
||||||
|
|
||||||
|
class DatabaseLocation;
|
||||||
|
|
||||||
|
|
||||||
class TextSnifferAddon : public BMimeSnifferAddon {
|
class TextSnifferAddon : public BMimeSnifferAddon {
|
||||||
public:
|
public:
|
||||||
TextSnifferAddon();
|
TextSnifferAddon(
|
||||||
|
DatabaseLocation* databaseLocation);
|
||||||
virtual ~TextSnifferAddon();
|
virtual ~TextSnifferAddon();
|
||||||
|
|
||||||
virtual size_t MinimalBufferSize();
|
virtual size_t MinimalBufferSize();
|
||||||
@@ -26,6 +30,9 @@ public:
|
|||||||
virtual float GuessMimeType(BFile* file,
|
virtual float GuessMimeType(BFile* file,
|
||||||
const void* buffer, int32 length,
|
const void* buffer, int32 length,
|
||||||
BMimeType* type);
|
BMimeType* type);
|
||||||
|
|
||||||
|
private:
|
||||||
|
DatabaseLocation* fDatabaseLocation;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -8,18 +8,22 @@
|
|||||||
|
|
||||||
#include <MimeType.h>
|
#include <MimeType.h>
|
||||||
|
|
||||||
|
#include <mime/DatabaseLocation.h>
|
||||||
|
|
||||||
|
|
||||||
namespace BPrivate {
|
namespace BPrivate {
|
||||||
namespace Storage {
|
namespace Storage {
|
||||||
namespace Mime {
|
namespace Mime {
|
||||||
|
|
||||||
|
|
||||||
static int file_ascmagic(const unsigned char *buf, size_t nbytes,
|
static int file_ascmagic(DatabaseLocation* databaseLocation,
|
||||||
BMimeType* mimeType);
|
const unsigned char *buf, size_t nbytes, BMimeType* mimeType);
|
||||||
|
|
||||||
|
|
||||||
// constructor
|
// constructor
|
||||||
TextSnifferAddon::TextSnifferAddon()
|
TextSnifferAddon::TextSnifferAddon(DatabaseLocation* databaseLocation)
|
||||||
|
:
|
||||||
|
fDatabaseLocation(databaseLocation)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,7 +52,8 @@ float
|
|||||||
TextSnifferAddon::GuessMimeType(BFile* file, const void* buffer, int32 length,
|
TextSnifferAddon::GuessMimeType(BFile* file, const void* buffer, int32 length,
|
||||||
BMimeType* type)
|
BMimeType* type)
|
||||||
{
|
{
|
||||||
if (file_ascmagic((const unsigned char*)buffer, length, type)) {
|
if (file_ascmagic(fDatabaseLocation, (const unsigned char*)buffer, length,
|
||||||
|
type)) {
|
||||||
// If the buffer is very short, we return a lower priority. Maybe
|
// If the buffer is very short, we return a lower priority. Maybe
|
||||||
// someone else knows better.
|
// someone else knows better.
|
||||||
if (length < 20)
|
if (length < 20)
|
||||||
@@ -128,7 +133,8 @@ static int ascmatch(const unsigned char *, const my_unichar *, size_t);
|
|||||||
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
file_ascmagic(const unsigned char *buf, size_t nbytes, BMimeType* mimeType)
|
file_ascmagic(DatabaseLocation* databaseLocation, const unsigned char *buf,
|
||||||
|
size_t nbytes, BMimeType* mimeType)
|
||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
unsigned char *nbuf = NULL;
|
unsigned char *nbuf = NULL;
|
||||||
@@ -330,14 +336,16 @@ done:
|
|||||||
|
|
||||||
bool found = false;
|
bool found = false;
|
||||||
if (subtypeMimeSpecific != NULL) {
|
if (subtypeMimeSpecific != NULL) {
|
||||||
mimeType->SetTo(subtypeMimeSpecific);
|
if (databaseLocation->IsInstalled(subtypeMimeSpecific)) {
|
||||||
if (mimeType->IsInstalled())
|
mimeType->SetTo(subtypeMimeSpecific);
|
||||||
found = true;
|
found = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (!found && subtypeMimeGeneric != NULL) {
|
if (!found && subtypeMimeGeneric != NULL) {
|
||||||
mimeType->SetTo(subtypeMimeGeneric);
|
if (databaseLocation->IsInstalled(subtypeMimeGeneric)) {
|
||||||
if (mimeType->IsInstalled())
|
mimeType->SetTo(subtypeMimeGeneric);
|
||||||
found = true;
|
found = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (!found)
|
if (!found)
|
||||||
mimeType->SetTo("text/plain");
|
mimeType->SetTo("text/plain");
|
||||||
|
|||||||
@@ -51,7 +51,8 @@ init_mime_sniffer_add_on_manager()
|
|||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
MimeSnifferAddonManager* manager = MimeSnifferAddonManager::Default();
|
MimeSnifferAddonManager* manager = MimeSnifferAddonManager::Default();
|
||||||
manager->AddMimeSnifferAddon(new(nothrow) TextSnifferAddon());
|
manager->AddMimeSnifferAddon(new(nothrow) TextSnifferAddon(
|
||||||
|
BPrivate::Storage::Mime::default_database_location()));
|
||||||
return manager;
|
return manager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user