-
Notifications
You must be signed in to change notification settings - Fork 1
/
bic-mailbox-tree.el
321 lines (287 loc) · 11.5 KB
/
bic-mailbox-tree.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
;;; bic-mailbox-tree.el --- display tree of mailboxes -*- lexical-binding: t; -*-
;; Copyright (C) 2015 Magnus Henoch
;; Author: Magnus Henoch <[email protected]>
;; 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:
;; Use tree-widget to display known accounts and their mailboxes.
;;; Code:
(require 'tree-widget)
;;; Mailbox tree
(defface bic-mailbox-tree-account-connected
'((t (:inherit gnus-server-opened)))
"Face used for connected accounts in mailbox tree."
:group 'bic)
(defface bic-mailbox-tree-account-disconnected
'((t (:inherit gnus-server-denied)))
"Face used for disconnected accounts in mailbox tree."
:group 'bic)
(defface bic-mailbox-tree-account-deactivated
'((t (:inherit gnus-server-closed)))
"Face used for deactivated accounts in mailbox tree."
:group 'bic)
(defface bic-mailbox-tree-mailbox-unlimited-sync-unread
'((t (:inherit gnus-group-mail-1)))
"Face used for fully synced mailboxes with unread messages in mailbox tree."
:group 'bic)
(defface bic-mailbox-tree-mailbox-unlimited-sync
'((t (:inherit gnus-group-mail-1-empty)))
"Face used for fully synced mailboxes in mailbox tree."
:group 'bic)
(defface bic-mailbox-tree-mailbox-partial-sync-unread
'((t (:inherit gnus-group-mail-low)))
"Face used for partially synced mailboxes in mailbox tree."
:group 'bic)
(defface bic-mailbox-tree-mailbox-partial-sync
'((t (:inherit gnus-group-mail-low-empty)))
"Face used for partially synced mailboxes in mailbox tree."
:group 'bic)
(defface bic-mailbox-tree-mailbox-unsubscribed
'((t (:inherit gnus-group-news-low-empty)))
"Face used for unsubscribed mailboxes in mailbox tree."
:group 'bic)
(defvar bic-mailbox-tree-mode-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map special-mode-map)
(define-key map (kbd "RET") 'bic-mailbox-tree-press-button-on-current-line)
(define-key map (kbd "s") 'bic-mailbox-tree-sync)
(define-key map (kbd "u") 'bic-mailbox-tree-no-sync)
map))
(define-derived-mode bic-mailbox-tree-mode special-mode "BIC mailbox tree"
"Major mode for tree of IMAP mailboxes accessed by `bic'."
(add-hook 'bic-account-state-update-functions
'bic-mailbox-tree--update-account-state)
(add-hook 'bic-account-mailbox-update-functions
'bic-mailbox-tree--update-mailbox-state)
(widget-minor-mode)
(setq-local widget-global-map bic-mailbox-tree-mode-map))
(defun bic-mailbox-tree-press-button-on-current-line (&optional event)
;; checkdoc-params: (event)
"Find button on current line and press it.
By default, widget mode is too stingy about where the point has
to be for the button press to count. Let's try to do what the
user expects."
(interactive "@d")
(let ((button (bic--button-on-current-line)))
(if (null button)
(user-error "No button on this line")
(widget-apply-action button event))))
(defun bic-mailbox-tree-sync ()
"Set sync level for mailbox on current line.
If the mailbox is not being synced, set it to unlimited sync.
If the mailbox is being synced, toggle betweed unlimited and
partial sync."
(interactive)
;; Need to get the button that has the mailbox name as a property,
;; not the tree widget button.
(let* ((button (bic--button-on-current-line
(lambda (b) (not (null (widget-get b :mailbox-name))))))
(account-name (and button (widget-get button :account-name)))
(mailbox-name (and button (widget-get button :mailbox-name))))
(if (or (null button)
(null account-name))
(user-error "No mailbox on this line")
(let* ((mailbox-data
(gethash mailbox-name (gethash account-name bic-account-mailbox-table)))
(new-sync-level
(cl-ecase (bic--infer-sync-level mailbox-data)
((nil :partial-sync)
(message "Changing %s to unlimited sync" mailbox-name)
'unlimited-sync)
(:unlimited-sync
(message "Changing %s to partial sync" mailbox-name)
'partial-sync))))
(bic-mailbox-set-sync-level account-name mailbox-name new-sync-level)))))
(defun bic-mailbox-tree-no-sync ()
"Set mailbox on current line to \"no sync\"."
(interactive)
(let* ((button (bic--button-on-current-line
(lambda (b) (not (null (widget-get b :mailbox-name))))))
(account-name (and button (widget-get button :account-name)))
(mailbox-name (and button (widget-get button :mailbox-name))))
(if (or (null button)
(null account-name))
(user-error "No mailbox on this line")
(message "Changing %s to no sync" mailbox-name)
(bic-mailbox-set-sync-level account-name mailbox-name 'no-sync))))
(defun bic--button-on-current-line (&optional predicate)
"Find and return a button on the current line, if any.
If PREDICATE is non-nil, return the first button that
satisifies PREDICATE."
(save-excursion
(forward-line 0)
(let ((end (line-end-position))
(button-pos (point))
button)
(while (and (null button) button-pos (< button-pos end))
(setq button (get-char-property button-pos 'button))
(when predicate
(unless (funcall predicate button)
(setq button nil)))
(unless button
(setq button-pos
(next-single-char-property-change
button-pos 'button nil (line-end-position)))))
button)))
;;;###autoload
(defun bic-mailbox-tree ()
"Show mailbox tree buffer."
(interactive)
(with-current-buffer (get-buffer-create "*Mailboxes*")
(unless (derived-mode-p 'bic-mailbox-tree-mode)
(bic-mailbox-tree-mode)
(bic-mailbox-tree--init))
(switch-to-buffer (current-buffer))))
(defvar-local bic-mailbox-tree--widget nil)
(defun bic-mailbox-tree--init ()
"Initialise a mailbox tree buffer."
(unless bic-mailbox-tree--widget
(setq bic-mailbox-tree--widget
(widget-create
'tree-widget
:tag "Accounts"
:open t
:expander #'bic-mailbox-tree--accounts
:expander-p (lambda (&rest _) t)))))
(defun bic-mailbox-tree--accounts (_parent)
"Return tree widgets for all accounts."
(mapcar
(lambda (fsm)
(let ((address (plist-get (fsm-get-state-data fsm) :address)))
(bic-mailbox-tree--account address)))
bic-running-accounts))
(defun bic-mailbox-tree--account (address)
"Return a tree widget for the account named ADDRESS."
(widget-convert
'tree-widget
:tag (bic-mailbox-tree--account-tag address (gethash address bic-account-state-table))
:address address
:expander #'bic-mailbox-tree--mailboxes
:expander-p (lambda (&rest _) t)))
(defun bic-mailbox-tree--account-tag (address state)
"Return a \"tag\" for the account named ADDRESS based on its STATE.
The tag consists of the address, followed by one of \"connected\",
\"disconnected\" or \"deactivated\" in parentheses."
(propertize
(format "%s (%s)" address (substring (symbol-name state) 1))
'face
(cl-case state
(:connected 'bic-mailbox-tree-account-connected)
(:disconnected 'bic-mailbox-tree-account-disconnected)
(:deactivated 'bic-mailbox-tree-account-deactivated))))
(defun bic-mailbox-tree--update-account-state (account new-state)
"Update the state of ACCOUNT in the mailbox tree buffer.
NEW-STATE is a keyword, one of :connected, :disconnected or
:deactivated."
(let ((buffer (get-buffer "*Mailboxes*")))
(when buffer
(with-current-buffer buffer
(let* ((children (widget-get bic-mailbox-tree--widget :children))
(account-widget
(cl-find-if
(lambda (child)
(and (tree-widget-p child)
(equal (widget-get child :address) account)))
children)))
(cond
((and account-widget new-state)
;; State change for existing account
(let ((new-tag (bic-mailbox-tree--account-tag account new-state))
(node (car (widget-get account-widget :children))))
(if (null node)
(warn "no node: %S" account-widget)
;; XXX: not sure why we need to put the tag in two places
(widget-put account-widget :tag new-tag)
(widget-put node :tag new-tag)
;; Redraw.
(widget-value-set node (widget-value node)))))
((or
;; Account removed
(and account-widget (null new-state))
;; New account
(and new-state (null account-widget)))
;; Just redraw the tree. TODO: it would be nice to
;; preserve the "open" state of tree nodes.
(widget-value-set bic-mailbox-tree--widget (widget-value bic-mailbox-tree--widget)))))))))
(defun bic-mailbox-tree--mailboxes (parent)
"Return widgets for all mailboxes in an account.
PARENT is the tree widget node for the account."
(let* ((account-name (widget-get parent :address))
(mailbox-table (gethash account-name bic-account-mailbox-table))
mailboxes)
(when mailbox-table
(maphash
(lambda (mailbox data)
(push (cons mailbox data) mailboxes))
mailbox-table))
(setq mailboxes (cl-sort mailboxes #'string-lessp :key #'car))
(mapcar
(lambda (mailbox-data)
(let ((mailbox-name (car mailbox-data))
(attributes (plist-get (cdr mailbox-data) :attributes))
(sync-level (bic--infer-sync-level mailbox-data))
(unseen (plist-get (cdr mailbox-data) :unseen)))
;; It's unclear whether these attributes are case sensitive
;; or not, so let's use cl-equalp.
(if (or (cl-member "\\Noselect" attributes :test #'cl-equalp)
(cl-member "\\NonExistent" attributes :test #'cl-equalp))
(widget-convert 'item mailbox-name)
(widget-convert
'link
:account-name account-name
:mailbox-name (car mailbox-data)
:notify (lambda (widget &rest _ignore)
(bic-mailbox-open (widget-get widget :account-name)
(widget-get widget :mailbox-name)))
:tag (if unseen (bic-number-to-string unseen) "?")
:format (concat
"%[%v%]"
(when sync-level " (%t)")
(cond
((plist-get (cdr mailbox-data) :not-all-unread)
(propertize " [not all unread fetched]"
'face 'error))
((plist-get (cdr mailbox-data) :not-all-recent)
(propertize " [not all recent fetched]"
'face 'warning)))
"\n")
:button-face (pcase (cons sync-level (and (numberp unseen) (not (zerop unseen))))
(`(:unlimited-sync . t)
'bic-mailbox-tree-mailbox-unlimited-sync-unread)
(`(:unlimited-sync . nil)
'bic-mailbox-tree-mailbox-unlimited-sync)
(`(:partial-sync . t)
'bic-mailbox-tree-mailbox-partial-sync-unread)
(`(:partial-sync . nil)
'bic-mailbox-tree-mailbox-partial-sync)
(_
'bic-mailbox-tree-mailbox-unsubscribed))
(utf7-decode mailbox-name t)))))
mailboxes)))
(defun bic-mailbox-tree--update-mailbox-state (account _mailbox _state)
;; checkdoc-params: (account)
"Update the state of a mailbox in the mailbox tree buffer.
This function is meant to be called from the hook
`bic-account-mailbox-update-functions'."
(let ((buffer (get-buffer "*Mailboxes*")))
(when buffer
(with-current-buffer buffer
;; TODO: check mailbox non-nil
(let ((account-widget
(cl-find-if
(lambda (child)
(and (tree-widget-p child)
(equal (widget-get child :address) account)))
(widget-get bic-mailbox-tree--widget :children))))
(when account-widget
(widget-value-set account-widget (widget-value account-widget))))))))
(provide 'bic-mailbox-tree)
;;; bic-mailbox-tree.el ends here