From b740e27e365000380d22a290ad6615e5b5bce7e5 Mon Sep 17 00:00:00 2001 From: Andrew Knight Date: Fri, 6 Jun 2014 14:19:04 +0300 Subject: winrt: Clean up QWinRTServices - Remove WinRT types from the header - Use ComPtr everywhere - Use convenience methods for HRESULT and async operations Task-number: QTBUG-38115 Change-Id: I540a3349612b98c45545c92b2cb6d21a34918b8f Reviewed-by: Oliver Wolff --- src/plugins/platforms/winrt/qwinrtservices.cpp | 122 +++++++++++++------------ src/plugins/platforms/winrt/qwinrtservices.h | 27 ++---- 2 files changed, 68 insertions(+), 81 deletions(-) (limited to 'src') diff --git a/src/plugins/platforms/winrt/qwinrtservices.cpp b/src/plugins/platforms/winrt/qwinrtservices.cpp index b0f9247d36..6272b46f44 100644 --- a/src/plugins/platforms/winrt/qwinrtservices.cpp +++ b/src/plugins/platforms/winrt/qwinrtservices.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the plugins of the Qt Toolkit. @@ -43,6 +43,7 @@ #include #include #include +#include #include #include @@ -56,83 +57,84 @@ using namespace ABI::Windows::System; QT_BEGIN_NAMESPACE +class QWinRTServicesPrivate +{ +public: + ComPtr uriFactory; + ComPtr fileFactory; + ComPtr launcher; +}; + QWinRTServices::QWinRTServices() + : d_ptr(new QWinRTServicesPrivate) { - GetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Foundation_Uri).Get(), &m_uriFactory); - GetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Storage_StorageFile).Get(), &m_fileFactory); - GetActivationFactory(HString::MakeReference(RuntimeClass_Windows_System_Launcher).Get(), &m_launcher); + Q_D(QWinRTServices); + + HRESULT hr; + hr = RoGetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Foundation_Uri).Get(), + IID_PPV_ARGS(&d->uriFactory)); + Q_ASSERT_X(SUCCEEDED(hr), Q_FUNC_INFO, qPrintable(qt_error_string(hr))); + + hr = RoGetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Storage_StorageFile).Get(), + IID_PPV_ARGS(&d->fileFactory)); + Q_ASSERT_X(SUCCEEDED(hr), Q_FUNC_INFO, qPrintable(qt_error_string(hr))); + + hr = RoGetActivationFactory(HString::MakeReference(RuntimeClass_Windows_System_Launcher).Get(), + IID_PPV_ARGS(&d->launcher)); + Q_ASSERT_X(SUCCEEDED(hr), Q_FUNC_INFO, qPrintable(qt_error_string(hr))); } QWinRTServices::~QWinRTServices() { - if (m_uriFactory) - m_uriFactory->Release(); - - if (m_fileFactory) - m_fileFactory->Release(); - - if (m_launcher) - m_launcher->Release(); } bool QWinRTServices::openUrl(const QUrl &url) { - if (!(m_uriFactory && m_launcher)) - return QPlatformServices::openUrl(url); + Q_D(QWinRTServices); - IUriRuntimeClass *uri; + ComPtr uri; QString urlString = url.toString(); - // ### TODO: Replace with HStringReference when WP8.0 support is removed - HString uriString; - uriString.Set((const wchar_t*)urlString.utf16(), urlString.length()); - m_uriFactory->CreateUri(uriString.Get(), &uri); - if (!uri) - return false; - - IAsyncOperation *launchOp; - m_launcher->LaunchUriAsync(uri, &launchOp); - uri->Release(); - if (!launchOp) - return false; - - boolean result = false; - while (launchOp->GetResults(&result) == E_ILLEGAL_METHOD_CALL) - QCoreApplication::processEvents(); - launchOp->Release(); + HStringReference uriString(reinterpret_cast(urlString.utf16()), urlString.length()); + HRESULT hr = d->uriFactory->CreateUri(uriString.Get(), &uri); + RETURN_FALSE_IF_FAILED("Failed to create URI from QUrl."); + + ComPtr> op; + hr = d->launcher->LaunchUriAsync(uri.Get(), &op); + RETURN_FALSE_IF_FAILED("Failed to start URI launch."); + + boolean result; + hr = QWinRTFunctions::await(op, &result); + RETURN_FALSE_IF_FAILED("Failed to launch URI."); return result; } bool QWinRTServices::openDocument(const QUrl &url) { - if (!(m_fileFactory && m_launcher)) - return QPlatformServices::openDocument(url); - - const QString pathString = QDir::toNativeSeparators(url.toLocalFile()); - // ### TODO: Replace with HStringReference when WP8.0 support is removed - HString path; - path.Set((const wchar_t*)pathString.utf16(), pathString.length()); - IAsyncOperation *fileOp; - m_fileFactory->GetFileFromPathAsync(path.Get(), &fileOp); - if (!fileOp) - return false; - - IStorageFile *file = nullptr; - while (fileOp->GetResults(&file) == E_ILLEGAL_METHOD_CALL) - QCoreApplication::processEvents(); - fileOp->Release(); - if (!file) - return false; - - IAsyncOperation *launchOp; - m_launcher->LaunchFileAsync(file, &launchOp); - if (!launchOp) - return false; - - boolean result = false; - while (launchOp->GetResults(&result) == E_ILLEGAL_METHOD_CALL) - QCoreApplication::processEvents(); - launchOp->Release(); + Q_D(QWinRTServices); + + HRESULT hr; + ComPtr file; + { + const QString pathString = QDir::toNativeSeparators(url.toLocalFile()); + HStringReference path(reinterpret_cast(pathString.utf16()), pathString.length()); + ComPtr> op; + hr = d->fileFactory->GetFileFromPathAsync(path.Get(), &op); + RETURN_FALSE_IF_FAILED("Failed to initialize file URI."); + + hr = QWinRTFunctions::await(op, file.GetAddressOf()); + RETURN_FALSE_IF_FAILED("Failed to get file URI."); + } + + boolean result; + { + ComPtr> op; + hr = d->launcher->LaunchFileAsync(file.Get(), &op); + RETURN_FALSE_IF_FAILED("Failed to start file launch."); + + hr = QWinRTFunctions::await(op, &result); + RETURN_FALSE_IF_FAILED("Failed to launch file."); + } return result; } diff --git a/src/plugins/platforms/winrt/qwinrtservices.h b/src/plugins/platforms/winrt/qwinrtservices.h index 9cc917030a..d3abe6f2bd 100644 --- a/src/plugins/platforms/winrt/qwinrtservices.h +++ b/src/plugins/platforms/winrt/qwinrtservices.h @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the plugins of the Qt Toolkit. @@ -43,23 +43,11 @@ #define QWINRTSERVICES_H #include +#include -namespace ABI { - namespace Windows { - namespace Foundation { - struct IUriRuntimeClassFactory; - } - namespace Storage { - struct IStorageFileStatics; - } - namespace System { - struct ILauncherStatics; - } - } -} - -QT_BEGIN_NAMESPACE +QT_USE_NAMESPACE +class QWinRTServicesPrivate; class QWinRTServices : public QPlatformServices { public: @@ -70,11 +58,8 @@ public: bool openDocument(const QUrl &url); private: - ABI::Windows::Foundation::IUriRuntimeClassFactory *m_uriFactory; - ABI::Windows::Storage::IStorageFileStatics *m_fileFactory; - ABI::Windows::System::ILauncherStatics *m_launcher; + QScopedPointer d_ptr; + Q_DECLARE_PRIVATE(QWinRTServices) }; -QT_END_NAMESPACE - #endif // QWINRTSERVICES_H -- cgit v1.2.3