LibreOffice Module connectivity (master) 1
CConnection.cxx
Go to the documentation of this file.
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>
22#include <calc/CCatalog.hxx>
23#include <calc/CDriver.hxx>
25#include <com/sun/star/frame/Desktop.hpp>
26#include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
27#include <tools/urlobj.hxx>
33#include <strings.hrc>
34
35using namespace connectivity::calc;
36using namespace connectivity::file;
37
39
40
41using namespace ::com::sun::star::uno;
42using namespace ::com::sun::star::beans;
43using namespace ::com::sun::star::sdbcx;
44using namespace ::com::sun::star::sdbc;
45using namespace ::com::sun::star::lang;
46using namespace ::com::sun::star::frame;
47using namespace ::com::sun::star::sheet;
48
49
50OCalcConnection::OCalcConnection(ODriver* _pDriver) : OConnection(_pDriver),m_nDocCount(0)
51{
52 // m_aFilenameExtension is not used
53}
54
56{
57}
58
59void OCalcConnection::construct(const OUString& url,const Sequence< PropertyValue >& info)
60{
61 // open file
62
63 sal_Int32 nLen = url.indexOf(':');
64 nLen = url.indexOf(':',nLen+1);
65 OUString aDSN(url.copy(nLen+1));
66
67 m_aFileName = aDSN;
69 aURL.SetSmartProtocol(INetProtocol::File);
70 {
71 SvtPathOptions aPathOptions;
73 }
74 aURL.SetSmartURL(m_aFileName);
75 if ( aURL.GetProtocol() == INetProtocol::NotValid )
76 {
77 // don't pass invalid URL to loadComponentFromURL
78 throw SQLException();
79 }
81
82 m_sPassword.clear();
83 const char pPwd[] = "password";
84
85 const PropertyValue *pIter = info.getConstArray();
86 const PropertyValue *pEnd = pIter + info.getLength();
87 for(;pIter != pEnd;++pIter)
88 {
89 if(pIter->Name == pPwd)
90 {
91 pIter->Value >>= m_sPassword;
92 break;
93 }
94 } // for(;pIter != pEnd;++pIter)
95 ODocHolder aDocHolder(this); // just to test that the doc can be loaded
96 acquireDoc();
97}
98
100{
101 if ( m_xDoc.is() )
102 {
103 osl_atomic_increment(&m_nDocCount);
104 return m_xDoc;
105 }
106 // open read-only as long as updating isn't implemented
107 Sequence<PropertyValue> aArgs(m_sPassword.isEmpty() ? 2 : 3);
108 auto pArgs = aArgs.getArray();
109 pArgs[0].Name = "Hidden";
110 pArgs[0].Value <<= true;
111 pArgs[1].Name = "ReadOnly";
112 pArgs[1].Value <<= true;
113
114 if ( !m_sPassword.isEmpty() )
115 {
116 pArgs[2].Name = "Password";
117 pArgs[2].Value <<= m_sPassword;
118 }
119
120 Reference< XDesktop2 > xDesktop = Desktop::create( getDriver()->getComponentContext() );
121 Reference< XComponent > xComponent;
122 Any aLoaderException;
123 try
124 {
125 xComponent = xDesktop->loadComponentFromURL(
126 m_aFileName, "_blank", 0, aArgs );
127 }
128 catch( const Exception& )
129 {
130 aLoaderException = ::cppu::getCaughtException();
131 }
132
133 m_xDoc.set(xComponent, UNO_QUERY );
134
135 // if the URL is not a spreadsheet document, throw the exception here
136 // instead of at the first access to it
137 if ( !m_xDoc.is() )
138 {
139 Any aErrorDetails;
140 if ( aLoaderException.hasValue() )
141 {
142 Exception aLoaderError;
143 OSL_VERIFY( aLoaderException >>= aLoaderError );
144
145 SQLException aDetailException;
146 aDetailException.Message = m_aResources.getResourceStringWithSubstitution(
147 STR_LOAD_FILE_ERROR_MESSAGE,
148 "$exception_type$", aLoaderException.getValueTypeName(),
149 "$error_message$", aLoaderError.Message
150 );
151 aErrorDetails <<= aDetailException;
152 }
153
154 const OUString sError( m_aResources.getResourceStringWithSubstitution(
155 STR_COULD_NOT_LOAD_FILE,
156 "$filename$", m_aFileName
157 ) );
158 ::dbtools::throwGenericSQLException( sError, *this, aErrorDetails );
159 }
160 osl_atomic_increment(&m_nDocCount);
163 return m_xDoc;
164}
165
167{
168 if ( osl_atomic_decrement(&m_nDocCount) == 0 )
169 {
171 {
172 m_xCloseVetoButTerminateListener->stop(); // dispose m_xDoc
174 }
175 m_xDoc.clear();
176 }
177}
178
180{
181 ::osl::MutexGuard aGuard(m_aMutex);
182
183 m_nDocCount = 0;
185 {
186 m_xCloseVetoButTerminateListener->stop(); // dispose m_xDoc
188 }
189 m_xDoc.clear();
190
191 OConnection::disposing();
192}
193
194// XServiceInfo
195
196
197IMPLEMENT_SERVICE_INFO(OCalcConnection, "com.sun.star.sdbc.drivers.calc.Connection", "com.sun.star.sdbc.Connection")
198
199
200Reference< XDatabaseMetaData > SAL_CALL OCalcConnection::getMetaData( )
201{
202 ::osl::MutexGuard aGuard( m_aMutex );
203 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
204
205
206 Reference< XDatabaseMetaData > xMetaData = m_xMetaData;
207 if(!xMetaData.is())
208 {
209 xMetaData = new OCalcDatabaseMetaData(this);
210 m_xMetaData = xMetaData;
211 }
212
213 return xMetaData;
214}
215
216
217css::uno::Reference< XTablesSupplier > OCalcConnection::createCatalog()
218{
219 ::osl::MutexGuard aGuard( m_aMutex );
221 if(!xTab.is())
222 {
223 xTab = new OCalcCatalog(this);
224 m_xCatalog = xTab;
225 }
226 return xTab;
227}
228
229
231{
232 ::osl::MutexGuard aGuard( m_aMutex );
233 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
234
235
237 m_aStatements.push_back(WeakReferenceHelper(xReturn));
238 return xReturn;
239}
240
241
243{
244 ::osl::MutexGuard aGuard( m_aMutex );
245 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
246
247
249 pStmt->construct(sql);
250 m_aStatements.push_back(WeakReferenceHelper(*pStmt));
251 return pStmt;
252}
253
254
256{
257 ::osl::MutexGuard aGuard( m_aMutex );
258 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
259
260 ::dbtools::throwFeatureNotImplementedSQLException( "XConnection::prepareCall", *this );
261 return nullptr;
262}
263
264
265/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
connectivity::file::OConnection OConnection_BASE
Definition: CConnection.cxx:38
#define IMPLEMENT_SERVICE_INFO(classname, implasciiname, serviceasciiname)
OUString SubstituteVariable(const OUString &rVar) const
connectivity::OWeakRefArray m_aStatements
Definition: TConnection.hxx:47
OUString getResourceStringWithSubstitution(TranslateId pResId, const char *_pAsciiPatternToReplace, const OUString &_rStringToSubstitute) const
loads a string from the shared resource file, and replaces a given ASCII pattern with a given string
css::uno::Reference< css::sheet::XSpreadsheetDocument > const & acquireDoc()
Definition: CConnection.cxx:99
css::uno::Reference< css::sheet::XSpreadsheetDocument > m_xDoc
Definition: CConnection.hxx:42
virtual css::uno::Reference< css::sdbc::XStatement > SAL_CALL createStatement() override
virtual void construct(const OUString &_rUrl, const css::uno::Sequence< css::beans::PropertyValue > &_rInfo) override
Definition: CConnection.cxx:59
virtual css::uno::Reference< css::sdbc::XPreparedStatement > SAL_CALL prepareStatement(const OUString &sql) override
rtl::Reference< CloseVetoButTerminateListener > m_xCloseVetoButTerminateListener
virtual css::uno::Reference< css::sdbcx::XTablesSupplier > createCatalog() override
virtual css::uno::Reference< css::sdbc::XPreparedStatement > SAL_CALL prepareCall(const OUString &sql) override
virtual void SAL_CALL disposing() override
Prepared statement implementation for Writer tables and Calc sheets.
Statement implementation for Writer tables and Calc sheets.
Definition: CStatement.hxx:29
css::uno::WeakReference< css::sdbcx::XTablesSupplier > m_xCatalog
Definition: FConnection.hxx:41
OFileDriver * getDriver() const
URL aURL
std::mutex m_aMutex
@ Exception
Reference< XComponentContext > getComponentContext(Reference< XMultiServiceFactory > const &factory)
void checkDisposed(bool _bThrow)
Definition: dbtools.cxx:1951
void throwFeatureNotImplementedSQLException(const OUString &_rFeatureName, const Reference< XInterface > &_rxContext, const Any &_rNextException)
void throwGenericSQLException(const OUString &_rMsg, const css::uno::Reference< css::uno::XInterface > &_rxSource)
throw a generic SQLException, i.e.