1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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 * This file incorporates work covered by the following license notice:
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 .
20 #include <writer/WConnection.hxx>
21 #include <writer/WDatabaseMetaData.hxx>
22 #include <writer/WCatalog.hxx>
23 #include <writer/WDriver.hxx>
24 #include <resource/sharedresources.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>
30 #include <component/CPreparedStatement.hxx>
31 #include <component/CStatement.hxx>
32 #include <unotools/pathoptions.hxx>
33 #include <connectivity/dbexception.hxx>
34 #include <cppuhelper/exc_hlp.hxx>
35 #include <comphelper/diagnose_ex.hxx>
37 using namespace ::com::sun::star
;
39 using OConnection_BASE
= connectivity::file::OConnection
;
41 namespace connectivity::writer
43 OWriterConnection::OWriterConnection(ODriver
* _pDriver
)
44 : OConnection(_pDriver
)
48 OWriterConnection::~OWriterConnection() = default;
50 void OWriterConnection::construct(const OUString
& rURL
,
51 const uno::Sequence
<beans::PropertyValue
>& rInfo
)
55 sal_Int32 nLen
= rURL
.indexOf(':');
56 nLen
= rURL
.indexOf(':', nLen
+ 1);
58 m_aFileName
= rURL
.copy(nLen
+ 1); // DSN
61 aURL
.SetSmartProtocol(INetProtocol::File
);
63 SvtPathOptions aPathOptions
;
64 m_aFileName
= aPathOptions
.SubstituteVariable(m_aFileName
);
66 aURL
.SetSmartURL(m_aFileName
);
67 if (aURL
.GetProtocol() == INetProtocol::NotValid
)
69 // don't pass invalid URL to loadComponentFromURL
70 throw sdbc::SQLException();
72 m_aFileName
= aURL
.GetMainURL(INetURLObject::DecodeMechanism::NONE
);
75 const char pPwd
[] = "password";
77 const beans::PropertyValue
* pIter
= rInfo
.getConstArray();
78 const beans::PropertyValue
* pEnd
= pIter
+ rInfo
.getLength();
79 for (; pIter
!= pEnd
; ++pIter
)
81 if (pIter
->Name
== pPwd
)
83 pIter
->Value
>>= m_sPassword
;
86 } // for(;pIter != pEnd;++pIter)
87 ODocHolder
aDocHolder(this); // just to test that the doc can be loaded
91 uno::Reference
<text::XTextDocument
> const& OWriterConnection::acquireDoc()
95 osl_atomic_increment(&m_nDocCount
);
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;
106 if (!m_sPassword
.isEmpty())
108 pArgs
[2].Name
= "Password";
109 pArgs
[2].Value
<<= m_sPassword
;
112 uno::Reference
<frame::XDesktop2
> xDesktop
113 = frame::Desktop::create(getDriver()->getComponentContext());
114 uno::Reference
<lang::XComponent
> xComponent
;
115 uno::Any aLoaderException
;
118 xComponent
= xDesktop
->loadComponentFromURL(m_aFileName
, u
"_blank"_ustr
, 0, aArgs
);
120 catch (const uno::Exception
&)
122 aLoaderException
= ::cppu::getCaughtException();
125 m_xDoc
.set(xComponent
, uno::UNO_QUERY
);
127 // if the URL is not a text document, throw the exception here
128 // instead of at the first access to it
131 if (aLoaderException
.hasValue())
133 uno::Exception aLoaderError
;
134 OSL_VERIFY(aLoaderException
>>= aLoaderError
);
136 SAL_WARN("connectivity.writer",
137 "empty m_xDoc, " << exceptionToString(aLoaderException
));
140 const OUString
sError(m_aResources
.getResourceStringWithSubstitution(
141 STR_COULD_NOT_LOAD_FILE
, "$filename$", m_aFileName
));
142 ::dbtools::throwGenericSQLException(sError
, *this);
144 osl_atomic_increment(&m_nDocCount
);
145 m_xCloseVetoButTerminateListener
.set(new CloseVetoButTerminateListener
);
146 m_xCloseVetoButTerminateListener
->start(m_xDoc
, xDesktop
);
150 void OWriterConnection::releaseDoc()
152 if (osl_atomic_decrement(&m_nDocCount
) == 0)
154 if (m_xCloseVetoButTerminateListener
.is())
156 m_xCloseVetoButTerminateListener
->stop(); // dispose m_xDoc
157 m_xCloseVetoButTerminateListener
.clear();
163 void OWriterConnection::disposing()
165 ::osl::MutexGuard
aGuard(m_aMutex
);
168 if (m_xCloseVetoButTerminateListener
.is())
170 m_xCloseVetoButTerminateListener
->stop(); // dispose m_xDoc
171 m_xCloseVetoButTerminateListener
.clear();
175 OConnection::disposing();
180 IMPLEMENT_SERVICE_INFO(OWriterConnection
, u
"com.sun.star.sdbc.drivers.writer.Connection"_ustr
,
181 u
"com.sun.star.sdbc.Connection"_ustr
)
183 uno::Reference
<sdbc::XDatabaseMetaData
> SAL_CALL
OWriterConnection::getMetaData()
185 ::osl::MutexGuard
aGuard(m_aMutex
);
186 checkDisposed(OConnection_BASE::rBHelper
.bDisposed
);
188 uno::Reference
<sdbc::XDatabaseMetaData
> xMetaData
= m_xMetaData
;
191 xMetaData
= new OWriterDatabaseMetaData(this);
192 m_xMetaData
= xMetaData
;
198 css::uno::Reference
<css::sdbcx::XTablesSupplier
> OWriterConnection::createCatalog()
200 ::osl::MutexGuard
aGuard(m_aMutex
);
201 rtl::Reference
<connectivity::sdbcx::OCatalog
> xTab
= m_xCatalog
;
204 xTab
= new OWriterCatalog(this);
205 m_xCatalog
= xTab
.get();
210 uno::Reference
<sdbc::XStatement
> SAL_CALL
OWriterConnection::createStatement()
212 ::osl::MutexGuard
aGuard(m_aMutex
);
213 checkDisposed(OConnection_BASE::rBHelper
.bDisposed
);
215 uno::Reference
<sdbc::XStatement
> xReturn
= new component::OComponentStatement(this);
216 m_aStatements
.emplace_back(xReturn
);
220 uno::Reference
<sdbc::XPreparedStatement
>
221 SAL_CALL
OWriterConnection::prepareStatement(const OUString
& sql
)
223 ::osl::MutexGuard
aGuard(m_aMutex
);
224 checkDisposed(OConnection_BASE::rBHelper
.bDisposed
);
226 rtl::Reference
<component::OComponentPreparedStatement
> pStmt
227 = new component::OComponentPreparedStatement(this);
228 pStmt
->construct(sql
);
229 m_aStatements
.emplace_back(*pStmt
);
233 uno::Reference
<sdbc::XPreparedStatement
>
234 SAL_CALL
OWriterConnection::prepareCall(const OUString
& /*sql*/)
236 ::osl::MutexGuard
aGuard(m_aMutex
);
237 checkDisposed(OConnection_BASE::rBHelper
.bDisposed
);
239 ::dbtools::throwFeatureNotImplementedSQLException(u
"XConnection::prepareCall"_ustr
, *this);
244 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */