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