File: UFileAssoc.pas

package info (click to toggle)
tuxcmd 0.6.70%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, stretch
  • size: 3,612 kB
  • ctags: 2,757
  • sloc: pascal: 49,754; ansic: 2,272; makefile: 106
file content (411 lines) | stat: -rw-r--r-- 16,480 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
(*
    Tux Commander - UFileAssoc - File Association system objects and functions
    Copyright (C) 2007 Tomas Bzatek <tbzatek@users.sourceforge.net>
    Check for updates on tuxcmd.sourceforge.net

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*)
unit UFileAssoc;

interface

uses Classes, SysUtils, StrUtils, GTKPixbuf, GTKClasses, GTKUtils, UEngines, UVFSCore, UCoreUtils;

type TAssocAction = class
       public
         ActionName, ActionCommand: string;
         AutodetectGUI, RunInTerminal: boolean;
         constructor Create;
     end;

     TFileAssoc = class
       public
         Extensions: TOpenStringArray;
         FileTypeName, FileTypeIcon, ColorString: string;
         DefaultAction: integer;
         ActionList: TList;
         Pixmap, LnkPixmap: TGDKPixbuf;
         constructor Create;
         destructor Destroy; override;
     end;

const ConstFTAMetaDirectory = '<directory>';
      ConstFTAMetaFile = '<file>';

{$I 'pixmaps/emblem_symbolic_link_png.inc'}
{$I 'pixmaps/gnome_dev_cdrom_16_png.inc'}
{$I 'pixmaps/gnome_dev_floppy_16_png.inc'}
{$I 'pixmaps/gnome_dev_harddisk_16_png.inc'}
{$I 'pixmaps/gnome_dev_removable_usb_16_png.inc'}
{$I 'pixmaps/gnome_mime_application_zip_16_png.inc'}
{$I 'pixmaps/gnome_mime_x_directory_smb_share_16_png.inc'}
{$I 'pixmaps/stock_folder_16_png.inc'}
{$I 'pixmaps/stock_lock_16_png.inc'}
{$I 'pixmaps/stock_lock_48_png.inc'}
{$I 'pixmaps/stock_new_16_png.inc'}
{$I 'pixmaps/stock_up_one_dir_16_png.inc'}
{$I 'pixmaps/tuxcmd_16_png.inc'}
{$I 'pixmaps/tuxcmd_24_png.inc'}
{$I 'pixmaps/tuxcmd_32_png.inc'}
{$I 'pixmaps/tuxcmd_48_png.inc'}
{$I 'pixmaps/tuxcmd_64_png.inc'}
{$I 'pixmaps/tuxcmd_128_png.inc'}





var FolderIcon, FileIcon, UpDirIcon, SymLinkEmblem, FolderIconLnk, FileIconLnk,
    FolderIconCached, FileIconCached, FolderIconLnkCached, FileIconLnkCached: TGDKPixbuf;
    MounterHDD, MounterRemovable, MounterFloppy, MounterCD, MounterNetwork: TGDKPixbuf;
    StockLock16, StockLock48: TGDKPixbuf;
    ArchiveIcon, ArchiveIconLnk: TGDKPixbuf;
    AppIcon16, AppIcon24, AppIcon32, AppIcon48, AppIcon64, AppIcon128: TGDKPixbuf;


procedure LoadIcons;
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;


(********************************************************************************************************************************)
(********************************************************************************************************************************)
(********************************************************************************************************************************)
constructor TFileAssoc.Create;
begin
  ActionList := TList.Create;
  DefaultAction := 0;
  FileTypeIcon := '';
  SetLength(Extensions, 0);
  FileTypeName := '';
  ColorString := '';
  Pixmap := TGDKPixbuf.Create(nil);
end;

destructor TFileAssoc.Destroy;
var i: integer;
begin
  try
    if Assigned(ActionList) and (ActionList.Count > 0) then
      for i := ActionList.Count - 1 downto 0 do
        TAssocAction(ActionList[i]).Free;
    ActionList.Clear;
    ActionList.Free;
//    Pixmap.Free;
  except
  end;
end;

(********************************************************************************************************************************)
constructor TAssocAction.Create;
begin
  RunInTerminal := False;
  AutodetectGUI := True;
end;

(********************************************************************************************************************************)
procedure LoadIcons;
begin
  FolderIcon := TGDKPixbuf.Create(Application);
  FolderIcon.LoadFromInline(@stock_folder_16_png[1]);
  FolderIcon.ScaleSimple(ConfRowHeightReal, ConfRowHeightReal);
  FileIcon := TGDKPixbuf.Create(Application);
  FileIcon.LoadFromInline(@stock_new_16_png[1]);
  FileIcon.ScaleSimple(ConfRowHeightReal, ConfRowHeightReal);
  UpDirIcon := TGDKPixbuf.Create(Application);
  UpDirIcon.LoadFromInline(@stock_up_one_dir_16_png[1]);
  UpDirIcon.ScaleSimple(ConfRowHeightReal, ConfRowHeightReal);
  SymLinkEmblem := TGDKPixbuf.Create(Application);
  SymLinkEmblem.LoadFromInline(@emblem_symbolic_link_png[1]);
  FolderIconLnk := TGDKPixbuf.Create(Application);
  FolderIconLnk.FPixbuf := FolderIcon.Copy;
  FolderIconLnk.CopyArea(SymLinkEmblem, 0, 0, SymLinkEmblem.Width, SymLinkEmblem.Height, FolderIconLnk.Width - SymLinkEmblem.Width, FolderIconLnk.Height - SymLinkEmblem.Height);
  FileIconLnk := TGDKPixbuf.Create(Application);
  FileIconLnk.FPixbuf := FileIcon.Copy;
  FileIconLnk.CopyArea(SymLinkEmblem, 0, 0, SymLinkEmblem.Width, SymLinkEmblem.Height, FileIconLnk.Width - SymLinkEmblem.Width, FileIconLnk.Height - SymLinkEmblem.Height);
  FolderIconCached := FolderIcon;
  FileIconCached := FileIcon;
  FolderIconLnkCached := FolderIconLnk;
  FileIconLnkCached := FileIconLnk;
  MounterHDD := TGDKPixbuf.Create(Application);
  MounterHDD.LoadFromInline(@gnome_dev_harddisk_16_png[1]);
  MounterRemovable := TGDKPixbuf.Create(Application);
  MounterRemovable.LoadFromInline(@gnome_dev_removable_usb_16_png[1]);
  MounterFloppy := TGDKPixbuf.Create(Application);
  MounterFloppy.LoadFromInline(@gnome_dev_floppy_16_png[1]);
  MounterCD := TGDKPixbuf.Create(Application);
  MounterCD.LoadFromInline(@gnome_dev_cdrom_16_png[1]);
  MounterNetwork := TGDKPixbuf.Create(Application);
  MounterNetwork.LoadFromInline(@gnome_mime_x_directory_smb_share_16_png[1]);
  StockLock16 := TGDKPixbuf.Create(Application);
  StockLock16.LoadFromInline(@stock_lock_16_png[1]);
  StockLock48 := TGDKPixbuf.Create(Application);
  StockLock48.LoadFromInline(@stock_lock_48_png[1]);
  ArchiveIcon := TGDKPixbuf.Create(Application);
  ArchiveIcon.LoadFromInline(@gnome_mime_application_zip_16_png[1]);
  ArchiveIcon.ScaleSimple(ConfRowHeightReal, ConfRowHeightReal);
  ArchiveIconLnk := TGDKPixbuf.Create(Application);
  ArchiveIconLnk.FPixbuf := ArchiveIcon.Copy;
  ArchiveIconLnk.CopyArea(SymLinkEmblem, 0, 0, SymLinkEmblem.Width, SymLinkEmblem.Height, FolderIconLnk.Width - SymLinkEmblem.Width, FolderIconLnk.Height - SymLinkEmblem.Height);

  AppIcon16 := TGDKPixbuf.Create(Application);
  AppIcon16.LoadFromInline(@tuxcmd_16_png[1]);
  AppIcon24 := TGDKPixbuf.Create(Application);
  AppIcon24.LoadFromInline(@tuxcmd_24_png[1]);
  AppIcon32 := TGDKPixbuf.Create(Application);
  AppIcon32.LoadFromInline(@tuxcmd_32_png[1]);
  AppIcon48 := TGDKPixbuf.Create(Application);
  AppIcon48.LoadFromInline(@tuxcmd_48_png[1]);
  AppIcon64 := TGDKPixbuf.Create(Application);
  AppIcon64.LoadFromInline(@tuxcmd_64_png[1]);
  AppIcon128 := TGDKPixbuf.Create(Application);
  AppIcon128.LoadFromInline(@tuxcmd_128_png[1]);
end;

(********************************************************************************************************************************)
function FindVFSPlugin(Filename: string): TVFSPlugin;
var Ext, s: string;
    b: boolean;
    i, j, MaxFound: integer;
begin
  Result := nil;
  MaxFound := 0;
  if (Pos('.', Filename) > 0) 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 (RightStr(Ext, Length(s)) = s) 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, MaxFound: integer;
begin
  Result := nil;
  MaxFound := 0;
  if (Pos('.', Filename) > 0) 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 (RightStr(Ext, Length(s)) = s) 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 PDataItem(Item)^ do begin
    ItemColor := nil;
    if IsLnk and (not ConfLinkItemDefaultColors) then ItemColor := LinkItemGDKColor else
    if IsDotFile and (not ConfDotFileItemDefaultColors) then ItemColor := DotFileItemGDKColor;
    if IsDir then begin
      if IsLnk then Icon := FolderIconLnkCached.FPixbuf
               else Icon := FolderIconCached.FPixbuf; 
    end else
    begin
      if not IsLnk then Icon := FileIconCached.FPixbuf
                   else Icon := FileIconLnkCached.FPixbuf;
      if (Pos('.', FName) > 1) and (LastDelimiter('.', FName) < Length(FName)) then begin
        Plugin := FindVFSPlugin(FName);
        Assoc := FindAssoc(FName);

        //  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;
          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;

(********************************************************************************************************************************)
procedure RecreateIcons(List: TList; const FreePixmaps: boolean = True);
var i: integer;
    b: boolean;
begin
  if Assigned(List) and (List.Count > 0) then
    for i := 0 to List.Count - 1 do
      if Assigned(List[i]) and (TObject(List[i]) is TFileAssoc) then
        with TFileAssoc(List[i]) do begin
          //  Destroy old objects
          if (Pixmap <> FileIcon) and (Pixmap <> FileIconLnk) and (Pixmap <> UpDirIcon) and (Pixmap <> FolderIcon) and
             (Pixmap <> FileIconCached) and (Pixmap <> FileIconLnkCached) and (Pixmap <> FolderIconCached) and (Pixmap <> FolderIconLnkCached) and
             (Pixmap <> FolderIconLnk) and Assigned(Pixmap) and Assigned(Pixmap.FPixbuf) then
          begin
            if FreePixmaps then Pixmap.Free;
            Pixmap := nil;
          end;
          if (LnkPixmap <> FileIcon) and (LnkPixmap <> FileIconLnk) and (LnkPixmap <> UpDirIcon) and (LnkPixmap <> FolderIcon) and
             (Pixmap <> FileIconCached) and (Pixmap <> FileIconLnkCached) and (Pixmap <> FolderIconCached) and (Pixmap <> FolderIconLnkCached) and
             (LnkPixmap <> FolderIconLnk) and Assigned(LnkPixmap) and Assigned(LnkPixmap.FPixbuf) then
          begin
            if FreePixmaps then LnkPixmap.Free;
            LnkPixmap := nil;
          end;
          //  Load / Assign icon
          b := FileExists(FileTypeIcon);
          if b then begin
            if not Assigned(Pixmap) then Pixmap := TGDKPixbuf.Create(nil);
            b := Pixmap.LoadFromFile(FileTypeIcon);
            if b then Pixmap.ScaleSimple(ConfRowHeightReal, ConfRowHeightReal);
          end;
          if not b then
            if FileTypeName = ConstFTAMetaDirectory then Pixmap := FolderIcon else
            if FileTypeName = ConstFTAMetaFile then Pixmap := FileIcon
            else Pixmap := FileIconCached;
          //  Create Link overelay
          if Assigned(Pixmap) and Assigned(Pixmap.FPixbuf) and Assigned(SymLinkEmblem) then begin
            LnkPixmap := TGDKPixbuf.Create(nil);
            LnkPixmap.FPixbuf := Pixmap.Copy;
            LnkPixmap.CopyArea(SymLinkEmblem, 0, 0, SymLinkEmblem.Width, SymLinkEmblem.Height, LnkPixmap.Width - SymLinkEmblem.Width, LnkPixmap.Height - SymLinkEmblem.Height);
          end else LnkPixmap := Pixmap;

          if FileTypeName = ConstFTAMetaDirectory then begin
            FolderIconCached := Pixmap;
            FolderIconLnkCached := LnkPixmap;
          end;
          if FileTypeName = ConstFTAMetaFile then begin
            FileIconCached := Pixmap;
            FileIconLnkCached := LnkPixmap;
          end;
        end;
end;

(********************************************************************************************************************************)
procedure RemoveIconRefs(List: TList; FreeIt: boolean);
var i: integer;
begin
  if Assigned(List) and (List.Count > 0) then
    for i := 0 to List.Count - 1 do
      if Assigned(List[i]) and (TObject(List[i]) is TFileAssoc) then
        with TFileAssoc(List[i]) do begin
          if FreeIt and Assigned(Pixmap) then Pixmap.Free;
          Pixmap := nil;
          if FreeIt and Assigned(LnkPixmap) then LnkPixmap.Free;
          LnkPixmap := nil;
        end;
end;

(********************************************************************************************************************************)
procedure AddDefaultItems(List: TList);
var Item: TFileAssoc;
    i: integer;
    Found: boolean; 
begin
  //  Add default directory item
  try
    Found := False;
    if List.Count > 0 then
      for i := 0 to List.Count - 1 do
        if TFileAssoc(List[i]).FileTypeName = ConstFTAMetaDirectory then begin
          Found := True;
          Break;
        end;
    if not Found then begin
      Item := TFileAssoc.Create;
      Item.FileTypeName := ConstFTAMetaDirectory;
      SetLength(Item.Extensions, 0);
      Item.FileTypeIcon := '';
      Item.ColorString := '';
      Item.DefaultAction := 0;
      Item.ActionList := TList.Create;
      Item.Pixmap := nil;
      Item.LnkPixmap := nil;
      List.Insert(0, Item);
    end;
  except end;

  //  Add default file item
  try
    Found := False;
    if List.Count > 0 then
      for i := 0 to List.Count - 1 do
        if TFileAssoc(List[i]).FileTypeName = ConstFTAMetaFile then begin
          Found := True;
          Break;
        end;
    if not Found then begin
      Item := TFileAssoc.Create;
      Item.FileTypeName := ConstFTAMetaFile;
      SetLength(Item.Extensions, 0);
      Item.FileTypeIcon := '';
      Item.ColorString := '';
      Item.DefaultAction := 0;
      Item.ActionList := TList.Create;
      Item.Pixmap := nil;
      Item.LnkPixmap := nil;
      List.Insert(0, Item);
    end;
  except end;
end;

(********************************************************************************************************************************)

end.