2024-02-05 15:50:59 -06:00
|
|
|
|
;; Copyright © 2024 Jaidyn Ann <jadedctrl@posteo.at>
|
2024-02-04 20:42:34 -06:00
|
|
|
|
;;
|
|
|
|
|
;; This program is free software: you can redistribute it and/or
|
|
|
|
|
;; modify it under the terms of the GNU General Public License as
|
|
|
|
|
;; published by the Free Software Foundation, either version 3 of
|
|
|
|
|
;; the License, or (at your option) any later version.
|
|
|
|
|
;;
|
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
;;
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
|
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
2024-02-08 12:00:06 -06:00
|
|
|
|
(import scheme
|
|
|
|
|
(chicken base)
|
2024-02-08 19:37:10 -06:00
|
|
|
|
(chicken condition)
|
2024-02-08 12:00:06 -06:00
|
|
|
|
(chicken file)
|
2024-02-08 19:37:10 -06:00
|
|
|
|
(chicken pathname)
|
2024-02-10 00:45:09 -06:00
|
|
|
|
(chicken process)
|
2024-02-08 12:00:06 -06:00
|
|
|
|
(chicken io)
|
|
|
|
|
(chicken repl)
|
2024-02-08 19:37:10 -06:00
|
|
|
|
(chicken process-context)
|
2024-02-04 21:47:56 -06:00
|
|
|
|
(chicken string)
|
2024-02-05 18:10:17 -06:00
|
|
|
|
(chicken time)
|
2024-02-09 16:26:34 -06:00
|
|
|
|
(srfi 1)
|
2024-02-11 18:49:41 -06:00
|
|
|
|
(srfi 4)
|
2024-02-10 00:45:09 -06:00
|
|
|
|
(srfi 13)
|
2024-02-09 16:26:34 -06:00
|
|
|
|
(srfi 18)
|
2024-02-08 19:37:10 -06:00
|
|
|
|
(prefix getopt-long getopt:)
|
2024-02-09 16:26:34 -06:00
|
|
|
|
(prefix nrepl nrepl:)
|
2024-02-08 12:00:06 -06:00
|
|
|
|
(prefix uri-common uri:)
|
2024-02-09 16:26:34 -06:00
|
|
|
|
(prefix vcarded vcard:)
|
|
|
|
|
qt-light)
|
2024-02-04 20:42:34 -06:00
|
|
|
|
|
|
|
|
|
|
2024-02-12 22:46:37 -06:00
|
|
|
|
(define *qt-app* #f) ;; The <qt-application> object.
|
|
|
|
|
(define *qt-win* #f) ;; The <qt-window> object.
|
|
|
|
|
(define *vcard-pathname* #f) ;; Path to current vCard file.
|
2024-02-05 18:10:17 -06:00
|
|
|
|
|
|
|
|
|
|
2024-02-04 20:42:34 -06:00
|
|
|
|
;; Start & run the application.
|
|
|
|
|
(define (init)
|
2024-02-08 19:37:10 -06:00
|
|
|
|
(let [(cli-args (parse-cli-args (command-line-arguments)))]
|
2024-02-09 16:26:34 -06:00
|
|
|
|
;; If --help, then print a usage message and quit.
|
2024-02-08 19:37:10 -06:00
|
|
|
|
(if (alist-ref 'help cli-args)
|
|
|
|
|
(cli-usage))
|
2024-02-09 16:26:34 -06:00
|
|
|
|
;; Otherwise, let’s get our threads started!
|
|
|
|
|
(let [(qt-thread (init-qt cli-args))
|
|
|
|
|
(nrepl-thread (init-nrepl cli-args))
|
|
|
|
|
(repl-thread (thread-start! repl))]
|
2024-02-09 00:09:28 -06:00
|
|
|
|
;; Wait for the QT program, even after stdin is closed off.
|
|
|
|
|
(thread-join! qt-thread))))
|
2024-02-08 19:37:10 -06:00
|
|
|
|
|
|
|
|
|
|
2024-02-12 22:46:37 -06:00
|
|
|
|
(define (last-free-arg cli-args)
|
|
|
|
|
(condition-case (last (car cli-args)) (var () #f)))
|
|
|
|
|
|
|
|
|
|
|
2024-02-09 16:26:34 -06:00
|
|
|
|
;; Set up some global variables (for easier live REPL use), prepare the QT app.
|
|
|
|
|
(define (init-qt cli-args)
|
2024-02-12 22:46:37 -06:00
|
|
|
|
(set! *qt-app* (qt:init))
|
|
|
|
|
(set! *qt-win* (create-window))
|
|
|
|
|
(init-window *qt-win*)
|
2024-02-09 16:26:34 -06:00
|
|
|
|
;; Kick off the QT thread, then open the cli free-arg vCard file, if provided.
|
|
|
|
|
;; That is, like `$ contact freeArgFile.vcf`.
|
|
|
|
|
(let [(qt-thread (thread-start! qt-loop))
|
2024-02-12 22:46:37 -06:00
|
|
|
|
(last-arg (last-free-arg cli-args))]
|
|
|
|
|
(when (and (string? last-arg) (file-exists? last-arg))
|
|
|
|
|
(open-vcard-file *qt-win* last-arg))
|
2024-02-09 16:26:34 -06:00
|
|
|
|
qt-thread))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; Kick off our remote TCP-accessible REPL, if the user enabled it at cli.
|
|
|
|
|
(define (init-nrepl cli-args)
|
|
|
|
|
(if (alist-ref 'repl cli-args)
|
|
|
|
|
(thread-start!
|
|
|
|
|
(lambda ()
|
|
|
|
|
(nrepl:nrepl (string->number (alist-ref 'repl cli-args)))))))
|
|
|
|
|
|
|
|
|
|
|
2024-02-08 19:37:10 -06:00
|
|
|
|
;; Print a “usage” help message, telling the user how to run the program.
|
|
|
|
|
(define (cli-usage)
|
2024-02-09 00:09:28 -06:00
|
|
|
|
(print "usage: " (pathname-file (program-name)) " [-h] [--repl PORT] [VCF_FILE]")
|
2024-02-08 19:37:10 -06:00
|
|
|
|
(print)
|
|
|
|
|
(print (pathname-file (program-name)) " is a simple contacts program for managing")
|
|
|
|
|
(print "vCard-format contacts.")
|
|
|
|
|
(print)
|
|
|
|
|
(print (getopt:usage cli-args-grammar))
|
|
|
|
|
(exit))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; Parse out command-line args into an alist.
|
|
|
|
|
;; '("dad" "-h" "--repl=12" "mm") → '(("dad" "mm") (help #t) (repl 12))
|
|
|
|
|
(define (parse-cli-args args)
|
|
|
|
|
(handle-exceptions
|
|
|
|
|
exn
|
|
|
|
|
(and
|
|
|
|
|
(print ((condition-property-accessor 'exn 'message) exn))
|
|
|
|
|
'((help . #t)))
|
|
|
|
|
(getopt:getopt-long args cli-args-grammar)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; Definition of our command-line arguments, in getopt-long’s format.
|
|
|
|
|
(define cli-args-grammar
|
|
|
|
|
'((help "display this help message"
|
|
|
|
|
(single-char #\h))
|
|
|
|
|
(repl "start a TCP-accesible REPL at the given port"
|
|
|
|
|
(value #t))))
|
2024-02-05 18:10:17 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; Loop through QT’s processing, again and again.
|
|
|
|
|
(define (qt-loop)
|
|
|
|
|
(qt:run #t)
|
|
|
|
|
(qt-loop))
|
2024-02-04 20:42:34 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; Create the application window.
|
|
|
|
|
(define (create-window)
|
|
|
|
|
(qt:widget (window-contents)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; Return the UI’s XML, read from “contacts.ui”.
|
|
|
|
|
;; We could generate this XML ourselves, and write a nice s-expr front-end,
|
|
|
|
|
;; maybe… `o`
|
|
|
|
|
(define (window-contents)
|
|
|
|
|
(call-with-input-file
|
|
|
|
|
"contact.ui"
|
|
|
|
|
(lambda (in-port) (read-string #f in-port))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; Initialize the window.
|
|
|
|
|
(define (init-window window)
|
2024-02-11 11:52:20 -06:00
|
|
|
|
;; Set the profile-picture label to a default theme icon.
|
|
|
|
|
(let [(default-profile-pic
|
|
|
|
|
(or (qt:theme-icon "person") (qt:theme-icon "contact-new")))]
|
|
|
|
|
(if default-profile-pic
|
|
|
|
|
(set! (qt:property (qt:find window "avatarLabel") "pixmap")
|
|
|
|
|
(qt:icon->pixmap default-profile-pic 100 100))))
|
|
|
|
|
;; Now prepare callbacks and show the window.
|
2024-02-04 20:54:58 -06:00
|
|
|
|
(window-callbacks window)
|
2024-02-04 20:42:34 -06:00
|
|
|
|
(qt:show window))
|
|
|
|
|
|
|
|
|
|
|
2024-02-04 20:54:58 -06:00
|
|
|
|
;; Connect callback functions to widgets’ signals.
|
|
|
|
|
(define (window-callbacks window)
|
2024-02-08 12:00:06 -06:00
|
|
|
|
(menubar-callbacks window))
|
2024-02-04 22:00:06 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; Connect callback functions to menubar items.
|
|
|
|
|
(define (menubar-callbacks window)
|
2024-02-05 15:50:59 -06:00
|
|
|
|
(let* [(menu-file-exit (qt:find window "actionQuit"))
|
2024-02-08 12:00:06 -06:00
|
|
|
|
(menu-file-save (qt:find window "actionSave"))
|
2024-02-10 00:45:09 -06:00
|
|
|
|
(menu-file-open (qt:find window "actionOpen"))
|
|
|
|
|
(menu-file-new (qt:find window "actionNew"))]
|
2024-02-04 21:47:56 -06:00
|
|
|
|
;; We connect to https://doc.qt.io/qt-6/qaction.html#triggered
|
2024-02-10 00:45:09 -06:00
|
|
|
|
;; Simply kill the program.
|
2024-02-05 15:50:59 -06:00
|
|
|
|
(if menu-file-exit
|
2024-02-04 20:54:58 -06:00
|
|
|
|
(qt:connect
|
2024-02-05 15:50:59 -06:00
|
|
|
|
menu-file-exit "triggered()"
|
2024-02-04 22:00:06 -06:00
|
|
|
|
(qt:receiver exit)))
|
2024-02-10 00:45:09 -06:00
|
|
|
|
;; If they try to Save, tell them it’s not supported.
|
2024-02-05 15:50:59 -06:00
|
|
|
|
(if menu-file-save
|
2024-02-04 22:00:06 -06:00
|
|
|
|
(qt:connect
|
2024-02-05 15:50:59 -06:00
|
|
|
|
menu-file-save "triggered()"
|
2024-02-04 22:00:06 -06:00
|
|
|
|
(qt:receiver
|
|
|
|
|
(lambda ()
|
2024-02-08 12:00:06 -06:00
|
|
|
|
(qt:message "Saving is not implemented.")))))
|
2024-02-10 00:45:09 -06:00
|
|
|
|
;; If they want a new contact, create a new, blank, window.
|
|
|
|
|
;; That is, a new process.
|
|
|
|
|
(if menu-file-new
|
|
|
|
|
(qt:connect
|
|
|
|
|
menu-file-new "triggered()"
|
2024-02-12 22:46:37 -06:00
|
|
|
|
(qt:receiver create-new-window)))
|
2024-02-10 00:45:09 -06:00
|
|
|
|
;; If they want to open a contact through the Open… dialogue, we should open
|
|
|
|
|
;; the contact in a new window.
|
2024-02-08 12:00:06 -06:00
|
|
|
|
(if menu-file-open
|
|
|
|
|
(qt:connect
|
|
|
|
|
menu-file-open "triggered()"
|
|
|
|
|
(qt:receiver
|
|
|
|
|
(lambda ()
|
2024-02-11 11:52:20 -06:00
|
|
|
|
(let* [(contacts-dir (conc (get-environment-variable "HOME")
|
|
|
|
|
"/Contacts"))
|
2024-02-10 00:45:09 -06:00
|
|
|
|
(selected-file (qt:get-open-filename
|
|
|
|
|
"Select a contact file to open…"
|
2024-02-12 22:46:37 -06:00
|
|
|
|
contacts-dir))]
|
2024-02-10 00:45:09 -06:00
|
|
|
|
(if (not (string-null? selected-file))
|
2024-02-12 22:46:37 -06:00
|
|
|
|
(if *vcard-pathname*
|
|
|
|
|
(create-new-window selected-file)
|
|
|
|
|
(open-vcard-file window selected-file))))))))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; Executes a new instance of the program; optionally, with a contact-file’s
|
|
|
|
|
;; pathname as a new argument.
|
|
|
|
|
;; Functionally, opens a new contacts window.
|
|
|
|
|
(define (create-new-window . pathname)
|
|
|
|
|
(let* [(pathname (optional pathname #f))
|
|
|
|
|
(clean-cli-args
|
|
|
|
|
(drop-right (cdr (argv))
|
|
|
|
|
(length (command-line-arguments))))
|
|
|
|
|
(program-args
|
|
|
|
|
(if pathname
|
|
|
|
|
(append clean-cli-args (list pathname))
|
|
|
|
|
clean-cli-args))]
|
|
|
|
|
(process-run
|
|
|
|
|
(executable-pathname)
|
|
|
|
|
program-args)))
|
2024-02-10 00:45:09 -06:00
|
|
|
|
|
2024-02-08 12:00:06 -06:00
|
|
|
|
|
|
|
|
|
;; Parse a vCard file and populate the window’s forms with its contents.
|
|
|
|
|
(define (open-vcard-file window file)
|
2024-02-12 22:46:37 -06:00
|
|
|
|
(when (and (file-exists? file)
|
|
|
|
|
(file-readable? file))
|
|
|
|
|
(set! *vcard-pathname* file)
|
|
|
|
|
(thread-start! (lambda () (populate-with-vcard
|
|
|
|
|
window
|
|
|
|
|
(with-input-from-file file
|
|
|
|
|
vcard:read-vcard))))))
|
2024-02-08 12:00:06 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; Simply map vCard property-names to their corresponding name in the window’s
|
|
|
|
|
;; fields.
|
|
|
|
|
(define property->formname-alist
|
|
|
|
|
'((FN . "name")
|
2024-02-11 18:49:41 -06:00
|
|
|
|
;; (ADR . "address")
|
2024-02-08 12:00:06 -06:00
|
|
|
|
(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)
|
|
|
|
|
(let* [(formname (alist-ref (car property) property->formname-alist))
|
|
|
|
|
(lineEditName (conc formname "LineEdit"))
|
|
|
|
|
(lineEditWidget (if formname (qt:find window lineEditName) #f))]
|
2024-02-11 18:49:41 -06:00
|
|
|
|
(cond
|
|
|
|
|
[lineEditWidget
|
2024-02-11 19:12:20 -06:00
|
|
|
|
(set! (qt:property lineEditWidget "text")
|
|
|
|
|
(cond
|
|
|
|
|
[(string? (last property))
|
|
|
|
|
(last property)]
|
|
|
|
|
[(uri:uri? (last property))
|
|
|
|
|
(uri:uri->string (last property))]
|
|
|
|
|
[#t ""]))]
|
2024-02-11 18:49:41 -06:00
|
|
|
|
[(and (eq? (car property) 'PHOTO)
|
|
|
|
|
(list? (last property)))
|
|
|
|
|
(let* [(avatar (qt:find window "avatarLabel"))
|
2024-02-12 22:46:37 -06:00
|
|
|
|
(old-pixmap
|
|
|
|
|
(if avatar (qt:property avatar "pixmap")))
|
|
|
|
|
[new-pixmap
|
|
|
|
|
(if avatar (u8vector->pixmap (cadr (last property))))]]
|
2024-02-11 18:49:41 -06:00
|
|
|
|
(when avatar
|
2024-02-11 19:12:20 -06:00
|
|
|
|
(set! (qt:property avatar "pixmap") new-pixmap)))])))
|
2024-02-08 12:00:06 -06:00
|
|
|
|
vcard-alist))
|
2024-02-04 20:54:58 -06:00
|
|
|
|
|
|
|
|
|
|
2024-02-11 18:49:41 -06:00
|
|
|
|
;; Given a image bytevector (u8vector), create a corresponding pixmap.
|
|
|
|
|
(define (u8vector->pixmap vector)
|
|
|
|
|
(let* ([temp-file (write-temporary-file vector)]
|
|
|
|
|
[pixmap (qt:pixmap temp-file)])
|
|
|
|
|
(delete-file* temp-file)
|
|
|
|
|
pixmap))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; Given a u8vector for file-contents, create a tempory file and write the
|
|
|
|
|
;; contents to it. Returns the pathname.
|
|
|
|
|
(define (write-temporary-file u8vector-contents)
|
|
|
|
|
(let [(temp-file (create-temporary-file))]
|
|
|
|
|
(with-output-to-file temp-file
|
|
|
|
|
(lambda () (map write-byte (u8vector->list u8vector-contents))))
|
|
|
|
|
temp-file))
|
|
|
|
|
|
|
|
|
|
|
2024-02-04 20:42:34 -06:00
|
|
|
|
(init)
|