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