00001
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "../config.h"
00023 #ifdef USE_LUA
00024 #include <gtk/gtk.h>
00025
00026 #include <lua.h>
00027 #include <lualib.h>
00028
00029 #include "guiapi.h"
00030 #endif
00031
00034 int lua_supported(void) {
00035 #ifdef USE_LUA
00036 return 1;
00037 #else
00038 return 0;
00039 #endif
00040 }
00041
00042
00043
00044 #ifdef USE_LUA
00045
00047 static lua_State *lua = NULL;
00048
00049
00050
00054 int lic_addQuery(lua_State* luaVM) {
00055 int args = lua_gettop(luaVM);
00056
00057 if ((args < 1) || (args > 2)) {
00058 printf("USAGE: addQuery([\"Title\", ] \"querytext\")\n");
00059 lua_pushnumber(luaVM, 1);
00060 return 1;
00061 }
00062 if (gui_connection_ok()) {
00063
00064 gui_add_query();
00065
00066
00067 gui_set_query_text((gchar *)lua_tostring(lua, -1));
00068 if (args == 2)
00069 gui_set_query_name((gchar *)lua_tostring(lua, -2));
00070 }
00071 lua_pushnumber(luaVM, 0);
00072 return 1;
00073 }
00074
00077 int lic_columnCount(lua_State* luaVM) {
00078 int count;
00079
00080
00081 if (lua_gettop(luaVM) != 0) {
00082 g_warning("USAGE: columnCount()\n");
00083 lua_pushnumber(luaVM, 1);
00084 return 1;
00085 }
00086
00087
00088 count = gui_get_result_columns();
00089
00090 lua_pushnumber(luaVM, count);
00091 return 1;
00092 }
00093
00098 int lic_connect(lua_State* luaVM) {
00099 DBConnection *conn;
00100
00101
00102 if (lua_gettop(luaVM) != 1) {
00103 g_warning("USAGE: connect(\"url\")");
00104 lua_pushnumber(luaVM, 1);
00105 return 1;
00106 }
00107
00108
00109 conn = db_connect_url((gchar *)lua_tostring(lua, -1));
00110 gui_set_connection(conn);
00111
00112
00113 gui_delete_all_queries();
00114
00115 lua_pushnumber(luaVM, 0);
00116 return 1;
00117 }
00118
00122 int lic_deleteQueries(lua_State* luaVM) {
00123
00124
00125
00126 if (lua_gettop(luaVM) != 0) {
00127 g_warning("USAGE: deleteQueries()\n");
00128 lua_pushnumber(luaVM, 1);
00129 return 1;
00130 }
00131
00132 gui_delete_all_queries();
00133
00134 lua_pushnumber(luaVM, 0);
00135 return 1;
00136 }
00137
00139 void disconnect_error(char *title, char *msg) {
00140 printf("%s\n%s\n\n", title, msg);
00141 }
00142
00146 int lic_disconnect(lua_State* luaVM) {
00147
00148 if (lua_gettop(luaVM) != 0) {
00149 g_warning("USAGE: disconnect()\n");
00150 lua_pushnumber(luaVM, 1);
00151 return 1;
00152 }
00153
00154 if (gui_connection_ok()) {
00155 gui_disconnect();
00156 }
00157 lua_pushnumber(luaVM, 0);
00158 return 1;
00159 }
00160
00163 int lic_exit(lua_State* luaVM) {
00164
00165 if (lua_gettop(luaVM) != 0) {
00166 g_warning("USAGE: exit()\n");
00167 lua_pushnumber(luaVM, 1);
00168 return 1;
00169 }
00170
00171
00172 if (running != 0)
00173 gtk_main_quit();
00174 else
00175 exit(0);
00176
00177 lua_pushnumber(luaVM, 0);
00178 return 1;
00179 }
00180
00183 int lic_getField(lua_State* luaVM) {
00184 int row;
00185 int column;
00186 char *s;
00187
00188
00189 if (lua_gettop(luaVM) != 2) {
00190 g_warning("USAGE: getField(row, column)\n");
00191 lua_pushnumber(luaVM, 1);
00192 return 1;
00193 }
00194
00195
00196 row = lua_tonumber(luaVM, 1);
00197 column = lua_tonumber(luaVM, 2);
00198 s = gui_get_result_field(row, column);
00199 lua_pushstring(lua, s);
00200 return 1;
00201 }
00202
00205 int lic_help(lua_State* luaVM) {
00206
00207 printf("GtkSQL scripting commands:\n");
00208 printf("addQuery([\"title\",]\"query text\")\n\tAdd a query containing the given.\n");
00209 printf("\ttext and (optional) title.\n");
00210 printf("columnCount()\n\tReturn the number of columns in the query result.\n");
00211 printf("connect(\"connectionurl\")\n\tConnect to the given URL.\n");
00212 printf("disconnect()\n\tDisconnects if a database is connected\n");
00213 printf("deleteQueries()\n\tClose all open queries.\n");
00214 printf("exit()\n\tExit the application.\n");
00215 printf("getField(row, column)\n\tReturn the field text for the row/column specified\n");
00216 printf("help()\n\tThis text.\n");
00217 printf("loadQuery(\"filename\")\n\tOpen a new query from the given file.\n");
00218 printf("moveWindow(x, y)\n\tMove the top-left corner of the window to the given location.\n");
00219 printf("rowCount()\n\tReturns the number of rows in the current result.\n");
00220 printf("runQuery(\"query\")\n\tRun either the given query, or the current query\nif no query is passed here.\n");
00221 printf("sizeWindow(width, height)\n\tSet the window to the given width and height.\n");
00222 printf("\n\nNOTE: All standard LUA functions are also available.\n\n");
00223
00224 lua_pushnumber(luaVM, 0);
00225 return 1;
00226 }
00227
00231 int lic_loadQuery(lua_State* luaVM) {
00232
00233 if (lua_gettop(luaVM) != 1) {
00234 g_warning("USAGE: loadQuery(\"filename\")\n");
00235 lua_pushnumber(luaVM, 1);
00236 return 1;
00237 }
00238
00239
00240 gui_add_query();
00241
00242
00243 gui_load_query_from_file((char *)lua_tostring(lua, -1));
00244
00245 lua_pushnumber(luaVM, 0);
00246 return 1;
00247 }
00248
00252 int lic_moveWindow(lua_State* luaVM) {
00253 int x;
00254 int y;
00255
00256
00257 if (lua_gettop(luaVM) != 2) {
00258 g_warning("USAGE: moveWindow(x, y)\n");
00259 lua_pushnumber(luaVM, 1);
00260 return 1;
00261 }
00262
00263 x = lua_tonumber(luaVM, 1);
00264 y = lua_tonumber(luaVM, 2);
00265
00266
00267 gtk_widget_set_uposition(GTK_WIDGET(mainWindow), x, y);
00268
00269 lua_pushnumber(luaVM, 0);
00270 return 1;
00271 }
00272
00275 int lic_rowCount(lua_State* luaVM) {
00276 int count;
00277
00278
00279 if (lua_gettop(luaVM) != 0) {
00280 g_warning("USAGE: recordCount()\n");
00281 lua_pushnumber(luaVM, 1);
00282 return 1;
00283 }
00284
00285 count = gui_get_result_rows();
00286
00287 lua_pushnumber(luaVM, count);
00288 return 1;
00289 }
00290
00294 int lic_runQuery(lua_State* luaVM) {
00295 int args = lua_gettop(luaVM);
00296
00297 if (args > 1) {
00298 g_warning("USAGE: runQuery(query)\n");
00299 lua_pushnumber(luaVM, 1);
00300 return 1;
00301 }
00302
00303 if (gui_connection_ok()) {
00304 if (args == 1) {
00305
00306 if (gui_get_query_count() == 0)
00307 gui_add_query();
00308
00309
00310 gui_set_query_text((gchar *)lua_tostring(lua, -1));
00311 }
00312 gui_send_query();
00313 lua_pushnumber(luaVM, 0);
00314 }
00315 else
00316 lua_pushnumber(luaVM, 1);
00317 return 1;
00318 }
00319
00322 int lic_sizeWindow(lua_State* luaVM) {
00323 int width;
00324 int height;
00325
00326
00327 if (lua_gettop(luaVM) != 2) {
00328 g_warning("USAGE: sizeWindow(widgth, height)\n");
00329 lua_pushnumber(luaVM, 1);
00330 return 1;
00331 }
00332
00333 width = lua_tonumber(luaVM, 1);
00334 height = lua_tonumber(luaVM, 2);
00335
00336
00337 gtk_widget_set_usize(GTK_WIDGET(mainWindow), width, height);
00338
00339 lua_pushnumber(luaVM, 0);
00340 return 1;
00341 }
00342
00343
00344
00345 void luaif_init(void) {
00346 if (lua != NULL)
00347 return;
00348
00349 lua = lua_open();
00350 if (lua == NULL) {
00351 g_warning("Could not initialise scripting engine.\n");
00352 return;
00353 }
00354
00355 luaopen_base(lua);
00356 lua_register(lua, "addQuery", lic_addQuery);
00357 lua_register(lua, "columnCount", lic_columnCount);
00358 lua_register(lua, "connect", lic_connect);
00359 lua_register(lua, "deleteQueries", lic_deleteQueries);
00360 lua_register(lua, "disconnect", lic_disconnect);
00361 lua_register(lua, "exit", lic_exit);
00362 lua_register(lua, "getField", lic_getField);
00363 lua_register(lua, "help", lic_help);
00364 lua_register(lua, "loadQuery", lic_loadQuery);
00365 lua_register(lua, "moveWindow", lic_moveWindow);
00366 lua_register(lua, "sizeWindow", lic_sizeWindow);
00367 lua_register(lua, "rowCount", lic_rowCount);
00368 lua_register(lua, "runQuery", lic_runQuery);
00369 }
00370
00372 void luaif_runfile(char *fn) {
00373 if (lua == NULL)
00374 return;
00375
00376 lua_dofile(lua, fn);
00377 }
00378
00380 void luaif_runstring(char *s) {
00381 if ((lua == NULL) || (s == NULL))
00382 return;
00383
00384 lua_dostring(lua, s);
00385 }
00386
00387 void luaif_shutdown(void) {
00388 if (lua == NULL)
00389 return;
00390
00391 lua_close(lua);
00392 }
00393
00394 #endif