Skip to content

Commit

Permalink
Rework sniffing MIME types and related filtering and drag & drop.
Browse files Browse the repository at this point in the history
Clean up #include's in RuleRunner and TypedRefFilter.
  • Loading branch information
owenca authored and humdinger committed Sep 16, 2017
1 parent d2d3aad commit 774b5ca
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 75 deletions.
4 changes: 2 additions & 2 deletions sources/ActionView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ ActionView::MessageReceived(BMessage* msg)
const char* fileType;
switch (fType) {
case ACTION_ARCHIVE:
fileType = archiveMime;
fileType = kArchiveMime;
break;
case ACTION_COMMAND:
fileType = scriptMime;
fileType = kScriptMime;
break;
default:
fileType = "";
Expand Down
67 changes: 23 additions & 44 deletions sources/RuleRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,23 @@
Owen Pan <[email protected]>, 2017
*/

#include "RuleRunner.h"

#include <fs_attr.h>

#include <Catalog.h>
#include <Directory.h>
#include <Entry.h>
#include <FindDirectory.h>
#include <Mime.h>
#include <MimeType.h>
#include <Path.h>
#include <Roster.h>

#include "ConflictWindow.h"
#include "CppSQLite3.h"
#include "Database.h"
#include "ConflictWindow.h"
#include "FSUtils.h"
#include "main.h"
#include "PatternProcessor.h"
#include "RuleRunner.h"

#include <fs_attr.h>
#include <stdlib.h>

/*
FilerAction message fields:
Expand Down Expand Up @@ -247,9 +246,8 @@ const NamePair sActions[] = {
};
const unsigned nActions = sizeof(sActions) / sizeof(sActions[0]);

const char* const genericMime = "application/octet-stream";
const char* const archiveMime = "application/zip";
const char* const scriptMime = "text/plain application/x-vnd.Be-elfexecutable";
const char* const kArchiveMime = "application/zip";
const char* const kScriptMime = "text/plain";


RuleRunner::RuleRunner()
Expand Down Expand Up @@ -1432,31 +1430,25 @@ GetDirectoryPath(BString& str, const entry_ref& ref)
static bool
GetPathForRef(BString& str, const entry_ref& ref, const char* mime)
{
BPath path(&ref);
if (path.InitCheck() != B_OK)
return false;

BEntry entry(&ref, true);
if (entry.InitCheck() != B_OK)
return false;

if (!entry.IsDirectory()) {
BNode node(&entry);
if (node.InitCheck() != B_OK)
entry_ref target;
if (entry.GetRef(&target) != B_OK)
return false;

attr_info info;
if (node.GetAttrInfo("BEOS:TYPE", &info) == B_OK
|| update_mime_info(path.Path(), NULL, 1,
B_UPDATE_MIME_INFO_NO_FORCE) == B_OK) {
BString mimeType;
if (node.ReadAttrString("BEOS:TYPE", &mimeType) == B_OK
&& strstr(mime, mimeType) == NULL
&& strcmp(mimeType, genericMime))
return false;
}
BMimeType mimeType;
if (BMimeType::GuessMimeType(&target, &mimeType) != B_OK
|| strcmp(mimeType.Type(), mime))
return false;
}

BPath path(&ref);
if (path.InitCheck() != B_OK)
return false;

str = path.Path();
return true;
}
Expand Down Expand Up @@ -1494,9 +1486,9 @@ SetTextForType(BString& text, int8 type, const entry_ref& ref, bool isTest)
return true;
case ACTION_COMMAND:
return !entry.IsFile() ? false
: GetPathForRef(text, ref, scriptMime);
: GetPathForRef(text, ref, kScriptMime);
case ACTION_ARCHIVE:
return GetPathForRef(text, target, archiveMime);
return GetPathForRef(text, target, kArchiveMime);
default:
return false;
}
Expand All @@ -1506,24 +1498,11 @@ SetTextForType(BString& text, int8 type, const entry_ref& ref, bool isTest)
bool
SetTextForMime(BString& text, const entry_ref& ref)
{
BNode node(&ref);
if (node.InitCheck() != B_OK)
return false;

attr_info info;
if (node.GetAttrInfo("BEOS:TYPE", &info) != B_OK) {
BPath path(&ref);
if (path.InitCheck() != B_OK
|| update_mime_info(path.Path(), NULL, 1,
B_UPDATE_MIME_INFO_NO_FORCE) != B_OK)
return false;
}

BString mimeType;
if (node.ReadAttrString("BEOS:TYPE", &mimeType) != B_OK)
BMimeType mimeType;
if (BMimeType::GuessMimeType(&ref, &mimeType) != B_OK)
return false;

text = mimeType;
text = mimeType.Type();
return true;
}

Expand Down
9 changes: 2 additions & 7 deletions sources/RuleRunner.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
#define RULERUNNER_H

#include "FilerRule.h"
#include "ObjectList.h"

#include <Entry.h>
#include <Message.h>

struct NamePair
{
Expand All @@ -31,9 +27,8 @@ extern const unsigned nActions;
extern const char* const sSizeUnits[];
extern const unsigned nSizeUnits;

extern const char* const genericMime;
extern const char* const archiveMime;
extern const char* const scriptMime;
extern const char* const kArchiveMime;
extern const char* const kScriptMime;

enum {
TEST_TYPE_NULL = 0,
Expand Down
30 changes: 10 additions & 20 deletions sources/TypedRefFilter.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#include "TypedRefFilter.h"

#include <fs_attr.h>
#include <MimeType.h>

#include <Mime.h>
#include <Path.h>
#include <compat/sys/stat.h>

#include "RuleRunner.h"

TypedRefFilter::TypedRefFilter()
:
Expand Down Expand Up @@ -61,27 +59,17 @@ TypedRefFilter::Filter(const entry_ref* ref, BNode* node, struct stat_beos* st,
const char* filetype)
{
struct stat targetStat;
BString targetType;

entry_ref target(*ref);
bool isLink = S_ISLNK(st->st_mode);

if (isLink) {
BEntry entry(ref, true);
if (entry.InitCheck() == B_OK) {
if (entry.GetStat(&targetStat) == B_OK)
st = (struct stat_beos*) (&targetStat);

BNode target(&entry);
if (target.InitCheck() == B_OK) {
attr_info info;
BPath path(ref);

if ((target.GetAttrInfo("BEOS:TYPE", &info) == B_OK
|| path.InitCheck() == B_OK
&& update_mime_info(path.Path(), NULL, 1,
B_UPDATE_MIME_INFO_NO_FORCE) == B_OK)
&& target.ReadAttrString("BEOS:TYPE", &targetType) == B_OK)
filetype = targetType;
}
if (entry.GetRef(&target) != B_OK)
target = *ref;
}
}

Expand All @@ -97,6 +85,8 @@ TypedRefFilter::Filter(const entry_ref* ref, BNode* node, struct stat_beos* st,
if (fFileType.IsEmpty())
return true;

return isDir || fFileType.FindFirst(filetype) != B_ERROR
|| strcmp(filetype, genericMime) == 0;
BMimeType mimeType;

return isDir || fFileType == (BMimeType::GuessMimeType(&target, &mimeType)
== B_OK ? mimeType.Type() : filetype);
}
2 changes: 0 additions & 2 deletions sources/TypedRefFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include <FilePanel.h>
#include <String.h>

#include <compat/sys/stat.h>

/*
This utility class is for filtering
*/
Expand Down

0 comments on commit 774b5ca

Please sign in to comment.