Avoid potential negative array index access to cached text.
[LibreOffice.git] / extensions / source / abpilot / abspilot.cxx
blob1b04a34ceb9bf20aab80d66b128d4baf4c511846
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/svapp.hxx>
29 #include <vcl/weld.hxx>
30 #include <osl/diagnose.h>
31 #include "abpfinalpage.hxx"
32 #include "fieldmappingpage.hxx"
33 #include "fieldmappingimpl.hxx"
35 using vcl::RoadmapWizardTypes::PathId;
37 namespace abp
41 #define STATE_SELECT_ABTYPE 0
42 #define STATE_INVOKE_ADMIN_DIALOG 1
43 #define STATE_TABLE_SELECTION 2
44 #define STATE_MANUAL_FIELD_MAPPING 3
45 #define STATE_FINAL_CONFIRM 4
47 #define PATH_COMPLETE 1
48 #define PATH_NO_SETTINGS 2
49 #define PATH_NO_FIELDS 3
50 #define PATH_NO_SETTINGS_NO_FIELDS 4
52 using namespace ::com::sun::star::uno;
53 using namespace ::com::sun::star::lang;
55 OAddressBookSourcePilot::OAddressBookSourcePilot(weld::Window* _pParent, const Reference< XComponentContext >& _rxORB)
56 :OAddressBookSourcePilot_Base( _pParent )
57 ,m_xORB(_rxORB)
58 ,m_aNewDataSource(_rxORB)
59 ,m_eNewDataSourceType( AST_INVALID )
61 declarePath( PATH_COMPLETE,
62 {STATE_SELECT_ABTYPE,
63 STATE_INVOKE_ADMIN_DIALOG,
64 STATE_TABLE_SELECTION,
65 STATE_MANUAL_FIELD_MAPPING,
66 STATE_FINAL_CONFIRM}
68 declarePath( PATH_NO_SETTINGS,
69 {STATE_SELECT_ABTYPE,
70 STATE_TABLE_SELECTION,
71 STATE_MANUAL_FIELD_MAPPING,
72 STATE_FINAL_CONFIRM}
74 declarePath( PATH_NO_FIELDS,
75 {STATE_SELECT_ABTYPE,
76 STATE_INVOKE_ADMIN_DIALOG,
77 STATE_TABLE_SELECTION,
78 STATE_FINAL_CONFIRM}
80 declarePath( PATH_NO_SETTINGS_NO_FIELDS,
81 {STATE_SELECT_ABTYPE,
82 STATE_TABLE_SELECTION,
83 STATE_FINAL_CONFIRM}
86 m_xPrevPage->set_help_id(HID_ABSPILOT_PREVIOUS);
87 m_xNextPage->set_help_id(HID_ABSPILOT_NEXT);
88 m_xCancel->set_help_id(HID_ABSPILOT_CANCEL);
89 m_xFinish->set_help_id(HID_ABSPILOT_FINISH);
90 m_xHelp->set_help_id(UID_ABSPILOT_HELP);
92 // some initial settings
93 #ifdef UNX
94 #ifdef MACOSX
95 m_aSettings.eType = AST_MACAB;
96 #else
97 // FIXME: if KDE use KAB instead
98 m_aSettings.eType = AST_EVOLUTION;
99 #endif
100 #else
101 m_aSettings.eType = AST_OTHER;
102 #endif
103 m_aSettings.sDataSourceName = compmodule::ModuleRes(RID_STR_DEFAULT_NAME);
104 m_aSettings.bRegisterDataSource = false;
105 m_aSettings.bEmbedDataSource = false;
106 m_aSettings.bIgnoreNoTable = false;
108 defaultButton(WizardButtonFlags::NEXT);
109 enableButtons(WizardButtonFlags::FINISH, false);
110 ActivatePage();
111 m_xAssistant->set_current_page(0);
113 typeSelectionChanged( m_aSettings.eType );
115 OUString sDialogTitle = compmodule::ModuleRes(RID_STR_ABSOURCEDIALOGTITLE);
116 setTitleBase(sDialogTitle);
117 m_xAssistant->set_help_id(HID_ABSPILOT);
120 OUString OAddressBookSourcePilot::getStateDisplayName( WizardState _nState ) const
122 TranslateId pResId;
123 switch ( _nState )
125 case STATE_SELECT_ABTYPE: pResId = RID_STR_SELECT_ABTYPE; break;
126 case STATE_INVOKE_ADMIN_DIALOG: pResId = RID_STR_INVOKE_ADMIN_DIALOG; break;
127 case STATE_TABLE_SELECTION: pResId = RID_STR_TABLE_SELECTION; break;
128 case STATE_MANUAL_FIELD_MAPPING: pResId = RID_STR_MANUAL_FIELD_MAPPING; break;
129 case STATE_FINAL_CONFIRM: pResId = RID_STR_FINAL_CONFIRM; break;
131 DBG_ASSERT( pResId, "OAddressBookSourcePilot::getStateDisplayName: don't know this state!" );
133 OUString sDisplayName;
134 if (pResId)
136 sDisplayName = compmodule::ModuleRes(pResId);
139 return sDisplayName;
142 void OAddressBookSourcePilot::implCommitAll()
144 // in real, the data source already exists in the data source context
145 // Thus, if the user changed the name, we have to rename the data source
146 if ( m_aSettings.sDataSourceName != m_aNewDataSource.getName() )
147 m_aNewDataSource.rename( m_aSettings.sDataSourceName );
149 // 1. the data source
150 m_aNewDataSource.store(m_aSettings);
152 // 2. check if we need to register the data source
153 if ( m_aSettings.bRegisterDataSource )
154 m_aNewDataSource.registerDataSource(m_aSettings.sRegisteredDataSourceName);
156 // 3. write the data source / table names into the configuration
157 addressconfig::writeTemplateAddressSource( getORB(), m_aSettings.bRegisterDataSource ? m_aSettings.sRegisteredDataSourceName : m_aSettings.sDataSourceName, m_aSettings.sSelectedTable );
159 // 4. write the field mapping
160 fieldmapping::writeTemplateAddressFieldMapping( getORB(), std::map(m_aSettings.aFieldMapping) );
163 void OAddressBookSourcePilot::implCleanup()
165 if ( m_aNewDataSource.isValid() )
166 m_aNewDataSource.remove();
169 short OAddressBookSourcePilot::run()
171 short nRet = OAddressBookSourcePilot_Base::run();
173 implCleanup();
175 return nRet;
178 bool OAddressBookSourcePilot::onFinish()
180 if ( !OAddressBookSourcePilot_Base::onFinish() )
181 return false;
183 implCommitAll();
185 addressconfig::markPilotSuccess( getORB() );
187 return true;
190 void OAddressBookSourcePilot::enterState( WizardState _nState )
192 switch ( _nState )
194 case STATE_SELECT_ABTYPE:
195 impl_updateRoadmap( static_cast< TypeSelectionPage* >( GetPage( STATE_SELECT_ABTYPE ) )->getSelectedType() );
196 break;
198 case STATE_FINAL_CONFIRM:
199 if ( !needManualFieldMapping( ) )
200 implDoAutoFieldMapping();
201 break;
203 case STATE_TABLE_SELECTION:
204 implDefaultTableName();
205 break;
208 OAddressBookSourcePilot_Base::enterState(_nState);
212 bool OAddressBookSourcePilot::prepareLeaveCurrentState( CommitPageReason _eReason )
214 if ( !OAddressBookSourcePilot_Base::prepareLeaveCurrentState( _eReason ) )
215 return false;
217 if ( _eReason == vcl::WizardTypes::eTravelBackward )
218 return true;
220 bool bAllow = true;
222 switch ( getCurrentState() )
224 case STATE_SELECT_ABTYPE:
225 implCreateDataSource();
226 if ( needAdminInvokationPage() )
227 break;
228 [[fallthrough]];
230 case STATE_INVOKE_ADMIN_DIALOG:
231 if ( !connectToDataSource( false ) )
233 // connecting did not succeed -> do not allow proceeding
234 bAllow = false;
235 break;
239 // now that we connected to the data source, check whether we need the "table selection" page
240 const StringBag& aTables = m_aNewDataSource.getTableNames();
242 if ( aTables.empty() )
244 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xAssistant.get(),
245 VclMessageType::Question, VclButtonsType::YesNo,
246 compmodule::ModuleRes(getSettings().eType == AST_EVOLUTION_GROUPWISE ? RID_STR_QRY_NO_EVO_GW : RID_STR_QRY_NOTABLES)));
248 if (RET_YES != xBox->run())
250 // cannot ask the user, or the user chose to use this data source, though there are no tables
251 bAllow = false;
252 break;
255 m_aSettings.bIgnoreNoTable = true;
258 if ( aTables.size() == 1 )
259 // remember the one and only table we have
260 m_aSettings.sSelectedTable = *aTables.begin();
262 break;
265 impl_updateRoadmap( m_aSettings.eType );
266 return bAllow;
269 void OAddressBookSourcePilot::implDefaultTableName()
271 const StringBag& rTableNames = getDataSource().getTableNames();
272 if ( rTableNames.end() != rTableNames.find( getSettings().sSelectedTable ) )
273 // already a valid table selected
274 return;
276 const char* pGuess = nullptr;
277 switch ( getSettings().eType )
279 case AST_THUNDERBIRD : pGuess = "Personal Address book"; break;
280 case AST_EVOLUTION :
281 case AST_EVOLUTION_GROUPWISE:
282 case AST_EVOLUTION_LDAP : pGuess = "Personal"; break;
283 default:
284 OSL_FAIL( "OAddressBookSourcePilot::implDefaultTableName: unhandled case!" );
285 return;
287 const OUString sGuess = OUString::createFromAscii( pGuess );
288 if ( rTableNames.end() != rTableNames.find( sGuess ) )
289 getSettings().sSelectedTable = sGuess;
292 void OAddressBookSourcePilot::implDoAutoFieldMapping()
294 DBG_ASSERT( !needManualFieldMapping( ), "OAddressBookSourcePilot::implDoAutoFieldMapping: invalid call!" );
296 fieldmapping::defaultMapping( getORB(), m_aSettings.aFieldMapping );
299 void OAddressBookSourcePilot::implCreateDataSource()
301 if (m_aNewDataSource.isValid())
302 { // we already have a data source object
303 if ( m_aSettings.eType == m_eNewDataSourceType )
304 // and it already has the correct type
305 return;
307 // it has a wrong type -> remove it
308 m_aNewDataSource.remove();
311 ODataSourceContext aContext( getORB() );
312 aContext.disambiguate( m_aSettings.sDataSourceName );
314 switch (m_aSettings.eType)
316 case AST_THUNDERBIRD:
317 m_aNewDataSource = aContext.createNewThunderbird( m_aSettings.sDataSourceName );
318 break;
320 case AST_EVOLUTION:
321 m_aNewDataSource = aContext.createNewEvolution( m_aSettings.sDataSourceName );
322 break;
324 case AST_EVOLUTION_GROUPWISE:
325 m_aNewDataSource = aContext.createNewEvolutionGroupwise( m_aSettings.sDataSourceName );
326 break;
328 case AST_EVOLUTION_LDAP:
329 m_aNewDataSource = aContext.createNewEvolutionLdap( m_aSettings.sDataSourceName );
330 break;
332 case AST_KAB:
333 m_aNewDataSource = aContext.createNewKab( m_aSettings.sDataSourceName );
334 break;
336 case AST_MACAB:
337 m_aNewDataSource = aContext.createNewMacab( m_aSettings.sDataSourceName );
338 break;
340 case AST_OTHER:
341 m_aNewDataSource = aContext.createNewOther( m_aSettings.sDataSourceName );
342 break;
344 case AST_INVALID:
345 OSL_FAIL( "OAddressBookSourcePilot::implCreateDataSource: illegal data source type!" );
346 break;
348 m_eNewDataSourceType = m_aSettings.eType;
351 bool OAddressBookSourcePilot::connectToDataSource( bool _bForceReConnect )
353 DBG_ASSERT( m_aNewDataSource.isValid(), "OAddressBookSourcePilot::implConnect: invalid current data source!" );
355 weld::WaitObject aWaitCursor(m_xAssistant.get());
356 if ( _bForceReConnect && m_aNewDataSource.isConnected( ) )
357 m_aNewDataSource.disconnect( );
359 return m_aNewDataSource.connect(m_xAssistant.get());
362 std::unique_ptr<BuilderPage> OAddressBookSourcePilot::createPage(WizardState _nState)
364 OUString sIdent(OUString::number(_nState));
365 weld::Container* pPageContainer = m_xAssistant->append_page(sIdent);
367 std::unique_ptr<vcl::OWizardPage> xRet;
369 switch (_nState)
371 case STATE_SELECT_ABTYPE:
372 xRet = std::make_unique<TypeSelectionPage>(pPageContainer, this);
373 break;
374 case STATE_INVOKE_ADMIN_DIALOG:
375 xRet = std::make_unique<AdminDialogInvokationPage>(pPageContainer, this);
376 break;
377 case STATE_TABLE_SELECTION:
378 xRet = std::make_unique<TableSelectionPage>(pPageContainer, this);
379 break;
380 case STATE_MANUAL_FIELD_MAPPING:
381 xRet = std::make_unique<FieldMappingPage>(pPageContainer, this);
382 break;
383 case STATE_FINAL_CONFIRM:
384 xRet = std::make_unique<FinalPage>(pPageContainer, this);
385 break;
386 default:
387 assert(false && "OAddressBookSourcePilot::createPage: invalid state!");
388 break;
391 m_xAssistant->set_page_title(sIdent, getStateDisplayName(_nState));
393 return xRet;
396 void OAddressBookSourcePilot::impl_updateRoadmap( AddressSourceType _eType )
398 bool bSettingsPage = needAdminInvokationPage( _eType );
399 bool bTablesPage = needTableSelection( _eType );
400 bool bFieldsPage = needManualFieldMapping( _eType );
402 bool bConnected = m_aNewDataSource.isConnected();
403 bool bCanSkipTables =
404 ( m_aNewDataSource.hasTable( m_aSettings.sSelectedTable )
405 || m_aSettings.bIgnoreNoTable
408 enableState( STATE_INVOKE_ADMIN_DIALOG, bSettingsPage );
410 enableState( STATE_TABLE_SELECTION,
411 bTablesPage && ( bConnected ? !bCanSkipTables : !bSettingsPage )
412 // if we do not need a settings page, we connect upon "Next" on the first page
415 enableState( STATE_MANUAL_FIELD_MAPPING,
416 bFieldsPage && bConnected && m_aNewDataSource.hasTable( m_aSettings.sSelectedTable )
419 enableState( STATE_FINAL_CONFIRM,
420 bConnected && bCanSkipTables
424 void OAddressBookSourcePilot::typeSelectionChanged( AddressSourceType _eType )
426 PathId nCurrentPathID( PATH_COMPLETE );
427 bool bSettingsPage = needAdminInvokationPage( _eType );
428 bool bFieldsPage = needManualFieldMapping( _eType );
429 if ( !bSettingsPage )
430 if ( !bFieldsPage )
431 nCurrentPathID = PATH_NO_SETTINGS_NO_FIELDS;
432 else
433 nCurrentPathID = PATH_NO_SETTINGS;
434 else
435 if ( !bFieldsPage )
436 nCurrentPathID = PATH_NO_FIELDS;
437 else
438 nCurrentPathID = PATH_COMPLETE;
439 activatePath( nCurrentPathID, true );
441 m_aNewDataSource.disconnect();
442 m_aSettings.bIgnoreNoTable = false;
443 impl_updateRoadmap( _eType );
446 } // namespace abp
449 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */