summaryrefslogtreecommitdiff
path: root/writerperfect/source/common/DirectoryStream.cxx
blob: eb275abec5e5b816577ea758c1efabcdbf96f2c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* writerperfect
 * Version: MPL 2.0 / LGPLv2.1+
 *
 * 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/.
 *
 * Major Contributor(s):
 * Copyright (C) 2007 Fridrich Strba (fridrich.strba@bluewin.ch)
 *
 * For minor contributions see the git repository.
 *
 * Alternatively, the contents of this file may be used under the terms
 * of the GNU Lesser General Public License Version 2.1 or later
 * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
 * applicable instead of those above.
 *
 * For further information visit http://libwpd.sourceforge.net
 */

#include <memory>
#include <com/sun/star/container/XChild.hpp>
#include <com/sun/star/io/XInputStream.hpp>
#include <com/sun/star/sdbc/XResultSet.hpp>
#include <com/sun/star/sdbc/XRow.hpp>
#include <com/sun/star/ucb/XContent.hpp>
#include <com/sun/star/ucb/XContentAccess.hpp>
#include <com/sun/star/ucb/XCommandEnvironment.hpp>

#include <com/sun/star/uno/Reference.hxx>
#include <com/sun/star/uno/Sequence.hxx>

#include <comphelper/processfactory.hxx>

#include <rtl/ustring.hxx>

#include <ucbhelper/content.hxx>

#include <DirectoryStream.hxx>
#include <WPXSvInputStream.hxx>

namespace io = com::sun::star::io;
namespace sdbc = com::sun::star::sdbc;
namespace ucb = com::sun::star::ucb;
namespace uno = com::sun::star::uno;

namespace writerperfect
{
namespace
{
uno::Reference<io::XInputStream> findStream(ucbhelper::Content& rContent, const OUString& rName)
{
    uno::Reference<io::XInputStream> xInputStream;

    uno::Sequence<OUString> lPropNames{ "Title" };
    try
    {
        const uno::Reference<sdbc::XResultSet> xResultSet(
            rContent.createCursor(lPropNames, ucbhelper::INCLUDE_DOCUMENTS_ONLY));
        if (xResultSet->first())
        {
            const uno::Reference<ucb::XContentAccess> xContentAccess(xResultSet,
                                                                     uno::UNO_QUERY_THROW);
            const uno::Reference<sdbc::XRow> xRow(xResultSet, uno::UNO_QUERY_THROW);
            do
            {
                const OUString aTitle(xRow->getString(1));
                if (aTitle == rName)
                {
                    const uno::Reference<ucb::XContent> xSubContent(xContentAccess->queryContent());
                    ucbhelper::Content aSubContent(xSubContent,
                                                   uno::Reference<ucb::XCommandEnvironment>(),
                                                   comphelper::getProcessComponentContext());
                    xInputStream = aSubContent.openStream();
                    break;
                }
            } while (xResultSet->next());
        }
    }
    catch (const uno::RuntimeException&)
    {
        // ignore
    }
    catch (const uno::Exception&)
    {
        // ignore
    }

    return xInputStream;
}
}

struct DirectoryStream::Impl
{
    explicit Impl(const uno::Reference<ucb::XContent>& rxContent);

    uno::Reference<ucb::XContent> xContent;
};

DirectoryStream::Impl::Impl(const uno::Reference<ucb::XContent>& rxContent)
    : xContent(rxContent)
{
}

DirectoryStream::DirectoryStream(const css::uno::Reference<css::ucb::XContent>& xContent)
    : m_pImpl(isDirectory(xContent) ? new Impl(xContent) : nullptr)
{
}

DirectoryStream::~DirectoryStream() {}

bool DirectoryStream::isDirectory(const css::uno::Reference<css::ucb::XContent>& xContent)
{
    try
    {
        if (!xContent.is())
            return false;

        ucbhelper::Content aContent(xContent, uno::Reference<ucb::XCommandEnvironment>(),
                                    comphelper::getProcessComponentContext());
        return aContent.isFolder();
    }
    catch (...)
    {
        return false;
    }
}

std::unique_ptr<DirectoryStream>
DirectoryStream::createForParent(const css::uno::Reference<css::ucb::XContent>& xContent)
{
    try
    {
        if (!xContent.is())
            return nullptr;

        std::unique_ptr<DirectoryStream> pDir;

        const uno::Reference<css::container::XChild> xChild(xContent, uno::UNO_QUERY);
        if (xChild.is())
        {
            const uno::Reference<ucb::XContent> xDirContent(xChild->getParent(), uno::UNO_QUERY);
            if (xDirContent.is())
            {
                pDir = std::make_unique<DirectoryStream>(xDirContent);
                if (!pDir->isStructured())
                    pDir.reset();
            }
        }

        return pDir;
    }
    catch (...)
    {
        return nullptr;
    }
}

css::uno::Reference<css::ucb::XContent> DirectoryStream::getContent() const
{
    if (!m_pImpl)
        return css::uno::Reference<css::ucb::XContent>();
    return m_pImpl->xContent;
}

bool DirectoryStream::isStructured() { return m_pImpl != nullptr; }

unsigned DirectoryStream::subStreamCount()
{
    // TODO: implement me
    return 0;
}

const char* DirectoryStream::subStreamName(unsigned /* id */)
{
    // TODO: implement me
    return nullptr;
}

bool DirectoryStream::existsSubStream(const char* /* name */)
{
    // TODO: implement me
    return false;
}

librevenge::RVNGInputStream* DirectoryStream::getSubStreamByName(const char* const pName)
{
    if (!m_pImpl)
        return nullptr;

    ucbhelper::Content aContent(m_pImpl->xContent, uno::Reference<ucb::XCommandEnvironment>(),
                                comphelper::getProcessComponentContext());
    const uno::Reference<io::XInputStream> xInputStream(
        findStream(aContent, OUString::createFromAscii(pName)));
    if (xInputStream.is())
        return new WPXSvInputStream(xInputStream);

    return nullptr;
}

librevenge::RVNGInputStream* DirectoryStream::getSubStreamById(unsigned /* id */)
{
    // TODO: implement me
    return nullptr;
}

const unsigned char* DirectoryStream::read(unsigned long, unsigned long& nNumBytesRead)
{
    nNumBytesRead = 0;
    return nullptr;
}

int DirectoryStream::seek(tools::Long, librevenge::RVNG_SEEK_TYPE) { return -1; }

tools::Long DirectoryStream::tell() { return 0; }

bool DirectoryStream::isEnd() { return true; }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */