Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "abspilot.hxx"
21 : #include "abpilot.hrc"
22 : #include "abpresid.hrc"
23 : #include "componentmodule.hxx"
24 : #include <tools/debug.hxx>
25 : #include <svtools/localresaccess.hxx>
26 : #include "typeselectionpage.hxx"
27 : #include "admininvokationpage.hxx"
28 : #include "tableselectionpage.hxx"
29 : #include <vcl/waitobj.hxx>
30 : #include <vcl/msgbox.hxx>
31 : #include "abpfinalpage.hxx"
32 : #include "fieldmappingpage.hxx"
33 : #include "fieldmappingimpl.hxx"
34 :
35 : //.........................................................................
36 : namespace abp
37 : {
38 : //.........................................................................
39 :
40 : #define STATE_SELECT_ABTYPE 0
41 : #define STATE_INVOKE_ADMIN_DIALOG 1
42 : #define STATE_TABLE_SELECTION 2
43 : #define STATE_MANUAL_FIELD_MAPPING 3
44 : #define STATE_FINAL_CONFIRM 4
45 :
46 : #define PATH_COMPLETE 1
47 : #define PATH_NO_SETTINGS 2
48 : #define PATH_NO_FIELDS 3
49 : #define PATH_NO_SETTINGS_NO_FIELDS 4
50 :
51 : using namespace ::svt;
52 : using namespace ::com::sun::star::uno;
53 : using namespace ::com::sun::star::lang;
54 :
55 : //=====================================================================
56 : //= OAddessBookSourcePilot
57 : //=====================================================================
58 : //---------------------------------------------------------------------
59 0 : OAddessBookSourcePilot::OAddessBookSourcePilot(Window* _pParent, const Reference< XMultiServiceFactory >& _rxORB)
60 : :OAddessBookSourcePilot_Base( _pParent, ModuleRes( RID_DLG_ADDRESSBOOKSOURCEPILOT ),
61 : WZB_HELP | WZB_FINISH | WZB_CANCEL | WZB_NEXT | WZB_PREVIOUS )
62 : ,m_xORB(_rxORB)
63 : ,m_aNewDataSource(_rxORB)
64 0 : ,m_eNewDataSourceType( AST_INVALID )
65 : {
66 0 : SetPageSizePixel(LogicToPixel(Size(WINDOW_SIZE_X, WINDOW_SIZE_Y), MAP_APPFONT));
67 :
68 0 : ShowButtonFixedLine(sal_True);
69 :
70 : declarePath( PATH_COMPLETE,
71 : STATE_SELECT_ABTYPE,
72 : STATE_INVOKE_ADMIN_DIALOG,
73 : STATE_TABLE_SELECTION,
74 : STATE_MANUAL_FIELD_MAPPING,
75 : STATE_FINAL_CONFIRM,
76 : WZS_INVALID_STATE
77 0 : );
78 : declarePath( PATH_NO_SETTINGS,
79 : STATE_SELECT_ABTYPE,
80 : STATE_TABLE_SELECTION,
81 : STATE_MANUAL_FIELD_MAPPING,
82 : STATE_FINAL_CONFIRM,
83 : WZS_INVALID_STATE
84 0 : );
85 : declarePath( PATH_NO_FIELDS,
86 : STATE_SELECT_ABTYPE,
87 : STATE_INVOKE_ADMIN_DIALOG,
88 : STATE_TABLE_SELECTION,
89 : STATE_FINAL_CONFIRM,
90 : WZS_INVALID_STATE
91 0 : );
92 : declarePath( PATH_NO_SETTINGS_NO_FIELDS,
93 : STATE_SELECT_ABTYPE,
94 : STATE_TABLE_SELECTION,
95 : STATE_FINAL_CONFIRM,
96 : WZS_INVALID_STATE
97 0 : );
98 :
99 0 : m_pPrevPage->SetHelpId(HID_ABSPILOT_PREVIOUS);
100 0 : m_pNextPage->SetHelpId(HID_ABSPILOT_NEXT);
101 0 : m_pCancel->SetHelpId(HID_ABSPILOT_CANCEL);
102 0 : m_pFinish->SetHelpId(HID_ABSPILOT_FINISH);
103 0 : m_pHelp->SetUniqueId(UID_ABSPILOT_HELP);
104 :
105 0 : m_pCancel->SetClickHdl( LINK( this, OAddessBookSourcePilot, OnCancelClicked) );
106 :
107 : // some initial settings
108 : #ifdef UNX
109 : #ifdef MACOSX
110 : m_aSettings.eType = AST_MACAB;
111 : #else
112 : // FIXME: if KDE use KAB instead
113 0 : m_aSettings.eType = AST_EVOLUTION;
114 : #endif
115 : #else
116 : m_aSettings.eType = AST_OTHER;
117 : #endif
118 0 : m_aSettings.sDataSourceName = String(ModuleRes(RID_STR_DEFAULT_NAME));
119 0 : m_aSettings.bRegisterDataSource = false;
120 0 : m_aSettings.bIgnoreNoTable = false;
121 :
122 0 : defaultButton(WZB_NEXT);
123 0 : enableButtons(WZB_FINISH, sal_False);
124 0 : ActivatePage();
125 :
126 0 : typeSelectionChanged( m_aSettings.eType );
127 0 : }
128 :
129 0 : OAddessBookSourcePilot::~OAddessBookSourcePilot()
130 : {
131 0 : FreeResource();
132 0 : }
133 :
134 : //---------------------------------------------------------------------
135 0 : String OAddessBookSourcePilot::getStateDisplayName( WizardState _nState ) const
136 : {
137 0 : sal_uInt16 nResId = 0;
138 0 : switch ( _nState )
139 : {
140 0 : case STATE_SELECT_ABTYPE: nResId = STR_SELECT_ABTYPE; break;
141 0 : case STATE_INVOKE_ADMIN_DIALOG: nResId = STR_INVOKE_ADMIN_DIALOG; break;
142 0 : case STATE_TABLE_SELECTION: nResId = STR_TABLE_SELECTION; break;
143 0 : case STATE_MANUAL_FIELD_MAPPING: nResId = STR_MANUAL_FIELD_MAPPING; break;
144 0 : case STATE_FINAL_CONFIRM: nResId = STR_FINAL_CONFIRM; break;
145 : }
146 : DBG_ASSERT( nResId, "OAddessBookSourcePilot::getStateDisplayName: don't know this state!" );
147 :
148 0 : String sDisplayName;
149 0 : if ( nResId )
150 : {
151 0 : svt::OLocalResourceAccess aAccess( ModuleRes( RID_DLG_ADDRESSBOOKSOURCEPILOT ), RSC_MODALDIALOG );
152 0 : sDisplayName = String( ModuleRes( nResId ) );
153 : }
154 :
155 0 : return sDisplayName;
156 : }
157 :
158 : //---------------------------------------------------------------------
159 0 : void OAddessBookSourcePilot::implCommitAll()
160 : {
161 : // in real, the data source already exists in the data source context
162 : // Thus, if the user changed the name, we have to rename the data source
163 0 : if ( m_aSettings.sDataSourceName != m_aNewDataSource.getName() )
164 0 : m_aNewDataSource.rename( m_aSettings.sDataSourceName );
165 :
166 : // 1. the data source
167 0 : m_aNewDataSource.store();
168 :
169 : // 2. check if we need to register the data source
170 0 : if ( m_aSettings.bRegisterDataSource )
171 0 : m_aNewDataSource.registerDataSource(m_aSettings.sRegisteredDataSourceName);
172 :
173 : // 3. write the data source / table names into the configuration
174 0 : addressconfig::writeTemplateAddressSource( getORB(), m_aSettings.bRegisterDataSource ? m_aSettings.sRegisteredDataSourceName : m_aSettings.sDataSourceName, m_aSettings.sSelectedTable );
175 :
176 : // 4. write the field mapping
177 0 : fieldmapping::writeTemplateAddressFieldMapping( getORB(), m_aSettings.aFieldMapping );
178 0 : }
179 :
180 : //---------------------------------------------------------------------
181 0 : void OAddessBookSourcePilot::implCleanup()
182 : {
183 0 : if ( m_aNewDataSource.isValid() )
184 0 : m_aNewDataSource.remove();
185 0 : }
186 :
187 : //---------------------------------------------------------------------
188 0 : IMPL_LINK( OAddessBookSourcePilot, OnCancelClicked, void*, /*NOTINTERESTEDIN*/ )
189 : {
190 : // do cleanups
191 0 : implCleanup();
192 :
193 : // reset the click hdl
194 0 : m_pCancel->SetClickHdl( Link() );
195 : // simulate the click again - this time, the default handling of the button will strike ....
196 0 : m_pCancel->Click();
197 :
198 0 : return 0L;
199 : }
200 :
201 : //---------------------------------------------------------------------
202 0 : sal_Bool OAddessBookSourcePilot::Close()
203 : {
204 0 : implCleanup();
205 :
206 0 : return OAddessBookSourcePilot_Base::Close();
207 : }
208 :
209 : //---------------------------------------------------------------------
210 0 : sal_Bool OAddessBookSourcePilot::onFinish()
211 : {
212 0 : if ( !OAddessBookSourcePilot_Base::onFinish() )
213 0 : return sal_False;
214 :
215 0 : implCommitAll();
216 :
217 0 : addressconfig::markPilotSuccess( getORB() );
218 :
219 0 : return sal_True;
220 : }
221 :
222 : //---------------------------------------------------------------------
223 0 : void OAddessBookSourcePilot::enterState( WizardState _nState )
224 : {
225 0 : switch ( _nState )
226 : {
227 : case STATE_SELECT_ABTYPE:
228 0 : impl_updateRoadmap( static_cast< TypeSelectionPage* >( GetPage( STATE_SELECT_ABTYPE ) )->getSelectedType() );
229 0 : break;
230 :
231 : case STATE_FINAL_CONFIRM:
232 0 : if ( !needManualFieldMapping( ) )
233 0 : implDoAutoFieldMapping();
234 0 : break;
235 :
236 : case STATE_TABLE_SELECTION:
237 0 : implDefaultTableName();
238 0 : break;
239 : }
240 :
241 0 : OAddessBookSourcePilot_Base::enterState(_nState);
242 0 : }
243 :
244 : //---------------------------------------------------------------------
245 0 : sal_Bool OAddessBookSourcePilot::prepareLeaveCurrentState( CommitPageReason _eReason )
246 : {
247 0 : if ( !OAddessBookSourcePilot_Base::prepareLeaveCurrentState( _eReason ) )
248 0 : return sal_False;
249 :
250 0 : if ( _eReason == eTravelBackward )
251 0 : return sal_True;
252 :
253 0 : sal_Bool bAllow = sal_True;
254 :
255 0 : switch ( getCurrentState() )
256 : {
257 : case STATE_SELECT_ABTYPE:
258 0 : implCreateDataSource();
259 0 : if ( needAdminInvokationPage() )
260 0 : break;
261 : // no break here
262 :
263 : case STATE_INVOKE_ADMIN_DIALOG:
264 0 : if ( !connectToDataSource( sal_False ) )
265 : {
266 : // connecting did not succeed -> do not allow proceeding
267 0 : bAllow = sal_False;
268 0 : break;
269 : }
270 :
271 : // ........................................................
272 : // now that we connected to the data source, check whether we need the "table selection" page
273 0 : const StringBag& aTables = m_aNewDataSource.getTableNames();
274 :
275 0 : if ( aTables.empty() )
276 : {
277 0 : if ( RET_YES != QueryBox( this, ModuleRes( ( getSettings().eType == AST_EVOLUTION_GROUPWISE ? RID_QRY_NO_EVO_GW : RID_QRY_NOTABLES ) ) ).Execute() )
278 : {
279 : // cannot ask the user, or the user chose to use this data source, though there are no tables
280 0 : bAllow = sal_False;
281 0 : break;
282 : }
283 :
284 0 : m_aSettings.bIgnoreNoTable = true;
285 : }
286 :
287 0 : if ( aTables.size() == 1 )
288 : // remember the one and only table we have
289 0 : m_aSettings.sSelectedTable = *aTables.begin();
290 :
291 0 : break;
292 : }
293 :
294 0 : impl_updateRoadmap( m_aSettings.eType );
295 0 : return bAllow;
296 : }
297 :
298 : //---------------------------------------------------------------------
299 0 : void OAddessBookSourcePilot::implDefaultTableName()
300 : {
301 0 : const StringBag& rTableNames = getDataSource().getTableNames();
302 0 : if ( rTableNames.end() != rTableNames.find( getSettings().sSelectedTable ) )
303 : // already a valid table selected
304 : return;
305 :
306 0 : const sal_Char* pGuess = NULL;
307 0 : switch ( getSettings().eType )
308 : {
309 : case AST_MORK :
310 0 : case AST_THUNDERBIRD : pGuess = "Personal Address book"; break;
311 0 : case AST_LDAP : pGuess = "LDAP Directory"; break;
312 : case AST_EVOLUTION :
313 : case AST_EVOLUTION_GROUPWISE:
314 0 : case AST_EVOLUTION_LDAP : pGuess = "Personal"; break;
315 : default:
316 : OSL_FAIL( "OAddessBookSourcePilot::implDefaultTableName: unhandled case!" );
317 : return;
318 : }
319 0 : const ::rtl::OUString sGuess = ::rtl::OUString::createFromAscii( pGuess );
320 0 : if ( rTableNames.end() != rTableNames.find( sGuess ) )
321 0 : getSettings().sSelectedTable = sGuess;
322 : }
323 :
324 : //---------------------------------------------------------------------
325 0 : void OAddessBookSourcePilot::implDoAutoFieldMapping()
326 : {
327 : DBG_ASSERT( !needManualFieldMapping( ), "OAddessBookSourcePilot::implDoAutoFieldMapping: invalid call!" );
328 :
329 0 : fieldmapping::defaultMapping( getORB(), m_aSettings.aFieldMapping );
330 0 : }
331 :
332 : //---------------------------------------------------------------------
333 0 : void OAddessBookSourcePilot::implCreateDataSource()
334 : {
335 0 : if (m_aNewDataSource.isValid())
336 : { // we already have a data source object
337 0 : if ( m_aSettings.eType == m_eNewDataSourceType )
338 : // and it already has the correct type
339 0 : return;
340 :
341 : // it has a wrong type -> remove it
342 0 : m_aNewDataSource.remove();
343 : }
344 :
345 0 : ODataSourceContext aContext( getORB() );
346 0 : aContext.disambiguate( m_aSettings.sDataSourceName );
347 :
348 0 : switch (m_aSettings.eType)
349 : {
350 : case AST_MORK:
351 0 : m_aNewDataSource = aContext.createNewMORK( m_aSettings.sDataSourceName );
352 0 : break;
353 :
354 : case AST_THUNDERBIRD:
355 0 : m_aNewDataSource = aContext.createNewThunderbird( m_aSettings.sDataSourceName );
356 0 : break;
357 :
358 : case AST_EVOLUTION:
359 0 : m_aNewDataSource = aContext.createNewEvolution( m_aSettings.sDataSourceName );
360 0 : break;
361 :
362 : case AST_EVOLUTION_GROUPWISE:
363 0 : m_aNewDataSource = aContext.createNewEvolutionGroupwise( m_aSettings.sDataSourceName );
364 0 : break;
365 :
366 : case AST_EVOLUTION_LDAP:
367 0 : m_aNewDataSource = aContext.createNewEvolutionLdap( m_aSettings.sDataSourceName );
368 0 : break;
369 :
370 : case AST_KAB:
371 0 : m_aNewDataSource = aContext.createNewKab( m_aSettings.sDataSourceName );
372 0 : break;
373 :
374 : case AST_MACAB:
375 0 : m_aNewDataSource = aContext.createNewMacab( m_aSettings.sDataSourceName );
376 0 : break;
377 :
378 : case AST_LDAP:
379 0 : m_aNewDataSource = aContext.createNewLDAP( m_aSettings.sDataSourceName );
380 0 : break;
381 :
382 : case AST_OUTLOOK:
383 0 : m_aNewDataSource = aContext.createNewOutlook( m_aSettings.sDataSourceName );
384 0 : break;
385 :
386 : case AST_OE:
387 0 : m_aNewDataSource = aContext.createNewOE( m_aSettings.sDataSourceName );
388 0 : break;
389 :
390 : case AST_OTHER:
391 0 : m_aNewDataSource = aContext.createNewDBase( m_aSettings.sDataSourceName );
392 0 : break;
393 :
394 : case AST_INVALID:
395 : OSL_FAIL( "OAddessBookSourcePilot::implCreateDataSource: illegal data source type!" );
396 0 : break;
397 : }
398 0 : m_eNewDataSourceType = m_aSettings.eType;
399 : }
400 :
401 : //---------------------------------------------------------------------
402 0 : sal_Bool OAddessBookSourcePilot::connectToDataSource( sal_Bool _bForceReConnect )
403 : {
404 : DBG_ASSERT( m_aNewDataSource.isValid(), "OAddessBookSourcePilot::implConnect: invalid current data source!" );
405 :
406 0 : WaitObject aWaitCursor( this );
407 0 : if ( _bForceReConnect && m_aNewDataSource.isConnected( ) )
408 0 : m_aNewDataSource.disconnect( );
409 :
410 0 : return m_aNewDataSource.connect( this );
411 : }
412 :
413 : //---------------------------------------------------------------------
414 0 : OWizardPage* OAddessBookSourcePilot::createPage(WizardState _nState)
415 : {
416 0 : switch (_nState)
417 : {
418 : case STATE_SELECT_ABTYPE:
419 0 : return new TypeSelectionPage( this );
420 :
421 : case STATE_INVOKE_ADMIN_DIALOG:
422 0 : return new AdminDialogInvokationPage( this );
423 :
424 : case STATE_TABLE_SELECTION:
425 0 : return new TableSelectionPage( this );
426 :
427 : case STATE_MANUAL_FIELD_MAPPING:
428 0 : return new FieldMappingPage( this );
429 :
430 : case STATE_FINAL_CONFIRM:
431 0 : return new FinalPage( this );
432 :
433 : default:
434 : OSL_FAIL("OAddessBookSourcePilot::createPage: invalid state!");
435 0 : return NULL;
436 : }
437 : }
438 :
439 : //---------------------------------------------------------------------
440 0 : void OAddessBookSourcePilot::impl_updateRoadmap( AddressSourceType _eType )
441 : {
442 0 : bool bSettingsPage = needAdminInvokationPage( _eType );
443 0 : bool bTablesPage = needTableSelection( _eType );
444 0 : bool bFieldsPage = needManualFieldMapping( _eType );
445 :
446 0 : bool bConnected = m_aNewDataSource.isConnected();
447 : bool bCanSkipTables =
448 0 : ( m_aNewDataSource.hasTable( m_aSettings.sSelectedTable )
449 : || m_aSettings.bIgnoreNoTable
450 0 : );
451 :
452 0 : enableState( STATE_INVOKE_ADMIN_DIALOG, bSettingsPage );
453 :
454 : enableState( STATE_TABLE_SELECTION,
455 0 : bTablesPage && ( bConnected ? !bCanSkipTables : !bSettingsPage )
456 : // if we do not need a settings page, we connect upon "Next" on the first page
457 0 : );
458 :
459 : enableState( STATE_MANUAL_FIELD_MAPPING,
460 0 : bFieldsPage && bConnected && m_aNewDataSource.hasTable( m_aSettings.sSelectedTable )
461 0 : );
462 :
463 : enableState( STATE_FINAL_CONFIRM,
464 : bConnected && bCanSkipTables
465 0 : );
466 0 : }
467 :
468 : //---------------------------------------------------------------------
469 0 : void OAddessBookSourcePilot::typeSelectionChanged( AddressSourceType _eType )
470 : {
471 0 : PathId nCurrentPathID( PATH_COMPLETE );
472 0 : bool bSettingsPage = needAdminInvokationPage( _eType );
473 0 : bool bFieldsPage = needManualFieldMapping( _eType );
474 0 : if ( !bSettingsPage )
475 0 : if ( !bFieldsPage )
476 0 : nCurrentPathID = PATH_NO_SETTINGS_NO_FIELDS;
477 : else
478 0 : nCurrentPathID = PATH_NO_SETTINGS;
479 : else
480 0 : if ( !bFieldsPage )
481 0 : nCurrentPathID = PATH_NO_FIELDS;
482 : else
483 0 : nCurrentPathID = PATH_COMPLETE;
484 0 : activatePath( nCurrentPathID, true );
485 :
486 0 : m_aNewDataSource.disconnect();
487 0 : m_aSettings.bIgnoreNoTable = false;
488 0 : impl_updateRoadmap( _eType );
489 0 : }
490 :
491 : //.........................................................................
492 : } // namespace abp
493 : //.........................................................................
494 :
495 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|