1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 #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>
29 #include <comphelper/processfactory.hxx>
30 #include <vcl/svapp.hxx>
31 #include <vcl/weld.hxx>
32 #include <jvmfwk/framework.hxx>
34 #include <svtools/restartdialog.hxx>
35 #include <svtools/svtresid.hxx>
36 #include <svtools/javainteractionhandler.hxx>
37 #include <unotools/configmgr.hxx>
38 #include <officecfg/Office/Common.hxx>
40 using namespace com::sun::star::uno
;
41 using namespace com::sun::star::task
;
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
;
53 : bDisabledHandled(false)
54 , bInvalidSettingsHandled(false)
55 , bNotFoundHandled(false)
56 , bVMCreationFailureHandled(false)
57 , bRestartRequiredHandled(false)
65 JavaInteractionHandler::JavaInteractionHandler() :
70 JavaInteractionHandler::~JavaInteractionHandler()
74 Any SAL_CALL
JavaInteractionHandler::queryInterface(const Type
& aType
)
76 if (aType
== cppu::UnoType
<XInterface
>::get())
77 return Any( static_cast<XInterface
*>(this), aType
);
78 else if (aType
== cppu::UnoType
<XInteractionHandler
>::get())
79 return Any( static_cast<XInteractionHandler
*>(this), aType
);
83 void SAL_CALL
JavaInteractionHandler::acquire( ) noexcept
85 osl_atomic_increment( &m_aRefCount
);
88 void SAL_CALL
JavaInteractionHandler::release( ) noexcept
90 if (! osl_atomic_decrement( &m_aRefCount
))
94 void SAL_CALL
JavaInteractionHandler::handle( const Reference
< XInteractionRequest
>& Request
)
96 Any anyExc
= Request
->getRequest();
97 const Sequence
< Reference
< XInteractionContinuation
> > aSeqCont
= Request
->getContinuations();
99 Reference
< XInteractionAbort
> abort
;
100 Reference
< XInteractionRetry
> retry
;
102 for ( const auto& rCont
: aSeqCont
)
104 abort
.set( rCont
, UNO_QUERY
);
109 for ( const auto& rCont
: aSeqCont
)
111 retry
.set( rCont
, UNO_QUERY
);
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
;
127 SolarMutexGuard aSolarGuard
;
128 if( !g_JavaEvents
.bNotFoundHandled
)
130 // No suitable JRE found
132 OUString
urlLink(officecfg::Office::Common::Menus::InstallJavaURL::get() + // https://hub.libreoffice.org/InstallJava/
133 "?LOlocale=" + utl::ConfigManager::getUILocale());
134 g_JavaEvents
.bNotFoundHandled
= true;
136 sPrimTex
= SvtResId(STR_WARNING_JAVANOTFOUND_MAC
);
137 #elif defined(_WIN32)
138 sPrimTex
= SvtResId(STR_WARNING_JAVANOTFOUND_WIN
);
140 sPrimTex
= sPrimTex
.replaceAll("%BITNESS", "64");
142 sPrimTex
= sPrimTex
.replaceAll("%BITNESS", "32");
145 sPrimTex
= SvtResId(STR_WARNING_JAVANOTFOUND
);
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();
158 else if ( anyExc
>>= e2
)
160 SolarMutexGuard aSolarGuard
;
161 if( !g_JavaEvents
.bInvalidSettingsHandled
)
163 // javavendors.xml was updated and Java has not been configured yet
164 g_JavaEvents
.bInvalidSettingsHandled
= true;
166 OUString
sWarning(SvtResId(STR_WARNING_INVALIDJAVASETTINGS_MAC
));
168 OUString
sWarning(SvtResId(STR_WARNING_INVALIDJAVASETTINGS
));
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();
180 else if ( anyExc
>>= e3
)
182 SolarMutexGuard aSolarGuard
;
183 if( !g_JavaEvents
.bDisabledHandled
)
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, u
"svt/ui/javadisableddialog.ui"_ustr
));
188 std::unique_ptr
<weld::MessageDialog
> xQueryBox(xBuilder
->weld_message_dialog(u
"JavaDisabledDialog"_ustr
));
189 nResult
= xQueryBox
->run();
190 if ( nResult
== RET_YES
)
192 jfw_setEnabled(true);
195 g_JavaEvents
.nResult_JavaDisabled
= nResult
;
200 nResult
= g_JavaEvents
.nResult_JavaDisabled
;
203 else if ( anyExc
>>= e4
)
205 SolarMutexGuard aSolarGuard
;
206 if( !g_JavaEvents
.bVMCreationFailureHandled
)
208 // Java not correctly installed, or damaged
209 g_JavaEvents
.bVMCreationFailureHandled
= true;
211 OUString
sWarning(SvtResId(STR_ERROR_JVMCREATIONFAILED_MAC
));
213 OUString
sWarning(SvtResId(STR_ERROR_JVMCREATIONFAILED
));
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();
225 else if ( anyExc
>>= e5
)
227 SolarMutexGuard aSolarGuard
;
228 if( !g_JavaEvents
.bRestartRequiredHandled
)
230 // a new JRE was selected, but office needs to be restarted
231 //before it can be used.
232 g_JavaEvents
.bRestartRequiredHandled
= true;
233 svtools::executeRestartDialog(
234 comphelper::getProcessComponentContext(), nullptr,
235 svtools::RESTART_REASON_JAVA
);
240 if ( nResult
== RET_CANCEL
|| nResult
== RET_NO
)
242 // Unknown exception type or user wants to cancel
246 else // nResult == RET_OK
248 // User selected OK => retry Java usage
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */