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);
65 OUString
aDSN(url
.copy(nLen
+1));
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
);
83 const char pPwd
[] = "password";
85 const PropertyValue
*pIter
= info
.getConstArray();
86 const PropertyValue
*pEnd
= pIter
+ info
.getLength();
87 for(;pIter
!= pEnd
;++pIter
)
89 if(pIter
->Name
== pPwd
)
91 pIter
->Value
>>= m_sPassword
;
94 } // for(;pIter != pEnd;++pIter)
95 ODocHolder
aDocHolder(this); // just to test that the doc can be loaded
99 Reference
< XSpreadsheetDocument
> const & OCalcConnection::acquireDoc()
103 osl_atomic_increment(&m_nDocCount
);
106 // open read-only as long as updating isn't implemented
107 Sequence
<PropertyValue
> aArgs(2);
108 aArgs
[0].Name
= "Hidden";
109 aArgs
[0].Value
<<= true;
110 aArgs
[1].Name
= "ReadOnly";
111 aArgs
[1].Value
<<= true;
113 if ( !m_sPassword
.isEmpty() )
115 const sal_Int32 nPos
= aArgs
.getLength();
116 aArgs
.realloc(nPos
+1);
117 aArgs
[nPos
].Name
= "Password";
118 aArgs
[nPos
].Value
<<= m_sPassword
;
121 Reference
< XDesktop2
> xDesktop
= Desktop::create( getDriver()->getComponentContext() );
122 Reference
< XComponent
> xComponent
;
123 Any aLoaderException
;
126 xComponent
= xDesktop
->loadComponentFromURL(
127 m_aFileName
, "_blank", 0, aArgs
);
129 catch( const Exception
& )
131 aLoaderException
= ::cppu::getCaughtException();
134 m_xDoc
.set(xComponent
, UNO_QUERY
);
136 // if the URL is not a spreadsheet document, throw the exception here
137 // instead of at the first access to it
141 if ( aLoaderException
.hasValue() )
143 Exception aLoaderError
;
144 OSL_VERIFY( aLoaderException
>>= aLoaderError
);
146 SQLException aDetailException
;
147 aDetailException
.Message
= m_aResources
.getResourceStringWithSubstitution(
148 STR_LOAD_FILE_ERROR_MESSAGE
,
149 "$exception_type$", aLoaderException
.getValueTypeName(),
150 "$error_message$", aLoaderError
.Message
152 aErrorDetails
<<= aDetailException
;
155 const OUString
sError( m_aResources
.getResourceStringWithSubstitution(
156 STR_COULD_NOT_LOAD_FILE
,
157 "$filename$", m_aFileName
159 ::dbtools::throwGenericSQLException( sError
, *this, aErrorDetails
);
161 osl_atomic_increment(&m_nDocCount
);
162 m_xCloseVetoButTerminateListener
.set(new CloseVetoButTerminateListener
);
163 m_xCloseVetoButTerminateListener
->start(m_xDoc
, xDesktop
);
167 void OCalcConnection::releaseDoc()
169 if ( osl_atomic_decrement(&m_nDocCount
) == 0 )
171 if (m_xCloseVetoButTerminateListener
.is())
173 m_xCloseVetoButTerminateListener
->stop(); // dispose m_xDoc
174 m_xCloseVetoButTerminateListener
.clear();
180 void OCalcConnection::disposing()
182 ::osl::MutexGuard
aGuard(m_aMutex
);
185 if (m_xCloseVetoButTerminateListener
.is())
187 m_xCloseVetoButTerminateListener
->stop(); // dispose m_xDoc
188 m_xCloseVetoButTerminateListener
.clear();
192 OConnection::disposing();
198 IMPLEMENT_SERVICE_INFO(OCalcConnection
, "com.sun.star.sdbc.drivers.calc.Connection", "com.sun.star.sdbc.Connection")
201 Reference
< XDatabaseMetaData
> SAL_CALL
OCalcConnection::getMetaData( )
203 ::osl::MutexGuard
aGuard( m_aMutex
);
204 checkDisposed(OConnection_BASE::rBHelper
.bDisposed
);
207 Reference
< XDatabaseMetaData
> xMetaData
= m_xMetaData
;
210 xMetaData
= new OCalcDatabaseMetaData(this);
211 m_xMetaData
= xMetaData
;
218 css::uno::Reference
< XTablesSupplier
> OCalcConnection::createCatalog()
220 ::osl::MutexGuard
aGuard( m_aMutex
);
221 Reference
< XTablesSupplier
> xTab
= m_xCatalog
;
224 xTab
= new OCalcCatalog(this);
231 Reference
< XStatement
> SAL_CALL
OCalcConnection::createStatement( )
233 ::osl::MutexGuard
aGuard( m_aMutex
);
234 checkDisposed(OConnection_BASE::rBHelper
.bDisposed
);
237 Reference
< XStatement
> xReturn
= new connectivity::component::OComponentStatement(this);
238 m_aStatements
.push_back(WeakReferenceHelper(xReturn
));
243 Reference
< XPreparedStatement
> SAL_CALL
OCalcConnection::prepareStatement( const OUString
& sql
)
245 ::osl::MutexGuard
aGuard( m_aMutex
);
246 checkDisposed(OConnection_BASE::rBHelper
.bDisposed
);
249 rtl::Reference
<connectivity::component::OComponentPreparedStatement
> pStmt
= new connectivity::component::OComponentPreparedStatement(this);
250 pStmt
->construct(sql
);
251 m_aStatements
.push_back(WeakReferenceHelper(*pStmt
));
256 Reference
< XPreparedStatement
> SAL_CALL
OCalcConnection::prepareCall( const OUString
& /*sql*/ )
258 ::osl::MutexGuard
aGuard( m_aMutex
);
259 checkDisposed(OConnection_BASE::rBHelper
.bDisposed
);
261 ::dbtools::throwFeatureNotImplementedSQLException( "XConnection::prepareCall", *this );
266 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */