Recent

Author Topic: Fixing GetAppConfigDir and GetAppConfigFile for MacOS X  (Read 2050 times)

Hansaplast

  • Hero Member
  • *****
  • Posts: 534
  • Tweaking4All.com
    • Tweaking4All
Fixing GetAppConfigDir and GetAppConfigFile for MacOS X
« on: May 12, 2016, 01:50:15 pm »
This is not the first time this topic is discussed, but as a Mac user, something I'd like to fix.


The problem:
Both GetAppConfigDir and GetAppConfigFile return an inappropriate result for Mac OS X (per Apple guidelines).


jwdietrich posted a way to work around this in this post.


I just modified this a little bit ...


Code: [Select]
function GetPreferencesFolder(Global:boolean): String;
  { platform-independend method to search for the location of preferences folder}
const
  kMaxPath = 1024;
var
  {$IFDEF LCLCarbon}
  theError: OSErr;
  theRef: FSRef;
  {$ENDIF}
  pathBuffer: PChar;
begin
  {$IFDEF LCLCarbon}
    try
      pathBuffer := Allocmem(kMaxPath);
    except on exception do exit;
    end;
    try
      Fillchar(pathBuffer^, kMaxPath, #0);
      Fillchar(theRef, Sizeof(theRef), #0);
      if Global then   // kLocalDomain
        theError := FSFindFolder(kLocalDomain, kApplicationSupportFolderType, kDontCreateFolder, theRef)
      else
        theError := FSFindFolder(kUserDomain , kApplicationSupportFolderType, kDontCreateFolder, theRef);
      if (pathBuffer <> nil) and (theError = noErr) then
      begin
        theError := FSRefMakePath(theRef, pathBuffer, kMaxPath);
        if theError = noErr then GetPreferencesFolder := UTF8ToAnsi(StrPas(pathBuffer)) + '/';
      end;
    finally
      Freemem(pathBuffer);
    end
  {$ELSE}
    GetPreferencesFolder := GetAppConfigDir(false);
  {$ENDIF}
  {
   User preferences live in the location returned by FSFindFolder(kUserDomain,kPreferencesFolderType).
   User application support items live in the location returned by FSFindFolder(kUserDomain, kApplicationSupportFolderType).
   Global preferences live in the location returned by FSFindFolder(kLocalDomain,kPreferencesFolderType).
   Global application support items live in the location returned by FSFindFolder(kLocalDomain, kApplicationSupportFolderType).
  }
end;


Since GetAppConfigDir/File does not distinguishes between "Preferences" and "Application Data", I picked "Application Data". One can argue this might not be the 100% correct solution, but at least it better than the current "~/.config" location, and one could even store INI files there (instead of properly using XML in "Preferences").


So I went look for the functions GetAppConfigDir and GetAppConfigFile (sysutils), to see if I can incorporate this code, but I cannot edit these files. Probably because they are part of FPC?


I'd love to help out and create a patch to address this issue. Would anyone be able to assist?

tintinux

  • Sr. Member
  • ****
  • Posts: 299
    • Gestinux
Re: Fixing GetAppConfigDir and GetAppConfigFile for MacOS X
« Reply #1 on: July 19, 2016, 10:29:26 am »
Hi

Thanks for your proposal in the bug tracker.

You have a page in the wiki to make a patch. The problem is that you must use the svn fpc and lazarus version, and this is not possible for me.

I just think you should remove the UTF8ToAnsi, and Use DirectorySeparator instead of the constant '/' , but it is a detail.

But in which unit is now the OSErr type ?   I had it in previous versions of Lazarus, but no more in 1.6 ? I Can't find it anywhere.

Thanks


« Last Edit: July 19, 2016, 10:36:48 am by tintinux »
Initiator of gestinux, open-source, multi-database and multilingual accounting and billing software made with LAZARUS.

You can help to develop, to make and improve translations, and to provide examples of legal charts and reports from more countries.

Hansaplast

  • Hero Member
  • *****
  • Posts: 534
  • Tweaking4All.com
    • Tweaking4All
Re: Fixing GetAppConfigDir and GetAppConfigFile for MacOS X
« Reply #2 on: July 19, 2016, 11:42:51 am »
Good point (DirectorySeparator and removing UTF8ToAnsi).
OSErr is defined in "MacOSAll" I believe. :-)