Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Wed Jul 17, 2019 2:27 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 23 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: New Preference Solution!
PostPosted: Sun Jun 26, 2011 4:04 pm 
Offline
User avatar

Joined: Mon Feb 05, 2007 6:24 pm
Posts: 99
Location: USA
As many of you might know, the preferences solution called BPrefs from Dreystone Software finally died with the release of Real Studio 2011r2. I decided to make my own preference file solution that makes plain-text XML plist documents that conforms to Apple's guidelines. I also decided to make it free! Download an example project with XPrefs module included here: http://cl.ly/8LCp

Please test the module out and post improvements below!

About XPrefs: 
XPrefs is a module that simplifies the task of storing preferences in an XML file. Xprefs makes storing and loading preferences easy by using the Dictionary interface. Simply call 'XPrefs' like you would a global Dictionary object using KEY/VALUE pairs. Xprefs preference files are located in the SpecialFolder.Preferences folder and can be read/manipulated using the Plist Editor included in Apple's developer tools.

XPrefs is strongly based on BPrefs from Dreystone Software. (http://www.dreystone.com)

------------------------------------
Using XPrefs:
------------------------------------
Change the constant called PREFERENCES_FILE_NAME to the following values
-Mac OS X:
--The application's Bundle Identifier followed by ".plist". This is a MUST for apps that are being submitted to the App Store!
-Windows/Linux:
--Whatever you want, there are no guidlines for these platforms.

DICTIONARY WRAPPER FUNCTIONS:
-XPrefs.Clear()
--Deletes all key/value pairs.
-XPrefs.HasKey(key) As Variant
--Checks to see if the passed key exists.
-XPrefs.Remove(key)
--Deletes the passed key and associated value.
-XPrefs.Value(key) = Variant
--Sets a key/value pair.
-XPrefs.Value(key) As Variant
--Gets the value associated with the passed key.

PREFERENCE FILE FUNCTIONS:
-XPrefs.Save()
--Saves the preferences in their current state. Put this in places that you want to trigger a save. Not neccesary if the autosave feature is enabled.
-XPrefs.ToString() as String
--Use this to view the preferences file in raw XML format as it will be saved to the disk. (usually for testing purposes only)
-XPrefs.File() as Folderitem
--Returns the preferences folderitem on disk.
-XPrefs.AutoSave as Boolean
--If this property is true, the preferences will save whenever a value is changed.

PREFERENCE PROGRESS WINDOW:
-The window XPrefsSaving is simply a way to let your user see that the application is active while saving large preference files. Simply set the SaveAlertWait property to determine how long XPrefs should wait (in seconds) to display the window. The reccomended time is 2 seconds. Set SaveAlertWait to a negative number to disable the window completely.

ERROR CODES:
-Check the error status of the last save by reading the LastErrorCode property. You can test it against one of the error constants.
--101 - OutputInterrupted
---The file write was interrupted. An example of this is a thread that uses XPrefs bing paused by a menu bar interaction.
--102 - AccessDenied
---Access to the preference file was denied.
--103 - NotEnoughMemory
---You ran out of memory.
--104 - FileInUse
---The preference file is already in use.
--105 - InvalidName
---The PREFERENCES_FILE_NAME constant is not a valid file name.

------------------------------------
Release Notes:
------------------------------------
Version 1.6.4 -- (July 10, 2011)
-[NEW] Added SaveAlertWait as Double for long save times
-[NEW] Added XPrefsSaving as Window for long save times
-[NEW] Added LastErrorCode as Integer in case something goes wrong
-[NEW] Added error code constants in case something goes wrong
-[NEW] Added Access as Semaphore for multithreading
-[NEW] Replaced slow string processor with RegEx for speed reasons
-[FIX] Optimized portions of the code
-[FIX] Changed scope of many methods, properties, and constants
-[FIX] Corrupt preferences no longer crash XPrefs
-[FIX] Fixed a KeyNotFoundException
Version 1.0 -- (June 25, 2011)
-[NEW] Initial Release

Contact: 
Author: 10101 Software
Website: http://www.10101software.co.nr
Contact: tbg10101@gmail.com

_________________
Sole Proprietor
10101 Software
http://www.10101software.co.nr/
RB 2011r4.3, MBS 12.4


Last edited by tbg10101 on Sun Jul 10, 2011 6:53 pm, edited 3 times in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: New Preference Solution!
PostPosted: Mon Jun 27, 2011 9:18 am 
Offline
Real Software Engineer

Joined: Fri Jan 28, 2011 5:24 pm
Posts: 145
If you're submitting apps to the Mac App Store, you really should be using NSUserDefaults/CFPreferences instead of writing your own file to the preferences folder.

_________________
Joe Ranieri
Mac Frameworks & Compiler
REAL Software, Inc.


Top
 Profile  
Reply with quote  
 Post subject: Re: New Preference Solution!
PostPosted: Mon Jun 27, 2011 2:10 pm 
Offline
User avatar

Joined: Mon Feb 05, 2007 6:24 pm
Posts: 99
Location: USA
Actually, Apple doesn't care how the file is managed so long as it is named correctly and is in the correct folder.

_________________
Sole Proprietor
10101 Software
http://www.10101software.co.nr/
RB 2011r4.3, MBS 12.4


Top
 Profile  
Reply with quote  
 Post subject: Re: New Preference Solution!
PostPosted: Mon Jun 27, 2011 4:54 pm 
Offline
Real Software Engineer

Joined: Fri Jan 28, 2011 5:24 pm
Posts: 145
They might not reject your application, but it is not allowed. If you read through the "File-System Usage Requirements for the Mac App Store" section of the "Submitting to the Mac App Store" guide, you'll see that ~/Library/Preferences is not on the list of places you're allowed to write.

_________________
Joe Ranieri
Mac Frameworks & Compiler
REAL Software, Inc.


Top
 Profile  
Reply with quote  
 Post subject: Re: New Preference Solution!
PostPosted: Mon Jun 27, 2011 5:47 pm 
Offline

Joined: Mon May 31, 2010 5:35 am
Posts: 2412
Location: Netherlands
jranieri wrote:
They might not reject your application, but it is not allowed. If you read through the "File-System Usage Requirements for the Mac App Store" section of the "Submitting to the Mac App Store" guide, you'll see that ~/Library/Preferences is not on the list of places you're allowed to write.


right, this should be known to all app store devlopers.
So please read again, and make sure.

_________________
SWORT™
- Windows Vista, Windows XP, Windows 7, Linux Mint 11 and Mac OSX Lion
- REAL Studio Enterprise(latest) and WEB Edition(latest)
- Plugins (MBS Complete, ToringoLib, Einhugur, VanHoek + Some open source plugins)
For Dutch User Groups ask me to enter!


Top
 Profile  
Reply with quote  
 Post subject: Re: New Preference Solution!
PostPosted: Tue Jun 28, 2011 1:45 pm 
Offline
User avatar

Joined: Fri Nov 10, 2006 4:10 pm
Posts: 1855
Location: Michigan, USA
Since you're using a module I'd recommend switching the global methods to public, otherwise the methods and properties are tossed into the completely global mix. It just makes more sense to call XPrefs every time outside of the module.

Otherwise it's great. It's a lot simpler than the other what I was using. I'll switch to yours. It'd be nice, though, to see an example of saving and loading custom classes (e.g. property classes).

_________________
Windows 8 x64
Windows XP Pro SP3
Ubuntu 11.04 via Virtual Box
RS Enterprise 2012r1.1

Programming Tutorials & Free Projects: http://www.JasonTheAdams.com
"Christianity has not been tried and found wanting; it has been found difficult and not tried." - G.K. Chesterton


Top
 Profile  
Reply with quote  
 Post subject: Re: New Preference Solution!
PostPosted: Tue Jun 28, 2011 2:05 pm 
Offline
User avatar

Joined: Mon Feb 05, 2007 6:24 pm
Posts: 99
Location: USA
@Jason_Adams:
Thanks! I will definitely change the scope of the methods for the next version.

I will also look into storing structures and classes but as of now I don't know how to do that. Do you have any pointers?

_________________
Sole Proprietor
10101 Software
http://www.10101software.co.nr/
RB 2011r4.3, MBS 12.4


Top
 Profile  
Reply with quote  
 Post subject: Re: New Preference Solution!
PostPosted: Tue Jun 28, 2011 3:55 pm 
Offline
User avatar

Joined: Fri Nov 10, 2006 4:10 pm
Posts: 1855
Location: Michigan, USA
Well, after poking through your code, I feel like you already have the basis for saving custom classes. I'll walk you through my purpose, which you may include a similar example in your project.

I have a custom class which contains properties of a connection profile (i.e. address, port, a file name, etc.), and keep an array of this class for every instance the user creates. Within this class I created two methods: one for assigning, and one for retrieving a dictionary version of class. When I mean to store the class I retrieve the dictionary version and place it an array of dictionaries (via your module); upon loading I pull the array of dictionaries and reassign the dictionary to a new instance of the class and store it back into the array.

This method works due to the recursive method you use for nesting arrays and dictionaries.

Finally, on a side note, I added the following line to the Value (key As String) As Variant function as to avoid a KeyNotFoundException:

If Not DictData.HasKey(key) Then Return Nil


Hope this helps! :)

_________________
Windows 8 x64
Windows XP Pro SP3
Ubuntu 11.04 via Virtual Box
RS Enterprise 2012r1.1

Programming Tutorials & Free Projects: http://www.JasonTheAdams.com
"Christianity has not been tried and found wanting; it has been found difficult and not tried." - G.K. Chesterton


Top
 Profile  
Reply with quote  
 Post subject: Re: New Preference Solution!
PostPosted: Tue Jun 28, 2011 4:01 pm 
Offline

Joined: Mon May 30, 2011 12:56 am
Posts: 702
Quote:
you'll see that ~/Library/Preferences is not on the list of places you're allowed to write.


a: So why does it exist, who IS allowed to write there, and why?

b: Shouldnt you actually be writing to <username>/Library/Preferences ?


Top
 Profile  
Reply with quote  
 Post subject: Re: New Preference Solution!
PostPosted: Tue Jun 28, 2011 4:32 pm 
Offline
User avatar

Joined: Mon Feb 05, 2007 6:24 pm
Posts: 99
Location: USA
Quote:
a: So why does it exist, who IS allowed to write there, and why?

b: Shouldnt you actually be writing to <username>/Library/Preferences ?


The 'preferred' way of writing preferences is by sending a
defaults write
command to the OS so it can manage the preferences, I think.

"~" is the user's home directory: http://en.wikipedia.org/wiki/Tilde#Directories_and_URLs

_________________
Sole Proprietor
10101 Software
http://www.10101software.co.nr/
RB 2011r4.3, MBS 12.4


Last edited by tbg10101 on Sun Jul 10, 2011 6:21 pm, edited 2 times in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: New Preference Solution!
PostPosted: Tue Jun 28, 2011 4:48 pm 
Offline
Real Software Engineer

Joined: Fri Jan 28, 2011 5:24 pm
Posts: 145
kermit wrote:
a: So why does it exist, who IS allowed to write there, and why?


It exists for CFPreferences and NSUserDefaults or legacy applications who write their preferences there. While it is allowed for applications to write there outside of the Mac App Store, the App Store has tighter limitations and doing so will violate the rules.

kermit wrote:
b: Shouldnt you actually be writing to <username>/Library/Preferences ?


This is the same as ~/Library/Preferences (the tilde expands to the user's home folder).

tbg10101 wrote:
The 'preferred' way of writing prefernces is by sending a
write defaults
command to the OS so it can manage the preferences, I think.


This isn't correct. The preferred way is to use CFPreferences or NSUserDefaults.

In general, using a shell is the wrong way to do anything if there's an available API to get the job done.

_________________
Joe Ranieri
Mac Frameworks & Compiler
REAL Software, Inc.


Top
 Profile  
Reply with quote  
 Post subject: Re: New Preference Solution!
PostPosted: Tue Jun 28, 2011 5:33 pm 
Offline
User avatar

Joined: Sun Feb 19, 2006 3:10 pm
Posts: 1583
Location: Hamburg, Germany
jranieri wrote:
The preferred way is to use CFPreferences or NSUserDefaults.

In general, using a shell is the wrong way to do anything if there's an available API to get the job done.

Is this going to be supported natively in RS? Personally I don’t care about the App Store and the restrictions that go with it (I even use my own preferred preference file format rather than a property list), but it looks like the only ‘legal’ way of managing preferences requires fiddling with declares.

_________________
Michael J. Hußmann
http://digicam-experts.de


Top
 Profile  
Reply with quote  
 Post subject: Re: New Preference Solution!
PostPosted: Tue Jun 28, 2011 5:43 pm 
Offline

Joined: Thu Jul 13, 2006 11:20 am
Posts: 114
mjh wrote:
jranieri wrote:
The preferred way is to use CFPreferences or NSUserDefaults.

In general, using a shell is the wrong way to do anything if there's an available API to get the job done.

Is this going to be supported natively in RS? Personally I don’t care about the App Store and the restrictions that go with it (I even use my own preferred preference file format rather than a property list), but it looks like the only ‘legal’ way of managing preferences requires fiddling with declares.


macoslib has a CFPreferences wrapper. It could also have one for NSUserDefaults, were there interest. Cash would of course also be welcome.


Top
 Profile  
Reply with quote  
 Post subject: Re: New Preference Solution!
PostPosted: Sun Jul 10, 2011 6:11 pm 
Offline
User avatar

Joined: Mon Feb 05, 2007 6:24 pm
Posts: 99
Location: USA
New version is up! Tell me what you think. Thanks to all the people who had suggestion and bug reports!
Download: http://cl.ly/8LCp

------------------------------------
Release Notes:
------------------------------------
Version 1.6.4 -- (July 10, 2011)
[NEW] Added SaveAlertWait as Double for long save times
[NEW] Added XPrefsSaving as Window for long save times
[NEW] Added LastErrorCode as Integer in case something goes wrong
[NEW] Added error code constants in case something goes wrong
[NEW] Added Access as Semaphore for multithreading
[NEW] Replaced slow string processor with RegEx for speed reasons
[FIX] Optimized portions of the code
[FIX] Changed scope of many methods, properties, and constants
[FIX] Corrupt preferences no longer crash XPrefs
[FIX] Fixed a KeyNotFoundException

_________________
Sole Proprietor
10101 Software
http://www.10101software.co.nr/
RB 2011r4.3, MBS 12.4


Top
 Profile  
Reply with quote  
 Post subject: Re: New Preference Solution!
PostPosted: Tue Nov 22, 2011 7:28 pm 
Offline
User avatar

Joined: Mon Feb 05, 2007 6:24 pm
Posts: 99
Location: USA
I just released XDB (X Database) as a lightweight database solution for RB developers.

Please use the XDB Light module as a replacement for XPrefs. It can only talk to a single file in the OS Applications Data folder.

The (regular) XDB module can handle as many files (wherever you want) as you want by setting the ActiveDB property and telling XDB to load a folderitem.

The XDB class is new. Each instance controls an XML file (wherever you want) of its own so now you can have a better DBMS interface. (typically one instance of XDB per table) The class is also a sub-class of Dictionary so there is less overhead for routine dictionary activities.

Download here: http://cl.ly/C3Fd

And tell me about any bugs!

-Tristan

_________________
Sole Proprietor
10101 Software
http://www.10101software.co.nr/
RB 2011r4.3, MBS 12.4


Last edited by tbg10101 on Wed Nov 23, 2011 1:50 pm, edited 3 times in total.

Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 23 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group