From 7d46e610c0c93d68cb28e51f42691b3a404f2f7e Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Mon, 20 Jul 2015 20:35:35 +0000 Subject: MirLua: - added iterators in m_database and m_protocols - added more short aliases for functions in m_database - added some examples in m_database - version bump git-svn-id: http://svn.miranda-ng.org/main/trunk@14590 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/MirLua/docs/examples/database.lua | 29 ++++- plugins/MirLua/src/m_database.cpp | 201 ++++++++++++++++++++++++++---- plugins/MirLua/src/m_protocols.cpp | 66 ++++++++++ plugins/MirLua/src/mlua_script_loader.cpp | 2 +- plugins/MirLua/src/mlua_script_loader.h | 4 +- plugins/MirLua/src/version.h | 2 +- 6 files changed, 272 insertions(+), 32 deletions(-) (limited to 'plugins') diff --git a/plugins/MirLua/docs/examples/database.lua b/plugins/MirLua/docs/examples/database.lua index 93c17cb818..033293e5c7 100644 --- a/plugins/MirLua/docs/examples/database.lua +++ b/plugins/MirLua/docs/examples/database.lua @@ -1,23 +1,44 @@ --- include m_database module local db = require('m_database') +--- Iterate all contact stored in db +-- @param protoName The name of protocol account or nothing +for hContact in db.AllContacts() do + --print(hContact) +end + +local hContact = 15 + +--- Iterate all contact events stored in db +-- param hContact The handle of contact +for hEvent in db.AllEvents(hContact) do + --print(hEvent) +end + +--- Iterate all setting names stored in db +-- param module The name of module +-- param hContact The handle of contact or nothing +for setting in db.AllSettings('CList') do + --print(setting) +end + --- Save value to database -- @param hContact The handle of contact (can be NULL) -- @param module The name of section -- @param setting The name of setting -- @return value The value -db.WriteContactSetting(nil, 'MirLua', 'testNum', -2342) +db.WriteSetting(nil, 'MirLua', 'testNum', -2342) --- Return value from database -- @param hContact The handle of contact (can be NULL) -- @param module The name of section -- @param setting The name of setting -- @param default The value which will be returned if setting doesn't not exists -local bValue = db.GetContactSetting(nil, 'MirLua', 'testByte'); +local bValue = db.GetSetting(nil, 'MirLua', 'testByte'); -- print string value if bool value is true if bValue then - local sValue = db.GetContactSetting(nil, 'MirLua', 'testString', 'Hello!') + local sValue = db.GetSetting(nil, 'MirLua', 'testString', 'Hello!') print(sValue) end @@ -25,4 +46,4 @@ if bValue then -- @param hContact The handle of contact (can be NULL) -- @param module The name of section -- @param setting The name of setting -db.DeleteContactSetting(nil, 'MirLua', 'testNum'); +db.DeleteSetting(nil, 'MirLua', 'testNum'); diff --git a/plugins/MirLua/src/m_database.cpp b/plugins/MirLua/src/m_database.cpp index a1cf6f1dc6..537322d5f4 100644 --- a/plugins/MirLua/src/m_database.cpp +++ b/plugins/MirLua/src/m_database.cpp @@ -12,7 +12,7 @@ static int lua_FindFirstContact(lua_State *L) static int lua_FindNextContact(lua_State *L) { - MCONTACT hContact = lua_tointeger(L, 1); + MCONTACT hContact = luaL_checkinteger(L, 1); const char *szProto = lua_tostring(L, 2); MCONTACT res = db_find_next(hContact, szProto); @@ -21,9 +21,43 @@ static int lua_FindNextContact(lua_State *L) return 1; } +static int lua_ContactIterator(lua_State *L) +{ + MCONTACT hContact = lua_tointeger(L, lua_upvalueindex(1)); + const char *szProto = lua_tostring(L, lua_upvalueindex(2)); + + hContact = hContact == NULL + ? db_find_first(szProto) + : db_find_next(hContact); + + if (hContact) + { + lua_pushinteger(L, hContact); + lua_pushvalue(L, -1); + lua_replace(L, lua_upvalueindex(1)); + } + else + lua_pushnil(L); + + return 1; +} + +static int lua_AllContacts(lua_State *L) +{ + const char *szProto = lua_tostring(L, 1); + + lua_pushinteger(L, NULL); + lua_pushstring(L, szProto); + lua_pushcclosure(L, lua_ContactIterator, 2); + + return 1; +} + +/***********************************************/ + static int lua_GetEventCount(lua_State *L) { - MCONTACT hContact = lua_tointeger(L, 1); + MCONTACT hContact = luaL_checkinteger(L, 1); int res = ::db_event_count(hContact); lua_pushinteger(L, res); @@ -33,7 +67,7 @@ static int lua_GetEventCount(lua_State *L) static int lua_GetFirstEvent(lua_State *L) { - MCONTACT hContact = lua_tointeger(L, 1); + MCONTACT hContact = luaL_checkinteger(L, 1); MEVENT res = ::db_event_first(hContact); lua_pushinteger(L, res); @@ -43,8 +77,8 @@ static int lua_GetFirstEvent(lua_State *L) static int lua_GetPrevEvent(lua_State *L) { - MCONTACT hContact = lua_tointeger(L, 1); - MEVENT hEvent = lua_tointeger(L, 2); + MCONTACT hContact = luaL_checkinteger(L, 1); + MEVENT hEvent = luaL_checkinteger(L, 2); MEVENT res = ::db_event_prev(hContact, hEvent); lua_pushinteger(L, res); @@ -54,8 +88,8 @@ static int lua_GetPrevEvent(lua_State *L) static int lua_GetNextEvent(lua_State *L) { - MCONTACT hContact = lua_tointeger(L, 1); - MEVENT hEvent = lua_tointeger(L, 2); + MCONTACT hContact = luaL_checkinteger(L, 1); + MEVENT hEvent = luaL_checkinteger(L, 2); MEVENT res = ::db_event_next(hContact, hEvent); lua_pushinteger(L, res); @@ -65,7 +99,7 @@ static int lua_GetNextEvent(lua_State *L) static int lua_GetLastEvent(lua_State *L) { - MCONTACT hContact = lua_tointeger(L, 1); + MCONTACT hContact = luaL_checkinteger(L, 1); MEVENT res = ::db_event_last(hContact); lua_pushinteger(L, res); @@ -75,7 +109,7 @@ static int lua_GetLastEvent(lua_State *L) static int lua_GetEvent(lua_State *L) { - MEVENT hEvent = lua_tointeger(L, 1); + MEVENT hEvent = luaL_checkinteger(L, 1); DBEVENTINFO dbei = { sizeof(DBEVENTINFO) }; dbei.cbBlob = db_event_getBlobSize(hEvent); @@ -119,7 +153,73 @@ static int lua_GetEvent(lua_State *L) return 1; } -static int lua_WriteContactSetting(lua_State *L) +static int lua_EventIterator(lua_State *L) +{ + MCONTACT hContact = luaL_checkinteger(L, lua_upvalueindex(1)); + MEVENT hEvent = luaL_checkinteger(L, lua_upvalueindex(2)); + + hEvent = hEvent == NULL + ? db_event_first(hContact) + : db_event_next(hContact, hEvent); + + if (hEvent) + { + lua_pushinteger(L, hContact); + lua_pushvalue(L, -1); + lua_replace(L, lua_upvalueindex(2)); + } + else + lua_pushnil(L); + + return 1; +} + +static int lua_AllEvents(lua_State *L) +{ + MCONTACT hContact = luaL_checkinteger(L, 1); + + lua_pushinteger(L, hContact); + lua_pushinteger(L, NULL); + lua_pushcclosure(L, lua_EventIterator, 2); + + return 1; +} + +static int lua_EventReverseIterator(lua_State *L) +{ + MCONTACT hContact = luaL_checkinteger(L, lua_upvalueindex(1)); + MEVENT hEvent = luaL_checkinteger(L, lua_upvalueindex(2)); + + hEvent = hEvent == NULL + ? db_event_last(hContact) + : db_event_prev(hContact, hEvent); + + if (hEvent) + { + lua_pushinteger(L, hContact); + lua_pushvalue(L, -1); + lua_replace(L, lua_upvalueindex(2)); + } + else + lua_pushnil(L); + + return 1; +} + +static int lua_AllEventsFromEnd(lua_State *L) +{ + MCONTACT hContact = luaL_checkinteger(L, 1); + + lua_pushinteger(L, hContact); + lua_pushinteger(L, NULL); + lua_pushcclosure(L, lua_EventReverseIterator, 2); + + return 1; +} + +/***********************************************/ + +static int lua_WriteSetting(lua_State *L) { MCONTACT hContact = lua_tointeger(L, 1); LPCSTR szModule = luaL_checkstring(L, 2); @@ -153,7 +253,7 @@ static int lua_WriteContactSetting(lua_State *L) return 1; } -static int lua_GetContactSetting(lua_State *L) +static int lua_GetSetting(lua_State *L) { MCONTACT hContact = lua_tointeger(L, 1); LPCSTR szModule = luaL_checkstring(L, 2); @@ -200,7 +300,7 @@ static int lua_GetContactSetting(lua_State *L) typedef struct { - int arrlen; + int count; char **pszSettingName; } enumDBSettingsParam; @@ -211,17 +311,62 @@ static int SettingsEnumProc(const char* szSetting, LPARAM lParam) { enumDBSettingsParam* p = (enumDBSettingsParam*)lParam; - p->arrlen++; - p->pszSettingName = (char**)mir_realloc(p->pszSettingName, p->arrlen * sizeof(char*)); - p->pszSettingName[p->arrlen - 1] = mir_strdup(szSetting); + p->count++; + p->pszSettingName = (char**)mir_realloc(p->pszSettingName, p->count * sizeof(char*)); + p->pszSettingName[p->count - 1] = mir_strdup(szSetting); } return 0; } +static int lua_SettingIterator(lua_State *L) +{ + int i = lua_tointeger(L, lua_upvalueindex(1)); + enumDBSettingsParam* param = (enumDBSettingsParam*)lua_touserdata(L, lua_upvalueindex(2)); + + if (i < param->count) + { + lua_pushinteger(L, (i + 1)); + lua_replace(L, lua_upvalueindex(1)); + lua_pushstring(L, ptrA(mir_utf8encode(param->pszSettingName[i]))); + mir_free(param->pszSettingName[i]); + } + else + { + lua_pushnil(L); + mir_free(param->pszSettingName); + //mir_free(param); + } + + return 1; +} + +static int lua_AllSettings(lua_State *L) +{ + const char* szModule = luaL_checkstring(L, 1); + MCONTACT hContact = lua_tointeger(L, 2); + + enumDBSettingsParam* param = (enumDBSettingsParam*)mir_alloc(sizeof(enumDBSettingsParam*)); + param->count = 0; + param->pszSettingName = NULL; + + DBCONTACTENUMSETTINGS dbces = { 0 }; + dbces.pfnEnumProc = SettingsEnumProc; + dbces.szModule = szModule; + dbces.ofsSettings = 0; + dbces.lParam = (LPARAM)param; + ::CallService(MS_DB_CONTACT_ENUMSETTINGS, hContact, (LPARAM)&dbces); + + lua_pushinteger(L, 0); + lua_pushlightuserdata(L, param); + lua_pushcclosure(L, lua_SettingIterator, 2); + + return 1; +} + static int lua_EnumSettings(lua_State *L) { - MCONTACT hContact = lua_tointeger(L, 1); - LPCSTR szModule = luaL_checkstring(L, 2); + LPCSTR szModule = luaL_checkstring(L, 1); + MCONTACT hContact = lua_tointeger(L, 2); if (!lua_isfunction(L, 3)) { @@ -241,7 +386,7 @@ static int lua_EnumSettings(lua_State *L) dbces.lParam = (LPARAM)¶m; INT_PTR res = ::CallService(MS_DB_CONTACT_ENUMSETTINGS, hContact, (LPARAM)&dbces); - for (int i = 0; i < param.arrlen; i++) + for (int i = 0; i < param.count; i++) { lua_rawgeti(L, LUA_REGISTRYINDEX, ref); lua_pushstring(L, mir_utf8encode(param.pszSettingName[i])); @@ -258,7 +403,7 @@ static int lua_EnumSettings(lua_State *L) return 1; } -static int lua_DeleteContactSetting(lua_State *L) +static int lua_DeleteSetting(lua_State *L) { MCONTACT hContact = lua_tointeger(L, 1); LPCSTR szModule = luaL_checkstring(L, 2); @@ -333,7 +478,7 @@ static int SettingsChangedHookEventObjParam(void *obj, WPARAM wParam, LPARAM lPa return res; } -static int lua_OnContactSettingChanged(lua_State *L) +static int lua_OnSettingChanged(lua_State *L) { if (!lua_isfunction(L, 1)) { @@ -398,6 +543,7 @@ static luaL_Reg databaseApi[] = { { "FindFirstContact", lua_FindFirstContact }, { "FindNextContact", lua_FindNextContact }, + { "AllContacts", lua_AllContacts }, { "GetEventCount", lua_GetEventCount }, @@ -405,18 +551,25 @@ static luaL_Reg databaseApi[] = { "GetPrevEvent", lua_GetPrevEvent }, { "GetNextEvent", lua_GetNextEvent }, { "GetLastEvent", lua_GetLastEvent }, + { "AllEvents", lua_AllEvents }, + { "GetEventsFromEnd", lua_AllEventsFromEnd }, { "GetEvent", lua_GetEvent }, - { "WriteContactSetting", lua_WriteContactSetting }, + { "WriteContactSetting", lua_WriteSetting }, + { "WriteSetting", lua_WriteSetting }, - { "GetContactSetting", lua_GetContactSetting }, + { "GetContactSetting", lua_GetSetting }, + { "GetSetting", lua_GetSetting }, + { "AllSettings", lua_AllSettings }, { "EnumSettings", lua_EnumSettings }, - { "DeleteContactSetting", lua_DeleteContactSetting }, + { "DeleteContactSetting", lua_DeleteSetting }, + { "DeleteSetting", lua_DeleteSetting }, { "DeleteModule", lua_DeleteModule }, - { "OnContactSettingChanged", lua_OnContactSettingChanged }, + { "OnContactSettingChanged", lua_OnSettingChanged }, + { "OnSettingChanged", lua_OnSettingChanged }, { "DecodeDBCONTACTWRITESETTING", lua_DecodeDBCONTACTWRITESETTING }, { NULL, NULL } diff --git a/plugins/MirLua/src/m_protocols.cpp b/plugins/MirLua/src/m_protocols.cpp index 9e121b8a66..c095f44470 100644 --- a/plugins/MirLua/src/m_protocols.cpp +++ b/plugins/MirLua/src/m_protocols.cpp @@ -25,6 +25,38 @@ static int lua_GetProto(lua_State *L) return 1; } +static int lua_ProtoIterator(lua_State *L) +{ + int i = lua_tointeger(L, lua_upvalueindex(1)); + int count = lua_tointeger(L, lua_upvalueindex(2)); + PROTOCOLDESCRIPTOR** protos = (PROTOCOLDESCRIPTOR**)lua_touserdata(L, lua_upvalueindex(3)); + + if (i < count) + { + lua_pushinteger(L, (i + 1)); + lua_replace(L, lua_upvalueindex(1)); + MapToTable(L, protos[i]); + } + else + lua_pushnil(L); + + return 1; +} + +static int lua_AllProtos(lua_State *L) +{ + int count; + PROTOCOLDESCRIPTOR** protos; + Proto_EnumProtocols(&count, &protos); + + lua_pushinteger(L, 0); + lua_pushinteger(L, count); + lua_pushlightuserdata(L, protos); + lua_pushcclosure(L, lua_ProtoIterator, 3); + + return 1; +} + static int lua_EnumProtos(lua_State *L) { if (!lua_isfunction(L, 1)) @@ -94,6 +126,38 @@ static int lua_GetAccount(lua_State *L) return 1; } +static int lua_AccountIterator(lua_State *L) +{ + int i = lua_tointeger(L, lua_upvalueindex(1)); + int count = lua_tointeger(L, lua_upvalueindex(2)); + PROTOACCOUNT** accounts = (PROTOACCOUNT**)lua_touserdata(L, lua_upvalueindex(3)); + + if (i < count) + { + lua_pushinteger(L, (i + 1)); + lua_replace(L, lua_upvalueindex(1)); + MapToTable(L, accounts[i]); + } + else + lua_pushnil(L); + + return 1; +} + +static int lua_AllAccounts(lua_State *L) +{ + int count; + PROTOACCOUNT** accounts; + Proto_EnumAccounts(&count, &accounts); + + lua_pushinteger(L, 0); + lua_pushinteger(L, count); + lua_pushlightuserdata(L, accounts); + lua_pushcclosure(L, lua_AccountIterator, 3); + + return 1; +} + static int lua_EnumAccounts(lua_State *L) { if (!lua_isfunction(L, 1)) @@ -126,9 +190,11 @@ static int lua_EnumAccounts(lua_State *L) static luaL_Reg protocolsApi[] = { { "GetProto", lua_GetProto }, + { "AllProtos", lua_AllProtos }, { "EnumProtos", lua_EnumProtos }, { "GetAccount", lua_GetAccount }, + { "AllAccounts", lua_AllAccounts }, { "EnumAccounts", lua_EnumAccounts }, { NULL, NULL } diff --git a/plugins/MirLua/src/mlua_script_loader.cpp b/plugins/MirLua/src/mlua_script_loader.cpp index bdcca3c9f4..d26ccd1e90 100644 --- a/plugins/MirLua/src/mlua_script_loader.cpp +++ b/plugins/MirLua/src/mlua_script_loader.cpp @@ -20,7 +20,7 @@ void CLuaScriptLoader::RegisterScriptsFolder(const char *path) void CLuaScriptLoader::LoadScript(const TCHAR *path, int iGroup) { - CMLuaScript *script = new CMLuaScript(L, path, 0); + CMLuaScript *script = new CMLuaScript(L, path, iGroup); g_mLua->Scripts.insert(script); if (script->Load()) diff --git a/plugins/MirLua/src/mlua_script_loader.h b/plugins/MirLua/src/mlua_script_loader.h index 7088706387..d70d481f3a 100644 --- a/plugins/MirLua/src/mlua_script_loader.h +++ b/plugins/MirLua/src/mlua_script_loader.h @@ -11,8 +11,8 @@ private: void RegisterScriptsFolder(const char *path); - void LoadScript(const TCHAR *path, int iGroup); - void LoadScripts(const TCHAR *scriptDir, int iGroup); + void LoadScript(const TCHAR *path, int iGroup = 0); + void LoadScripts(const TCHAR *scriptDir, int iGroup = 0); public: static void Load(lua_State *L); diff --git a/plugins/MirLua/src/version.h b/plugins/MirLua/src/version.h index 48f4e6cac3..e71e5aa79c 100644 --- a/plugins/MirLua/src/version.h +++ b/plugins/MirLua/src/version.h @@ -1,7 +1,7 @@ #define __MAJOR_VERSION 0 #define __MINOR_VERSION 11 #define __RELEASE_NUM 4 -#define __BUILD_NUM 0 +#define __BUILD_NUM 1 #include -- cgit v1.2.3