From 21379bcb3f434a3233a2c3932a5b24844141300f Mon Sep 17 00:00:00 2001 From: Volker Birk Date: Wed, 24 Oct 2018 13:10:43 +0200 Subject: [PATCH] allow UserInterface on one thread --- src/user_interface.cc | 24 +++++++++++++++++------- src/user_interface.hh | 10 +++++----- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/user_interface.cc b/src/user_interface.cc index 47bfa86..44838be 100644 --- a/src/user_interface.cc +++ b/src/user_interface.cc @@ -7,8 +7,22 @@ namespace pEp { namespace PythonAdapter { + UserInterface *UserInterface::_ui = nullptr; + + UserInterface::UserInterface() + { + if (_ui) + throw runtime_error("only one UserInterface thread allowed"); + _ui = this; + } + + UserInterface::~UserInterface() + { + _ui = nullptr; + } + UserInterface_callback::UserInterface_callback(PyObject *self) : - _self(self) + UserInterface(), _self(self) { adapter.ui_object(self); PEP_STATUS status = ::register_sync_callbacks(adapter.session(), @@ -23,19 +37,15 @@ namespace pEp { ::unregister_sync_callbacks(adapter.session()); } - PEP_STATUS UserInterface::_notifyHandshake(void *obj, + PEP_STATUS UserInterface::_notifyHandshake( pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal ) { - if (!obj) - return PEP_SEND_FUNCTION_NOT_REGISTERED; - if (!(me && partner)) return PEP_ILLEGAL_VALUE; - auto that = dynamic_cast< UserInterface_callback * >( - static_cast< UserInterface * > (obj)); + auto that = dynamic_cast< UserInterface_callback * >(_ui); that->notifyHandshake(Identity(me), Identity(partner), signal); return PEP_STATUS_OK; diff --git a/src/user_interface.hh b/src/user_interface.hh index a2d52f4..9aa3681 100644 --- a/src/user_interface.hh +++ b/src/user_interface.hh @@ -10,9 +10,10 @@ namespace pEp { namespace PythonAdapter { class UserInterface { + static UserInterface *_ui; public: - UserInterface() { } - virtual ~UserInterface() { } + UserInterface(); + virtual ~UserInterface(); virtual void notifyHandshake( pEp::PythonAdapter::Identity me, @@ -26,14 +27,13 @@ namespace pEp { pEp::PythonAdapter::Identity partner, int result); protected: - static PEP_STATUS _notifyHandshake(void *obj, pEp_identity *me, + static PEP_STATUS _notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal); static SYNC_EVENT retrieve_next_sync_event(void *management, time_t threshold); }; class UserInterface_callback : public UserInterface { - PyObject *_self; - + PyObject *_self; public: UserInterface_callback(PyObject *self); ~UserInterface_callback();