sprefs.scm
(module sprefs mzscheme
        (require "scfg.scm")
        (provide sprefs-new
                 sprefs-lambda-new
                 sprefs-get
                 sprefs-set!
                 sprefs-cond-set!-get)
        (require "sutil.scm")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;#+pod
;
;=pod
;
;=syn scm,8
;
;=wikiwikiwiki
;
;=SPREFS - Preferences for a user for an application
;
;Uses SCFG to provide preferences for a given user for a given application.
;This keeps a global module variable #PREFERENCES#. This module uses the
;#scfg-mult-get/set!# functions for accessing its configuration files.
;
;Configuration files will be stored in #$HOME/.scm_settings#. Filename:
;#$HOME/.scm_settings/<application>.scfg#. Where #<application># is given
;with #sprefs-new#.
;
;=Synopsis
;
; >(require (planet "scfg.scm" ("oesterholt" "ho-utils.plt" 1 0)))
;
; >(sprefs-get 'a)
; Call sprefs-new first
;
; >(sprefs-new "my-application")
;
; >(sprefs-set! '(a b c) "abc")
;
; >(sprefs-get '(a b c))
; "abc"
; >(sprefs-get '(a b c d))
; undef
; >(sprefs-get '(a b c d) "HI")
; "HI"
;
; >(sprefs-cond-set!-get '(a b c d) "HI")
; "HI"
; >(sprefs-cond-set!-get '(a b c d) "HI" "OK!")
; "OK!"
; >(sprefs-get '(a b c d) "HI")
; "OK!"
;
;=API
;
;===#(sprefs-new <application>) : boolean#
;
;Initializes the global preferences configuration file.
;/Returns/ !#t.
;
;===#(sprefs-lambda-new setter getter) : <unspecified>#
;
;Initializes the global preferences from a getter and a setter function.
;The getter must be of type #(getter key . default-value) : value#, the
;setter of type #(setter key value) : <unspecified>#.
;         
;===#(sprefs-get <key> . <default-value>) : <value>#
;
;Performs [scfg.html #scfg-get# ] on the given configuration file.
;
;===#(sprefs-set! <key> <value>) : <unspecified>#
;
;Performs [scfg.html #scfg-set!# ] on the application configuration file.
;
;===#(sprefs-cond-set!-get <key> <default-value> . <set-value>) : <value>#
;
;Performs [scfg.html #scfg-cond-set!-get# ] on the application configuration file.
;
;
;=Info
;
;(c) 2005 Hans !Oesterholt-Dijkema. Distributed undef LGPL.
;Contact: send email to hans in domain elemental-programming.org.
;Homepage: [http://www.elemental-programming.org].
;
;=wikiwikiwiki
;
;=cut
;
;##
	

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Determine where HOME is and create settings directory
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(define HOME (path->string (find-system-path 'home-dir)))

(define SETTINGS (string-append HOME "/.scm_settings"))

(if (not (directory-exists? SETTINGS))
    (make-directory SETTINGS))

(define PREFERENCES #f)
(define SETTER #f)
(define GETTER #f)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Work out preferences; they should be individual.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(define (sprefs-new application)
  (let ((filename (string-append SETTINGS "/" application ".scfg")))
    (set! PREFERENCES (scfg-new filename))
    #t))

(define (sprefs-lambda-new setter getter)
  (set! PREFERENCES 'lambda)
  (set! GETTER getter)
  (set! SETTER setter))

(define (sprefs-get key . default)
  (if (eq? PREFERENCES #f)
      (error "Call sprefs-new first")
      (if (eq? PREFERENCES 'lambda)
          (if (null? default)
              (GETTER key)
              (GETTER key (car default)))
          (if (null? default)
              (scfg-multi-get PREFERENCES key)
              (scfg-multi-get PREFERENCES key (car default))))))

(define (sprefs-set! key value)
  (if (eq? PREFERENCES #f)
      (error "Call sprefs-new first")
      (if (eq? PREFERENCES 'lambda)
          (SETTER key value)
          (scfg-multi-set! PREFERENCES key value))))

(define (sprefs-cond-set!-get key default . value)
  (if (not (null? value))
      (sprefs-set! key (car value)))
  (sprefs-get key default))

)