LibreOffice Module svtools (master) 1
javainteractionhandler.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 <svtools/strings.hrc>
21#include <com/sun/star/task/XInteractionContinuation.hpp>
22#include <com/sun/star/task/XInteractionAbort.hpp>
23#include <com/sun/star/task/XInteractionRetry.hpp>
24#include <com/sun/star/java/JavaNotFoundException.hpp>
25#include <com/sun/star/java/InvalidJavaSettingsException.hpp>
26#include <com/sun/star/java/JavaDisabledException.hpp>
27#include <com/sun/star/java/JavaVMCreationFailureException.hpp>
28#include <com/sun/star/java/RestartRequiredException.hpp>
30#include <vcl/svapp.hxx>
31#include <vcl/weld.hxx>
32#include <jvmfwk/framework.hxx>
33
35#include <svtools/svtresid.hxx>
38#include <officecfg/Office/Common.hxx>
39
40using namespace com::sun::star::uno;
41using namespace com::sun::star::task;
42
43namespace
44{
45struct JavaEvents {
46 bool bDisabledHandled : 1;
47 bool bInvalidSettingsHandled : 1;
48 bool bNotFoundHandled : 1;
49 bool bVMCreationFailureHandled : 1;
50 bool bRestartRequiredHandled : 1;
51 sal_uInt16 nResult_JavaDisabled = RET_NO;
52 JavaEvents()
53 : bDisabledHandled(false)
54 , bInvalidSettingsHandled(false)
55 , bNotFoundHandled(false)
56 , bVMCreationFailureHandled(false)
57 , bRestartRequiredHandled(false)
58 {}
59} g_JavaEvents;
60}
61
62namespace svt
63{
64
66 m_aRefCount(0)
67{
68}
69
71{
72}
73
75{
76 if (aType == cppu::UnoType<XInterface>::get())
77 return Any( static_cast<XInterface*>(this), aType);
79 return Any( static_cast<XInteractionHandler*>(this), aType);
80 return Any();
81}
82
83void SAL_CALL JavaInteractionHandler::acquire( ) noexcept
84{
85 osl_atomic_increment( &m_aRefCount );
86}
87
88void SAL_CALL JavaInteractionHandler::release( ) noexcept
89{
90 if (! osl_atomic_decrement( &m_aRefCount ))
91 delete this;
92}
93
95{
96 Any anyExc = Request->getRequest();
97 const Sequence< Reference< XInteractionContinuation > > aSeqCont = Request->getContinuations();
98
101
102 for ( const auto& rCont : aSeqCont )
103 {
104 abort.set( rCont, UNO_QUERY );
105 if ( abort.is() )
106 break;
107 }
108
109 for ( const auto& rCont : aSeqCont )
110 {
111 retry.set( rCont, UNO_QUERY );
112 if ( retry.is() )
113 break;
114 }
115
116 css::java::JavaNotFoundException e1;
117 css::java::InvalidJavaSettingsException e2;
118 css::java::JavaDisabledException e3;
119 css::java::JavaVMCreationFailureException e4;
120 css::java::RestartRequiredException e5;
121 // Try to recover the Exception type in the any and
122 // react accordingly.
123 sal_uInt16 nResult = RET_CANCEL;
124
125 if ( anyExc >>= e1 )
126 {
127 SolarMutexGuard aSolarGuard;
128 if( !g_JavaEvents.bNotFoundHandled )
129 {
130 // No suitable JRE found
131 OUString sPrimTex;
132 OUString urlLink(officecfg::Office::Common::Menus::InstallJavaURL::get() + // https://hub.libreoffice.org/InstallJava/
133 "?LOlocale=" + utl::ConfigManager::getUILocale());
134 g_JavaEvents.bNotFoundHandled = true;
135#if defined(MACOSX)
136 sPrimTex = SvtResId(STR_WARNING_JAVANOTFOUND_MAC);
137#elif defined(_WIN32)
138 sPrimTex = SvtResId(STR_WARNING_JAVANOTFOUND_WIN);
139#if defined(_WIN64)
140 sPrimTex = sPrimTex.replaceAll("%BITNESS", "64");
141#else
142 sPrimTex = sPrimTex.replaceAll("%BITNESS", "32");
143#endif
144#else
145 sPrimTex = SvtResId(STR_WARNING_JAVANOTFOUND);
146#endif
147 sPrimTex = sPrimTex.replaceAll("%FAQLINK", urlLink);
148 std::unique_ptr<weld::MessageDialog> xWarningBox(Application::CreateMessageDialog(
149 nullptr, VclMessageType::Warning, VclButtonsType::Ok, sPrimTex));
150 xWarningBox->set_title(SvtResId(STR_WARNING_JAVANOTFOUND_TITLE));
151 nResult = xWarningBox->run();
152 }
153 else
154 {
155 nResult = RET_OK;
156 }
157 }
158 else if ( anyExc >>= e2 )
159 {
160 SolarMutexGuard aSolarGuard;
161 if( !g_JavaEvents.bInvalidSettingsHandled )
162 {
163 // javavendors.xml was updated and Java has not been configured yet
164 g_JavaEvents.bInvalidSettingsHandled = true;
165#ifdef MACOSX
166 OUString sWarning(SvtResId(STR_WARNING_INVALIDJAVASETTINGS_MAC));
167#else
168 OUString sWarning(SvtResId(STR_WARNING_INVALIDJAVASETTINGS));
169#endif
170 std::unique_ptr<weld::MessageDialog> xWarningBox(Application::CreateMessageDialog(nullptr,
171 VclMessageType::Warning, VclButtonsType::Ok, sWarning));
172 xWarningBox->set_title(SvtResId(STR_WARNING_INVALIDJAVASETTINGS_TITLE));
173 nResult = xWarningBox->run();
174 }
175 else
176 {
177 nResult = RET_OK;
178 }
179 }
180 else if ( anyExc >>= e3 )
181 {
182 SolarMutexGuard aSolarGuard;
183 if( !g_JavaEvents.bDisabledHandled )
184 {
185 g_JavaEvents.bDisabledHandled = true;
186 // Java disabled. Give user a chance to enable Java inside Office.
187 std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(nullptr, "svt/ui/javadisableddialog.ui"));
188 std::unique_ptr<weld::MessageDialog> xQueryBox(xBuilder->weld_message_dialog("JavaDisabledDialog"));
189 nResult = xQueryBox->run();
190 if ( nResult == RET_YES )
191 {
192 jfw_setEnabled(true);
193 }
194
195 g_JavaEvents.nResult_JavaDisabled = nResult;
196
197 }
198 else
199 {
200 nResult = g_JavaEvents.nResult_JavaDisabled;
201 }
202 }
203 else if ( anyExc >>= e4 )
204 {
205 SolarMutexGuard aSolarGuard;
206 if( !g_JavaEvents.bVMCreationFailureHandled )
207 {
208 // Java not correctly installed, or damaged
209 g_JavaEvents.bVMCreationFailureHandled = true;
210#ifdef MACOSX
211 OUString sWarning(SvtResId(STR_ERROR_JVMCREATIONFAILED_MAC));
212#else
213 OUString sWarning(SvtResId(STR_ERROR_JVMCREATIONFAILED));
214#endif
215 std::unique_ptr<weld::MessageDialog> xErrorBox(Application::CreateMessageDialog(nullptr,
216 VclMessageType::Warning, VclButtonsType::Ok, sWarning));
217 xErrorBox->set_title(SvtResId(STR_ERROR_JVMCREATIONFAILED_TITLE));
218 nResult = xErrorBox->run();
219 }
220 else
221 {
222 nResult = RET_OK;
223 }
224 }
225 else if ( anyExc >>= e5 )
226 {
227 SolarMutexGuard aSolarGuard;
228 if( !g_JavaEvents.bRestartRequiredHandled )
229 {
230 // a new JRE was selected, but office needs to be restarted
231 //before it can be used.
232 g_JavaEvents.bRestartRequiredHandled = true;
236 }
237 nResult = RET_OK;
238 }
239
240 if ( nResult == RET_CANCEL || nResult == RET_NO)
241 {
242 // Unknown exception type or user wants to cancel
243 if ( abort.is() )
244 abort->select();
245 }
246 else // nResult == RET_OK
247 {
248 // User selected OK => retry Java usage
249 if ( retry.is() )
250 retry->select();
251 }
252}
253
254}
255
256/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static std::unique_ptr< weld::Builder > CreateBuilder(weld::Widget *pParent, const OUString &rUIFile, bool bMobile=false, sal_uInt64 nLOKWindowId=0)
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage, const ILibreOfficeKitNotifier *pNotifier=nullptr)
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &aType) override
virtual void SAL_CALL acquire() noexcept override
virtual void SAL_CALL release() noexcept override
virtual void SAL_CALL handle(const css::uno::Reference< css::task::XInteractionRequest > &Request) override
virtual SVT_DLLPRIVATE ~JavaInteractionHandler()
static OUString getUILocale()
javaFrameworkError jfw_setEnabled(bool bEnabled)
Reference< XComponentContext > getProcessComponentContext()
Type
@ RESTART_REASON_JAVA
SVT_DLLPUBLIC bool executeRestartDialog(css::uno::Reference< css::uno::XComponentContext > const &context, weld::Window *parent, RestartReason reason)
OUString SvtResId(TranslateId aId)
Definition: svtresid.cxx:24
RET_OK
RET_CANCEL
RET_NO
RET_YES