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