Recent

Author Topic: TXMLConfig issue on MAC OS X but not with WIndows or Linux  (Read 4061 times)

Tommi

  • Full Member
  • ***
  • Posts: 237
TXMLConfig issue on MAC OS X but not with WIndows or Linux
« on: June 15, 2016, 07:49:12 pm »
I have an issue very strange with MAC OS X. When I launch my application from lazarus it works perfectly. When I launch directly the same application from the file browser it cannot read xml file with TXMLConfig.

Every field is filled with default values.

The same application has no problem under Linux or Windows.

Is there any known problem ?


Thank you

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: TXMLConfig issue on MAC OS X but not with WIndows or Linux
« Reply #1 on: June 15, 2016, 07:52:15 pm »
Yes, you "current directory" is not your project directory, but rather an place within the application bundle (I'm assuming your app is GUI).

The proper way to handle it is to place and load the configuration from either the bundle itself. Or from the (system designated) users directory. (Which is an application library in case of OSX)

However, if your app is a console app, it would work the same in all system.

Tommi

  • Full Member
  • ***
  • Posts: 237
Re: TXMLConfig issue on MAC OS X but not with WIndows or Linux
« Reply #2 on: June 16, 2016, 08:33:29 am »
OK,
and is there any specific API to discover that folder ?

madref

  • Hero Member
  • *****
  • Posts: 1111
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: TXMLConfig issue on MAC OS X but not with WIndows or Linux
« Reply #3 on: June 16, 2016, 01:40:43 pm »

Also read this topic

Nope but you can write a function for it.
Code: Pascal  [Select][+][-]
  1. // -----------------------------------------------------------------------------
  2. // Platform-independend method to search for the location of preferences folder}
  3. // -----------------------------------------------------------------------------
  4. function GetPreferencesFolder: String;
  5. const
  6.   kMaxPath = 1024;
  7. var
  8.   {$IFDEF LCLCarbon}
  9.   theError: OSErr;
  10.   theRef: FSRef;
  11.   {$ENDIF}
  12.   pathBuffer: PChar;
  13. begin
  14.   {$IFDEF LCLCarbon}
  15.     try
  16.       pathBuffer := Allocmem(kMaxPath);
  17.     except on exception do exit;
  18.     end;
  19.     try
  20.       Fillchar(pathBuffer^, kMaxPath, #0);
  21.       Fillchar(theRef, Sizeof(theRef), #0);
  22.       theError := FSFindFolder(kOnAppropriateDisk, kPreferencesFolderType, kDontCreateFolder, theRef);
  23.       if (pathBuffer <> nil) and (theError = noErr) then
  24.       begin
  25.         theError := FSRefMakePath(theRef, pathBuffer, kMaxPath);
  26.         if theError = noErr then GetPreferencesFolder := UTF8ToAnsi(StrPas(pathBuffer)) + '/';
  27.       end;
  28.     finally
  29.       Freemem(pathBuffer);
  30.     end
  31.   {$ELSE}
  32.     GetPreferencesFolder := GetAppConfigDir(false);
  33.   {$ENDIF}
  34. end;     // GetPreferencesFolder
« Last Edit: June 16, 2016, 01:43:35 pm by madref »
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Sequoaia 15.6.1
Lazarus 4.99 (rev main_4_99-2644-gfd63613782) FPC 3.3.1 x86_64-darwin-cocoa

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: TXMLConfig issue on MAC OS X but not with WIndows or Linux
« Reply #4 on: June 16, 2016, 04:08:22 pm »
and is there any specific API to discover that folder ?

Is it a configuration file for the app (ie, not generally user-editable), or is it a preferences file (user edits persist when a newer version is installed)? If a configuration file, this should go in the .app bundle's Resources folder.

If a preferences file, most Mac apps store those settings in a .plist (property list file) in ~/Library/Preferences. You can create and manipulate a .plist file either with Apple's C or Objective C APIs. The PrefsUtils unit uses the C API and is part of XDev Toolkit: http://wiki.lazarus.freepascal.org/XDev_Toolkit

Also, a bit more info here:

https://dl.dropboxusercontent.com/u/28343282/MacXPlatform/MacXPlatform_Part8.html#Preferences

Note that you can create a regular XML file in the Preferences folder too, although this is rarely done.

Whatever you do, don't take the Linux (and Lazarus) route of storing prefs in a hidden folder under ~.


Tommi

  • Full Member
  • ***
  • Posts: 237
Re: TXMLConfig issue on MAC OS X but not with WIndows or Linux
« Reply #5 on: June 16, 2016, 10:16:19 pm »
I have 2 files. One is called xmlData.xml and it is read only. It just gives some data to the application when it starts. The second one (xmlConfig) is a preference file, when you change some setting in the app, it saves new settings there.

So i should do:
xmlConfig.fileName:='GetPreferencesFolder/xmlConfig.xml';

where GetPreferencesFolder is the function of madref.

Is it right ?

And what do I put in:

xmlData.fileName:= ??

Anyway I cannot do as in windows where I give my software to the customer in a zip file conatining the executable and these two file  ?
Under MAC OS X I must use an installer to put in the right position xmlData, is it right?

Thank you

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: TXMLConfig issue on MAC OS X but not with WIndows or Linux
« Reply #6 on: June 16, 2016, 10:23:47 pm »
Yes, I think you've got it. And good that you've separate config from pref data, although I find the naming of those files a bit confusing.

You don't need an installer. Just copy xmlData.xml into the .app bundle.

To get the location of the Resources folder, you could use NSMisc unit from the .zip file of handy source code for Mac from here:

https://dl.dropboxusercontent.com/u/28343282/ObjP/index.html

Then it's just:

xmlData.fileName := GetResourcesPath;

You can also put xmlConfig.xml in the Resources folder, then copy it to Preferences at startup if it doesn't yet exist there.

Not a bad idea to read Apple's overview too:

https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPRuntimeConfig/Articles/UserPreferences.html



Tommi

  • Full Member
  • ***
  • Posts: 237
Re: TXMLConfig issue on MAC OS X but not with WIndows or Linux
« Reply #7 on: June 17, 2016, 04:55:19 pm »
Ok, perfect!

Thank you very much.

 

TinyPortal © 2005-2018