bump product version to 6.3.0.0.beta1
[LibreOffice.git] / extensions / source / abpilot / abspilot.cxx
blobfaf99e5184da753aeea268f7db69b193dd921e42
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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"
21 #include <helpids.h>
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"
38 namespace abp
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 )
62 ,m_xORB(_rxORB)
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,
69 {STATE_SELECT_ABTYPE,
70 STATE_INVOKE_ADMIN_DIALOG,
71 STATE_TABLE_SELECTION,
72 STATE_MANUAL_FIELD_MAPPING,
73 STATE_FINAL_CONFIRM}
75 declarePath( PATH_NO_SETTINGS,
76 {STATE_SELECT_ABTYPE,
77 STATE_TABLE_SELECTION,
78 STATE_MANUAL_FIELD_MAPPING,
79 STATE_FINAL_CONFIRM}
81 declarePath( PATH_NO_FIELDS,
82 {STATE_SELECT_ABTYPE,
83 STATE_INVOKE_ADMIN_DIALOG,
84 STATE_TABLE_SELECTION,
85 STATE_FINAL_CONFIRM}
87 declarePath( PATH_NO_SETTINGS_NO_FIELDS,
88 {STATE_SELECT_ABTYPE,
89 STATE_TABLE_SELECTION,
90 STATE_FINAL_CONFIRM}
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
102 #ifdef UNX
103 #ifdef MACOSX
104 m_aSettings.eType = AST_MACAB;
105 #else
106 // FIXME: if KDE use KAB instead
107 m_aSettings.eType = AST_EVOLUTION;
108 #endif
109 #else
110 m_aSettings.eType = AST_OTHER;
111 #endif
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);
119 ActivatePage();
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;
131 switch ( _nState )
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;
142 if (pResId)
144 sDisplayName = compmodule::ModuleRes(pResId);
147 return sDisplayName;
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 )
182 // do cleanups
183 implCleanup();
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 ....
188 m_pCancel->Click();
192 bool OAddressBookSourcePilot::Close()
194 implCleanup();
196 return OAddressBookSourcePilot_Base::Close();
200 bool OAddressBookSourcePilot::onFinish()
202 if ( !OAddressBookSourcePilot_Base::onFinish() )
203 return false;
205 implCommitAll();
207 addressconfig::markPilotSuccess( getORB() );
209 return true;
213 void OAddressBookSourcePilot::enterState( WizardState _nState )
215 switch ( _nState )
217 case STATE_SELECT_ABTYPE:
218 impl_updateRoadmap( static_cast< TypeSelectionPage* >( GetPage( STATE_SELECT_ABTYPE ) )->getSelectedType() );
219 break;
221 case STATE_FINAL_CONFIRM:
222 if ( !needManualFieldMapping( ) )
223 implDoAutoFieldMapping();
224 break;
226 case STATE_TABLE_SELECTION:
227 implDefaultTableName();
228 break;
231 OAddressBookSourcePilot_Base::enterState(_nState);
235 bool OAddressBookSourcePilot::prepareLeaveCurrentState( CommitPageReason _eReason )
237 if ( !OAddressBookSourcePilot_Base::prepareLeaveCurrentState( _eReason ) )
238 return false;
240 if ( _eReason == eTravelBackward )
241 return true;
243 bool bAllow = true;
245 switch ( getCurrentState() )
247 case STATE_SELECT_ABTYPE:
248 implCreateDataSource();
249 if ( needAdminInvokationPage() )
250 break;
251 [[fallthrough]];
253 case STATE_INVOKE_ADMIN_DIALOG:
254 if ( !connectToDataSource( false ) )
256 // connecting did not succeed -> do not allow proceeding
257 bAllow = false;
258 break;
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
274 bAllow = false;
275 break;
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();
285 break;
288 impl_updateRoadmap( m_aSettings.eType );
289 return bAllow;
293 void OAddressBookSourcePilot::implDefaultTableName()
295 const StringBag& rTableNames = getDataSource().getTableNames();
296 if ( rTableNames.end() != rTableNames.find( getSettings().sSelectedTable ) )
297 // already a valid table selected
298 return;
300 const sal_Char* pGuess = nullptr;
301 switch ( getSettings().eType )
303 case AST_MORK :
304 case AST_THUNDERBIRD : pGuess = "Personal Address book"; break;
305 case AST_EVOLUTION :
306 case AST_EVOLUTION_GROUPWISE:
307 case AST_EVOLUTION_LDAP : pGuess = "Personal"; break;
308 default:
309 OSL_FAIL( "OAddressBookSourcePilot::implDefaultTableName: unhandled case!" );
310 return;
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
332 return;
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)
343 case AST_MORK:
344 m_aNewDataSource = aContext.createNewMORK( m_aSettings.sDataSourceName );
345 break;
347 case AST_THUNDERBIRD:
348 m_aNewDataSource = aContext.createNewThunderbird( m_aSettings.sDataSourceName );
349 break;
351 case AST_EVOLUTION:
352 m_aNewDataSource = aContext.createNewEvolution( m_aSettings.sDataSourceName );
353 break;
355 case AST_EVOLUTION_GROUPWISE:
356 m_aNewDataSource = aContext.createNewEvolutionGroupwise( m_aSettings.sDataSourceName );
357 break;
359 case AST_EVOLUTION_LDAP:
360 m_aNewDataSource = aContext.createNewEvolutionLdap( m_aSettings.sDataSourceName );
361 break;
363 case AST_KAB:
364 m_aNewDataSource = aContext.createNewKab( m_aSettings.sDataSourceName );
365 break;
367 case AST_MACAB:
368 m_aNewDataSource = aContext.createNewMacab( m_aSettings.sDataSourceName );
369 break;
371 case AST_OTHER:
372 m_aNewDataSource = aContext.createNewDBase( m_aSettings.sDataSourceName );
373 break;
375 case AST_INVALID:
376 OSL_FAIL( "OAddressBookSourcePilot::implCreateDataSource: illegal data source type!" );
377 break;
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)
397 switch (_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 );
414 default:
415 OSL_FAIL("OAddressBookSourcePilot::createPage: invalid state!");
416 return nullptr;
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 )
456 if ( !bFieldsPage )
457 nCurrentPathID = PATH_NO_SETTINGS_NO_FIELDS;
458 else
459 nCurrentPathID = PATH_NO_SETTINGS;
460 else
461 if ( !bFieldsPage )
462 nCurrentPathID = PATH_NO_FIELDS;
463 else
464 nCurrentPathID = PATH_COMPLETE;
465 activatePath( nCurrentPathID, true );
467 m_aNewDataSource.disconnect();
468 m_aSettings.bIgnoreNoTable = false;
469 impl_updateRoadmap( _eType );
473 } // namespace abp
476 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */