avoid some OUString <-> string_view conversion
[LibreOffice.git] / svtools / source / java / javainteractionhandler.cxx
blob79052d8844562883bfe69edf60d458752cd9231d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
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;
43 namespace
45 struct 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)
59 } g_JavaEvents;
62 namespace svt
65 JavaInteractionHandler::JavaInteractionHandler() :
66 m_aRefCount(0)
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);
80 return Any();
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 ))
91 delete this;
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 );
105 if ( abort.is() )
106 break;
109 for ( const auto& rCont : aSeqCont )
111 retry.set( rCont, UNO_QUERY );
112 if ( retry.is() )
113 break;
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;
125 if ( anyExc >>= e1 )
127 SolarMutexGuard aSolarGuard;
128 if( !g_JavaEvents.bNotFoundHandled )
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();
153 else
155 nResult = RET_OK;
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;
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();
175 else
177 nResult = RET_OK;
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;
198 else
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;
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();
220 else
222 nResult = RET_OK;
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);
237 nResult = RET_OK;
240 if ( nResult == RET_CANCEL || nResult == RET_NO)
242 // Unknown exception type or user wants to cancel
243 if ( abort.is() )
244 abort->select();
246 else // nResult == RET_OK
248 // User selected OK => retry Java usage
249 if ( retry.is() )
250 retry->select();
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */