1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: abspilot.cxx,v $
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"
37 #ifndef EXTENSIONS_ABPRESID_HRC
38 #include "abpresid.hrc"
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 //.........................................................................
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
)
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
,
89 STATE_INVOKE_ADMIN_DIALOG
,
90 STATE_TABLE_SELECTION
,
91 STATE_MANUAL_FIELD_MAPPING
,
95 declarePath( PATH_NO_SETTINGS
,
97 STATE_TABLE_SELECTION
,
98 STATE_MANUAL_FIELD_MAPPING
,
102 declarePath( PATH_NO_FIELDS
,
104 STATE_INVOKE_ADMIN_DIALOG
,
105 STATE_TABLE_SELECTION
,
109 declarePath( PATH_NO_SETTINGS_NO_FIELDS
,
111 STATE_TABLE_SELECTION
,
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
127 m_aSettings
.eType
= AST_MACAB
;
129 // FIXME: if KDE use KAB instead
130 m_aSettings
.eType
= AST_EVOLUTION
;
133 m_aSettings
.eType
= AST_OTHER
;
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
);
143 typeSelectionChanged( m_aSettings
.eType
);
146 //---------------------------------------------------------------------
147 String
OAddessBookSourcePilot::getStateDisplayName( WizardState _nState
) const
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!" );
163 svt::OLocalResourceAccess
aAccess( ModuleRes( RID_DLG_ADDRESSBOOKSOURCEPILOT
), RSC_MODALDIALOG
);
164 sDisplayName
= String( ModuleRes( nResId
) );
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*/ )
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 ....
213 //---------------------------------------------------------------------
214 BOOL
OAddessBookSourcePilot::Close()
218 return OAddessBookSourcePilot_Base::Close();
221 //---------------------------------------------------------------------
222 sal_Bool
OAddessBookSourcePilot::onFinish(sal_Int32 _nResult
)
224 if (!OAddessBookSourcePilot_Base::onFinish(_nResult
))
227 if (RET_OK
!= _nResult
)
232 addressconfig::markPilotSuccess( getORB() );
237 //---------------------------------------------------------------------
238 void OAddessBookSourcePilot::enterState( WizardState _nState
)
242 case STATE_SELECT_ABTYPE
:
243 impl_updateRoadmap( static_cast< TypeSelectionPage
* >( GetPage( STATE_SELECT_ABTYPE
) )->getSelectedType() );
246 case STATE_FINAL_CONFIRM
:
247 if ( !needManualFieldMapping( ) )
248 implDoAutoFieldMapping();
251 case STATE_TABLE_SELECTION
:
252 implDefaultTableName();
256 OAddessBookSourcePilot_Base::enterState(_nState
);
259 //---------------------------------------------------------------------
260 sal_Bool
OAddessBookSourcePilot::prepareLeaveCurrentState( CommitPageReason _eReason
)
262 if ( !OAddessBookSourcePilot_Base::prepareLeaveCurrentState( _eReason
) )
265 if ( _eReason
== eTravelBackward
)
268 sal_Bool bAllow
= sal_True
;
270 switch ( getCurrentState() )
272 case STATE_SELECT_ABTYPE
:
273 implCreateDataSource();
274 if ( needAdminInvokationPage() )
278 case STATE_INVOKE_ADMIN_DIALOG
:
279 if ( !connectToDataSource( sal_False
) )
281 // connecting did not succeed -> do not allow proceeding
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
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();
311 impl_updateRoadmap( m_aSettings
.eType
);
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
323 const sal_Char
* pGuess
= NULL
;
324 switch ( getSettings().eType
)
327 case AST_THUNDERBIRD
: pGuess
= "Personal Address book"; break;
328 case AST_LDAP
: pGuess
= "LDAP Directory"; break;
330 case AST_EVOLUTION_GROUPWISE
:
331 case AST_EVOLUTION_LDAP
: pGuess
= "Personal"; break;
333 DBG_ERROR( "OAddessBookSourcePilot::implDefaultTableName: unhandled case!" );
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
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
)
368 m_aNewDataSource
= aContext
.createNewMORK( m_aSettings
.sDataSourceName
);
371 case AST_THUNDERBIRD
:
372 m_aNewDataSource
= aContext
.createNewThunderbird( m_aSettings
.sDataSourceName
);
376 m_aNewDataSource
= aContext
.createNewEvolution( m_aSettings
.sDataSourceName
);
379 case AST_EVOLUTION_GROUPWISE
:
380 m_aNewDataSource
= aContext
.createNewEvolutionGroupwise( m_aSettings
.sDataSourceName
);
383 case AST_EVOLUTION_LDAP
:
384 m_aNewDataSource
= aContext
.createNewEvolutionLdap( m_aSettings
.sDataSourceName
);
388 m_aNewDataSource
= aContext
.createNewKab( m_aSettings
.sDataSourceName
);
392 m_aNewDataSource
= aContext
.createNewMacab( m_aSettings
.sDataSourceName
);
396 m_aNewDataSource
= aContext
.createNewLDAP( m_aSettings
.sDataSourceName
);
400 m_aNewDataSource
= aContext
.createNewOutlook( m_aSettings
.sDataSourceName
);
404 m_aNewDataSource
= aContext
.createNewOE( m_aSettings
.sDataSourceName
);
408 m_aNewDataSource
= aContext
.createNewDBase( m_aSettings
.sDataSourceName
);
412 DBG_ERROR( "OAddessBookSourcePilot::implCreateDataSource: illegal data source type!" );
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
)
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 );
451 DBG_ERROR("OAddessBookSourcePilot::createPage: invalid state!");
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
)
493 nCurrentPathID
= PATH_NO_SETTINGS_NO_FIELDS
;
495 nCurrentPathID
= PATH_NO_SETTINGS
;
498 nCurrentPathID
= PATH_NO_FIELDS
;
500 nCurrentPathID
= PATH_COMPLETE
;
501 activatePath( nCurrentPathID
, true );
503 m_aNewDataSource
.disconnect();
504 m_aSettings
.bIgnoreNoTable
= false;
505 impl_updateRoadmap( _eType
);
508 //.........................................................................
510 //.........................................................................