1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/.
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>
21 using namespace ::cppu
;
22 using namespace ::css::uno
;
23 using namespace ::css::lang
;
24 using namespace ::css::frame
;
31 class TerminateListener
: public WeakImplHelper1
<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
45 std::cout
<< "Terminate while we are at work? You can't be serious ;-)!\n";
46 throw TerminationVetoException();
51 virtual void SAL_CALL
disposing(const EventObject
& event
) SAL_OVERRIDE
{}
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
),
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.
76 bool terminated
= xDesktop
->terminate();
77 std::cout
<< "The Office "
78 << (terminated
? "has been terminated" : "is still running, we are at work.")
81 // Try to terminate when we are NOT at work.
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.")
90 catch (const RuntimeException
& e
)
92 std::cerr
<< e
.Message
<< '\n';
98 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */