tdf#143122 Port Java TerminationTest example to C++

This patch ports odk/examples/DevelopersGuide/OfficeDev/TerminationTest
example to C++.

The original Java example code was moved to a java/ subdirectory, and
the new C++ code was added to the cxx/ subdirectory. Necessary changes
were made in the corresponding Makefiles.

Along with the code in TerminationTest.java, the TerminateListener
class was also ported and used in the same C++ file.

Change-Id: I889b8893740bc8f80cdd9aa9925e83cadcea9592
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/165916
Tested-by: Jenkins
Tested-by: Hossein <hossein@libreoffice.org>
Reviewed-by: Hossein <hossein@libreoffice.org>
This commit is contained in:
RMZeroFour 2024-04-10 01:37:42 +05:30 committed by Hossein
parent b671bb2ea7
commit c299e54ce2
8 changed files with 183 additions and 5 deletions

View file

@ -19,6 +19,7 @@ my_example_dirs = \
DevelopersGuide/Database/DriverSkeleton \
DevelopersGuide/Extensions/DialogWithHelp \
DevelopersGuide/OfficeDev/FilterDevelopment/FlatXmlFilter_cpp \
DevelopersGuide/OfficeDev/TerminationTest/cxx \
DevelopersGuide/ProfUNO/CppBinding \
DevelopersGuide/ProfUNO/SimpleBootstrap_cpp \
OLE/activex \

View file

@ -35,7 +35,7 @@ my_example_dirs_java = \
DevelopersGuide/OfficeDev/Linguistic \
DevelopersGuide/OfficeDev/PathSettings \
DevelopersGuide/OfficeDev/PathSubstitution \
DevelopersGuide/OfficeDev/TerminationTest \
DevelopersGuide/OfficeDev/TerminationTest/java \
DevelopersGuide/ProfUNO/InterprocessConn \
DevelopersGuide/ProfUNO/Lifetime \
DevelopersGuide/ProfUNO/SimpleBootstrap_java \

View file

@ -322,9 +322,11 @@ $(eval $(call gb_Package_add_files_with_dir,odk_examples,$(SDKDIRNAME)/examples,
DevelopersGuide/OfficeDev/PathSettings/PathSettingsTest.java \
DevelopersGuide/OfficeDev/PathSubstitution/Makefile \
DevelopersGuide/OfficeDev/PathSubstitution/PathSubstitutionTest.java \
DevelopersGuide/OfficeDev/TerminationTest/Makefile \
DevelopersGuide/OfficeDev/TerminationTest/TerminateListener.java \
DevelopersGuide/OfficeDev/TerminationTest/TerminationTest.java \
DevelopersGuide/OfficeDev/TerminationTest/cxx/Makefile \
DevelopersGuide/OfficeDev/TerminationTest/cxx/TerminationTest.cxx \
DevelopersGuide/OfficeDev/TerminationTest/java/Makefile \
DevelopersGuide/OfficeDev/TerminationTest/java/TerminateListener.java \
DevelopersGuide/OfficeDev/TerminationTest/java/TerminationTest.java \
DevelopersGuide/ProfUNO/CppBinding/Makefile \
DevelopersGuide/ProfUNO/CppBinding/office_connect.cxx \
DevelopersGuide/ProfUNO/CppBinding/string_samples.cxx \

View file

@ -0,0 +1,77 @@
# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t; fill-column: 100 -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Builds the TerminationTest example of the SDK.
PRJ=../../../../..
SETTINGS=$(PRJ)/settings
include $(SETTINGS)/settings.mk
include $(SETTINGS)/std.mk
# Define non-platform/compiler specific settings
APP_NAME=TerminationTest
OUT_APP_INC = $(OUT_INC)/$(APP_NAME)
OUT_APP_GEN = $(OUT_MISC)/$(APP_NAME)
OUT_APP_OBJ=$(OUT_OBJ)/$(APP_NAME)
CXXFILES = TerminationTest.cxx
OBJFILES = $(patsubst %.cxx,$(OUT_SLO_COMP)/%.$(OBJ_EXT),$(CXXFILES))
# Targets
.PHONY: ALL
ALL : \
TerminationTest
include $(SETTINGS)/stdtarget.mk
$(OUT_APP_OBJ)/%.$(OBJ_EXT) : %.cxx $(SDKTYPEFLAG)
-$(MKDIR) $(subst /,$(PS),$(@D))
$(CC) $(CC_FLAGS) $(CC_INCLUDES) -I$(OUT_APP_INC) $(CC_DEFINES) $(CC_OUTPUT_SWITCH)$(subst /,$(PS),$@) $<
$(OUT_BIN)/_$(APP_NAME)$(EXE_EXT) : $(OUT_APP_OBJ)/$(APP_NAME).$(OBJ_EXT)
-$(MKDIR) $(subst /,$(PS),$(@D))
-$(MKDIR) $(subst /,$(PS),$(OUT_APP_GEN))
ifeq "$(OS)" "WIN"
$(LINK) $(EXE_LINK_FLAGS) /OUT:$@ /MAP:$(OUT_APP_GEN)/$(basename $(@F)).map \
$< $(CPPUHELPERLIB) $(CPPULIB) $(SALHELPERLIB) $(SALLIB)
else
$(LINK) $(EXE_LINK_FLAGS) $(LINK_LIBS) -o $@ $< \
$(CPPUHELPERLIB) $(CPPULIB) $(SALHELPERLIB) $(SALLIB) $(STDC++LIB)
ifeq "$(OS)" "MACOSX"
$(INSTALL_NAME_URELIBS_BIN) $@
endif
endif
$(OUT_BIN)/$(APP_NAME)$(EXE_EXT) : $(OUT_BIN)/_$(APP_NAME)$(EXE_EXT)
-$(MKDIR) $(subst /,$(PS),$(@D))
$(COPY) $(subst /,$(PS),$(BIN_DIR)/unoapploader$(EXE_EXT)) $(subst /,$(PS),$@)
# workaround for touch problem under Windows with full qualified paths
make -t $@
TerminationTest: $(OUT_BIN)/$(APP_NAME)$(EXE_EXT)
@echo --------------------------------------------------------------------------------
@echo Please use the following command to execute the example!
@echo -
@echo $(MAKE) TerminationTest.run
@echo --------------------------------------------------------------------------------
%.run: $(OUT_BIN)/TerminationTest$(EXE_EXT)
cd $(subst /,$(PS),$(OUT_BIN)) && $(basename $@)
.PHONY: clean
clean :
-$(DELRECURSIVE) $(subst /,$(PS),$(OUT_APP_INC))
-$(DELRECURSIVE) $(subst /,$(PS),$(OUT_APP_GEN))
-$(DELRECURSIVE) $(subst /,$(PS),$(OUT_APP_OBJ))
-$(DEL) $(subst \\,\,$(subst /,$(PS),$(OUT_BIN)/*TerminationTest*))
# vim: set noet sw=4 ts=4:

View file

@ -0,0 +1,98 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <sal/main.h>
#include <cppuhelper/bootstrap.hxx>
#include <cppuhelper/implbase1.hxx>
#include <com/sun/star/uno/XComponentContext.hpp>
#include <com/sun/star/lang/XMultiComponentFactory.hpp>
#include <com/sun/star/frame/XDesktop.hpp>
#include <com/sun/star/frame/XTerminateListener.hpp>
#include <com/sun/star/frame/TerminationVetoException.hpp>
#include <iostream>
using namespace ::cppu;
using namespace ::css::uno;
using namespace ::css::lang;
using namespace ::css::frame;
namespace
{
bool atWork = false;
}
class TerminateListener : public WeakImplHelper1<XTerminateListener>
{
public:
// XTerminateListener
virtual void SAL_CALL notifyTermination(const EventObject& event) SAL_OVERRIDE
{
std::cout << "About to terminate...\n";
}
virtual void SAL_CALL queryTermination(const EventObject& event) SAL_OVERRIDE
{
// Test if we can terminate now
if (atWork)
{
std::cout << "Terminate while we are at work? You can't be serious ;-)!\n";
throw TerminationVetoException();
}
}
// XEventListener
virtual void SAL_CALL disposing(const EventObject& event) SAL_OVERRIDE {}
};
SAL_IMPLEMENT_MAIN()
{
try
{
// Connect to, or create, an instance of the Office.
Reference<XComponentContext> xContext(bootstrap());
std::cout << "Connected to a running office...\n";
// Get a reference to the multi-component factory, and use it
// to create a Desktop reference.
Reference<XMultiComponentFactory> xMCF(xContext->getServiceManager());
Reference<XDesktop> xDesktop(
xMCF->createInstanceWithContext("com.sun.star.frame.Desktop", xContext),
UNO_QUERY_THROW);
// Create our termination request listener, and register it.
TerminateListener listener;
Reference<XTerminateListener> xTerminateListener(listener, UNO_QUERY_THROW);
xDesktop->addTerminateListener(xTerminateListener);
// Try to terminate while we are at work.
atWork = true;
bool terminated = xDesktop->terminate();
std::cout << "The Office "
<< (terminated ? "has been terminated" : "is still running, we are at work.")
<< '\n';
// Try to terminate when we are NOT at work.
atWork = false;
terminated = xDesktop->terminate();
std::cout << "The Office "
<< (terminated ? "has been terminated"
: "is still running. Something else prevents termination,"
"such as the quickstarter.")
<< '\n';
}
catch (const RuntimeException& e)
{
std::cerr << e.Message << '\n';
return 1;
}
return 0;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */

View file

@ -34,7 +34,7 @@
# Builds the OfficeDevClipboard example of the Developers Guide.
PRJ=../../../..
PRJ=../../../../..
SETTINGS=$(PRJ)/settings
include $(SETTINGS)/settings.mk