bump product version to 5.0.4.1
[LibreOffice.git] / extensions / source / abpilot / abspilot.cxx
bloba27b1e66040ddce9c419cd3793a8238f6d508765
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 "abpilot.hrc"
22 #include "abpresid.hrc"
23 #include "componentmodule.hxx"
24 #include <comphelper/processfactory.hxx>
25 #include <tools/debug.hxx>
26 #include <svtools/localresaccess.hxx>
27 #include "typeselectionpage.hxx"
28 #include "admininvokationpage.hxx"
29 #include "tableselectionpage.hxx"
30 #include <vcl/waitobj.hxx>
31 #include <vcl/layout.hxx>
32 #include <vcl/settings.hxx>
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 using namespace ::svt;
54 using namespace ::com::sun::star::uno;
55 using namespace ::com::sun::star::lang;
57 OAddessBookSourcePilot::OAddessBookSourcePilot(vcl::Window* _pParent, const Reference< XComponentContext >& _rxORB)
58 :OAddessBookSourcePilot_Base( _pParent,
59 WizardButtonFlags::HELP | WizardButtonFlags::FINISH | WizardButtonFlags::CANCEL | WizardButtonFlags::NEXT | WizardButtonFlags::PREVIOUS )
60 ,m_xORB(_rxORB)
61 ,m_aNewDataSource(_rxORB)
62 ,m_eNewDataSourceType( AST_INVALID )
64 SetPageSizePixel(LogicToPixel(Size(WINDOW_SIZE_X, WINDOW_SIZE_Y), MAP_APPFONT));
66 declarePath( PATH_COMPLETE,
67 STATE_SELECT_ABTYPE,
68 STATE_INVOKE_ADMIN_DIALOG,
69 STATE_TABLE_SELECTION,
70 STATE_MANUAL_FIELD_MAPPING,
71 STATE_FINAL_CONFIRM,
72 WZS_INVALID_STATE
74 declarePath( PATH_NO_SETTINGS,
75 STATE_SELECT_ABTYPE,
76 STATE_TABLE_SELECTION,
77 STATE_MANUAL_FIELD_MAPPING,
78 STATE_FINAL_CONFIRM,
79 WZS_INVALID_STATE
81 declarePath( PATH_NO_FIELDS,
82 STATE_SELECT_ABTYPE,
83 STATE_INVOKE_ADMIN_DIALOG,
84 STATE_TABLE_SELECTION,
85 STATE_FINAL_CONFIRM,
86 WZS_INVALID_STATE
88 declarePath( PATH_NO_SETTINGS_NO_FIELDS,
89 STATE_SELECT_ABTYPE,
90 STATE_TABLE_SELECTION,
91 STATE_FINAL_CONFIRM,
92 WZS_INVALID_STATE
95 m_pPrevPage->SetHelpId(HID_ABSPILOT_PREVIOUS);
96 m_pNextPage->SetHelpId(HID_ABSPILOT_NEXT);
97 m_pCancel->SetHelpId(HID_ABSPILOT_CANCEL);
98 m_pFinish->SetHelpId(HID_ABSPILOT_FINISH);
99 m_pHelp->SetUniqueId(UID_ABSPILOT_HELP);
101 m_pCancel->SetClickHdl( LINK( this, OAddessBookSourcePilot, OnCancelClicked) );
103 // some initial settings
104 #ifdef UNX
105 #ifdef MACOSX
106 m_aSettings.eType = AST_MACAB;
107 #else
108 // FIXME: if KDE use KAB instead
109 m_aSettings.eType = AST_EVOLUTION;
110 #endif
111 #else
112 m_aSettings.eType = AST_OTHER;
113 #endif
114 m_aSettings.sDataSourceName = ModuleRes(RID_STR_DEFAULT_NAME).toString();
115 m_aSettings.bRegisterDataSource = false;
116 m_aSettings.bIgnoreNoTable = false;
118 defaultButton(WizardButtonFlags::NEXT);
119 enableButtons(WizardButtonFlags::FINISH, false);
120 ActivatePage();
122 typeSelectionChanged( m_aSettings.eType );
124 OUString sDialogTitle = ModuleRes(RID_STR_ABSOURCEDIALOGTITLE).toString();
125 setTitleBase(sDialogTitle);
126 SetHelpId(HID_ABSPILOT);
129 OUString OAddessBookSourcePilot::getStateDisplayName( WizardState _nState ) const
131 sal_uInt16 nResId = 0;
132 switch ( _nState )
134 case STATE_SELECT_ABTYPE: nResId = RID_STR_SELECT_ABTYPE; break;
135 case STATE_INVOKE_ADMIN_DIALOG: nResId = RID_STR_INVOKE_ADMIN_DIALOG; break;
136 case STATE_TABLE_SELECTION: nResId = RID_STR_TABLE_SELECTION; break;
137 case STATE_MANUAL_FIELD_MAPPING: nResId = RID_STR_MANUAL_FIELD_MAPPING; break;
138 case STATE_FINAL_CONFIRM: nResId = RID_STR_FINAL_CONFIRM; break;
140 DBG_ASSERT( nResId, "OAddessBookSourcePilot::getStateDisplayName: don't know this state!" );
142 OUString sDisplayName;
143 if ( nResId )
145 sDisplayName = ModuleRes(nResId).toString();
148 return sDisplayName;
152 void OAddessBookSourcePilot::implCommitAll()
154 // in real, the data source already exists in the data source context
155 // Thus, if the user changed the name, we have to rename the data source
156 if ( m_aSettings.sDataSourceName != m_aNewDataSource.getName() )
157 m_aNewDataSource.rename( m_aSettings.sDataSourceName );
159 // 1. the data source
160 m_aNewDataSource.store();
162 // 2. check if we need to register the data source
163 if ( m_aSettings.bRegisterDataSource )
164 m_aNewDataSource.registerDataSource(m_aSettings.sRegisteredDataSourceName);
166 // 3. write the data source / table names into the configuration
167 addressconfig::writeTemplateAddressSource( getORB(), m_aSettings.bRegisterDataSource ? m_aSettings.sRegisteredDataSourceName : m_aSettings.sDataSourceName, m_aSettings.sSelectedTable );
169 // 4. write the field mapping
170 fieldmapping::writeTemplateAddressFieldMapping( getORB(), m_aSettings.aFieldMapping );
174 void OAddessBookSourcePilot::implCleanup()
176 if ( m_aNewDataSource.isValid() )
177 m_aNewDataSource.remove();
181 IMPL_LINK_NOARG( OAddessBookSourcePilot, OnCancelClicked )
183 // do cleanups
184 implCleanup();
186 // reset the click hdl
187 m_pCancel->SetClickHdl( Link<>() );
188 // simulate the click again - this time, the default handling of the button will strike ....
189 m_pCancel->Click();
191 return 0L;
195 bool OAddessBookSourcePilot::Close()
197 implCleanup();
199 return OAddessBookSourcePilot_Base::Close();
203 bool OAddessBookSourcePilot::onFinish()
205 if ( !OAddessBookSourcePilot_Base::onFinish() )
206 return false;
208 implCommitAll();
210 addressconfig::markPilotSuccess( getORB() );
212 return true;
216 void OAddessBookSourcePilot::enterState( WizardState _nState )
218 switch ( _nState )
220 case STATE_SELECT_ABTYPE:
221 impl_updateRoadmap( static_cast< TypeSelectionPage* >( GetPage( STATE_SELECT_ABTYPE ) )->getSelectedType() );
222 break;
224 case STATE_FINAL_CONFIRM:
225 if ( !needManualFieldMapping( ) )
226 implDoAutoFieldMapping();
227 break;
229 case STATE_TABLE_SELECTION:
230 implDefaultTableName();
231 break;
234 OAddessBookSourcePilot_Base::enterState(_nState);
238 bool OAddessBookSourcePilot::prepareLeaveCurrentState( CommitPageReason _eReason )
240 if ( !OAddessBookSourcePilot_Base::prepareLeaveCurrentState( _eReason ) )
241 return false;
243 if ( _eReason == eTravelBackward )
244 return true;
246 bool bAllow = true;
248 switch ( getCurrentState() )
250 case STATE_SELECT_ABTYPE:
251 implCreateDataSource();
252 if ( needAdminInvokationPage() )
253 break;
254 // no break here
256 case STATE_INVOKE_ADMIN_DIALOG:
257 if ( !connectToDataSource( false ) )
259 // connecting did not succeed -> do not allow proceeding
260 bAllow = false;
261 break;
265 // now that we connected to the data source, check whether we need the "table selection" page
266 const StringBag& aTables = m_aNewDataSource.getTableNames();
268 if ( aTables.empty() )
270 if (RET_YES != ScopedVclPtrInstance<MessageDialog>::Create(this, ModuleRes(( getSettings().eType == AST_EVOLUTION_GROUPWISE ? RID_STR_QRY_NO_EVO_GW : RID_STR_QRY_NOTABLES)), VCL_MESSAGE_QUESTION, VCL_BUTTONS_YES_NO)->Execute())
272 // cannot ask the user, or the user chose to use this data source, though there are no tables
273 bAllow = false;
274 break;
277 m_aSettings.bIgnoreNoTable = true;
280 if ( aTables.size() == 1 )
281 // remember the one and only table we have
282 m_aSettings.sSelectedTable = *aTables.begin();
284 break;
287 impl_updateRoadmap( m_aSettings.eType );
288 return bAllow;
292 void OAddessBookSourcePilot::implDefaultTableName()
294 const StringBag& rTableNames = getDataSource().getTableNames();
295 if ( rTableNames.end() != rTableNames.find( getSettings().sSelectedTable ) )
296 // already a valid table selected
297 return;
299 const sal_Char* pGuess = NULL;
300 switch ( getSettings().eType )
302 case AST_MORK :
303 case AST_THUNDERBIRD : pGuess = "Personal Address book"; break;
304 case AST_LDAP : pGuess = "LDAP Directory"; break;
305 case AST_EVOLUTION :
306 case AST_EVOLUTION_GROUPWISE:
307 case AST_EVOLUTION_LDAP : pGuess = "Personal"; break;
308 default:
309 OSL_FAIL( "OAddessBookSourcePilot::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 OAddessBookSourcePilot::implDoAutoFieldMapping()
320 DBG_ASSERT( !needManualFieldMapping( ), "OAddessBookSourcePilot::implDoAutoFieldMapping: invalid call!" );
322 fieldmapping::defaultMapping( getORB(), m_aSettings.aFieldMapping );
326 void OAddessBookSourcePilot::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_LDAP:
372 m_aNewDataSource = aContext.createNewLDAP( m_aSettings.sDataSourceName );
373 break;
375 case AST_OUTLOOK:
376 m_aNewDataSource = aContext.createNewOutlook( m_aSettings.sDataSourceName );
377 break;
379 case AST_OE:
380 m_aNewDataSource = aContext.createNewOE( m_aSettings.sDataSourceName );
381 break;
383 case AST_OTHER:
384 m_aNewDataSource = aContext.createNewDBase( m_aSettings.sDataSourceName );
385 break;
387 case AST_INVALID:
388 OSL_FAIL( "OAddessBookSourcePilot::implCreateDataSource: illegal data source type!" );
389 break;
391 m_eNewDataSourceType = m_aSettings.eType;
395 bool OAddessBookSourcePilot::connectToDataSource( bool _bForceReConnect )
397 DBG_ASSERT( m_aNewDataSource.isValid(), "OAddessBookSourcePilot::implConnect: invalid current data source!" );
399 WaitObject aWaitCursor( this );
400 if ( _bForceReConnect && m_aNewDataSource.isConnected( ) )
401 m_aNewDataSource.disconnect( );
403 return m_aNewDataSource.connect( this );
407 VclPtr<TabPage> OAddessBookSourcePilot::createPage(WizardState _nState)
409 switch (_nState)
411 case STATE_SELECT_ABTYPE:
412 return VclPtr<TypeSelectionPage>::Create( this );
414 case STATE_INVOKE_ADMIN_DIALOG:
415 return VclPtr<AdminDialogInvokationPage>::Create( this );
417 case STATE_TABLE_SELECTION:
418 return VclPtr<TableSelectionPage>::Create( this );
420 case STATE_MANUAL_FIELD_MAPPING:
421 return VclPtr<FieldMappingPage>::Create( this );
423 case STATE_FINAL_CONFIRM:
424 return VclPtr<FinalPage>::Create( this );
426 default:
427 OSL_FAIL("OAddessBookSourcePilot::createPage: invalid state!");
428 return NULL;
433 void OAddessBookSourcePilot::impl_updateRoadmap( AddressSourceType _eType )
435 bool bSettingsPage = needAdminInvokationPage( _eType );
436 bool bTablesPage = needTableSelection( _eType );
437 bool bFieldsPage = needManualFieldMapping( _eType );
439 bool bConnected = m_aNewDataSource.isConnected();
440 bool bCanSkipTables =
441 ( m_aNewDataSource.hasTable( m_aSettings.sSelectedTable )
442 || m_aSettings.bIgnoreNoTable
445 enableState( STATE_INVOKE_ADMIN_DIALOG, bSettingsPage );
447 enableState( STATE_TABLE_SELECTION,
448 bTablesPage && ( bConnected ? !bCanSkipTables : !bSettingsPage )
449 // if we do not need a settings page, we connect upon "Next" on the first page
452 enableState( STATE_MANUAL_FIELD_MAPPING,
453 bFieldsPage && bConnected && m_aNewDataSource.hasTable( m_aSettings.sSelectedTable )
456 enableState( STATE_FINAL_CONFIRM,
457 bConnected && bCanSkipTables
462 void OAddessBookSourcePilot::typeSelectionChanged( AddressSourceType _eType )
464 PathId nCurrentPathID( PATH_COMPLETE );
465 bool bSettingsPage = needAdminInvokationPage( _eType );
466 bool bFieldsPage = needManualFieldMapping( _eType );
467 if ( !bSettingsPage )
468 if ( !bFieldsPage )
469 nCurrentPathID = PATH_NO_SETTINGS_NO_FIELDS;
470 else
471 nCurrentPathID = PATH_NO_SETTINGS;
472 else
473 if ( !bFieldsPage )
474 nCurrentPathID = PATH_NO_FIELDS;
475 else
476 nCurrentPathID = PATH_COMPLETE;
477 activatePath( nCurrentPathID, true );
479 m_aNewDataSource.disconnect();
480 m_aSettings.bIgnoreNoTable = false;
481 impl_updateRoadmap( _eType );
485 } // namespace abp
488 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */