-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.rkt
executable file
·581 lines (563 loc) · 21.9 KB
/
main.rkt
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
#!/usr/bin/env racket
#lang racket/base
; main.rkt
; main file for ivy, the taggable image viewer
(require racket/class
racket/cmdline
racket/gui/base
racket/list
racket/path
riff
txexpr
xml
"base.rkt"
"config.rkt"
"db.rkt"
"embed.rkt"
"error-log.rkt"
(only-in "files.rkt" ivy-version)
"frame.rkt"
"meta-editor.rkt"
"thumbnails.rkt")
(define show-frame? (make-parameter #t))
(define tags-to-search (make-parameter empty))
(define search-type (make-parameter #f))
(define tags-to-exclude (make-parameter empty))
(define excluding? (make-parameter #f))
(define null-flag (make-parameter #f))
(define verbose? (make-parameter #f))
(define list-tags? (make-parameter #f))
(define add-tags? (make-parameter #f))
(define tags-to-add (make-parameter empty))
(define delete-tags? (make-parameter #f))
(define tags-to-delete (make-parameter empty))
(define set-tags? (make-parameter #f))
(define tags-to-set (make-parameter empty))
(define moving? (make-parameter #f))
(define purging? (make-parameter #f))
(define show-xmp? (make-parameter #f))
(define set-xmp? (make-parameter #f))
(define xmp-to-set (make-parameter ""))
(define show-rating? (make-parameter #f))
(define set-rating? (make-parameter #f))
(define rating-to-set (make-parameter "0"))
(define show-tagged? (make-parameter #f))
(define show-untagged? (make-parameter #f))
; make sure the path provided is a proper absolute path
(define relative->absolute (compose1 simple-form-path expand-user-path))
; resize ivy-frame based on the dimensions of the image
(define (resize-ivy-frame bmp)
; determine the monitor dimensions
(define-values (monitor-width monitor-height) (get-display-size))
; approximate canvas offset
(define canvas-offset 84)
(define max-width (- monitor-width 100))
(define max-height (- monitor-height 100))
(define bmp-width (send bmp get-width))
(define bmp-height (send bmp get-height))
(cond
; all good, let's shrink the frame
[(and (< bmp-width max-width)
(< bmp-height max-height))
(send ivy-frame resize
(inexact->exact (floor bmp-width))
(+ (inexact->exact (floor bmp-height)) canvas-offset))]
; image is the same size as the monitor
[(and (= bmp-width max-width)
(= bmp-height max-height))
(define resized
(let ([scale (/ (- max-height canvas-offset) bmp-height)])
(* bmp-height scale)))
(send ivy-frame resize resized max-height)]
; wide image
[(and (>= bmp-width max-width)
(<= bmp-height max-height))
(define y
(cond [(> bmp-height (- max-height canvas-offset))
max-height]
[(<= bmp-height (- max-height canvas-offset))
(+ bmp-height canvas-offset)]
[else bmp-height]))
(define scale
(let ([scale-w (/ max-width bmp-width)]
[scale-y (/ max-height y)])
(if (<= scale-w scale-y) scale-w scale-y)))
(define resized-height (* y scale))
(send ivy-frame resize
max-width
(+ (inexact->exact (floor resized-height)) canvas-offset))]
; tall image
[(and (<= bmp-width max-width)
(>= bmp-height max-height))
(define resized
(let ([scale (/ (- max-height canvas-offset) bmp-height)])
(* bmp-width scale)))
(send ivy-frame resize (inexact->exact (floor resized)) max-height)]
; both wide and tall
[(and (> bmp-width max-width)
(> bmp-height max-height))
(define scale
(let ([scale-x (/ max-width bmp-width)]
[scale-y (/ (- max-height canvas-offset) bmp-height)])
(if (<= scale-x scale-y) scale-x scale-y)))
(define resized-width (* bmp-width scale))
(send ivy-frame resize (inexact->exact (floor resized-width)) max-height)]))
(define (set-image-paths! paths)
; if there are directories as paths, scan them
(define absolute-paths
(remove-duplicates
(for/fold ([imgs empty])
([ap (map relative->absolute paths)])
; directory?
(if (directory-exists? ap)
(append imgs (dir-files ap))
(append imgs (list ap))))))
(cond [(> (length absolute-paths) 1)
; we want to load a collection
(pfs absolute-paths)]
[else
; we want to load the image from the directory
(define base (path-only (first absolute-paths)))
(image-dir base)
; (path-files dir) filters only supported images
(pfs (path-files base))])
(image-path (first absolute-paths)))
; application handler stuff
(application-file-handler
(λ (path)
(when (supported-file? path)
(cond [(equal? (image-path) +root-path+)
; prep work
(set-image-paths! (list path))
; actually load the file
(load-image path)
; only bother resizing ivy-frame if we're starting fresh
(resize-ivy-frame image-bmp-master)
; center the frame
(send ivy-frame center 'both)]
[else
; append image to the collection
(send (ivy-canvas) on-drop-file path)]))))
; accept command-line path to load image
(command-line
#:program "Ivy"
#:usage-help
"Calling Ivy without a path will simply open the GUI."
"Supplying a path will tell Ivy to load the provided image."
"Supplying multiple paths will tell Ivy to load them as a collection."
#:once-any
[("-V" "--version")
"Display Ivy version."
(printf "Ivy ~a~n" ivy-version)
(exit:exit)]
[("-o" "--search-or")
taglist
"Search the tags database inclusively with a comma-separated string."
(show-frame? #f)
(search-type 'or)
(tags-to-search (string->taglist taglist))]
[("-a" "--search-and")
taglist
"Search the tags database exclusively with a comma-separated string."
(show-frame? #f)
(search-type 'and)
(tags-to-search (string->taglist taglist))]
[("-L" "--list-tags")
"Lists the tags for the image(s)."
(show-frame? #f)
(list-tags? #t)]
[("-A" "--add-tags")
taglist
"Add tags to an image. ex: ivy -A \"tag0, tag1, ...\" /path/to/image ..."
(show-frame? #f)
(add-tags? #t)
(tags-to-add (string->taglist taglist))]
[("-D" "--delete-tags")
taglist
"Delete tags from image. ex: ivy -D \"tag0, tag1, ...\" /path/to/image ..."
(show-frame? #f)
(delete-tags? #t)
(tags-to-delete (string->taglist taglist))]
[("-P" "--purge")
"Remove all tags from the images and purge from the database. ex: ivy -P /path/to/image ..."
(show-frame? #f)
(purging? #t)]
[("-T" "--set-tags")
taglist
"Sets the taglist of the image. ex: ivy -T \"tag0, tag1, ...\" /path/to/image ..."
(show-frame? #f)
(set-tags? #t)
(tags-to-set (string->taglist taglist))]
[("-M" "--move-image")
"Moves the source file(s) to the destination, updating the database."
(show-frame? #f)
(moving? #t)]
[("--show-xmp")
"Extract the embedded XMP in supported images."
(show-frame? #f)
(show-xmp? #t)]
[("--set-xmp")
xmp-str
"Set the embedded XMP in supported images."
(show-frame? #f)
(set-xmp? #t)
(xmp-to-set xmp-str)]
[("--show-rating")
"Show the stored rating from the database."
(show-frame? #f)
(show-rating? #t)]
[("--set-rating")
rating
"Set the xmp:Rating for the image."
(show-frame? #f)
(set-rating? #t)
(rating-to-set rating)]
[("-t" "--tagged")
"List tagged images in a given directory."
(show-frame? #f)
(show-tagged? #t)]
[("-u" "--untagged")
"List untagged images in a given directory."
(show-frame? #f)
(show-untagged? #t)]
#:once-each
[("-e" "--exact-search")
"Search the tags database for exact matches."
(show-frame? #f)
(exact-search? #t)]
[("-x" "--exclude")
exclude
"Search the tags database with -o/-a, but exclude images with the specified tags."
(show-frame? #f)
(excluding? #t)
(tags-to-exclude (string->taglist exclude))]
[("-n" "--null")
"Search result items are terminated by a null character instead of by whitespace."
(show-frame? #f)
(null-flag #t)]
[("-v" "--verbose")
"Display verbose information for certain operations."
(show-frame? #f)
(verbose? #t)]
#:args args
; hijack requested-images for -M
(unless (or (not (show-frame?)) (empty? args))
; if there are directories as paths, scan them
(set-image-paths! args))
(unless (show-frame?)
(uncaught-exception-handler (λ (err)
(printf "ivy: error: ~a~n" (exn-message err))
(exit 1))))
(cond
; we aren't search for tags on the cmdline, open frame
[(show-frame?)
; only change the error port if we're opening the GUI
(current-error-port err-port)
(send (ivy-canvas) focus)
; center the frame
(send ivy-frame center 'both)
(send ivy-frame show #t)
; canvas won't resize until the frame is shown.
(when (supported-file? (image-path))
(load-image (image-path))
(resize-ivy-frame image-bmp-master)
(send ivy-frame center 'both))]
; only searching for tags
[(and (search-type)
(not (excluding?))
(not (empty? (tags-to-search)))
(empty? (tags-to-exclude)))
(define search-results
(if (exact-search?)
(search-db-exact (search-type) (tags-to-search))
(search-db-inexact (search-type) (tags-to-search))))
(define search-sorted (sort (map path->string search-results) string<?))
(define len (length search-sorted))
(unless (zero? len)
(for ([sr (in-list search-sorted)])
(if (null-flag)
(printf "~a" (bytes-append (string->bytes/utf-8 sr) (bytes 0)))
(printf "~a~n" sr)))
(when (verbose?)
(printf "Found ~a results for tags ~v~n" len (tags-to-search))))]
; only excluding tags (resulting output may be very long!)
[(and (excluding?)
(not (search-type))
(not (empty? (tags-to-exclude))))
(define imgs (table-column 'images 'Path))
(define excluded
(if (exact-search?)
(exclude-search-exact imgs (tags-to-exclude))
(exclude-search-inexact imgs (tags-to-exclude))))
(define final-sorted (sort (map path->string excluded) string<?))
(define len (length final-sorted))
(unless (zero? len)
(for ([sr (in-list final-sorted)])
(if (null-flag)
(printf "~a" (bytes-append (string->bytes/utf-8 sr) (bytes 0)))
(printf "~a~n" sr)))
(when (verbose?)
(printf "Found ~a results without tags ~v~n" len (tags-to-exclude))))]
; searching for tags and excluding tags
[(and (search-type)
(excluding?)
(not (empty? (tags-to-search)))
(not (empty? (tags-to-exclude))))
(define search-results
(if (exact-search?)
(search-db-exact (search-type) (tags-to-search))
(search-db-inexact (search-type) (tags-to-search))))
(cond [(zero? (length search-results))
(when (verbose?)
(printf "Found 0 results for tags ~v~n" (tags-to-search)))]
[else
(define exclude
(if (exact-search?)
(exclude-search-exact search-results (tags-to-exclude))
(exclude-search-inexact search-results (tags-to-exclude))))
(define exclude-sorted (sort (map path->string exclude) string<?))
(for ([ex (in-list exclude-sorted)])
(if (null-flag)
(printf "~a" (bytes-append (string->bytes/utf-8 ex) (bytes 0)))
(printf "~a~n" ex)))
(when (verbose?)
(printf "Found ~a results for tags ~v, excluding tags ~v~n"
(length exclude-sorted) (tags-to-search) (tags-to-exclude)))])]
[(list-tags?)
(for ([img (in-list args)])
(define absolute-path (path->string (relative->absolute img)))
(when (db-has-key? 'images absolute-path)
(when (verbose?)
(printf "~a:~n" absolute-path))
(define taglist (image-taglist absolute-path))
(for ([tag (in-list taglist)])
(if (null-flag)
(printf "~a" (bytes-append (string->bytes/utf-8 tag) (bytes 0)))
(printf "~a~n" tag)))))]
[(show-xmp?)
(for ([img (in-list args)])
(define absolute-path (path->string (relative->absolute img)))
(when (embed-support? absolute-path)
(when (verbose?)
(printf "~a:~n" absolute-path))
(define xmp (get-embed-xmp absolute-path))
(for ([str (in-list xmp)])
(if (null-flag)
(printf "~a" (bytes-append (string->bytes/utf-8 str) (bytes 0)))
(printf "~a~n" str)))))]
; default to 0 if there is no recorded xmp:Rating
[(show-rating?)
(for ([img (in-list args)])
(define absolute-path (path->string (relative->absolute img)))
(when (embed-support? absolute-path)
(when (verbose?)
(printf "~a: " absolute-path))
(cond [(db-has-key? 'ratings absolute-path)
(displayln (image-rating absolute-path))]
[else (displayln 0)])))]
[(add-tags?)
(cond
[(empty? args)
(raise-argument-error 'add-tags "1 or more image arguments" (length args))
(exit:exit)]
[else
(for ([img (in-list args)])
(define absolute-path (path->string (relative->absolute img)))
(unless (empty? (tags-to-add))
(define img-obj
(if (db-has-key? 'images absolute-path)
(make-data-object sqlc image% absolute-path)
(new image% [path absolute-path])))
(when (verbose?)
(printf "Adding tags ~v to ~v~n" (tags-to-add) absolute-path))
(when (embed-support? img)
(add-embed-tags! img (tags-to-add)))
(add-tags! img-obj (tags-to-add))))])]
[(delete-tags?)
(cond
[(empty? args)
(raise-argument-error 'delete-tags "1 or more image arguments" (length args))
(exit:exit)]
[else
(for ([img (in-list args)])
(define absolute-path (path->string (relative->absolute img)))
(when (and (not (empty? (tags-to-delete)))
(db-has-key? 'images absolute-path))
(define img-obj (make-data-object sqlc image% absolute-path))
(when (verbose?)
(printf "Removing tags ~v from ~v~n" (tags-to-delete) absolute-path))
(when (embed-support? img)
(del-embed-tags! img (tags-to-delete)))
(del-tags! img-obj (tags-to-delete))))])]
[(purging?)
(for ([img (in-list args)])
(define absolute-path (path->string (relative->absolute img)))
; clean up the thumbnail cache a little
(define thumb-name (path->md5 absolute-path))
(when (verbose?)
(printf "Purging ~v from the database.~n" absolute-path))
(db-purge! absolute-path)
; remove the old thumbnail
(when (file-exists? thumb-name)
(delete-file thumb-name)))]
[(set-tags?)
(cond
[(empty? args)
(raise-argument-error 'set-tags "1 or more image arguments" (length args))
(exit:exit)]
[else
(for ([img (in-list args)])
(define absolute-path (path->string (relative->absolute img)))
(unless (empty? (tags-to-set))
(when (verbose?)
(printf "Setting tags of ~v to ~v~n" absolute-path (tags-to-set)))
(reconcile-tags! absolute-path (tags-to-set))
(when (embed-support? absolute-path)
(set-embed-tags! absolute-path (tags-to-set)))))])]
; set the XMP metadata for a file
[(set-xmp?)
(cond
[(empty? args)
(raise-argument-error 'set-xmp "1 or more image arguments" (length args))
(exit:exit)]
[else
; make sure the paths are absolute
(define absolutes (map relative->absolute args))
(for ([path (in-list absolutes)])
(when (embed-support? path)
; set the XMP data
(set-embed-xmp! path (xmp-to-set))
; grab the tag list and update the database
(define xexpr (string->xexpr (xmp-to-set)))
; find the dc:subject info
(define dc:sub-lst (findf*-txexpr xexpr is-dc:subject?))
(define tags
(if dc:sub-lst
; grab the embedded tags
(flatten (map dc:subject->list dc:sub-lst))
empty))
(when (verbose?)
(printf "Setting the XMP of ~a...~n" path))
(reconcile-tags! (path->string path) (sort tags string<?))))])]
; set the xmp:Rating in both the database and the XMP
[(set-rating?)
(cond
[(empty? args)
(raise-argument-error 'add-tags "1 or more image arguments" (length args))
(exit:exit)]
[else
(for ([img (in-list args)])
(define absolute-path (path->string (relative->absolute img)))
(when (verbose?)
(printf "Setting the rating for ~a...~n" absolute-path))
; set the rating in the database
(set-image-rating! absolute-path (string->number (rating-to-set)))
; set the rating in the embedded XMP
(when (embed-support? absolute-path)
(define xmp (get-embed-xmp absolute-path))
(define xexpr (if (empty? xmp)
(make-xmp-xexpr empty)
(string->xexpr (first xmp))))
(define tag (findf-txexpr xexpr (is-tag? 'xmp:Rating)))
(define setted
((set-xmp-tag (if tag 'xmp:Rating 'rdf:Description))
xexpr
(create-dc-meta "xmp:Rating"
(rating-to-set)
""
(box (list (xexpr->string xexpr))))))
(set-embed-xmp! absolute-path (xexpr->string setted))))])]
; moving an image in the database to another location
[(moving?)
(define len (length args))
(cond
[(< len 2)
(raise-argument-error 'move-image "2 or more arguments" len)
(exit:exit)]
[else
; make sure the paths are absolute
(define absolute-str
(for/list ([ri (in-list args)])
(path->string (relative->absolute ri))))
(define dest-or-dir (last absolute-str))
(define-values (dest-base dest-name must-be-dir?) (split-path dest-or-dir))
(for ([old-path (in-list (take absolute-str (- len 1)))])
(define new-path
(let ([file-name (file-name-from-path old-path)])
(cond
; dest is a directory ending in /
[must-be-dir? (path->string (build-path dest-base dest-name file-name))]
; dest is a directory that does not end in /
[(directory-exists? (build-path dest-base dest-name))
(path->string (build-path dest-base dest-name file-name))]
; dest is a file path
[else
(cond
[(> len 2)
(raise-argument-error 'move-image
"destination needs to be a directory"
dest-or-dir)
#f]
[else dest-or-dir])])))
; do the actual moving
(when new-path
; if new-path exists or old-path doesn't exist, error out
(cond [(file-exists? new-path)
(raise-user-error '--move-image "Destination path ~v already exists." new-path)]
[(not (file-exists? old-path))
(raise-user-error '--move-image "Source path ~v does not exist." old-path)]
[else
(cond
; reassociate the tags to the new destination
[(db-has-key? 'images old-path)
; clean up the thumbnail cache a little
(define old-thumb-name (path->md5 old-path))
(define new-thumb-name (path->md5 new-path))
(define old-img-obj (make-data-object sqlc image% old-path))
(define tags (send old-img-obj get-tags))
; remove the old thumbnails
(when (file-exists? old-thumb-name)
(delete-file old-thumb-name))
(when (file-exists? new-thumb-name)
(delete-file new-thumb-name))
; move the tags
(reconcile-tags! new-path tags)
; preserve the old rating, if we can
(define old-rating
(if (db-has-key? 'ratings old-path)
(image-rating old-path)
0))
(set-image-rating! new-path old-rating)
(db-purge! old-path)]
[else
; spit out an error message, but move anyway
(eprintf "Database does not contain ~v~n" old-path)])
; copy the file over, do not overwrite dest if exists
(when (verbose?)
(printf "Moving ~v to ~v~n" old-path new-path))
(rename-file-or-directory old-path new-path #f)])))])]
[(show-tagged?)
(cond [(empty? args)
(raise-argument-error 'show-tagged "at least one directory" 0)
(exit:exit)]
[else
(for ([dir (in-list args)])
(define files (for/list ([f (in-directory dir)]) f))
(define absolute-paths (map (compose1 path->string relative->absolute) files))
(for ([path (in-list absolute-paths)])
(when (db-has-key? 'images path)
(printf "~a~n" path))))])]
[(show-untagged?)
(cond [(empty? args)
(raise-argument-error 'show-tagged "at least one directory" 0)
(exit:exit)]
[else
(for ([dir (in-list args)])
(define files (for/list ([f (in-directory dir)]) f))
(define absolute-paths (map (compose1 path->string relative->absolute) files))
(for ([path (in-list absolute-paths)])
(unless (db-has-key? 'images path)
(printf "~a~n" path))))])])
; exit explicitly
(unless (show-frame?)
(exit:exit)))