Fix untranslated text
[geeqie.git] / src / lua.cc
1 /*
2  * Copyright (C) 2008 - 2016 The Geeqie Team
3  *
4  * Author: Klaus Ethgen
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include <config.h>
22
23 #if HAVE_LUA
24
25 #define _XOPEN_SOURCE
26
27 #include "glua.h"
28
29 #include <unistd.h>
30
31 #include <cstring>
32 #include <ctime>
33 #include <memory>
34 #include <utility>
35
36 #include <glib.h>
37 #include <lua.hpp>
38
39 #include "debug.h"
40 #include "exif.h"
41 #include "filedata.h"
42 #include "main.h"
43 #include "ui-fileops.h"
44
45 struct ExifData;
46
47 /**
48  * @file
49  * User API consists of the following namespaces:
50  *
51  * @link image_methods Image:@endlink basic image information
52  *
53  * <b>Collection</b>: not implemented
54  *
55  * @link exif_methods <exif-structure>:get_datum() @endlink get single exif parameter
56  *
57  */
58
59 static lua_State *L; /** The LUA object needed for all operations (NOTE: That is
60                        * a upper-case variable to match the documentation!) */
61
62 /* Taking that definition from lua 5.1 source */
63 #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 502
64 int luaL_typerror(lua_State *L, int narg, const char *tname)
65 {
66         const char *msg = lua_pushfstring(L, "%s expected, got %s", tname, luaL_typename(L, narg));
67         return luaL_argerror(L, narg, msg);
68 }
69
70 # define LUA_register_meta(L, meta) luaL_setfuncs(L, meta, 0);
71 # define LUA_register_global(L, string, func) \
72         lua_newtable(L); \
73         luaL_setfuncs(L, func, 0); \
74         lua_pushvalue(L, -1); \
75         lua_setglobal(L, string)
76 #else
77 # define LUA_register_meta(L, meta) luaL_register(L, NULL, meta)
78 # define LUA_register_global(L, string, func) luaL_register(L, string, func)
79 #endif
80
81 static FileData *lua_check_image(lua_State *L, int index)
82 {
83         FileData **fd;
84         luaL_checktype(L, index, LUA_TUSERDATA);
85         fd = static_cast<FileData **>(luaL_checkudata(L, index, "Image"));
86         if (fd == nullptr) luaL_typerror(L, index, "Image");
87         return *fd;
88 }
89
90 /**
91  * @brief Get exif structure of selected image
92  * @param L
93  * @returns An #ExifData data structure containing the entire exif data
94  *
95  * To be used in conjunction with @link lua_exif_get_datum <exif-structure>:get_datum() @endlink
96  */
97 static int lua_image_get_exif(lua_State *L)
98 {
99         FileData *fd;
100         ExifData *exif;
101         ExifData **exif_data;
102
103         fd = lua_check_image(L, 1);
104         exif = exif_read_fd(fd);
105
106         exif_data = static_cast<ExifData **>(lua_newuserdata(L, sizeof(ExifData *)));
107         luaL_getmetatable(L, "Exif");
108         lua_setmetatable(L, -2);
109
110         *exif_data = exif;
111
112         return 1;
113 }
114
115 /**
116  * @brief Get full path of selected image
117  * @param L
118  * @returns char The full path of the file, including filename and extension
119  *
120  *
121  */
122 static int lua_image_get_path(lua_State *L)
123 {
124         FileData *fd;
125
126         fd = lua_check_image(L, 1);
127         lua_pushstring(L, fd->path);
128         return 1;
129 }
130
131 /**
132  * @brief Get full filename of selected image
133  * @param L
134  * @returns char The full filename including extension
135  *
136  *
137  */
138 static int lua_image_get_name(lua_State *L)
139 {
140         FileData *fd;
141
142         fd = lua_check_image(L, 1);
143         lua_pushstring(L, fd->name);
144         return 1;
145 }
146
147 /**
148  * @brief Get file extension of selected image
149  * @param L
150  * @returns char The file extension including preceding dot
151  *
152  *
153  */
154 static int lua_image_get_extension(lua_State *L)
155 {
156         FileData *fd;
157
158         fd = lua_check_image(L, 1);
159         lua_pushstring(L, fd->extension);
160         return 1;
161 }
162
163 /**
164  * @brief Get file date of selected image
165  * @param L
166  * @returns time_t The file date in Unix timestamp format.
167  *
168  * time_t - signed integer which represents the number of seconds since
169  * the start of the Unix epoch: midnight UTC of January 1, 1970
170  */
171 static int lua_image_get_date(lua_State *L)
172 {
173         FileData *fd;
174
175         fd = lua_check_image(L, 1);
176         lua_pushnumber(L, fd->date);
177         return 1;
178 }
179
180 /**
181  * @brief Get file size of selected image
182  * @param L
183  * @returns integer The file size in bytes
184  *
185  *
186  */
187 static int lua_image_get_size(lua_State *L)
188 {
189         FileData *fd;
190
191         fd = lua_check_image(L, 1);
192         lua_pushnumber(L, fd->size);
193         return 1;
194 }
195
196 /**
197  * @brief Get marks of selected image
198  * @param L
199  * @returns unsigned integer Bit map of marks set
200  *
201  * Bit 0 == Mark 1 etc.
202  *
203  *
204  */
205 static int lua_image_get_marks(lua_State *L)
206 {
207         FileData *fd;
208
209         fd = lua_check_image(L, 1);
210         lua_pushnumber(L, fd->marks);
211         return 1;
212 }
213
214 static ExifData *lua_check_exif(lua_State *L, int index)
215 {
216         ExifData **exif;
217         luaL_checktype(L, index, LUA_TUSERDATA);
218         exif = static_cast<ExifData **>(luaL_checkudata(L, index, "Exif"));
219         if (exif == nullptr) luaL_typerror(L, index, "Exif");
220         return *exif;
221 }
222
223 /**
224  * @brief Interface for EXIF data
225  * @param L
226  * @returns <i>return</i> A single exif tag extracted from a structure output by the @link lua_image_get_exif Image:get_exif() @endlink command
227  *
228  * e.g. \n
229  * exif_structure = Image:get_exif(); \n
230  * DateTimeDigitized = exif_structure:get_datum("Exif.Photo.DateTimeDigitized");
231  *
232  * Where <i>return</i> is: \n
233  * Exif.Photo.DateTimeOriginal = signed integer time_t \n
234  * Exif.Photo.DateTimeDigitized = signed integer time_t \n
235  * otherwise char
236  *
237  */
238 static int lua_exif_get_datum(lua_State *L)
239 {
240         const gchar *key;
241         gchar *value = nullptr;
242         ExifData *exif;
243         struct tm tm;
244         time_t datetime;
245
246         exif = lua_check_exif(L, 1);
247         key = luaL_checkstring(L, 2);
248         if (key == nullptr || key[0] == '\0')
249                 {
250                 lua_pushnil(L);
251                 return 1;
252                 }
253         if (!exif)
254                 {
255                 lua_pushnil(L);
256                 return 1;
257                 }
258         value = exif_get_data_as_text(exif, key);
259         if (strcmp(key, "Exif.Photo.DateTimeOriginal") == 0)
260                 {
261                 memset(&tm, 0, sizeof(tm));
262                 if (value && strptime(value, "%Y:%m:%d %H:%M:%S", &tm))
263                         {
264                         datetime = mktime(&tm);
265                         lua_pushnumber(L, datetime);
266                         return 1;
267                         }
268
269                 lua_pushnil(L);
270                 return 1;
271                 }
272
273         if (strcmp(key, "Exif.Photo.DateTimeDigitized") == 0)
274                 {
275                 memset(&tm, 0, sizeof(tm));
276                 if (value && strptime(value, "%Y:%m:%d %H:%M:%S", &tm))
277                         {
278                         datetime = mktime(&tm);
279                         lua_pushnumber(L, datetime);
280                         return 1;
281                         }
282
283                 lua_pushnil(L);
284                 return 1;
285                 }
286         lua_pushstring(L, value);
287         return 1;
288 }
289
290 /**
291  * @brief  <b>Image:</b> metatable and methods \n
292  * Call by e.g. \n
293  * path_name = @link lua_image_get_path Image:getpath() @endlink \n
294  * where the keyword <b>Image</b> represents the currently selected image
295  */
296 static const luaL_Reg image_methods[] = {
297                 {"get_path", lua_image_get_path},
298                 {"get_name", lua_image_get_name},
299                 {"get_extension", lua_image_get_extension},
300                 {"get_date", lua_image_get_date},
301                 {"get_size", lua_image_get_size},
302                 {"get_exif", lua_image_get_exif},
303                 {"get_marks", lua_image_get_marks},
304                 {nullptr, nullptr}
305 };
306
307 /**
308  * @brief  <b>exif:</b> table and methods \n
309  * Call by e.g. \n
310  * @link lua_exif_get_datum <exif-structure>:get_datum() @endlink \n
311  * where <exif-structure> is the output of @link lua_image_get_exif Image:get_exif() @endlink
312  *
313  * exif_structure = Image:get_exif(); \n
314  * DateTimeDigitized = exif_structure:get_datum("Exif.Photo.DateTimeDigitized");
315  */
316 static const luaL_Reg exif_methods[] = {
317                 {"get_datum", lua_exif_get_datum},
318                 {nullptr, nullptr}
319 };
320
321 /**
322  * @brief Initialize the lua interpreter.
323  */
324 void lua_init()
325 {
326         L = luaL_newstate();
327         luaL_openlibs(L); /* Open all libraries for lua programs */
328
329         /* Now create custom methodes to do something */
330         static const luaL_Reg meta_methods[] = {
331                         {nullptr, nullptr}
332         };
333
334         LUA_register_global(L, "Image", image_methods);
335         luaL_newmetatable(L, "Image");
336         LUA_register_meta(L, meta_methods);
337         lua_pushliteral(L, "__index");
338         lua_pushvalue(L, -3);
339         lua_settable(L, -3);
340         lua_pushliteral(L, "__metatable");
341         lua_pushvalue(L, -3);
342         lua_settable(L, -3);
343         lua_pop(L, 1);
344         lua_pop(L, 1);
345
346         LUA_register_global(L, "Exif", exif_methods);
347         luaL_newmetatable(L, "Exif");
348         LUA_register_meta(L, meta_methods);
349         lua_pushliteral(L, "__index");
350         lua_pushvalue(L, -3);
351         lua_settable(L, -3);
352         lua_pushliteral(L, "__metatable");
353         lua_pushvalue(L, -3);
354         lua_settable(L, -3);
355         lua_pop(L, 1);
356         lua_pop(L, 1);
357 }
358
359 /**
360  * @brief Call a lua function to get a single value.
361  */
362 gchar *lua_callvalue(FileData *fd, const gchar *file, const gchar *function)
363 {
364         std::unique_ptr<gchar, decltype(&g_free)> path{g_build_filename(get_rc_dir(), "lua", file, NULL), g_free};
365         if (access(path.get(), R_OK) == -1)
366                 {
367                 path.reset(g_build_filename(gq_bindir, file, NULL));
368                 if (access(path.get(), R_OK) == -1)
369                         {
370                         return g_strdup("");
371                         }
372                 }
373
374         /* Collection Table (Dummy at the moment) */
375         lua_newtable(L);
376         lua_setglobal(L, "Collection");
377
378         /* Current Image */
379         auto image_data = static_cast<FileData **>(lua_newuserdata(L, sizeof(FileData *)));
380         luaL_getmetatable(L, "Image");
381         lua_setmetatable(L, -2);
382         lua_setglobal(L, "Image");
383
384         *image_data = fd;
385
386         gint result;
387         if (file[0] == '\0')
388                 {
389                 result = luaL_dostring(L, function);
390                 }
391         else
392                 {
393                 result = luaL_dofile(L, path.get());
394                 }
395
396         if (result)
397                 {
398                 return g_strdup_printf("Error running lua script: %s", lua_tostring(L, -1));
399                 }
400
401         gchar *data = g_strdup(lua_tostring(L, -1));
402         GError *error = nullptr;
403         gchar *tmp = g_locale_to_utf8(data, strlen(data), nullptr, nullptr, &error);
404         if (error)
405                 {
406                 log_printf("Error converting lua output from locale to UTF-8: %s\n", error->message);
407                 g_error_free(error);
408                 }
409         else
410                 {
411                 std::swap(data, tmp);
412                 } // if (error) { ... } else
413         g_free(tmp);
414         return data;
415 }
416 #else
417 using dummy_variable = int;
418 #endif
419 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */