Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / connectivity / source / inc / calc / CConnection.hxx
blob6eabeccef5ff27f0b39636a930d9b25edf8ba801
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #pragma once
22 #include <memory>
23 #include <file/FConnection.hxx>
24 #include <com/sun/star/frame/XDesktop2.hpp>
25 #include <com/sun/star/frame/XTerminateListener.hpp>
26 #include <rtl/ref.hxx>
27 #include <unotools/closeveto.hxx>
29 namespace com::sun::star {
30 namespace sheet { class XSpreadsheetDocument; }
33 namespace utl { class CloseVeto; }
36 namespace connectivity::calc
38 class ODriver;
39 class OCalcConnection : public file::OConnection
41 // the spreadsheet document:
42 css::uno::Reference< css::sheet::XSpreadsheetDocument > m_xDoc;
43 OUString m_sPassword;
44 OUString m_aFileName;
45 oslInterlockedCount m_nDocCount;
47 class CloseVetoButTerminateListener : public cppu::WeakComponentImplHelper<css::frame::XTerminateListener>
49 private:
50 /// close listener that vetoes so nobody else disposes m_xDoc
51 std::unique_ptr<utl::CloseVeto> m_pCloseListener;
52 /// but also listen to XDesktop and if app is terminating anyway, dispose m_xDoc while
53 /// its still possible to do so properly
54 css::uno::Reference<css::frame::XDesktop2> m_xDesktop;
55 osl::Mutex m_aMutex;
56 public:
57 CloseVetoButTerminateListener()
58 : cppu::WeakComponentImplHelper<css::frame::XTerminateListener>(m_aMutex)
62 void start(const css::uno::Reference<css::uno::XInterface>& rCloseable,
63 const css::uno::Reference<css::frame::XDesktop2>& rDesktop)
65 m_xDesktop = rDesktop;
66 m_xDesktop->addTerminateListener(this);
67 m_pCloseListener.reset(new utl::CloseVeto(rCloseable, true));
70 void stop()
72 m_pCloseListener.reset();
73 if (!m_xDesktop.is())
74 return;
75 m_xDesktop->removeTerminateListener(this);
76 m_xDesktop.clear();
79 // XTerminateListener
80 virtual void SAL_CALL queryTermination(const css::lang::EventObject& /*rEvent*/) override
84 virtual void SAL_CALL notifyTermination(const css::lang::EventObject& /*rEvent*/) override
86 stop();
89 virtual void SAL_CALL disposing() override
91 stop();
92 cppu::WeakComponentImplHelperBase::disposing();
95 virtual void SAL_CALL disposing(const css::lang::EventObject& rEvent) override
97 const bool bShutDown = (rEvent.Source == m_xDesktop);
98 if (bShutDown)
99 stop();
103 rtl::Reference<CloseVetoButTerminateListener> m_xCloseVetoButTerminateListener;
105 public:
106 OCalcConnection(ODriver* _pDriver);
107 virtual ~OCalcConnection() override;
109 virtual void construct(const OUString& _rUrl,
110 const css::uno::Sequence< css::beans::PropertyValue >& _rInfo ) override;
112 // XServiceInfo
113 DECLARE_SERVICE_INFO();
115 // OComponentHelper
116 virtual void SAL_CALL disposing() override;
118 // XConnection
119 virtual css::uno::Reference< css::sdbc::XDatabaseMetaData > SAL_CALL getMetaData( ) override;
120 virtual css::uno::Reference< css::sdbcx::XTablesSupplier > createCatalog() override;
121 virtual css::uno::Reference< css::sdbc::XStatement > SAL_CALL createStatement( ) override;
122 virtual css::uno::Reference< css::sdbc::XPreparedStatement > SAL_CALL prepareStatement( const OUString& sql ) override;
123 virtual css::uno::Reference< css::sdbc::XPreparedStatement > SAL_CALL prepareCall( const OUString& sql ) override;
125 // no interface methods
126 css::uno::Reference< css::sheet::XSpreadsheetDocument> const & acquireDoc();
127 void releaseDoc();
129 class ODocHolder
131 OCalcConnection* m_pConnection;
132 css::uno::Reference< css::sheet::XSpreadsheetDocument> m_xDoc;
133 public:
134 ODocHolder(OCalcConnection* _pConnection) : m_pConnection(_pConnection)
136 m_xDoc = m_pConnection->acquireDoc();
138 ~ODocHolder()
140 m_xDoc.clear();
141 m_pConnection->releaseDoc();
143 const css::uno::Reference< css::sheet::XSpreadsheetDocument>& getDoc() const { return m_xDoc; }
149 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */