2 * Copyright (C) 2008 - 2016 The Geeqie Team
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.
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.
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.
38 #include "ui_fileops.h"
41 static lua_State *L; /** The LUA object needed for all operations (NOTE: That is
42 * a upper-case variable to match the documentation!) */
44 static FileData *lua_check_image(lua_State *L, int index)
47 luaL_checktype(L, index, LUA_TUSERDATA);
48 fd = (FileData **)luaL_checkudata(L, index, "Image");
49 if (fd == NULL) luaL_typerror(L, index, "Image");
53 static int lua_image_get_exif(lua_State *L)
59 fd = lua_check_image(L, 1);
60 exif = exif_read_fd(fd);
62 exif_data = (ExifData **)lua_newuserdata(L, sizeof(ExifData *));
63 luaL_getmetatable(L, "Exif");
64 lua_setmetatable(L, -2);
71 static int lua_image_get_path(lua_State *L)
75 fd = lua_check_image(L, 1);
76 lua_pushstring(L, fd->path);
80 static int lua_image_get_name(lua_State *L)
84 fd = lua_check_image(L, 1);
85 lua_pushstring(L, fd->name);
89 static int lua_image_get_extension(lua_State *L)
93 fd = lua_check_image(L, 1);
94 lua_pushstring(L, fd->extension);
98 static int lua_image_get_date(lua_State *L)
102 fd = lua_check_image(L, 1);
103 lua_pushnumber(L, fd->date);
107 static int lua_image_get_size(lua_State *L)
111 fd = lua_check_image(L, 1);
112 lua_pushnumber(L, fd->size);
116 static int lua_image_get_marks(lua_State *L)
120 fd = lua_check_image(L, 1);
121 lua_pushnumber(L, fd->marks);
125 static ExifData *lua_check_exif(lua_State *L, int index)
128 luaL_checktype(L, index, LUA_TUSERDATA);
129 exif = (ExifData **)luaL_checkudata(L, index, "Exif");
130 if (exif == NULL) luaL_typerror(L, index, "Exif");
134 /* Interface for EXIF data */
135 static int lua_exif_get_datum(lua_State *L)
143 exif = lua_check_exif(L, 1);
144 key = luaL_checkstring(L, 2);
145 if (key == (gchar*)NULL || key[0] == '\0')
155 value = exif_get_data_as_text(exif, key);
156 if (strcmp(key, "Exif.Photo.DateTimeOriginal") == 0)
158 memset(&tm, 0, sizeof(tm));
159 if (value && strptime(value, "%Y:%m:%d %H:%M:%S", &tm))
161 datetime = mktime(&tm);
162 lua_pushnumber(L, datetime);
170 } // if (strcmp(key, "Exif.Photo.Da...
171 lua_pushstring(L, value);
176 * \brief Initialize the lua interpreter.
181 luaL_openlibs(L); /* Open all libraries for lua programms */
183 /* Now create custom methodes to do something */
184 static const luaL_Reg meta_methods[] = {
188 /* The Image metatable and methodes */
189 static const luaL_Reg image_methods[] = {
190 {"get_path", lua_image_get_path},
191 {"get_name", lua_image_get_name},
192 {"get_extension", lua_image_get_extension},
193 {"get_date", lua_image_get_date},
194 {"get_size", lua_image_get_size},
195 {"get_exif", lua_image_get_exif},
196 {"get_marks", lua_image_get_marks},
199 luaL_register(L, "Image", image_methods);
200 luaL_newmetatable(L, "Image");
201 luaL_register(L, NULL, meta_methods);
202 lua_pushliteral(L, "__index");
203 lua_pushvalue(L, -3);
205 lua_pushliteral(L, "__metatable");
206 lua_pushvalue(L, -3);
211 /* The Exif table and methodes */
212 static const luaL_Reg exif_methods[] = {
213 {"get_datum", lua_exif_get_datum},
216 luaL_register(L, "Exif", exif_methods);
217 luaL_newmetatable(L, "Exif");
218 luaL_register(L, NULL, meta_methods);
219 lua_pushliteral(L, "__index");
220 lua_pushvalue(L, -3);
222 lua_pushliteral(L, "__metatable");
223 lua_pushvalue(L, -3);
230 * \brief Call a lua function to get a single value.
232 gchar *lua_callvalue(FileData *fd, const gchar *file, const gchar *function)
238 FileData **image_data;
240 GError *error = NULL;
242 /* Collection Table (Dummy at the moment) */
244 lua_setglobal(L, "Collection");
247 image_data = (FileData **)lua_newuserdata(L, sizeof(FileData *));
248 luaL_getmetatable(L, "Image");
249 lua_setmetatable(L, -2);
250 lua_setglobal(L, "Image");
255 result = luaL_dostring(L, function);
259 dir = g_build_filename(get_rc_dir(), "lua", NULL);
260 path = g_build_filename(dir, file, NULL);
261 result = luaL_dofile(L, path);
268 data = g_strdup_printf("Error running lua script: %s", lua_tostring(L, -1));
271 data = g_strdup(lua_tostring(L, -1));
272 tmp = g_locale_to_utf8(data, strlen(data), NULL, NULL, &error);
275 log_printf("Error converting lua output from locale to UTF-8: %s\n", error->message);
281 data = g_strdup(tmp);
282 } // if (error) { ... } else
287 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */