You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 line
3.5 KiB

  1. {
  2. Virtual File System: OS Special Folders provider
  3. FileSpec ist relativ zu einem Special Folder, der in der ExtendedData angegeben
  4. wird. Mögliche Schlüssel:
  5. 'LOCALAPPDATA' : User Application Data (machine local)
  6. 'APPDATA' : User Application Data (roaming profile)
  7. 'COMMONAPPDATA': Common Application Data
  8. 'LOCALTEMP' : User-Local Temporary files
  9. 'TEMP' : Temporary files (global)
  10. 'HOME',
  11. 'USERPROFILE' : User profile root ($HOME)
  12. 'PROGRAM' : Application Binary path
  13. (Maßgeblich ist das was in vfsResolveSystemPath steht!)
  14. }
  15. unit uvfsSpecialFolder;
  16. interface
  17. uses
  18. SysUtils, Classes, uvfsManager, uvfsFolder;
  19. type
  20. TvfsSpecialFolder = class(TvfsFileSystemFolder)
  21. private
  22. FUserRoot,
  23. FUserSpec: string;
  24. public
  25. constructor Create(const FileSpec:string; const ExtendedData: string=''); override;
  26. class function StorageName: string; override;
  27. function StorageGetFileSpec: string; override;
  28. function StorageGetData: string; override;
  29. end;
  30. function vfsRealGetAppConfigDir(Global: Boolean): string;
  31. function vfsResolveSystemPath(const PathSpec: string): string;
  32. implementation
  33. {$IFDEF MSWINDOWS}
  34. uses
  35. windirs;
  36. {$ENDIF}
  37. { TvfsSpecialFolder }
  38. Function NoVendorAppNameEvent : String;
  39. begin
  40. Result:= '';
  41. end;
  42. {
  43. GetAppConfigDir appends a subdirectory for vendor and another for application name.
  44. This behaviour cannot be disabled, and there is no unified cross-platform way to
  45. get *only* the %APPDATA% direcory.
  46. This hack temporarily returns empty vendor and appname, which works with the current
  47. implementation. YMMV.
  48. }
  49. function vfsRealGetAppConfigDir(Global: Boolean): string;
  50. var
  51. vn: TGetVendorNameEvent;
  52. an: TGetAppNameEvent;
  53. begin
  54. vn:= OnGetVendorName;
  55. an:= OnGetApplicationName;
  56. try
  57. OnGetApplicationName:= @NoVendorAppNameEvent;
  58. OnGetVendorName:= @NoVendorAppNameEvent;
  59. Result:= GetAppConfigDir(Global);
  60. finally
  61. OnGetApplicationName:= an;
  62. OnGetVendorName:= vn;
  63. end;
  64. end;
  65. function vfsResolveSystemPath(const PathSpec: string): string;
  66. begin
  67. Result:= '';
  68. case UpperCase(PathSpec) of
  69. 'LOCALAPPDATA' : // User Application Data (machine local)
  70. Result:= vfsRealGetAppConfigDir(false);
  71. 'APPDATA' : // User Application Data (roaming profile)
  72. {$IFDEF MSWINDOWS}
  73. Result:= GetWindowsSpecialDir(CSIDL_APPDATA);
  74. {$ELSE}
  75. Result:= vfsResolveSystemPath('LOCALAPPDATA');
  76. {$ENDIF}
  77. 'COMMONAPPDATA': // Common Application Data
  78. Result:= vfsRealGetAppConfigDir(true);
  79. 'LOCALTEMP' : // User-Local Temporary files
  80. Result:= GetTempDir(false);
  81. 'TEMP' : // Temporary files (global)
  82. Result:= GetTempDir(true);
  83. 'HOME',
  84. 'USERPROFILE' : // User profile root ($HOME)
  85. Result:= GetUserDir;
  86. 'PROGRAM' : // Application Binary path
  87. Result:= ExtractFilePath(ParamStr(0));
  88. end;
  89. end;
  90. constructor TvfsSpecialFolder.Create(const FileSpec: string; const ExtendedData: string);
  91. var
  92. relroot: string;
  93. begin
  94. relroot:= vfsResolveSystemPath(ExtendedData);
  95. relroot:= vfsExpandFileName(FileSpec, relroot);
  96. inherited Create(relroot, '');
  97. FUserRoot:= FileSpec;
  98. FUserSpec:= ExtendedData;
  99. end;
  100. class function TvfsSpecialFolder.StorageName: string;
  101. begin
  102. Result:= 'special';
  103. end;
  104. function TvfsSpecialFolder.StorageGetFileSpec: string;
  105. begin
  106. Result:= FUserRoot;
  107. end;
  108. function TvfsSpecialFolder.StorageGetData: string;
  109. begin
  110. Result:= FUserSpec;
  111. end;
  112. initialization
  113. VFSManager.RegisterProvider(TvfsSpecialFolder);
  114. end.