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