1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_svtools.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include <svtools/svtools.hrc>
34*cdf0e10cSrcweir #include <tools/resid.hxx>
35*cdf0e10cSrcweir #include <com/sun/star/task/XInteractionContinuation.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/task/XInteractionAbort.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/task/XInteractionRetry.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/java/JavaNotFoundException.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/java/InvalidJavaSettingsException.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/java/JavaDisabledException.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/java/JavaVMCreationFailureException.hpp>
42*cdf0e10cSrcweir #include <com/sun/star/java/RestartRequiredException.hpp>
43*cdf0e10cSrcweir #include <vcl/svapp.hxx>
44*cdf0e10cSrcweir #include <vcl/msgbox.hxx>
45*cdf0e10cSrcweir #include <vos/mutex.hxx>
46*cdf0e10cSrcweir #include <tools/string.hxx>
47*cdf0e10cSrcweir #include <tools/rcid.h>
48*cdf0e10cSrcweir #include <jvmfwk/framework.h>
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir #include <svtools/svtdata.hxx>
51*cdf0e10cSrcweir #include <svtools/javainteractionhandler.hxx>
52*cdf0e10cSrcweir #include <svtools/javacontext.hxx>
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir using namespace com::sun::star::uno;
55*cdf0e10cSrcweir using namespace com::sun::star::task;
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir namespace svt
58*cdf0e10cSrcweir {
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir JavaInteractionHandler::JavaInteractionHandler():
61*cdf0e10cSrcweir     m_aRefCount(0),
62*cdf0e10cSrcweir     m_bShowErrorsOnce(false),
63*cdf0e10cSrcweir     m_bJavaDisabled_Handled(false),
64*cdf0e10cSrcweir     m_bInvalidSettings_Handled(false),
65*cdf0e10cSrcweir     m_bJavaNotFound_Handled(false),
66*cdf0e10cSrcweir     m_bVMCreationFailure_Handled(false),
67*cdf0e10cSrcweir     m_bRestartRequired_Handled(false),
68*cdf0e10cSrcweir     m_nResult_JavaDisabled(RET_NO)
69*cdf0e10cSrcweir {
70*cdf0e10cSrcweir }
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir JavaInteractionHandler::JavaInteractionHandler(bool bReportErrorOnce) :
73*cdf0e10cSrcweir     m_aRefCount(0),
74*cdf0e10cSrcweir     m_bShowErrorsOnce(bReportErrorOnce),
75*cdf0e10cSrcweir     m_bJavaDisabled_Handled(false),
76*cdf0e10cSrcweir     m_bInvalidSettings_Handled(false),
77*cdf0e10cSrcweir     m_bJavaNotFound_Handled(false),
78*cdf0e10cSrcweir     m_bVMCreationFailure_Handled(false),
79*cdf0e10cSrcweir     m_bRestartRequired_Handled(false),
80*cdf0e10cSrcweir     m_nResult_JavaDisabled(RET_NO)
81*cdf0e10cSrcweir {
82*cdf0e10cSrcweir }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir JavaInteractionHandler::~JavaInteractionHandler()
85*cdf0e10cSrcweir {
86*cdf0e10cSrcweir }
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir Any SAL_CALL JavaInteractionHandler::queryInterface(const Type& aType )
89*cdf0e10cSrcweir     throw (RuntimeException)
90*cdf0e10cSrcweir {
91*cdf0e10cSrcweir     if (aType == getCppuType(reinterpret_cast<Reference<XInterface>*>(0)))
92*cdf0e10cSrcweir         return Any( static_cast<XInterface*>(this), aType);
93*cdf0e10cSrcweir     else if (aType == getCppuType(reinterpret_cast<Reference<XInteractionHandler>*>(0)))
94*cdf0e10cSrcweir         return Any( static_cast<XInteractionHandler*>(this), aType);
95*cdf0e10cSrcweir     return Any();
96*cdf0e10cSrcweir }
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir void SAL_CALL JavaInteractionHandler::acquire(  ) throw ()
99*cdf0e10cSrcweir {
100*cdf0e10cSrcweir     osl_incrementInterlockedCount( &m_aRefCount );
101*cdf0e10cSrcweir }
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir void SAL_CALL JavaInteractionHandler::release(  ) throw ()
104*cdf0e10cSrcweir {
105*cdf0e10cSrcweir     if (! osl_decrementInterlockedCount( &m_aRefCount ))
106*cdf0e10cSrcweir 		delete this;
107*cdf0e10cSrcweir }
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir void SAL_CALL JavaInteractionHandler::handle( const Reference< XInteractionRequest >& Request ) throw (RuntimeException)
111*cdf0e10cSrcweir {
112*cdf0e10cSrcweir     Any anyExc = Request->getRequest();
113*cdf0e10cSrcweir     Sequence< Reference< XInteractionContinuation > > aSeqCont = Request->getContinuations();
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir     Reference< XInteractionAbort > abort;
116*cdf0e10cSrcweir     Reference< XInteractionRetry > retry;
117*cdf0e10cSrcweir     sal_Int32 i;
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir     for ( i = 0; i < aSeqCont.getLength(); i++ )
120*cdf0e10cSrcweir     {
121*cdf0e10cSrcweir         abort = Reference< XInteractionAbort>::query( aSeqCont[i]);
122*cdf0e10cSrcweir         if ( abort.is() )
123*cdf0e10cSrcweir             break;
124*cdf0e10cSrcweir     }
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 	for ( i= 0; i < aSeqCont.getLength(); i++)
127*cdf0e10cSrcweir     {
128*cdf0e10cSrcweir         retry= Reference<XInteractionRetry>::query( aSeqCont[i]);
129*cdf0e10cSrcweir         if ( retry.is() )
130*cdf0e10cSrcweir             break;
131*cdf0e10cSrcweir     }
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir     com::sun::star::java::JavaNotFoundException e1;
134*cdf0e10cSrcweir     com::sun::star::java::InvalidJavaSettingsException e2;
135*cdf0e10cSrcweir  	com::sun::star::java::JavaDisabledException				e3;
136*cdf0e10cSrcweir     com::sun::star::java::JavaVMCreationFailureException	e4;
137*cdf0e10cSrcweir     com::sun::star::java::RestartRequiredException e5;
138*cdf0e10cSrcweir 	// Try to recover the Exception type in the any and
139*cdf0e10cSrcweir 	// react accordingly.
140*cdf0e10cSrcweir 	sal_uInt16		nResult = RET_CANCEL;
141*cdf0e10cSrcweir     ::rtl::OUString    aParameter;
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir 	if ( anyExc >>= e1 )
144*cdf0e10cSrcweir 	{
145*cdf0e10cSrcweir         if( ! (m_bShowErrorsOnce && m_bJavaNotFound_Handled))
146*cdf0e10cSrcweir         {
147*cdf0e10cSrcweir            // No suitable JRE found
148*cdf0e10cSrcweir             vos::OGuard aSolarGuard( Application::GetSolarMutex() );
149*cdf0e10cSrcweir             m_bJavaNotFound_Handled = true;
150*cdf0e10cSrcweir             WarningBox aWarningBox( NULL, SvtResId( WARNINGBOX_JAVANOTFOUND ) );
151*cdf0e10cSrcweir             String aTitle( SvtResId( STR_WARNING_JAVANOTFOUND ) );
152*cdf0e10cSrcweir             aWarningBox.SetText( aTitle );
153*cdf0e10cSrcweir             nResult = aWarningBox.Execute();
154*cdf0e10cSrcweir         }
155*cdf0e10cSrcweir         else
156*cdf0e10cSrcweir         {
157*cdf0e10cSrcweir             nResult = RET_OK;
158*cdf0e10cSrcweir         }
159*cdf0e10cSrcweir 	}
160*cdf0e10cSrcweir 	else if ( anyExc >>= e2 )
161*cdf0e10cSrcweir 	{
162*cdf0e10cSrcweir         if( !(m_bShowErrorsOnce && m_bInvalidSettings_Handled))
163*cdf0e10cSrcweir         {
164*cdf0e10cSrcweir            // javavendors.xml was updated and Java has not been configured yet
165*cdf0e10cSrcweir             vos::OGuard aSolarGuard( Application::GetSolarMutex() );
166*cdf0e10cSrcweir             m_bInvalidSettings_Handled = true;
167*cdf0e10cSrcweir             WarningBox aWarningBox( NULL, SvtResId( WARNINGBOX_INVALIDJAVASETTINGS ) );
168*cdf0e10cSrcweir             String aTitle( SvtResId(STR_WARNING_INVALIDJAVASETTINGS));
169*cdf0e10cSrcweir             aWarningBox.SetText( aTitle );
170*cdf0e10cSrcweir             nResult = aWarningBox.Execute();
171*cdf0e10cSrcweir         }
172*cdf0e10cSrcweir         else
173*cdf0e10cSrcweir         {
174*cdf0e10cSrcweir             nResult = RET_OK;
175*cdf0e10cSrcweir         }
176*cdf0e10cSrcweir 	}
177*cdf0e10cSrcweir 	else if ( anyExc >>= e3 )
178*cdf0e10cSrcweir 	{
179*cdf0e10cSrcweir         if( !(m_bShowErrorsOnce && m_bJavaDisabled_Handled))
180*cdf0e10cSrcweir         {
181*cdf0e10cSrcweir             vos::OGuard aSolarGuard( Application::GetSolarMutex() );
182*cdf0e10cSrcweir             m_bJavaDisabled_Handled = true;
183*cdf0e10cSrcweir             // Java disabled. Give user a chance to enable Java inside Office.
184*cdf0e10cSrcweir             QueryBox aQueryBox( NULL, SvtResId( QBX_JAVADISABLED ) );
185*cdf0e10cSrcweir             String aTitle( SvtResId( STR_QUESTION_JAVADISABLED ) );
186*cdf0e10cSrcweir             aQueryBox.SetText( aTitle );
187*cdf0e10cSrcweir             nResult = aQueryBox.Execute();
188*cdf0e10cSrcweir             if ( nResult == RET_YES )
189*cdf0e10cSrcweir             {
190*cdf0e10cSrcweir                 jfw_setEnabled(sal_True);
191*cdf0e10cSrcweir             }
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir             m_nResult_JavaDisabled = nResult;
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir         }
196*cdf0e10cSrcweir         else
197*cdf0e10cSrcweir         {
198*cdf0e10cSrcweir             nResult = m_nResult_JavaDisabled;
199*cdf0e10cSrcweir         }
200*cdf0e10cSrcweir 	}
201*cdf0e10cSrcweir 	else if ( anyExc >>= e4 )
202*cdf0e10cSrcweir 	{
203*cdf0e10cSrcweir         if( !(m_bShowErrorsOnce && m_bVMCreationFailure_Handled))
204*cdf0e10cSrcweir         {
205*cdf0e10cSrcweir             // Java not correctly installed, or damaged
206*cdf0e10cSrcweir             vos::OGuard aSolarGuard( Application::GetSolarMutex() );
207*cdf0e10cSrcweir             m_bVMCreationFailure_Handled = true;
208*cdf0e10cSrcweir             ErrorBox aErrorBox( NULL, SvtResId( ERRORBOX_JVMCREATIONFAILED ) );
209*cdf0e10cSrcweir             String aTitle( SvtResId( STR_ERROR_JVMCREATIONFAILED ) );
210*cdf0e10cSrcweir             aErrorBox.SetText( aTitle );
211*cdf0e10cSrcweir             nResult = aErrorBox.Execute();
212*cdf0e10cSrcweir         }
213*cdf0e10cSrcweir         else
214*cdf0e10cSrcweir         {
215*cdf0e10cSrcweir             nResult = RET_OK;
216*cdf0e10cSrcweir         }
217*cdf0e10cSrcweir 	}
218*cdf0e10cSrcweir 	else if ( anyExc >>= e5 )
219*cdf0e10cSrcweir 	{
220*cdf0e10cSrcweir         if( !(m_bShowErrorsOnce && m_bRestartRequired_Handled))
221*cdf0e10cSrcweir         {
222*cdf0e10cSrcweir             // a new JRE was selected, but office needs to be restarted
223*cdf0e10cSrcweir             //before it can be used.
224*cdf0e10cSrcweir             vos::OGuard aSolarGuard( Application::GetSolarMutex() );
225*cdf0e10cSrcweir             m_bRestartRequired_Handled = true;
226*cdf0e10cSrcweir             ErrorBox aErrorBox(NULL, SvtResId( ERRORBOX_RESTARTREQUIRED ) );
227*cdf0e10cSrcweir             String aTitle( SvtResId( STR_ERROR_RESTARTREQUIRED ) );
228*cdf0e10cSrcweir             aErrorBox.SetText( aTitle );
229*cdf0e10cSrcweir             nResult = aErrorBox.Execute();
230*cdf0e10cSrcweir         }
231*cdf0e10cSrcweir         else
232*cdf0e10cSrcweir         {
233*cdf0e10cSrcweir             nResult = RET_OK;
234*cdf0e10cSrcweir         }
235*cdf0e10cSrcweir 	}
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir 	if ( nResult == RET_CANCEL || nResult == RET_NO)
238*cdf0e10cSrcweir 	{
239*cdf0e10cSrcweir 		// Unknown exception type or user wants to cancel
240*cdf0e10cSrcweir 		if ( abort.is() )
241*cdf0e10cSrcweir 			abort->select();
242*cdf0e10cSrcweir 	}
243*cdf0e10cSrcweir 	else // nResult == RET_OK
244*cdf0e10cSrcweir 	{
245*cdf0e10cSrcweir 		// User selected OK => retry Java usage
246*cdf0e10cSrcweir 		if ( retry.is() )
247*cdf0e10cSrcweir 			retry->select();
248*cdf0e10cSrcweir 	}
249*cdf0e10cSrcweir }
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir }
252