Actually save edited contacts to vCard files

… at least, modified vCard files. No support for
new files, nor error-handling.
This commit is contained in:
Jaidyn Ann 2024-02-22 12:25:01 -06:00
parent 51de5b4df2
commit bb7816727e

View File

@ -40,6 +40,7 @@
(define *qt-app* #f) ;; The <qt-application> object. (define *qt-app* #f) ;; The <qt-application> object.
(define *qt-win* #f) ;; The <qt-window> object. (define *qt-win* #f) ;; The <qt-window> object.
(define *vcard-pathname* #f) ;; Path to current vCard file. (define *vcard-pathname* #f) ;; Path to current vCard file.
(define *vcard-alist* #f) ;; Parsed alist of current vCard file.
;; Start & run the application. ;; Start & run the application.
@ -167,7 +168,11 @@
menu-file-save "triggered()" menu-file-save "triggered()"
(qt:receiver (qt:receiver
(lambda () (lambda ()
(qt:message "Saving is not implemented."))))) (call-with-output-file *vcard-pathname*
(lambda (in-port)
(vcard:write-vcard
(populate-vcard-with-window window *vcard-alist*)
in-port)))))))
;; If they want a new contact, create a new, blank, window. ;; If they want a new contact, create a new, blank, window.
;; That is, a new process. ;; That is, a new process.
(if menu-file-new (if menu-file-new
@ -216,19 +221,24 @@
(thread-start! (thread-start!
(lambda () (lambda ()
(condition-case (condition-case
(populate-with-vcard ;; Parse the vCard, then populate the windows fields.
window (begin
(with-input-from-file file (set! *vcard-alist*
vcard:read-vcard)) (with-input-from-file file vcard:read-vcard))
(populate-window-with-vcard window *vcard-alist*))
;; … if that didnt work out, complain to the user!
[(vcard) [(vcard)
(set! *vcard-pathname* #f) (set! *vcard-pathname* #f)
(set! *vcard-alist* #f)
(set-window-title! window "New contact") (set-window-title! window "New contact")
(qt:message (qt:message
(string-join (list "This file doesnt seem to be a valid vCard file." (string-join (list "This file doesnt seem to be a valid vCard file."
"Please make sure you selected the right file, and take a look at it manually.")) "Please make sure you selected the right file, and take a look at it manually."))
title: "Parsing error" type: 'critical)] title: "Parsing error" type: 'critical)]
;; … complain harder, harder!!
[exn (file) [exn (file)
(set! *vcard-pathname* #f) (set! *vcard-pathname* #f)
(set! *vcard-alist* #f)
(set-window-title! window "New contact") (set-window-title! window "New contact")
(qt:message (qt:message
(string-join (list "Failed to open the file." (string-join (list "Failed to open the file."
@ -236,18 +246,22 @@
title: "File error" type: 'critical)])))) title: "File error" type: 'critical)]))))
;; Simply map vCard property-names to their corresponding name in the windows ;; Simply map vCard property-names to their corresponding name in the windows
;; fields. ;; fields.
(define property->formname-alist (define property->formname-alist
'((FN . "name") '((FN . "nameLineEdit")
;; (ADR . "address") ;; (ADR . "address")
(TEL . "homePhone") (TEL . "homePhoneLineEdit")
(TEL . "workPhone") (TEL . "workPhoneLineEdit")
(EMAIL . "eMail") (EMAIL . "eMailLineEdit")
(URL . "url") (URL . "urlLineEdit")
(NICKNAME . "nickname"))) (NICKNAME . "nicknameLineEdit")))
;; … and likewise, map window fields names to vCard properties.
(define formname-alist->property
(map (lambda (a) (cons (cdr a) (car a)))
property->formname-alist))
;; Given a parsed vCard in vcardeds alist format, populate the windows fields. ;; Given a parsed vCard in vcardeds alist format, populate the windows fields.
@ -255,11 +269,10 @@
;; ((PROPERTY (ATTRIBUTES) VALUE) ;; ((PROPERTY (ATTRIBUTES) VALUE)
;; (FN () "A. Dmytryshyn") ;; (FN () "A. Dmytryshyn")
;; (ADR ("TYPE=home") ("" "" "1234 Abc.", "", "", "", ""))) ;; (ADR ("TYPE=home") ("" "" "1234 Abc.", "", "", "", "")))
(define (populate-with-vcard window vcard-alist) (define (populate-window-with-vcard window vcard-alist)
(map (lambda (property) (map (lambda (property)
(let* [(formname (alist-ref (car property) property->formname-alist)) (let* [(formname (alist-ref (car property) property->formname-alist))
(lineEditName (conc formname "LineEdit")) (lineEditWidget (if formname (qt:find window formname) #f))]
(lineEditWidget (if formname (qt:find window lineEditName) #f))]
(cond (cond
[lineEditWidget [lineEditWidget
(set! (qt:property lineEditWidget "text") (set! (qt:property lineEditWidget "text")
@ -283,6 +296,26 @@
(set-window-title! window (last (alist-ref 'FN vcard-alist))))) (set-window-title! window (last (alist-ref 'FN vcard-alist)))))
;; Given a Contacts QT window, take its vCard lineEdit widgets and populate a
;; vcarded-style alist with their values.
;; Returns a vCard according the windows textbox values.
(define (populate-vcard-with-window window vcard-alist)
(for-each
(lambda (lineEditName)
(let* [(widget (if lineEditName (qt:find window lineEditName) #f))
(widget-value (if widget (qt:property widget "text") #f))
(property-sym (alist-ref lineEditName formname-alist->property equal?))
(property-value (alist-ref property-sym vcard-alist))]
(when (and widget (not (string-null? widget-value)))
(set! vcard-alist
(alist-update property-sym
(list (if (list? property-value) (car property-value) '())
(qt:property widget "text"))
vcard-alist)))))
(map car formname-alist->property))
vcard-alist)
;; Set a QT windows title, suffixing with the program name (Contact). ;; Set a QT windows title, suffixing with the program name (Contact).
(define (set-window-title! window title) (define (set-window-title! window title)
(set! (qt:property window "windowTitle") (set! (qt:property window "windowTitle")