diff options
author | Jan-Marek Glogowski <glogow@fbihome.de> | 2019-03-23 04:05:04 +0000 |
---|---|---|
committer | Jan-Marek Glogowski <glogow@fbihome.de> | 2019-04-04 14:19:43 +0200 |
commit | 810e62b8ba9c0fa5152592ef6b01400bfcfe59c0 (patch) | |
tree | dcdf418abba843fa154acbe7e22e7a016c9e3469 /vcl/qt5 | |
parent | ae1e23651a6d2e97d39bbe46bab83b7982d19611 (diff) |
Qt5 / KDE5 refactor create_SalInstance
Move most common code into Qt5Instance (static) functions.
Not much saved with regard to LOC, but since the code was
95% C'n'P, I hope it'll be less error prone this way.
Also handle all QApplication argument pointers via
std::unique_ptr from the beginning.
Change-Id: I87b2f571d398db7ccce3e740f16205b251be4ced
Reviewed-on: https://gerrit.libreoffice.org/69578
Tested-by: Jenkins
Reviewed-by: Aleksei Nikiforov <darktemplar@basealt.ru>
Diffstat (limited to 'vcl/qt5')
-rw-r--r-- | vcl/qt5/Qt5Instance.cxx | 104 |
1 files changed, 60 insertions, 44 deletions
diff --git a/vcl/qt5/Qt5Instance.cxx b/vcl/qt5/Qt5Instance.cxx index f0d42666e3ce..dd0edddc97cf 100644 --- a/vcl/qt5/Qt5Instance.cxx +++ b/vcl/qt5/Qt5Instance.cxx @@ -200,10 +200,11 @@ void Qt5Instance::ImplRunInMain() (void)this; // suppress unhelpful [loplugin:staticmethods]; can't be static } -Qt5Instance::Qt5Instance(bool bUseCairo) +Qt5Instance::Qt5Instance(std::unique_ptr<QApplication>& pQApp, bool bUseCairo) : SalGenericInstance(std::make_unique<Qt5YieldMutex>()) , m_postUserEventId(-1) , m_bUseCairo(bUseCairo) + , m_pQApplication(std::move(pQApp)) { ImplSVData* pSVData = ImplGetSVData(); if (bUseCairo) @@ -232,8 +233,6 @@ Qt5Instance::~Qt5Instance() // force freeing the QApplication before freeing the arguments, // as it uses references to the provided arguments! m_pQApplication.reset(); - for (int i = 0; i < *m_pFakeArgc; i++) - free(m_pFakeArgvFreeable[i]); } void Qt5Instance::deleteObjectLater(QObject* pObject) { pObject->deleteLater(); } @@ -441,18 +440,16 @@ Reference<XInterface> Qt5Instance::CreateDropTarget() return Reference<XInterface>(static_cast<cppu::OWeakObject*>(new Qt5DropTarget())); } -extern "C" { -VCLPLUG_QT5_PUBLIC SalInstance* create_SalInstance() +void Qt5Instance::AllocFakeCmdlineArgs(std::unique_ptr<char* []>& rFakeArgv, + std::unique_ptr<int>& rFakeArgc, + std::vector<FreeableCStr>& rFakeArgvFreeable) { OString aVersion(qVersion()); SAL_INFO("vcl.qt5", "qt version string is " << aVersion); - QApplication* pQApplication; - char** pFakeArgvFreeable = nullptr; - - int nFakeArgc = 2; const sal_uInt32 nParams = osl_getCommandArgCount(); OString aDisplay; + sal_uInt32 nDisplayValueIdx = 0; OUString aParam, aBin; for (sal_uInt32 nIdx = 0; nIdx < nParams; ++nIdx) @@ -460,66 +457,85 @@ VCLPLUG_QT5_PUBLIC SalInstance* create_SalInstance() osl_getCommandArg(nIdx, &aParam.pData); if (aParam != "-display") continue; - if (!pFakeArgvFreeable) - { - pFakeArgvFreeable = new char*[nFakeArgc + 2]; - pFakeArgvFreeable[nFakeArgc++] = strdup("-display"); - } - else - free(pFakeArgvFreeable[nFakeArgc]); - ++nIdx; - osl_getCommandArg(nIdx, &aParam.pData); - aDisplay = OUStringToOString(aParam, osl_getThreadTextEncoding()); - pFakeArgvFreeable[nFakeArgc] = strdup(aDisplay.getStr()); + nDisplayValueIdx = nIdx; } - if (!pFakeArgvFreeable) - pFakeArgvFreeable = new char*[nFakeArgc]; - else - nFakeArgc++; osl_getExecutableFile(&aParam.pData); osl_getSystemPathFromFileURL(aParam.pData, &aBin.pData); OString aExec = OUStringToOString(aBin, osl_getThreadTextEncoding()); - pFakeArgvFreeable[0] = strdup(aExec.getStr()); - pFakeArgvFreeable[1] = strdup("--nocrashhandler"); - char** pFakeArgv = new char*[nFakeArgc]; + std::vector<FreeableCStr> aFakeArgvFreeable; + aFakeArgvFreeable.reserve(4); + aFakeArgvFreeable.emplace_back(strdup(aExec.getStr())); + aFakeArgvFreeable.emplace_back(strdup("--nocrashhandler")); + if (nDisplayValueIdx) + { + aFakeArgvFreeable.emplace_back(strdup("-display")); + osl_getCommandArg(nDisplayValueIdx, &aParam.pData); + aDisplay = OUStringToOString(aParam, osl_getThreadTextEncoding()); + aFakeArgvFreeable.emplace_back(strdup(aDisplay.getStr())); + } + rFakeArgvFreeable.swap(aFakeArgvFreeable); + + const int nFakeArgc = rFakeArgvFreeable.size(); + rFakeArgv.reset(new char*[nFakeArgc]); for (int i = 0; i < nFakeArgc; i++) - pFakeArgv[i] = pFakeArgvFreeable[i]; + rFakeArgv[i] = rFakeArgvFreeable[i].get(); + + rFakeArgc.reset(new int); + *rFakeArgc = nFakeArgc; +} - char* session_manager = nullptr; +void Qt5Instance::MoveFakeCmdlineArgs(std::unique_ptr<char* []>& rFakeArgv, + std::unique_ptr<int>& rFakeArgc, + std::vector<FreeableCStr>& rFakeArgvFreeable) +{ + m_pFakeArgv = std::move(rFakeArgv); + m_pFakeArgc = std::move(rFakeArgc); + m_pFakeArgvFreeable.swap(rFakeArgvFreeable); +} + +std::unique_ptr<QApplication> Qt5Instance::CreateQApplication(int& nArgc, char** pArgv) +{ + QApplication::setAttribute(Qt::AA_DisableHighDpiScaling); + + FreeableCStr session_manager; if (getenv("SESSION_MANAGER") != nullptr) { - session_manager = strdup(getenv("SESSION_MANAGER")); + session_manager.reset(strdup(getenv("SESSION_MANAGER"))); unsetenv("SESSION_MANAGER"); } - QApplication::setAttribute(Qt::AA_DisableHighDpiScaling); - - int* pFakeArgc = new int; - *pFakeArgc = nFakeArgc; - pQApplication = new QApplication(*pFakeArgc, pFakeArgv); + std::unique_ptr<QApplication> pQApp = std::make_unique<QApplication>(nArgc, pArgv); if (session_manager != nullptr) { // coverity[tainted_string] - trusted source for setenv - setenv("SESSION_MANAGER", session_manager, 1); - free(session_manager); + setenv("SESSION_MANAGER", session_manager.get(), 1); } QApplication::setQuitOnLastWindowClosed(false); + return pQApp; +} +extern "C" { +VCLPLUG_QT5_PUBLIC SalInstance* create_SalInstance() +{ static const bool bUseCairo = (nullptr != getenv("SAL_VCL_QT5_USE_CAIRO")); - Qt5Instance* pInstance = new Qt5Instance(bUseCairo); - // initialize SalData - new Qt5Data(pInstance); + std::unique_ptr<char* []> pFakeArgv; + std::unique_ptr<int> pFakeArgc; + std::vector<FreeableCStr> aFakeArgvFreeable; + Qt5Instance::AllocFakeCmdlineArgs(pFakeArgv, pFakeArgc, aFakeArgvFreeable); - pInstance->m_pQApplication.reset(pQApplication); - pInstance->m_pFakeArgvFreeable.reset(pFakeArgvFreeable); - pInstance->m_pFakeArgv.reset(pFakeArgv); - pInstance->m_pFakeArgc.reset(pFakeArgc); + std::unique_ptr<QApplication> pQApp + = Qt5Instance::CreateQApplication(*pFakeArgc, pFakeArgv.get()); + + Qt5Instance* pInstance = new Qt5Instance(pQApp, bUseCairo); + pInstance->MoveFakeCmdlineArgs(pFakeArgv, pFakeArgc, aFakeArgvFreeable); + + new Qt5Data(pInstance); return pInstance; } |