-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpbui-contacts-app.el
223 lines (189 loc) · 9.1 KB
/
pbui-contacts-app.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
;;; pbui-contacts-app.el --- A PBUI demo application -*- lexical-binding: t -*-
;; Copyright (C) 2021 Mariano Montone
;; Author: Mariano Montone <marianomontone@gmail.com>
;; URL: https://github.com/mmontone/pbui
;; Keywords: user-interface
;; Version: 0.1
;; Package-Requires: ((emacs "25") (dash "2.19.1"))
;; 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 <http://www.gnu.org/licenses/>.
;;; Commentary:
;; A PBUI demo application.
;; Load and then M-x contacts-app to run.
;;; Code:
(require 'request)
(require 's)
(require 'calendar)
(require 'outline)
(require 'pbui)
(defvar contacts-app:contacts nil)
(defmacro with-text-properties (properties &rest body)
`(let ((tp-start (point)))
,@body
(set-text-properties tp-start (point) ,properties)))
(defun contacts-app:user-fullname (user)
(format "%s %s" (alist-get 'first (alist-get 'name user))
(alist-get 'last (alist-get 'name user))))
(defun contacts-app:user-id (user)
(alist-get 'value (alist-get 'id user)))
(defun contacts-app:user-email (user)
(alist-get 'email user))
(defun contacts-app:phone (user)
(alist-get 'phone user))
(defun contacts-app:create-user-birthday-event (user)
(list 'title (format "%s birthday" (contacts-app:user-fullname user))
'date (let ((date (parse-time-string (alist-get 'date (alist-get 'dob user)))))
(setf (nth 5 date) (nth 2 (calendar-current-date)))
date)
'date-string (alist-get 'date (alist-get 'dob user))
'description (format "It is %s birthday." (contacts-app:user-fullname user))))
(defun contacts-app ()
"Contacts application entry point command."
(interactive)
(request "https://randomuser.me/api/?results=50"
:success
(cl-function
(lambda (&key data &allow-other-keys)
(setf contacts-app:contacts (alist-get 'results (json-read-from-string data)))
(contacts-app-create-buffer)))))
(defun contacts-app-create-buffer ()
(let ((buffer (get-buffer-create "*contacts-app*")))
(with-current-buffer buffer
(cl-loop for user across contacts-app:contacts
do
;; Heading
(insert "* ")
(with-text-properties
(list 'presentation
(list 'type 'contacts-app:user
'value user
'printer 'contacts-app:user-fullname))
(insert (contacts-app:user-fullname user)))
(insert " - ")
(with-text-properties
(list 'presentation
(list 'type 'email
'value (alist-get 'email user)))
(insert (alist-get 'email user)))
(newline)
;; Body
(insert "- Fullname: " (alist-get 'first (alist-get 'name user))
" " (alist-get 'last (alist-get 'name user)))
(newline)
(insert "- Email: " (propertize (alist-get 'email user)
'presentation
(list 'type 'email
'value (alist-get 'email user))))
(newline)
(insert "- Phone: " (propertize (alist-get 'phone user)
'presentation
(list 'type 'phone
'value (alist-get 'phone user))))
(newline)
(insert "- Cell: " (propertize (alist-get 'cell user)
'presentation
(list 'type 'phone
'value (alist-get 'cell user))))
(newline)
(insert "- Birthdate: "
(let ((birthdate (alist-get 'date (alist-get 'dob user))))
;;(propertize birthdate
;; 'presentation
;; (list 'type 'date
;; 'value birthdate))
(propertize birthdate
'presentation
(list 'type 'calendar-event
'value (contacts-app:create-user-birthday-event user)
'printer (lambda (event)
(format "Calendar event - %s" (cl-getf event 'title)))))))
(newline))
(outline-mode)
(outline-hide-sublevels 1)
(setq buffer-read-only t))
(pop-to-buffer buffer)))
(def-presentation-command (contacts-app:delete-contacts
:title "Delete contacts"
:description "Delete contacts")
((users contacts-app:user))
(when (yes-or-no-p (format "Delete %d selected contacts?"
(length users)))
(let ((users-ids (cl-remove-if 'null (mapcar 'contacts-app:user-id users))))
(setf contacts-app:contacts
(cl-delete-if (lambda (user)
(cl-member (contacts-app:user-id user)
users-ids))
contacts-app:contacts))
(let ((buffer (get-buffer "*contacts-app*")))
(with-current-buffer buffer
(with-write-buffer
(erase-buffer)
(contacts-app-create-buffer))))
(message "Selected contacts deleted"))))
(def-presentation-command (contacts-app:send-email
:title "Send email"
:description "Send email to contacts")
((users contacts-app:user))
(call-process "/usr/bin/xdg-open" nil nil nil
(format "mailto:%s" (s-join "," (mapcar 'contacts-app:user-email users)))))
(def-presentation-command (contacts-app:add-event-to-google-calendar
:title "Add event to Google calendar"
:description "Add event to Google calendar")
((event calendar-event))
(call-process "/usr/bin/xdg-open" nil nil nil
(format "http://www.google.com/calendar/render?action=TEMPLATE&text=%s&dates=%s&details=%s&location=%s"
(cl-getf event 'title)
(format "%s%s%s"
(nth 5 (cl-getf event 'date))
(nth 4 (cl-getf event 'date))
(nth 3 (cl-getf event 'date)))
(cl-getf event 'description)
"")))
(def-presentation-command (contacts-app:send-files-in-email
:title "Send files by email"
:description "Send files by email"
:applyable-when (lambda (args)
(and args
(cl-some (lambda (arg)
(member (cl-getf arg 'type) '(contacts-app:user email)))
args)
(cl-some (lambda (arg)
(eql (cl-getf arg 'type) 'file)) args))))
((users contacts-app:user) (emails email) (files file))
(let ((all-emails (append (mapcar 'contacts-app:user-email users)
emails)))
(call-process "/usr/bin/thunderbird" nil nil nil
"-compose"
(format "to='%s',attachment='%s'"
(s-join "," (mapcar 'contacts-app:user-email users))
(s-join "," files)))))
(def-presentation-command (contacts-app:insert-birthday-in-org
:title "Insert birthday in org file"
:description "Insert birthday in org file"
:condition (lambda (ps) (and (featurep 'org) (eql major-mode 'org-mode))))
((user contacts-app:user))
(insert
(format-time-string
(org-time-stamp-format)
(parse-time-string
(alist-get 'date (alist-get 'dob user))))))
(def-presentation-command (contacts-app:insert-calendar-event-in-org
:title "Insert calendar event in org file"
:description "Insert calendar event in org file"
:condition (lambda (ps) (and (featurep 'org) (eql major-mode 'org-mode))))
((event calendar-event))
(insert
(format-time-string
(org-time-stamp-format)
(parse-time-string
(cl-getf event 'date-string)))))
(provide 'pbui-contacts-app)
;;; pbui-contacts-app ends here