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