forked from sachac/org-toodledo
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathorg-toodledo-sim.el
337 lines (304 loc) · 11.1 KB
/
org-toodledo-sim.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
(defvar org-toodledo-sim-id 10000)
(defvar org-toodledo-sim-lastedit-task 0)
(defvar org-toodledo-sim-lastdelete-task 0)
(defvar org-toodledo-sim-curtime 0)
(defvar org-toodledo-sim-pro "1")
(defvar org-toodledo-sim-db-tasks nil)
(defvar org-toodledo-sim-db-deleted nil)
(defvar org-toodledo-sim-invalid-key nil)
(defvar org-toodledo-sim-db-folders nil)
(defun org-toodledo-sim-init (pro)
(setq org-toodledo-sim-curtime 1
org-toodledo-sim-lastedit-task 0
org-toodledo-sim-lastdelete-task 0
org-toodledo-sim-db-tasks nil
org-toodledo-sim-db-deleted nil
org-toodledo-sim-pro pro
org-toodledo-sim-mode t))
(defun org-toodledo-sim-http-post (method &optional params)
(org-toodledo-debug "Simulated http-post for '%s' with '%S'" method params)
(with-temp-buffer
(org-toodledo-sim-xml-init)
(cond
(org-toodledo-sim-invalid-key (org-toodledo-sim-invalid-key))
((string= method "account/token") (org-toodledo-sim-account-token params))
((string= method "account/get") (org-toodledo-sim-account-get params))
((string= method "tasks/get") (org-toodledo-sim-tasks-get params))
((string= method "tasks/add") (org-toodledo-sim-tasks-add params))
((string= method "tasks/edit") (org-toodledo-sim-tasks-edit params))
((string= method "tasks/delete") (org-toodledo-sim-tasks-delete params))
((string= method "tasks/deleted") (org-toodledo-sim-tasks-deleted params))
((string= method "folders/get") (org-toodledo-sim-folders-get params))
((string= method "folders/add") (org-toodledo-sim-folders-add params))
(t (error "No sim support for method: %S" method)))
(list (buffer-substring (point-min) (point-max))
(format "HTTP/1.1 200 OK
Date: Sat, 10 Mar 2012 01:21:28 GMT
Server: Apache/2.2.3 (Red Hat)
Content-Length: %d
Keep-Alive: timeout=2, max=100
Connection: Keep-Alive
Content-Type: application/xml
" (point-max)) 200)))
;;
;; invalid key
;;
(defun org-toodledo-sim-invalid-key ()
(insert-string "<error id='2'>Invalid key</error>")
(setq org-toodledo-sim-invalid-key nil)
)
;;
;; account/token
;;
(defun org-toodledo-sim-account-token (params)
(insert-string (format "<token>%d</token>\n" (time-to-seconds)))
)
;;
;; account/get
;;
(defun org-toodledo-sim-account-get (params)
(insert-string "<account>\n")
(insert-string (format " <lastedit_task>%s</lastedit_task>\n"
org-toodledo-sim-lastedit-task))
(insert-string (format " <lastdelete_task>%s</lastdelete_task>\n"
org-toodledo-sim-lastdelete-task))
(insert-string (format " <pro>%s</pro>\n" org-toodledo-sim-pro))
(insert-string "</account>\n")
)
;;
;; tasks/get
;;
(defun org-toodledo-sim-tasks-get (params)
(let ((modafter (string-to-int (cdr (assoc "modafter" params))))
(comp (string-to-int (cdr (assoc "comp" params)))))
(org-toodledo-sim-xml-get-tasks
(delq nil
(mapcar
(lambda (task)
(if (and (> (string-to-int (org-toodledo-task-modified task))
modafter)
(or (eq comp -1)
(not (org-toodledo-task-is-completed task))))
task nil))
(mapcar 'cdr org-toodledo-sim-db-tasks))))))
(defun org-toodledo-sim-xml-get-tasks (tasks)
(let ((l (length tasks)))
(insert-string (format "<tasks num='%d' total='%d'>\n" l l)))
(mapc 'org-toodledo-sim-xml-get-task tasks)
(insert-string "</tasks>"))
(defun org-toodledo-sim-xml-get-task (task)
(insert-string " <task>\n")
(mapc (lambda (pair)
(let ((f (car pair))
(v (cdr pair)))
(insert-string (format " <%s>%s</%s>\n" f v f))))
task)
(insert-string " </task>\n")
)
;;
;; tasks/deleted
;;
(defun org-toodledo-sim-tasks-deleted (params)
(let*
((after (string-to-int (cdr (assoc "after" params))))
(del-list
(delq nil
(mapcar (lambda (elem)
(let ((id (car elem))
(deltime (cdr elem)))
(if (> deltime after) elem nil)))
org-toodledo-sim-db-deleted))))
(insert-string (format "<deleted num=\"%d\">\n" (length del-list)))
(mapc (lambda (elem)
(insert-string
(format " <task><id>%s</id><stamp>%s</stamp></task>\n"
(car elem) (cdr elem))))
del-list)
(insert-string "</deleted>\n")))
;;
;; tasks/add
;;
(defun org-toodledo-sim-tasks-add (params)
(insert-string "<tasks>\n")
(mapc
(lambda (json-task)
(setq task
(mapcar (lambda (pair)
(cons (symbol-name (car pair)) (cdr pair))) json-task))
(let ((ref (cdr (assoc "ref" task)))
(id (org-toodledo-sim-db-new-task task)))
(cond
((integerp id)
(insert-string
(format " <error id='%d'>%s</error>"
id (org-toodledo-error-num-to-str (int-to-string id)))))
(t
(insert-string " <task>\n")
(insert-string (format " <id>%s</id>\n" id))
(insert-string (format " <ref>%s</ref>\n" (or ref "")))
(insert-string " </task>\n")))))
(json-read-from-string (cdr (assoc "tasks" params))))
(insert-string "</tasks>\n"))
;;
;; tasks/edit
;;
(defun org-toodledo-sim-tasks-edit (params)
(insert-string "<tasks>\n")
(mapc
(lambda (json-task)
(setq task
(mapcar (lambda (pair)
(cons (symbol-name (car pair)) (cdr pair))) json-task))
(let ((id (org-toodledo-sim-db-edit-task task)))
(cond
((> id 0)
(insert-string
(format " <error id='%d'>%s</error>"
id (org-toodledo-error-num-to-str (int-to-string id)))))
(t
(insert-string " <task>\n")
(insert-string
(format " <id>%s</id>\n" (org-toodledo-task-id task)))
(insert-string " </task>\n")))))
(json-read-from-string (cdr (assoc "tasks" params))))
(insert-string "</tasks>\n"))
;;
;; tasks/delete
;;
(defun org-toodledo-sim-tasks-delete (params)
(insert-string "<deleted>\n")
(mapc
(lambda (id)
(if (org-toodledo-sim-db-delete-task id)
(insert-string (format " <id>%s</id>\n" id))
(insert-string " <error id='7'>Invalid ID number</error>")))
(json-read-from-string (cdr (assoc "tasks" params))))
(insert-string "</deleted>\n"))
;;
;; Managing the simulated DB of tasks and deleted tasks
;;
(defun org-toodledo-sim-db-new-task (task)
(org-toodledo-sim-advance-time)
(let ((id (org-toodledo-task-id task))
(title (org-toodledo-task-title task)))
(cond
((string-match "simerror=\\([0-9]+\\)" title)
(string-to-int (match-string 1 title)))
(t
(if (or (not id) (equal id "0"))
(setq id (int-to-string
(setq org-toodledo-sim-id (1+ org-toodledo-sim-id)))))
(alist-put task "id" id)
(alist-put task "modified" (int-to-string org-toodledo-sim-curtime))
(setq org-toodledo-sim-lastedit-task org-toodledo-sim-curtime)
(alist-put org-toodledo-sim-db-tasks id task)
id))))
(defun org-toodledo-sim-db-edit-task (task)
(org-toodledo-sim-advance-time)
(let ((id (org-toodledo-task-id task))
(title (org-toodledo-task-title task)))
(cond
((string-match "simerror=\\([0-9]+\\)" title)
(string-to-int (match-string 1 title)))
((null (assoc id org-toodledo-sim-db-tasks))
;; invalid task-id
7)
(t
(alist-put task "modified" (int-to-string org-toodledo-sim-curtime))
(setq org-toodledo-sim-lastedit-task org-toodledo-sim-curtime)
(alist-put org-toodledo-sim-db-tasks id task)
0))))
(defun org-toodledo-sim-db-delete-task (id &optional dont-save)
(org-toodledo-sim-advance-time)
(if (null (assoc id org-toodledo-sim-db-tasks))
(progn
(org-toodledo-debug
"org-toodledo-sim-db-delete-task: task not found %S" id)
nil)
(org-toodledo-debug "org-toodledo-sim-db-delete-task: deleting task %S" id)
(alist-delete org-toodledo-sim-db-tasks id)
(unless dont-save
(alist-put org-toodledo-sim-db-deleted id org-toodledo-sim-curtime)
(setq org-toodledo-sim-lastdelete-task org-toodledo-sim-curtime))
t))
;;
;; Folders
;;
(defun org-toodledo-sim-folders-get (params)
(insert-string "<folders>")
(mapc (lambda (f)
(insert-string
(format "<folder><id>%s</id><private>0</private>\
<archived>0</archived><order>%s</order><name>%s</name></folder>"
(cdr (assoc "id" f))
(cdr (assoc "order" f))
(cdr (assoc "name" f)))))
org-toodledo-sim-db-folders)
(insert-string "</folders>")
)
(defun org-toodledo-sim-folders-add (params)
(let ((name (cdr (assoc "name" params)))
(id (int-to-string
(1+ (apply 'max (mapcar
(lambda (folder)
(string-to-int (cdr (assoc "id" folder))))
org-toodledo-sim-db-folders)))))
(order (int-to-string
(1+ (apply 'max (mapcar
(lambda (folder)
(string-to-int (cdr (assoc "order" folder))))
org-toodledo-sim-db-folders)))))
)
(if (member-ignore-case name
(mapcar (lambda (folder)
(cdr (assoc "name" folder)))
org-toodledo-sim-db-folders))
(insert-string
"<error id='5'>A folder with that name that already exists</error>")
(org-toodledo-sim-db-new-folder id name order)
(insert-string "<folders>\n")
(format "<folder><id>%s</id><private>0</private>\
<archived>0</archived><order>%s</order><name>%s</name></folder>"
id name order)
(insert-string "</folders>\n")
)
)
)
(defun org-toodledo-sim-db-new-folder (id name order)
(let ((f `(("id" . ,id)
("name" . ,name)
("order" . ,order))))
(setq org-toodledo-sim-db-folders
(append org-toodledo-sim-db-folders (list f)))))
;;
;; Misc
;;
(defun org-toodledo-sim-advance-time ()
(setq org-toodledo-sim-curtime (+ org-toodledo-sim-curtime 1)))
(defun org-toodledo-sim-xml-init ()
(insert-string "<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?>\n")
)
(defun org-toodledo-sim-make-task (&rest params)
(let* ((task '(("id" . "0")
("title" . "Task title")
("modified" . "0")
("completed" . "0")
("folder" . "0")
("context" . "0")
("goal" . "0")
("priority" . "0")
("repeat" . "")
("repeatfrom" . "0")
("duedate" . "0")
("startdate" . "0")
("duetime" . "0")
("starttime" . "0")
("length" . "0")
("status" . "0")
("note" . "")
("parent" . "0")
)))
(mapcar (lambda (pair)
(alist-put task (car pair) (cdr pair))) params)
task))
(provide 'org-toodledo-sim)