Line data Source code
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 :
20 : #include "calc/CConnection.hxx"
21 : #include "calc/CDatabaseMetaData.hxx"
22 : #include "calc/CCatalog.hxx"
23 : #include "calc/CDriver.hxx"
24 : #include "resource/calc_res.hrc"
25 : #include "resource/sharedresources.hxx"
26 : #include <com/sun/star/lang/DisposedException.hpp>
27 : #include <com/sun/star/frame/Desktop.hpp>
28 : #include <com/sun/star/frame/XComponentLoader.hpp>
29 : #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
30 : #include <tools/urlobj.hxx>
31 : #include "calc/CPreparedStatement.hxx"
32 : #include "calc/CStatement.hxx"
33 : #include <unotools/pathoptions.hxx>
34 : #include <unotools/closeveto.hxx>
35 : #include <connectivity/dbexception.hxx>
36 : #include <cppuhelper/exc_hlp.hxx>
37 : #include <comphelper/processfactory.hxx>
38 :
39 : using namespace connectivity::calc;
40 : using namespace connectivity::file;
41 :
42 : typedef connectivity::file::OConnection OConnection_BASE;
43 :
44 :
45 :
46 : using namespace ::com::sun::star::uno;
47 : using namespace ::com::sun::star::beans;
48 : using namespace ::com::sun::star::sdbcx;
49 : using namespace ::com::sun::star::sdbc;
50 : using namespace ::com::sun::star::lang;
51 : using namespace ::com::sun::star::frame;
52 : using namespace ::com::sun::star::sheet;
53 :
54 :
55 :
56 9 : OCalcConnection::OCalcConnection(ODriver* _pDriver) : OConnection(_pDriver),m_nDocCount(0)
57 : {
58 : // m_aFilenameExtension is not used
59 9 : }
60 :
61 16 : OCalcConnection::~OCalcConnection()
62 : {
63 16 : }
64 :
65 9 : void OCalcConnection::construct(const OUString& url,const Sequence< PropertyValue >& info)
66 : throw(SQLException, RuntimeException, DeploymentException)
67 : {
68 : // open file
69 :
70 9 : sal_Int32 nLen = url.indexOf(':');
71 9 : nLen = url.indexOf(':',nLen+1);
72 9 : OUString aDSN(url.copy(nLen+1));
73 :
74 9 : m_aFileName = aDSN;
75 18 : INetURLObject aURL;
76 9 : aURL.SetSmartProtocol(INetProtocol::File);
77 : {
78 9 : SvtPathOptions aPathOptions;
79 9 : m_aFileName = aPathOptions.SubstituteVariable(m_aFileName);
80 : }
81 9 : aURL.SetSmartURL(m_aFileName);
82 9 : if ( aURL.GetProtocol() == INetProtocol::NotValid )
83 : {
84 : // don't pass invalid URL to loadComponentFromURL
85 0 : throw SQLException();
86 : }
87 9 : m_aFileName = aURL.GetMainURL(INetURLObject::NO_DECODE);
88 :
89 9 : m_sPassword.clear();
90 9 : const char* pPwd = "password";
91 :
92 9 : const PropertyValue *pIter = info.getConstArray();
93 9 : const PropertyValue *pEnd = pIter + info.getLength();
94 9 : for(;pIter != pEnd;++pIter)
95 : {
96 0 : if(pIter->Name.equalsAscii(pPwd))
97 : {
98 0 : pIter->Value >>= m_sPassword;
99 0 : break;
100 : }
101 : } // for(;pIter != pEnd;++pIter)
102 18 : ODocHolder aDocHodler(this); // just to test that the doc can be loaded
103 18 : acquireDoc();
104 9 : }
105 :
106 44 : Reference< XSpreadsheetDocument> OCalcConnection::acquireDoc()
107 : {
108 44 : if ( m_xDoc.is() )
109 : {
110 35 : osl_atomic_increment(&m_nDocCount);
111 35 : return m_xDoc;
112 : }
113 : // open read-only as long as updating isn't implemented
114 9 : Sequence<PropertyValue> aArgs(2);
115 9 : aArgs[0].Name = "Hidden";
116 9 : aArgs[0].Value <<= true;
117 9 : aArgs[1].Name = "ReadOnly";
118 9 : aArgs[1].Value <<= true;
119 :
120 9 : if ( !m_sPassword.isEmpty() )
121 : {
122 0 : const sal_Int32 nPos = aArgs.getLength();
123 0 : aArgs.realloc(nPos+1);
124 0 : aArgs[nPos].Name = "Password";
125 0 : aArgs[nPos].Value <<= m_sPassword;
126 : }
127 :
128 18 : Reference< XDesktop2 > xDesktop = Desktop::create( getDriver()->getComponentContext() );
129 18 : Reference< XComponent > xComponent;
130 18 : Any aLoaderException;
131 : try
132 : {
133 27 : xComponent = xDesktop->loadComponentFromURL(
134 18 : m_aFileName, OUString("_blank"), 0, aArgs );
135 : }
136 0 : catch( const Exception& )
137 : {
138 0 : aLoaderException = ::cppu::getCaughtException();
139 : }
140 :
141 9 : m_xDoc.set(xComponent, UNO_QUERY );
142 :
143 : // if the URL is not a spreadsheet document, throw the exception here
144 : // instead of at the first access to it
145 9 : if ( !m_xDoc.is() )
146 : {
147 0 : Any aErrorDetails;
148 0 : if ( aLoaderException.hasValue() )
149 : {
150 0 : Exception aLoaderError;
151 0 : OSL_VERIFY( aLoaderException >>= aLoaderError );
152 :
153 0 : SQLException aDetailException;
154 0 : aDetailException.Message = m_aResources.getResourceStringWithSubstitution(
155 : STR_LOAD_FILE_ERROR_MESSAGE,
156 : "$exception_type$", aLoaderException.getValueTypeName(),
157 : "$error_message$", aLoaderError.Message
158 0 : );
159 0 : aErrorDetails <<= aDetailException;
160 : }
161 :
162 : const OUString sError( m_aResources.getResourceStringWithSubstitution(
163 : STR_COULD_NOT_LOAD_FILE,
164 : "$filename$", m_aFileName
165 0 : ) );
166 0 : ::dbtools::throwGenericSQLException( sError, *this, aErrorDetails );
167 : }
168 9 : osl_atomic_increment(&m_nDocCount);
169 9 : m_pCloseListener.reset(new utl::CloseVeto(m_xDoc, true));
170 18 : return m_xDoc;
171 : }
172 :
173 35 : void OCalcConnection::releaseDoc()
174 : {
175 35 : if ( osl_atomic_decrement(&m_nDocCount) == 0 )
176 : {
177 0 : m_pCloseListener.reset(); // dispose m_xDoc
178 0 : m_xDoc.clear();
179 : }
180 35 : }
181 :
182 8 : void OCalcConnection::disposing()
183 : {
184 8 : ::osl::MutexGuard aGuard(m_aMutex);
185 :
186 8 : m_nDocCount = 0;
187 8 : m_pCloseListener.reset(); // dispose m_xDoc
188 8 : m_xDoc.clear();
189 :
190 8 : OConnection::disposing();
191 8 : }
192 :
193 : // XServiceInfo
194 :
195 :
196 0 : IMPLEMENT_SERVICE_INFO(OCalcConnection, "com.sun.star.sdbc.drivers.calc.Connection", "com.sun.star.sdbc.Connection")
197 :
198 :
199 :
200 416 : Reference< XDatabaseMetaData > SAL_CALL OCalcConnection::getMetaData( ) throw(SQLException, RuntimeException, std::exception)
201 : {
202 416 : ::osl::MutexGuard aGuard( m_aMutex );
203 416 : checkDisposed(OConnection_BASE::rBHelper.bDisposed);
204 :
205 :
206 416 : Reference< XDatabaseMetaData > xMetaData = m_xMetaData;
207 416 : if(!xMetaData.is())
208 : {
209 9 : xMetaData = new OCalcDatabaseMetaData(this);
210 9 : m_xMetaData = xMetaData;
211 : }
212 :
213 416 : return xMetaData;
214 : }
215 :
216 :
217 :
218 25 : ::com::sun::star::uno::Reference< XTablesSupplier > OCalcConnection::createCatalog()
219 : {
220 25 : ::osl::MutexGuard aGuard( m_aMutex );
221 25 : Reference< XTablesSupplier > xTab = m_xCatalog;
222 25 : if(!xTab.is())
223 : {
224 9 : OCalcCatalog *pCat = new OCalcCatalog(this);
225 9 : xTab = pCat;
226 9 : m_xCatalog = xTab;
227 : }
228 25 : return xTab;
229 : }
230 :
231 :
232 :
233 0 : Reference< XStatement > SAL_CALL OCalcConnection::createStatement( ) throw(SQLException, RuntimeException, std::exception)
234 : {
235 0 : ::osl::MutexGuard aGuard( m_aMutex );
236 0 : checkDisposed(OConnection_BASE::rBHelper.bDisposed);
237 :
238 :
239 0 : Reference< XStatement > xReturn = new OCalcStatement(this);
240 0 : m_aStatements.push_back(WeakReferenceHelper(xReturn));
241 0 : return xReturn;
242 : }
243 :
244 :
245 :
246 16 : Reference< XPreparedStatement > SAL_CALL OCalcConnection::prepareStatement( const OUString& sql )
247 : throw(SQLException, RuntimeException, std::exception)
248 : {
249 16 : ::osl::MutexGuard aGuard( m_aMutex );
250 16 : checkDisposed(OConnection_BASE::rBHelper.bDisposed);
251 :
252 :
253 16 : OCalcPreparedStatement* pStmt = new OCalcPreparedStatement(this);
254 32 : Reference< XPreparedStatement > xHoldAlive = pStmt;
255 16 : pStmt->construct(sql);
256 16 : m_aStatements.push_back(WeakReferenceHelper(*pStmt));
257 32 : return pStmt;
258 : }
259 :
260 :
261 :
262 0 : Reference< XPreparedStatement > SAL_CALL OCalcConnection::prepareCall( const OUString& /*sql*/ )
263 : throw(SQLException, RuntimeException, std::exception)
264 : {
265 0 : ::osl::MutexGuard aGuard( m_aMutex );
266 0 : checkDisposed(OConnection_BASE::rBHelper.bDisposed);
267 :
268 0 : ::dbtools::throwFeatureNotImplementedSQLException( "XConnection::prepareCall", *this );
269 0 : return NULL;
270 : }
271 :
272 :
273 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|