Skip to content

Commit

Permalink
Check the MIME type when drag&dropping a file to Test/Action text
Browse files Browse the repository at this point in the history
control fields.

Update the MIME info if the BEOS:TYPE info of a file node is missing.

Now that B_TRANSLATE_MARK_CONTEXT has been implemented in hrev51350, use it to
replace B_TRANSLATE_MARK_ALL.
  • Loading branch information
owenca authored and humdinger committed Sep 12, 2017
1 parent a230720 commit d2d3aad
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 52 deletions.
85 changes: 42 additions & 43 deletions sources/RuleRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <Entry.h>
#include <FindDirectory.h>
#include <Mime.h>
#include <NodeInfo.h>
#include <Path.h>
#include <Roster.h>

Expand Down Expand Up @@ -248,6 +247,7 @@ 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";

Expand Down Expand Up @@ -1432,36 +1432,30 @@ GetDirectoryPath(BString& str, const entry_ref& ref)
static bool
GetPathForRef(BString& str, const entry_ref& ref, const char* mime)
{
// The code below checks if the MIME type matches. Skipped for now because
// Haiku doesn't recognize some file types (zip, script, etc.) from other
// file systems.
#if 0
BPath path(&ref);
if (path.InitCheck() != B_OK)
return false;

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

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

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

BNodeInfo nodeInfo(&node);
if (nodeInfo.InitCheck() != B_OK)
return false;

char type[B_MIME_TYPE_LENGTH];
if (nodeInfo.GetType(type) != B_OK || strstr(mime, type) == NULL)
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;
}
}
#endif

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

str = path.Path();
return true;
Expand Down Expand Up @@ -1490,22 +1484,22 @@ SetTextForType(BString& text, int8 type, const entry_ref& ref, bool isTest)
default:
return false;
}
else
switch (type) {
case ACTION_MOVE:
case ACTION_COPY:
return GetDirectoryPath(text, target);
case ACTION_RENAME:
text = ref.name;
return true;
case ACTION_COMMAND:
return !entry.IsFile() ? false
: GetPathForRef(text, ref, scriptMime);
case ACTION_ARCHIVE:
return GetPathForRef(text, target, archiveMime);
default:
return false;
}

switch (type) {
case ACTION_MOVE:
case ACTION_COPY:
return GetDirectoryPath(text, target);
case ACTION_RENAME:
text = ref.name;
return true;
case ACTION_COMMAND:
return !entry.IsFile() ? false
: GetPathForRef(text, ref, scriptMime);
case ACTION_ARCHIVE:
return GetPathForRef(text, target, archiveMime);
default:
return false;
}
}


Expand All @@ -1516,12 +1510,17 @@ SetTextForMime(BString& text, const entry_ref& ref)
if (node.InitCheck() != B_OK)
return false;

BNodeInfo nodeInfo(&node);
if (nodeInfo.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;
}

char mimeType[B_MIME_TYPE_LENGTH];
if (nodeInfo.GetType(mimeType) != B_OK)
BString mimeType;
if (node.ReadAttrString("BEOS:TYPE", &mimeType) != B_OK)
return false;

text = mimeType;
Expand Down
1 change: 1 addition & 0 deletions sources/RuleRunner.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ 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;

Expand Down
4 changes: 2 additions & 2 deletions sources/RuleTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ RuleTab::_BuildLayout()

fAddRemoveButtons = new AddRemoveButtons(MSG_SHOW_ADD_WINDOW,
MSG_REMOVE_RULE, this, height, B_USE_DEFAULT_SPACING,
B_TRANSLATE_MARK_ALL("Add rule", ADD_REMVOE_BUTTONS_CONTEXT, NULL),
B_TRANSLATE_MARK_ALL("Remove rule", ADD_REMVOE_BUTTONS_CONTEXT, NULL));
B_TRANSLATE_MARK_CONTEXT("Add rule", ADD_REMVOE_BUTTONS_CONTEXT),
B_TRANSLATE_MARK_CONTEXT("Remove rule", ADD_REMVOE_BUTTONS_CONTEXT));
fAddRemoveButtons->SetRemoveEnabled(false);

fMoveUpButton = new BButton("moveupbutton", B_TRANSLATE("Move up"),
Expand Down
22 changes: 16 additions & 6 deletions sources/TypedRefFilter.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#include "TypedRefFilter.h"

#include <NodeInfo.h>
#include <fs_attr.h>

#include <Mime.h>
#include <Path.h>

#include "RuleRunner.h"

TypedRefFilter::TypedRefFilter()
:
Expand Down Expand Up @@ -56,7 +61,7 @@ TypedRefFilter::Filter(const entry_ref* ref, BNode* node, struct stat_beos* st,
const char* filetype)
{
struct stat targetStat;
char targetType[B_MIME_TYPE_LENGTH];
BString targetType;

bool isLink = S_ISLNK(st->st_mode);
if (isLink) {
Expand All @@ -67,9 +72,14 @@ TypedRefFilter::Filter(const entry_ref* ref, BNode* node, struct stat_beos* st,

BNode target(&entry);
if (target.InitCheck() == B_OK) {
BNodeInfo nodeInfo(&target);
if (nodeInfo.InitCheck() == B_OK
&& nodeInfo.GetType(targetType) == 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;
}
}
Expand All @@ -88,5 +98,5 @@ TypedRefFilter::Filter(const entry_ref* ref, BNode* node, struct stat_beos* st,
return true;

return isDir || fFileType.FindFirst(filetype) != B_ERROR
|| strcmp(filetype, "application/octet-stream") == 0;
|| strcmp(filetype, genericMime) == 0;
}
2 changes: 2 additions & 0 deletions sources/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ App::FileRef(entry_ref ref)
}


#if 0
void
App::SetupTypeMenu()
{
Expand Down Expand Up @@ -276,6 +277,7 @@ App::SetupTypeMenu()
menu->Archive(&gArchivedTypeMenu);
delete menu;
}
#endif


void
Expand Down
2 changes: 1 addition & 1 deletion sources/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class App : public BApplication
void ArgvReceived(int32 argc, char** argv);
void ReadyToRun();

void SetupTypeMenu();
// void SetupTypeMenu();
void ShowHTML(const char* docfile);
void FileRef(entry_ref ref);
char GetDecimalMark() { return fDecimalMark; }
Expand Down

0 comments on commit d2d3aad

Please sign in to comment.