diff options
| author | Tomas Bzatek <tbzatek@users.sourceforge.net> | 2008-06-18 22:30:08 +0200 |
|---|---|---|
| committer | Tomas Bzatek <tbzatek@users.sourceforge.net> | 2008-06-18 22:30:08 +0200 |
| commit | e7a55631dfa3e4011e420dbd22891d4adf98aa7d (patch) | |
| tree | f87f517df24b0267dcda78e8cade9e574b8f03f3 /UFileAssoc.pas | |
| parent | fd60abd18e718c2134b350308c8d4dbf0d95dcc0 (diff) | |
| download | tuxcmd-e7a55631dfa3e4011e420dbd22891d4adf98aa7d.tar.xz | |
Handle multiple file extensions
Resolves #1120769
Diffstat (limited to 'UFileAssoc.pas')
| -rw-r--r-- | UFileAssoc.pas | 157 |
1 files changed, 95 insertions, 62 deletions
diff --git a/UFileAssoc.pas b/UFileAssoc.pas index 2c1f708..10d0f73 100644 --- a/UFileAssoc.pas +++ b/UFileAssoc.pas @@ -21,7 +21,7 @@ unit UFileAssoc; interface -uses Classes, SysUtils, GTKPixbuf, GTKClasses, GTKUtils, UEngines; +uses Classes, SysUtils, GTKPixbuf, GTKClasses, GTKUtils, UEngines, UVFSCore, UCoreUtils; type TAssocAction = class ActionName, ActionCommand: string; @@ -30,7 +30,8 @@ type TAssocAction = class end; TFileAssoc = class - Extensions, FileTypeName, FileTypeIcon, ColorString: string; + Extensions: TOpenStringArray; + FileTypeName, FileTypeIcon, ColorString: string; DefaultAction: integer; ActionList: TList; Pixmap, LnkPixmap: TGDKPixbuf; @@ -66,14 +67,16 @@ var FolderIcon, FileIcon, UpDirIcon, SymLinkEmblem, FolderIconLnk, FileIconLnk, procedure LoadIcons; -procedure AddFileTypeIcon(Item: PDataItem); +function FindVFSPlugin(Filename: string): TVFSPlugin; +function FindAssoc(Filename: string): TFileAssoc; +procedure AssignFileType(Item: PDataItem); procedure RecreateIcons(List: TList; const FreePixmaps: boolean = True); procedure RemoveIconRefs(List: TList; FreeIt: boolean); procedure AddDefaultItems(List: TList); implementation -uses GTKForms, UConfig, UCore, UCoreUtils, UVFSCore; +uses GTKForms, UConfig, UCore; (********************************************************************************************************************************) @@ -84,7 +87,7 @@ begin ActionList := TList.Create; DefaultAction := 0; FileTypeIcon := ''; - Extensions := ''; + SetLength(Extensions, 0); FileTypeName := ''; ColorString := ''; Pixmap := TGDKPixbuf.Create(nil); @@ -158,14 +161,74 @@ begin end; (********************************************************************************************************************************) -procedure AddFileTypeIcon(Item: PDataItem); -var Ext: string; +function FindVFSPlugin(Filename: string): TVFSPlugin; +var Ext, s: string; b: boolean; - i, j, Last: integer; - AColor: TGDKColor; + i, j, Last, MaxFound: integer; +begin + b := False; + Result := nil; + MaxFound := 0; + if (Pos('.', Filename) > 1) and (LastDelimiter('.', Filename) < Length(Filename)) then begin + Ext := WideUpperCase(Trim(Copy(Filename, Pos('.', Filename), Length(Filename) - Pos('.', Filename) + 1))); + if PluginList.Count > 0 then + for i := 0 to PluginList.Count - 1 do + with TVFSPlugin(PluginList[i]) do + if Length(Extensions) > 0 then begin + b := False; + s := ''; + for j := 0 to Length(Extensions) - 1 do begin + s := ANSIUpperCase(IncludeLeadingDot(Extensions[j])); + if Length(Ext) = Length(s) + then b := (Ext = s) and (Length(s) > MaxFound) + else b := (Pos(s, Ext) > 0) and (Pos(s, Ext) = (Length(Ext) - Length(s) + 1)) and (Length(s) > MaxFound); + if b then Break; + end; + if b then begin + Result := PluginList[i]; + MaxFound := Length(s); + end; + end; + end; +end; + +function FindAssoc(Filename: string): TFileAssoc; +var Ext, s: string; + b: boolean; + i, j, Last, MaxFound: integer; +begin + b := False; + Result := nil; + MaxFound := 0; + if (Pos('.', Filename) > 1) and (LastDelimiter('.', Filename) < Length(Filename)) then begin + Ext := WideUpperCase(Trim(Copy(Filename, Pos('.', Filename), Length(Filename) - Pos('.', Filename) + 1))); + if AssocList.Count > 0 then + for i := 0 to AssocList.Count - 1 do + with TFileAssoc(AssocList[i]) do + if Length(Extensions) > 0 then begin + b := False; + s := ''; + for j := 0 to Length(Extensions) - 1 do begin + s := ANSIUpperCase(IncludeLeadingDot(Extensions[j])); + if Length(Ext) = Length(s) + then b := (Ext = s) and (Length(s) > MaxFound) + else b := (Pos(s, Ext) > 0) and (Pos(s, Ext) = (Length(Ext) - Length(s) + 1)) and (Length(s) > MaxFound); + if b then Break; + end; + if b then begin + Result := AssocList[i]; + MaxFound := Length(s); + end; + end; + end; +end; + +procedure AssignFileType(Item: PDataItem); +var AColor: TGDKColor; Plugin: TVFSPlugin; + Assoc: TFileAssoc; begin - with Item^ do begin + with PDataItem(Item)^ do begin ItemColor := nil; if IsLnk and (not ConfLinkItemDefaultColors) then ItemColor := LinkItemGDKColor else if IsDotFile and (not ConfDotFileItemDefaultColors) then ItemColor := DotFileItemGDKColor; @@ -177,58 +240,28 @@ begin if not IsLnk then Icon := FileIconCached.FPixbuf else Icon := FileIconLnkCached.FPixbuf; if (Pos('.', FName) > 1) and (LastDelimiter('.', FName) < Length(FName)) then begin - Ext := WideUpperCase(Trim(Copy(FDisplayName, LastDelimiter('.', FDisplayName) + 1, Length(FDisplayName) - LastDelimiter('.', FDisplayName)))); - - // Try to find a plugin which can handle this archive type - b := False; - Plugin := nil; - if PluginList.Count > 0 then - for i := 0 to PluginList.Count - 1 do begin - Plugin := TVFSPlugin(PluginList[i]); - if Length(Plugin.Extensions) > 0 then - for j := 0 to Length(Plugin.Extensions) - 1 do begin - if AnsiCompareText(Plugin.Extensions[j], Ext) = 0 then begin - b := True; - if IsLnk then Icon := ArchiveIconLnk.FPixbuf - else Icon := ArchiveIcon.FPixbuf; - Break; - end; - end; - if b then Break; + Plugin := FindVFSPlugin(FDisplayName); + Assoc := FindAssoc(FDisplayName); + + // Asssign icon and color + if Plugin <> nil then begin + if IsLnk then Icon := ArchiveIconLnk.FPixbuf + else Icon := ArchiveIcon.FPixbuf; + end else + if Assoc <> nil then begin + if Assigned(Assoc.Pixmap) then begin + if IsLnk and Assigned(Assoc.LnkPixmap) and Assigned(Assoc.LnkPixmap.FPixbuf) + then Icon := Assoc.LnkPixmap.FPixbuf + else Icon := Assoc.Pixmap.FPixbuf; end; - - // Try to find an association - if (not b) and (AssocList.Count > 0) then - for i := 0 to AssocList.Count - 1 do - with TFileAssoc(AssocList[i]) do - if Length(Trim(Extensions)) > 0 then begin - b := False; - if Pos(';', Extensions) = 0 then b := ANSIUpperCase(Trim(Extensions)) = Ext else begin - Last := 0; - for j := 1 to Length(Extensions) do - if Extensions[j] = ';' then begin - if ANSIUpperCase(Trim(Copy(Extensions, Last + 1, j - Last - 1))) = Ext then begin - b := True; - Break; - end; - Last := j; - end; - if not b then b := ANSIUpperCase(Trim(Copy(Extensions, LastDelimiter(';', Extensions) + 1, Length(Extensions) - LastDelimiter(';', Extensions)))) = Ext; - end; - if b then begin - if Assigned(Pixmap) then - if IsLnk and Assigned(LnkPixmap) and Assigned(LnkPixmap.FPixbuf) - then Icon := LnkPixmap.FPixbuf - else Icon := Pixmap.FPixbuf; - if ItemColor = nil then - if (ColorString = '') or (not StringToGDKColor(ColorString, AColor)) - then ItemColor := NormalItemGDKColor - else ItemColor := GDKColorToPGdkColor(AColor); - end; - end; - + if ItemColor = nil then begin + if (Assoc.ColorString = '') or (not StringToGDKColor(Assoc.ColorString, AColor)) + then ItemColor := NormalItemGDKColor + else ItemColor := GDKColorToPGdkColor(AColor); + end; + end; end; - end; + end; end; end; @@ -318,7 +351,7 @@ begin if not Found then begin Item := TFileAssoc.Create; Item.FileTypeName := ConstFTAMetaDirectory; - Item.Extensions := ''; + SetLength(Item.Extensions, 0); Item.FileTypeIcon := ''; Item.ColorString := ''; Item.DefaultAction := 0; @@ -341,7 +374,7 @@ begin if not Found then begin Item := TFileAssoc.Create; Item.FileTypeName := ConstFTAMetaFile; - Item.Extensions := ''; + SetLength(Item.Extensions, 0); Item.FileTypeIcon := ''; Item.ColorString := ''; Item.DefaultAction := 0; |
