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