cid#1606940 Check of thread-shared field evades lock acquisition
[LibreOffice.git] / writerperfect / source / common / DirectoryStream.cxx
blob6f0d593cefbf2d617b35b04e06639f074fa194cc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* writerperfect
3 * Version: MPL 2.0 / LGPLv2.1+
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * Major Contributor(s):
10 * Copyright (C) 2007 Fridrich Strba (fridrich.strba@bluewin.ch)
12 * For minor contributions see the git repository.
14 * Alternatively, the contents of this file may be used under the terms
15 * of the GNU Lesser General Public License Version 2.1 or later
16 * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
17 * applicable instead of those above.
19 * For further information visit http://libwpd.sourceforge.net
22 #include <memory>
23 #include <com/sun/star/container/XChild.hpp>
24 #include <com/sun/star/io/XInputStream.hpp>
25 #include <com/sun/star/sdbc/XResultSet.hpp>
26 #include <com/sun/star/sdbc/XRow.hpp>
27 #include <com/sun/star/ucb/XContent.hpp>
28 #include <com/sun/star/ucb/XContentAccess.hpp>
29 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
31 #include <com/sun/star/uno/Reference.hxx>
32 #include <com/sun/star/uno/Sequence.hxx>
34 #include <comphelper/processfactory.hxx>
36 #include <rtl/ustring.hxx>
38 #include <ucbhelper/content.hxx>
40 #include <DirectoryStream.hxx>
41 #include <WPXSvInputStream.hxx>
42 #include <utility>
44 namespace io = css::io;
45 namespace sdbc = css::sdbc;
46 namespace ucb = css::ucb;
47 namespace uno = css::uno;
49 namespace writerperfect
51 namespace
53 uno::Reference<io::XInputStream> findStream(ucbhelper::Content& rContent, std::u16string_view rName)
55 uno::Reference<io::XInputStream> xInputStream;
57 uno::Sequence<OUString> lPropNames{ u"Title"_ustr };
58 try
60 const uno::Reference<sdbc::XResultSet> xResultSet(
61 rContent.createCursor(lPropNames, ucbhelper::INCLUDE_DOCUMENTS_ONLY));
62 if (!xResultSet->first())
63 return xInputStream;
65 const uno::Reference<ucb::XContentAccess> xContentAccess(xResultSet, uno::UNO_QUERY);
66 if (!xContentAccess)
67 return xInputStream;
68 const uno::Reference<sdbc::XRow> xRow(xResultSet, uno::UNO_QUERY);
69 if (!xRow)
70 return xInputStream;
73 const OUString aTitle(xRow->getString(1));
74 if (aTitle == rName)
76 const uno::Reference<ucb::XContent> xSubContent(xContentAccess->queryContent());
77 ucbhelper::Content aSubContent(xSubContent,
78 uno::Reference<ucb::XCommandEnvironment>(),
79 comphelper::getProcessComponentContext());
80 xInputStream = aSubContent.openStream();
81 break;
83 } while (xResultSet->next());
85 catch (const uno::RuntimeException&)
87 // ignore
89 catch (const uno::Exception&)
91 // ignore
94 return xInputStream;
98 struct DirectoryStream::Impl
100 explicit Impl(uno::Reference<ucb::XContent> xContent);
102 uno::Reference<ucb::XContent> xContent;
105 DirectoryStream::Impl::Impl(uno::Reference<ucb::XContent> _xContent)
106 : xContent(std::move(_xContent))
110 DirectoryStream::DirectoryStream(const css::uno::Reference<css::ucb::XContent>& xContent)
111 : m_pImpl(isDirectory(xContent) ? new Impl(xContent) : nullptr)
115 DirectoryStream::~DirectoryStream() {}
117 bool DirectoryStream::isDirectory(const css::uno::Reference<css::ucb::XContent>& xContent)
121 if (!xContent.is())
122 return false;
124 ucbhelper::Content aContent(xContent, uno::Reference<ucb::XCommandEnvironment>(),
125 comphelper::getProcessComponentContext());
126 return aContent.isFolder();
128 catch (...)
130 return false;
134 std::unique_ptr<DirectoryStream>
135 DirectoryStream::createForParent(const css::uno::Reference<css::ucb::XContent>& xContent)
139 if (!xContent.is())
140 return nullptr;
142 std::unique_ptr<DirectoryStream> pDir;
144 const uno::Reference<css::container::XChild> xChild(xContent, uno::UNO_QUERY);
145 if (xChild.is())
147 const uno::Reference<ucb::XContent> xDirContent(xChild->getParent(), uno::UNO_QUERY);
148 if (xDirContent.is())
150 pDir = std::make_unique<DirectoryStream>(xDirContent);
151 if (!pDir->isStructured())
152 pDir.reset();
156 return pDir;
158 catch (...)
160 return nullptr;
164 css::uno::Reference<css::ucb::XContent> DirectoryStream::getContent() const
166 if (!m_pImpl)
167 return css::uno::Reference<css::ucb::XContent>();
168 return m_pImpl->xContent;
171 bool DirectoryStream::isStructured() { return m_pImpl != nullptr; }
173 unsigned DirectoryStream::subStreamCount()
175 // TODO: implement me
176 return 0;
179 const char* DirectoryStream::subStreamName(unsigned /* id */)
181 // TODO: implement me
182 return nullptr;
185 bool DirectoryStream::existsSubStream(const char* /* name */)
187 // TODO: implement me
188 return false;
191 librevenge::RVNGInputStream* DirectoryStream::getSubStreamByName(const char* const pName)
193 if (!m_pImpl)
194 return nullptr;
196 ucbhelper::Content aContent(m_pImpl->xContent, uno::Reference<ucb::XCommandEnvironment>(),
197 comphelper::getProcessComponentContext());
198 const uno::Reference<io::XInputStream> xInputStream(
199 findStream(aContent, OUString::createFromAscii(pName)));
200 if (xInputStream.is())
201 return new WPXSvInputStream(xInputStream);
203 return nullptr;
206 librevenge::RVNGInputStream* DirectoryStream::getSubStreamById(unsigned /* id */)
208 // TODO: implement me
209 return nullptr;
212 const unsigned char* DirectoryStream::read(unsigned long, unsigned long& nNumBytesRead)
214 nNumBytesRead = 0;
215 return nullptr;
218 int DirectoryStream::seek(long, librevenge::RVNG_SEEK_TYPE) { return -1; }
220 long DirectoryStream::tell() { return 0; }
222 bool DirectoryStream::isEnd() { return true; }
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */