Support base-level opening of vCard files

Fields are filled naively and unreliably; this
just lays the groundwork.
Also removes the old testing callbacks for textboxes.
This commit is contained in:
Jaidyn Ann 2024-02-08 12:00:06 -06:00
parent a06bd33f9d
commit b91590133c
2 changed files with 64 additions and 33 deletions

View File

@ -13,13 +13,20 @@
;; You should have received a copy of the GNU General Public License ;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
(import (chicken io) (import scheme
(chicken base)
(chicken file)
(chicken io)
(chicken repl)
(chicken repl) (chicken repl)
(chicken string) (chicken string)
(chicken time) (chicken time)
srfi-1
srfi-18 srfi-18
nrepl nrepl
qt-light) qt-light
(prefix uri-common uri:)
(prefix vcarded vcard:))
(define qt-app #f) ;; The <qt-application> object. (define qt-app #f) ;; The <qt-application> object.
@ -71,14 +78,14 @@
;; Connect callback functions to widgets signals. ;; Connect callback functions to widgets signals.
(define (window-callbacks window) (define (window-callbacks window)
(menubar-callbacks window) (menubar-callbacks window))
(textbox-callbacks window))
;; Connect callback functions to menubar items. ;; Connect callback functions to menubar items.
(define (menubar-callbacks window) (define (menubar-callbacks window)
(let* [(menu-file-exit (qt:find window "actionQuit")) (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 ;; We connect to https://doc.qt.io/qt-6/qaction.html#triggered
(if menu-file-exit (if menu-file-exit
(qt:connect (qt:connect
@ -89,36 +96,53 @@
menu-file-save "triggered()" menu-file-save "triggered()"
(qt:receiver (qt:receiver
(lambda () (lambda ()
(qt:message "Saving is not implemented."))))))) (qt:message "Saving is not implemented.")))))
(if menu-file-open
(qt:connect
;; Connect a text-printing callback to each textbox. menu-file-open "triggered()"
(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, yknow, the user
;; changed something). It defaults to disabled in the contact.ui file.
(if textbox
(qt:connect textbox "textChanged(QString)"
(qt:receiver (qt:receiver
(lambda () (lambda ()
(let [(menu-file-save (qt:find window "actionSave"))] (open-vcard-file
(if menu-file-save window
(set! (qt:property menu-file-save "enabled") #t))) (qt:get-open-filename "birdo" "/home/jaidyn/Contacts"))))))))
(print (qt:property textbox "text")))))))
textboxes)))
;; Parse a vCard file and populate the windows 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 windows
;; fields.
(define property->formname-alist
'((FN . "name")
(ADR . "address")
(TEL . "homePhone")
(TEL . "workPhone")
(EMAIL . "eMail")
(URL . "url")
(NICKNAME . "nickname")))
;; Given a parsed vCard in vcardeds alist format, populate the windows fields.
;; Heres 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) (init)

View File

@ -225,7 +225,9 @@
<property name="title"> <property name="title">
<string>File</string> <string>File</string>
</property> </property>
<addaction name="actionOpen"/>
<addaction name="actionSave"/> <addaction name="actionSave"/>
<addaction name="separator"/>
<addaction name="actionQuit"/> <addaction name="actionQuit"/>
</widget> </widget>
<addaction name="menuFile"/> <addaction name="menuFile"/>
@ -244,6 +246,11 @@
<string>Save</string> <string>Save</string>
</property> </property>
</action> </action>
<action name="actionOpen">
<property name="text">
<string>Open…</string>
</property>
</action>
</widget> </widget>
<resources/> <resources/>
<connections/> <connections/>