-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathcollection.c
More file actions
3204 lines (2863 loc) · 108 KB
/
collection.c
File metadata and controls
3204 lines (2863 loc) · 108 KB
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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file is part of darktable,
Copyright (C) 2024-2025 darktable developers.
darktable is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
darktable 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 darktable. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common/collection.h"
#include "common/debug.h"
#include "common/image.h"
#include "common/image_cache.h"
#include "common/metadata.h"
#include "common/utility.h"
#include "common/map_locations.h"
#include "common/datetime.h"
#include "control/conf.h"
#include "control/control.h"
#include "imageio/imageio_rawspeed.h"
#include <assert.h>
#include <glib.h>
#include <memory.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#ifdef _WIN32
//MSVCRT does not have strptime implemented
#include "win/strptime.h"
#endif
#ifdef USE_LUA
#include "lua/call.h"
#include "lua/events.h"
#endif
#define SELECT_QUERY "SELECT DISTINCT * FROM %s"
#define LIMIT_QUERY "LIMIT ?1, ?2"
/* Stores the collection query, returns 1 if changed.. */
static int _dt_collection_store(const dt_collection_t *collection,
gchar *query,
gchar *query_no_group);
/* Counts the number of images in the current collection */
static uint32_t _dt_collection_compute_count(const dt_collection_t *collection,
const gboolean no_group);
/* signal handlers to update the cached count when something
* interesting might have happened. we need 2 different since there
* are different kinds of signals we need to listen to. */
static void _dt_collection_recount_callback_tag(gpointer instance,
gpointer user_data);
static void _dt_collection_recount_callback_filmroll(gpointer instance,
gpointer user_data);
static void _dt_collection_recount_callback_2(gpointer instance,
const uint8_t id,
gpointer user_data);
static void _dt_collection_filmroll_imported_callback(gpointer instance,
const uint8_t id,
gpointer user_data);
/* determine image offset of specified imgid for the given collection */
static int dt_collection_image_offset_with_collection(const dt_collection_t *collection,
const dt_imgid_t imgid);
/* update aspect ratio for the selected images */
static void _collection_update_aspect_ratio(const dt_collection_t *collection);
const dt_collection_t *dt_collection_new(const dt_collection_t *clone)
{
dt_collection_t *collection = g_malloc0(sizeof(dt_collection_t));
/* initialize collection context*/
if(clone) /* if clone is provided let's copy it into this context */
{
memcpy(&collection->params, &clone->params, sizeof(dt_collection_params_t));
memcpy(&collection->store, &clone->store, sizeof(dt_collection_params_t));
collection->where_ext = g_strdupv(clone->where_ext);
collection->query = g_strdup(clone->query);
collection->query_no_group = g_strdup(clone->query_no_group);
collection->clone = 1;
collection->count = clone->count;
collection->count_no_group = clone->count_no_group;
collection->tagid = clone->tagid;
}
else /* else we just initialize using the reset */
dt_collection_reset(collection);
/* connect to all the signals that might indicate that the count of
images matching the collection changed
*/
DT_CONTROL_SIGNAL_CONNECT(DT_SIGNAL_TAG_CHANGED, _dt_collection_recount_callback_tag, collection);
DT_CONTROL_SIGNAL_CONNECT(DT_SIGNAL_FILMROLLS_CHANGED, _dt_collection_recount_callback_filmroll, collection);
DT_CONTROL_SIGNAL_CONNECT(DT_SIGNAL_FILMROLLS_REMOVED, _dt_collection_recount_callback_filmroll, collection);
DT_CONTROL_SIGNAL_CONNECT(DT_SIGNAL_IMAGE_IMPORT, _dt_collection_recount_callback_2, collection);
DT_CONTROL_SIGNAL_CONNECT(DT_SIGNAL_FILMROLLS_IMPORTED, _dt_collection_filmroll_imported_callback, collection);
return collection;
}
void dt_collection_free(const dt_collection_t *collection)
{
DT_CONTROL_SIGNAL_DISCONNECT_ALL(collection, "collection");
g_free(collection->query);
g_free(collection->query_no_group);
g_strfreev(collection->where_ext);
g_free((dt_collection_t *)collection);
}
const dt_collection_params_t *dt_collection_params(const dt_collection_t *collection)
{
return &collection->params;
}
// Return a pointer to a static string for an "AND" operator if the
// number of terms processed so far requires it. The variable used
// for term should be an int initialized to and_operator_initial()
// before use.
#define and_operator_initial() (0)
static char * and_operator(int *term)
{
assert(term != NULL);
if(*term == 0)
{
*term = 1;
return "";
}
else
{
return " AND ";
}
assert(0); // Not reached.
}
void dt_collection_memory_update()
{
if(!darktable.collection || !darktable.db) return;
sqlite3_stmt *stmt;
/* check if we can get a query from collection */
gchar *query = g_strdup(dt_collection_get_query(darktable.collection));
if(!query) return;
// we have a new query for the collection of images to display. For
// speed reason we collect all images into a temporary (in-memory)
// table (collected_images).
// 1. drop previous data
// clang-format off
DT_DEBUG_SQLITE3_EXEC(dt_database_get(darktable.db),
"DELETE FROM memory.collected_images",
NULL, NULL, NULL);
// reset autoincrement. need in star_key_accel_callback
DT_DEBUG_SQLITE3_EXEC(dt_database_get(darktable.db),
"DELETE FROM memory.sqlite_sequence"
" WHERE name='collected_images'",
NULL, NULL, NULL);
// clang-format on
// 2. insert collected images into the temporary table
gchar *ins_query = g_strdup_printf("INSERT INTO memory.collected_images (imgid) %s", query);
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db), ins_query, -1, &stmt, NULL);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, 0);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, -1);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
g_free(query);
g_free(ins_query);
}
static void _dt_collection_set_selq_pre_sort(const dt_collection_t *collection,
char **selq_pre)
{
const uint32_t tagid = collection->tagid;
char tag[16] = { 0 };
snprintf(tag, sizeof(tag), "%u", tagid);
gboolean tag_join = FALSE;
// select which fields are needed
gchar *fields = g_strdup("mi.id"); // we always want to load the image id
// the other needed fields are for the sorting order
if(collection->params.query_flags & COLLECTION_QUERY_USE_SORT)
{
// we always need filename and version : they are fallback orders in any cases
dt_util_str_cat(&fields, ", filename, version");
if(collection->params.sorts[DT_COLLECTION_SORT_GROUP])
dt_util_str_cat(&fields, ", group_id");
if(collection->params.sorts[DT_COLLECTION_SORT_PATH])
dt_util_str_cat(&fields, ", film_id");
if(collection->params.sorts[DT_COLLECTION_SORT_DATETIME])
dt_util_str_cat(&fields, ", datetime_taken");
if(collection->params.sorts[DT_COLLECTION_SORT_IMPORT_TIMESTAMP])
dt_util_str_cat(&fields, ", import_timestamp");
if(collection->params.sorts[DT_COLLECTION_SORT_CHANGE_TIMESTAMP])
dt_util_str_cat(&fields, ", change_timestamp");
if(collection->params.sorts[DT_COLLECTION_SORT_EXPORT_TIMESTAMP])
dt_util_str_cat(&fields, ", export_timestamp");
if(collection->params.sorts[DT_COLLECTION_SORT_PRINT_TIMESTAMP])
dt_util_str_cat(&fields, ", print_timestamp");
if(collection->params.sorts[DT_COLLECTION_SORT_ASPECT_RATIO])
dt_util_str_cat(&fields, ", aspect_ratio");
if(collection->params.sorts[DT_COLLECTION_SORT_RATING])
dt_util_str_cat(&fields, ", flags");
if(collection->params.sorts[DT_COLLECTION_SORT_CUSTOM_ORDER])
{
tag_join = (tagid);
dt_util_str_cat(&fields,
", %s position",
tagid ? "CASE WHEN ti.position IS NULL THEN 0 ELSE ti.position END AS" : "");
}
}
// clang-format off
dt_util_str_cat
(selq_pre,
"SELECT DISTINCT sel.id"
" FROM (SELECT %s"
" FROM main.images AS mi"
" %s%s"
" WHERE ",
fields,
tag_join ? " LEFT JOIN main.tagged_images AS ti"
" ON ti.imgid = mi.id AND ti.tagid = " : "",
tag_join ? tag : "");
// clang-format on
g_free(fields);
}
int dt_collection_update(const dt_collection_t *collection)
{
uint32_t result;
gchar *wq, *wq_no_group, *sq, *selq_pre, *selq_post, *query, *query_no_group;
wq = wq_no_group = sq = selq_pre = selq_post = query = query_no_group = NULL;
/* build where part */
int and_term = and_operator_initial();
/* add default filters */
if(collection->params.filter_flags & COLLECTION_FILTER_FILM_ID)
{
wq = g_strdup_printf("%s (film_id = %u)",
and_operator(&and_term), collection->params.film_id);
}
// DON'T SELECT IMAGES MARKED TO BE DELETED.
dt_util_str_cat(&wq, " %s (flags & %d) != %d",
and_operator(&and_term), DT_IMAGE_REMOVE,
DT_IMAGE_REMOVE);
/* add where ext if wanted */
if((collection->params.query_flags & COLLECTION_QUERY_USE_WHERE_EXT))
{
gchar *where_ext = dt_collection_get_extended_where(collection, -1);
dt_util_str_cat(&wq, " %s %s", and_operator(&and_term), where_ext);
g_free(where_ext);
}
wq_no_group = g_strdup(wq);
/* grouping */
if(darktable.gui && darktable.gui->grouping)
{
// clang-format off
/* Show the expanded group... */
dt_util_str_cat
(&wq,
" AND (group_id = %d OR "
/* ...and, in unexpanded groups, show the representative image.
* It's possible that the above WHERE clauses will filter out the representative
* image, so we have some logic here to pick the image id closest to the
* representative image.
* The *2+CASE statement are to break ties, so that when id < group_id, it's
* weighted a little higher than when id > group_id. */
"mi.id IN (SELECT id FROM "
"(SELECT id,"
" MIN(ABS(id-group_id)*2 + CASE WHEN (id-group_id) < 0 THEN 1 ELSE 0 END)"
" FROM main.images AS mi WHERE %s GROUP BY group_id)))",
darktable.gui->expanded_group_id, wq_no_group);
// clang-format on
/* Additionally, when a group is expanded, make sure the
* representative image wasn't filtered out. This is important,
* because otherwise it may be impossible to collapse the group
* again. */
dt_util_str_cat(&wq, " OR (mi.id = %d)", darktable.gui->expanded_group_id);
}
// get all the sort items
dt_collection_params_t *params = (dt_collection_params_t *)&collection->params;
for(int i = 0; i < DT_COLLECTION_SORT_LAST; i++)
params->sorts[i] = FALSE;
const int nb_sort = CLAMP(dt_conf_get_int("plugins/lighttable/filtering/num_sort"),
0, DT_COLLECTION_MAX_RULES);
char confname[200] = { 0 };
for(int i = 0; i < nb_sort; i++)
{
snprintf(confname, sizeof(confname), "plugins/lighttable/filtering/sort%1d", i);
params->sorts[dt_conf_get_int(confname)] = TRUE;
}
// and we also take account of the last sort
params->sorts[dt_conf_get_int("plugins/lighttable/filtering/lastsort")] = TRUE;
/* build select part includes where */
_dt_collection_set_selq_pre_sort(collection, &selq_pre);
selq_post = g_strdup(") AS sel");
if(collection->params.query_flags & COLLECTION_QUERY_USE_SORT)
{
// some sort orders require to join tables to the query
if(collection->params.sorts[DT_COLLECTION_SORT_COLOR])
{
dt_util_str_cat
(&selq_post, " LEFT OUTER JOIN main.color_labels AS b ON sel.id = b.imgid");
}
if(collection->params.sorts[DT_COLLECTION_SORT_PATH])
{
// clang-format off
dt_util_str_cat
(&selq_post, " JOIN (SELECT id AS film_rolls_id, folder"
" FROM main.film_rolls) ON film_id = film_rolls_id");
// clang-format on
}
if(collection->params.sorts[DT_COLLECTION_SORT_TITLE])
{
dt_util_str_cat
(&selq_post,
" LEFT OUTER JOIN main.meta_data AS mt ON sel.id = mt.id"
" AND mt.key = (SELECT key FROM data.meta_data WHERE tagname = 'Xmp.dc.title')");
}
if(collection->params.sorts[DT_COLLECTION_SORT_DESCRIPTION])
{
dt_util_str_cat
(&selq_post,
" LEFT OUTER JOIN main.meta_data AS md ON sel.id = md.id"
" AND md.key = (SELECT key FROM data.meta_data WHERE tagname = 'Xmp.dc.description')");
}
}
/* build sort order part */
if(collection->params.query_flags & COLLECTION_QUERY_USE_SORT)
{
sq = dt_collection_get_sort_query(collection);
}
/* store the new query */
dt_util_str_cat(&query, "%s%s%s %s%s",
selq_pre, wq, selq_post, sq ? sq : "",
(collection->params.query_flags & COLLECTION_QUERY_USE_LIMIT)
? " " LIMIT_QUERY : "");
dt_util_str_cat(&query_no_group, "%s%s%s %s%s",
selq_pre, wq_no_group, selq_post, sq ? sq : "",
(collection->params.query_flags & COLLECTION_QUERY_USE_LIMIT)
? " " LIMIT_QUERY : "");
result = _dt_collection_store(collection, query, query_no_group);
/* free memory used */
g_free(sq);
g_free(wq);
g_free(wq_no_group);
g_free(selq_pre);
g_free(selq_post);
g_free(query);
g_free(query_no_group);
/* update the cached count. collection isn't a real const anyway, we
* are writing to it in _dt_collection_store, too. */
((dt_collection_t *)collection)->count = UINT32_MAX;
((dt_collection_t *)collection)->count_no_group =
_dt_collection_compute_count(collection, TRUE);
dt_collection_hint_message(collection);
_collection_update_aspect_ratio(collection);
return result;
}
void dt_collection_reset(const dt_collection_t *collection)
{
dt_collection_params_t *params = (dt_collection_params_t *)&collection->params;
/* setup defaults */
params->query_flags = COLLECTION_QUERY_FULL;
params->filter_flags = COLLECTION_FILTER_FILM_ID | COLLECTION_FILTER_ATLEAST_RATING;
params->film_id = 1;
/* apply stored query parameters from previous darktable session */
params->film_id = dt_conf_get_int("plugins/collection/film_id");
params->filter_flags = dt_conf_get_int("plugins/collection/filter_flags");
dt_collection_update_query(collection,
DT_COLLECTION_CHANGE_NEW_QUERY,
DT_COLLECTION_PROP_UNDEF, NULL);
}
const gchar *dt_collection_get_query(const dt_collection_t *collection)
{
/* ensure there is a query string for collection */
if(!collection->query)
dt_collection_update(collection);
return collection->query;
}
const gchar *dt_collection_get_query_no_group(const dt_collection_t *collection)
{
/* ensure there is a query string for collection */
if(!collection->query_no_group)
dt_collection_update(collection);
return collection->query_no_group;
}
uint32_t dt_collection_get_filter_flags(const dt_collection_t *collection)
{
return collection->params.filter_flags;
}
void dt_collection_set_filter_flags(const dt_collection_t *collection,
const uint32_t flags)
{
dt_collection_params_t *params = (dt_collection_params_t *)&collection->params;
params->filter_flags = flags;
}
uint32_t dt_collection_get_query_flags(const dt_collection_t *collection)
{
return collection->params.query_flags;
}
void dt_collection_set_query_flags(const dt_collection_t *collection,
const uint32_t flags)
{
dt_collection_params_t *params = (dt_collection_params_t *)&collection->params;
params->query_flags = flags;
}
gchar *dt_collection_get_extended_where(const dt_collection_t *collection,
const int exclude)
{
gchar *complete_string = NULL;
if(exclude >= 0)
{
complete_string = g_strdup("");
char confname[200];
snprintf(confname, sizeof(confname), "plugins/lighttable/collect/mode%1d", exclude);
const int mode = dt_conf_get_int(confname);
// we only want collect rules, not filtering ones
const int nb_rules = CLAMP(dt_conf_get_int("plugins/lighttable/collect/num_rules"),
1, 10);
for(int i = 0;
i < nb_rules && collection->where_ext[i] != NULL;
i++)
{
// exclude the one rule from extended where
if(i != exclude || mode == 1)
dt_util_str_cat(&complete_string, "%s", collection->where_ext[i]);
else if(i == 0 && g_strcmp0(collection->where_ext[i], ""))
dt_util_str_cat(&complete_string, "1=1");
}
}
else
{
// WHERE part is composed by (COLLECT) AND (FILTERING)
// first, the COLLECT PART (can be empty)
complete_string = g_strdup("");
const int nb_rules = CLAMP(dt_conf_get_int("plugins/lighttable/collect/num_rules"),
1, 10);
gchar *rules_txt = g_strdup("");
for(int i = 0;
i < nb_rules && collection->where_ext[i] != NULL;
i++)
{
dt_util_str_cat(&rules_txt, "%s", collection->where_ext[i]);
}
if(g_strcmp0(rules_txt, ""))
dt_util_str_cat(&complete_string, "(%s)", rules_txt);
g_free(rules_txt);
// and now the FILTERING part (can be empty)
rules_txt = g_strdup("");
const int nb_filters = CLAMP(dt_conf_get_int("plugins/lighttable/filtering/num_rules"),
0, 10);
for(int i = 0;
i < nb_filters && collection->where_ext[i + nb_rules] != NULL;
i++)
{
dt_util_str_cat(&rules_txt, "%s", collection->where_ext[i + nb_rules]);
}
if(g_strcmp0(rules_txt, ""))
{
if(g_strcmp0(complete_string, ""))
dt_util_str_cat(&complete_string, " AND ");
dt_util_str_cat(&complete_string, "(%s)", rules_txt);
}
g_free(rules_txt);
}
if(!g_strcmp0(complete_string, ""))
dt_util_str_cat(&complete_string, "1=1");
gchar *where_ext = g_strdup_printf("(%s)", complete_string);
g_free(complete_string);
return where_ext;
}
void dt_collection_set_extended_where(const dt_collection_t *collection,
gchar **extended_where)
{
/* free extended where if already exists */
g_strfreev(collection->where_ext);
/* set new from parameter */
((dt_collection_t *)collection)->where_ext = g_strdupv(extended_where);
}
void dt_collection_set_film_id(const dt_collection_t *collection,
const int32_t film_id)
{
dt_collection_params_t *params = (dt_collection_params_t *)&collection->params;
params->film_id = film_id;
}
void dt_collection_set_tag_id(dt_collection_t *collection,
const uint32_t tagid)
{
collection->tagid = tagid;
}
static void _image_set_raw_aspect_ratio(const dt_imgid_t imgid)
{
dt_image_t *image = dt_image_cache_get(imgid, 'w');
if(image)
{
/* set image aspect ratio */
const int side = image->orientation < ORIENTATION_SWAP_XY ? image->p_height : image->p_width;
image->aspect_ratio = dt_usable_aspect((float )image->p_height / (float )(MAX(1, side)));
dt_image_cache_write_release_info(image, DT_IMAGE_CACHE_SAFE, "dt_image_set_raw_aspect_ratio");
}
}
static void _collection_update_aspect_ratio(const dt_collection_t *collection)
{
dt_collection_params_t *params = (dt_collection_params_t *)&collection->params;
// Update the aspect ratio for selected images in the collection if
// needed, we do not do this for all images as it could take a long
// time. The aspect ratio is then updated when needed, and at some
// point all aspect ratio for all images will be set and this could
// won't be really needed.
if(params->sorts[DT_COLLECTION_SORT_ASPECT_RATIO])
{
const float MAX_TIME = 7.0;
const gchar *where_ext = dt_collection_get_extended_where(collection, -1);
sqlite3_stmt *stmt = NULL;
// clang-format off
gchar *query = g_strdup_printf(
"SELECT id"
" FROM main.images AS mi"
" WHERE %s AND (aspect_ratio=0.0 OR aspect_ratio IS NULL)", where_ext);
// clang-format on
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db), query, -1, &stmt, NULL);
const double start = dt_get_wtime();
while(sqlite3_step(stmt) == SQLITE_ROW)
{
const dt_imgid_t imgid = sqlite3_column_int(stmt, 0);
_image_set_raw_aspect_ratio(imgid);
if(dt_get_wtime() - start > MAX_TIME)
{
dt_control_log(_("too much time to update aspect ratio for the collection"));
break;
}
}
sqlite3_finalize(stmt);
g_free(query);
}
}
const char *dt_collection_name_untranslated(const dt_collection_properties_t prop)
{
char *col_name = NULL;
switch(prop)
{
case DT_COLLECTION_PROP_FILMROLL:
return N_("film roll");
case DT_COLLECTION_PROP_FOLDERS:
return N_("folder");
case DT_COLLECTION_PROP_CAMERA:
return N_("camera");
case DT_COLLECTION_PROP_TAG:
return N_("tag");
case DT_COLLECTION_PROP_DAY:
return N_("capture date");
case DT_COLLECTION_PROP_TIME:
return N_("capture time");
case DT_COLLECTION_PROP_IMPORT_TIMESTAMP:
return N_("import time");
case DT_COLLECTION_PROP_CHANGE_TIMESTAMP:
return N_("modification time");
case DT_COLLECTION_PROP_EXPORT_TIMESTAMP:
return N_("export time");
case DT_COLLECTION_PROP_PRINT_TIMESTAMP:
return N_("print time");
case DT_COLLECTION_PROP_HISTORY:
return N_("history");
case DT_COLLECTION_PROP_COLORLABEL:
return N_("color label");
case DT_COLLECTION_PROP_LENS:
return N_("lens");
case DT_COLLECTION_PROP_FOCAL_LENGTH:
return N_("focal length");
case DT_COLLECTION_PROP_ISO:
return N_("ISO");
case DT_COLLECTION_PROP_APERTURE:
return N_("aperture");
case DT_COLLECTION_PROP_EXPOSURE:
return N_("exposure");
case DT_COLLECTION_PROP_EXPOSURE_BIAS:
return N_("exposure bias");
case DT_COLLECTION_PROP_ASPECT_RATIO:
return N_("aspect ratio");
case DT_COLLECTION_PROP_FILENAME:
return N_("filename");
case DT_COLLECTION_PROP_GEOTAGGING:
return N_("geotagging");
case DT_COLLECTION_PROP_GROUP_ID:
return N_("group");
case DT_COLLECTION_PROP_LOCAL_COPY:
return N_("local copy");
case DT_COLLECTION_PROP_MODULE:
return N_("module");
case DT_COLLECTION_PROP_ORDER:
return N_("module order");
case DT_COLLECTION_PROP_RATING_RANGE:
return N_("range rating");
case DT_COLLECTION_PROP_RATING:
return N_("rating");
case DT_COLLECTION_PROP_TEXTSEARCH:
return N_("search");
case DT_COLLECTION_PROP_WHITEBALANCE:
return N_("white balance");
case DT_COLLECTION_PROP_FLASH:
return N_("flash");
case DT_COLLECTION_PROP_EXPOSURE_PROGRAM:
return N_("exposure program");
case DT_COLLECTION_PROP_METERING_MODE:
return N_("metering mode");
case DT_COLLECTION_PROP_LAST:
return NULL;
default:
return NULL;
}
return col_name;
}
const char *dt_collection_name(const dt_collection_properties_t prop)
{
return _(dt_collection_name_untranslated(prop));
};
static gchar *_dt_collection_get_sort_text(const dt_collection_sort_t sort,
const int sortorder)
{
// construct the text query
gchar *sq = NULL;
switch(sort)
{
case DT_COLLECTION_SORT_DATETIME:
case DT_COLLECTION_SORT_IMPORT_TIMESTAMP:
case DT_COLLECTION_SORT_CHANGE_TIMESTAMP:
case DT_COLLECTION_SORT_EXPORT_TIMESTAMP:
case DT_COLLECTION_SORT_PRINT_TIMESTAMP:
{
const int local_order = sort;
char *colname;
switch(local_order)
{
case DT_COLLECTION_SORT_DATETIME:
colname = "datetime_taken";
break;
case DT_COLLECTION_SORT_IMPORT_TIMESTAMP:
colname = "import_timestamp";
break;
case DT_COLLECTION_SORT_CHANGE_TIMESTAMP:
colname = "change_timestamp";
break;
case DT_COLLECTION_SORT_EXPORT_TIMESTAMP:
colname = "export_timestamp";
break;
case DT_COLLECTION_SORT_PRINT_TIMESTAMP:
colname = "print_timestamp";
break;
default:
colname = "";
}
sq = g_strdup_printf("%s%s", colname, (sortorder) ? " DESC" : "");
break;
}
case DT_COLLECTION_SORT_RATING:
sq = g_strdup_printf("CASE WHEN flags & 8 = 8 THEN -1 ELSE flags & 7 END%s",
(sortorder) ? " DESC" : "");
break;
case DT_COLLECTION_SORT_FILENAME:
sq = g_strdup_printf("filename%s", (sortorder) ? " DESC" : "");
break;
case DT_COLLECTION_SORT_ID:
sq = g_strdup_printf("sel.id%s", (sortorder) ? " DESC" : "");
break;
case DT_COLLECTION_SORT_COLOR:
sq = g_strdup_printf("color%s", (sortorder) ? "" : " DESC");
break;
case DT_COLLECTION_SORT_GROUP:
sq = g_strdup_printf("group_id%s, sel.id-group_id != 0, sel.id",
(sortorder) ? " DESC" : "");
break;
case DT_COLLECTION_SORT_PATH:
sq = g_strdup_printf("folder%s, filename%s", (sortorder) ? " DESC" : "",
(sortorder) ? " DESC" : "");
break;
case DT_COLLECTION_SORT_CUSTOM_ORDER:
sq = g_strdup_printf("position%s", (sortorder) ? " DESC" : "");
break;
case DT_COLLECTION_SORT_TITLE:
sq = g_strdup_printf("mt.value%s", (sortorder) ? " DESC" : "");
break;
case DT_COLLECTION_SORT_DESCRIPTION:
sq = g_strdup_printf("md.value%s", (sortorder) ? " DESC" : "");
break;
case DT_COLLECTION_SORT_ASPECT_RATIO:
sq = g_strdup_printf("aspect_ratio%s", (sortorder) ? " DESC" : "");
break;
case DT_COLLECTION_SORT_SHUFFLE:
sq = g_strdup("RANDOM()"); /* do not consider second order for shuffle */
/* do not remember shuffle for second order */
break;
case DT_COLLECTION_SORT_NONE:
default: /*fall through for default*/
// shouldn't happen
sq = g_strdup("sel.id");
break;
}
return sq;
}
gchar *dt_collection_get_sort_query(const dt_collection_t *collection)
{
gboolean filename = FALSE;
int first_order = 0;
const dt_collection_sort_t lastsort = dt_conf_get_int("plugins/lighttable/filtering/lastsort");
const int lastsortorder = dt_conf_get_int("plugins/lighttable/filtering/lastsortorder");
gboolean already_last_sort = FALSE;
gchar *query = g_strdup("ORDER BY");
const int nb_sort = CLAMP(dt_conf_get_int("plugins/lighttable/filtering/num_sort"),
0, DT_COLLECTION_MAX_RULES);
for(int i = 0; i < nb_sort; i++)
{
// read the sort value from conf
char confname[200] = { 0 };
snprintf(confname, sizeof(confname), "plugins/lighttable/filtering/sort%1d", i);
const dt_collection_sort_t sort = dt_conf_get_int(confname);
snprintf(confname, sizeof(confname), "plugins/lighttable/filtering/sortorder%1d", i);
const int sortorder = dt_conf_get_int(confname);
// get the sort query
gchar *sq = _dt_collection_get_sort_text(sort, sortorder);
dt_util_str_cat(&query, "%s %s", (i == 0) ? "" : ",", sq);
g_free(sq);
// set the "already done" values
if(sort == DT_COLLECTION_SORT_FILENAME)
filename = TRUE;
if(i == 0)
first_order = sortorder;
if(sort == lastsort)
already_last_sort = TRUE;
}
// and last sort order set
if(!already_last_sort)
{
gchar *lsq = _dt_collection_get_sort_text(lastsort, lastsortorder);
dt_util_str_cat(&query, ", %s", lsq);
g_free(lsq);
if(lastsort == DT_COLLECTION_SORT_FILENAME)
filename = TRUE;
}
// complete the query with fallback if needed
if(!filename)
dt_util_str_cat(&query, ", filename%s",
(first_order) ? " DESC" : "");
dt_util_str_cat(&query, ", version ASC");
return query;
}
static int _dt_collection_store(const dt_collection_t *collection,
gchar *query,
gchar *query_no_group)
{
/* store flags to conf */
if(collection == darktable.collection)
{
dt_conf_set_int("plugins/collection/query_flags", collection->params.query_flags);
dt_conf_set_int("plugins/collection/filter_flags", collection->params.filter_flags);
dt_conf_set_int("plugins/collection/film_id", collection->params.film_id);
}
/* store query in context */
g_free(collection->query);
g_free(collection->query_no_group);
((dt_collection_t *)collection)->query = g_strdup(query);
((dt_collection_t *)collection)->query_no_group = g_strdup(query_no_group);
return 1;
}
static uint32_t _dt_collection_compute_count(const dt_collection_t *collection,
const gboolean no_group)
{
sqlite3_stmt *stmt = NULL;
uint32_t count = 0;
const gchar *query = no_group
? dt_collection_get_query_no_group(collection)
: dt_collection_get_query(collection);
gchar *count_query = NULL;
gchar *fq = g_strstr_len(query, strlen(query), "FROM");
count_query = g_strdup_printf("SELECT COUNT(DISTINCT sel.id) %s", fq);
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db), count_query, -1, &stmt, NULL);
if(collection->params.query_flags & COLLECTION_QUERY_USE_LIMIT)
{
DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, 0);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, -1);
}
if(sqlite3_step(stmt) == SQLITE_ROW)
count = sqlite3_column_int(stmt, 0);
sqlite3_finalize(stmt);
g_free(count_query);
return count;
}
uint32_t dt_collection_get_count(const dt_collection_t *collection)
{
if(collection->count == UINT32_MAX)
((dt_collection_t*)collection)->count = _dt_collection_compute_count(collection, FALSE);
return collection->count;
}
uint32_t dt_collection_get_count_no_group(const dt_collection_t *collection)
{
return collection->count_no_group;
}
uint32_t dt_collection_get_selected_count(void)
{
sqlite3_stmt *stmt = NULL;
uint32_t count = 0;
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db),
"SELECT COUNT(*) FROM main.selected_images",
-1, &stmt, NULL);
if(sqlite3_step(stmt) == SQLITE_ROW)
count = sqlite3_column_int(stmt, 0);
sqlite3_finalize(stmt);
return count;
}
uint32_t dt_collection_get_collected_count(void)
{
sqlite3_stmt *stmt = NULL;
uint32_t count = 0;
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db),
"SELECT COUNT(*) FROM memory.collected_images",
-1, &stmt, NULL);
if(sqlite3_step(stmt) == SQLITE_ROW)
count = sqlite3_column_int(stmt, 0);
sqlite3_finalize(stmt);
return count;
}
GList *dt_collection_get(const dt_collection_t *collection,
const int limit,
const gboolean selected)
{
GList *list = NULL;
const gchar *query = dt_collection_get_query_no_group(collection);
if(query)
{
sqlite3_stmt *stmt = NULL;
if(selected)
{
// clang-format off
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db),
"SELECT mi.imgid FROM main.selected_images AS s"
" JOIN memory.collected_images AS mi"
" WHERE mi.imgid = s.imgid"
" LIMIT -1, ?1",
-1, &stmt, NULL);
// clang-format on
DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, limit);
}
else
{
if(collection->params.query_flags & COLLECTION_QUERY_USE_LIMIT)
{
DT_DEBUG_SQLITE3_PREPARE_V2
(dt_database_get(darktable.db),
"SELECT imgid FROM memory.collected_images LIMIT -1, ?1",
-1, &stmt, NULL);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, limit);
}
else
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db),
"SELECT imgid FROM memory.collected_images",
-1, &stmt, NULL);
}
while(sqlite3_step(stmt) == SQLITE_ROW)
{
const dt_imgid_t imgid = sqlite3_column_int(stmt, 0);
list = g_list_prepend(list, GINT_TO_POINTER(imgid));
}
sqlite3_finalize(stmt);
}
return g_list_reverse(list); // list built in reverse order, so un-reverse it
}
GList *dt_collection_get_all(const dt_collection_t *collection, int limit)
{
return dt_collection_get(collection, limit, FALSE);
}
int dt_collection_get_nth(const dt_collection_t *collection, int nth)
{
if(nth < 0 || nth >= dt_collection_get_count(collection))
return -1;
const gchar *query = dt_collection_get_query(collection);
sqlite3_stmt *stmt = NULL;
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db), query, -1, &stmt, NULL);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, nth);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, 1);
int result = -1;
if(sqlite3_step(stmt) == SQLITE_ROW)
{
result = sqlite3_column_int(stmt, 0);
}
sqlite3_finalize(stmt);