Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
example.c
Go to the documentation of this file.
1/*
2 This file is part of darktable,
3 Copyright (C) 2010-2011 Henrik Andersson.
4 Copyright (C) 2010-2012, 2014 johannes hanika.
5 Copyright (C) 2010 Stuart Henderson.
6 Copyright (C) 2010-2018, 2020 Tobias Ellinghaus.
7 Copyright (C) 2011 Robert Bieber.
8 Copyright (C) 2012 Christian Tellefsen.
9 Copyright (C) 2012-2014 Jérémy Rosen.
10 Copyright (C) 2012 Richard Wonka.
11 Copyright (C) 2012 Simon Spannagel.
12 Copyright (C) 2013-2014, 2018, 2020 Pascal Obry.
13 Copyright (C) 2014 Pascal de Bruijn.
14 Copyright (C) 2014-2016 Roman Lebedev.
15 Copyright (C) 2019 Denis Dyakov.
16 Copyright (C) 2019 Heiko Bauke.
17 Copyright (C) 2019 jakubfi.
18 Copyright (C) 2019 Philippe Weyland.
19 Copyright (C) 2022 Martin Bařinka.
20 Copyright (C) 2023, 2025 Aurélien PIERRE.
21 Copyright (C) 2023 Maurizio Paglia.
22 Copyright (C) 2023 Ricky Moon.
23
24 darktable is free software: you can redistribute it and/or modify
25 it under the terms of the GNU General Public License as published by
26 the Free Software Foundation, either version 3 of the License, or
27 (at your option) any later version.
28
29 darktable is distributed in the hope that it will be useful,
30 but WITHOUT ANY WARRANTY; without even the implied warranty of
31 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 GNU General Public License for more details.
33
34 You should have received a copy of the GNU General Public License
35 along with darktable. If not, see <http://www.gnu.org/licenses/>.
36*/
37
38#include "common/darktable.h"
40#include "common/image.h"
41#include "common/image_cache.h"
42#include "common/imageio.h"
44#include "control/conf.h"
45#include "control/control.h"
46#include "dtgtk/button.h"
47#include "dtgtk/paint.h"
48#include "gui/gtk.h"
50#include <stdio.h>
51#include <stdlib.h>
52
53DT_MODULE(2)
54
55typedef struct _email_attachment_t
56{
57 int32_t imgid; // The image id of exported image
58 gchar *file; // Full filename of exported image
60
61// saved params
67
68
69const char *name(const struct dt_imageio_module_storage_t *self)
70{
71 return _("send as email");
72}
73
74void *legacy_params(dt_imageio_module_storage_t *self, const void *const old_params,
75 const size_t old_params_size, const int old_version, const int new_version,
76 size_t *new_size)
77{
78 if(old_version == 1 && new_version == 2)
79 {
80 typedef struct dt_imageio_email_v1_t
81 {
82 char filename[1024];
83 GList *images;
84 } dt_imageio_email_v1_t;
85
87 dt_imageio_email_v1_t *o = (dt_imageio_email_v1_t *)old_params;
88
89 g_strlcpy(n->filename, o->filename, sizeof(n->filename));
90
91 *new_size = self->params_size(self);
92 return n;
93 }
94 return NULL;
95}
96
98{
99 *width = 1536;
100 *height = 1536;
101 return 1;
102}
103
104
108
110{
111 dt_free(self->gui_data);
112}
113
117
118int store(dt_imageio_module_storage_t *self, dt_imageio_module_data_t *sdata, const int32_t imgid,
119 dt_imageio_module_format_t *format, dt_imageio_module_data_t *fdata, const int num, const int total,
120 const gboolean high_quality, const gboolean upscale, const gboolean export_masks,
121 dt_colorspaces_color_profile_type_t icc_type, const gchar *icc_filename, dt_iop_color_intent_t icc_intent,
122 dt_export_metadata_t *metadata)
123{
125
126 _email_attachment_t *attachment = (_email_attachment_t *)g_malloc(sizeof(_email_attachment_t));
127 attachment->imgid = imgid;
128
129 /* construct a temporary file name */
130 char tmpdir[PATH_MAX] = { 0 };
131 dt_loc_get_tmp_dir(tmpdir, sizeof(tmpdir));
132
133 char dirname[PATH_MAX] = { 0 };
134 gboolean from_cache = FALSE;
135 dt_image_full_path(imgid, dirname, sizeof(dirname), &from_cache, __FUNCTION__);
136 gchar *filename = g_path_get_basename(dirname);
137
138 g_strlcpy(dirname, filename, sizeof(dirname));
139
140 dt_image_path_append_version(imgid, dirname, sizeof(dirname));
141
142 gchar *end = g_strrstr(dirname, ".") + 1;
143
144 if(end) *end = '\0';
145
146 g_strlcat(dirname, format->extension(fdata), sizeof(dirname));
147
148 // set exported filename
149
150 attachment->file = g_build_filename(tmpdir, dirname, (char *)NULL);
151
152 if(dt_imageio_export(imgid, attachment->file, format, fdata, high_quality, upscale, TRUE, export_masks, icc_type,
153 icc_filename, icc_intent, self, sdata, num, total, metadata) != 0)
154 {
155 fprintf(stderr, "[imageio_storage_email] could not export to file: `%s'!\n", attachment->file);
156 dt_control_log(_("could not export to file `%s'!"), attachment->file);
157 dt_free(attachment->file);
158 dt_free(attachment);
159 dt_free(filename);
160 return 1;
161 }
162
163 dt_control_log(ngettext("%d/%d exported to `%s'", "%d/%d exported to `%s'", num),
164 num, total, attachment->file);
165
166#ifdef _OPENMP // store can be called in parallel, so synch access to shared memory
167#pragma omp critical
168#endif
169 d->images = g_list_append(d->images, attachment);
170
171 dt_free(filename);
172
173 return 0;
174}
175
177{
178 return sizeof(dt_imageio_email_t) - sizeof(GList *);
179}
180
182{
183}
184
186{
188 return d;
189}
190
191int set_params(dt_imageio_module_storage_t *self, const void *params, const int size)
192{
193 if(size != self->params_size(self)) return 1;
194 return 0;
195}
196
198{
199 if(IS_NULL_PTR(params)) return;
200 dt_free(params);
201}
202
204{
206
207 const gchar *imageBodyFormat = " - %s (%s)\\n"; // filename, exif oneliner
208 const gint nb_images = g_list_length(d->images);
209 const gint argc = 5 + (2 * nb_images);
210
211 char **argv = g_malloc0(sizeof(char *) * (argc + 1));
212
213 gchar *body = NULL;
214
215 argv[0] = "xdg-email";
216 argv[1] = "--subject";
217 argv[2] = _("images exported from Ansel");
218 argv[3] = "--body";
219 int n = 5;
220
221 for(GList *iter = d->images; iter; iter = g_list_next(iter))
222 {
223 gchar exif[256] = { 0 };
224 _email_attachment_t *attachment = (_email_attachment_t *)iter->data;
225 gchar *filename = g_path_get_basename(attachment->file);
226 const dt_image_t *img = dt_image_cache_get(darktable.image_cache, attachment->imgid, 'r');
227 dt_image_print_exif(img, exif, sizeof(exif));
229
230 gchar *imgbody = g_strdup_printf(imageBodyFormat, filename, exif);
231 if (!IS_NULL_PTR(body)) {
232 gchar *body_bak = body;
233 body = g_strconcat(body_bak, imgbody, NULL);
234 dt_free(body_bak);
235 }
236 else
237 {
238 body = g_strdup(imgbody);
239 }
240 dt_free(imgbody);
241 dt_free(filename);
242
243 argv[n] = g_strdup("--attach");
244 // use attachment->file directly as we need to freed it, and this way it will be
245 // freed as part of the argument release after the spawn below.
246 argv[n+1] = attachment->file;
247 n += 2;
248 }
249 g_list_free_full(d->images, dt_free_gpointer);
250 d->images = NULL;
251
252 argv[4] = body;
253
254 argv[argc] = NULL;
255
256 fprintf(stderr, "[email] launching '");
257 for (int k=0; k<argc; k++) fprintf(stderr, " %s", argv[k]);
258 fprintf(stderr, "'\n");
259
260 gint exit_status = 0;
261
262 g_spawn_sync (NULL, argv, NULL, G_SPAWN_SEARCH_PATH | G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
263 NULL, NULL, NULL, NULL, &exit_status, NULL);
264
265 for (int k=4; k<argc; k++) dt_free(argv[k]);
266 dt_free(argv);
267
268 if(exit_status)
269 {
270 dt_control_log(_("could not launch email client!"));
271 }
272}
273
275{
276 const char *mime = format->mime(NULL);
277 if(mime[0] == '\0') // this seems to be the copy format
278 return 0;
279
280 return 1;
281}
282
283// clang-format off
284// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
285// vim: shiftwidth=2 expandtab tabstop=2 cindent
286// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
287// clang-format on
#define TRUE
Definition ashift_lsd.c:162
#define FALSE
Definition ashift_lsd.c:158
const char * mime(dt_imageio_module_data_t *data)
Definition avif.c:640
int width
Definition bilateral.h:1
int height
Definition bilateral.h:1
dt_iop_color_intent_t
Definition colorspaces.h:63
dt_colorspaces_color_profile_type_t
Definition colorspaces.h:81
void dt_image_path_append_version(const int32_t imgid, char *pathname, size_t pathname_len)
void dt_image_print_exif(const dt_image_t *img, char *line, size_t line_len)
void dt_image_full_path(const int32_t imgid, char *pathname, size_t pathname_len, gboolean *from_cache, const char *calling_func)
Get the full path of an image out of the database.
char * name
void dt_control_log(const char *msg,...)
Definition control.c:761
darktable_t darktable
Definition darktable.c:181
#define DT_MODULE(MODVER)
Definition darktable.h:140
static void dt_free_gpointer(gpointer ptr)
Definition darktable.h:463
#define dt_free(ptr)
Definition darktable.h:456
#define DT_MAX_PATH_FOR_PARAMS
Definition darktable.h:1071
#define PATH_MAX
Definition darktable.h:1062
#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 darktable.h:281
void * get_params(dt_imageio_module_storage_t *self)
Definition example.c:185
void finalize_store(dt_imageio_module_storage_t *self, dt_imageio_module_data_t *params)
Definition example.c:203
int recommended_dimension(struct dt_imageio_module_storage_t *self, dt_imageio_module_data_t *data, uint32_t *width, uint32_t *height)
Definition example.c:97
void gui_cleanup(dt_imageio_module_storage_t *self)
Definition example.c:109
void free_params(dt_imageio_module_storage_t *self, dt_imageio_module_data_t *params)
Definition example.c:197
int store(dt_imageio_module_storage_t *self, dt_imageio_module_data_t *sdata, const int32_t imgid, dt_imageio_module_format_t *format, dt_imageio_module_data_t *fdata, const int num, const int total, const gboolean high_quality, const gboolean upscale, const gboolean export_masks, dt_colorspaces_color_profile_type_t icc_type, const gchar *icc_filename, dt_iop_color_intent_t icc_intent, dt_export_metadata_t *metadata)
Definition example.c:118
size_t params_size(dt_imageio_module_storage_t *self)
Definition example.c:176
void gui_init(dt_imageio_module_storage_t *self)
Definition example.c:105
int supported(struct dt_imageio_module_storage_t *storage, struct dt_imageio_module_format_t *format)
Definition example.c:274
void init(dt_imageio_module_storage_t *self)
Definition example.c:181
void gui_reset(dt_imageio_module_storage_t *self)
Definition example.c:114
void * legacy_params(dt_imageio_module_storage_t *self, const void *const old_params, const size_t old_params_size, const int old_version, const int new_version, size_t *new_size)
Definition example.c:74
int set_params(dt_imageio_module_storage_t *self, const void *params, const int size)
Definition example.c:191
void dt_loc_get_tmp_dir(char *tmpdir, size_t bufsize)
void dt_image_cache_read_release(dt_image_cache_t *cache, const dt_image_t *img)
dt_image_t * dt_image_cache_get(dt_image_cache_t *cache, const int32_t imgid, char mode)
int dt_imageio_export(const int32_t imgid, const char *filename, dt_imageio_module_format_t *format, dt_imageio_module_data_t *format_params, const gboolean high_quality, const gboolean copy_metadata, const gboolean export_masks, dt_colorspaces_color_profile_type_t icc_type, const gchar *icc_filename, dt_iop_color_intent_t icc_intent, dt_imageio_module_storage_t *storage, dt_imageio_module_data_t *storage_params, int num, int total, dt_export_metadata_t *metadata)
Definition imageio.c:765
float *const restrict const size_t k
size_t size
Definition mipmap_cache.c:3
struct dt_image_cache_t * image_cache
Definition darktable.h:777
char filename[DT_MAX_PATH_FOR_PARAMS]
Definition example.c:64