xref: /trunk/main/dbaccess/source/ui/dlg/ConnectionHelper.cxx (revision 66b843ff8f1eedd2e69941f1ea52fa080f01ec28)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_dbui.hxx"
26 
27 #include "dsnItem.hxx"
28 #include "ConnectionHelper.hxx"
29 #include "AutoControlsDef.hrc"
30 #include "dbu_dlg.hrc"
31 #include "dbu_misc.hrc"
32 #include <svl/itemset.hxx>
33 #include <unotools/moduleoptions.hxx>
34 #include <sfx2/fcontnr.hxx>
35 #include <unotools/pathoptions.hxx>
36 #include <svl/stritem.hxx>
37 #include <svl/eitem.hxx>
38 #include <svl/intitem.hxx>
39 #include "dsitems.hxx"
40 #include "dbaccess_helpid.hrc"
41 #include "localresaccess.hxx"
42 #include <osl/process.h>
43 #include <vcl/msgbox.hxx>
44 #include <sfx2/filedlghelper.hxx>
45 #include "dbadmin.hxx"
46 #include <comphelper/types.hxx>
47 #include <vcl/stdtext.hxx>
48 #include "sqlmessage.hxx"
49 #include "odbcconfig.hxx"
50 #include "dsselect.hxx"
51 #include <svl/filenotation.hxx>
52 #include "dbustrings.hrc"
53 #include <com/sun/star/ui/dialogs/XFolderPicker.hpp>
54 #include <com/sun/star/sdbc/XRow.hpp>
55 #include <com/sun/star/awt/XWindow.hpp>
56 // #106016# ------------------------------------
57 #include <com/sun/star/task/XInteractionHandler.hpp>
58 #include <com/sun/star/ucb/XProgressHandler.hpp>
59 #include "UITools.hxx"
60 #include <unotools/localfilehelper.hxx>
61 #include <unotools/ucbhelper.hxx>
62 #include <ucbhelper/commandenvironment.hxx>
63 #include "finteraction.hxx"
64 #include <connectivity/CommonTools.hxx>
65 #include <tools/urlobj.hxx>
66 #include <tools/diagnose_ex.h>
67 #include <sfx2/docfilt.hxx>
68 #if !defined(WINDOWS_VISTA_PSDK) && defined(WNT)
69 #define _ADO_DATALINK_BROWSE_
70 #endif
71 
72 #ifdef _ADO_DATALINK_BROWSE_
73 #if defined( WNT )
74     #include <tools/prewin.h>
75     #include <windows.h>
76     #include <tools/postwin.h>
77 #endif
78 #ifndef _SV_SYSDATA_HXX
79 #include <vcl/sysdata.hxx>
80 #endif
81 #ifndef _DBAUI_ADO_DATALINK_HXX_
82 #include "adodatalinks.hxx"
83 #endif
84 #endif //_ADO_DATALINK_BROWSE_
85 
86 #ifndef _COM_SUN_STAR_MOZILLA_XMOZILLABOOTSTRAP_HPP_
87 #include <com/sun/star/mozilla/XMozillaBootstrap.hpp>
88 #endif
89 #include <unotools/processfactory.hxx>
90 
91 
92 
93 //.........................................................................
94 namespace dbaui
95 {
96 //.........................................................................
97     using namespace ::com::sun::star::uno;
98     using namespace ::com::sun::star::ucb;
99     using namespace ::com::sun::star::ui::dialogs;
100     using namespace ::com::sun::star::sdbc;
101     using namespace ::com::sun::star::beans;
102     using namespace ::com::sun::star::lang;
103     using namespace ::com::sun::star::container;
104     using namespace ::com::sun::star::mozilla;
105     using namespace ::dbtools;
106     using namespace ::svt;
107 
108 
109 DBG_NAME(OConnectionHelper)
110 
111     OConnectionHelper::OConnectionHelper( Window* pParent, const ResId& _rId, const SfxItemSet& _rCoreAttrs)
112         :OGenericAdministrationPage(pParent, _rId, _rCoreAttrs)
113         ,m_aFT_Connection   ( this, ResId( FT_AUTOBROWSEURL, *_rId.GetResMgr() ) )
114         ,m_aConnectionURL   ( this, ResId( ET_AUTOBROWSEURL, *_rId.GetResMgr() ) )
115         ,m_aPB_Connection   ( this, ResId( PB_AUTOBROWSEURL, *_rId.GetResMgr() ) )
116     {
117         DBG_CTOR(OConnectionHelper,NULL);
118 
119         // extract the datasource type collection from the item set
120         DbuTypeCollectionItem* pCollectionItem = PTR_CAST(DbuTypeCollectionItem, _rCoreAttrs.GetItem(DSID_TYPECOLLECTION));
121         if (pCollectionItem)
122             m_pCollection = pCollectionItem->getCollection();
123         m_aPB_Connection.SetClickHdl(LINK(this, OConnectionHelper, OnBrowseConnections));
124         DBG_ASSERT(m_pCollection, "OConnectionHelper::OConnectionHelper : really need a DSN type collection !");
125         m_aConnectionURL.SetTypeCollection(m_pCollection);
126     }
127 
128 
129     OConnectionHelper::~OConnectionHelper()
130     {
131 
132         DBG_DTOR(OConnectionHelper,NULL);
133     }
134 
135 
136     // -----------------------------------------------------------------------
137     void OConnectionHelper::implInitControls(const SfxItemSet& _rSet, sal_Bool _bSaveValue)
138     {
139         // check whether or not the selection is invalid or readonly (invalid implies readonly, but not vice versa)
140         sal_Bool bValid, bReadonly;
141         getFlags(_rSet, bValid, bReadonly);
142 
143         m_aFT_Connection.Show();
144         m_aConnectionURL.Show();
145         m_aConnectionURL.ShowPrefix( ::dbaccess::DST_JDBC == m_pCollection->determineType(m_eType) );
146 
147         sal_Bool bEnableBrowseButton = m_pCollection->supportsBrowsing( m_eType );
148         m_aPB_Connection.Show( bEnableBrowseButton );
149 
150         SFX_ITEMSET_GET(_rSet, pUrlItem, SfxStringItem, DSID_CONNECTURL, sal_True);
151 
152         // forward the values to the controls
153         if ( bValid )
154         {
155             String sUrl = pUrlItem->GetValue();
156             setURL( sUrl );
157 
158             checkTestConnection();
159             m_aConnectionURL.ClearModifyFlag();
160         }
161 
162         OGenericAdministrationPage::implInitControls(_rSet, _bSaveValue);
163     }
164 
165     // -----------------------------------------------------------------------
166     void OConnectionHelper::implUpdateURLDependentStates() const
167     {
168         OSL_PRECOND( m_pAdminDialog, "OConnectionHelper::implUpdateURLDependentStates: no admin dialog!" );
169         if ( !m_pAdminDialog )
170             return;
171 
172         if ( m_pCollection->isFileSystemBased(m_eType) )
173             m_pAdminDialog->enableConfirmSettings( getURLNoPrefix().Len() > 0 );
174     }
175 
176     // -----------------------------------------------------------------------
177     IMPL_LINK(OConnectionHelper, OnBrowseConnections, PushButton*, /*_pButton*/)
178     {
179         OSL_ENSURE(m_pAdminDialog,"No Admin dialog set! ->GPF");
180         const ::dbaccess::DATASOURCE_TYPE eType = m_pCollection->determineType(m_eType);
181         switch ( eType )
182         {
183             case  ::dbaccess::DST_DBASE:
184             case  ::dbaccess::DST_FLAT:
185             {
186                 try
187                 {
188                     ::rtl::OUString sFolderPickerService = ::rtl::OUString::createFromAscii(SERVICE_UI_FOLDERPICKER);
189                     Reference< XFolderPicker > xFolderPicker(m_xORB->createInstance(sFolderPickerService), UNO_QUERY);
190                     if (!xFolderPicker.is())
191                     {
192                         ShowServiceNotAvailableError(GetParent(), sFolderPickerService, sal_True);
193                         break;
194                     }
195 
196                     sal_Bool bDoBrowse = sal_False;
197                     String sOldPath = getURLNoPrefix();
198                     do
199                     {
200                         if (sOldPath.Len())
201                             xFolderPicker->setDisplayDirectory(sOldPath);
202                         if (0 == xFolderPicker->execute())
203                             // cancelled by the user
204                             return 0L;
205 
206                         sOldPath = xFolderPicker->getDirectory();
207                         switch (checkPathExistence(sOldPath))
208                         {
209                             case RET_RETRY:
210                                 bDoBrowse = sal_True;
211                                 break;
212                             case RET_CANCEL:
213                                 return 0L;
214                             default:
215                                 break;
216                         }
217                     }
218                     while (bDoBrowse);
219 
220                     String sSelectedDirectory = xFolderPicker->getDirectory();
221                     INetURLObject aSelectedDirectory( sSelectedDirectory, INetURLObject::WAS_ENCODED, RTL_TEXTENCODING_UTF8 );
222 
223                     // for UI purpose, we don't want to have the path encoded
224                     sSelectedDirectory = aSelectedDirectory.GetMainURL( INetURLObject::DECODE_WITH_CHARSET, RTL_TEXTENCODING_UTF8  );
225 
226                     setURLNoPrefix( sSelectedDirectory );
227                     SetRoadmapStateValue(sal_True);
228                     callModifiedHdl();
229                 }
230                 catch( const Exception& )
231                 {
232                     DBG_UNHANDLED_EXCEPTION();
233                 }
234             }
235             break;
236             case  ::dbaccess::DST_CALC:
237             {
238                 SvtModuleOptions aModule;
239                 ::sfx2::FileDialogHelper aFileDlg(WB_3DLOOK | WB_STDMODAL | WB_OPEN
240                                                 ,aModule.GetFactoryEmptyDocumentURL(SvtModuleOptions::E_CALC)
241                                                 ,SFX_FILTER_IMPORT);
242                 askForFileName(aFileDlg);
243             }
244             break;
245             case  ::dbaccess::DST_MSACCESS:
246             {
247                 const ::rtl::OUString sExt(RTL_CONSTASCII_USTRINGPARAM("*.mdb"));
248                 String sFilterName(ModuleRes (STR_MSACCESS_FILTERNAME));
249                 ::sfx2::FileDialogHelper aFileDlg(WB_3DLOOK | WB_STDMODAL | WB_OPEN);
250                 aFileDlg.AddFilter(sFilterName,sExt);
251                 aFileDlg.SetCurrentFilter(sFilterName);
252                 askForFileName(aFileDlg);
253             }
254             break;
255             case  ::dbaccess::DST_MSACCESS_2007:
256             {
257                 const ::rtl::OUString sAccdb(RTL_CONSTASCII_USTRINGPARAM("*.accdb"));
258                 String sFilterName2(ModuleRes (STR_MSACCESS_2007_FILTERNAME));
259                 ::sfx2::FileDialogHelper aFileDlg(WB_3DLOOK | WB_STDMODAL | WB_OPEN);
260                 aFileDlg.AddFilter(sFilterName2,sAccdb);
261                 aFileDlg.SetCurrentFilter(sFilterName2);
262                 askForFileName(aFileDlg);
263             }
264             break;
265             case  ::dbaccess::DST_ADABAS:
266             {
267                 // collect all names from the config dir
268                 // and all dir's of the DBWORK/wrk or DBROOT/wrk dir
269                 // compare the names
270 
271                 // collect the names of the installed databases
272                 StringBag aInstalledDBs;
273                 ::rtl::OUString sAdabasConfigDir,sAdabasWorkDir,sRootDir;
274                 ::rtl::OUString sEnvVarName(RTL_CONSTASCII_USTRINGPARAM("DBWORK"));
275                 rtl_uString* pDbVar = NULL;
276                 if(osl_getEnvironment(sEnvVarName.pData,&pDbVar) == osl_Process_E_None && pDbVar)
277                 {
278                     sAdabasWorkDir = pDbVar;
279                     String sURL;
280                     utl::LocalFileHelper::ConvertPhysicalNameToURL(sAdabasWorkDir,sURL);
281                     sAdabasWorkDir = sURL;
282                     rtl_uString_release(pDbVar);
283                     pDbVar = NULL;
284                 }
285 
286                 sEnvVarName = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DBCONFIG"));
287                 if(osl_getEnvironment(sEnvVarName.pData,&pDbVar) == osl_Process_E_None && pDbVar)
288                 {
289                     sAdabasConfigDir = pDbVar;
290                     String sURL;
291                     utl::LocalFileHelper::ConvertPhysicalNameToURL(sAdabasConfigDir,sURL);
292                     sAdabasConfigDir = sURL;
293                     rtl_uString_release(pDbVar);
294                     pDbVar = NULL;
295                 }
296 
297                 sEnvVarName = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DBROOT"));
298                 if(osl_getEnvironment(sEnvVarName.pData,&pDbVar) == osl_Process_E_None && pDbVar)
299                 {
300                     sRootDir = pDbVar;
301                     String sURL;
302                     utl::LocalFileHelper::ConvertPhysicalNameToURL(sRootDir,sURL);
303                     sRootDir = sURL;
304                     rtl_uString_release(pDbVar);
305                     pDbVar = NULL;
306                 }
307 
308                 sal_Bool bOldFashion = sAdabasConfigDir.getLength() && sAdabasWorkDir.getLength();
309 
310                 if(!bOldFashion) // we have a normal adabas installation
311                 {    // so we check the local database names in $DBROOT/config
312                     sAdabasConfigDir    = sRootDir;
313                     sAdabasWorkDir      = sRootDir;
314                 }
315 
316                 if(sAdabasConfigDir.getLength() && sAdabasWorkDir.getLength() && sRootDir.getLength())
317                 {
318 
319                     aInstalledDBs   = getInstalledAdabasDBs(sAdabasConfigDir,sAdabasWorkDir);
320 
321                     if(!aInstalledDBs.size() && bOldFashion)
322                     {
323                         sAdabasConfigDir    = sRootDir;
324                         sAdabasWorkDir      = sRootDir;
325                         aInstalledDBs       = getInstalledAdabasDBs(sAdabasConfigDir,sAdabasWorkDir);
326                     }
327 
328                     ODatasourceSelectDialog aSelector(GetParent(), aInstalledDBs, true,m_pItemSetHelper->getWriteOutputSet());
329                     if (RET_OK == aSelector.Execute())
330                     {
331                         setURLNoPrefix(aSelector.GetSelected());
332                         //  checkCreateDatabase( ::dbaccess::DST_ADABAS);
333                         SetRoadmapStateValue(sal_True);
334                         callModifiedHdl();
335                     }
336                 }
337                 else
338                 {
339                     LocalResourceAccess aLocRes( PAGE_CONNECTION, RSC_TABPAGE );
340                     String sError = String(ModuleRes(STR_NO_ADABASE_DATASOURCES));
341                     ErrorBox aBox(this, WB_OK, sError);
342                     aBox.Execute();
343                 }
344             }
345             break;
346             case  ::dbaccess::DST_MYSQL_ODBC:
347             case  ::dbaccess::DST_ODBC:
348             {
349                 // collect all ODBC data source names
350                 ::rtl::OUString sCurrDatasource = getURLNoPrefix();
351                 ::rtl::OUString sDataSource;
352                 if ( getSelectedDataSource(sDataSource,sCurrDatasource) && sDataSource.getLength() )
353                 {
354                     setURLNoPrefix(sDataSource);
355                     SetRoadmapStateValue(sal_True);
356                     callModifiedHdl();
357                 }
358                 else
359                     return 1L;
360             }
361             break;
362 #ifdef _ADO_DATALINK_BROWSE_
363             case  ::dbaccess::DST_ADO:
364             {
365                 ::rtl::OUString sOldDataSource=getURLNoPrefix();
366                 ::rtl::OUString sNewDataSource;
367                 HWND hWnd = GetParent()->GetSystemData()->hWnd;
368                 sNewDataSource = getAdoDatalink((long)hWnd,sOldDataSource);
369                 if ( sNewDataSource.getLength() )
370                 {
371                     setURLNoPrefix(sNewDataSource);
372                     SetRoadmapStateValue(sal_True);
373                     callModifiedHdl();
374                 }
375                 else
376                     return 1L;
377             }
378             break;
379 #endif
380             case  ::dbaccess::DST_MOZILLA:
381             case  ::dbaccess::DST_THUNDERBIRD:
382             {
383                 MozillaProductType profileType = MozillaProductType_Mozilla;
384                 if (eType ==  ::dbaccess::DST_THUNDERBIRD)
385                     profileType = MozillaProductType_Thunderbird;
386 
387                 Reference<XMultiServiceFactory> xFactory = ::comphelper::getProcessServiceFactory();
388                 OSL_ENSURE( xFactory.is(), "can't get service factory" );
389 
390                 Reference<XInterface> xInstance = xFactory->createInstance(::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.mozilla.MozillaBootstrap")) );
391                 OSL_ENSURE( xInstance.is(), "failed to create instance" );
392                 Reference<XMozillaBootstrap> xMozillaBootstrap =  Reference<XMozillaBootstrap>(xInstance,UNO_QUERY);
393                 OSL_ENSURE( xMozillaBootstrap.is(), "failed to create instance" );
394 
395                 if (xMozillaBootstrap.is())
396                 {
397                     // collect all Mozilla Profiles
398                     ::com::sun::star::uno::Sequence< ::rtl::OUString > list;
399 
400                     xMozillaBootstrap->getProfileList( profileType, list );
401                     const ::rtl::OUString * pArray = list.getConstArray();
402 
403                     sal_Int32 count = list.getLength();
404 
405                     StringBag aProfiles;
406                     for (sal_Int32 index=0; index < count; index++)
407                         aProfiles.insert(pArray[index]);
408 
409 
410                     // excute the select dialog
411                     ODatasourceSelectDialog aSelector(GetParent(), aProfiles, eType);
412                     ::rtl::OUString sOldProfile=getURLNoPrefix();
413 
414                     if (sOldProfile.getLength())
415                         aSelector.Select(sOldProfile);
416                     else
417                         aSelector.Select(xMozillaBootstrap->getDefaultProfile(profileType));
418 
419                     if ( RET_OK == aSelector.Execute() )
420                         setURLNoPrefix(aSelector.GetSelected());
421                     break;
422                 }
423             }
424             default:
425                 break;
426         }
427 
428         checkTestConnection();
429 
430         return 0L;
431     }
432 
433     //-------------------------------------------------------------------------
434 
435     bool OConnectionHelper::checkTestConnection()
436     {
437         return true;
438     }
439 
440     //-------------------------------------------------------------------------
441     void OConnectionHelper::impl_setURL( const String& _rURL, sal_Bool _bPrefix )
442     {
443         String sURL( _rURL );
444         DBG_ASSERT( m_pCollection, "OConnectionHelper::impl_setURL: have no interpreter for the URLs!" );
445 
446         if ( m_pCollection && sURL.Len() )
447         {
448             if ( m_pCollection->isFileSystemBased( m_eType ) )
449             {
450                 // get the tow parts: prefix and file URL
451                 String sTypePrefix, sFileURLEncoded;
452                 if ( _bPrefix )
453                 {
454                     sTypePrefix = m_pCollection->getPrefix( m_eType );
455                     sFileURLEncoded = m_pCollection->cutPrefix( sURL );
456                 }
457                 else
458                 {
459                     sFileURLEncoded = sURL;
460                 }
461 
462                 // substitute any variables
463                 sFileURLEncoded = SvtPathOptions().SubstituteVariable( sFileURLEncoded );
464 
465                 // decode the URL
466                 sURL = sTypePrefix;
467                 if ( sFileURLEncoded.Len() )
468                 {
469                     OFileNotation aFileNotation(sFileURLEncoded);
470                     // set this decoded URL as text
471                     sURL += String(aFileNotation.get(OFileNotation::N_SYSTEM));
472                 }
473             }
474         }
475 
476         if ( _bPrefix )
477             m_aConnectionURL.SetText( sURL );
478         else
479             m_aConnectionURL.SetTextNoPrefix( sURL );
480 
481         implUpdateURLDependentStates();
482     }
483 
484     //-------------------------------------------------------------------------
485     String OConnectionHelper::impl_getURL( sal_Bool _bPrefix ) const
486     {
487         // get the pure text
488         String sURL = _bPrefix ? m_aConnectionURL.GetText() : m_aConnectionURL.GetTextNoPrefix();
489 
490         DBG_ASSERT( m_pCollection, "OConnectionHelper::impl_getURL: have no interpreter for the URLs!" );
491 
492         if ( m_pCollection && sURL.Len() )
493         {
494             if ( m_pCollection->isFileSystemBased( m_eType ) )
495             {
496                 // get the tow parts: prefix and file URL
497                 String sTypePrefix, sFileURLDecoded;
498                 if ( _bPrefix )
499                 {
500                     sTypePrefix = m_pCollection->getPrefix( m_eType );
501                     sFileURLDecoded = m_pCollection->cutPrefix( sURL );
502                 }
503                 else
504                 {
505                     sFileURLDecoded = sURL;
506                 }
507 
508                 sURL = sTypePrefix;
509                 if ( sFileURLDecoded.Len() )
510                 {
511                     OFileNotation aFileNotation( sFileURLDecoded, OFileNotation::N_SYSTEM );
512                     sURL += String( aFileNotation.get( OFileNotation::N_URL ) );
513                 }
514 
515                 // encode the URL
516                 INetURLObject aFileURL( sFileURLDecoded, INetURLObject::ENCODE_ALL, RTL_TEXTENCODING_UTF8 );
517                 sFileURLDecoded = aFileURL.GetMainURL( INetURLObject::NO_DECODE );
518             }
519         }
520         return sURL;
521     }
522 
523     //-------------------------------------------------------------------------
524     void OConnectionHelper::setURL( const String& _rURL )
525     {
526         impl_setURL( _rURL, sal_True );
527     }
528 
529     //-------------------------------------------------------------------------
530     String OConnectionHelper::getURLNoPrefix( ) const
531     {
532         return impl_getURL( sal_False );
533     }
534 
535     //-------------------------------------------------------------------------
536     void OConnectionHelper::setURLNoPrefix( const String& _rURL )
537     {
538         impl_setURL( _rURL, sal_False );
539     }
540 
541     //-------------------------------------------------------------------------
542     sal_Int32 OConnectionHelper::checkPathExistence(const String& _rURL)
543     {
544         IS_PATH_EXIST e_exists = pathExists(_rURL, sal_False);
545         if (( e_exists == PATH_NOT_EXIST) || ( e_exists == PATH_NOT_KNOWN))
546         {
547             String sQuery(ModuleRes(STR_ASK_FOR_DIRECTORY_CREATION));
548             OFileNotation aTransformer(_rURL);
549             sQuery.SearchAndReplaceAscii("$path$", aTransformer.get(OFileNotation::N_SYSTEM));
550 
551             m_bUserGrabFocus = sal_False;
552             QueryBox aQuery(GetParent(), WB_YES_NO | WB_DEF_YES, sQuery);
553             sal_Int32 nQueryResult = aQuery.Execute();
554             m_bUserGrabFocus = sal_True;
555 
556             switch (nQueryResult)
557             {
558                 case RET_YES:
559                 {
560                     sal_Bool bTryCreate = sal_False;
561                     do
562                     {
563                         if ( !createDirectoryDeep(_rURL) )
564                         {   // could not create the directory
565                             sQuery = String(ModuleRes(STR_COULD_NOT_CREATE_DIRECTORY));
566                             sQuery.SearchAndReplaceAscii("$name$", aTransformer.get(OFileNotation::N_SYSTEM));
567 
568                             m_bUserGrabFocus = sal_False;
569                             QueryBox aWhatToDo(GetParent(), WB_RETRY_CANCEL | WB_DEF_RETRY, sQuery);
570                             nQueryResult = aWhatToDo.Execute();
571                             m_bUserGrabFocus = sal_True;
572 
573                             if (RET_RETRY == nQueryResult)
574                                 bTryCreate = sal_True;
575                             else
576                             {
577                                 SetRoadmapStateValue(sal_False);
578                                 callModifiedHdl();
579                                 return RET_RETRY;
580                             }
581                         }
582                     }
583                     while (bTryCreate);
584                 }
585                 break;
586 
587                 case RET_NO:
588                      // SetRoadmapStateValue(sal_False);
589                     callModifiedHdl();
590                     return RET_OK;
591 
592                 default:
593                     // cancelled
594                     SetRoadmapStateValue(sal_False);
595                     callModifiedHdl();
596                     return RET_CANCEL;
597             }
598         }
599 /*        else
600         {
601             // TODO: error msg
602             return RET_CANCEL;
603         } */
604         SetRoadmapStateValue(sal_True);
605         callModifiedHdl();
606         return RET_OK;
607     }
608 
609 
610     //-------------------------------------------------------------------------
611     StringBag OConnectionHelper::getInstalledAdabasDBDirs(const String& _rPath,const ::ucbhelper::ResultSetInclude& _reResultSetInclude)
612     {
613         INetURLObject aNormalizer;
614         aNormalizer.SetSmartProtocol(INET_PROT_FILE);
615         aNormalizer.SetSmartURL(_rPath);
616         String sAdabasConfigDir = aNormalizer.GetMainURL(INetURLObject::NO_DECODE);
617 
618         ::ucbhelper::Content aAdabasConfigDir;
619         try
620         {
621             aAdabasConfigDir = ::ucbhelper::Content(sAdabasConfigDir, Reference< ::com::sun::star::ucb::XCommandEnvironment >());
622         }
623         catch(::com::sun::star::ucb::ContentCreationException&)
624         {
625             return StringBag();
626         }
627 
628         StringBag aInstalledDBs;
629         sal_Bool bIsFolder = sal_False;
630         try
631         {
632             bIsFolder = aAdabasConfigDir.isFolder();
633         }
634         catch(Exception&) // the exception is thrown when the path doesn't exists
635         {
636         }
637         if (bIsFolder && aAdabasConfigDir.get().is())
638         {   // we have a content for the directory, loop through all entries
639             Sequence< ::rtl::OUString > aProperties(1);
640             aProperties[0] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Title"));
641 
642             try
643             {
644                 Reference< XResultSet > xFiles = aAdabasConfigDir.createCursor(aProperties, _reResultSetInclude);
645                 Reference< XRow > xRow(xFiles, UNO_QUERY);
646                 xFiles->beforeFirst();
647                 while (xFiles->next())
648                 {
649 #ifdef DBG_UTIL
650                     ::rtl::OUString sName = xRow->getString(1);
651 #endif
652                     aInstalledDBs.insert(xRow->getString(1));
653                 }
654             }
655             catch(Exception&)
656             {
657                 DBG_ERROR("OConnectionHelper::getInstalledAdabasDBDirs: could not enumerate the adabas config files!");
658             }
659         }
660 
661 
662         return aInstalledDBs;
663     }
664     // -----------------------------------------------------------------------------
665     StringBag OConnectionHelper::getInstalledAdabasDBs(const String &_rConfigDir,const String &_rWorkDir)
666     {
667         String sAdabasConfigDir(_rConfigDir),sAdabasWorkDir(_rWorkDir);
668 
669         if (sAdabasConfigDir.Len() && ('/' == sAdabasConfigDir.GetBuffer()[sAdabasConfigDir.Len() - 1]))
670             sAdabasConfigDir.AppendAscii("config");
671         else
672             sAdabasConfigDir.AppendAscii("/config");
673 
674         if (sAdabasWorkDir.Len() && ('/' == sAdabasWorkDir.GetBuffer()[sAdabasWorkDir.Len() - 1]))
675             sAdabasWorkDir.AppendAscii("wrk");
676         else
677             sAdabasWorkDir.AppendAscii("/wrk");
678         // collect the names of the installed databases
679         StringBag aInstalledDBs;
680         // collect the names of the installed databases
681         StringBag aConfigDBs,aWrkDBs;
682         aConfigDBs  = getInstalledAdabasDBDirs(sAdabasConfigDir,::ucbhelper::INCLUDE_DOCUMENTS_ONLY);
683         aWrkDBs     = getInstalledAdabasDBDirs(sAdabasWorkDir,::ucbhelper::INCLUDE_FOLDERS_ONLY);
684         ConstStringBagIterator aOuter = aConfigDBs.begin();
685         ConstStringBagIterator aOuterEnd = aConfigDBs.end();
686         for(;aOuter != aOuterEnd;++aOuter)
687         {
688             ConstStringBagIterator aInner = aWrkDBs.begin();
689             ConstStringBagIterator aInnerEnd = aWrkDBs.end();
690             for (;aInner != aInnerEnd; ++aInner)
691             {
692                 if (aInner->equalsIgnoreAsciiCase(*aOuter))
693                 {
694                     aInstalledDBs.insert(*aInner);
695                     break;
696                 }
697             }
698         }
699         return aInstalledDBs;
700     }
701     // #106016# -------------------------------------------------------------------
702     IS_PATH_EXIST OConnectionHelper::pathExists(const ::rtl::OUString& _rURL, sal_Bool bIsFile) const
703     {
704         ::ucbhelper::Content aCheckExistence;
705         sal_Bool bExists = sal_False;
706         IS_PATH_EXIST eExists = PATH_NOT_EXIST;
707         Reference< ::com::sun::star::task::XInteractionHandler > xInteractionHandler = Reference< ::com::sun::star::task::XInteractionHandler >(
708             m_xORB->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.task.InteractionHandler") ) ), UNO_QUERY );
709         OFilePickerInteractionHandler* pHandler = new OFilePickerInteractionHandler(xInteractionHandler);
710         xInteractionHandler = pHandler;
711 
712         Reference< XCommandEnvironment > xCmdEnv = new ::ucbhelper::CommandEnvironment( xInteractionHandler, Reference< XProgressHandler >() );
713         try
714         {
715             aCheckExistence = ::ucbhelper::Content(_rURL, xCmdEnv );
716             bExists = bIsFile? aCheckExistence.isDocument(): aCheckExistence.isFolder();
717             eExists = bExists? PATH_EXIST: PATH_NOT_EXIST;
718         }
719         catch(const Exception&)
720         {
721             eExists = ( pHandler && pHandler->isDoesNotExist() ) ? PATH_NOT_EXIST: (bIsFile ? PATH_NOT_EXIST : PATH_NOT_KNOWN);
722         }
723         return eExists;
724     }
725     //-------------------------------------------------------------------------
726     long OConnectionHelper::PreNotify( NotifyEvent& _rNEvt )
727     {
728         if ( m_pCollection->isFileSystemBased(m_eType) )
729         {
730             switch (_rNEvt.GetType())
731             {
732                 case EVENT_GETFOCUS:
733                     if (m_aConnectionURL.IsWindowOrChild(_rNEvt.GetWindow()) && m_bUserGrabFocus)
734                     {   // a descendant of the URL edit field got the focus
735                         m_aConnectionURL.SaveValueNoPrefix();
736                     }
737                     break;
738 
739                 case EVENT_LOSEFOCUS:
740                     if (m_aConnectionURL.IsWindowOrChild(_rNEvt.GetWindow()) && m_bUserGrabFocus)
741                     {   // a descendant of the URL edit field lost the focus
742                         if (!commitURL())
743                             return 1L;  // handled
744                     }
745                     break;
746             } // switch (_rNEvt.GetType())
747         }
748 
749         return OGenericAdministrationPage::PreNotify( _rNEvt );
750     }
751 
752     //-------------------------------------------------------------------------
753 
754     sal_Bool OConnectionHelper::createDirectoryDeep(const String& _rPathURL)
755     {
756         ::rtl::OUString sPath(_rPathURL);
757 
758         // get an URL object analyzing the URL for us ...
759         INetURLObject aParser;
760         aParser.SetURL(_rPathURL);
761 
762         INetProtocol eProtocol = aParser.GetProtocol();
763 
764         ::std::vector< ::rtl::OUString > aToBeCreated;  // the to-be-created levels
765 
766         // search a level which exists
767         // #106016# ---------------------
768         IS_PATH_EXIST eParentExists = PATH_NOT_EXIST;
769         while ( eParentExists == PATH_NOT_EXIST && aParser.getSegmentCount())
770         {
771             aToBeCreated.push_back(aParser.getName());  // remember the local name for creation
772             aParser.removeSegment();                    // cut the local name
773             eParentExists = pathExists(aParser.GetMainURL(INetURLObject::NO_DECODE), sal_False);
774         }
775 
776         if (!aParser.getSegmentCount())
777             return sal_False;
778 
779         // create all the missing levels
780         try
781         {
782             // the parent content
783             Reference< XCommandEnvironment > xEmptyEnv;
784             ::ucbhelper::Content aParent(aParser.GetMainURL(INetURLObject::NO_DECODE), xEmptyEnv);
785 
786             ::rtl::OUString sContentType;
787             if ( INET_PROT_FILE == eProtocol )
788             {
789                 sContentType = ::rtl::OUString::createFromAscii( "application/vnd.sun.staroffice.fsys-folder" );
790                 // the file UCP currently does not support the ContentType property
791             }
792             else
793             {
794                 Any aContentType = aParent.getPropertyValue( ::rtl::OUString::createFromAscii( "ContentType" ) );
795                 aContentType >>= sContentType;
796             }
797 
798             // the properties which need to be set on the new content
799             Sequence< ::rtl::OUString > aNewDirectoryProperties(1);
800             aNewDirectoryProperties[0] = ::rtl::OUString::createFromAscii("Title");
801 
802             // the values to be set
803             Sequence< Any > aNewDirectoryAttributes(1);
804 
805             // loop
806             for (   ::std::vector< ::rtl::OUString >::reverse_iterator aLocalName = aToBeCreated.rbegin();
807                     aLocalName != aToBeCreated.rend();
808                     ++aLocalName
809                 )
810             {
811                 aNewDirectoryAttributes[0] <<= *aLocalName;
812                 if (!aParent.insertNewContent(sContentType, aNewDirectoryProperties, aNewDirectoryAttributes, aParent))
813                     return sal_False;
814             }
815         }
816         catch ( const Exception& )
817         {
818             DBG_UNHANDLED_EXCEPTION();
819             return sal_False;
820         }
821 
822         return sal_True;
823     }
824 
825 
826     // -----------------------------------------------------------------------
827     void OConnectionHelper::fillWindows(::std::vector< ISaveValueWrapper* >& _rControlList)
828     {
829         _rControlList.push_back(new ODisableWrapper<FixedText>(&m_aFT_Connection));
830         _rControlList.push_back(new ODisableWrapper<PushButton>(&m_aPB_Connection));
831     }
832 
833 
834     // -----------------------------------------------------------------------
835     void OConnectionHelper::fillControls(::std::vector< ISaveValueWrapper* >& _rControlList)
836     {
837         _rControlList.push_back( new OSaveValueWrapper<Edit>( &m_aConnectionURL ) );
838     }
839 
840     //-------------------------------------------------------------------------
841     sal_Bool OConnectionHelper::commitURL()
842     {
843         String sURL;
844         String sOldPath;
845         sOldPath = m_aConnectionURL.GetSavedValueNoPrefix();
846         sURL = m_aConnectionURL.GetTextNoPrefix();
847 
848         if ( m_pCollection->isFileSystemBased(m_eType) )
849         {
850             if ( ( sURL != sOldPath ) && ( 0 != sURL.Len() ) )
851             {   // the text changed since entering the control
852 
853                 // the path may be in system notation ....
854                 OFileNotation aTransformer(sURL);
855                 sURL = aTransformer.get(OFileNotation::N_URL);
856 
857                 const ::dbaccess::DATASOURCE_TYPE eType = m_pCollection->determineType(m_eType);
858 
859                 if ( ( ::dbaccess::DST_CALC == eType) || ( ::dbaccess::DST_MSACCESS == eType) || ( ::dbaccess::DST_MSACCESS_2007 == eType) )
860                 { // #106016# --------------------------
861                     if( pathExists(sURL, sal_True) == PATH_NOT_EXIST )
862                     {
863                         String sFile = String( ModuleRes( STR_FILE_DOES_NOT_EXIST ) );
864                         sFile.SearchAndReplaceAscii("$file$", aTransformer.get(OFileNotation::N_SYSTEM));
865                         OSQLWarningBox( this, sFile ).Execute();
866                         setURLNoPrefix(sOldPath);
867                         SetRoadmapStateValue(sal_False);
868                         callModifiedHdl();
869                         return sal_False;
870                     }
871                 }
872                 else
873                 {
874                     switch (checkPathExistence(sURL))
875                     {
876                         case RET_RETRY:
877                             m_bUserGrabFocus = sal_False;
878                             m_aConnectionURL.GrabFocus();
879                             m_bUserGrabFocus = sal_True;
880                             return sal_False;
881 
882                         case RET_CANCEL:
883                             setURLNoPrefix(sOldPath);
884                             return sal_False;
885                     }
886                 }
887             }
888         }
889 
890         setURLNoPrefix(sURL);
891         m_aConnectionURL.SaveValueNoPrefix();
892         return sal_True;
893     }
894     //-------------------------------------------------------------------------
895     void OConnectionHelper::askForFileName(::sfx2::FileDialogHelper& _aFileOpen)
896     {
897         String sOldPath = getURLNoPrefix();
898         if ( sOldPath.Len() )
899             _aFileOpen.SetDisplayDirectory(sOldPath);
900         else
901             _aFileOpen.SetDisplayDirectory( SvtPathOptions().GetWorkPath() );
902         if (0 == _aFileOpen.Execute())
903         {
904             setURLNoPrefix(_aFileOpen.GetPath());
905             SetRoadmapStateValue(checkTestConnection());
906             callModifiedHdl();
907         }
908     }
909 
910 //.........................................................................
911 }   // namespace dbaui
912 //.........................................................................
913