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