Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
selection_repository.c
Go to the documentation of this file.
1/*
2 This file is part of darktable,
3 Copyright (C) 2026 Aurélien PIERRE.
4
5 darktable is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 darktable is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with darktable. If not, see <http://www.gnu.org/licenses/>.
17*/
18
20
21#include "common/image.h" // for UNKNOWN_IMAGE
22#include "database/database.h"
23#include "database/sql_debug.h"
24#include "system/macros.h"
25#include "system/mem_alloc.h"
26
27#include <sqlite3.h>
28
29/* The read is on the hot path -- every selection change re-reads the whole table to
30 * rebuild the in-memory mirror -- so its statement is cached. The writes are one per user
31 * action and are not. */
32
33void dt_selection_repository_select(const int32_t imgid)
34{
35 if(imgid < 0) return;
36
37 gchar *query = g_strdup_printf("INSERT OR IGNORE INTO main.selected_images VALUES (%d)", imgid);
40}
41
42void dt_selection_repository_deselect(const int32_t imgid)
43{
44 if(imgid < 0) return;
45
46 gchar *query = g_strdup_printf("DELETE FROM main.selected_images WHERE imgid = %d", imgid);
49}
50
52{
53 if(IS_NULL_PTR(ids)) return;
54
55 gchar *query = g_strdup_printf("INSERT OR IGNORE INTO main.selected_images VALUES %s", ids);
58}
59
61{
62 if(IS_NULL_PTR(ids)) return;
63
64 gchar *query = g_strdup_printf("DELETE FROM main.selected_images WHERE imgid IN (%s)", ids);
67}
68
70{
71 DT_DEBUG_SQLITE3_EXEC(dt_database_get_sqlite3_global(), "DELETE FROM main.selected_images",
72 NULL, NULL, NULL);
73}
74
76{
77 /* Prepared per call: a cached statement here would be shared GUI-thread/worker-thread
78 * state with no lock, and two threads stepping one statement interleave both row
79 * cursors. Same rule as the rest of this module's uncached functions. */
81 // clang-format off
83 "SELECT imgid FROM main.selected_images ORDER BY imgid DESC",
84 -1, &stmt, NULL);
85 // clang-format on
86 if(IS_NULL_PTR(stmt)) return NULL;
87
88 GList *list = NULL;
91
93
94 /* Not reversed: the query orders DESC and prepending flips it, so the caller gets
95 * ascending order. That was the previous behaviour and the selection's in-memory mirror
96 * depends on it. */
97 return list;
98}
99
101{
102 // clang-format off
104 "DELETE FROM main.selected_images"
105 " WHERE imgid NOT IN"
106 " (SELECT imgid FROM memory.collected_images)", NULL, NULL, NULL);
107 // clang-format on
108}
109
111{
112 DT_DEBUG_SQLITE3_EXEC(dt_database_get_sqlite3_global(), "DELETE FROM memory.selected_backup",
113 NULL, NULL, NULL);
114 // clang-format off
116 "INSERT INTO memory.selected_backup"
117 " SELECT * FROM main.selected_images", NULL, NULL, NULL);
118 // clang-format on
119}
120
122{
123 DT_DEBUG_SQLITE3_EXEC(dt_database_get_sqlite3_global(), "DELETE FROM main.selected_images",
124 NULL, NULL, NULL);
125 // clang-format off
127 "INSERT INTO main.selected_images"
128 " SELECT * FROM memory.selected_backup", NULL, NULL, NULL);
129 // clang-format on
130}
131
133{
134 int32_t imgid = UNKNOWN_IMAGE;
137 "SELECT imgid FROM main.selected_images", -1, &stmt, NULL);
138 if(IS_NULL_PTR(stmt)) return imgid;
139
142
143 return imgid;
144}
145
147{
148 /* Nothing cached any more: every statement in this file is prepared and finalised per
149 * call. Kept because the connection's close order calls every repository's cleanup. */
150}
151
152// clang-format off
153// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
154// vim: shiftwidth=2 expandtab tabstop=2 cindent
155// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
156// clang-format on
sqlite3 * dt_database_get_sqlite3_global(void)
Definition database.c:3753
#define UNKNOWN_IMAGE
Definition image.h:77
#define IS_NULL_PTR(p)
C is way too permissive with !=, == and if(var) checks, which can mean too many things depending on w...
Definition macros.h:65
#define dt_free(ptr)
Definition mem_alloc.h:97
void dt_selection_repository_select(const int32_t imgid)
Add imgid to the selection. Selecting an already-selected image does nothing.
void dt_selection_repository_clear(void)
Empty the selection.
void dt_selection_repository_cleanup(void)
Finalise whatever this repository still caches – today, nothing. See dt_colorlabel_repository_cleanup...
void dt_selection_repository_pop(void)
Copy memory.selected_backup back over the selection.
void dt_selection_repository_deselect_list(const char *ids)
Remove every id in ids from the selection.
void dt_selection_repository_deselect(const int32_t imgid)
Remove imgid from the selection.
void dt_selection_repository_drop_uncollected(void)
Drop selected images that are no longer in the current collection.
GList * dt_selection_repository_get_all(void)
Every selected image id, GINT_TO_POINTER, ASCENDING. Free with g_list_free().
int32_t dt_selection_repository_get_lowest_id(void)
The lowest selected image id, or UNKNOWN_IMAGE when nothing is selected.
void dt_selection_repository_push(void)
Copy the selection into memory.selected_backup, replacing what was there.
void dt_selection_repository_select_list(const char *ids)
Add every id in ids to the selection.
main.selected_images, and the memory.selected_backup it is pushed onto.
Checked wrappers around the sqlite3_* calls: trace the statement, assert the return code,...
#define DT_DEBUG_SQLITE3_EXEC(a, b, c, d, e)
Definition sql_debug.h:129
#define DT_DEBUG_SQLITE3_PREPARE_V2(a, b, c, d, e)
Definition sql_debug.h:137