1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "abspilot.hxx"
22 #include <strings.hrc>
23 #include <componentmodule.hxx>
24 #include <tools/debug.hxx>
25 #include "typeselectionpage.hxx"
26 #include "admininvokationpage.hxx"
27 #include "tableselectionpage.hxx"
28 #include <vcl/settings.hxx>
29 #include <vcl/svapp.hxx>
30 #include <vcl/waitobj.hxx>
31 #include <vcl/weld.hxx>
32 #include <osl/diagnose.h>
33 #include "abpfinalpage.hxx"
34 #include "fieldmappingpage.hxx"
35 #include "fieldmappingimpl.hxx"
42 #define STATE_SELECT_ABTYPE 0
43 #define STATE_INVOKE_ADMIN_DIALOG 1
44 #define STATE_TABLE_SELECTION 2
45 #define STATE_MANUAL_FIELD_MAPPING 3
46 #define STATE_FINAL_CONFIRM 4
48 #define PATH_COMPLETE 1
49 #define PATH_NO_SETTINGS 2
50 #define PATH_NO_FIELDS 3
51 #define PATH_NO_SETTINGS_NO_FIELDS 4
53 #define WINDOW_SIZE_X 240
54 #define WINDOW_SIZE_Y 185
56 using namespace ::svt
;
57 using namespace ::com::sun::star::uno
;
58 using namespace ::com::sun::star::lang
;
60 OAddressBookSourcePilot::OAddressBookSourcePilot(vcl::Window
* _pParent
, const Reference
< XComponentContext
>& _rxORB
)
61 :OAddressBookSourcePilot_Base( _pParent
)
63 ,m_aNewDataSource(_rxORB
)
64 ,m_eNewDataSourceType( AST_INVALID
)
66 SetPageSizePixel(LogicToPixel(Size(WINDOW_SIZE_X
, WINDOW_SIZE_Y
), MapMode(MapUnit::MapAppFont
)));
68 declarePath( PATH_COMPLETE
,
70 STATE_INVOKE_ADMIN_DIALOG
,
71 STATE_TABLE_SELECTION
,
72 STATE_MANUAL_FIELD_MAPPING
,
75 declarePath( PATH_NO_SETTINGS
,
77 STATE_TABLE_SELECTION
,
78 STATE_MANUAL_FIELD_MAPPING
,
81 declarePath( PATH_NO_FIELDS
,
83 STATE_INVOKE_ADMIN_DIALOG
,
84 STATE_TABLE_SELECTION
,
87 declarePath( PATH_NO_SETTINGS_NO_FIELDS
,
89 STATE_TABLE_SELECTION
,
93 m_pPrevPage
->SetHelpId(HID_ABSPILOT_PREVIOUS
);
94 m_pNextPage
->SetHelpId(HID_ABSPILOT_NEXT
);
95 m_pCancel
->SetHelpId(HID_ABSPILOT_CANCEL
);
96 m_pFinish
->SetHelpId(HID_ABSPILOT_FINISH
);
97 m_pHelp
->SetHelpId(UID_ABSPILOT_HELP
);
99 m_pCancel
->SetClickHdl( LINK( this, OAddressBookSourcePilot
, OnCancelClicked
) );
101 // some initial settings
104 m_aSettings
.eType
= AST_MACAB
;
106 // FIXME: if KDE use KAB instead
107 m_aSettings
.eType
= AST_EVOLUTION
;
110 m_aSettings
.eType
= AST_OTHER
;
112 m_aSettings
.sDataSourceName
= compmodule::ModuleRes(RID_STR_DEFAULT_NAME
);
113 m_aSettings
.bRegisterDataSource
= false;
114 m_aSettings
.bEmbedDataSource
= false;
115 m_aSettings
.bIgnoreNoTable
= false;
117 defaultButton(WizardButtonFlags::NEXT
);
118 enableButtons(WizardButtonFlags::FINISH
, false);
121 typeSelectionChanged( m_aSettings
.eType
);
123 OUString sDialogTitle
= compmodule::ModuleRes(RID_STR_ABSOURCEDIALOGTITLE
);
124 setTitleBase(sDialogTitle
);
125 SetHelpId(HID_ABSPILOT
);
128 OUString
OAddressBookSourcePilot::getStateDisplayName( WizardState _nState
) const
130 const char* pResId
= nullptr;
133 case STATE_SELECT_ABTYPE
: pResId
= RID_STR_SELECT_ABTYPE
; break;
134 case STATE_INVOKE_ADMIN_DIALOG
: pResId
= RID_STR_INVOKE_ADMIN_DIALOG
; break;
135 case STATE_TABLE_SELECTION
: pResId
= RID_STR_TABLE_SELECTION
; break;
136 case STATE_MANUAL_FIELD_MAPPING
: pResId
= RID_STR_MANUAL_FIELD_MAPPING
; break;
137 case STATE_FINAL_CONFIRM
: pResId
= RID_STR_FINAL_CONFIRM
; break;
139 DBG_ASSERT( pResId
, "OAddressBookSourcePilot::getStateDisplayName: don't know this state!" );
141 OUString sDisplayName
;
144 sDisplayName
= compmodule::ModuleRes(pResId
);
151 void OAddressBookSourcePilot::implCommitAll()
153 // in real, the data source already exists in the data source context
154 // Thus, if the user changed the name, we have to rename the data source
155 if ( m_aSettings
.sDataSourceName
!= m_aNewDataSource
.getName() )
156 m_aNewDataSource
.rename( m_aSettings
.sDataSourceName
);
158 // 1. the data source
159 m_aNewDataSource
.store(m_aSettings
);
161 // 2. check if we need to register the data source
162 if ( m_aSettings
.bRegisterDataSource
)
163 m_aNewDataSource
.registerDataSource(m_aSettings
.sRegisteredDataSourceName
);
165 // 3. write the data source / table names into the configuration
166 addressconfig::writeTemplateAddressSource( getORB(), m_aSettings
.bRegisterDataSource
? m_aSettings
.sRegisteredDataSourceName
: m_aSettings
.sDataSourceName
, m_aSettings
.sSelectedTable
);
168 // 4. write the field mapping
169 fieldmapping::writeTemplateAddressFieldMapping( getORB(), m_aSettings
.aFieldMapping
);
173 void OAddressBookSourcePilot::implCleanup()
175 if ( m_aNewDataSource
.isValid() )
176 m_aNewDataSource
.remove();
180 IMPL_LINK_NOARG( OAddressBookSourcePilot
, OnCancelClicked
, Button
*, void )
185 // reset the click hdl
186 m_pCancel
->SetClickHdl( Link
<Button
*, void>() );
187 // simulate the click again - this time, the default handling of the button will strike ....
192 bool OAddressBookSourcePilot::Close()
196 return OAddressBookSourcePilot_Base::Close();
200 bool OAddressBookSourcePilot::onFinish()
202 if ( !OAddressBookSourcePilot_Base::onFinish() )
207 addressconfig::markPilotSuccess( getORB() );
213 void OAddressBookSourcePilot::enterState( WizardState _nState
)
217 case STATE_SELECT_ABTYPE
:
218 impl_updateRoadmap( static_cast< TypeSelectionPage
* >( GetPage( STATE_SELECT_ABTYPE
) )->getSelectedType() );
221 case STATE_FINAL_CONFIRM
:
222 if ( !needManualFieldMapping( ) )
223 implDoAutoFieldMapping();
226 case STATE_TABLE_SELECTION
:
227 implDefaultTableName();
231 OAddressBookSourcePilot_Base::enterState(_nState
);
235 bool OAddressBookSourcePilot::prepareLeaveCurrentState( CommitPageReason _eReason
)
237 if ( !OAddressBookSourcePilot_Base::prepareLeaveCurrentState( _eReason
) )
240 if ( _eReason
== eTravelBackward
)
245 switch ( getCurrentState() )
247 case STATE_SELECT_ABTYPE
:
248 implCreateDataSource();
249 if ( needAdminInvokationPage() )
253 case STATE_INVOKE_ADMIN_DIALOG
:
254 if ( !connectToDataSource( false ) )
256 // connecting did not succeed -> do not allow proceeding
262 // now that we connected to the data source, check whether we need the "table selection" page
263 const StringBag
& aTables
= m_aNewDataSource
.getTableNames();
265 if ( aTables
.empty() )
267 std::unique_ptr
<weld::MessageDialog
> xBox(Application::CreateMessageDialog(GetFrameWeld(),
268 VclMessageType::Question
, VclButtonsType::YesNo
,
269 compmodule::ModuleRes(getSettings().eType
== AST_EVOLUTION_GROUPWISE
? RID_STR_QRY_NO_EVO_GW
: RID_STR_QRY_NOTABLES
)));
271 if (RET_YES
!= xBox
->run())
273 // cannot ask the user, or the user chose to use this data source, though there are no tables
278 m_aSettings
.bIgnoreNoTable
= true;
281 if ( aTables
.size() == 1 )
282 // remember the one and only table we have
283 m_aSettings
.sSelectedTable
= *aTables
.begin();
288 impl_updateRoadmap( m_aSettings
.eType
);
293 void OAddressBookSourcePilot::implDefaultTableName()
295 const StringBag
& rTableNames
= getDataSource().getTableNames();
296 if ( rTableNames
.end() != rTableNames
.find( getSettings().sSelectedTable
) )
297 // already a valid table selected
300 const sal_Char
* pGuess
= nullptr;
301 switch ( getSettings().eType
)
304 case AST_THUNDERBIRD
: pGuess
= "Personal Address book"; break;
306 case AST_EVOLUTION_GROUPWISE
:
307 case AST_EVOLUTION_LDAP
: pGuess
= "Personal"; break;
309 OSL_FAIL( "OAddressBookSourcePilot::implDefaultTableName: unhandled case!" );
312 const OUString sGuess
= OUString::createFromAscii( pGuess
);
313 if ( rTableNames
.end() != rTableNames
.find( sGuess
) )
314 getSettings().sSelectedTable
= sGuess
;
318 void OAddressBookSourcePilot::implDoAutoFieldMapping()
320 DBG_ASSERT( !needManualFieldMapping( ), "OAddressBookSourcePilot::implDoAutoFieldMapping: invalid call!" );
322 fieldmapping::defaultMapping( getORB(), m_aSettings
.aFieldMapping
);
326 void OAddressBookSourcePilot::implCreateDataSource()
328 if (m_aNewDataSource
.isValid())
329 { // we already have a data source object
330 if ( m_aSettings
.eType
== m_eNewDataSourceType
)
331 // and it already has the correct type
334 // it has a wrong type -> remove it
335 m_aNewDataSource
.remove();
338 ODataSourceContext
aContext( getORB() );
339 aContext
.disambiguate( m_aSettings
.sDataSourceName
);
341 switch (m_aSettings
.eType
)
344 m_aNewDataSource
= aContext
.createNewMORK( m_aSettings
.sDataSourceName
);
347 case AST_THUNDERBIRD
:
348 m_aNewDataSource
= aContext
.createNewThunderbird( m_aSettings
.sDataSourceName
);
352 m_aNewDataSource
= aContext
.createNewEvolution( m_aSettings
.sDataSourceName
);
355 case AST_EVOLUTION_GROUPWISE
:
356 m_aNewDataSource
= aContext
.createNewEvolutionGroupwise( m_aSettings
.sDataSourceName
);
359 case AST_EVOLUTION_LDAP
:
360 m_aNewDataSource
= aContext
.createNewEvolutionLdap( m_aSettings
.sDataSourceName
);
364 m_aNewDataSource
= aContext
.createNewKab( m_aSettings
.sDataSourceName
);
368 m_aNewDataSource
= aContext
.createNewMacab( m_aSettings
.sDataSourceName
);
372 m_aNewDataSource
= aContext
.createNewDBase( m_aSettings
.sDataSourceName
);
376 OSL_FAIL( "OAddressBookSourcePilot::implCreateDataSource: illegal data source type!" );
379 m_eNewDataSourceType
= m_aSettings
.eType
;
383 bool OAddressBookSourcePilot::connectToDataSource( bool _bForceReConnect
)
385 DBG_ASSERT( m_aNewDataSource
.isValid(), "OAddressBookSourcePilot::implConnect: invalid current data source!" );
387 WaitObject
aWaitCursor( this );
388 if ( _bForceReConnect
&& m_aNewDataSource
.isConnected( ) )
389 m_aNewDataSource
.disconnect( );
391 return m_aNewDataSource
.connect(GetFrameWeld());
395 VclPtr
<TabPage
> OAddressBookSourcePilot::createPage(WizardState _nState
)
399 case STATE_SELECT_ABTYPE
:
400 return VclPtr
<TypeSelectionPage
>::Create( this );
402 case STATE_INVOKE_ADMIN_DIALOG
:
403 return VclPtr
<AdminDialogInvokationPage
>::Create( this );
405 case STATE_TABLE_SELECTION
:
406 return VclPtr
<TableSelectionPage
>::Create( this );
408 case STATE_MANUAL_FIELD_MAPPING
:
409 return VclPtr
<FieldMappingPage
>::Create( this );
411 case STATE_FINAL_CONFIRM
:
412 return VclPtr
<FinalPage
>::Create( this );
415 OSL_FAIL("OAddressBookSourcePilot::createPage: invalid state!");
421 void OAddressBookSourcePilot::impl_updateRoadmap( AddressSourceType _eType
)
423 bool bSettingsPage
= needAdminInvokationPage( _eType
);
424 bool bTablesPage
= needTableSelection( _eType
);
425 bool bFieldsPage
= needManualFieldMapping( _eType
);
427 bool bConnected
= m_aNewDataSource
.isConnected();
428 bool bCanSkipTables
=
429 ( m_aNewDataSource
.hasTable( m_aSettings
.sSelectedTable
)
430 || m_aSettings
.bIgnoreNoTable
433 enableState( STATE_INVOKE_ADMIN_DIALOG
, bSettingsPage
);
435 enableState( STATE_TABLE_SELECTION
,
436 bTablesPage
&& ( bConnected
? !bCanSkipTables
: !bSettingsPage
)
437 // if we do not need a settings page, we connect upon "Next" on the first page
440 enableState( STATE_MANUAL_FIELD_MAPPING
,
441 bFieldsPage
&& bConnected
&& m_aNewDataSource
.hasTable( m_aSettings
.sSelectedTable
)
444 enableState( STATE_FINAL_CONFIRM
,
445 bConnected
&& bCanSkipTables
450 void OAddressBookSourcePilot::typeSelectionChanged( AddressSourceType _eType
)
452 PathId
nCurrentPathID( PATH_COMPLETE
);
453 bool bSettingsPage
= needAdminInvokationPage( _eType
);
454 bool bFieldsPage
= needManualFieldMapping( _eType
);
455 if ( !bSettingsPage
)
457 nCurrentPathID
= PATH_NO_SETTINGS_NO_FIELDS
;
459 nCurrentPathID
= PATH_NO_SETTINGS
;
462 nCurrentPathID
= PATH_NO_FIELDS
;
464 nCurrentPathID
= PATH_COMPLETE
;
465 activatePath( nCurrentPathID
, true );
467 m_aNewDataSource
.disconnect();
468 m_aSettings
.bIgnoreNoTable
= false;
469 impl_updateRoadmap( _eType
);
476 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */