diff --git a/contact.scm b/contact.scm index c231c5a..194a32b 100644 --- a/contact.scm +++ b/contact.scm @@ -13,13 +13,20 @@ ;; You should have received a copy of the GNU General Public License ;; along with this program. If not, see . -(import (chicken io) +(import scheme + (chicken base) + (chicken file) + (chicken io) + (chicken repl) (chicken repl) (chicken string) (chicken time) + srfi-1 srfi-18 nrepl - qt-light) + qt-light + (prefix uri-common uri:) + (prefix vcarded vcard:)) (define qt-app #f) ;; The object. @@ -71,14 +78,14 @@ ;; Connect callback functions to widgets’ signals. (define (window-callbacks window) - (menubar-callbacks window) - (textbox-callbacks window)) + (menubar-callbacks window)) ;; Connect callback functions to menubar items. (define (menubar-callbacks window) (let* [(menu-file-exit (qt:find window "actionQuit")) - (menu-file-save (qt:find window "actionSave"))] + (menu-file-save (qt:find window "actionSave")) + (menu-file-open (qt:find window "actionOpen"))] ;; We connect to https://doc.qt.io/qt-6/qaction.html#triggered (if menu-file-exit (qt:connect @@ -89,36 +96,53 @@ menu-file-save "triggered()" (qt:receiver (lambda () - (qt:message "Saving is not implemented."))))))) + (qt:message "Saving is not implemented."))))) + (if menu-file-open + (qt:connect + menu-file-open "triggered()" + (qt:receiver + (lambda () + (open-vcard-file + window + (qt:get-open-filename "birdo" "/home/jaidyn/Contacts")))))))) -;; Connect a text-printing callback to each textbox. -(define (textbox-callbacks window) - (let* [(property-names - '("address" "city" "company" "country" "eMail" "fax" - "homePhone" "name" "nickname" "state" "url" - "workPhone" "zip")) - (textboxes - ;; The texbox widgets are all named like “countryLineEdit”. - (map (lambda (property) - (qt:find window (conc property "LineEdit"))) - property-names))] - ;; Cycle through each textbox, connecting them to a callback that prints - ;; their contents each time you type in text. - (map (lambda (textbox) - ;; We connect to https://doc.qt.io/qt-6/qlineedit.html#textChanged - ;; … and print the value of https://doc.qt.io/qt-6/qlineedit.html#text-prop - ;; Also enable the File→Save menu-item (since, y’know, the user - ;; changed something). It defaults to disabled in the contact.ui file. - (if textbox - (qt:connect textbox "textChanged(QString)" - (qt:receiver - (lambda () - (let [(menu-file-save (qt:find window "actionSave"))] - (if menu-file-save - (set! (qt:property menu-file-save "enabled") #t))) - (print (qt:property textbox "text"))))))) - textboxes))) +;; Parse a vCard file and populate the window’s forms with its contents. +(define (open-vcard-file window file) + (if (and (file-exists? file) + (file-readable? file)) + (thread-start! (lambda () (populate-with-vcard + window + (with-input-from-file file + vcard:read-vcard)))))) + + +;; Simply map vCard property-names to their corresponding name in the window’s +;; fields. +(define property->formname-alist + '((FN . "name") + (ADR . "address") + (TEL . "homePhone") + (TEL . "workPhone") + (EMAIL . "eMail") + (URL . "url") + (NICKNAME . "nickname"))) + + +;; Given a parsed vCard in vcarded’s alist format, populate the window’s fields. +;; Here’s the format: +;; ((PROPERTY (ATTRIBUTES) VALUE) +;; (FN () "A. Dmytryshyn") +;; (ADR ("TYPE=home") ("" "" "1234 Abc.", "", "", "", ""))) +(define (populate-with-vcard window vcard-alist) + (map (lambda (property) + (print property) + (let* [(formname (alist-ref (car property) property->formname-alist)) + (lineEditName (conc formname "LineEdit")) + (lineEditWidget (if formname (qt:find window lineEditName) #f))] + (if lineEditWidget + (set! (qt:property lineEditWidget "text") (last property))))) + vcard-alist)) (init) diff --git a/contact.ui b/contact.ui index 128ea85..5ddcb90 100644 --- a/contact.ui +++ b/contact.ui @@ -225,7 +225,9 @@ File + + @@ -244,6 +246,11 @@ Save + + + Open… + +