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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* -*- 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 .
 */

#include <writer/WConnection.hxx>
#include <writer/WDatabaseMetaData.hxx>
#include <writer/WCatalog.hxx>
#include <writer/WDriver.hxx>
#include <resource/sharedresources.hxx>
#include <strings.hrc>
#include <com/sun/star/frame/Desktop.hpp>
#include <com/sun/star/text/XTextDocument.hpp>
#include <tools/urlobj.hxx>
#include <sal/log.hxx>
#include <component/CPreparedStatement.hxx>
#include <component/CStatement.hxx>
#include <unotools/pathoptions.hxx>
#include <connectivity/dbexception.hxx>
#include <cppuhelper/exc_hlp.hxx>

using namespace ::com::sun::star;

using OConnection_BASE = connectivity::file::OConnection;

namespace connectivity
{
namespace writer
{
OWriterConnection::OWriterConnection(ODriver* _pDriver)
    : OConnection(_pDriver)
    , m_nDocCount(0)
{
}

OWriterConnection::~OWriterConnection() = default;

void OWriterConnection::construct(const OUString& rURL,
                                  const uno::Sequence<beans::PropertyValue>& rInfo)
{
    //  open file

    sal_Int32 nLen = rURL.indexOf(':');
    nLen = rURL.indexOf(':', nLen + 1);
    OUString aDSN(rURL.copy(nLen + 1));

    m_aFileName = aDSN;
    INetURLObject aURL;
    aURL.SetSmartProtocol(INetProtocol::File);
    {
        SvtPathOptions aPathOptions;
        m_aFileName = aPathOptions.SubstituteVariable(m_aFileName);
    }
    aURL.SetSmartURL(m_aFileName);
    if (aURL.GetProtocol() == INetProtocol::NotValid)
    {
        //  don't pass invalid URL to loadComponentFromURL
        throw sdbc::SQLException();
    }
    m_aFileName = aURL.GetMainURL(INetURLObject::DecodeMechanism::NONE);

    m_sPassword.clear();
    const char pPwd[] = "password";

    const beans::PropertyValue* pIter = rInfo.getConstArray();
    const beans::PropertyValue* pEnd = pIter + rInfo.getLength();
    for (; pIter != pEnd; ++pIter)
    {
        if (pIter->Name == pPwd)
        {
            pIter->Value >>= m_sPassword;
            break;
        }
    } // for(;pIter != pEnd;++pIter)
    ODocHolder aDocHolder(this); // just to test that the doc can be loaded
    acquireDoc();
}

uno::Reference<text::XTextDocument> const& OWriterConnection::acquireDoc()
{
    if (m_xDoc.is())
    {
        osl_atomic_increment(&m_nDocCount);
        return m_xDoc;
    }
    //  open read-only as long as updating isn't implemented
    uno::Sequence<beans::PropertyValue> aArgs(2);
    aArgs[0].Name = "Hidden";
    aArgs[0].Value <<= true;
    aArgs[1].Name = "ReadOnly";
    aArgs[1].Value <<= true;

    if (!m_sPassword.isEmpty())
    {
        const sal_Int32 nPos = aArgs.getLength();
        aArgs.realloc(nPos + 1);
        aArgs[nPos].Name = "Password";
        aArgs[nPos].Value <<= m_sPassword;
    }

    uno::Reference<frame::XDesktop2> xDesktop
        = frame::Desktop::create(getDriver()->getComponentContext());
    uno::Reference<lang::XComponent> xComponent;
    uno::Any aLoaderException;
    try
    {
        xComponent = xDesktop->loadComponentFromURL(m_aFileName, "_blank", 0, aArgs);
    }
    catch (const uno::Exception&)
    {
        aLoaderException = ::cppu::getCaughtException();
    }

    m_xDoc.set(xComponent, uno::UNO_QUERY);

    //  if the URL is not a text document, throw the exception here
    //  instead of at the first access to it
    if (!m_xDoc.is())
    {
        if (aLoaderException.hasValue())
        {
            uno::Exception aLoaderError;
            OSL_VERIFY(aLoaderException >>= aLoaderError);

            SAL_WARN("connectivity.writer",
                     "empty m_xDoc, exception type: " << aLoaderException.getValueTypeName()
                                                      << ", error message: " << aLoaderError);
        }

        const OUString sError(m_aResources.getResourceStringWithSubstitution(
            STR_COULD_NOT_LOAD_FILE, "$filename$", m_aFileName));
        ::dbtools::throwGenericSQLException(sError, *this);
    }
    osl_atomic_increment(&m_nDocCount);
    m_xCloseVetoButTerminateListener.set(new CloseVetoButTerminateListener);
    m_xCloseVetoButTerminateListener->start(m_xDoc, xDesktop);
    return m_xDoc;
}

void OWriterConnection::releaseDoc()
{
    if (osl_atomic_decrement(&m_nDocCount) == 0)
    {
        if (m_xCloseVetoButTerminateListener.is())
        {
            m_xCloseVetoButTerminateListener->stop(); // dispose m_xDoc
            m_xCloseVetoButTerminateListener.clear();
        }
        m_xDoc.clear();
    }
}

void OWriterConnection::disposing()
{
    ::osl::MutexGuard aGuard(m_aMutex);

    m_nDocCount = 0;
    if (m_xCloseVetoButTerminateListener.is())
    {
        m_xCloseVetoButTerminateListener->stop(); // dispose m_xDoc
        m_xCloseVetoButTerminateListener.clear();
    }
    m_xDoc.clear();

    OConnection::disposing();
}

// XServiceInfo

IMPLEMENT_SERVICE_INFO(OWriterConnection, "com.sun.star.sdbc.drivers.writer.Connection",
                       "com.sun.star.sdbc.Connection")

uno::Reference<sdbc::XDatabaseMetaData> SAL_CALL OWriterConnection::getMetaData()
{
    ::osl::MutexGuard aGuard(m_aMutex);
    checkDisposed(OConnection_BASE::rBHelper.bDisposed);

    uno::Reference<sdbc::XDatabaseMetaData> xMetaData = m_xMetaData;
    if (!xMetaData.is())
    {
        xMetaData = new OWriterDatabaseMetaData(this);
        m_xMetaData = xMetaData;
    }

    return xMetaData;
}

css::uno::Reference<css::sdbcx::XTablesSupplier> OWriterConnection::createCatalog()
{
    ::osl::MutexGuard aGuard(m_aMutex);
    uno::Reference<css::sdbcx::XTablesSupplier> xTab = m_xCatalog;
    if (!xTab.is())
    {
        auto pCat = new OWriterCatalog(this);
        xTab = pCat;
        m_xCatalog = xTab;
    }
    return xTab;
}

uno::Reference<sdbc::XStatement> SAL_CALL OWriterConnection::createStatement()
{
    ::osl::MutexGuard aGuard(m_aMutex);
    checkDisposed(OConnection_BASE::rBHelper.bDisposed);

    uno::Reference<sdbc::XStatement> xReturn = new component::OComponentStatement(this);
    m_aStatements.push_back(uno::WeakReferenceHelper(xReturn));
    return xReturn;
}

uno::Reference<sdbc::XPreparedStatement>
    SAL_CALL OWriterConnection::prepareStatement(const OUString& sql)
{
    ::osl::MutexGuard aGuard(m_aMutex);
    checkDisposed(OConnection_BASE::rBHelper.bDisposed);

    auto pStmt = new component::OComponentPreparedStatement(this);
    uno::Reference<sdbc::XPreparedStatement> xHoldAlive = pStmt;<--- Variable 'xHoldAlive' is assigned a value that is never used.
    pStmt->construct(sql);
    m_aStatements.push_back(uno::WeakReferenceHelper(*pStmt));
    return pStmt;
}

uno::Reference<sdbc::XPreparedStatement>
    SAL_CALL OWriterConnection::prepareCall(const OUString& /*sql*/)
{
    ::osl::MutexGuard aGuard(m_aMutex);
    checkDisposed(OConnection_BASE::rBHelper.bDisposed);

    ::dbtools::throwFeatureNotImplementedSQLException("XConnection::prepareCall", *this);
    return nullptr;
}

} // namespace writer
} // namespace connectivity

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