LibreOffice Module connectivity (master) 1
WConnection.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
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 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
22#include <writer/WCatalog.hxx>
23#include <writer/WDriver.hxx>
25#include <strings.hrc>
26#include <com/sun/star/frame/Desktop.hpp>
27#include <com/sun/star/text/XTextDocument.hpp>
28#include <tools/urlobj.hxx>
29#include <sal/log.hxx>
36
37using namespace ::com::sun::star;
38
40
42{
44 : OConnection(_pDriver)
45{
46}
47
49
50void OWriterConnection::construct(const OUString& rURL,
51 const uno::Sequence<beans::PropertyValue>& rInfo)
52{
53 // open file
54
55 sal_Int32 nLen = rURL.indexOf(':');
56 nLen = rURL.indexOf(':', nLen + 1);
57 OUString aDSN(rURL.copy(nLen + 1));
58
59 m_aFileName = aDSN;
61 aURL.SetSmartProtocol(INetProtocol::File);
62 {
63 SvtPathOptions aPathOptions;
65 }
66 aURL.SetSmartURL(m_aFileName);
67 if (aURL.GetProtocol() == INetProtocol::NotValid)
68 {
69 // don't pass invalid URL to loadComponentFromURL
70 throw sdbc::SQLException();
71 }
73
74 m_sPassword.clear();
75 const char pPwd[] = "password";
76
77 const beans::PropertyValue* pIter = rInfo.getConstArray();
78 const beans::PropertyValue* pEnd = pIter + rInfo.getLength();
79 for (; pIter != pEnd; ++pIter)
80 {
81 if (pIter->Name == pPwd)
82 {
83 pIter->Value >>= m_sPassword;
84 break;
85 }
86 } // for(;pIter != pEnd;++pIter)
87 ODocHolder aDocHolder(this); // just to test that the doc can be loaded
88 acquireDoc();
89}
90
91uno::Reference<text::XTextDocument> const& OWriterConnection::acquireDoc()
92{
93 if (m_xDoc.is())
94 {
95 osl_atomic_increment(&m_nDocCount);
96 return m_xDoc;
97 }
98 // open read-only as long as updating isn't implemented
99 uno::Sequence<beans::PropertyValue> aArgs(m_sPassword.isEmpty() ? 2 : 3);
100 auto pArgs = aArgs.getArray();
101 pArgs[0].Name = "Hidden";
102 pArgs[0].Value <<= true;
103 pArgs[1].Name = "ReadOnly";
104 pArgs[1].Value <<= true;
105
106 if (!m_sPassword.isEmpty())
107 {
108 pArgs[2].Name = "Password";
109 pArgs[2].Value <<= m_sPassword;
110 }
111
112 uno::Reference<frame::XDesktop2> xDesktop
113 = frame::Desktop::create(getDriver()->getComponentContext());
114 uno::Reference<lang::XComponent> xComponent;
115 uno::Any aLoaderException;
116 try
117 {
118 xComponent = xDesktop->loadComponentFromURL(m_aFileName, "_blank", 0, aArgs);
119 }
120 catch (const uno::Exception&)
121 {
122 aLoaderException = ::cppu::getCaughtException();
123 }
124
125 m_xDoc.set(xComponent, uno::UNO_QUERY);
126
127 // if the URL is not a text document, throw the exception here
128 // instead of at the first access to it
129 if (!m_xDoc.is())
130 {
131 if (aLoaderException.hasValue())
132 {
133 uno::Exception aLoaderError;
134 OSL_VERIFY(aLoaderException >>= aLoaderError);
135
136 SAL_WARN("connectivity.writer",
137 "empty m_xDoc, " << exceptionToString(aLoaderException));
138 }
139
141 STR_COULD_NOT_LOAD_FILE, "$filename$", m_aFileName));
143 }
144 osl_atomic_increment(&m_nDocCount);
147 return m_xDoc;
148}
149
151{
152 if (osl_atomic_decrement(&m_nDocCount) == 0)
153 {
155 {
156 m_xCloseVetoButTerminateListener->stop(); // dispose m_xDoc
158 }
159 m_xDoc.clear();
160 }
161}
162
164{
165 ::osl::MutexGuard aGuard(m_aMutex);
166
167 m_nDocCount = 0;
169 {
170 m_xCloseVetoButTerminateListener->stop(); // dispose m_xDoc
172 }
173 m_xDoc.clear();
174
175 OConnection::disposing();
176}
177
178// XServiceInfo
179
180IMPLEMENT_SERVICE_INFO(OWriterConnection, "com.sun.star.sdbc.drivers.writer.Connection",
181 "com.sun.star.sdbc.Connection")
182
183uno::Reference<sdbc::XDatabaseMetaData> SAL_CALL OWriterConnection::getMetaData()
184{
185 ::osl::MutexGuard aGuard(m_aMutex);
186 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
187
188 uno::Reference<sdbc::XDatabaseMetaData> xMetaData = m_xMetaData;
189 if (!xMetaData.is())
190 {
191 xMetaData = new OWriterDatabaseMetaData(this);
192 m_xMetaData = xMetaData;
193 }
194
195 return xMetaData;
196}
197
198css::uno::Reference<css::sdbcx::XTablesSupplier> OWriterConnection::createCatalog()
199{
200 ::osl::MutexGuard aGuard(m_aMutex);
201 uno::Reference<css::sdbcx::XTablesSupplier> xTab = m_xCatalog;
202 if (!xTab.is())
203 {
204 xTab = new OWriterCatalog(this);
205 m_xCatalog = xTab;
206 }
207 return xTab;
208}
209
210uno::Reference<sdbc::XStatement> SAL_CALL OWriterConnection::createStatement()
211{
212 ::osl::MutexGuard aGuard(m_aMutex);
213 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
214
215 uno::Reference<sdbc::XStatement> xReturn = new component::OComponentStatement(this);
216 m_aStatements.push_back(uno::WeakReferenceHelper(xReturn));
217 return xReturn;
218}
219
220uno::Reference<sdbc::XPreparedStatement>
221 SAL_CALL OWriterConnection::prepareStatement(const OUString& sql)
222{
223 ::osl::MutexGuard aGuard(m_aMutex);
224 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
225
228 pStmt->construct(sql);
229 m_aStatements.push_back(uno::WeakReferenceHelper(*pStmt));
230 return pStmt;
231}
232
233uno::Reference<sdbc::XPreparedStatement>
234 SAL_CALL OWriterConnection::prepareCall(const OUString& /*sql*/)
235{
236 ::osl::MutexGuard aGuard(m_aMutex);
237 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
238
239 ::dbtools::throwFeatureNotImplementedSQLException("XConnection::prepareCall", *this);
240 return nullptr;
241}
242
243} // namespace
244
245/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString SubstituteVariable(const OUString &rVar) const
connectivity::OWeakRefArray m_aStatements
Definition: TConnection.hxx:47
OUString getResourceStringWithSubstitution(TranslateId pResId, const char *_pAsciiPatternToReplace, const OUString &_rStringToSubstitute) const
loads a string from the shared resource file, and replaces a given ASCII pattern with a given string
Prepared statement implementation for Writer tables and Calc sheets.
Statement implementation for Writer tables and Calc sheets.
Definition: CStatement.hxx:29
css::uno::WeakReference< css::sdbcx::XTablesSupplier > m_xCatalog
Definition: FConnection.hxx:41
OFileDriver * getDriver() const
css::uno::Reference< css::sdbc::XPreparedStatement > SAL_CALL prepareStatement(const OUString &sql) override
rtl::Reference< CloseVetoButTerminateListener > m_xCloseVetoButTerminateListener
css::uno::Reference< css::sdbcx::XTablesSupplier > createCatalog() override
css::uno::Reference< css::text::XTextDocument > m_xDoc
Definition: WConnection.hxx:44
void SAL_CALL disposing() override
css::uno::Reference< css::sdbc::XStatement > SAL_CALL createStatement() override
void construct(const OUString &rURL, const css::uno::Sequence< css::beans::PropertyValue > &rInfo) override
Definition: WConnection.cxx:50
css::uno::Reference< css::sdbc::XPreparedStatement > SAL_CALL prepareCall(const OUString &sql) override
css::uno::Reference< css::text::XTextDocument > const & acquireDoc()
Definition: WConnection.cxx:91
OString exceptionToString(const css::uno::Any &caught)
URL aURL
std::mutex m_aMutex
#define SAL_WARN(area, stream)
Reference< XComponentContext > getComponentContext(Reference< XMultiServiceFactory > const &factory)
IMPLEMENT_SERVICE_INFO(OWriterConnection, "com.sun.star.sdbc.drivers.writer.Connection", "com.sun.star.sdbc.Connection") uno
void checkDisposed(bool _bThrow)
Definition: dbtools.cxx:1951
void throwFeatureNotImplementedSQLException(const OUString &_rFeatureName, const Reference< XInterface > &_rxContext, const Any &_rNextException)
void throwGenericSQLException(const OUString &_rMsg, const css::uno::Reference< css::uno::XInterface > &_rxSource)
throw a generic SQLException, i.e.
bool hasValue()