dbsync.scm 31.4 KB
Newer Older
Dave Griffiths's avatar
Dave Griffiths committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
;; Starwisp Copyright (C) 2013 Dave Griffiths
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Affero 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 Affero General Public License for more details.
;;
;; You should have received a copy of the GNU Affero General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.


;; abstractions for synced databased

19
(msg "dbsync.scm")
Dave Griffiths's avatar
Dave Griffiths committed
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

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; stuff in memory

(define (store-set store key value)
  (cond
   ((null? store) (list (list key value)))
   ((eq? key (car (car store)))
    (cons (list key value) (cdr store)))
   (else
    (cons (car store) (store-set (cdr store) key value)))))

(define (store-get store key default)
  (cond
   ((null? store) default)
   ((eq? key (car (car store)))
    (cadr (car store)))
   (else
    (store-get (cdr store) key default))))

(define (store-exists? store key)
  (cond
   ((null? store) #f)
   ((eq? key (car (car store)))
    #t)
   (else
    (store-exists? (cdr store) key))))

(define store '())

(define (set-current! key value)
  (set! store (store-set store key value)))

(define (get-current key default)
  (store-get store key default))

(define (current-exists? key)
  (store-exists? store key))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; db abstraction

62
(define (entity-init! db table entity-type ktv-list)
Dave Griffiths's avatar
Dave Griffiths committed
63
  (entity-reset!)
64
65
66
67
68
  (entity-set! ktv-list)
  (set-current! 'db db)
  (set-current! 'table table)
  (set-current! 'entity-type entity-type))

Dave Griffiths's avatar
Dave Griffiths committed
69

Dave Griffiths's avatar
Dave Griffiths committed
70
;; store a ktv, replaces existing with same key
71
72
73
74
75
76
;;(define (entity-add-value! key type value)
;;  (set-current!
;;   'entity-values
;;   (ktv-set
;;    (get-current 'entity-values '())
;;    (ktv key type value))))
Dave Griffiths's avatar
Dave Griffiths committed
77

78
(define (entity-add-value-create! key type value)
79
  (msg "entity-add-value-create!" key type value)
80
81
82
83
84
85
  (set-current!
   'entity-values
   (ktv-set
    (get-current 'entity-values '())
    (ktv-create key type value))))

Dave Griffiths's avatar
Dave Griffiths committed
86
87
88
(define (entity-set! ktv-list)
  (set-current! 'entity-values ktv-list))

89
90
91
92
93
94
95
96
97
98
99
100
(define (entity-get-value key)
  (ktv-get (get-current 'entity-values '()) key))

;; version to check the entity has the key
(define (entity-set-value! key type value)
  (let ((existing-type (ktv-get-type (get-current 'entity-values '()) key)))
    (if (equal? existing-type type)
        (set-current!
         'entity-values
         (ktv-set
          (get-current 'entity-values '())
          (ktv key type value)))
101
102
103
        ;;
        (begin
          (msg "entity-set-value! - adding new " key "of type" type "to entity")
Dave Griffiths's avatar
Dave Griffiths committed
104
          (entity-add-value-create! key type value)))))
105

106

Dave Griffiths's avatar
Dave Griffiths committed
107
108
109
(define (date-time->string dt)
  (string-append
   (number->string (list-ref dt 0)) "-"
110
111
112
113
114
   (substring (number->string (+ (list-ref dt 1) 100)) 1 3) "-"
   (substring (number->string (+ (list-ref dt 2) 100)) 1 3) " "
   (substring (number->string (+ (list-ref dt 3) 100)) 1 3) ":"
   (substring (number->string (+ (list-ref dt 4) 100)) 1 3) ":"
   (substring (number->string (+ (list-ref dt 5) 100)) 1 3)))
Dave Griffiths's avatar
Dave Griffiths committed
115
116

;; build entity from all ktvs, insert to db, return unique_id
117
118
119
120
121
(define (entity-record-values!)
  (let ((db (get-current 'db #f))
        (table (get-current 'table #f))
        (type (get-current 'entity-type #f)))
    ;; standard bits
122
123
124
125
126
    (let ((r (entity-create! db table type (get-current 'entity-values '()))))
      (entity-reset!) r)))


(define (entity-create! db table entity-type ktv-list)
127
  (msg "creating:" entity-type ktv-list)
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
  (let ((values
         (append
          (list
           (ktv-create "user" "varchar" (get-current 'user-id "none"))
           (ktv-create "time" "varchar" (date-time->string (date-time)))
           (ktv-create "lat" "real" (car (get-current 'location '(0 0))))
           (ktv-create "lon" "real" (cadr (get-current 'location '(0 0))))
           (ktv-create "deleted" "int" 0))
          ktv-list)))
    (let ((r (insert-entity/get-unique
              db table entity-type (get-current 'user-id "no id")
              values)))
      (msg "entity-create: " entity-type)
      r)))

143
144
145
146
147
148
149
150
151
152
153

(define (entity-update-values!)
  (let ((db (get-current 'db #f))
        (table (get-current 'table #f)))
    ;; standard bits
    (let ((values (get-current 'entity-values '()))
          (unique-id (ktv-get (get-current 'entity-values '()) "unique_id")))
      (cond
       ((and unique-id (not (null? values)))
        (update-entity db table (entity-id-from-unique db table unique-id) values)
        (msg "updated " unique-id)
154
        (msg values)
155
156
157
        ;; removed due to save button no longer exiting activity - need to keep!
        ;;(entity-reset!)
        )
158
159
       (else
        (msg "no values or no id to update as entity:" unique-id "values:" values))))))
Dave Griffiths's avatar
Dave Griffiths committed
160
161

(define (entity-reset!)
162
163
164
165
  (set-current! 'entity-values '())
  (set-current! 'db "reset")
  (set-current! 'table "reset")
  (set-current! 'entity-type "reset"))
Dave Griffiths's avatar
Dave Griffiths committed
166
167
168
169
170
171
172
173
174
175
176
177

(define (assemble-array entities)
  (foldl
   (lambda (i r)
     (if (equal? r "") (ktv-get i "unique_id")
         (string-append r "," (ktv-get i "unique_id"))))
   ""
   entities))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; syncing code

Dave Griffiths's avatar
Dave Griffiths committed
178
(define url "http://192.168.2.1:8889/symbai?")
Dave Griffiths's avatar
Dave Griffiths committed
179

180
181
(msg "url")

Dave Griffiths's avatar
Dave Griffiths committed
182
(define (build-url-from-ktv ktv)
183
  (string-append "&" (ktv-key ktv) ":" (ktv-type ktv) ":" (number->string (ktv-version ktv)) "=" (stringify-value-url ktv)))
Dave Griffiths's avatar
Dave Griffiths committed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201

(define (build-url-from-ktvlist ktvlist)
  (foldl
   (lambda (ktv r)
     (string-append r (build-url-from-ktv ktv)))
   "" ktvlist))

(define (build-url-from-entity table e)
  (string-append
   url
   "fn=sync"
   "&table=" table
   "&entity-type=" (list-ref (car e) 0)
   "&unique-id=" (list-ref (car e) 1)
   "&dirty=" (number->string (list-ref (car e) 2))
   "&version=" (number->string (list-ref (car e) 3))
   (build-url-from-ktvlist (cadr e))))

Dave Griffiths's avatar
Dave Griffiths committed
202
203
;; todo fix all hardcoded paths here
(define (send-files ktvlist)
Dave Griffiths's avatar
Dave Griffiths committed
204
  (msg "send-files" ktvlist)
Dave Griffiths's avatar
Dave Griffiths committed
205
206
  (foldl
   (lambda (ktv r)
Dave Griffiths's avatar
Dave Griffiths committed
207
     (msg (ktv-type ktv))
Dave Griffiths's avatar
Dave Griffiths committed
208
     (if (equal? (ktv-type ktv) "file")
Dave Griffiths's avatar
Dave Griffiths committed
209
210
211
212
213
214
215
         (begin
           (msg "sending" (ktv-value ktv))
           (cons (http-upload
                  (string-append "upload-" (ktv-value ktv))
                  "http://192.168.2.1:8889/symbai?fn=upload"
                  (string-append "/sdcard/symbai/files/" (ktv-value ktv)))
                 r))
Dave Griffiths's avatar
Dave Griffiths committed
216
217
218
         r))
   '() ktvlist))

Dave Griffiths's avatar
Dave Griffiths committed
219
220
;; spit all dirty entities to server
(define (spit db table entities)
221
  (msg "running spit")
Dave Griffiths's avatar
Dave Griffiths committed
222
223
  (foldl
   (lambda (e r)
224
     ;;(msg (car (car e)))
Dave Griffiths's avatar
Dave Griffiths committed
225
226
227
228
229
230
231
     (debug! (string-append "Sending a " (car (car e)) " to Raspberry Pi"))
     (append
      (list
       (http-request
        (string-append "req-" (list-ref (car e) 1))
        (build-url-from-entity table e)
        (lambda (v)
Dave Griffiths's avatar
Dave Griffiths committed
232
          (msg "in spit..." v)
Dave Griffiths's avatar
Dave Griffiths committed
233
234
235
          (cond
           ((or (equal? (car v) "inserted") (equal? (car v) "match"))
            (update-entity-clean db table (cadr v))
Dave Griffiths's avatar
Dave Griffiths committed
236
            (debug! (string-append "Uploaded " (car (car e)))))
Dave Griffiths's avatar
Dave Griffiths committed
237
238
239
240
           ((equal? (car v) "no change")
            (debug! (string-append "No change for " (car (car e)))))
           ((equal? (car v) "updated")
            (update-entity-clean db table (cadr v))
Dave Griffiths's avatar
Dave Griffiths committed
241
            (debug! (string-append "Updated changed " (car (car e)))))
Dave Griffiths's avatar
Dave Griffiths committed
242
243
244
245
           (else
            (debug! (string-append
                     "Problem uploading "
                     (car (car e)) " : " (car v)))))
Dave Griffiths's avatar
Dave Griffiths committed
246
247
248
249
250
251
252
253
254
          (append
           ;; check for file uploads
           (if (or (equal? (car v) "updated")
                   (equal? (car v) "inserted")
                   (equal? (car v) "match"))
               (send-files (cadr e)) ;; takes a ktvlist
               '())
           (list
            (update-widget 'text-view (get-id "sync-dirty") 'text (build-dirty db)))))))
Dave Griffiths's avatar
Dave Griffiths committed
255
256
257
258
      r))
   '()
   entities))

Dave Griffiths's avatar
Dave Griffiths committed
259
260
;; todo fix all hardcoded paths here
(define (request-files ktvlist)
Dave Griffiths's avatar
Dave Griffiths committed
261
  (msg "request-files")
Dave Griffiths's avatar
Dave Griffiths committed
262
263
264
  (foldl
   (lambda (ktv r)
     (if (equal? (ktv-type ktv) "file")
Dave Griffiths's avatar
Dave Griffiths committed
265
266
267
268
269
270
271
         (begin
           (msg "requesting" (ktv-value ktv))
           (cons (http-download
                  (string-append "download-" (ktv-value ktv))
                  (string-append "http://192.168.2.1:8889/files/" (ktv-value ktv))
                  (string-append "/sdcard/symbai/files/" (ktv-value ktv)))
                 r))
Dave Griffiths's avatar
Dave Griffiths committed
272
273
274
         r))
   '() ktvlist))

275
276
277
(msg "suck ent")


Dave Griffiths's avatar
Dave Griffiths committed
278
(define (suck-entity-from-server db table unique-id)
Dave Griffiths's avatar
Dave Griffiths committed
279
280
281
282
283
284
  ;; ask for the current version
  (http-request
   (string-append unique-id "-update-new")
   (string-append url "fn=entity&table=" table "&unique-id=" unique-id)
   (lambda (data)
     ;; check "sync-insert" in sync.ss raspberry pi-side for the contents of 'entity'
Dave Griffiths's avatar
Dave Griffiths committed
285
286
287
288
     (let* ((entity (list-ref data 0))
            (ktvlist (list-ref data 1))
            (unique-id (list-ref entity 1))
            (exists (entity-exists? db table unique-id)))
289
290
       ;;(msg "from server...:")
       ;;(msg ktvlist)
Dave Griffiths's avatar
Dave Griffiths committed
291
       ;; need to check exists again here, due to delays back and forth
Dave Griffiths's avatar
Dave Griffiths committed
292
293
294
295
       (if (not exists)
           (insert-entity-wholesale
            db table
            (list-ref entity 0) ;; entity-type
Dave Griffiths's avatar
Dave Griffiths committed
296
            unique-id
Dave Griffiths's avatar
Dave Griffiths committed
297
298
299
300
301
302
303
            0 ;; dirty
            (list-ref entity 2) ;; version
            ktvlist)
           (update-to-version
            db table (get-entity-id db table unique-id)
            (list-ref entity 2) ktvlist))
       (debug! (string-append (if exists "Got new: " "Updated: ") (ktv-get ktvlist "name")))
Dave Griffiths's avatar
Dave Griffiths committed
304
305
306
307
       (cons
        (update-widget 'text-view (get-id "sync-dirty") 'text (build-dirty db))
        (request-files ktvlist))))))

Dave Griffiths's avatar
Dave Griffiths committed
308
309
310

;; repeatedly read version and request updates
(define (suck-new db table)
Dave Griffiths's avatar
Dave Griffiths committed
311
  (msg "suck-new")
Dave Griffiths's avatar
Dave Griffiths committed
312
313
314
315
316
317
  (debug! "Requesting new entities")
  (list
   (http-request
    "new-entities-req"
    (string-append url "fn=entity-versions&table=" table)
    (lambda (data)
318
      (msg "entity-versions:" data)
Dave Griffiths's avatar
Dave Griffiths committed
319
320
321
322
323
324
325
326
327
328
329
      (let ((r (foldl
                (lambda (i r)
                  (let* ((unique-id (car i))
                         (version (cadr i))
                         (exists (entity-exists? db table unique-id))
                         (old
                          (if exists
                              (> version (get-entity-version
                                          db table
                                          (get-entity-id db table unique-id)))
                              #f)))
330

Dave Griffiths's avatar
Dave Griffiths committed
331
                    ;; if we don't have this entity or the version on the server is newer
Dave Griffiths's avatar
Dave Griffiths committed
332
333
334
                    (if (and (or (not exists) old)
                             ;; limit this to 5 a time
                             (< (length r) 5))
Dave Griffiths's avatar
Dave Griffiths committed
335
                        (cons (suck-entity-from-server db table unique-id) r)
Dave Griffiths's avatar
Dave Griffiths committed
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
                        r)))
                '()
                data)))
        (cond
         ((null? r)
          (debug! "No new data to download")
          (set-current! 'download 1)
          (append
           (if (eqv? (get-current 'upload 0) 1)
               (list (play-sound "ping")) '())
           (list
            (toast "No new data to download")) r))
         (else
          (debug! (string-append
                   "Requesting "
                   (number->string (length r)) " entities"))
          (cons
           (play-sound "active")
           r))))))))

356
357
358
(msg "build-dirty defined...")

(define (build-dirty db)
Dave Griffiths's avatar
Dave Griffiths committed
359
360
361
  (let ((sync (get-dirty-stats db "sync"))
        (stream (get-dirty-stats db "stream")))
    (string-append
362
363
     "Sync data: " (number->string (car sync)) "/" (number->string (cadr sync)) " "
     "Stream data: " (number->string (car stream)) "/" (number->string (cadr stream)))))
Dave Griffiths's avatar
Dave Griffiths committed
364
365

(define (upload-dirty db)
Dave Griffiths's avatar
Dave Griffiths committed
366
  (msg "upload-dirty")
Dave Griffiths's avatar
Dave Griffiths committed
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
  (let ((r (append
            (spit db "sync" (dirty-entities db "sync"))
            (spit db "stream" (dirty-entities db "stream")))))
    (append (cond
             ((> (length r) 0)
              (debug! (string-append "Uploading " (number->string (length r)) " items..."))
              (list
               (toast "Uploading data...")
               (play-sound "active")))
             (else
              (debug! "No data changed to upload")
              (set-current! 'upload 1)
              (append
               (if (eqv? (get-current 'download 0) 1)
                   (list (play-sound "ping")) '())
               (list
                (toast "No data changed to upload"))))) r)))

(define (connect-to-net fn)
  (list
   (network-connect
    "network"
    "mongoose-web"
    (lambda (state)
      (debug! (string-append "Raspberry Pi connection state now: " state))
      (append
       (if (equal? state "Connected") (fn) '())
       (list
        ;;(update-widget 'text-view (get-id "sync-connect") 'text state)
        ))))))
Dave Griffiths's avatar
Dave Griffiths committed
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413




(define i18n-lang 0)

(define i18n-text
  (list))

(define (mtext-lookup id)
  (define (_ l)
    (cond
     ((null? l) (string-append (symbol->string id) " not translated"))
     ((eq? (car (car l)) id)
      (let ((translations (cadr (car l))))
        (if (<= (length translations) i18n-lang)
            (string-append (symbol->string id) " not translated")
Dave Griffiths's avatar
Dave Griffiths committed
414
415
416
            (let ((r (list-ref translations i18n-lang)))
              (if (or (equal? r "") (equal? r " "))
                  (list-ref translations 0) r)))))
Dave Griffiths's avatar
Dave Griffiths committed
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
     (else (_ (cdr l)))))
  (_ i18n-text))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(define (symbol->id id)
  (when (not (symbol? id))
        (msg "symbol->id: [" id "] is not a symbol"))
  (make-id (symbol->string id)))

(define (get-symbol-id id)
  (when (not (symbol? id))
        (msg "symbol->id: [" id "] is not a symbol"))
  (get-id (symbol->string id)))

(define (mbutton id fn)
  (button (symbol->id id)
          (mtext-lookup id)
          40 (layout 'fill-parent 'wrap-content -1 'centre 5) fn))

(define (mbutton-scale id fn)
  (button (symbol->id id)
          (mtext-lookup id)
          40 (layout 'fill-parent 'wrap-content 1 'centre 5) fn))

(define (mtoggle-button id fn)
  (toggle-button (symbol->id id)
                 (mtext-lookup id)
Dave Griffiths's avatar
Dave Griffiths committed
445
446
447
                 30 (layout 'fill-parent 'wrap-content -1 'centre 0) "fancy"
                 ;; convert to 0/1 for easier db storage
                 (lambda (v) (fn (if v 1 0)))))
Dave Griffiths's avatar
Dave Griffiths committed
448
449
450
451

(define (mtoggle-button-scale id fn)
  (toggle-button (symbol->id id)
                 (mtext-lookup id)
Dave Griffiths's avatar
Dave Griffiths committed
452
453
                 30 (layout 'fill-parent 'wrap-content 1 'centre 0) "fancy"
                 (lambda (v) (fn (if v 1 0)))))
Dave Griffiths's avatar
Dave Griffiths committed
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

(define (mtext id)
  (text-view (symbol->id id)
             (mtext-lookup id)
             30 (layout 'wrap-content 'wrap-content -1 'centre 0)))

(define (mtext-fixed w id)
  (text-view (symbol->id id)
             (mtext-lookup id)
             30 (layout w 'wrap-content -1 'centre 0)))

(define (mtext-small id)
  (text-view (symbol->id id)
             (mtext-lookup id)
             20 (layout 'wrap-content 'wrap-content -1 'centre 0)))

(define (mtext-scale id)
  (text-view (symbol->id id)
             (mtext-lookup id)
             30 (layout 'wrap-content 'wrap-content 1 'centre 0)))

(define (mtitle id)
  (text-view (symbol->id id)
             (mtext-lookup id)
             50 (layout 'fill-parent 'wrap-content -1 'centre 5)))

(define (mtitle-scale id)
  (text-view (symbol->id id)
             (mtext-lookup id)
             50 (layout 'fill-parent 'wrap-content 1 'centre 5)))

(define (medit-text id type fn)
  (vert
   (text-view 0 (mtext-lookup id)
              30 (layout 'wrap-content 'wrap-content -1 'centre 0))
   (edit-text (symbol->id id) "" 30 type
              (layout 'fill-parent 'wrap-content -1 'centre 0)
              fn)))

(define (medit-text-scale id type fn)
  (vert
   (text-view 0 (mtext-lookup id)
              30 (layout 'wrap-content 'wrap-content 1 'centre 0))
   (edit-text (symbol->id id) "" 30 type
              (layout 'fill-parent 'wrap-content 1 'centre 0)
              fn)))

(define (mspinner id types fn)
  (vert
   (text-view (symbol->id id)
              (mtext-lookup id)
              30 (layout 'wrap-content 'wrap-content 1 'centre 10))
   (spinner (make-id (string-append (symbol->string id) "-spinner"))
            (map mtext-lookup types)
            (layout 'wrap-content 'wrap-content 1 'centre 0)
            (lambda (c) (fn c)))))

(define (mspinner-other id types fn)
  (horiz
   (vert
    (text-view (symbol->id id)
               (mtext-lookup id)
               30 (layout 'wrap-content 'wrap-content 1 'centre 10))
    (spinner (make-id (string-append (symbol->string id) "-spinner"))
             (map mtext-lookup types)
             (layout 'wrap-content 'wrap-content 1 'centre 0)
             (lambda (c) (fn c))))
   (vert
    (mtext-scale 'other)
    (edit-text (make-id (string-append (symbol->string id) "-edit-text"))
               "" 30 "normal"
               (layout 'fill-parent 'wrap-content 1 'centre 0)
               (lambda (t) (fn t))))))

Dave Griffiths's avatar
Dave Griffiths committed
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
(define (mspinner-other-vert id text-id types fn)
  (linear-layout
   0 'vertical
   (layout 'fill-parent 'wrap-content 1 'centre 5)
   (list 0 0 0 0)
   (list
    (text-view (symbol->id id)
               (mtext-lookup text-id)
               30 (layout 'wrap-content 'wrap-content 1 'centre 5))
    (spinner (make-id (string-append (symbol->string id) "-spinner"))
             (map mtext-lookup types)
             (layout 'wrap-content 'wrap-content 1 'centre 0)
             (lambda (c) (fn c)))
    (mtext-scale 'other)
    (edit-text (make-id (string-append (symbol->string id) "-edit-text"))
               "" 30 "normal"
               (layout 'fill-parent 'wrap-content 1 'centre 0)
               (lambda (t) (fn t))))))

Dave Griffiths's avatar
Dave Griffiths committed
547
548
549
550
551
552
553
554
555
556
557
558
559
560

(define (mclear-toggles id-list)
  (map
   (lambda (id)
     (update-widget 'toggle-button (get-id id) 'checked 0))
   id-list))

(define (mclear-toggles-not-me me id-list)
  (foldl
   (lambda (id r)
     (if (equal? me id)
         r (cons (update-widget 'toggle-button (get-id id) 'checked 0) r)))
   '() id-list))

561
562
563
564
565
(define (image-invalid? image-name)
  (or (null? image-name)
      (not image-name)
      (equal? image-name "none")))

Dave Griffiths's avatar
Dave Griffiths committed
566
567
568
569
570
571
572
573
;; fill out the widget from the current entity in the memory store
;; dispatches based on widget type
(define (mupdate widget-type id-symbol key)
  (cond
   ((or (eq? widget-type 'edit-text) (eq? widget-type 'text-view))
    (update-widget widget-type (get-symbol-id id-symbol) 'text
                   (entity-get-value key)))
   ((eq? widget-type 'toggle-button)
Dave Griffiths's avatar
Dave Griffiths committed
574
    (update-widget widget-type (get-symbol-id id-symbol) 'checked
Dave Griffiths's avatar
Dave Griffiths committed
575
576
577
                   (entity-get-value key)))
   ((eq? widget-type 'image-view)
    (let ((image-name (entity-get-value key)))
578
      (if (image-invalid? image-name)
Dave Griffiths's avatar
Dave Griffiths committed
579
580
581
582
583
          (update-widget widget-type (get-symbol-id id-symbol) 'image "face")
          (update-widget widget-type (get-symbol-id id-symbol) 'external-image
                         (string-append dirname "files/" image-name)))))
   (else (msg "mupdate-widget unhandled widget type" widget-type))))

584
585
586
(define (spinner-choice l i)
  (symbol->string (list-ref l i)))

587
(define (mupdate-spinner id-symbol key choices)
588
589
  (let* ((val (entity-get-value key)))
    (if (not val)
590
591
        (update-widget 'spinner
                       (get-id (string-append (symbol->string id-symbol) "-spinner"))
592
593
594
595
596
597
598
599
600
601
602
                       'selection 0)
        (let ((index (index-find (string->symbol val) choices)))
          (if index
              (update-widget 'spinner
                             (get-id (string-append (symbol->string id-symbol) "-spinner"))
                             'selection index)
              (begin
                (msg "spinner item in db " val " not found in list of items")
                (update-widget 'spinner
                               (get-id (string-append (symbol->string id-symbol) "-spinner"))
                               'selection 0)))))))
603
604

(define (mupdate-spinner-other id-symbol key choices)
605
606
  (let* ((val (dbg (entity-get-value key))))
    (if (not val)
607
608
        (update-widget 'spinner
                       (get-id (string-append (symbol->string id-symbol) "-spinner"))
609
610
611
612
613
614
615
616
617
                       'selection 0)
        (let ((index (index-find (string->symbol val) choices)))
          (if index
              (update-widget 'spinner
                             (get-id (string-append (symbol->string id-symbol) "-spinner"))
                             'selection index)
              (update-widget 'edit-text
                             (get-id (string-append (symbol->string id-symbol) "-edit-text"))
                             'selection index))))))
618

Dave Griffiths's avatar
Dave Griffiths committed
619
620
621
622
623
624
625
626
627
628
629
630
631
;;;;
;; (y m d h m s)
(define (date-minus-months d ms)
  (let ((year (list-ref d 0))
        (month (- (list-ref d 1) 1)))
    (let ((new-month (- month ms)))
      (list
       (if (< new-month 0) (- year 1) year)
       (+ (if (< new-month 0) (+ new-month 12) new-month) 1)
       (list-ref d 2)
       (list-ref d 3)
       (list-ref d 4)
       (list-ref d 5)))))
Dave Griffiths's avatar
Dave Griffiths committed
632
633
634

(define (do-gps display-id key-prepend)
  (let ((loc (get-current 'location '(0 0))))
635
636
    (entity-set-value! (string-append key-prepend "-lat") "real" (car loc))
    (entity-set-value! (string-append key-prepend "-lon") "real" (cadr loc))
Dave Griffiths's avatar
Dave Griffiths committed
637
638
639
640
641
642
643
644
645
646
647
648
649
    (list
     (update-widget
      'text-view
      (get-id (string-append (symbol->string display-id) "-lat"))
      'text
      (number->string (car loc)))
     (update-widget
      'text-view
      (get-id (string-append (symbol->string display-id) "-lon"))
      'text
      (number->string (cadr loc))))))

(define (mupdate-gps display-id key-prepend)
Dave Griffiths's avatar
Dave Griffiths committed
650
651
652
  (let ((lat (entity-get-value (string-append key-prepend "-lat")))
        (lon (entity-get-value (string-append key-prepend "-lon"))))
    (if (or (not lat) (not lon))
Dave Griffiths's avatar
Dave Griffiths committed
653
654
655
656
657
658
659
        (list
         (update-widget
          'text-view (get-id (string-append (symbol->string display-id) "-lat"))
          'text "O")
         (update-widget
          'text-view (get-id (string-append (symbol->string display-id) "-lon"))
          'text "0"))
Dave Griffiths's avatar
Dave Griffiths committed
660
661
662
663
664
665
666
        (list
         (update-widget
          'text-view (get-id (string-append (symbol->string display-id) "-lat"))
          'text (number->string lat))
         (update-widget
          'text-view (get-id (string-append (symbol->string display-id) "-lon"))
          'text (number->string lon))))))
667
668
669
670


;; a standard builder for list widgets of entities and a
;; make new button, to add defaults to the list
Dave Griffiths's avatar
Dave Griffiths committed
671
(define (build-list-widget db table title entity-type edit-activity parent-fn ktv-default-fn)
672
673
674
675
676
677
678
679
680
    (vert-colour
     colour-two
     (horiz
      (mtitle-scale title)
      (button
       (make-id (string-append (symbol->string title) "-add"))
       (mtext-lookup 'add-item-to-list)
       40 (layout 100 'wrap-content 1 'centre 5)
       (lambda ()
681
682
683
         (entity-create!
          db table entity-type
          (ktvlist-merge
Dave Griffiths's avatar
Dave Griffiths committed
684
           (ktv-default-fn)
685
           (list (ktv "parent" "varchar" (parent-fn)))))
686
687
688
689
690
691
692
693
694
695
696
697
         (list (update-list-widget db table entity-type edit-activity (parent-fn))))))
     (linear-layout
      (make-id (string-append entity-type "-list"))
      'vertical
      (layout 'fill-parent 'wrap-content 1 'centre 20)
      (list 0 0 0 0)
      (list))))

;; pull db data into list of button widgets
(define (update-list-widget db table entity-type edit-activity parent)
  (let ((search-results
         (if parent
698
699
700
             (db-filter-only db table entity-type
                             (list (list "parent" "varchar" "=" parent))
                             (list (list "name" "varchar")))
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
             (db-all db table entity-type))))
    (update-widget
     'linear-layout
     (get-id (string-append entity-type "-list"))
     'contents
     (if (null? search-results)
         (list (mtext 'list-empty))
         (map
          (lambda (e)
            (button
             (make-id (string-append "list-button-" (ktv-get e "unique_id")))
             (or (ktv-get e "name") "Unamed item")
             40 (layout 'fill-parent 'wrap-content 1 'centre 5)
             (lambda ()
               (msg "sending start act" (ktv-get e "unique_id"))
               (list (start-activity edit-activity 0 (ktv-get e "unique_id"))))))
          search-results)))))

(define (delete-button)
  (mbutton
   'delete
   (lambda ()
     (list
      (alert-dialog
       "delete-check"
       (mtext-lookup 'delete-are-you-sure)
       (lambda (v)
         (cond
          ((eqv? v 1)
730
           (entity-set-value! "deleted" "int" 1)
731
732
733
734
           (entity-update-values!)
           (list (finish-activity 1)))
          (else
           (list)))))))))
Dave Griffiths's avatar
Dave Griffiths committed
735

736
737
738
739
740
741
742
743
744
745
(define (build-array-from-names db table entity-type)
  (map
   (lambda (e)
     (list (ktv-get e "name")
           (ktv-get e "unique_id")))
   (dbg (db-filter-only db table entity-type
                        (list)
                        (list (list "name" "varchar"))))))


Dave Griffiths's avatar
Dave Griffiths committed
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
(define vowel (map symbol->string (list 'a 'e 'i 'o 'u)))
(define consonant (map symbol->string (list 'b 'c 'd 'f 'g 'h 'j 'k 'l 'm 'n 'p 'q 'r 's 't 'v 'w 'x 'y 'z)))

(define (word-gen)
  (define (_ s vowel-prob)
    (cond
     ((zero? s) '())
     ((< (random) vowel-prob)
      (cons (choose vowel) (_ (- s 1) (/ vowel-prob 2))))
     (else
      (cons (choose consonant) (_ (- s 1) (* vowel-prob 2))))))
  (apply string-append (_ (+ 3 (random-int 8)) 0.5)))



(define (simpsons-village db table default-ktvlist)
  (entity-create! db table "village"
                  (ktvlist-merge
                   default-ktvlist
                   (list
766
                    (ktv "name" "varchar" (string-append "Village-" (number->string (random-int 1000))))
Dave Griffiths's avatar
Dave Griffiths committed
767
768
769
770
771
772
773
774
775
                    (ktv "block" "varchar" (word-gen))
                    (ktv "district" "varchar" (word-gen))
                    (ktv "car" "int" (random-int 2))))))

(define (simpsons-household db table parent default-ktvlist)
  (entity-create! db table "household"
                  (ktvlist-merge
                   default-ktvlist
                   (list
776
                    (ktv "name" "varchar" (string-append "Household-" (number->string (random-int 1000))))
Dave Griffiths's avatar
Dave Griffiths committed
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
                    (ktv "num-pots" "int" (random-int 10))
                    (ktv "parent" "varchar" parent)))))

(define (simpsons-individual db table parent default-ktvlist)
  (let ((n (random-int 1000)))
  (entity-create! db table "individual"
                  (ktvlist-merge
                   default-ktvlist
                   (append
                    (list (ktv "parent" "varchar" parent))
  (choose
   (list
   (list
    (ktv-create "name" "varchar"
                (string-append "Abe-" (number->string n)))
Dave Griffiths's avatar
Dave Griffiths committed
792
    (ktv-create "gender" "varchar" "male")
Dave Griffiths's avatar
Dave Griffiths committed
793
794
795
796
    (ktv-create "photo" "file" "abe.jpg"))
   (list
    (ktv-create
     "name" "varchar" (string-append "Akira-" (number->string n)))
Dave Griffiths's avatar
Dave Griffiths committed
797
    (ktv-create "gender" "varchar" "male")
Dave Griffiths's avatar
Dave Griffiths committed
798
799
800
801
    (ktv-create "photo" "file" "akira.jpg"))
   (list
    (ktv-create
     "name" "varchar" (string-append "Apu-" (number->string n)))
Dave Griffiths's avatar
Dave Griffiths committed
802
    (ktv-create "gender" "varchar" "male")
Dave Griffiths's avatar
Dave Griffiths committed
803
804
805
806
    (ktv-create "photo" "file" "apu.jpg"))
   (list
    (ktv-create
     "name" "varchar" (string-append "Barney-" (number->string n)))
Dave Griffiths's avatar
Dave Griffiths committed
807
    (ktv-create "gender" "varchar" "male")
Dave Griffiths's avatar
Dave Griffiths committed
808
809
810
811
    (ktv-create "photo" "file" "barney.jpg"))
   (list
    (ktv-create
     "name" "varchar" (string-append "Bart-" (number->string n)))
Dave Griffiths's avatar
Dave Griffiths committed
812
    (ktv-create "gender" "varchar" "male")
Dave Griffiths's avatar
Dave Griffiths committed
813
814
815
816
    (ktv-create "photo" "file" "bartsimpson.jpg"))
   (list
    (ktv-create
     "name" "varchar" (string-append "Billy-" (number->string n)))
Dave Griffiths's avatar
Dave Griffiths committed
817
    (ktv-create "gender" "varchar" "male")
Dave Griffiths's avatar
Dave Griffiths committed
818
819
820
821
    (ktv-create "photo" "file" "billy.jpg"))
   (list
    (ktv-create
     "name" "varchar" (string-append "Carl-" (number->string n)))
Dave Griffiths's avatar
Dave Griffiths committed
822
    (ktv-create "gender" "varchar" "male")
Dave Griffiths's avatar
Dave Griffiths committed
823
824
825
826
    (ktv-create "photo" "file" "carl.jpg"))
   (list
    (ktv-create
     "name" "varchar" (string-append "Cletus-" (number->string n)))
Dave Griffiths's avatar
Dave Griffiths committed
827
    (ktv-create "gender" "varchar" "male")
Dave Griffiths's avatar
Dave Griffiths committed
828
829
830
831
    (ktv-create "photo" "file" "cletus.jpg"))
   (list
    (ktv-create
     "name" "varchar" (string-append "ComicBookGuy-" (number->string n)))
Dave Griffiths's avatar
Dave Griffiths committed
832
    (ktv-create "gender" "varchar" "male")
Dave Griffiths's avatar
Dave Griffiths committed
833
834
835
836
    (ktv-create "photo" "file" "comicbookguy.jpg"))
   (list
    (ktv-create
     "name" "varchar" (string-append "Homer-" (number->string n)))
Dave Griffiths's avatar
Dave Griffiths committed
837
    (ktv-create "gender" "varchar" "male")
Dave Griffiths's avatar
Dave Griffiths committed
838
839
840
841
    (ktv-create "photo" "file" "homersimpson.jpg"))
   (list
    (ktv-create
     "name" "varchar" (string-append "Jasper-" (number->string n)))
Dave Griffiths's avatar
Dave Griffiths committed
842
    (ktv-create "gender" "varchar" "male")
Dave Griffiths's avatar
Dave Griffiths committed
843
844
845
846
    (ktv-create "photo" "file" "jasper.jpg"))
   (list
    (ktv-create
     "name" "varchar" (string-append "Kent-" (number->string n)))
Dave Griffiths's avatar
Dave Griffiths committed
847
    (ktv-create "gender" "varchar" "male")
Dave Griffiths's avatar
Dave Griffiths committed
848
849
850
851
    (ktv-create "photo" "file" "kentbrockman.jpg"))
   (list
    (ktv-create
     "name" "varchar" (string-append "Kodos-" (number->string n)))
Dave Griffiths's avatar
Dave Griffiths committed
852
    (ktv-create "gender" "varchar" "male")
Dave Griffiths's avatar
Dave Griffiths committed
853
854
855
856
    (ktv-create "photo" "file" "kodos.jpg"))
   (list
    (ktv-create
     "name" "varchar" (string-append "Lenny-" (number->string n)))
Dave Griffiths's avatar
Dave Griffiths committed
857
    (ktv-create "gender" "varchar" "male")
Dave Griffiths's avatar
Dave Griffiths committed
858
859
860
861
    (ktv-create "photo" "file" "lenny.jpg"))
   (list
    (ktv-create
     "name" "varchar" (string-append "Lisa-" (number->string n)))
Dave Griffiths's avatar
Dave Griffiths committed
862
    (ktv-create "gender" "varchar" "female")
Dave Griffiths's avatar
Dave Griffiths committed
863
864
865
866
    (ktv-create "photo" "file" "lisasimpson.jpg"))
   (list
    (ktv-create
     "name" "varchar" (string-append "Marge-" (number->string n)))
Dave Griffiths's avatar
Dave Griffiths committed
867
    (ktv-create "gender" "varchar" "female")
Dave Griffiths's avatar
Dave Griffiths committed
868
869
870
871
    (ktv-create "photo" "file" "margesimpson.jpg"))
   (list
    (ktv-create
     "name" "varchar" (string-append "Martin-" (number->string n)))
Dave Griffiths's avatar
Dave Griffiths committed
872
    (ktv-create "gender" "varchar" "male")
Dave Griffiths's avatar
Dave Griffiths committed
873
874
875
876
    (ktv-create "photo" "file" "martinprince.jpg"))
   (list
    (ktv-create
     "name" "varchar" (string-append "Milhouse-" (number->string n)))
Dave Griffiths's avatar
Dave Griffiths committed
877
    (ktv-create "gender" "varchar" "male")
Dave Griffiths's avatar
Dave Griffiths committed
878
879
880
881
    (ktv-create "photo" "file" "milhouse.jpg"))
   (list
    (ktv-create
     "name" "varchar" (string-append "MrBurns-" (number->string n)))
Dave Griffiths's avatar
Dave Griffiths committed
882
    (ktv-create "gender" "varchar" "male")
Dave Griffiths's avatar
Dave Griffiths committed
883
884
885
886
    (ktv-create "photo" "file" "mrburns.jpg"))
   (list
    (ktv-create
     "name" "varchar" (string-append "Ned-" (number->string n)))
Dave Griffiths's avatar
Dave Griffiths committed
887
    (ktv-create "gender" "varchar" "male")
Dave Griffiths's avatar
Dave Griffiths committed
888
889
890
891
    (ktv-create "photo" "file" "nedflanders.jpg"))
   (list
    (ktv-create
     "name" "varchar" (string-append "Nelson-" (number->string n)))
Dave Griffiths's avatar
Dave Griffiths committed
892
    (ktv-create "gender" "varchar" "male")
Dave Griffiths's avatar
Dave Griffiths committed
893
894
895
896
    (ktv-create "photo" "file" "nelson.jpg"))
   (list
    (ktv-create
     "name" "varchar" (string-append "Otto-" (number->string n)))
Dave Griffiths's avatar
Dave Griffiths committed
897
    (ktv-create "gender" "varchar" "male")
Dave Griffiths's avatar
Dave Griffiths committed
898
899
900
901
    (ktv-create "photo" "file" "otto.jpg"))
   (list
    (ktv-create
     "name" "varchar" (string-append "Ralph-" (number->string n)))
Dave Griffiths's avatar
Dave Griffiths committed
902
    (ktv-create "gender" "varchar" "male")
Dave Griffiths's avatar
Dave Griffiths committed
903
904
905
906
    (ktv-create "photo" "file" "ralphwiggum.jpg"))
   (list
    (ktv-create
     "name" "varchar" (string-append "Santaslittlehelper-" (number->string n)))
Dave Griffiths's avatar
Dave Griffiths committed
907
    (ktv-create "gender" "varchar" "male")
Dave Griffiths's avatar
Dave Griffiths committed
908
909
910
911
    (ktv-create "photo" "file" "santaslittlehelper.jpg"))
   (list
    (ktv-create
     "name" "varchar" (string-append "SideshowBob-" (number->string n)))
Dave Griffiths's avatar
Dave Griffiths committed
912
    (ktv-create "gender" "varchar" "male")
Dave Griffiths's avatar
Dave Griffiths committed
913
914
915
916
917
918
919
920
921
    (ktv-create "photo" "file" "sideshowbob.jpg")))))))))

(define (looper! n fn)
  (when (not (zero? n))
        (fn n)
        (looper! (- n 1) fn)))

(define (build-test! db table village-ktvlist household-ktvlist individual-ktvlist)
  (looper!
922
   1
Dave Griffiths's avatar
Dave Griffiths committed
923
924
925
926
   (lambda (i)
     (msg "making village" i)
     (let ((village (simpsons-village db table village-ktvlist)))
       (looper!
927
        3
Dave Griffiths's avatar
Dave Griffiths committed
928
929
930
931
932
        (lambda (i)
          (alog "household")
          (msg "making household" i)
          (let ((household (simpsons-household db table village household-ktvlist)))
            (looper!
933
             (random-int 10)
Dave Griffiths's avatar
Dave Griffiths committed
934
935
936
             (lambda (i)
               (msg "making individual" i)
               (simpsons-individual db table household individual-ktvlist))))))))))