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
;; 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 string)
(chicken time)
srfi-1
srfi-18
nrepl
qt-light)
qt-light
(prefix uri-common uri:)
(prefix vcarded vcard:))
(define qt-app #f) ;; The <qt-application> 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, yknow, 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 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)

View File

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