1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_extensions.hxx"
30 #include "abspilot.hxx"
31 #include "abpilot.hrc"
32 #include "abpresid.hrc"
33 #include "componentmodule.hxx"
34 #include <tools/debug.hxx>
35 #include <svtools/localresaccess.hxx>
36 #include "typeselectionpage.hxx"
37 #include "admininvokationpage.hxx"
38 #include "tableselectionpage.hxx"
39 #include <vcl/waitobj.hxx>
40 #include <vcl/msgbox.hxx>
41 #include "abpfinalpage.hxx"
42 #include "fieldmappingpage.hxx"
43 #include "fieldmappingimpl.hxx"
45 //.........................................................................
48 //.........................................................................
50 #define STATE_SELECT_ABTYPE 0
51 #define STATE_INVOKE_ADMIN_DIALOG 1
52 #define STATE_TABLE_SELECTION 2
53 #define STATE_MANUAL_FIELD_MAPPING 3
54 #define STATE_FINAL_CONFIRM 4
56 #define PATH_COMPLETE 1
57 #define PATH_NO_SETTINGS 2
58 #define PATH_NO_FIELDS 3
59 #define PATH_NO_SETTINGS_NO_FIELDS 4
61 using namespace ::svt
;
62 using namespace ::com::sun::star::uno
;
63 using namespace ::com::sun::star::lang
;
65 //=====================================================================
66 //= OAddessBookSourcePilot
67 //=====================================================================
68 //---------------------------------------------------------------------
69 OAddessBookSourcePilot::OAddessBookSourcePilot(Window
* _pParent
, const Reference
< XMultiServiceFactory
>& _rxORB
)
70 :OAddessBookSourcePilot_Base( _pParent
, ModuleRes( RID_DLG_ADDRESSBOOKSOURCEPILOT
),
71 WZB_HELP
| WZB_FINISH
| WZB_CANCEL
| WZB_NEXT
| WZB_PREVIOUS
)
73 ,m_aNewDataSource(_rxORB
)
74 ,m_eNewDataSourceType( AST_INVALID
)
76 SetPageSizePixel(LogicToPixel(Size(WINDOW_SIZE_X
, WINDOW_SIZE_Y
), MAP_APPFONT
));
78 ShowButtonFixedLine(sal_True
);
80 declarePath( PATH_COMPLETE
,
82 STATE_INVOKE_ADMIN_DIALOG
,
83 STATE_TABLE_SELECTION
,
84 STATE_MANUAL_FIELD_MAPPING
,
88 declarePath( PATH_NO_SETTINGS
,
90 STATE_TABLE_SELECTION
,
91 STATE_MANUAL_FIELD_MAPPING
,
95 declarePath( PATH_NO_FIELDS
,
97 STATE_INVOKE_ADMIN_DIALOG
,
98 STATE_TABLE_SELECTION
,
102 declarePath( PATH_NO_SETTINGS_NO_FIELDS
,
104 STATE_TABLE_SELECTION
,
109 m_pPrevPage
->SetHelpId(HID_ABSPILOT_PREVIOUS
);
110 m_pNextPage
->SetHelpId(HID_ABSPILOT_NEXT
);
111 m_pCancel
->SetHelpId(HID_ABSPILOT_CANCEL
);
112 m_pFinish
->SetHelpId(HID_ABSPILOT_FINISH
);
113 m_pHelp
->SetUniqueId(UID_ABSPILOT_HELP
);
115 m_pCancel
->SetClickHdl( LINK( this, OAddessBookSourcePilot
, OnCancelClicked
) );
117 // some initial settings
119 m_aSettings
.eType
= AST_MACAB
;
122 m_aSettings
.eType
= AST_MORK
;
124 m_aSettings
.eType
= AST_OE
;
127 m_aSettings
.eType
= AST_OTHER
;
129 m_aSettings
.sDataSourceName
= String(ModuleRes(RID_STR_DEFAULT_NAME
));
130 m_aSettings
.bRegisterDataSource
= false;
131 m_aSettings
.bIgnoreNoTable
= false;
133 defaultButton(WZB_NEXT
);
134 enableButtons(WZB_FINISH
, sal_False
);
137 typeSelectionChanged( m_aSettings
.eType
);
140 //---------------------------------------------------------------------
141 String
OAddessBookSourcePilot::getStateDisplayName( WizardState _nState
) const
146 case STATE_SELECT_ABTYPE
: nResId
= STR_SELECT_ABTYPE
; break;
147 case STATE_INVOKE_ADMIN_DIALOG
: nResId
= STR_INVOKE_ADMIN_DIALOG
; break;
148 case STATE_TABLE_SELECTION
: nResId
= STR_TABLE_SELECTION
; break;
149 case STATE_MANUAL_FIELD_MAPPING
: nResId
= STR_MANUAL_FIELD_MAPPING
; break;
150 case STATE_FINAL_CONFIRM
: nResId
= STR_FINAL_CONFIRM
; break;
152 DBG_ASSERT( nResId
, "OAddessBookSourcePilot::getStateDisplayName: don't know this state!" );
157 svt::OLocalResourceAccess
aAccess( ModuleRes( RID_DLG_ADDRESSBOOKSOURCEPILOT
), RSC_MODALDIALOG
);
158 sDisplayName
= String( ModuleRes( nResId
) );
164 //---------------------------------------------------------------------
165 void OAddessBookSourcePilot::implCommitAll()
167 // in real, the data source already exists in the data source context
168 // Thus, if the user changed the name, we have to rename the data source
169 if ( m_aSettings
.sDataSourceName
!= m_aNewDataSource
.getName() )
170 m_aNewDataSource
.rename( m_aSettings
.sDataSourceName
);
172 // 1. the data source
173 m_aNewDataSource
.store();
175 // 2. check if we need to register the data source
176 if ( m_aSettings
.bRegisterDataSource
)
177 m_aNewDataSource
.registerDataSource(m_aSettings
.sRegisteredDataSourceName
);
179 // 3. write the data source / table names into the configuration
180 addressconfig::writeTemplateAddressSource( getORB(), m_aSettings
.bRegisterDataSource
? m_aSettings
.sRegisteredDataSourceName
: m_aSettings
.sDataSourceName
, m_aSettings
.sSelectedTable
);
182 // 4. write the field mapping
183 fieldmapping::writeTemplateAddressFieldMapping( getORB(), m_aSettings
.aFieldMapping
);
186 //---------------------------------------------------------------------
187 void OAddessBookSourcePilot::implCleanup()
189 if ( m_aNewDataSource
.isValid() )
190 m_aNewDataSource
.remove();
193 //---------------------------------------------------------------------
194 IMPL_LINK( OAddessBookSourcePilot
, OnCancelClicked
, void*, /*NOTINTERESTEDIN*/ )
199 // reset the click hdl
200 m_pCancel
->SetClickHdl( Link() );
201 // simulate the click again - this time, the default handling of the button will strike ....
207 //---------------------------------------------------------------------
208 BOOL
OAddessBookSourcePilot::Close()
212 return OAddessBookSourcePilot_Base::Close();
215 //---------------------------------------------------------------------
216 sal_Bool
OAddessBookSourcePilot::onFinish()
218 if ( !OAddessBookSourcePilot_Base::onFinish() )
223 addressconfig::markPilotSuccess( getORB() );
228 //---------------------------------------------------------------------
229 void OAddessBookSourcePilot::enterState( WizardState _nState
)
233 case STATE_SELECT_ABTYPE
:
234 impl_updateRoadmap( static_cast< TypeSelectionPage
* >( GetPage( STATE_SELECT_ABTYPE
) )->getSelectedType() );
237 case STATE_FINAL_CONFIRM
:
238 if ( !needManualFieldMapping( ) )
239 implDoAutoFieldMapping();
242 case STATE_TABLE_SELECTION
:
243 implDefaultTableName();
247 OAddessBookSourcePilot_Base::enterState(_nState
);
250 //---------------------------------------------------------------------
251 sal_Bool
OAddessBookSourcePilot::prepareLeaveCurrentState( CommitPageReason _eReason
)
253 if ( !OAddessBookSourcePilot_Base::prepareLeaveCurrentState( _eReason
) )
256 if ( _eReason
== eTravelBackward
)
259 sal_Bool bAllow
= sal_True
;
261 switch ( getCurrentState() )
263 case STATE_SELECT_ABTYPE
:
264 implCreateDataSource();
265 if ( needAdminInvokationPage() )
269 case STATE_INVOKE_ADMIN_DIALOG
:
270 if ( !connectToDataSource( sal_False
) )
272 // connecting did not succeed -> do not allow proceeding
277 // ........................................................
278 // now that we connected to the data source, check whether we need the "table selection" page
279 const StringBag
& aTables
= m_aNewDataSource
.getTableNames();
281 if ( aTables
.empty() )
283 if ( RET_YES
!= QueryBox( this, ModuleRes( RID_QRY_NOTABLES
) ).Execute() )
285 // cannot ask the user, or the user chose to use this data source, though there are no tables
290 m_aSettings
.bIgnoreNoTable
= true;
293 if ( aTables
.size() == 1 )
294 // remember the one and only table we have
295 m_aSettings
.sSelectedTable
= *aTables
.begin();
300 impl_updateRoadmap( m_aSettings
.eType
);
304 //---------------------------------------------------------------------
305 void OAddessBookSourcePilot::implDefaultTableName()
307 const StringBag
& rTableNames
= getDataSource().getTableNames();
308 if ( rTableNames
.end() != rTableNames
.find( getSettings().sSelectedTable
) )
309 // already a valid table selected
312 const sal_Char
* pGuess
= NULL
;
313 switch ( getSettings().eType
)
316 case AST_THUNDERBIRD
: pGuess
= "Personal Address book"; break;
317 case AST_LDAP
: pGuess
= "LDAP Directory"; break;
319 case AST_EVOLUTION_GROUPWISE
:
320 case AST_EVOLUTION_LDAP
: pGuess
= "Personal"; break;
322 DBG_ERROR( "OAddessBookSourcePilot::implDefaultTableName: unhandled case!" );
325 const ::rtl::OUString sGuess
= ::rtl::OUString::createFromAscii( pGuess
);
326 if ( rTableNames
.end() != rTableNames
.find( sGuess
) )
327 getSettings().sSelectedTable
= sGuess
;
330 //---------------------------------------------------------------------
331 void OAddessBookSourcePilot::implDoAutoFieldMapping()
333 DBG_ASSERT( !needManualFieldMapping( ), "OAddessBookSourcePilot::implDoAutoFieldMapping: invalid call!" );
335 fieldmapping::defaultMapping( getORB(), m_aSettings
.aFieldMapping
);
338 //---------------------------------------------------------------------
339 void OAddessBookSourcePilot::implCreateDataSource()
341 if (m_aNewDataSource
.isValid())
342 { // we already have a data source object
343 if ( m_aSettings
.eType
== m_eNewDataSourceType
)
344 // and it already has the correct type
347 // it has a wrong type -> remove it
348 m_aNewDataSource
.remove();
351 ODataSourceContext
aContext( getORB() );
352 aContext
.disambiguate( m_aSettings
.sDataSourceName
);
354 switch (m_aSettings
.eType
)
357 m_aNewDataSource
= aContext
.createNewMORK( m_aSettings
.sDataSourceName
);
360 case AST_THUNDERBIRD
:
361 m_aNewDataSource
= aContext
.createNewThunderbird( m_aSettings
.sDataSourceName
);
365 m_aNewDataSource
= aContext
.createNewEvolution( m_aSettings
.sDataSourceName
);
368 case AST_EVOLUTION_GROUPWISE
:
369 m_aNewDataSource
= aContext
.createNewEvolutionGroupwise( m_aSettings
.sDataSourceName
);
372 case AST_EVOLUTION_LDAP
:
373 m_aNewDataSource
= aContext
.createNewEvolutionLdap( m_aSettings
.sDataSourceName
);
377 m_aNewDataSource
= aContext
.createNewKab( m_aSettings
.sDataSourceName
);
381 m_aNewDataSource
= aContext
.createNewMacab( m_aSettings
.sDataSourceName
);
385 m_aNewDataSource
= aContext
.createNewLDAP( m_aSettings
.sDataSourceName
);
389 m_aNewDataSource
= aContext
.createNewOutlook( m_aSettings
.sDataSourceName
);
393 m_aNewDataSource
= aContext
.createNewOE( m_aSettings
.sDataSourceName
);
397 m_aNewDataSource
= aContext
.createNewDBase( m_aSettings
.sDataSourceName
);
401 DBG_ERROR( "OAddessBookSourcePilot::implCreateDataSource: illegal data source type!" );
404 m_eNewDataSourceType
= m_aSettings
.eType
;
407 //---------------------------------------------------------------------
408 sal_Bool
OAddessBookSourcePilot::connectToDataSource( sal_Bool _bForceReConnect
)
410 DBG_ASSERT( m_aNewDataSource
.isValid(), "OAddessBookSourcePilot::implConnect: invalid current data source!" );
412 WaitObject
aWaitCursor( this );
413 if ( _bForceReConnect
&& m_aNewDataSource
.isConnected( ) )
414 m_aNewDataSource
.disconnect( );
416 return m_aNewDataSource
.connect( this );
419 //---------------------------------------------------------------------
420 OWizardPage
* OAddessBookSourcePilot::createPage(WizardState _nState
)
424 case STATE_SELECT_ABTYPE
:
425 return new TypeSelectionPage( this );
427 case STATE_INVOKE_ADMIN_DIALOG
:
428 return new AdminDialogInvokationPage( this );
430 case STATE_TABLE_SELECTION
:
431 return new TableSelectionPage( this );
433 case STATE_MANUAL_FIELD_MAPPING
:
434 return new FieldMappingPage( this );
436 case STATE_FINAL_CONFIRM
:
437 return new FinalPage( this );
440 DBG_ERROR("OAddessBookSourcePilot::createPage: invalid state!");
445 //---------------------------------------------------------------------
446 void OAddessBookSourcePilot::impl_updateRoadmap( AddressSourceType _eType
)
448 bool bSettingsPage
= needAdminInvokationPage( _eType
);
449 bool bTablesPage
= needTableSelection( _eType
);
450 bool bFieldsPage
= needManualFieldMapping( _eType
);
452 bool bConnected
= m_aNewDataSource
.isConnected();
453 bool bCanSkipTables
=
454 ( m_aNewDataSource
.hasTable( m_aSettings
.sSelectedTable
)
455 || m_aSettings
.bIgnoreNoTable
458 enableState( STATE_INVOKE_ADMIN_DIALOG
, bSettingsPage
);
460 enableState( STATE_TABLE_SELECTION
,
461 bTablesPage
&& ( bConnected
? !bCanSkipTables
: !bSettingsPage
)
462 // if we do not need a settings page, we connect upon "Next" on the first page
465 enableState( STATE_MANUAL_FIELD_MAPPING
,
466 bFieldsPage
&& bConnected
&& m_aNewDataSource
.hasTable( m_aSettings
.sSelectedTable
)
469 enableState( STATE_FINAL_CONFIRM
,
470 bConnected
&& bCanSkipTables
474 //---------------------------------------------------------------------
475 void OAddessBookSourcePilot::typeSelectionChanged( AddressSourceType _eType
)
477 PathId
nCurrentPathID( PATH_COMPLETE
);
478 bool bSettingsPage
= needAdminInvokationPage( _eType
);
479 bool bFieldsPage
= needManualFieldMapping( _eType
);
480 if ( !bSettingsPage
)
482 nCurrentPathID
= PATH_NO_SETTINGS_NO_FIELDS
;
484 nCurrentPathID
= PATH_NO_SETTINGS
;
487 nCurrentPathID
= PATH_NO_FIELDS
;
489 nCurrentPathID
= PATH_COMPLETE
;
490 activatePath( nCurrentPathID
, true );
492 m_aNewDataSource
.disconnect();
493 m_aSettings
.bIgnoreNoTable
= false;
494 impl_updateRoadmap( _eType
);
497 //.........................................................................
499 //.........................................................................