Line data Source code
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 :
21 : #include "../unowizard.hxx"
22 : #include "wizardshell.hxx"
23 :
24 : #include <com/sun/star/lang/XInitialization.hpp>
25 : #include <com/sun/star/beans/XPropertySetInfo.hpp>
26 : #include <com/sun/star/uno/XComponentContext.hpp>
27 : #include <com/sun/star/ucb/AlreadyInitializedException.hpp>
28 : #include <com/sun/star/ui/dialogs/XWizardController.hpp>
29 : #include <com/sun/star/ui/dialogs/WizardButton.hpp>
30 :
31 : #include <tools/diagnose_ex.h>
32 : #include <rtl/strbuf.hxx>
33 : #include <osl/mutex.hxx>
34 : #include <vcl/svapp.hxx>
35 : #include <tools/urlobj.hxx>
36 :
37 : //......................................................................................................................
38 : namespace svt { namespace uno
39 : {
40 : //......................................................................................................................
41 :
42 : using ::com::sun::star::uno::Reference;
43 : using ::com::sun::star::uno::XInterface;
44 : using ::com::sun::star::uno::UNO_QUERY;
45 : using ::com::sun::star::uno::UNO_QUERY_THROW;
46 : using ::com::sun::star::uno::UNO_SET_THROW;
47 : using ::com::sun::star::uno::Exception;
48 : using ::com::sun::star::uno::RuntimeException;
49 : using ::com::sun::star::uno::Any;
50 : using ::com::sun::star::uno::makeAny;
51 : using ::com::sun::star::uno::Sequence;
52 : using ::com::sun::star::uno::Type;
53 : using ::com::sun::star::lang::XServiceInfo;
54 : using ::com::sun::star::ui::dialogs::XWizard;
55 : using ::com::sun::star::lang::XInitialization;
56 : using ::com::sun::star::beans::XPropertySetInfo;
57 : using ::com::sun::star::uno::XComponentContext;
58 : using ::com::sun::star::beans::Property;
59 : using ::com::sun::star::lang::IllegalArgumentException;
60 : using ::com::sun::star::ucb::AlreadyInitializedException;
61 : using ::com::sun::star::ui::dialogs::XWizardController;
62 : using ::com::sun::star::ui::dialogs::XWizardPage;
63 : using ::com::sun::star::container::NoSuchElementException;
64 : using ::com::sun::star::util::InvalidStateException;
65 : using ::com::sun::star::awt::XWindow;
66 :
67 : namespace WizardButton = ::com::sun::star::ui::dialogs::WizardButton;
68 :
69 : //------------------------------------------------------------------------------------------------------------------
70 : namespace
71 : {
72 0 : sal_uInt32 lcl_convertWizardButtonToWZB( const sal_Int16 i_nWizardButton )
73 : {
74 0 : switch ( i_nWizardButton )
75 : {
76 0 : case WizardButton::NONE: return WZB_NONE;
77 0 : case WizardButton::NEXT: return WZB_NEXT;
78 0 : case WizardButton::PREVIOUS: return WZB_PREVIOUS;
79 0 : case WizardButton::FINISH: return WZB_FINISH;
80 0 : case WizardButton::CANCEL: return WZB_CANCEL;
81 0 : case WizardButton::HELP: return WZB_HELP;
82 : }
83 : OSL_FAIL( "lcl_convertWizardButtonToWZB: invalid WizardButton constant!" );
84 0 : return WZB_NONE;
85 : }
86 : }
87 :
88 : //==================================================================================================================
89 : //= Wizard - implementation
90 : //==================================================================================================================
91 : //------------------------------------------------------------------------------------------------------------------
92 0 : Wizard::Wizard( const Reference< XComponentContext >& _rxContext )
93 0 : :Wizard_Base( _rxContext )
94 : {
95 0 : }
96 :
97 : //--------------------------------------------------------------------
98 0 : Wizard::~Wizard()
99 : {
100 : // we do this here cause the base class' call to destroyDialog won't reach us anymore : we're within an dtor,
101 : // so this virtual-method-call the base class does does not work, we're already dead then ...
102 0 : if ( m_pDialog )
103 : {
104 0 : ::osl::MutexGuard aGuard( m_aMutex );
105 0 : if ( m_pDialog )
106 0 : destroyDialog();
107 : }
108 0 : }
109 :
110 : //--------------------------------------------------------------------
111 0 : Reference< XInterface > SAL_CALL Wizard::Create( const Reference< XComponentContext >& _rxContext )
112 : {
113 0 : return *(new Wizard( _rxContext ) );
114 : }
115 :
116 : //--------------------------------------------------------------------
117 : namespace
118 : {
119 0 : static void lcl_checkPaths( const Sequence< Sequence< sal_Int16 > >& i_rPaths, const Reference< XInterface >& i_rContext )
120 : {
121 : // need at least one path
122 0 : if ( i_rPaths.getLength() == 0 )
123 0 : throw IllegalArgumentException( OUString(), i_rContext, 2 );
124 :
125 : // each path must be of length 1, at least
126 0 : for ( sal_Int32 i = 0; i < i_rPaths.getLength(); ++i )
127 : {
128 0 : if ( i_rPaths[i].getLength() == 0 )
129 0 : throw IllegalArgumentException( OUString(), i_rContext, 2 );
130 :
131 : // page IDs must be in ascending order
132 0 : sal_Int16 nPreviousPageID = i_rPaths[i][0];
133 0 : for ( sal_Int32 j=1; j<i_rPaths[i].getLength(); ++j )
134 : {
135 0 : if ( i_rPaths[i][j] <= nPreviousPageID )
136 : {
137 0 : OStringBuffer message;
138 0 : message.append( "Path " );
139 0 : message.append( i );
140 0 : message.append( ": invalid page ID sequence - each page ID must be greater than the previous one." );
141 : throw IllegalArgumentException(
142 : OStringToOUString( message.makeStringAndClear(), RTL_TEXTENCODING_ASCII_US ),
143 0 : i_rContext, 2 );
144 : }
145 0 : nPreviousPageID = i_rPaths[i][j];
146 : }
147 : }
148 :
149 : // if we have one path, that's okay
150 0 : if ( i_rPaths.getLength() == 1 )
151 0 : return;
152 :
153 : // if we have multiple paths, they must start with the same page id
154 0 : const sal_Int16 nFirstPageId = i_rPaths[0][0];
155 0 : for ( sal_Int32 i = 0; i < i_rPaths.getLength(); ++i )
156 : {
157 0 : if ( i_rPaths[i][0] != nFirstPageId )
158 : throw IllegalArgumentException(
159 : OUString( "All paths must start with the same page id." ),
160 0 : i_rContext, 2 );
161 : }
162 : }
163 : }
164 :
165 : //--------------------------------------------------------------------
166 0 : void SAL_CALL Wizard::initialize( const Sequence< Any >& i_Arguments ) throw (Exception, RuntimeException)
167 : {
168 0 : ::osl::MutexGuard aGuard( m_aMutex );
169 0 : if ( m_bInitialized )
170 0 : throw AlreadyInitializedException( OUString(), *this );
171 :
172 0 : if ( i_Arguments.getLength() != 2 )
173 0 : throw IllegalArgumentException( OUString(), *this, -1 );
174 :
175 : // the second argument must be a XWizardController, for each constructor
176 0 : m_xController.set( i_Arguments[1], UNO_QUERY );
177 0 : if ( !m_xController.is() )
178 0 : throw IllegalArgumentException( OUString(), *this, 2 );
179 :
180 : // the first arg is either a single path (short[]), or multiple paths (short[][])
181 0 : Sequence< sal_Int16 > aSinglePath;
182 0 : i_Arguments[0] >>= aSinglePath;
183 0 : Sequence< Sequence< sal_Int16 > > aMultiplePaths;
184 0 : i_Arguments[0] >>= aMultiplePaths;
185 :
186 0 : if ( !aMultiplePaths.getLength() )
187 : {
188 0 : aMultiplePaths.realloc(1);
189 0 : aMultiplePaths[0] = aSinglePath;
190 : }
191 0 : lcl_checkPaths( aMultiplePaths, *this );
192 : // if we survived this, the paths are valid, and we're done here ...
193 0 : m_aWizardSteps = aMultiplePaths;
194 :
195 0 : m_bInitialized = true;
196 0 : }
197 :
198 0 : static OString lcl_getHelpId( const OUString& _rHelpURL )
199 : {
200 0 : INetURLObject aHID( _rHelpURL );
201 0 : if ( aHID.GetProtocol() == INET_PROT_HID )
202 0 : return OUStringToOString( aHID.GetURLPath(), RTL_TEXTENCODING_UTF8 );
203 : else
204 0 : return OUStringToOString( _rHelpURL, RTL_TEXTENCODING_UTF8 );
205 : }
206 :
207 : //------------------------------------------------------------------------
208 0 : static OUString lcl_getHelpURL( const OString& sHelpId )
209 : {
210 0 : OUStringBuffer aBuffer;
211 : OUString aTmp(
212 0 : OStringToOUString( sHelpId, RTL_TEXTENCODING_UTF8 ) );
213 0 : INetURLObject aHID( aTmp );
214 0 : if ( aHID.GetProtocol() == INET_PROT_NOT_VALID )
215 0 : aBuffer.appendAscii( INET_HID_SCHEME );
216 0 : aBuffer.append( aTmp.getStr() );
217 0 : return aBuffer.makeStringAndClear();
218 : }
219 :
220 : //--------------------------------------------------------------------
221 0 : Dialog* Wizard::createDialog( Window* i_pParent )
222 : {
223 0 : WizardShell* pDialog( new WizardShell( i_pParent, this, m_xController, m_aWizardSteps ) );
224 0 : pDialog->SetHelpId( lcl_getHelpId( m_sHelpURL ) );
225 0 : pDialog->setTitleBase( m_sTitle );
226 0 : return pDialog;
227 : }
228 :
229 : //--------------------------------------------------------------------
230 0 : void Wizard::destroyDialog()
231 : {
232 0 : if ( m_pDialog )
233 0 : m_sHelpURL = lcl_getHelpURL( m_pDialog->GetHelpId() );
234 :
235 0 : Wizard_Base::destroyDialog();
236 0 : }
237 :
238 : //--------------------------------------------------------------------
239 0 : OUString SAL_CALL Wizard::getImplementationName_static() throw(RuntimeException)
240 : {
241 0 : return OUString( "com.sun.star.comp.svtools.uno.Wizard" );
242 : }
243 :
244 : //--------------------------------------------------------------------
245 0 : Sequence< OUString > SAL_CALL Wizard::getSupportedServiceNames_static() throw(RuntimeException)
246 : {
247 0 : Sequence< OUString > aServices(1);
248 0 : aServices[0] = OUString( "com.sun.star.ui.dialogs.Wizard" );
249 0 : return aServices;
250 : }
251 :
252 : //--------------------------------------------------------------------
253 0 : OUString SAL_CALL Wizard::getImplementationName() throw(RuntimeException)
254 : {
255 0 : return getImplementationName_static();
256 : }
257 :
258 : //--------------------------------------------------------------------
259 0 : Sequence< OUString > SAL_CALL Wizard::getSupportedServiceNames() throw(RuntimeException)
260 : {
261 0 : return getSupportedServiceNames_static();
262 : }
263 :
264 : //--------------------------------------------------------------------
265 0 : Reference< XPropertySetInfo > SAL_CALL Wizard::getPropertySetInfo() throw(RuntimeException)
266 : {
267 0 : return createPropertySetInfo( getInfoHelper() );
268 : }
269 :
270 : //--------------------------------------------------------------------
271 0 : ::cppu::IPropertyArrayHelper& SAL_CALL Wizard::getInfoHelper()
272 : {
273 0 : return *const_cast< Wizard* >( this )->getArrayHelper();
274 : }
275 :
276 : //--------------------------------------------------------------------
277 0 : ::cppu::IPropertyArrayHelper* Wizard::createArrayHelper( ) const
278 : {
279 0 : Sequence< Property > aProps;
280 0 : describeProperties( aProps );
281 0 : return new ::cppu::OPropertyArrayHelper( aProps );
282 : }
283 :
284 : //------------------------------------------------------------------------------------------------------------------
285 0 : OUString SAL_CALL Wizard::getHelpURL() throw (RuntimeException)
286 : {
287 0 : SolarMutexGuard aSolarGuard;
288 0 : ::osl::MutexGuard aGuard( m_aMutex );
289 :
290 0 : if ( !m_pDialog )
291 0 : return m_sHelpURL;
292 :
293 0 : return lcl_getHelpURL( m_pDialog->GetHelpId() );
294 : }
295 :
296 : //------------------------------------------------------------------------------------------------------------------
297 0 : void SAL_CALL Wizard::setHelpURL( const OUString& i_HelpURL ) throw (RuntimeException)
298 : {
299 0 : SolarMutexGuard aSolarGuard;
300 0 : ::osl::MutexGuard aGuard( m_aMutex );
301 :
302 0 : if ( !m_pDialog )
303 0 : m_sHelpURL = i_HelpURL;
304 : else
305 0 : m_pDialog->SetHelpId( lcl_getHelpId( i_HelpURL ) );
306 0 : }
307 :
308 : //------------------------------------------------------------------------------------------------------------------
309 0 : Reference< XWindow > SAL_CALL Wizard::getDialogWindow() throw (RuntimeException)
310 : {
311 0 : SolarMutexGuard aSolarGuard;
312 0 : ::osl::MutexGuard aGuard( m_aMutex );
313 :
314 0 : ENSURE_OR_RETURN( m_pDialog, "Wizard::getDialogWindow: illegal call (execution did not start, yet)!", NULL );
315 0 : return Reference< XWindow >( m_pDialog->GetComponentInterface(), UNO_QUERY );
316 : }
317 :
318 : //------------------------------------------------------------------------------------------------------------------
319 0 : void SAL_CALL Wizard::enableButton( ::sal_Int16 i_WizardButton, ::sal_Bool i_Enable ) throw (RuntimeException)
320 : {
321 0 : SolarMutexGuard aSolarGuard;
322 0 : ::osl::MutexGuard aGuard( m_aMutex );
323 :
324 0 : WizardShell* pWizardImpl = dynamic_cast< WizardShell* >( m_pDialog );
325 0 : ENSURE_OR_RETURN_VOID( pWizardImpl, "Wizard::enableButtons: invalid dialog implementation!" );
326 :
327 0 : pWizardImpl->enableButtons( lcl_convertWizardButtonToWZB( i_WizardButton ), i_Enable );
328 : }
329 :
330 : //------------------------------------------------------------------------------------------------------------------
331 0 : void SAL_CALL Wizard::setDefaultButton( ::sal_Int16 i_WizardButton ) throw (RuntimeException)
332 : {
333 0 : SolarMutexGuard aSolarGuard;
334 0 : ::osl::MutexGuard aGuard( m_aMutex );
335 :
336 0 : WizardShell* pWizardImpl = dynamic_cast< WizardShell* >( m_pDialog );
337 0 : ENSURE_OR_RETURN_VOID( pWizardImpl, "Wizard::setDefaultButton: invalid dialog implementation!" );
338 :
339 0 : pWizardImpl->defaultButton( lcl_convertWizardButtonToWZB( i_WizardButton ) );
340 : }
341 :
342 : //------------------------------------------------------------------------------------------------------------------
343 0 : sal_Bool SAL_CALL Wizard::travelNext( ) throw (RuntimeException)
344 : {
345 0 : SolarMutexGuard aSolarGuard;
346 0 : ::osl::MutexGuard aGuard( m_aMutex );
347 :
348 0 : WizardShell* pWizardImpl = dynamic_cast< WizardShell* >( m_pDialog );
349 0 : ENSURE_OR_RETURN_FALSE( pWizardImpl, "Wizard::travelNext: invalid dialog implementation!" );
350 :
351 0 : return pWizardImpl->travelNext();
352 : }
353 :
354 : //------------------------------------------------------------------------------------------------------------------
355 0 : sal_Bool SAL_CALL Wizard::travelPrevious( ) throw (RuntimeException)
356 : {
357 0 : SolarMutexGuard aSolarGuard;
358 0 : ::osl::MutexGuard aGuard( m_aMutex );
359 :
360 0 : WizardShell* pWizardImpl = dynamic_cast< WizardShell* >( m_pDialog );
361 0 : ENSURE_OR_RETURN_FALSE( pWizardImpl, "Wizard::travelPrevious: invalid dialog implementation!" );
362 :
363 0 : return pWizardImpl->travelPrevious();
364 : }
365 :
366 : //------------------------------------------------------------------------------------------------------------------
367 0 : void SAL_CALL Wizard::enablePage( ::sal_Int16 i_PageID, ::sal_Bool i_Enable ) throw (NoSuchElementException, InvalidStateException, RuntimeException)
368 : {
369 0 : SolarMutexGuard aSolarGuard;
370 0 : ::osl::MutexGuard aGuard( m_aMutex );
371 :
372 0 : WizardShell* pWizardImpl = dynamic_cast< WizardShell* >( m_pDialog );
373 0 : ENSURE_OR_RETURN_VOID( pWizardImpl, "Wizard::enablePage: invalid dialog implementation!" );
374 :
375 0 : if ( !pWizardImpl->knowsPage( i_PageID ) )
376 0 : throw NoSuchElementException( OUString(), *this );
377 :
378 0 : if ( i_PageID == pWizardImpl->getCurrentPage() )
379 0 : throw InvalidStateException( OUString(), *this );
380 :
381 0 : pWizardImpl->enablePage( i_PageID, i_Enable );
382 : }
383 :
384 : //------------------------------------------------------------------------------------------------------------------
385 0 : void SAL_CALL Wizard::updateTravelUI( ) throw (RuntimeException)
386 : {
387 0 : SolarMutexGuard aSolarGuard;
388 0 : ::osl::MutexGuard aGuard( m_aMutex );
389 :
390 0 : WizardShell* pWizardImpl = dynamic_cast< WizardShell* >( m_pDialog );
391 0 : ENSURE_OR_RETURN_VOID( pWizardImpl, "Wizard::updateTravelUI: invalid dialog implementation!" );
392 :
393 0 : pWizardImpl->updateTravelUI();
394 : }
395 :
396 : //------------------------------------------------------------------------------------------------------------------
397 0 : ::sal_Bool SAL_CALL Wizard::advanceTo( ::sal_Int16 i_PageId ) throw (RuntimeException)
398 : {
399 0 : SolarMutexGuard aSolarGuard;
400 0 : ::osl::MutexGuard aGuard( m_aMutex );
401 :
402 0 : WizardShell* pWizardImpl = dynamic_cast< WizardShell* >( m_pDialog );
403 0 : ENSURE_OR_RETURN_FALSE( pWizardImpl, "Wizard::advanceTo: invalid dialog implementation!" );
404 :
405 0 : return pWizardImpl->advanceTo( i_PageId );
406 : }
407 :
408 : //------------------------------------------------------------------------------------------------------------------
409 0 : ::sal_Bool SAL_CALL Wizard::goBackTo( ::sal_Int16 i_PageId ) throw (RuntimeException)
410 : {
411 0 : SolarMutexGuard aSolarGuard;
412 0 : ::osl::MutexGuard aGuard( m_aMutex );
413 :
414 0 : WizardShell* pWizardImpl = dynamic_cast< WizardShell* >( m_pDialog );
415 0 : ENSURE_OR_RETURN_FALSE( pWizardImpl, "Wizard::goBackTo: invalid dialog implementation!" );
416 :
417 0 : return pWizardImpl->goBackTo( i_PageId );
418 : }
419 :
420 : //------------------------------------------------------------------------------------------------------------------
421 0 : Reference< XWizardPage > SAL_CALL Wizard::getCurrentPage( ) throw (RuntimeException)
422 : {
423 0 : SolarMutexGuard aSolarGuard;
424 0 : ::osl::MutexGuard aGuard( m_aMutex );
425 :
426 0 : WizardShell* pWizardImpl = dynamic_cast< WizardShell* >( m_pDialog );
427 0 : ENSURE_OR_RETURN( pWizardImpl, "Wizard::getCurrentPage: invalid dialog implementation!", Reference< XWizardPage >() );
428 :
429 0 : return pWizardImpl->getCurrentWizardPage();
430 : }
431 :
432 : //------------------------------------------------------------------------------------------------------------------
433 0 : void SAL_CALL Wizard::activatePath( ::sal_Int16 i_PathIndex, ::sal_Bool i_Final ) throw (NoSuchElementException, InvalidStateException, RuntimeException)
434 : {
435 0 : SolarMutexGuard aSolarGuard;
436 0 : ::osl::MutexGuard aGuard( m_aMutex );
437 :
438 0 : if ( ( i_PathIndex < 0 ) || ( i_PathIndex >= m_aWizardSteps.getLength() ) )
439 0 : throw NoSuchElementException( OUString(), *this );
440 :
441 0 : WizardShell* pWizardImpl = dynamic_cast< WizardShell* >( m_pDialog );
442 0 : ENSURE_OR_RETURN_VOID( pWizardImpl, "Wizard::activatePath: invalid dialog implementation!" );
443 :
444 0 : pWizardImpl->activatePath( i_PathIndex, i_Final );
445 : }
446 :
447 : //------------------------------------------------------------------------------------------------------------------
448 0 : void SAL_CALL Wizard::setTitle( const OUString& i_Title ) throw (RuntimeException)
449 : {
450 : // simply disambiguate
451 0 : Wizard_Base::OGenericUnoDialog::setTitle( i_Title );
452 0 : }
453 :
454 : //------------------------------------------------------------------------------------------------------------------
455 0 : ::sal_Int16 SAL_CALL Wizard::execute( ) throw (RuntimeException)
456 : {
457 0 : return Wizard_Base::OGenericUnoDialog::execute();
458 : }
459 :
460 : //......................................................................................................................
461 465 : } } // namespace svt::uno
462 : //......................................................................................................................
463 :
464 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|