-
Notifications
You must be signed in to change notification settings - Fork 1
/
eosd-cache.el
135 lines (110 loc) · 4.5 KB
/
eosd-cache.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
;;; eosd-cache.el --- Caching layer for EOSD
;;
;;; Commentary:
;;
;; Copyright (C) 2016 Lincoln Clarete <[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/>.
;;
;;; Code:
(eval-when-compile (require 'cl))
(defvar eosd-notification-list nil
"The list of notifications to display.")
(defvar eosd-notification-filter-time nil
"Expression applied to filter notifications by their timestamp.")
(defvar eosd-notification-filter-app nil
"Expression applied to filter notifications by name of application that created them.")
(defvar eosd-notification-filter-text nil
"Expression applied to filter notifications by their text.")
(defvar eosd-notification-filter nil
"Expression applied to filter which notifications to show.
This is the variable that is actually used to filter
notifications. The current UI just concatenates the following
variables to form the value for this variable:
* `eosd-notification-filter-time'
* `eosd-notification-filter-text'
That's useful for composing UIs that want to give different
options for each filter type. Users that dislike the current UI
can just come up with their own input system and either use the
separate variables and assemble them together into this one or
ignore the specific vars and just use this one. Which is what
the filter system actually uses.")
(defvar eosd-notification-fields '(id timestamp app-name
replaces-id app-icon summary body actions hints expire-timeout)
"Field names of a notification entry.")
(defun eosd-cache-entry-id ()
"Generate a unique id for entries."
(random (expt 16 4)))
(defun eosd-cache-apply-filter (notification filter)
"Test if NOTIFICATION match with FILTER."
(let* ((operator (car filter))
(field (cadr filter))
(needed (caddr filter))
(content (cdr (assoc field notification))))
(if (apply (subst content field filter))
notification
nil)))
(defun eosd-cache-do-filters (filters notification)
"Apply all FILTERS in NOTIFICATION."
(if (null filters)
notification
(if (eosd-cache-apply-filter notification (car filters))
(eosd-cache-do-filters (cdr filters) notification))))
(defun eosd-cache-apply-filters (notification)
"Apply all filters in NOTIFICATION."
(eosd-cache-do-filters
(delq nil eosd-notification-filter) notification))
(defun eosd-cache-filter (notifications)
"Filter NOTIFICATIONS list."
(delq nil
(mapcar 'eosd-cache-apply-filters notifications)))
(defun eosd-cache-list ()
"Notifications filtered with expression in `eosd-notification-filter'."
(eosd-cache-filter eosd-notification-list))
(defun eosd-cache-new-notification (fields)
"Transform FIELDS into a notification and save it into cache.
The return value will be the newly created notification."
(let* ((entry-id (eosd-cache-entry-id))
(time (float-time))
(notification (cl-pairlis
eosd-notification-fields
(cons entry-id (cons time fields)))))
(push notification eosd-notification-list)
notification))
(defun eosd-cache-delete-notification (notification-id)
"Delete NOTIFICATION-ID from the cache."
(setq eosd-notification-list
(delq (assoc (cons 'id notification-id)
eosd-notification-list)
eosd-notification-list)))
;; Filters
(defun --eosd--seconds-ago (s)
"Time S seconds ago."
`(>= timestamp ,(- (float-time) s)))
(defun eosd-cache-filter-by-time (time)
"Only show messages at most TIME old."
(setq eosd-notification-filter-time
(--eosd--seconds-ago
(pcase time
('1m 60)
('5m (* 5 60))
('1h (* 60 60))
('24h (* 60 60 24))
('1w (* 60 60 24 7))
('1M (* 60 60 24 7 4))))))
(defun eosd-cache-filter-by-app (appname)
"Only show messages from app APPNAME."
(setq eosd-notification-filter-app `(equal app-name ,appname)))
(provide 'eosd-cache)
;;; eosd-cache.el ends here