merge the formfield patch from ooo-build
[ooovba.git] / extensions / source / abpilot / abspilot.cxx
blobde6b4ace5a1648851e620e2ed28b2996530eaf31
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: abspilot.cxx,v $
10 * $Revision: 1.21 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_extensions.hxx"
33 #include "abspilot.hxx"
34 #ifndef EXTENSIONS_INC_EXTENSIO_HRC
35 #include "extensio.hrc"
36 #endif
37 #ifndef EXTENSIONS_ABPRESID_HRC
38 #include "abpresid.hrc"
39 #endif
40 #include "componentmodule.hxx"
41 #include <tools/debug.hxx>
42 #include <svtools/localresaccess.hxx>
43 #include "typeselectionpage.hxx"
44 #include "admininvokationpage.hxx"
45 #include "tableselectionpage.hxx"
46 #include <vcl/waitobj.hxx>
47 #include <vcl/msgbox.hxx>
48 #include "abpfinalpage.hxx"
49 #include "fieldmappingpage.hxx"
50 #include "fieldmappingimpl.hxx"
52 //.........................................................................
53 namespace abp
55 //.........................................................................
57 #define STATE_SELECT_ABTYPE 0
58 #define STATE_INVOKE_ADMIN_DIALOG 1
59 #define STATE_TABLE_SELECTION 2
60 #define STATE_MANUAL_FIELD_MAPPING 3
61 #define STATE_FINAL_CONFIRM 4
63 #define PATH_COMPLETE 1
64 #define PATH_NO_SETTINGS 2
65 #define PATH_NO_FIELDS 3
66 #define PATH_NO_SETTINGS_NO_FIELDS 4
68 using namespace ::svt;
69 using namespace ::com::sun::star::uno;
70 using namespace ::com::sun::star::lang;
72 //=====================================================================
73 //= OAddessBookSourcePilot
74 //=====================================================================
75 //---------------------------------------------------------------------
76 OAddessBookSourcePilot::OAddessBookSourcePilot(Window* _pParent, const Reference< XMultiServiceFactory >& _rxORB)
77 :OAddessBookSourcePilot_Base( _pParent, ModuleRes( RID_DLG_ADDRESSBOOKSOURCEPILOT ),
78 WZB_HELP | WZB_FINISH | WZB_CANCEL | WZB_NEXT | WZB_PREVIOUS )
79 ,m_xORB(_rxORB)
80 ,m_aNewDataSource(_rxORB)
81 ,m_eNewDataSourceType( AST_INVALID )
83 SetPageSizePixel(LogicToPixel(Size(WINDOW_SIZE_X, WINDOW_SIZE_Y), MAP_APPFONT));
85 ShowButtonFixedLine(sal_True);
87 declarePath( PATH_COMPLETE,
88 STATE_SELECT_ABTYPE,
89 STATE_INVOKE_ADMIN_DIALOG,
90 STATE_TABLE_SELECTION,
91 STATE_MANUAL_FIELD_MAPPING,
92 STATE_FINAL_CONFIRM,
93 WZS_INVALID_STATE
95 declarePath( PATH_NO_SETTINGS,
96 STATE_SELECT_ABTYPE,
97 STATE_TABLE_SELECTION,
98 STATE_MANUAL_FIELD_MAPPING,
99 STATE_FINAL_CONFIRM,
100 WZS_INVALID_STATE
102 declarePath( PATH_NO_FIELDS,
103 STATE_SELECT_ABTYPE,
104 STATE_INVOKE_ADMIN_DIALOG,
105 STATE_TABLE_SELECTION,
106 STATE_FINAL_CONFIRM,
107 WZS_INVALID_STATE
109 declarePath( PATH_NO_SETTINGS_NO_FIELDS,
110 STATE_SELECT_ABTYPE,
111 STATE_TABLE_SELECTION,
112 STATE_FINAL_CONFIRM,
113 WZS_INVALID_STATE
116 m_pPrevPage->SetHelpId(HID_ABSPILOT_PREVIOUS);
117 m_pNextPage->SetHelpId(HID_ABSPILOT_NEXT);
118 m_pCancel->SetHelpId(HID_ABSPILOT_CANCEL);
119 m_pFinish->SetHelpId(HID_ABSPILOT_FINISH);
120 m_pHelp->SetUniqueId(UID_ABSPILOT_HELP);
122 m_pCancel->SetClickHdl( LINK( this, OAddessBookSourcePilot, OnCancelClicked) );
124 // some initial settings
125 #ifdef UNX
126 #ifdef MACOSX
127 m_aSettings.eType = AST_MACAB;
128 #else
129 // FIXME: if KDE use KAB instead
130 m_aSettings.eType = AST_EVOLUTION;
131 #endif
132 #else
133 m_aSettings.eType = AST_OTHER;
134 #endif
135 m_aSettings.sDataSourceName = String(ModuleRes(RID_STR_DEFAULT_NAME));
136 m_aSettings.bRegisterDataSource = false;
137 m_aSettings.bIgnoreNoTable = false;
139 defaultButton(WZB_NEXT);
140 enableButtons(WZB_FINISH, sal_False);
141 ActivatePage();
143 typeSelectionChanged( m_aSettings.eType );
146 //---------------------------------------------------------------------
147 String OAddessBookSourcePilot::getStateDisplayName( WizardState _nState ) const
149 USHORT nResId = 0;
150 switch ( _nState )
152 case STATE_SELECT_ABTYPE: nResId = STR_SELECT_ABTYPE; break;
153 case STATE_INVOKE_ADMIN_DIALOG: nResId = STR_INVOKE_ADMIN_DIALOG; break;
154 case STATE_TABLE_SELECTION: nResId = STR_TABLE_SELECTION; break;
155 case STATE_MANUAL_FIELD_MAPPING: nResId = STR_MANUAL_FIELD_MAPPING; break;
156 case STATE_FINAL_CONFIRM: nResId = STR_FINAL_CONFIRM; break;
158 DBG_ASSERT( nResId, "OAddessBookSourcePilot::getStateDisplayName: don't know this state!" );
160 String sDisplayName;
161 if ( nResId )
163 svt::OLocalResourceAccess aAccess( ModuleRes( RID_DLG_ADDRESSBOOKSOURCEPILOT ), RSC_MODALDIALOG );
164 sDisplayName = String( ModuleRes( nResId ) );
167 return sDisplayName;
170 //---------------------------------------------------------------------
171 void OAddessBookSourcePilot::implCommitAll()
173 // in real, the data source already exists in the data source context
174 // Thus, if the user changed the name, we have to rename the data source
175 if ( m_aSettings.sDataSourceName != m_aNewDataSource.getName() )
176 m_aNewDataSource.rename( m_aSettings.sDataSourceName );
178 // 1. the data source
179 m_aNewDataSource.store();
181 // 2. check if we need to register the data source
182 if ( m_aSettings.bRegisterDataSource )
183 m_aNewDataSource.registerDataSource(m_aSettings.sRegisteredDataSourceName);
185 // 3. write the data source / table names into the configuration
186 addressconfig::writeTemplateAddressSource( getORB(), m_aSettings.bRegisterDataSource ? m_aSettings.sRegisteredDataSourceName : m_aSettings.sDataSourceName, m_aSettings.sSelectedTable );
188 // 4. write the field mapping
189 fieldmapping::writeTemplateAddressFieldMapping( getORB(), m_aSettings.aFieldMapping );
192 //---------------------------------------------------------------------
193 void OAddessBookSourcePilot::implCleanup()
195 if ( m_aNewDataSource.isValid() )
196 m_aNewDataSource.remove();
199 //---------------------------------------------------------------------
200 IMPL_LINK( OAddessBookSourcePilot, OnCancelClicked, void*, /*NOTINTERESTEDIN*/ )
202 // do cleanups
203 implCleanup();
205 // reset the click hdl
206 m_pCancel->SetClickHdl( Link() );
207 // simulate the click again - this time, the default handling of the button will strike ....
208 m_pCancel->Click();
210 return 0L;
213 //---------------------------------------------------------------------
214 BOOL OAddessBookSourcePilot::Close()
216 implCleanup();
218 return OAddessBookSourcePilot_Base::Close();
221 //---------------------------------------------------------------------
222 sal_Bool OAddessBookSourcePilot::onFinish(sal_Int32 _nResult)
224 if (!OAddessBookSourcePilot_Base::onFinish(_nResult))
225 return sal_False;
227 if (RET_OK != _nResult)
228 return sal_True;
230 implCommitAll();
232 addressconfig::markPilotSuccess( getORB() );
234 return sal_True;
237 //---------------------------------------------------------------------
238 void OAddessBookSourcePilot::enterState( WizardState _nState )
240 switch ( _nState )
242 case STATE_SELECT_ABTYPE:
243 impl_updateRoadmap( static_cast< TypeSelectionPage* >( GetPage( STATE_SELECT_ABTYPE ) )->getSelectedType() );
244 break;
246 case STATE_FINAL_CONFIRM:
247 if ( !needManualFieldMapping( ) )
248 implDoAutoFieldMapping();
249 break;
251 case STATE_TABLE_SELECTION:
252 implDefaultTableName();
253 break;
256 OAddessBookSourcePilot_Base::enterState(_nState);
259 //---------------------------------------------------------------------
260 sal_Bool OAddessBookSourcePilot::prepareLeaveCurrentState( CommitPageReason _eReason )
262 if ( !OAddessBookSourcePilot_Base::prepareLeaveCurrentState( _eReason ) )
263 return sal_False;
265 if ( _eReason == eTravelBackward )
266 return sal_True;
268 sal_Bool bAllow = sal_True;
270 switch ( getCurrentState() )
272 case STATE_SELECT_ABTYPE:
273 implCreateDataSource();
274 if ( needAdminInvokationPage() )
275 break;
276 // no break here
278 case STATE_INVOKE_ADMIN_DIALOG:
279 if ( !connectToDataSource( sal_False ) )
281 // connecting did not succeed -> do not allow proceeding
282 bAllow = sal_False;
283 break;
286 // ........................................................
287 // now that we connected to the data source, check whether we need the "table selection" page
288 const StringBag& aTables = m_aNewDataSource.getTableNames();
290 if ( aTables.empty() )
292 if ( ( _eReason == eValidateNoUI )
293 || ( RET_YES != QueryBox( this, ModuleRes( RID_QRY_NOTABLES ) ).Execute() )
296 // cannot ask the user, or the user chose to use this data source, though there are no tables
297 bAllow = sal_False;
298 break;
301 m_aSettings.bIgnoreNoTable = true;
304 if ( aTables.size() == 1 )
305 // remember the one and only table we have
306 m_aSettings.sSelectedTable = *aTables.begin();
308 break;
311 impl_updateRoadmap( m_aSettings.eType );
312 return bAllow;
315 //---------------------------------------------------------------------
316 void OAddessBookSourcePilot::implDefaultTableName()
318 const StringBag& rTableNames = getDataSource().getTableNames();
319 if ( rTableNames.end() != rTableNames.find( getSettings().sSelectedTable ) )
320 // already a valid table selected
321 return;
323 const sal_Char* pGuess = NULL;
324 switch ( getSettings().eType )
326 case AST_MORK :
327 case AST_THUNDERBIRD : pGuess = "Personal Address book"; break;
328 case AST_LDAP : pGuess = "LDAP Directory"; break;
329 case AST_EVOLUTION :
330 case AST_EVOLUTION_GROUPWISE:
331 case AST_EVOLUTION_LDAP : pGuess = "Personal"; break;
332 default:
333 DBG_ERROR( "OAddessBookSourcePilot::implDefaultTableName: unhandled case!" );
334 return;
336 const ::rtl::OUString sGuess = ::rtl::OUString::createFromAscii( pGuess );
337 if ( rTableNames.end() != rTableNames.find( sGuess ) )
338 getSettings().sSelectedTable = sGuess;
341 //---------------------------------------------------------------------
342 void OAddessBookSourcePilot::implDoAutoFieldMapping()
344 DBG_ASSERT( !needManualFieldMapping( ), "OAddessBookSourcePilot::implDoAutoFieldMapping: invalid call!" );
346 fieldmapping::defaultMapping( getORB(), m_aSettings.aFieldMapping );
349 //---------------------------------------------------------------------
350 void OAddessBookSourcePilot::implCreateDataSource()
352 if (m_aNewDataSource.isValid())
353 { // we already have a data source object
354 if ( m_aSettings.eType == m_eNewDataSourceType )
355 // and it already has the correct type
356 return;
358 // it has a wrong type -> remove it
359 m_aNewDataSource.remove();
362 ODataSourceContext aContext( getORB() );
363 aContext.disambiguate( m_aSettings.sDataSourceName );
365 switch (m_aSettings.eType)
367 case AST_MORK:
368 m_aNewDataSource = aContext.createNewMORK( m_aSettings.sDataSourceName );
369 break;
371 case AST_THUNDERBIRD:
372 m_aNewDataSource = aContext.createNewThunderbird( m_aSettings.sDataSourceName );
373 break;
375 case AST_EVOLUTION:
376 m_aNewDataSource = aContext.createNewEvolution( m_aSettings.sDataSourceName );
377 break;
379 case AST_EVOLUTION_GROUPWISE:
380 m_aNewDataSource = aContext.createNewEvolutionGroupwise( m_aSettings.sDataSourceName );
381 break;
383 case AST_EVOLUTION_LDAP:
384 m_aNewDataSource = aContext.createNewEvolutionLdap( m_aSettings.sDataSourceName );
385 break;
387 case AST_KAB:
388 m_aNewDataSource = aContext.createNewKab( m_aSettings.sDataSourceName );
389 break;
391 case AST_MACAB:
392 m_aNewDataSource = aContext.createNewMacab( m_aSettings.sDataSourceName );
393 break;
395 case AST_LDAP:
396 m_aNewDataSource = aContext.createNewLDAP( m_aSettings.sDataSourceName );
397 break;
399 case AST_OUTLOOK:
400 m_aNewDataSource = aContext.createNewOutlook( m_aSettings.sDataSourceName );
401 break;
403 case AST_OE:
404 m_aNewDataSource = aContext.createNewOE( m_aSettings.sDataSourceName );
405 break;
407 case AST_OTHER:
408 m_aNewDataSource = aContext.createNewDBase( m_aSettings.sDataSourceName );
409 break;
411 case AST_INVALID:
412 DBG_ERROR( "OAddessBookSourcePilot::implCreateDataSource: illegal data source type!" );
413 break;
415 m_eNewDataSourceType = m_aSettings.eType;
418 //---------------------------------------------------------------------
419 sal_Bool OAddessBookSourcePilot::connectToDataSource( sal_Bool _bForceReConnect )
421 DBG_ASSERT( m_aNewDataSource.isValid(), "OAddessBookSourcePilot::implConnect: invalid current data source!" );
423 WaitObject aWaitCursor( this );
424 if ( _bForceReConnect && m_aNewDataSource.isConnected( ) )
425 m_aNewDataSource.disconnect( );
427 return m_aNewDataSource.connect( this );
430 //---------------------------------------------------------------------
431 OWizardPage* OAddessBookSourcePilot::createPage(WizardState _nState)
433 switch (_nState)
435 case STATE_SELECT_ABTYPE:
436 return new TypeSelectionPage( this );
438 case STATE_INVOKE_ADMIN_DIALOG:
439 return new AdminDialogInvokationPage( this );
441 case STATE_TABLE_SELECTION:
442 return new TableSelectionPage( this );
444 case STATE_MANUAL_FIELD_MAPPING:
445 return new FieldMappingPage( this );
447 case STATE_FINAL_CONFIRM:
448 return new FinalPage( this );
450 default:
451 DBG_ERROR("OAddessBookSourcePilot::createPage: invalid state!");
452 return NULL;
456 //---------------------------------------------------------------------
457 void OAddessBookSourcePilot::impl_updateRoadmap( AddressSourceType _eType )
459 bool bSettingsPage = needAdminInvokationPage( _eType );
460 bool bTablesPage = needTableSelection( _eType );
461 bool bFieldsPage = needManualFieldMapping( _eType );
463 bool bConnected = m_aNewDataSource.isConnected();
464 bool bCanSkipTables =
465 ( m_aNewDataSource.hasTable( m_aSettings.sSelectedTable )
466 || m_aSettings.bIgnoreNoTable
469 enableState( STATE_INVOKE_ADMIN_DIALOG, bSettingsPage );
471 enableState( STATE_TABLE_SELECTION,
472 bTablesPage && ( bConnected ? !bCanSkipTables : !bSettingsPage )
473 // if we do not need a settings page, we connect upon "Next" on the first page
476 enableState( STATE_MANUAL_FIELD_MAPPING,
477 bFieldsPage && bConnected && m_aNewDataSource.hasTable( m_aSettings.sSelectedTable )
480 enableState( STATE_FINAL_CONFIRM,
481 bConnected && bCanSkipTables
485 //---------------------------------------------------------------------
486 void OAddessBookSourcePilot::typeSelectionChanged( AddressSourceType _eType )
488 PathId nCurrentPathID( PATH_COMPLETE );
489 bool bSettingsPage = needAdminInvokationPage( _eType );
490 bool bFieldsPage = needManualFieldMapping( _eType );
491 if ( !bSettingsPage )
492 if ( !bFieldsPage )
493 nCurrentPathID = PATH_NO_SETTINGS_NO_FIELDS;
494 else
495 nCurrentPathID = PATH_NO_SETTINGS;
496 else
497 if ( !bFieldsPage )
498 nCurrentPathID = PATH_NO_FIELDS;
499 else
500 nCurrentPathID = PATH_COMPLETE;
501 activatePath( nCurrentPathID, true );
503 m_aNewDataSource.disconnect();
504 m_aSettings.bIgnoreNoTable = false;
505 impl_updateRoadmap( _eType );
508 //.........................................................................
509 } // namespace abp
510 //.........................................................................