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