LibreOffice Module writerperfect (master) 1
DirectoryStream.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/* writerperfect
3 * Version: MPL 2.0 / LGPLv2.1+
4 *
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/.
8 *
9 * Major Contributor(s):
10 * Copyright (C) 2007 Fridrich Strba (fridrich.strba@bluewin.ch)
11 *
12 * For minor contributions see the git repository.
13 *
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.
18 *
19 * For further information visit http://libwpd.sourceforge.net
20 */
21
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>
30
31#include <com/sun/star/uno/Reference.hxx>
32#include <com/sun/star/uno/Sequence.hxx>
33
35
36#include <rtl/ustring.hxx>
37
38#include <ucbhelper/content.hxx>
39
40#include <DirectoryStream.hxx>
41#include <WPXSvInputStream.hxx>
42#include <utility>
43
44namespace io = com::sun::star::io;
45namespace sdbc = com::sun::star::sdbc;
46namespace ucb = com::sun::star::ucb;
47namespace uno = com::sun::star::uno;
48
50{
51namespace
52{
53uno::Reference<io::XInputStream> findStream(ucbhelper::Content& rContent, std::u16string_view rName)
54{
56
57 uno::Sequence<OUString> lPropNames{ "Title" };
58 try
59 {
60 const uno::Reference<sdbc::XResultSet> xResultSet(
62 if (xResultSet->first())
63 {
64 const uno::Reference<ucb::XContentAccess> xContentAccess(xResultSet,
65 uno::UNO_QUERY_THROW);
66 const uno::Reference<sdbc::XRow> xRow(xResultSet, uno::UNO_QUERY_THROW);
67 do
68 {
69 const OUString aTitle(xRow->getString(1));
70 if (aTitle == rName)
71 {
72 const uno::Reference<ucb::XContent> xSubContent(xContentAccess->queryContent());
73 ucbhelper::Content aSubContent(xSubContent,
76 xInputStream = aSubContent.openStream();
77 break;
78 }
79 } while (xResultSet->next());
80 }
81 }
82 catch (const uno::RuntimeException&)
83 {
84 // ignore
85 }
86 catch (const uno::Exception&)
87 {
88 // ignore
89 }
90
91 return xInputStream;
92}
93}
94
96{
98
100};
101
103 : xContent(std::move(_xContent))
104{
105}
106
107DirectoryStream::DirectoryStream(const css::uno::Reference<css::ucb::XContent>& xContent)
108 : m_pImpl(isDirectory(xContent) ? new Impl(xContent) : nullptr)
109{
110}
111
113
114bool DirectoryStream::isDirectory(const css::uno::Reference<css::ucb::XContent>& xContent)
115{
116 try
117 {
118 if (!xContent.is())
119 return false;
120
123 return aContent.isFolder();
124 }
125 catch (...)
126 {
127 return false;
128 }
129}
130
131std::unique_ptr<DirectoryStream>
132DirectoryStream::createForParent(const css::uno::Reference<css::ucb::XContent>& xContent)
133{
134 try
135 {
136 if (!xContent.is())
137 return nullptr;
138
139 std::unique_ptr<DirectoryStream> pDir;
140
141 const uno::Reference<css::container::XChild> xChild(xContent, uno::UNO_QUERY);
142 if (xChild.is())
143 {
144 const uno::Reference<ucb::XContent> xDirContent(xChild->getParent(), uno::UNO_QUERY);
145 if (xDirContent.is())
146 {
147 pDir = std::make_unique<DirectoryStream>(xDirContent);
148 if (!pDir->isStructured())
149 pDir.reset();
150 }
151 }
152
153 return pDir;
154 }
155 catch (...)
156 {
157 return nullptr;
158 }
159}
160
161css::uno::Reference<css::ucb::XContent> DirectoryStream::getContent() const
162{
163 if (!m_pImpl)
164 return css::uno::Reference<css::ucb::XContent>();
165 return m_pImpl->xContent;
166}
167
168bool DirectoryStream::isStructured() { return m_pImpl != nullptr; }
169
171{
172 // TODO: implement me
173 return 0;
174}
175
176const char* DirectoryStream::subStreamName(unsigned /* id */)
177{
178 // TODO: implement me
179 return nullptr;
180}
181
182bool DirectoryStream::existsSubStream(const char* /* name */)
183{
184 // TODO: implement me
185 return false;
186}
187
188librevenge::RVNGInputStream* DirectoryStream::getSubStreamByName(const char* const pName)
189{
190 if (!m_pImpl)
191 return nullptr;
192
195 const uno::Reference<io::XInputStream> xInputStream(
196 findStream(aContent, OUString::createFromAscii(pName)));
197 if (xInputStream.is())
198 return new WPXSvInputStream(xInputStream);
199
200 return nullptr;
201}
202
203librevenge::RVNGInputStream* DirectoryStream::getSubStreamById(unsigned /* id */)
204{
205 // TODO: implement me
206 return nullptr;
207}
208
209const unsigned char* DirectoryStream::read(unsigned long, unsigned long& nNumBytesRead)
210{
211 nNumBytesRead = 0;
212 return nullptr;
213}
214
215int DirectoryStream::seek(long, librevenge::RVNG_SEEK_TYPE) { return -1; }
216
217long DirectoryStream::tell() { return 0; }
218
219bool DirectoryStream::isEnd() { return true; }
220}
221
222/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const char * pName
css::uno::Reference< css::io::XInputStream > openStream()
css::uno::Reference< css::sdbc::XResultSet > createCursor(const css::uno::Sequence< OUString > &rPropertyNames, ResultSetInclude eMode=INCLUDE_FOLDERS_AND_DOCUMENTS)
css::uno::Reference< css::ucb::XContent > getContent() const
DirectoryStream(const css::uno::Reference< css::ucb::XContent > &xContent)
static std::unique_ptr< DirectoryStream > createForParent(const css::uno::Reference< css::ucb::XContent > &xContent)
virtual SAL_DLLPRIVATE bool existsSubStream(const char *name) override
virtual bool isStructured() override
virtual SAL_DLLPRIVATE unsigned subStreamCount() override
std::unique_ptr< Impl > m_pImpl
virtual long tell() override
static bool isDirectory(const css::uno::Reference< css::ucb::XContent > &xContent)
virtual bool isEnd() override
virtual SAL_DLLPRIVATE const char * subStreamName(unsigned id) override
virtual const unsigned char * read(unsigned long numBytes, unsigned long &numBytesRead) override
virtual SAL_DLLPRIVATE librevenge::RVNGInputStream * getSubStreamByName(const char *name) override
virtual SAL_DLLPRIVATE librevenge::RVNGInputStream * getSubStreamById(unsigned id) override
virtual int seek(long offset, librevenge::RVNG_SEEK_TYPE seekType) override
Reference< XComponentContext > getProcessComponentContext()
INCLUDE_DOCUMENTS_ONLY
uno::Reference< ucb::XContent > xContent
Impl(uno::Reference< ucb::XContent > xContent)