Tracker: style fixes to Model class

This commit is contained in:
John Scipione
2014-06-20 21:02:01 -04:00
parent 40ef04f38e
commit c8d910f76b
2 changed files with 217 additions and 179 deletions
+52 -16
View File
@@ -38,6 +38,9 @@ All rights reserved.
// Consider moving iconFrom logic to BPose // Consider moving iconFrom logic to BPose
// use a more efficient way of storing file type and preferred app strings // use a more efficient way of storing file type and preferred app strings
#include "Model.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -60,8 +63,6 @@ All rights reserved.
#include <Volume.h> #include <Volume.h>
#include <VolumeRoster.h> #include <VolumeRoster.h>
#include "Model.h"
#include "Attributes.h" #include "Attributes.h"
#include "Bitmaps.h" #include "Bitmaps.h"
#include "FindPanel.h" #include "FindPanel.h"
@@ -71,23 +72,29 @@ All rights reserved.
#include "Tracker.h" #include "Tracker.h"
#include "Utilities.h" #include "Utilities.h"
#ifdef CHECK_OPEN_MODEL_LEAKS #ifdef CHECK_OPEN_MODEL_LEAKS
BObjectList<Model>* writableOpenModelList = NULL; BObjectList<Model>* writableOpenModelList = NULL;
BObjectList<Model>* readOnlyOpenModelList = NULL; BObjectList<Model>* readOnlyOpenModelList = NULL;
#endif #endif
namespace BPrivate { namespace BPrivate {
extern extern
#ifdef _IMPEXP_BE #ifdef _IMPEXP_BE
_IMPEXP_BE _IMPEXP_BE
#endif #endif
bool CheckNodeIconHintPrivate(const BNode*, bool); bool CheckNodeIconHintPrivate(const BNode*, bool);
} } // namespace BPrivate
// #pragma mark - Model()
#undef B_TRANSLATION_CONTEXT #undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "Model" #define B_TRANSLATION_CONTEXT "Model"
Model::Model() Model::Model()
: :
fPreferredAppName(NULL), fPreferredAppName(NULL),
@@ -362,6 +369,9 @@ Model::Name() const
case kDesktopNode: case kDesktopNode:
return B_TRANSLATE_NOCOLLECT(kDesktopNodeName); return B_TRANSLATE_NOCOLLECT(kDesktopNodeName);
default:
break;
} }
if (fHasLocalizedName && gLocalizedNamePreferred) if (fHasLocalizedName && gLocalizedNamePreferred)
@@ -378,6 +388,7 @@ Model::OpenNode(bool writable)
return B_OK; return B_OK;
OpenNodeCommon(writable); OpenNodeCommon(writable);
return fStatus; return fStatus;
} }
@@ -399,6 +410,7 @@ Model::UpdateStatAndOpenNode(bool writable)
return fStatus; return fStatus;
OpenNodeCommon(writable); OpenNodeCommon(writable);
return fStatus; return fStatus;
} }
@@ -605,7 +617,8 @@ Model::FinishSettingUpType()
// disk again for models that do not have an icon defined by the node // disk again for models that do not have an icon defined by the node
if (IsNodeOpen() if (IsNodeOpen()
&& fBaseType != kLinkNode && fBaseType != kLinkNode
&& !CheckNodeIconHintPrivate(fNode, dynamic_cast<TTracker*>(be_app) == NULL) && !CheckNodeIconHintPrivate(fNode,
dynamic_cast<TTracker*>(be_app) == NULL)
&& !HasVectorIconHint(fNode)) { && !HasVectorIconHint(fNode)) {
// when checking for the node icon hint, if we are libtracker, // when checking for the node icon hint, if we are libtracker,
// only check for small icons - checking for the large icons // only check for small icons - checking for the large icons
@@ -689,9 +702,9 @@ Model::FinishSettingUpType()
} }
char name[B_FILE_NAME_LENGTH]; char name[B_FILE_NAME_LENGTH];
BVolume volume(NodeRef()->device); BVolume volume(NodeRef()->device);
if (volume.InitCheck() == B_OK && volume.GetName(name) == B_OK) { if (volume.InitCheck() == B_OK && volume.GetName(name) == B_OK) {
if (fVolumeName) if (fVolumeName != NULL)
DeletePreferredAppVolumeNameLinkTo(); DeletePreferredAppVolumeNameLinkTo();
fVolumeName = strdup(name); fVolumeName = strdup(name);
@@ -834,13 +847,15 @@ Model::GetPreferredAppForBrokenSymLink(BString &result)
= info.GetPreferredApp(result.LockBuffer(B_MIME_TYPE_LENGTH)); = info.GetPreferredApp(result.LockBuffer(B_MIME_TYPE_LENGTH));
result.UnlockBuffer(); result.UnlockBuffer();
if (error != B_OK) if (error != B_OK) {
// Tracker will have to do // Tracker will have to do
result = kTrackerSignature; result = kTrackerSignature;
}
} }
// Node monitor updating stuff // #pragma mark - Node monitor updating methods
void void
Model::UpdateEntryRef(const node_ref* dirNode, const char* name) Model::UpdateEntryRef(const node_ref* dirNode, const char* name)
@@ -949,7 +964,9 @@ Model::StatChanged()
return false; return false;
} }
// Mime handling stuff
// #pragma mark - Mime handling methods
bool bool
Model::IsDropTarget(const Model* forDocument, bool traverse) const Model::IsDropTarget(const Model* forDocument, bool traverse) const
@@ -964,7 +981,8 @@ Model::IsDropTarget(const Model* forDocument, bool traverse) const
default: default:
break; break;
} }
if (!forDocument)
if (forDocument == NULL)
return true; return true;
if (traverse) { if (traverse) {
@@ -983,9 +1001,10 @@ Model::IsDropTarget(const Model* forDocument, bool traverse) const
return SupportsMimeType(mimeType, 0) != kDoesNotSupportType; return SupportsMimeType(mimeType, 0) != kDoesNotSupportType;
} }
// do some mime-based matching // do some mime-based matching
const char* documentMimeType = forDocument->MimeType(); const char* documentMimeType = forDocument->MimeType();
if (!documentMimeType) if (documentMimeType == NULL)
return false; return false;
return SupportsMimeType(documentMimeType, 0) != kDoesNotSupportType; return SupportsMimeType(documentMimeType, 0) != kDoesNotSupportType;
@@ -995,11 +1014,11 @@ Model::IsDropTarget(const Model* forDocument, bool traverse) const
Model::CanHandleResult Model::CanHandleResult
Model::CanHandleDrops() const Model::CanHandleDrops() const
{ {
if (IsDirectory()) if (IsDirectory()) {
// directories take anything // directories take anything
// resolve permissions here // resolve permissions here
return kCanHandle; return kCanHandle;
}
if (IsSymLink()) { if (IsSymLink()) {
// descend into symlink and try again on it's target // descend into symlink and try again on it's target
@@ -1039,6 +1058,7 @@ enum {
kMatch kMatch
}; };
static int32 static int32
MatchMimeTypeString(/*const */BString* documentType, const char* handlerType) MatchMimeTypeString(/*const */BString* documentType, const char* handlerType)
{ {
@@ -1050,9 +1070,10 @@ MatchMimeTypeString(/*const */BString* documentType, const char* handlerType)
int32 supertypeOnlyLength = 0; int32 supertypeOnlyLength = 0;
const char* tmp = strstr(handlerType, "/"); const char* tmp = strstr(handlerType, "/");
if (!tmp) if (tmp == NULL) {
// no subtype - supertype string only // no subtype - supertype string only
supertypeOnlyLength = (int32)strlen(handlerType); supertypeOnlyLength = (int32)strlen(handlerType);
}
if (supertypeOnlyLength) { if (supertypeOnlyLength) {
// compare just the supertype // compare just the supertype
@@ -1089,8 +1110,8 @@ Model::SupportsMimeType(const char* type, const BObjectList<BString>* list,
return kDoesNotSupportType; return kDoesNotSupportType;
for (int32 index = 0; ; index++) { for (int32 index = 0; ; index++) {
// check if this model lists the type of dropped document as supported // check if this model lists the type of dropped document as supported
const char* mimeSignature; const char* mimeSignature;
ssize_t bufferLength; ssize_t bufferLength;
@@ -1109,7 +1130,7 @@ Model::SupportsMimeType(const char* type, const BObjectList<BString>* list,
int32 match; int32 match;
if (type || (list != NULL && list->IsEmpty())) { if (type != NULL || (list != NULL && list->IsEmpty())) {
BString typeString(type); BString typeString(type);
match = MatchMimeTypeString(&typeString, mimeSignature); match = MatchMimeTypeString(&typeString, mimeSignature);
} else { } else {
@@ -1148,6 +1169,7 @@ Model::IsDropTargetForList(const BObjectList<BString>* list) const
default: default:
break; break;
} }
return SupportsMimeType(0, list) != kDoesNotSupportType; return SupportsMimeType(0, list) != kDoesNotSupportType;
} }
@@ -1284,6 +1306,7 @@ Model::GetVersionString(BString &result, version_kind kind)
sprintf(vstr, "%" B_PRId32 ".%" B_PRId32 ".%" B_PRId32, version.major, sprintf(vstr, "%" B_PRId32 ".%" B_PRId32 ".%" B_PRId32, version.major,
version.middle, version.minor); version.middle, version.minor);
result = vstr; result = vstr;
return B_OK; return B_OK;
} }
@@ -1359,30 +1382,41 @@ Model::PrintToStream(int32 level, bool deep)
case kUnknownSource: case kUnknownSource:
PRINT(("unknown\n")); PRINT(("unknown\n"));
break; break;
case kUnknownNotFromNode: case kUnknownNotFromNode:
PRINT(("unknown but not from a node\n")); PRINT(("unknown but not from a node\n"));
break; break;
case kTrackerDefault: case kTrackerDefault:
PRINT(("tracker default\n")); PRINT(("tracker default\n"));
break; break;
case kTrackerSupplied: case kTrackerSupplied:
PRINT(("tracker supplied\n")); PRINT(("tracker supplied\n"));
break; break;
case kMetaMime: case kMetaMime:
PRINT(("metamime\n")); PRINT(("metamime\n"));
break; break;
case kPreferredAppForType: case kPreferredAppForType:
PRINT(("preferred app for type\n")); PRINT(("preferred app for type\n"));
break; break;
case kPreferredAppForNode: case kPreferredAppForNode:
PRINT(("preferred app for node\n")); PRINT(("preferred app for node\n"));
break; break;
case kNode: case kNode:
PRINT(("node\n")); PRINT(("node\n"));
break; break;
case kVolume: case kVolume:
PRINT(("volume\n")); PRINT(("volume\n"));
break; break;
default:
break;
} }
PRINT(("model %s opened %s \n", !IsNodeOpen() ? "not " : "", PRINT(("model %s opened %s \n", !IsNodeOpen() ? "not " : "",
@@ -1497,6 +1531,7 @@ Model::TrackIconSource(icon_size size)
#ifdef CHECK_OPEN_MODEL_LEAKS #ifdef CHECK_OPEN_MODEL_LEAKS
namespace BPrivate { namespace BPrivate {
#include <stdio.h> #include <stdio.h>
void void
@@ -1514,6 +1549,7 @@ DumpOpenModels(bool extensive)
printf("%s\n", readOnlyOpenModelList->ItemAt(index)->Name()); printf("%s\n", readOnlyOpenModelList->ItemAt(index)->Name());
} }
} }
if (writableOpenModelList) { if (writableOpenModelList) {
int32 count = writableOpenModelList->CountItems(); int32 count = writableOpenModelList->CountItems();
printf("%ld models open writable:\n", count); printf("%ld models open writable:\n", count);
+165 -163
View File
@@ -31,11 +31,10 @@ of Be Incorporated in the United States and other countries. Other brand product
names are registered trademarks or trademarks of their respective holders. names are registered trademarks or trademarks of their respective holders.
All rights reserved. All rights reserved.
*/ */
#ifndef _NU_MODEL_H
#define _NU_MODEL_H
// Dedicated to BModel // Dedicated to BModel
#ifndef _NU_MODEL_H
#define _NU_MODEL_H
#include <AppFileInfo.h> #include <AppFileInfo.h>
@@ -53,6 +52,7 @@ class BHandler;
class BEntry; class BEntry;
class BQuery; class BQuery;
#if __GNUC__ && __GNUC__ < 3 #if __GNUC__ && __GNUC__ < 3
// using std::stat instead of just stat here because of what // using std::stat instead of just stat here because of what
// seems to be a gcc bug involving namespace and struct stat interaction // seems to be a gcc bug involving namespace and struct stat interaction
@@ -63,6 +63,7 @@ typedef struct std::stat StatStruct;
typedef struct stat StatStruct; typedef struct stat StatStruct;
#endif #endif
namespace BPrivate { namespace BPrivate {
enum { enum {
@@ -74,197 +75,197 @@ enum {
}; };
class Model { class Model {
public: public:
Model(); Model();
Model(const Model &); Model(const Model &);
Model(const BEntry* entry, bool open = false, bool writable = false); Model(const BEntry* entry, bool open = false, bool writable = false);
Model(const entry_ref*, bool traverse = false, bool open = false, Model(const entry_ref*, bool traverse = false, bool open = false,
bool writable = false); bool writable = false);
Model(const node_ref* dirNode, const node_ref* node, const char* name, Model(const node_ref* dirNode, const node_ref* node, const char* name,
bool open = false, bool writable = false); bool open = false, bool writable = false);
~Model(); ~Model();
Model& operator=(const Model&); Model& operator=(const Model&);
status_t InitCheck() const; status_t InitCheck() const;
status_t SetTo(const BEntry*, bool open = false, status_t SetTo(const BEntry*, bool open = false,
bool writable = false); bool writable = false);
status_t SetTo(const entry_ref*, bool traverse = false, status_t SetTo(const entry_ref*, bool traverse = false,
bool open = false, bool writable = false); bool open = false, bool writable = false);
status_t SetTo(const node_ref* dirNode, const node_ref* node, status_t SetTo(const node_ref* dirNode, const node_ref* node,
const char* name, bool open = false, bool writable = false); const char* name, bool open = false, bool writable = false);
int CompareFolderNamesFirst(const Model* compareModel) const; int CompareFolderNamesFirst(const Model* compareModel) const;
// node management // node management
status_t OpenNode(bool writable = false); status_t OpenNode(bool writable = false);
// also used to switch from read-only to writable // also used to switch from read-only to writable
void CloseNode(); void CloseNode();
bool IsNodeOpen() const; bool IsNodeOpen() const;
bool IsNodeOpenForWriting() const; bool IsNodeOpenForWriting() const;
status_t UpdateStatAndOpenNode(bool writable = false); status_t UpdateStatAndOpenNode(bool writable = false);
// like OpenNode, called on zombie poses to check if they turned // like OpenNode, called on zombie poses to check if they turned
// real, starts by rereading the stat structure // real, starts by rereading the stat structure
// basic getters // basic getters
const char* Name() const; const char* Name() const;
const entry_ref* EntryRef() const; const entry_ref* EntryRef() const;
const node_ref* NodeRef() const; const node_ref* NodeRef() const;
const StatStruct* StatBuf() const; const StatStruct* StatBuf() const;
BNode* Node() const; BNode* Node() const;
// returns null if not Open // returns NULL if not open
void GetPath(BPath*) const; void GetPath(BPath*) const;
void GetEntry(BEntry*) const; void GetEntry(BEntry*) const;
const char* MimeType() const; const char* MimeType() const;
const char* PreferredAppSignature() const; const char* PreferredAppSignature() const;
// only not-null if not default for type and not self for app // only not-null if not default for type and not self for app
void SetPreferredAppSignature(const char*); void SetPreferredAppSignature(const char*);
void GetPreferredAppForBrokenSymLink(BString &result); void GetPreferredAppForBrokenSymLink(BString &result);
// special purpose call - if a symlink is unresolvable, it makes // special purpose call - if a symlink is unresolvable, it makes
// sense to be able to get at it's preferred handler which may be // sense to be able to get at it's preferred handler which may be
// different from the Tracker. Used by the network neighborhood. // different from the Tracker. Used by the network neighborhood.
// type getters // type getters
bool IsFile() const; bool IsFile() const;
bool IsDirectory() const; bool IsDirectory() const;
bool IsQuery() const; bool IsQuery() const;
bool IsQueryTemplate() const; bool IsQueryTemplate() const;
bool IsContainer() const; bool IsContainer() const;
bool IsExecutable() const; bool IsExecutable() const;
bool IsSymLink() const; bool IsSymLink() const;
bool IsRoot() const; bool IsRoot() const;
bool IsTrash() const; bool IsTrash() const;
bool IsDesktop() const; bool IsDesktop() const;
bool IsVolume() const; bool IsVolume() const;
bool IsVirtualDirectory() const; bool IsVirtualDirectory() const;
IconSource IconFrom() const; IconSource IconFrom() const;
void SetIconFrom(IconSource); void SetIconFrom(IconSource);
// where is this model getting it's icon from // where is this model getting it's icon from
void ResetIconFrom(); void ResetIconFrom();
// called from the attribute changed calls to force a lookup of // called from the attribute changed calls to force a lookup of
// a new icon // a new icon
// symlink handling calls, mainly used by the IconCache // symlink handling calls, mainly used by the IconCache
const Model* ResolveIfLink() const; const Model* ResolveIfLink() const;
Model* ResolveIfLink(); Model* ResolveIfLink();
// works on anything // works on anything
Model* LinkTo() const; Model* LinkTo() const;
// fast, works only on symlinks // fast, works only on symlinks
void SetLinkTo(Model*); void SetLinkTo(Model*);
status_t GetLongVersionString(BString &, version_kind); status_t GetLongVersionString(BString &, version_kind);
status_t GetVersionString(BString &, version_kind); status_t GetVersionString(BString &, version_kind);
status_t AttrAsString(BString &, int64* value, status_t AttrAsString(BString &, int64* value,
const char* attributeName, uint32 attributeType); const char* attributeName, uint32 attributeType);
// Node monitor update call // Node monitor update call
void UpdateEntryRef(const node_ref* dirRef, const char* name); void UpdateEntryRef(const node_ref* dirRef, const char* name);
bool AttrChanged(const char*); bool AttrChanged(const char*);
// returns true if pose needs to update it's icon, etc. // returns true if pose needs to update it's icon, etc.
// pass null to force full update // pass null to force full update
bool StatChanged(); bool StatChanged();
// returns true if pose needs to update it's icon // returns true if pose needs to update it's icon
status_t WatchVolumeAndMountPoint(uint32, BHandler*); status_t WatchVolumeAndMountPoint(uint32, BHandler*);
// correctly handles boot volume name watching // correctly handles boot volume name watching
bool IsDropTarget(const Model* forDocument = 0, bool IsDropTarget(const Model* forDocument = 0,
bool traverse = false) const; bool traverse = false) const;
// if nonzero <forDocument> passed, mime info is used to // if nonzero <forDocument> passed, mime info is used to
// resolve if document can be opened // resolve if document can be opened
// if zero, all executables, directories and volumes pass // if zero, all executables, directories and volumes pass
// if traverse, dereference symlinks // if traverse, dereference symlinks
bool IsDropTargetForList(const BObjectList<BString>* list) const; bool IsDropTargetForList(const BObjectList<BString>* list) const;
// <list> contains mime types of all documents about to be handled // <list> contains mime types of all documents about to be handled
// by model // by model
#if DEBUG #if DEBUG
void PrintToStream(int32 level = 1, bool deep = false); void PrintToStream(int32 level = 1, bool deep = false);
void TrackIconSource(icon_size); void TrackIconSource(icon_size);
#endif #endif
bool IsSuperHandler() const; bool IsSuperHandler() const;
int32 SupportsMimeType(const char* type, int32 SupportsMimeType(const char* type,
const BObjectList<BString>* list, bool exactReason = false) const; const BObjectList<BString>* list, bool exactReason = false) const;
// pass in one string in <type> or a bunch in <list> // pass in one string in <type> or a bunch in <list>
// if <exactReason> false, returns as soon as it figures out that // if <exactReason> false, returns as soon as it figures out that
// app supports a given type, if true, returns an exact reason // app supports a given type, if true, returns an exact reason
// get rid of this?? // get rid of this??
ssize_t WriteAttr(const char* attr, type_code type, off_t, ssize_t WriteAttr(const char* attr, type_code type, off_t,
const void* buffer, size_t ); const void* buffer, size_t );
// cover call, creates a writable node and writes out attributes // cover call, creates a writable node and writes out attributes
// into it; work around for file nodes not being writeable // into it; work around for file nodes not being writeable
ssize_t WriteAttrKillForeign(const char* attr, ssize_t WriteAttrKillForeign(const char* attr,
const char* foreignAttr, type_code type, off_t, const char* foreignAttr, type_code type, off_t,
const void* buffer, size_t); const void* buffer, size_t);
bool Mimeset(bool force); bool Mimeset(bool force);
// returns true if mime type changed // returns true if mime type changed
bool HasLocalizedName() const; bool HasLocalizedName() const;
private: private:
status_t OpenNodeCommon(bool writable); status_t OpenNodeCommon(bool writable);
void SetupBaseType(); void SetupBaseType();
void FinishSettingUpType(); void FinishSettingUpType();
void DeletePreferredAppVolumeNameLinkTo(); void DeletePreferredAppVolumeNameLinkTo();
void CacheLocalizedName(); void CacheLocalizedName();
status_t FetchOneQuery(const BQuery*, BHandler* target, status_t FetchOneQuery(const BQuery*, BHandler* target,
BObjectList<BQuery>*, BVolume*); BObjectList<BQuery>*, BVolume*);
enum CanHandleResult { enum CanHandleResult {
kCanHandle, kCanHandle,
kCannotHandle, kCannotHandle,
kNeedToCheckType kNeedToCheckType
}; };
CanHandleResult CanHandleDrops() const; CanHandleResult CanHandleDrops() const;
enum NodeType { enum NodeType {
kPlainNode, kPlainNode,
kExecutableNode, kExecutableNode,
kDirectoryNode, kDirectoryNode,
kLinkNode, kLinkNode,
kQueryNode, kQueryNode,
kQueryTemplateNode, kQueryTemplateNode,
kVolumeNode, kVolumeNode,
kRootNode, kRootNode,
kTrashNode, kTrashNode,
kDesktopNode, kDesktopNode,
kVirtualDirectoryNode, kVirtualDirectoryNode,
kUnknownNode kUnknownNode
}; };
entry_ref fEntryRef; entry_ref fEntryRef;
StatStruct fStatBuf; StatStruct fStatBuf;
BString fMimeType; BString fMimeType;
// should use string that may be shared for common types // should use string that may be shared for common types
// bit of overloading hackery here to save on footprint // bit of overloading hackery here to save on footprint
union { union {
char* fPreferredAppName; // used if we are neither a volume char* fPreferredAppName; // used if we are neither a volume
// nor a symlink // nor a symlink
char* fVolumeName; // used if we are a volume char* fVolumeName; // used if we are a volume
Model* fLinkTo; // used if we are a symlink Model* fLinkTo; // used if we are a symlink
}; };
uint8 fBaseType; uint8 fBaseType;
uint8 fIconFrom; uint8 fIconFrom;
bool fWritable; bool fWritable;
BNode* fNode; BNode* fNode;
status_t fStatus; status_t fStatus;
BString fLocalizedName; BString fLocalizedName;
bool fHasLocalizedName; bool fHasLocalizedName;
bool fLocalizedNameIsCached; bool fLocalizedNameIsCached;
}; };
@@ -544,4 +545,5 @@ ModelNodeLazyOpener::OpenNode(bool writable)
} // namespace BPrivate } // namespace BPrivate
#endif // _NU_MODEL_H #endif // _NU_MODEL_H