From ed40d477b2412d4f23540052ca0748028c6103e6 Mon Sep 17 00:00:00 2001 From: Mike Kaganski Date: Fri, 26 Feb 2021 13:11:12 +0300 Subject: Drop ComPtr and use sal::systools::COMReference Change-Id: I9eb6d99d883b44943ad69c2c28d4e55716dc34f9 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/111673 Tested-by: Jenkins Reviewed-by: Mike Kaganski --- .../win32/IVistaFilePickerInternalNotify.hxx | 20 +- .../source/win32/VistaFilePickerEventHandler.cxx | 2 +- fpicker/source/win32/VistaFilePickerImpl.cxx | 92 ++++----- fpicker/source/win32/VistaFilePickerImpl.hxx | 1 - fpicker/source/win32/comptr.hxx | 212 --------------------- fpicker/source/win32/vistatypes.h | 13 +- 6 files changed, 60 insertions(+), 280 deletions(-) delete mode 100644 fpicker/source/win32/comptr.hxx (limited to 'fpicker') diff --git a/fpicker/source/win32/IVistaFilePickerInternalNotify.hxx b/fpicker/source/win32/IVistaFilePickerInternalNotify.hxx index f39d5aff4560..42f00c1960a4 100644 --- a/fpicker/source/win32/IVistaFilePickerInternalNotify.hxx +++ b/fpicker/source/win32/IVistaFilePickerInternalNotify.hxx @@ -17,22 +17,14 @@ * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ -#ifndef INCLUDED_FPICKER_SOURCE_WIN32_FILEPICKER_IVISTAFILEPICKERINTERNALNOTIFY_HXX -#define INCLUDED_FPICKER_SOURCE_WIN32_FILEPICKER_IVISTAFILEPICKERINTERNALNOTIFY_HXX +#pragma once -#include "vistatypes.h" +#include -#include -#include +namespace fpicker::win32::vista{ -#include -namespace fpicker{ -namespace win32{ -namespace vista{ - - -// types, const etcpp. +// types, const etc. /** todo document me @@ -51,8 +43,6 @@ class IVistaFilePickerInternalNotify ~IVistaFilePickerInternalNotify() {} }; -}}} - -#endif // INCLUDED_FPICKER_SOURCE_WIN32_FILEPICKER_IVISTAFILEPICKERINTERNALNOTIFY_HXX +} /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/fpicker/source/win32/VistaFilePickerEventHandler.cxx b/fpicker/source/win32/VistaFilePickerEventHandler.cxx index 1e148bb3e1db..c4a231164ef8 100644 --- a/fpicker/source/win32/VistaFilePickerEventHandler.cxx +++ b/fpicker/source/win32/VistaFilePickerEventHandler.cxx @@ -229,7 +229,7 @@ void VistaFilePickerEventHandler::stopListening() if (m_pDialog.is()) { m_pDialog->Unadvise(m_nListenerHandle); - m_pDialog.release(); + m_pDialog.clear(); } } diff --git a/fpicker/source/win32/VistaFilePickerImpl.cxx b/fpicker/source/win32/VistaFilePickerImpl.cxx index b0e733be88bc..2fef710b728a 100644 --- a/fpicker/source/win32/VistaFilePickerImpl.cxx +++ b/fpicker/source/win32/VistaFilePickerImpl.cxx @@ -27,7 +27,6 @@ #include #include #include -#include #include #include #include @@ -56,7 +55,8 @@ static HWND choose_parent_window() namespace { -bool createFolderItem(OUString const & url, ComPtr & folder) { +bool createFolderItem(OUString const& url, sal::systools::COMReference& folder) +{ OUString path; if (osl::FileBase::getSystemPathFromFileURL(url, path) != osl::FileBase::E_None) @@ -97,54 +97,53 @@ const GUID CLIENTID_FILEOPEN_LINK = {0x39AC4BAE, 0x7D2D, 0x46BC, 0xBE class TDialogImplBase { public: - template TDialogImplBase(ComPtrDialog&& iDialog) + TDialogImplBase(IFileDialog* iDialog) + : m_iDialog(iDialog) { - HRESULT hResult = iDialog.create(); - if (FAILED(hResult)) - throw css::uno::RuntimeException(WindowsErrorStringFromHRESULT(hResult)); - hResult = iDialog.query(&m_iDialog); - if (FAILED(hResult)) - throw css::uno::RuntimeException(WindowsErrorStringFromHRESULT(hResult)); } - TFileDialog getComPtr() { return m_iDialog; }; - virtual ComPtr getResult(bool bInExecute) + TFileDialog getComPtr() { return m_iDialog; } + virtual sal::systools::COMReference getResult(bool bInExecute) { - ComPtr iItem; + sal::systools::COMReference iItem; if (bInExecute) m_iDialog->GetCurrentSelection(&iItem); else m_iDialog->GetResult(&iItem); void* iItems = nullptr; - SHCreateShellItemArrayFromShellItem(iItem, IID_IShellItemArray, &iItems); - return ComPtr(reinterpret_cast(iItems)); + if (iItem.is()) + SHCreateShellItemArrayFromShellItem(iItem.get(), IID_IShellItemArray, &iItems); + return static_cast(iItems); } private: TFileDialog m_iDialog; }; -template class TDialogImpl : public TDialogImplBase +template class TDialogImpl : public TDialogImplBase { public: - TDialogImpl() : TDialogImplBase(ComPtrDialog()) {} + TDialogImpl() + : TDialogImplBase(ComPtrDialog().CoCreateInstance(CLSID).get()) + { + } }; -class TOpenDialogImpl : public TDialogImpl +class TOpenDialogImpl : public TDialogImpl { public: - ComPtr getResult(bool bInExecute) override + sal::systools::COMReference getResult(bool bInExecute) override { - ComPtr iItems; - TFileOpenDialog iDialog{ getComPtr() }; + sal::systools::COMReference iItems; + TFileOpenDialog iDialog(getComPtr(), sal::systools::COM_QUERY_THROW); if (FAILED(bInExecute ? iDialog->GetSelectedItems(&iItems) : iDialog->GetResults(&iItems))) iItems = TDialogImplBase::getResult(bInExecute); return iItems; } }; -using TSaveDialogImpl = TDialogImpl; -using TFolderPickerDialogImpl = TDialogImpl; +using TSaveDialogImpl = TDialogImpl; +using TFolderPickerDialogImpl = TDialogImpl; static OUString lcl_getURLFromShellItem (IShellItem* pItem) @@ -384,9 +383,11 @@ void VistaFilePickerImpl::impl_sta_addFilePickerListener(const RequestRef& rRequ aLock.clear(); // <- SYNCHRONIZED - VistaFilePickerEventHandler* pHandlerImpl = static_cast(iHandler.get()); - if (pHandlerImpl) + if (iHandler.is()) + { + auto* pHandlerImpl = static_cast(iHandler.get()); pHandlerImpl->addFilePickerListener(xListener); + } } @@ -403,9 +404,11 @@ void VistaFilePickerImpl::impl_sta_removeFilePickerListener(const RequestRef& rR aLock.clear(); // <- SYNCHRONIZED - VistaFilePickerEventHandler* pHandlerImpl = static_cast(iHandler.get()); - if (pHandlerImpl) + if (iHandler.is()) + { + auto* pHandlerImpl = static_cast(iHandler.get()); pHandlerImpl->removeFilePickerListener(xListener); + } } @@ -534,9 +537,11 @@ void VistaFilePickerImpl::impl_sta_InitDialog(const RequestRef& rRequest, DWORD ::sal_Int32 nTemplate = rRequest->getArgumentOrDefault(PROP_TEMPLATE_DESCR, ::sal_Int32(0)); impl_sta_enableFeatures(nFeatures, nTemplate); - VistaFilePickerEventHandler* pHandlerImpl = static_cast(iHandler.get()); - if (pHandlerImpl) + if (iHandler.is()) + { + auto* pHandlerImpl = static_cast(iHandler.get()); pHandlerImpl->startListening(iDialog); + } } @@ -794,21 +799,21 @@ void VistaFilePickerImpl::impl_sta_SetDirectory(const RequestRef& rRequest) aLock.clear(); // <- SYNCHRONIZED - ComPtr< IShellItem > pFolder; + sal::systools::COMReference pFolder; if ( !createFolderItem(sDirectory, pFolder) ) return; - iDialog->SetFolder(pFolder); + iDialog->SetFolder(pFolder.get()); } OUString VistaFilePickerImpl::GetDirectory() { TFileDialog iDialog = impl_getBaseDialogInterface(); - ComPtr< IShellItem > pFolder; + sal::systools::COMReference pFolder; HRESULT hResult = iDialog->GetFolder( &pFolder ); if ( FAILED(hResult) ) return OUString(); - return lcl_getURLFromShellItem(pFolder); + return lcl_getURLFromShellItem(pFolder.get()); } void VistaFilePickerImpl::impl_sta_GetDirectory(const RequestRef& rRequest) @@ -902,8 +907,8 @@ void VistaFilePickerImpl::impl_sta_getSelectedFiles(const RequestRef& rRequest) // ask dialog for results // we must react different if dialog is in execute or not .-( - ComPtr iItems = pDialog->getResult(bInExecute); - if (!iItems) + sal::systools::COMReference iItems = pDialog->getResult(bInExecute); + if (!iItems.is()) return; // convert and pack results @@ -912,9 +917,10 @@ void VistaFilePickerImpl::impl_sta_getSelectedFiles(const RequestRef& rRequest) { for (DWORD i = 0; i < nCount; ++i) { - if (ComPtr iItem; SUCCEEDED(iItems->GetItemAt(i, &iItem))) + if (sal::systools::COMReference iItem; + SUCCEEDED(iItems->GetItemAt(i, &iItem))) { - if (const OUString sURL = lcl_getURLFromShellItem(iItem); !sURL.isEmpty()) + if (const OUString sURL = lcl_getURLFromShellItem(iItem.get()); !sURL.isEmpty()) lFiles.push_back(sURL); } } @@ -948,7 +954,7 @@ void VistaFilePickerImpl::impl_sta_ShowDialogModal(const RequestRef& rRequest) // according to its client guid. if( m_sDirectory.getLength()) { - ComPtr< IShellItem > pFolder; + sal::systools::COMReference pFolder; if ( createFolderItem(m_sDirectory, pFolder) ) { if (m_sFilename.getLength()) @@ -988,14 +994,14 @@ void VistaFilePickerImpl::impl_sta_ShowDialogModal(const RequestRef& rRequest) WIN32_FIND_DATAW aFindFileData; HANDLE hFind = FindFirstFileW( o3tl::toW(aSystemPath.getStr()), &aFindFileData ); if (hFind != INVALID_HANDLE_VALUE) - iDialog->SetFolder(pFolder); + iDialog->SetFolder(pFolder.get()); else - hResult = iDialog->AddPlace(pFolder, FDAP_TOP); + hResult = iDialog->AddPlace(pFolder.get(), FDAP_TOP); FindClose( hFind ); } else - iDialog->AddPlace(pFolder, FDAP_TOP); + iDialog->AddPlace(pFolder.get(), FDAP_TOP); } } @@ -1044,15 +1050,13 @@ TFileDialog VistaFilePickerImpl::impl_getBaseDialogInterface() TFileDialogCustomize VistaFilePickerImpl::impl_getCustomizeInterface() { - TFileDialogCustomize iCustom; - // SYNCHRONIZED-> osl::MutexGuard aLock(m_aMutex); if (m_pDialog) - m_pDialog->getComPtr().query(&iCustom); + return { m_pDialog->getComPtr(), sal::systools::COM_QUERY_THROW }; - return iCustom; + return {}; } diff --git a/fpicker/source/win32/VistaFilePickerImpl.hxx b/fpicker/source/win32/VistaFilePickerImpl.hxx index 1353c55e869a..6e6e04ad56fc 100644 --- a/fpicker/source/win32/VistaFilePickerImpl.hxx +++ b/fpicker/source/win32/VistaFilePickerImpl.hxx @@ -25,7 +25,6 @@ #include #include "asyncrequests.hxx" -#include "comptr.hxx" #include "vistatypes.h" #include "FilterContainer.hxx" #include "VistaFilePickerEventHandler.hxx" diff --git a/fpicker/source/win32/comptr.hxx b/fpicker/source/win32/comptr.hxx deleted file mode 100644 index 2469d8b5e289..000000000000 --- a/fpicker/source/win32/comptr.hxx +++ /dev/null @@ -1,212 +0,0 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* - * This file is part of the LibreOffice project. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * This file incorporates work covered by the following license notice: - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed - * with this work for additional information regarding copyright - * ownership. The ASF licenses this file to you under the Apache - * License, Version 2.0 (the "License"); you may not use this file - * except in compliance with the License. You may obtain a copy of - * the License at http://www.apache.org/licenses/LICENSE-2.0 . - */ - -#ifndef INCLUDED_FPICKER_SOURCE_WIN32_FILEPICKER_COMPTR_HXX -#define INCLUDED_FPICKER_SOURCE_WIN32_FILEPICKER_COMPTR_HXX - -#include -#include -#include - -template< class T_INTERFACE , - REFIID P_IID = IID_NULL , - REFCLSID P_CLSID = CLSID_NULL > -class ComPtr -{ - public: - - - /** initialize com ptr with null. - */ - ComPtr() - { - m_pInterface = nullptr; - } - - - /** initialize com ptr with given interface. - */ - explicit ComPtr(T_INTERFACE* pInterface) - { - m_pInterface = pInterface; - if (m_pInterface) - m_pInterface->AddRef(); - } - - - /** copy ctor. - */ - ComPtr(const ComPtr< T_INTERFACE, P_IID, P_CLSID >& aCopy) - { - m_pInterface = aCopy.m_pInterface; - if (m_pInterface) - m_pInterface->AddRef(); - } - - - /** initialize object by querying external object for the right interface. - */ - explicit ComPtr(IUnknown* pIUnknown) - { - if (pIUnknown) - pIUnknown->QueryInterface(P_IID, (void**)&m_pInterface); - } - - - /** deinitialize com object right. - */ - ~ComPtr() - { - release(); - } - - public: - - - HRESULT create() - { - return CoCreateInstance(P_CLSID, nullptr, CLSCTX_ALL, P_IID, reinterpret_cast(&m_pInterface)); - } - - - operator T_INTERFACE*() const - { - return m_pInterface; - } - - - T_INTERFACE& operator*() const - { - return *m_pInterface; - } - - - T_INTERFACE** operator&() - { - return &m_pInterface; - } - - - T_INTERFACE* operator->() const - { - return m_pInterface; - } - - - T_INTERFACE* operator=(T_INTERFACE* pInterface) - { - if ( equals(pInterface) ) - return m_pInterface; - - m_pInterface->Release(); - m_pInterface = pInterface; - if (m_pInterface) - m_pInterface->AddRef(); - - return m_pInterface; - } - - - T_INTERFACE* operator=(IUnknown* pIUnknown) - { - if (pIUnknown) - pIUnknown->QueryInterface(P_IID, (void**)&m_pInterface); - return m_pInterface; - } - - - T_INTERFACE* operator=(const ComPtr< T_INTERFACE, P_IID, P_CLSID >& aCopy) - { - m_pInterface = aCopy.m_pInterface; - if (m_pInterface) - m_pInterface->AddRef(); - - return m_pInterface; - } - - - T_INTERFACE* get() const - { - return m_pInterface; - } - - - void attach(T_INTERFACE* pInterface) - { - if (pInterface) - { - m_pInterface->Release(); - m_pInterface = pInterface; - } - } - - - T_INTERFACE* detach() - { - T_INTERFACE* pInterface = m_pInterface; - m_pInterface = NULL; - return pInterface; - } - - - void release() - { - if (m_pInterface) - { - m_pInterface->Release(); - m_pInterface = nullptr; - } - } - - template< class T_QUERYINTERFACE > - HRESULT query(T_QUERYINTERFACE** pQuery) - { - return m_pInterface->QueryInterface(__uuidof(T_QUERYINTERFACE), reinterpret_cast(pQuery)); - } - - bool equals(IUnknown* pCheck) - { - if ( - ( ! m_pInterface ) && - ( ! pCheck ) - ) - return sal_True; - - IUnknown* pCurrent = NULL; - m_pInterface->QueryInterface(IID_IUnknown, (void**)&pCurrent); - - sal_Bool bEquals = (pCheck == pCurrent); - pCurrent->Release(); - - return bEquals; - } - - - bool is() - { - return (m_pInterface != nullptr); - } - - private: - T_INTERFACE* m_pInterface; -}; - -#endif - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/fpicker/source/win32/vistatypes.h b/fpicker/source/win32/vistatypes.h index 4271b0cea482..5e6c62a439f3 100644 --- a/fpicker/source/win32/vistatypes.h +++ b/fpicker/source/win32/vistatypes.h @@ -20,8 +20,8 @@ #ifndef INCLUDED_FPICKER_SOURCE_WIN32_FILEPICKER_VISTATYPES_H #define INCLUDED_FPICKER_SOURCE_WIN32_FILEPICKER_VISTATYPES_H -#include "comptr.hxx" #include +#include namespace fpicker{ namespace win32{ @@ -31,12 +31,11 @@ namespace vista{ // types, const etcpp. -typedef ComPtr< IFileDialog , IID_IFileDialog > TFileDialog; -typedef ComPtr< IFileOpenDialog , IID_IFileOpenDialog , CLSID_FileOpenDialog > TFileOpenDialog; -typedef ComPtr< IFileSaveDialog , IID_IFileSaveDialog , CLSID_FileSaveDialog > TFileSaveDialog; -typedef ComPtr< IFileDialogEvents , IID_IFileDialogEvents > TFileDialogEvents; -typedef ComPtr< IFileDialogCustomize, IID_IFileDialogCustomize > TFileDialogCustomize; -typedef TFileOpenDialog TFolderPickerDialog; +typedef sal::systools::COMReference TFileDialog; +typedef sal::systools::COMReference TFileOpenDialog; +typedef sal::systools::COMReference TFileSaveDialog; +typedef sal::systools::COMReference TFileDialogEvents; +typedef sal::systools::COMReference TFileDialogCustomize; } // namespace vista } // namespace win32 -- cgit