forked from stophlong/org-toodledo
-
Notifications
You must be signed in to change notification settings - Fork 5
/
org-toodledo.el
748 lines (693 loc) · 31.4 KB
/
org-toodledo.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
;;; org-toodledo.el - Toodledo integration for Emacs Org mode
;; (c) 2010 Sacha Chua ([email protected])
;;
;; This file is not part of GNU Emacs.
;; This 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 2, or (at your option) any later
;; version.
;;
;; This 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
;; MA 02111-1307, USA.
;; How to use:
;; 1. Customize org-toodledo-userid and org-toodledo-password
;; 2. Open a blank org file.
;; 3. Call org-toodledo-initialize-org
;; Call org-toodledo-update to bring in new/updated tasks (skips locally modified tasks newer than updated)
;; Call org-toodledo-sync-task to create or update the current task
;; Call org-toodledo-delete-current-task to delete the current task
;; Call org-toodledo-touch to mark the task as modified at the time it is touched
;;
;; Doesn't do lots of error trapping. Might be a good idea to version-control your Org file.
;;
;; TOODLEDO ATTRIBUTES and how they are bi-directionally handled
;; Context: Handled by tags (ex: :@work: :@errands:)
;; - will create new contexts if necessary
;; Task status: Mapped to TODO state.
;; See org-toodledo-status-to-string and org-toodledo-parse-current-task for the mapping
;; You will probably want something like this in your ~/.emacs:
;; (setq org-todo-keywords
;; '((sequence
;; "TODO(t)" ; next action
;; "PLAN(-)"
;; "STARTED(s)"
;; "WAITING(w@/!)"
;; "POSTPONED(p)" "SOMEDAY(s@/!)" "|" "DONE(x!)" "CANCELLED(c@)")
;; (type "DELEGATED(d@!)" "DONE(x)")))
;; Length: Mapped to effort
;; Priority: Mapped to [#A], [#B], or [#C]. (TODO: Change this to five levels of priority to match Toodledo)
;; Start date: Mapped to "SCHEDULED"
;; Due date: Mapped to "DEADLINE"
;; Tags: Mapped to tags
;; Note: Mapped to todo text. May get confused by asterisks, so don't use any starting asterisks in your body text.
;; (or anything that looks like an Org headline).
;; Completed: Mapped to DONE todo state.
;;
;; TODO:
;; - [ ] Double-check new/changed/deleted task updating, still seems buggy
;; - [ ] Test, test, test - maybe make test harness?
;; - [ ] Move status<->string mapping to a variable - lookups are better than logic
;; - [ ] Make sure sync timestamps aren't getting updated more often than needed
;; - [ ] Suggest some kind of hook to make it easier to mark a task as locally modified
;; ** TODO Problems: highest priority at top
;; - [ ] TAGS get dropped from emacs side after a few exchanges (but, "contexts" stay) (stophlong)
;; - [ ] Start date in toodledo does NOT get mapped to scheduled, but scheduled in emacs gets mapped to start date (stophlong)
;; - [ ] Due date in toodledo does get mapped to deadline minus 1, and deadline in emacs gets mapped to due date plus 1 (at least when I did it at late in the day.) (stophlong)
;; - [ ] If a task is marked DONE in emacs, sync'd, then marked TODO, sync'd, it is still marked "completed" in properties in emacs. (stophlong)
;;
;; ** TODO Feature Requests: highest priority at top
;; - [ ] It'd be great to allow notes to contain asterisks. Make "[CR]** " the special key? I use multiple asterisks all the time in notes. (stophlong)
;; - [ ] org-toodledo-push to push ALL tasks that have been modified more recently in emacs than toodledo
;; - [ ] Is it possible to hide :PROPERTIES: drawer on emacs side? If there is a way to hide properties that'd be great. (stophlong)
;; - [ ] I'd suggest renaming "update" to "pull"; rename "sync-task" to "push-task" to be consistent with org-mobile-pull and org-mobile-push. (stophlong)
;; - [ ] access to toodledo via proxy would also be good for those inside proxy based firewalls. (stophlong)
;; - [ ] How to deal with sub-tasks? the paid version of toodledo (which I don't have) has sub-tasks. At some point, might try to deal with those. (stophlong)
;; RECENTLY DONE:
;; -- Made w3mexcerpt.el file. I excerpted the needed things from w3m (since w3m requires things which require things which require things which require an executable which is not longer readily available.). (Tue, May 18 2010, 19:19:46)
;; -- Altered priority mapping to better sync org-mode's ABC and Toodledo's 3,2,1,0,-1
;;
;; Toodledo --> org-mode priorities
;; -1 --> C
;; 0 --> C (0=low is default in toodledo)
;; 1 --> no listed priority (close to org-mode's "nothing" which is equivalent to B in sorting)
;; 2 --> B
;; 3 --> A
;;
;; org-mode --> toodledo priorities.
;; C --> 0
;; nothing --> 1
;; B --> 2
;; A --> 3
;;
;; -- Defined what does org-toodledo-touch does in the documentation (stophlong)
(require 'org)
(unless (require 'w3m nil t)
(require 'w3mexcerpt))
(require 'xml)
(defcustom org-toodledo-userid ""
"UserID from Toodledo (not your e-mail address): http://www.toodledo.com/info/api_doc.php"
:group 'org-toodledo
:type 'string)
(defcustom org-toodledo-password ""
"Password for Toodledo."
:group 'org-toodledo
:type 'string)
(defvar org-toodledo-token-expiry nil "Expiry time for authentication token.")
(defvar org-toodledo-token nil "Authentication token.")
(defvar org-toodledo-key nil "Authentication key.")
(require 'url)
(require 'url-http)
(defun org-toodledo-initialize-org ()
"Replace buffer contents with Toodledo tasks."
(interactive)
(delete-region (point-min) (point-max))
(let ((account-info (org-toodledo-get-account-info))
(server-info (org-toodledo-get-server-info))
(tasks (org-toodledo-get-tasks '(("notcomp" . "1")))))
(insert "* Toodledo\n"
":PROPERTIES:\n"
":Last-modified: " (or (cdr (assoc "lastaddedit" account-info)) "0") "\n"
":Last-deleted: " (or (cdr (assoc "lastdelete" account-info)) "0") "\n"
":Last-sync: " (cdr (assoc "unixtime" server-info)) "\n"
":END:\n")
(insert (mapconcat 'org-toodledo-task-to-string tasks "\n"))))
(defun org-toodledo-get-token ()
"Retrieve authentication token valid for four hours."
(if (and org-toodledo-token
org-toodledo-token-expiry
(time-less-p (current-time) org-toodledo-token-expiry))
org-toodledo-token
;; Else retrieve a new token
(let ((response
(with-current-buffer
(url-retrieve-synchronously
(concat "http://api.toodledo.com/api.php?method=getToken;userid="
org-toodledo-userid))
(xml-parse-region (point-min) (point-max)))))
(if (equal (car (car response)) 'error)
(progn
(setq org-toodledo-token nil
org-toodledo-key nil
org-toodledo-token-expiry nil)
(error "Could not log in to Toodledo: %s" (elt (car response) 2)))
(setq org-toodledo-token
(elt (car response) 2))
(setq org-toodledo-key (org-toodledo-key)
;; Set the expiry time
org-toodledo-token-expiry
(seconds-to-time
(+ (time-to-seconds (current-time))
(* 60 60 4))))) ;; four hours
org-toodledo-token)))
(defun org-toodledo-key ()
"Return authentication key used for each request."
(if (and org-toodledo-token
org-toodledo-token-expiry
(time-less-p (current-time) org-toodledo-token-expiry)
org-toodledo-key)
org-toodledo-key
(setq org-toodledo-key
(md5 (concat (md5 org-toodledo-password)
org-toodledo-token
org-toodledo-userid)))))
(defun org-toodledo-get-url (method-name &optional params)
"Return URL for METHOD-NAME and PARAMS."
(org-toodledo-get-token)
(concat "http://api.toodledo.com/api.php?method="
(w3m-url-encode-string method-name)
";appid=orgtoodledosachac;key=" (org-toodledo-key)
(if params
(concat
";"
(mapconcat (lambda (x)
(concat
(w3m-url-encode-string (car x)) "="
(w3m-url-encode-string (cdr x))))
params
";"))
"")))
(defun org-toodledo-call-method (method-name &optional params)
"Call METHOD-NAME with PARAMS and return the parsed XML."
(setq params (cons (cons "unix" "1") params))
(with-current-buffer
(url-retrieve-synchronously
(org-toodledo-get-url method-name params))
(xml-parse-region (point-min) (point-max))))
(defmacro org-toodledo-defun (function-name api-name description)
`(defun ,function-name (params)
,description
(org-toodledo-call-method ,api-name params)))
(defmacro org-toodledo-make-lookup-function (function-name add-method get-method root-element get-function cache-variable)
"Create a lookup function and caching functions for FUNCTION-NAME."
(list
'progn
`(defvar ,(intern cache-variable) nil)
`(defun ,(intern get-function) (&optional force)
,(concat "Store an alist of (title . id) in `" cache-variable "'.
Reload if FORCE is non-nil.")
(if (or force (null ,(intern cache-variable)))
(setq ,(intern cache-variable)
(mapcar
(lambda (node)
(cons
(car (xml-node-children node))
(xml-get-attribute node 'id)))
(xml-get-children (car
(org-toodledo-call-method ,get-method)) ,root-element)))
,(intern cache-variable)))
`(defun ,(intern (concat "org-toodledo-" function-name "-to-id")) (item)
"Return numeric ID for CONTEXT, creating if necessary."
(let ((lookups ,(list (intern get-function))))
(if (null (assoc item lookups))
;; Create it if it does not yet exist
(let ((result
(org-toodledo-call-method
,add-method
(list (cons "title" item)))))
(if (eq (caar result) 'added)
(setq ,(intern cache-variable)
(cons (cons item
(elt (car result) 2))
,(intern cache-variable))
lookups ,(intern cache-variable)))))
(cdr (assoc item lookups))))))
(org-toodledo-make-lookup-function "context" "addContext" "getContexts" 'context "org-toodledo-get-contexts" "org-toodledo-contexts")
(org-toodledo-make-lookup-function "folder" "addFolder" "getFolders" 'folder "org-toodledo-get-folders" "org-toodledo-folders")
(org-toodledo-make-lookup-function "goal" "addGoal" "getGoals" 'goal "org-toodledo-get-goals" "org-toodledo-goals")
(defun org-toodledo-get-server-info ()
"Return server information."
(org-toodledo-convert-xml-result-to-alist
(car (org-toodledo-call-method "getServerInfo"))))
(defun org-toodledo-get-account-info ()
"Return server information."
(org-toodledo-convert-xml-result-to-alist
(car (org-toodledo-call-method "getAccountInfo"))))
(org-toodledo-defun org-toodledo-add-task "addTask" "Add task with PARAMS.")
(org-toodledo-defun org-toodledo-edit-task "editTask" "Edit task with PARAMS.")
(org-toodledo-defun org-toodledo-delete-task "deleteTask" "Delete task with PARAMS.")
;; (setq temp (org-toodledo-get-tasks '(("notcomp" . "1"))))
;; (setq server-info (org-toodledo-get-server-info))
;; (setq account-info (org-toodledo-get-account-info))
(defun org-toodledo-convert-xml-result-to-alist (info)
"Convert INFO to an alist."
(delq nil
(mapcar
(lambda (item)
(if (listp item)
(cons (symbol-name (car item)) (elt item 2))))
(xml-node-children (delete "\n\t" info)))))
(defun org-toodledo-get-tasks (&optional params)
"Retrieve tasks using PARAMS.
Return a list of task alists."
(mapcar
'org-toodledo-convert-xml-result-to-alist
(xml-get-children
(car (org-toodledo-call-method "getTasks" params))
'task)))
(defun org-toodledo-get-deleted (&optional params)
"Retrieve deleted tasks using PARAMS.
Return a list of task alists."
(mapcar
'org-toodledo-convert-xml-result-to-alist
(xml-get-children
(car (org-toodledo-call-method "getDeleted" params))
'task)))
(defun org-toodledo-entry-note ()
"Extract the note for this entry."
(save-excursion
(org-back-to-heading)
(when (looking-at org-complex-heading-regexp)
(goto-char (match-end 0))
(let ((text (buffer-substring-no-properties
(point)
(if (re-search-forward org-complex-heading-regexp nil t)
(match-beginning 0)
(org-end-of-subtree)))))
(with-temp-buffer
(insert text)
(goto-char (point-min))
(when (re-search-forward
(concat "\\<"
(regexp-quote org-deadline-string) " +<[^>\n]+>[ \t]*") nil t)
(replace-match ""))
(goto-char (point-min))
(when (re-search-forward
(concat "\\<"
(regexp-quote org-scheduled-string) " +<[^>\n]+>[ \t]*") nil t)
(replace-match ""))
(goto-char (point-min))
(while (re-search-forward "\n\n+" nil t)
(replace-match "\n"))
(org-export-remove-or-extract-drawers org-drawers nil nil)
(buffer-substring-no-properties (point-min)
(point-max)))))))
(defun org-toodledo-parse-current-task ()
"Extract the status and Toodledo ID of the current task."
(save-excursion
(org-back-to-heading t)
(when (and (looking-at org-complex-heading-regexp)
(match-string 2)) ;; TODO
(let* (info
(status (match-string-no-properties 2))
(priority (match-string-no-properties 3))
(title (match-string-no-properties 4))
(tags (match-string-no-properties 5))
(id (org-entry-get (point) "Toodledo-ID"))
(contexts (org-toodledo-get-contexts))
context)
;; (add-to-list 'info (cons "title" (match-string-no-properties 1)))
(if id (add-to-list 'info (cons "id" id)))
(when tags
(setq tags
(delq nil
(mapcar
(lambda (tag)
(if (> (length tag) 0)
(if (string-match (org-re "@\\([[:alnum:]_]+\\)") tag)
(setq context (org-toodledo-context-to-id (match-string 1 tag)))
tag)))
(split-string tags ":")))))
(setq info
(list
(cons "id" id)
(cons "title" title)
(cons "length" (org-entry-get (point) "Effort"))
(cons "context" context)
(cons "tag" (mapconcat 'identity tags " "))
(cons "completed" (if (equal status "DONE") "1" "0"))
(cons "status"
(cond
((equal status "STARTED") "2")
((equal status "DELEGATED") "4")
((equal status "SOMEDAY") "8")
((equal status "CANCELLED") "9")
((equal status "PLAN") "3")
((equal status "WAITING") "5")
((equal status "TODO") "1")))
(cons "priority"
(cond
((equal priority "[#A]") "2")
((equal priority "[#B]") "1")
((equal priority "[#C]") "0")
(t "1"))) ;; Force org-mode's no priority to be same as [#B] as is done in org-mode.
(cons "note"
(org-toodledo-entry-note))))
(when (org-entry-get nil "FOLDER")
(setq info (cons (cons "folder" (org-toodledo-folder-to-id (org-entry-get nil "FOLDER"))) info)))
(when (org-entry-get nil "GOAL")
(setq info (cons (cons "goal" (org-toodledo-goal-to-id (org-entry-get nil "GOAL"))) info)))
(when (org-entry-get nil "DEADLINE")
(setq info (cons (cons "duedate"
(substring (org-entry-get nil "DEADLINE")
0 10)) info)))
(when (org-entry-get nil "SCHEDULED")
(setq info (cons (cons "startdate"
(substring (org-entry-get nil "SCHEDULED")
0 10)) info)))
info))))
(defun org-toodledo-sync ()
"Synchronize all tasks."
;; Retrieve all tasks
;; For each task in the current buffer
;; Synchronize an existing task that has changed
(let ((regexp (concat "^\\*+[ \t]+\\(" org-todo-regexp "\\)")))
(goto-char (point-min))
(while (re-search-forward regexp nil t)
(org-toodledo-sync-task))))
(defun org-toodledo-update ()
"Insert new tasks and update previous tasks."
(interactive)
(let* ((server-info (org-toodledo-get-server-info))
(account-info (org-toodledo-get-account-info))
(changed (org-toodledo-account-changed account-info))
(last-deleted (string-to-number (or (org-entry-get-with-inheritance "Last-deleted") "0")))
(last-modified (string-to-number (or (org-entry-get-with-inheritance "Last-modified") "0")))
(last-update (string-to-number (or (org-entry-get-with-inheritance "Last-sync") "0")))
processed)
;; If tasks have been deleted or modified, then the Toodledo API
;; will give us the timestamps. We need to find out which tasks
;; have been deleted or modified since the last time we retrieved
;; the list of tasks that have been deleted or modified. We store
;; the last times in the properties of the root element.
(if (and (assoc "deleted" changed) ;; Tasks have been deleted
(>= (string-to-number (cdr (assoc "deleted" changed))) last-deleted))
(setq processed
(append (org-toodledo-process-deleted-tasks
last-deleted)
processed)))
(if (and (assoc "modified" changed) ;; Tasks have been added or edited
(>= (string-to-number (cdr (assoc "modified" changed)))
last-modified))
;; Retrieve added/modified tasks
(setq processed (append
(org-toodledo-process-modified-tasks last-modified) processed)))
;; TODO Look for tasks that were modified locally since the last synchronization
(org-toodledo-process-locally-modified-tasks last-update processed)
;; TODO Update timestamps here
(goto-char (point-min))
(when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
(org-entry-put (point)
"Last-sync"
(cdr (assoc "unixtime" server-info)))
(when (assoc "lastaddedit" account-info)
(org-entry-put (point)
"Last-modified"
(cdr
(assoc "lastaddedit" account-info))))
(when (assoc "lastdelete" account-info)
(org-entry-put (point)
"Last-deleted"
(cdr
(assoc "lastdelete" account-info)))))))
(defun org-toodledo-process-locally-modified-tasks (last-update processed)
"Synchronize tasks that were locally modified after LAST-UPDATE.
Skip tasks with IDs in PROCESSED."
(goto-char (point-min))
(let ((start (float-time (current-time))))
(while (re-search-forward org-complex-heading-regexp nil t)
;; Look for all tasks in this buffer
(if (match-string 2)
;; Is it a new task, or has it been modified since the last update?
(let ((id (org-entry-get (point) "Toodledo-ID"))
(modified (string-to-number (or (org-entry-get (point) "Modified") "")))
(last-sync (if (org-entry-get (point) "Sync")
(string-to-number (org-entry-get (point) "Sync"))
0)))
(if (or (null id)
(and (> modified last-sync)
(< modified start)
(not (member id processed))))
(save-excursion (org-toodledo-sync-task))))))))
(defun org-toodledo-touch ()
"Update the current task's timestamp so that it's included in the next sync."
(interactive)
(org-entry-put (point) "Modified" (format "%d" (float-time (current-time)))))
(defvar org-toodledo-actually-delete t)
(defun org-toodledo-process-deleted-tasks (timestamp)
"Remove tasks deleted after TIMESTAMP."
(delq nil
(mapcar
(lambda (task)
(when (org-toodledo-find-task task)
(if org-toodledo-actually-delete
(delete-region (org-back-to-heading)
(if (re-search-forward org-complex-heading-regexp nil t)
(match-beginning 0)
(org-end-of-subtree)))
(org-entry-delete (point) "Toodledo-ID")
(org-entry-put (point) "Toodledo-Deleted" (timestamp)))
(org-toodledo-task-id task)))
(org-toodledo-get-deleted
(list (cons "after" (number-to-string timestamp)))))))
(defun org-toodledo-process-modified-tasks (modified)
"Handle all the tasks that have been modified since MODIFIED."
(delq nil
(mapcar
(lambda (task)
(if (org-toodledo-find-task task)
(if (null (org-toodledo-update-task task modified))
(org-toodledo-task-id task))
(org-toodledo-create-task task)))
(org-toodledo-get-tasks (list (cons "modafter" (number-to-string modified)))))))
(defun org-toodledo-create-task (task)
"Create a task for TASK."
(goto-char (point-max))
(if (point-at-eol) (insert "\n"))
(insert (org-toodledo-task-to-string task))
(org-toodledo-task-id task))
(defun org-toodledo-find-task (task)
"Find the task specified by TASK."
(goto-char (point-min))
(re-search-forward
(concat "^[ \t]*:Toodledo-ID:[ \t]+" (org-toodledo-task-id task) "$")
nil t))
(defun org-toodledo-account-changed (account-info)
"Return non-nil if the account has changed since the last check.
The result will be an alist of (\"modified\" . \"timestamp\") if tasks have
been added/edited and (\"deleted\" . \"timestamp\") if tasks have been deleted."
(let ((last-modified (org-entry-get-with-inheritance "Last-modified"))
(last-deleted (org-entry-get-with-inheritance "Last-deleted"))
result)
(if (> (string-to-number (or (cdr (assoc "lastaddedit" account-info)) "0"))
(string-to-number (or last-modified "0")))
(add-to-list 'result (cons "modified" last-modified)))
(if (> (string-to-number (or (cdr (assoc "lastdelete" account-info)) ""))
(string-to-number (or last-deleted "0")))
(add-to-list 'result (cons "deleted" last-deleted)))
result))
(defun org-toodledo-sync-task (&optional force)
"Update my Toodledo for the current task."
(interactive "P")
(save-excursion
(let ((task (org-toodledo-parse-current-task)))
(if (null (org-toodledo-task-id task))
;; New task, create it
(let ((result (org-toodledo-add-task task)))
(when (eq (elt (car result) 0) 'added)
(org-entry-put (point) "Toodledo-ID" (elt (car result) 2))
(org-entry-put (point) "Sync"
(format "%d" (float-time (current-time)) 1000))))
;; Old task, update
(when (org-toodledo-success-p (org-toodledo-edit-task task))
(if (equal (org-toodledo-task-completed task) "1")
(org-entry-put (point) "Completed" "1")
(org-entry-delete (point) "Completed")
(org-entry-put (point) "Status" (org-toodledo-task-status task)))
(org-entry-put (point) "Sync"
(format "%d" (float-time (current-time)) 1000)))))))
;; (assert (equal (org-toodledo-format-date "2003-08-12") "<2003-08-12 Tue>"))
(defun org-toodledo-format-date (date &optional repeat)
"Return yyyy-mm-dd day for DATE."
(concat
"<"
(format-time-string
"%Y-%m-%d %a"
(cond
((listp date) date)
((numberp date) (seconds-to-time date))
((and (stringp date)
(string-match "^[0-9]+$" date))
(seconds-to-time (string-to-number date)))
(t (apply 'encode-time (org-parse-time-string date)))))
(if repeat (concat " " repeat) "")
">"))
;; (mapconcat 'org-toodledo-task-to-string temp "\n")
;; (setq task (elt temp 2))
;; (org-toodledo-task-to-string task)
(defun org-toodledo-task-to-string (task &optional level)
"Return an Org-formatted version of TASK."
(let* ((repeat (string-to-number (or (org-toodledo-task-repeat task) "0")))
(rep-advanced (org-toodledo-task-repeat-advanced task))
(repeat-string (org-toodledo-repeat-to-string repeat rep-advanced))
(priority (org-toodledo-task-priority task)))
(concat
(make-string (or level 2) ?*) " "
(org-toodledo-status-to-string task) " "
(cond
((equal priority "-1") "[#C] ")
((equal priority "0") "[#C] ")
((equal priority "1") "")
((equal priority "2") "[#B] ")
((equal priority "3") "[#A] "))
(org-toodledo-task-title task)
(if (org-toodledo-task-context task)
(concat " :@" (org-toodledo-task-context task) ":")
"")
"\n"
(if (and (org-toodledo-task-duedate task)
(not (equal (org-toodledo-task-duedate task) ""))
(not (< (string-to-number (org-toodledo-task-duedate task)) 0)))
(concat org-deadline-string " "
(org-toodledo-format-date
(org-toodledo-task-duedate task)
repeat-string)
"\n")
"")
(if (and (org-toodledo-task-startdate task)
(not (equal (org-toodledo-task-startdate task) ""))
(not (< (string-to-number (org-toodledo-task-startdate task)) 0)))
(concat org-scheduled-string " "
(org-toodledo-format-date
(org-toodledo-task-startdate task)
repeat-string)
"\n")
"")
(or (org-toodledo-task-note task) "") "\n"
":PROPERTIES:\n"
":Toodledo-ID: " (org-toodledo-task-id task) "\n"
":Modified: " (org-toodledo-task-modified task) "\n"
(if (not (equal (org-toodledo-task-folder task) "0"))
(concat ":Folder: " (car (rassoc (org-toodledo-task-folder task) org-toodledo-folders)) "\n")
"")
(if (not (equal (org-toodledo-task-goal task) "0"))
(concat ":Goal: " (car (rassoc (org-toodledo-task-goal task) org-toodledo-folders)) "\n")
"")
":Sync: " (format "%d" (float-time (current-time))) "\n"
":Effort: " (org-toodledo-task-length task) "\n"
":END:\n"
)))
;; (assert (equal (org-toodledo-repeat-to-string 0) ""))
;; (assert (equal (org-toodledo-repeat-to-string 1) "+1w"))
;; (assert (equal (org-toodledo-repeat-to-string 2) "+1m"))
;; (assert (equal (org-toodledo-repeat-to-string 3) "+1y"))
;; (assert (equal (org-toodledo-repeat-to-string 4) "+1d"))
;; (assert (equal (org-toodledo-repeat-to-string 5) "+2w"))
;; (assert (equal (org-toodledo-repeat-to-string 6) "+2m"))
;; (assert (equal (org-toodledo-repeat-to-string 7) "+6m"))
;; (assert (equal (org-toodledo-repeat-to-string 8) "+3m"))
;; (assert (equal (org-toodledo-repeat-to-string 108) ".+3m"))
;; (assert (equal (org-toodledo-repeat-to-string 101) ".+1w"))
;; (assert (equal (org-toodledo-repeat-to-string 0) ""))
(defconst org-toodledo-repeat-intervals '("" "+1w" "+1m" "+1y" "+1d" "+2w" "+2m" "+6m" "+3m"))
(defun org-toodledo-status-to-string (task)
(let ((comp (org-toodledo-task-completed task))
(status (string-to-number (org-toodledo-task-status task))))
(cond
((not (or (null comp) (equal comp "") (equal comp "0"))) "DONE")
((= status 0) "TODO")
((= status 1) "TODO")
((= status 2) "STARTED")
((= status 3) "PLAN")
((= status 4) "DELEGATED")
((= status 5) "WAITING")
((= status 6) "PLAN") ; hold
((= status 7) "SOMEDAY") ; postponed
((= status 8) "SOMEDAY")
((= status 9) "CANCELLED")
)))
(defun org-toodledo-repeat-to-string (repeat &optional rep-advanced)
"Turn TASK into a repeat sequence."
(cond
((= repeat 0) nil)
((> repeat 100) (concat "+" (org-toodledo-repeat-to-string (mod repeat 100) rep-advanced)))
((and (= repeat 50) rep-advanced)
(cond
((string-match "Every \\([0-9]+\\) week" rep-advanced)
(concat "+" (match-string 1 rep-advanced) "w"))
((string-match "Every \\([0-9]+\\) month" rep-advanced)
(concat "+" (match-string 1 rep-advanced) "m"))
((string-match "Every \\([0-9]+\\) year" rep-advanced)
(concat "+" (match-string 1 rep-advanced) "y"))
((string-match "Every \\([0-9]+\\) day" rep-advanced)
(concat "+" (match-string 1 rep-advanced) "d"))
(t rep-advanced)))
(t (elt org-toodledo-repeat-intervals repeat))))
(defun org-toodledo-delete-current-task ()
"Delete the current task."
(interactive)
(org-back-to-heading t)
(let ((task (org-toodledo-parse-current-task)))
(and (> (length (org-toodledo-task-id task)) 0)
(org-toodledo-success-p (org-toodledo-delete-task task)))
(delete-region
(point)
(if (and (end-of-line)
(re-search-forward org-complex-heading-regexp nil t))
(match-beginning 0)
(org-end-of-subtree t t)
(point)))))
(defun org-toodledo-task-get-prop (task prop) (cdr (assoc prop task)))
(defmacro org-toodledo-task-prop-defun (field)
`(defun ,(intern (concat "org-toodledo-task-" field)) (task)
(cdr (assoc ,field task))))
(defun org-toodledo-success-p (result)
"Return non-nil if RESULT indicates success."
(eq (car (car result)) 'success))
(org-toodledo-task-prop-defun "id")
(org-toodledo-task-prop-defun "title")
(org-toodledo-task-prop-defun "status")
(org-toodledo-task-prop-defun "completed")
(org-toodledo-task-prop-defun "repeat")
(org-toodledo-task-prop-defun "context")
(org-toodledo-task-prop-defun "duedate")
(org-toodledo-task-prop-defun "startdate")
(org-toodledo-task-prop-defun "modified")
(org-toodledo-task-prop-defun "folder")
(org-toodledo-task-prop-defun "goal")
(org-toodledo-task-prop-defun "priority")
(org-toodledo-task-prop-defun "note")
(org-toodledo-task-prop-defun "length")
;; defun'd separately because of the change in name
(defun org-toodledo-task-repeat-advanced (task)
(cdr (assoc "rep_advanced" task)))
(defun org-toodledo-agenda-touch ()
"Update the Modified timestamp for the current entry in the agenda."
(org-agenda-check-type t 'agenda 'timeline)
(org-agenda-check-no-diary)
(let* ((marker (or (org-get-at-bol 'org-marker)
(org-agenda-error)))
(buffer (marker-buffer marker))
(pos (marker-position marker)))
(org-with-remote-undo buffer
(with-current-buffer buffer
(widen)
(goto-char pos)
(if (org-entry-get (point) "Modified")
(org-entry-put (point) "Modified" (format "%d" (float-time (current-time)))))))))
(defun org-toodledo-update-task (task &optional last-update)
(let* ((modified (string-to-number (or (org-entry-get (point) "Modified") "")))
(last-sync (if (org-entry-get (point) "Sync")
(string-to-number (org-entry-get (point) "Sync"))
0))
(level (car (org-heading-components)))
(locally-modified (> modified last-sync)))
;; Locally modified? keep
(if locally-modified
nil
;; Not locally modified? replace
;; Figure out what our level is
(delete-region (org-back-to-heading)
(progn (goto-char (match-end 0))
(if (re-search-forward org-complex-heading-regexp nil t)
(goto-char (match-beginning 0))
(org-end-of-subtree))))
(insert (org-toodledo-task-to-string task level))
t)))
(provide 'org-toodledo)