From a766fe32decfe51a18547b8a3bf83b313eaa23ba Mon Sep 17 00:00:00 2001 From: Micke Prag Date: Thu, 23 Feb 2012 12:34:31 +0100 Subject: [PATCH] Add public function tdRemoveController() to telldus-core, see #108 --- telldus-core/client/telldus-core.cpp | 18 +++++++++++++++ telldus-core/client/telldus-core.h | 1 + .../service/ClientCommunicationHandler.cpp | 4 ++++ telldus-core/service/ControllerManager.cpp | 23 +++++++++++++++++++ telldus-core/service/ControllerManager.h | 1 + 5 files changed, 47 insertions(+) diff --git a/telldus-core/client/telldus-core.cpp b/telldus-core/client/telldus-core.cpp index f6fd6508..e3143fdc 100644 --- a/telldus-core/client/telldus-core.cpp +++ b/telldus-core/client/telldus-core.cpp @@ -758,4 +758,22 @@ int WINAPI tdSetControllerValue(int controllerId, const char *name, const char * return Client::getIntegerFromService(msg); } +/** + * This function removes a controller from the list + * of controllers. The controller must not be + * available (disconnected) for this to work. + * + * Added in version 2.1.2. + * @param controllerId The controller to remove + * @returns TELLSTICK_SUCCESS if the controller was + * removed, TELLSTICK_ERROR_NOT_FOUND if the controller was + * not found, and TELLSTICK_ERROR_PERMISSION_DENIED if the + * controller is still connected. + **/ +int WINAPI tdRemoveController(int controllerId) { + Message msg(L"tdRemoveController"); + msg.addArgument(controllerId); + return Client::getIntegerFromService(msg); +} + /* @} */ diff --git a/telldus-core/client/telldus-core.h b/telldus-core/client/telldus-core.h index c7a1fd56..c9b3d032 100644 --- a/telldus-core/client/telldus-core.h +++ b/telldus-core/client/telldus-core.h @@ -89,6 +89,7 @@ extern "C" { TELLSTICK_API int WINAPI tdController(int *controllerId, int *controllerType, char *name, int nameLen, int *available); TELLSTICK_API int WINAPI tdControllerValue(int controllerId, const char *name, char *value, int valueLen); TELLSTICK_API int WINAPI tdSetControllerValue(int controllerId, const char *name, const char *value); + TELLSTICK_API int WINAPI tdRemoveController(int controllerId); #ifdef __cplusplus } diff --git a/telldus-core/service/ClientCommunicationHandler.cpp b/telldus-core/service/ClientCommunicationHandler.cpp index 72eb6226..21159209 100644 --- a/telldus-core/service/ClientCommunicationHandler.cpp +++ b/telldus-core/service/ClientCommunicationHandler.cpp @@ -245,6 +245,10 @@ void ClientCommunicationHandler::parseMessage(const std::wstring &clientMessage, std::wstring value = TelldusCore::Message::takeString(&msg); (*intReturn) = d->controllerManager->setControllerValue(id, name, value); + } else if (function == L"tdRemoveController") { + int controllerId = TelldusCore::Message::takeInt(&msg); + (*intReturn) = d->controllerManager->removeController(controllerId); + } else{ (*intReturn) = TELLSTICK_ERROR_UNKNOWN; } diff --git a/telldus-core/service/ControllerManager.cpp b/telldus-core/service/ControllerManager.cpp index 210e7db0..8c88fba9 100644 --- a/telldus-core/service/ControllerManager.cpp +++ b/telldus-core/service/ControllerManager.cpp @@ -276,6 +276,29 @@ std::wstring ControllerManager::getControllerValue(int id, const std::wstring &n return L""; } +int ControllerManager::removeController(int id) { + TelldusCore::MutexLocker locker(&d->mutex); + + ControllerMap::iterator it = d->controllers.find(id); + if (it == d->controllers.end()) { + return TELLSTICK_ERROR_NOT_FOUND; + } + if (it->second.controller) { + //Still connected + return TELLSTICK_ERROR_PERMISSION_DENIED; + } + + int ret = d->settings.removeNode(Settings::Controller, id); + if (ret != TELLSTICK_SUCCESS) { + return ret; + } + + d->controllers.erase(it); + + //TODO: signal + return TELLSTICK_SUCCESS; +} + int ControllerManager::setControllerValue(int id, const std::wstring &name, const std::wstring &value) { TelldusCore::MutexLocker locker(&d->mutex); diff --git a/telldus-core/service/ControllerManager.h b/telldus-core/service/ControllerManager.h index 11cdcf4c..7e650323 100644 --- a/telldus-core/service/ControllerManager.h +++ b/telldus-core/service/ControllerManager.h @@ -23,6 +23,7 @@ public: std::wstring getControllers() const; std::wstring getControllerValue(int id, const std::wstring &name); + int removeController(int id); int setControllerValue(int id, const std::wstring &name, const std::wstring &value); private: