build fix
[LibreOffice.git] / connectivity / source / drivers / writer / WConnection.cxx
blob307ffa83c83f8c4ecc1d533a030239108b82d013
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "resource/common_res.hrc"
26 #include <com/sun/star/lang/DisposedException.hpp>
27 #include <com/sun/star/frame/Desktop.hpp>
28 #include <com/sun/star/text/XTextDocument.hpp>
29 #include <tools/urlobj.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/processfactory.hxx>
37 typedef connectivity::file::OConnection OConnection_BASE;
39 using namespace ::com::sun::star;
41 namespace connectivity
43 namespace writer
46 OWriterConnection::OWriterConnection(ODriver* _pDriver) : OConnection(_pDriver),m_nDocCount(0)
48 // m_aFilenameExtension is not used
51 OWriterConnection::~OWriterConnection()
55 void OWriterConnection::construct(const OUString& url,const uno::Sequence< beans::PropertyValue >& info) throw(sdbc::SQLException, uno::RuntimeException, uno::DeploymentException, std::exception)
57 // open file
59 sal_Int32 nLen = url.indexOf(':');
60 nLen = url.indexOf(':',nLen+1);
61 OUString aDSN(url.copy(nLen+1));
63 m_aFileName = aDSN;
64 INetURLObject aURL;
65 aURL.SetSmartProtocol(INetProtocol::File);
67 SvtPathOptions aPathOptions;
68 m_aFileName = aPathOptions.SubstituteVariable(m_aFileName);
70 aURL.SetSmartURL(m_aFileName);
71 if (aURL.GetProtocol() == INetProtocol::NotValid)
73 // don't pass invalid URL to loadComponentFromURL
74 throw sdbc::SQLException();
76 m_aFileName = aURL.GetMainURL(INetURLObject::DecodeMechanism::NONE);
78 m_sPassword.clear();
79 const char pPwd[] = "password";
81 const beans::PropertyValue* pIter = info.getConstArray();
82 const beans::PropertyValue* pEnd = pIter + info.getLength();
83 for (; pIter != pEnd; ++pIter)
85 if (pIter->Name == pPwd)
87 pIter->Value >>= m_sPassword;
88 break;
90 } // for(;pIter != pEnd;++pIter)
91 ODocHolder aDocHolder(this); // just to test that the doc can be loaded
92 acquireDoc();
95 uno::Reference<text::XTextDocument> const& OWriterConnection::acquireDoc()
97 if (m_xDoc.is())
99 osl_atomic_increment(&m_nDocCount);
100 return m_xDoc;
102 // open read-only as long as updating isn't implemented
103 uno::Sequence<beans::PropertyValue> aArgs(2);
104 aArgs[0].Name = "Hidden";
105 aArgs[0].Value <<= true;
106 aArgs[1].Name = "ReadOnly";
107 aArgs[1].Value <<= true;
109 if (!m_sPassword.isEmpty())
111 const sal_Int32 nPos = aArgs.getLength();
112 aArgs.realloc(nPos+1);
113 aArgs[nPos].Name = "Password";
114 aArgs[nPos].Value <<= m_sPassword;
117 uno::Reference< frame::XDesktop2 > xDesktop = frame::Desktop::create(getDriver()->getComponentContext());
118 uno::Reference< lang::XComponent > xComponent;
119 uno::Any aLoaderException;
122 xComponent = xDesktop->loadComponentFromURL(
123 m_aFileName, "_blank", 0, aArgs);
125 catch (const uno::Exception&)
127 aLoaderException = ::cppu::getCaughtException();
130 m_xDoc.set(xComponent, uno::UNO_QUERY);
132 // if the URL is not a text document, throw the exception here
133 // instead of at the first access to it
134 if (!m_xDoc.is())
136 if (aLoaderException.hasValue())
138 uno::Exception aLoaderError;
139 OSL_VERIFY(aLoaderException >>= aLoaderError);
141 SAL_WARN("connectivity.writer", "empty m_xDoc, exception type: " << aLoaderException.getValueTypeName() << ", error message: " << aLoaderError.Message);
144 const OUString sError(m_aResources.getResourceStringWithSubstitution(
145 STR_COULD_NOT_LOAD_FILE,
146 "$filename$", m_aFileName
148 ::dbtools::throwGenericSQLException(sError, *this);
150 osl_atomic_increment(&m_nDocCount);
151 m_xCloseVetoButTerminateListener.set(new CloseVetoButTerminateListener);
152 m_xCloseVetoButTerminateListener->start(m_xDoc, xDesktop);
153 return m_xDoc;
156 void OWriterConnection::releaseDoc()
158 if (osl_atomic_decrement(&m_nDocCount) == 0)
160 if (m_xCloseVetoButTerminateListener.is())
162 m_xCloseVetoButTerminateListener->stop(); // dispose m_xDoc
163 m_xCloseVetoButTerminateListener.clear();
165 m_xDoc.clear();
169 void OWriterConnection::disposing()
171 ::osl::MutexGuard aGuard(m_aMutex);
173 m_nDocCount = 0;
174 if (m_xCloseVetoButTerminateListener.is())
176 m_xCloseVetoButTerminateListener->stop(); // dispose m_xDoc
177 m_xCloseVetoButTerminateListener.clear();
179 m_xDoc.clear();
181 OConnection::disposing();
184 // XServiceInfo
187 IMPLEMENT_SERVICE_INFO(OWriterConnection, "com.sun.star.sdbc.drivers.writer.Connection", "com.sun.star.sdbc.Connection")
190 uno::Reference< sdbc::XDatabaseMetaData > SAL_CALL OWriterConnection::getMetaData() throw(sdbc::SQLException, uno::RuntimeException, std::exception)
192 ::osl::MutexGuard aGuard(m_aMutex);
193 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
196 uno::Reference< sdbc::XDatabaseMetaData > xMetaData = m_xMetaData;
197 if (!xMetaData.is())
199 xMetaData = new OWriterDatabaseMetaData(this);
200 m_xMetaData = xMetaData;
203 return xMetaData;
207 css::uno::Reference< css::sdbcx::XTablesSupplier > OWriterConnection::createCatalog()
209 ::osl::MutexGuard aGuard(m_aMutex);
210 uno::Reference< css::sdbcx::XTablesSupplier > xTab = m_xCatalog;
211 if (!xTab.is())
213 OWriterCatalog* pCat = new OWriterCatalog(this);
214 xTab = pCat;
215 m_xCatalog = xTab;
217 return xTab;
221 uno::Reference< sdbc::XStatement > SAL_CALL OWriterConnection::createStatement() throw(sdbc::SQLException, uno::RuntimeException, std::exception)
223 ::osl::MutexGuard aGuard(m_aMutex);
224 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
226 uno::Reference<sdbc::XStatement> xReturn = new component::OComponentStatement(this);
227 m_aStatements.push_back(uno::WeakReferenceHelper(xReturn));
228 return xReturn;
232 uno::Reference< sdbc::XPreparedStatement > SAL_CALL OWriterConnection::prepareStatement(const OUString& sql) throw(sdbc::SQLException, uno::RuntimeException, std::exception)
234 ::osl::MutexGuard aGuard(m_aMutex);
235 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
237 auto pStmt = new component::OComponentPreparedStatement(this);
238 uno::Reference<sdbc::XPreparedStatement> xHoldAlive = pStmt;
239 pStmt->construct(sql);
240 m_aStatements.push_back(uno::WeakReferenceHelper(*pStmt));
241 return pStmt;
245 uno::Reference< sdbc::XPreparedStatement > SAL_CALL OWriterConnection::prepareCall(const OUString& /*sql*/) throw(sdbc::SQLException, uno::RuntimeException, std::exception)
247 ::osl::MutexGuard aGuard(m_aMutex);
248 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
250 ::dbtools::throwFeatureNotImplementedSQLException("XConnection::prepareCall", *this);
251 return nullptr;
254 } // namespace writer
255 } // namespace connectivity
257 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */