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