ADD: Support for file type description property (Windows).

This commit is contained in:
cobines 2010-04-14 20:20:54 +00:00
commit 61155c44f0
5 changed files with 147 additions and 3 deletions

View file

@ -18,7 +18,8 @@ type
fpCreationTime,
fpLastAccessTime,
fpLink,
fpOwner
fpOwner,
fpType
);
TFilePropertiesTypes = set of TFilePropertyType;
@ -302,8 +303,11 @@ type
function Clone: TFileOwnerProperty; override;
procedure CloneTo(FileProperty: TFileProperty); override;
class function GetDescription: String; override;
class function GetID: TFilePropertyType; override;
function Format(Formatter: IFilePropertyFormatter): String; override;
property Owner: Cardinal read FOwner write FOwner;
property Group: Cardinal read FGroup write FGroup;
property OwnerStr: String read FOwnerStr write FOwnerStr;
@ -311,6 +315,31 @@ type
end;
{ TFileTypeProperty }
{en
File type description.
}
TFileTypeProperty = class(TFileProperty)
private
FType: String;
public
constructor Create; override;
function Clone: TFileTypeProperty; override;
procedure CloneTo(FileProperty: TFileProperty); override;
class function GetDescription: String; override;
class function GetID: TFilePropertyType; override;
function Format(Formatter: IFilePropertyFormatter): String; override;
property Value: String read FType write FType;
end;
// -- Property formatter interface ------------------------------------------
IFilePropertyFormatter = interface(IInterface)
@ -828,10 +857,61 @@ begin
end;
end;
class function TFileOwnerProperty.GetDescription: String;
begin
Result := '';
end;
class function TFileOwnerProperty.GetID: TFilePropertyType;
begin
Result := fpOwner;
end;
function TFileOwnerProperty.Format(Formatter: IFilePropertyFormatter): String;
begin
Result := '';
end;
{ TFileTypeProperty }
constructor TFileTypeProperty.Create;
begin
inherited Create;
end;
function TFileTypeProperty.Clone: TFileTypeProperty;
begin
Result := TFileTypeProperty.Create;
CloneTo(Result);
end;
procedure TFileTypeProperty.CloneTo(FileProperty: TFileProperty);
begin
if Assigned(FileProperty) then
begin
inherited CloneTo(FileProperty);
with FileProperty as TFileTypeProperty do
begin
FType := Self.FType;
end;
end;
end;
class function TFileTypeProperty.GetDescription: String;
begin
Result := '';
end;
class function TFileTypeProperty.GetID: TFilePropertyType;
begin
Result := fpType;
end;
function TFileTypeProperty.Format(Formatter: IFilePropertyFormatter): String;
begin
Result := FType;
end;
end.

View file

@ -53,6 +53,8 @@ type
procedure SetLastAccessTime(NewTime: TDateTime);
function GetIsLinkToDirectory: Boolean;
procedure SetIsLinkToDirectory(NewValue: Boolean);
function GetType: String;
procedure SetType(NewValue: String);
// Properties.
function GetNameProperty: TFileNameProperty;
@ -73,6 +75,8 @@ type
procedure SetLinkProperty(NewValue: TFileLinkProperty);
function GetOwnerProperty: TFileOwnerProperty;
procedure SetOwnerProperty(NewValue: TFileOwnerProperty);
function GetTypeProperty: TFileTypeProperty;
procedure SetTypeProperty(NewValue: TFileTypeProperty);
public
constructor Create(const APath: String);
@ -133,6 +137,7 @@ type
property LastAccessTimeProperty: TFileLastAccessDateTimeProperty read GetLastAccessTimeProperty write SetLastAccessTimeProperty;
property LinkProperty: TFileLinkProperty read GetLinkProperty write SetLinkProperty;
property OwnerProperty: TFileOwnerProperty read GetOwnerProperty write SetOwnerProperty;
property TypeProperty: TFileTypeProperty read GetTypeProperty write SetTypeProperty;
{ Accessors to each property's value. }
@ -152,6 +157,7 @@ type
property ModificationTime: TDateTime read GetModificationTime write SetModificationTime;
property CreationTime: TDateTime read GetCreationTime write SetCreationTime;
property LastAccessTime: TDateTime read GetLastAccessTime write SetLastAccessTime;
property FileType: String read GetType write SetType;
// Convenience functions.
// We assume here that when the file has no attributes
@ -454,6 +460,16 @@ begin
TFileLinkProperty(FProperties[fpLink]).IsLinkToDirectory := NewValue;
end;
function TFile.GetType: String;
begin
Result := TFileTypeProperty(FProperties[fpType]).Value;
end;
procedure TFile.SetType(NewValue: String);
begin
TFileTypeProperty(FProperties[fpType]).Value := NewValue;
end;
function TFile.GetNameProperty: TFileNameProperty;
begin
Result := TFileNameProperty(FProperties[fpName]);
@ -580,6 +596,20 @@ begin
Exclude(FSupportedProperties, fpOwner);
end;
function TFile.GetTypeProperty: TFileTypeProperty;
begin
Result := TFileTypeProperty(FProperties[fpType]);
end;
procedure TFile.SetTypeProperty(NewValue: TFileTypeProperty);
begin
FProperties[fpType] := NewValue;
if Assigned(NewValue) then
Include(FSupportedProperties, fpType)
else
Exclude(FSupportedProperties, fpType);
end;
function TFile.IsNameValid: Boolean;
begin
if Name <> '..' then

View file

@ -187,6 +187,9 @@ begin
OwnerProperty.GroupStr := sGroup;
end;
TypeProperty := TFileTypeProperty.Create;
TypeProperty.Value := GetFileDescription(Path + SearchRecord.Name);
{$ELSEIF DEFINED(UNIX)}
StatInfo := PUnixFindData(SearchRecord.FindHandle)^.StatRec;
@ -229,6 +232,8 @@ begin
OwnerProperty.OwnerStr := UIDToStr(StatInfo.st_uid);
OwnerProperty.GroupStr := GIDToStr(StatInfo.st_gid);
TypeProperty := TFileTypeProperty.Create;
{$ELSE}
AttributesProperty := TFileAttributesProperty.Create(SearchRecord.Attributes);
@ -237,6 +242,7 @@ begin
CreationTimeProperty := TFileCreationDateTimeProperty.Create(SearchRecord.Time);
LastAccessTimeProperty := TFileLastAccessDateTimeProperty.Create(SearchRecord.Time);
LinkProperty := TFileLinkProperty.Create;
TypeProperty := TFileTypeProperty.Create;
{$ENDIF}
@ -362,7 +368,8 @@ begin
fpCreationTime,
fpLastAccessTime,
uFileProperty.fpLink,
fpOwner
fpOwner,
fpType
];
end;
@ -502,4 +509,4 @@ begin
end;
end.

View file

@ -106,6 +106,11 @@ function mbGetShortPathName(const sLongPath: UTF8String; out sShortPath: AnsiStr
@param(sGroup Returns primary group of the file.)
}
function GetFileOwner(const sPath: String; out sUser, sGroup: String): Boolean;
{en
Retrieves a description of file's type.
@param(sPath Absolute path to the file.)
}
function GetFileDescription(const sPath: String): String;
implementation
@ -396,5 +401,16 @@ begin
end;
end;
function GetFileDescription(const sPath: String): String;
var
SFI: TSHFileInfoW;
begin
FillChar(SFI, SizeOf(SFI), 0);
if SHGetFileInfoW(PWideChar(UTF8Decode(sPath)), 0, SFI, SizeOf(SFI), SHGFI_TYPENAME) <> 0 then
Result := UTF8Encode(WideString(SFI.szTypeName))
else
Result := EmptyStr;
end;
end.

View file

@ -63,6 +63,7 @@ uses
fsfLastAccessTime,
fsfLinkTo,
fsfNameNoExtension,
fsfType,
fsfInvalid);
TFileFunctions = array of TFileFunction;
@ -80,6 +81,7 @@ uses
'GETFILELASTACCESSTIME',
'GETFILELINKTO',
'GETFILENAMENOEXT',
'GETFILETYPE',
'' // fsfInvalid
);
@ -97,6 +99,7 @@ uses
[fpLastAccessTime],
[fpLink],
[fpName],
[fpType],
[] { invalid });
type
@ -1023,6 +1026,14 @@ begin
else
Result:= AFile.NameNoExt;
end;
fsfType:
begin
if fpType in AFile.SupportedProperties then
Result := AFile.TypeProperty.Format(DefaultFilePropertyFormatter)
else
Result := '';
end;
end;
Exit;
end;