cid#1606940 Check of thread-shared field evades lock acquisition
[LibreOffice.git] / odk / examples / DevelopersGuide / OfficeDev / TerminationTest / cxx / TerminationTest.cxx
blobf783ca6ec89f69da55f6d02b1249325966080bc1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/.
8 */
10 #include <sal/main.h>
11 #include <cppuhelper/bootstrap.hxx>
12 #include <cppuhelper/implbase1.hxx>
13 #include <com/sun/star/uno/XComponentContext.hpp>
14 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
15 #include <com/sun/star/frame/XDesktop.hpp>
16 #include <com/sun/star/frame/XTerminateListener.hpp>
17 #include <com/sun/star/frame/TerminationVetoException.hpp>
19 #include <iostream>
21 using namespace ::cppu;
22 using namespace ::css::uno;
23 using namespace ::css::lang;
24 using namespace ::css::frame;
26 namespace
28 bool atWork = false;
31 class TerminateListener : public WeakImplHelper1<XTerminateListener>
33 public:
34 // XTerminateListener
35 virtual void SAL_CALL notifyTermination(const EventObject& event) SAL_OVERRIDE
37 std::cout << "About to terminate...\n";
40 virtual void SAL_CALL queryTermination(const EventObject& event) SAL_OVERRIDE
42 // Test if we can terminate now
43 if (atWork)
45 std::cout << "Terminate while we are at work? You can't be serious ;-)!\n";
46 throw TerminationVetoException();
50 // XEventListener
51 virtual void SAL_CALL disposing(const EventObject& event) SAL_OVERRIDE {}
54 SAL_IMPLEMENT_MAIN()
56 try
58 // Connect to, or create, an instance of the Office.
59 Reference<XComponentContext> xContext(bootstrap());
60 std::cout << "Connected to a running office...\n";
62 // Get a reference to the multi-component factory, and use it
63 // to create a Desktop reference.
64 Reference<XMultiComponentFactory> xMCF(xContext->getServiceManager());
65 Reference<XDesktop> xDesktop(
66 xMCF->createInstanceWithContext("com.sun.star.frame.Desktop", xContext),
67 UNO_QUERY_THROW);
69 // Create our termination request listener, and register it.
70 TerminateListener listener;
71 Reference<XTerminateListener> xTerminateListener(listener, UNO_QUERY_THROW);
72 xDesktop->addTerminateListener(xTerminateListener);
74 // Try to terminate while we are at work.
75 atWork = true;
76 bool terminated = xDesktop->terminate();
77 std::cout << "The Office "
78 << (terminated ? "has been terminated" : "is still running, we are at work.")
79 << '\n';
81 // Try to terminate when we are NOT at work.
82 atWork = false;
83 terminated = xDesktop->terminate();
84 std::cout << "The Office "
85 << (terminated ? "has been terminated"
86 : "is still running. Something else prevents termination,"
87 "such as the quickstarter.")
88 << '\n';
90 catch (const RuntimeException& e)
92 std::cerr << e.Message << '\n';
93 return 1;
95 return 0;
98 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */