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 <calc/CConnection.hxx>
21 #include <calc/CDatabaseMetaData.hxx>
22 #include <calc/CCatalog.hxx>
23 #include <calc/CDriver.hxx>
24 #include <resource/sharedresources.hxx>
25 #include <com/sun/star/frame/Desktop.hpp>
26 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
27 #include <tools/urlobj.hxx>
28 #include <component/CPreparedStatement.hxx>
29 #include <component/CStatement.hxx>
30 #include <unotools/pathoptions.hxx>
31 #include <connectivity/dbexception.hxx>
32 #include <cppuhelper/exc_hlp.hxx>
33 #include <strings.hrc>
35 using namespace connectivity::calc
;
36 using namespace connectivity::file
;
38 typedef connectivity::file::OConnection OConnection_BASE
;
41 using namespace ::com::sun::star::uno
;
42 using namespace ::com::sun::star::beans
;
43 using namespace ::com::sun::star::sdbcx
;
44 using namespace ::com::sun::star::sdbc
;
45 using namespace ::com::sun::star::lang
;
46 using namespace ::com::sun::star::frame
;
47 using namespace ::com::sun::star::sheet
;
50 OCalcConnection::OCalcConnection(ODriver
* _pDriver
) : OConnection(_pDriver
),m_nDocCount(0)
52 // m_aFilenameExtension is not used
55 OCalcConnection::~OCalcConnection()
59 void OCalcConnection::construct(const OUString
& url
,const Sequence
< PropertyValue
>& info
)
63 sal_Int32 nLen
= url
.indexOf(':');
64 nLen
= url
.indexOf(':',nLen
+1);
66 m_aFileName
= url
.copy(nLen
+1); // DSN
69 aURL
.SetSmartProtocol(INetProtocol::File
);
71 SvtPathOptions aPathOptions
;
72 m_aFileName
= aPathOptions
.SubstituteVariable(m_aFileName
);
74 aURL
.SetSmartURL(m_aFileName
);
75 if ( aURL
.GetProtocol() == INetProtocol::NotValid
)
77 // don't pass invalid URL to loadComponentFromURL
80 m_aFileName
= aURL
.GetMainURL(INetURLObject::DecodeMechanism::NONE
);
84 for (auto& propval
: info
)
86 if (propval
.Name
== "password")
88 propval
.Value
>>= m_sPassword
;
92 ODocHolder
aDocHolder(this); // just to test that the doc can be loaded
96 Reference
< XSpreadsheetDocument
> const & OCalcConnection::acquireDoc()
100 osl_atomic_increment(&m_nDocCount
);
103 // open read-only as long as updating isn't implemented
104 Sequence
<PropertyValue
> aArgs(m_sPassword
.isEmpty() ? 2 : 3);
105 auto pArgs
= aArgs
.getArray();
106 pArgs
[0].Name
= "Hidden";
107 pArgs
[0].Value
<<= true;
108 pArgs
[1].Name
= "ReadOnly";
109 pArgs
[1].Value
<<= true;
111 if ( !m_sPassword
.isEmpty() )
113 pArgs
[2].Name
= "Password";
114 pArgs
[2].Value
<<= m_sPassword
;
117 Reference
< XDesktop2
> xDesktop
= Desktop::create( getDriver()->getComponentContext() );
118 Reference
< XComponent
> xComponent
;
119 Any aLoaderException
;
122 xComponent
= xDesktop
->loadComponentFromURL(
123 m_aFileName
, u
"_blank"_ustr
, 0, aArgs
);
125 catch( const Exception
& )
127 aLoaderException
= ::cppu::getCaughtException();
130 m_xDoc
.set(xComponent
, UNO_QUERY
);
132 // if the URL is not a spreadsheet document, throw the exception here
133 // instead of at the first access to it
137 if ( aLoaderException
.hasValue() )
139 Exception aLoaderError
;
140 OSL_VERIFY( aLoaderException
>>= aLoaderError
);
142 SQLException
aDetailException(m_aResources
.getResourceStringWithSubstitution(
143 STR_LOAD_FILE_ERROR_MESSAGE
, "$exception_type$",
144 aLoaderException
.getValueTypeName(),
145 "$error_message$", aLoaderError
.Message
),
147 aErrorDetails
<<= aDetailException
;
150 const OUString
sError( m_aResources
.getResourceStringWithSubstitution(
151 STR_COULD_NOT_LOAD_FILE
,
152 "$filename$", m_aFileName
154 ::dbtools::throwGenericSQLException( sError
, *this, aErrorDetails
);
156 osl_atomic_increment(&m_nDocCount
);
157 m_xCloseVetoButTerminateListener
.set(new CloseVetoButTerminateListener
);
158 m_xCloseVetoButTerminateListener
->start(m_xDoc
, xDesktop
);
162 void OCalcConnection::releaseDoc()
164 if ( osl_atomic_decrement(&m_nDocCount
) == 0 )
166 if (m_xCloseVetoButTerminateListener
.is())
168 m_xCloseVetoButTerminateListener
->stop(); // dispose m_xDoc
169 m_xCloseVetoButTerminateListener
.clear();
175 void OCalcConnection::disposing()
177 ::osl::MutexGuard
aGuard(m_aMutex
);
180 if (m_xCloseVetoButTerminateListener
.is())
182 m_xCloseVetoButTerminateListener
->stop(); // dispose m_xDoc
183 m_xCloseVetoButTerminateListener
.clear();
187 OConnection::disposing();
193 IMPLEMENT_SERVICE_INFO(OCalcConnection
, u
"com.sun.star.sdbc.drivers.calc.Connection"_ustr
, u
"com.sun.star.sdbc.Connection"_ustr
)
196 Reference
< XDatabaseMetaData
> SAL_CALL
OCalcConnection::getMetaData( )
198 ::osl::MutexGuard
aGuard( m_aMutex
);
199 checkDisposed(OConnection_BASE::rBHelper
.bDisposed
);
202 Reference
< XDatabaseMetaData
> xMetaData
= m_xMetaData
;
205 xMetaData
= new OCalcDatabaseMetaData(this);
206 m_xMetaData
= xMetaData
;
213 css::uno::Reference
< XTablesSupplier
> OCalcConnection::createCatalog()
215 ::osl::MutexGuard
aGuard( m_aMutex
);
216 rtl::Reference
< connectivity::sdbcx::OCatalog
> xTab
= m_xCatalog
;
219 xTab
= new OCalcCatalog(this);
220 m_xCatalog
= xTab
.get();
226 Reference
< XStatement
> SAL_CALL
OCalcConnection::createStatement( )
228 ::osl::MutexGuard
aGuard( m_aMutex
);
229 checkDisposed(OConnection_BASE::rBHelper
.bDisposed
);
232 Reference
< XStatement
> xReturn
= new connectivity::component::OComponentStatement(this);
233 m_aStatements
.push_back(WeakReferenceHelper(xReturn
));
238 Reference
< XPreparedStatement
> SAL_CALL
OCalcConnection::prepareStatement( const OUString
& sql
)
240 ::osl::MutexGuard
aGuard( m_aMutex
);
241 checkDisposed(OConnection_BASE::rBHelper
.bDisposed
);
244 rtl::Reference
<connectivity::component::OComponentPreparedStatement
> pStmt
= new connectivity::component::OComponentPreparedStatement(this);
245 pStmt
->construct(sql
);
246 m_aStatements
.push_back(WeakReferenceHelper(*pStmt
));
251 Reference
< XPreparedStatement
> SAL_CALL
OCalcConnection::prepareCall( const OUString
& /*sql*/ )
253 ::osl::MutexGuard
aGuard( m_aMutex
);
254 checkDisposed(OConnection_BASE::rBHelper
.bDisposed
);
256 ::dbtools::throwFeatureNotImplementedSQLException( u
"XConnection::prepareCall"_ustr
, *this );
259 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */