Update ooo320-m1
[ooovba.git] / connectivity / source / drivers / mozab / mozillasrc / MDatabaseMetaDataHelper.cxx
blobf7e8d7750b42dc17f1e34fd2c7ff15adffbd4f9b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: MDatabaseMetaDataHelper.cxx,v $
10 * $Revision: 1.16.56.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
34 // Mozilla includes.
35 #include <MNSInclude.hxx>
37 #include "resource/mozab_res.hrc"
38 #include "MDatabaseMetaDataHelper.hxx"
39 #ifndef _CONNECTIVITY_MAB_CONVERSIONS_HXX_
40 #include "MTypeConverter.hxx"
41 #endif
42 #ifndef CONNECTIVITY_MOZAB_MCONFIGACCESS_HXX
43 #include "MConfigAccess.hxx"
44 #endif
45 #include <connectivity/dbexception.hxx>
46 #include <comphelper/uno3.hxx>
47 #include <comphelper/sequence.hxx>
48 #include <osl/mutex.hxx>
49 #include <osl/conditn.hxx>
51 #include <nsIAbDirFactoryService.h>
52 #include <MNSInit.hxx>
53 #include <MNameMapper.hxx>
54 #include "MNSMozabProxy.hxx"
55 #include <MNSDeclares.hxx>
57 static ::osl::Mutex m_aMetaMutex;
59 #include <osl/diagnose.h>
60 #include <com/sun/star/uno/Reference.hxx>
61 #include <com/sun/star/uno/Sequence.hxx>
62 #include <com/sun/star/uno/XInterface.hpp>
63 #include <com/sun/star/uno/Exception.hpp>
64 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
65 #include <com/sun/star/beans/Property.hpp>
66 #include <com/sun/star/beans/XPropertySet.hpp>
67 #include <com/sun/star/beans/XPropertySetInfo.hpp>
68 #include <com/sun/star/beans/PropertyAttribute.hpp>
69 #include <com/sun/star/sdb/ErrorCondition.hpp>
70 #include <unotools/processfactory.hxx>
71 #include <com/sun/star/mozilla/XMozillaBootstrap.hpp>
74 #if OSL_DEBUG_LEVEL > 0
75 # define OUtoCStr( x ) ( ::rtl::OUStringToOString ( (x), RTL_TEXTENCODING_ASCII_US).getStr())
76 #else /* OSL_DEBUG_LEVEL */
77 # define OUtoCStr( x ) ("dummy")
78 #endif /* OSL_DEBUG_LEVEL */
81 static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
82 static NS_DEFINE_CID(kAbDirFactoryServiceCID, NS_ABDIRFACTORYSERVICE_CID);
83 static NS_DEFINE_CID(kAddrBookSessionCID, NS_ADDRBOOKSESSION_CID);
85 using namespace connectivity::mozab;
86 using namespace connectivity;
87 using namespace ::com::sun::star::uno;
88 using namespace com::sun::star::lang;
89 using namespace ::com::sun::star::beans;
90 using namespace ::com::sun::star::sdbc;
91 using namespace com::sun::star::mozilla;
92 using namespace com::sun::star::sdb;
94 namespace connectivity
96 namespace mozab
98 static const char * const s_pADDRESSBOOKROOTDIR = "moz-abdirectory://";
102 extern sal_Bool MNS_Init(sal_Bool& aProfileExists);
104 // -------------------------------------------------------------------------
105 MDatabaseMetaDataHelper::MDatabaseMetaDataHelper()
106 :m_bProfileExists(sal_False)
108 OSL_TRACE( "IN MDatabaseMetaDataHelper::MDatabaseMetaDataHelper()\n" );
110 OSL_TRACE( "\tOUT MDatabaseMetaDataHelper::MDatabaseMetaDataHelper()\n" );
113 // -------------------------------------------------------------------------
114 MDatabaseMetaDataHelper::~MDatabaseMetaDataHelper()
116 m_aTableNames.clear();
117 m_aTableTypes.clear();
119 if (m_bProfileExists)
121 Reference<XMozillaBootstrap> xMozillaBootstrap;
122 Reference<XMultiServiceFactory> xFactory = ::comphelper::getProcessServiceFactory();
123 OSL_ENSURE( xFactory.is(), "can't get service factory" );
125 Reference<XInterface> xInstance = xFactory->createInstance(::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.mozilla.MozillaBootstrap")) );
126 OSL_ENSURE( xInstance.is(), "failed to create instance" );
127 xMozillaBootstrap = Reference<XMozillaBootstrap>(xInstance,UNO_QUERY);
128 m_bProfileExists = xMozillaBootstrap->shutdownProfile() > 0;
131 OSL_TRACE( "IN/OUT MDatabaseMetaDataHelper::~MDatabaseMetaDataHelper()\n" );
134 static nsresult enumSubs(nsISimpleEnumerator * subDirs,nsISupportsArray * array);
135 //enum all sub folders
136 static nsresult enumSubs(nsIAbDirectory * parentDir,nsISupportsArray * array)
138 nsresult rv = NS_OK ;
139 nsCOMPtr<nsISimpleEnumerator> subDirectories;
140 rv = parentDir->GetChildNodes(getter_AddRefs(subDirectories));
141 if (NS_FAILED(rv)) { return rv; }
143 rv = enumSubs(subDirectories,array);
144 return rv;
147 #include <prmem.h> //need for PR_FREEIF
148 static nsresult insertPABDescription()
150 nsresult rv = NS_OK ;
151 // mozilla loads the descriptions of pab and history from a file named addressbook.properties
152 // we never deliver it with OOo,so mozab does not auot load personal addressbook and Collected addresses
153 // If we want to deliver addressbook.properties with OOo,we have to deal with hole mozilla chrome mechanism.
154 // So I insert the descriptiones as user prefes,they will cover the default value.
156 //"Personal Address Book" "Collected Addresses" are loaded from officecfg
157 nsCOMPtr<nsIPref> prefs = do_GetService(NS_PREF_CONTRACTID, &rv);
158 NS_ENSURE_SUCCESS(rv, rv);
160 nsCAutoString pabPrefName(NS_LITERAL_CSTRING("ldap_2.servers.pab.description"));
162 sal_Bool bSetted=sal_False;
163 char *value = nsnull;
164 if (0 == prefs->CopyCharPref(pabPrefName.get(), &value))
166 if (!(value == nsnull || value[0] == 0))
168 bSetted = sal_True;
171 PR_FREEIF (value);
172 value=nsnull;
173 if (bSetted == sal_True)
175 const char* pPabDescription = static_cast< const char* >( getPabDescription( ) );
176 rv = prefs->SetCharPref (pabPrefName.get(), pPabDescription);
177 NS_ENSURE_SUCCESS(rv, rv);
180 nsCAutoString hisPrefName(NS_LITERAL_CSTRING("ldap_2.servers.history.description"));
182 bSetted=sal_False;
183 if (0 == prefs->CopyCharPref(hisPrefName.get(), &value))
185 if (!(value == nsnull || value[0] == 0))
187 bSetted = sal_True;
190 PR_FREEIF (value);
191 value=nsnull;
192 if (bSetted == sal_True)
194 const char* pHisDescription = static_cast< const char* >( getHisDescription( ) );
196 rv = prefs->SetCharPref (hisPrefName.get(), pHisDescription);
197 NS_ENSURE_SUCCESS(rv, rv);
199 return rv;
202 // nsAbDirectoryDataSource
204 #define NS_RDF_CONTRACTID "@mozilla.org/rdf"
205 #define NS_RDF_DATASOURCE_CONTRACTID NS_RDF_CONTRACTID "/datasource;1"
206 #define NS_RDF_DATASOURCE_CONTRACTID_PREFIX NS_RDF_DATASOURCE_CONTRACTID "?name="
207 #define NS_ABDIRECTORYDATASOURCE_CONTRACTID \
208 NS_RDF_DATASOURCE_CONTRACTID_PREFIX "addressdirectory"
209 #define database_uri "@mozilla.org/rdf/datasource;1?name=addressdirectory"
210 #define NS_ABDIRECTORYDATASOURCE_CID \
211 { /* 0A79186D-F754-11d2-A2DA-001083003D0C */ \
212 0xa79186d, 0xf754, 0x11d2, \
213 {0xa2, 0xda, 0x0, 0x10, 0x83, 0x0, 0x3d, 0xc} \
216 // -------------------------------------------------------------------------
217 // Case where we get a parent uri, and need to list its children.
218 static nsresult getSubsFromParent(const rtl::OString& aParent, nsIEnumerator **aSubs)
221 if (aSubs == nsnull) { return NS_ERROR_NULL_POINTER ; }
222 *aSubs = nsnull ;
223 nsresult retCode = NS_OK ;
224 nsCOMPtr<nsISupportsArray> array ;
226 NS_NewISupportsArray(getter_AddRefs(array)) ;
228 retCode = insertPABDescription();
229 NS_ENSURE_SUCCESS(retCode, retCode) ;
231 nsCOMPtr<nsIRDFService> rdfService(do_GetService(kRDFServiceCID, &retCode)) ;
232 NS_ENSURE_SUCCESS(retCode, retCode) ;
233 nsCOMPtr<nsIRDFResource> rdfResource ;
235 nsCOMPtr<nsIRDFDataSource> rdfDirectory ;
237 rtl::OString dir("rdf:addressdirectory");
238 retCode = rdfService->GetDataSource(dir.getStr(),getter_AddRefs(rdfDirectory)) ;
242 OSL_TRACE("uri: %s\n", aParent.getStr()) ;
243 retCode = rdfService->GetResource(nsDependentCString(aParent.getStr(),aParent.getLength()), getter_AddRefs(rdfResource)) ;
244 NS_ENSURE_SUCCESS(retCode, retCode) ;
245 nsCOMPtr<nsIAbDirectory> directory = do_QueryInterface(rdfResource, &retCode) ;
246 nsCOMPtr<nsISimpleEnumerator> tempEnum ;
248 NS_ENSURE_SUCCESS(retCode, retCode) ;
249 retCode = directory->GetChildNodes(getter_AddRefs(tempEnum)) ;
250 NS_ENSURE_SUCCESS(retCode, retCode) ;
252 PRBool hasMore = PR_TRUE ;
253 while (NS_SUCCEEDED(tempEnum->HasMoreElements(&hasMore)) && hasMore) {
254 nsCOMPtr<nsIAbDirectory> element ;
255 retCode = tempEnum->GetNext(getter_AddRefs(element)) ;
256 if (NS_SUCCEEDED(retCode))
258 array->AppendElement(element) ;
259 enumSubs(element,array);
263 array->Enumerate(aSubs) ;
264 return retCode ;
267 static nsresult enumSubs(nsISimpleEnumerator * subDirs,nsISupportsArray * array)
269 PRBool hasMore = PR_TRUE ;
270 nsresult rv = NS_OK ;
271 while (NS_SUCCEEDED(subDirs->HasMoreElements(&hasMore)) && hasMore) {
272 nsCOMPtr<nsISupports> newDirSupports ;
274 rv = subDirs->GetNext(getter_AddRefs(newDirSupports)) ;
275 NS_ENSURE_SUCCESS(rv, rv);
276 nsCOMPtr<nsIAbDirectory> childDir = do_QueryInterface(newDirSupports, &rv) ;
277 NS_ENSURE_SUCCESS(rv, rv);
278 array->AppendElement(childDir);
280 rv = enumSubs(childDir,array);
282 return rv;
285 // Case where we get a factory uri and need to have it build the directories.
286 static nsresult getSubsFromFactory(const rtl::OString& aFactory, nsIEnumerator **aSubs)
288 if (aSubs == nsnull) { return NS_ERROR_NULL_POINTER ; }
289 *aSubs = nsnull ;
290 nsresult retCode = NS_OK ;
292 nsCOMPtr<nsIAbDirFactoryService> dirFactoryService = do_GetService(NS_ABDIRFACTORYSERVICE_CONTRACTID, &retCode);
293 NS_ENSURE_SUCCESS(retCode, retCode) ;
294 nsCOMPtr<nsIAbDirFactory> dirFactory ;
296 retCode = dirFactoryService->GetDirFactory(aFactory.getStr(), getter_AddRefs(dirFactory)) ;
297 NS_ENSURE_SUCCESS (retCode, retCode) ;
298 nsCOMPtr <nsIAbDirectoryProperties> properties;
299 properties = do_CreateInstance(NS_ABDIRECTORYPROPERTIES_CONTRACTID, &retCode);
300 NS_ENSURE_SUCCESS(retCode, retCode) ;
302 retCode = properties->SetURI(aFactory.getStr());
303 NS_ENSURE_SUCCESS(retCode,retCode);
305 nsCOMPtr<nsISimpleEnumerator> subDirs ;
306 retCode = dirFactory->CreateDirectory(properties, getter_AddRefs(subDirs));
307 NS_ENSURE_SUCCESS(retCode, retCode) ;
308 nsCOMPtr<nsISupportsArray> array ;
310 NS_NewISupportsArray(getter_AddRefs(array)) ;
311 retCode = enumSubs(subDirs,array);
312 NS_ENSURE_SUCCESS(retCode, retCode) ;
313 array->Enumerate(aSubs) ;
314 return retCode ;
317 // Case where the uri itself is the directory we're looking for.
318 static nsresult getSubsFromURI(const rtl::OString& aUri, nsIEnumerator **aSubs)
320 if (aSubs == nsnull) { return NS_ERROR_NULL_POINTER ; }
321 *aSubs = nsnull ;
322 nsresult retCode = NS_OK ;
324 nsCOMPtr<nsIRDFService> rdfService(do_GetService(kRDFServiceCID, &retCode)) ;
325 NS_ENSURE_SUCCESS(retCode, retCode) ;
326 nsCOMPtr<nsIRDFResource> rdfResource ;
328 retCode = rdfService->GetResource(nsDependentCString(aUri.getStr(),aUri.getLength()), getter_AddRefs(rdfResource)) ;
329 NS_ENSURE_SUCCESS(retCode, retCode) ;
330 retCode = rdfService->UnregisterResource(rdfResource) ;
331 NS_ENSURE_SUCCESS(retCode, retCode) ;
333 nsCOMPtr<nsIAbDirectory> directory = do_QueryInterface(rdfResource, &retCode) ;
335 NS_ENSURE_SUCCESS(retCode, retCode) ;
336 nsAutoString nameAB ;
338 nameAB.AssignWithConversion("LDAP Directory") ;
339 directory->SetDirName(nameAB.get()) ;
340 nsCOMPtr<nsISupportsArray> array ;
342 NS_NewISupportsArray(getter_AddRefs(array)) ;
343 array->AppendElement(directory) ;
344 array->Enumerate(aSubs) ;
345 return retCode ;
348 namespace
350 static void lcl_setNoAddressBookFoundError( ErrorDescriptor& _rError, OConnection& _rCon, MozillaProductType _eProductType,
351 sal_Bool bGivenURI )
353 sal_uInt16 nAddressBookNameRes = 0;
354 if ( !bGivenURI && _eProductType == MozillaProductType_Mozilla)
356 nAddressBookNameRes = STR_MOZILLA_ADDRESSBOOKS;
358 else
360 if ( _eProductType == MozillaProductType_Thunderbird )
362 nAddressBookNameRes = STR_THUNDERBIRD_ADDRESSBOOKS;
364 else
366 if ( _rCon.usesFactory() )
368 if ( _rCon.isOutlookExpress() )
370 nAddressBookNameRes = STR_OE_ADDRESSBOOK;
372 else
374 nAddressBookNameRes = STR_OUTLOOK_MAPI_ADDRESSBOOK;
377 else
379 OSL_ENSURE( !_rCon.isLDAP(), "lcl_setNoAddressBookFoundError: not to be called for LDAP connections!" );
380 nAddressBookNameRes = STR_MOZILLA_ADDRESSBOOKS;
384 _rError.set( 0, ErrorCondition::AB_ADDRESSBOOK_NOT_FOUND, _rCon.getResources().getResourceString( nAddressBookNameRes ) );
388 nsresult getTableStringsProxied(const sal_Char* sAbURI, sal_Int32 *nDirectoryType,MNameMapper *nmap,
389 ::std::vector< ::rtl::OUString >* _rStrings,
390 ::std::vector< ::rtl::OUString >* _rTypes,
391 sal_Int32* pErrorId )
393 if (!sAbURI || !nmap || !_rStrings || !_rTypes || !pErrorId)
395 return NS_ERROR_NULL_POINTER;
397 OSL_TRACE("Using URI %s to getTables()\n",sAbURI );
399 // Get the mozilla addressbooks from the base directory.
400 nsresult rv = NS_OK;
401 nsCOMPtr<nsIEnumerator> subDirectories;
403 switch(*nDirectoryType)
405 case SDBCAddress::Mozilla:
406 rv = getSubsFromParent(sAbURI, getter_AddRefs(subDirectories)) ;
407 break;
408 case SDBCAddress::Outlook:
409 case SDBCAddress::OutlookExp:
410 rv = getSubsFromFactory(sAbURI, getter_AddRefs(subDirectories)) ;
411 break;
412 case SDBCAddress::LDAP:
413 rv = getSubsFromURI(sAbURI, getter_AddRefs(subDirectories)) ;
414 break;
415 default:
416 rv = getSubsFromParent(sAbURI, getter_AddRefs(subDirectories));
418 NS_ENSURE_SUCCESS(rv, rv);
420 // At this point we have a list containing the nsIAbDirectory we need to map as tables
421 rv = subDirectories -> First();
422 NS_ENSURE_SUCCESS(rv, rv);
424 PRUnichar *name = nsnull;
425 PRBool bIsMailList = PR_FALSE;
427 ::rtl::OUString aTableName;
428 nsCOMPtr<nsIRDFService> rdfService(do_GetService(kRDFServiceCID, &rv)) ;
429 NS_ENSURE_SUCCESS(rv, rv) ;
431 nmap->reset();
432 do {
433 nsCOMPtr<nsIAbDirectory> subDirectory;
435 nsCOMPtr<nsISupports> item;
436 rv = subDirectories -> CurrentItem(getter_AddRefs(item));
437 if ( NS_FAILED( rv ) )
439 *pErrorId = STR_COULD_NOT_RETRIEVE_AB_ENTRY;
440 return NS_ERROR_FAILURE;
443 subDirectory = do_QueryInterface(item, &rv);
445 subDirectory -> GetIsMailList(&bIsMailList);
446 // For now we're not interested in mailing lists.
447 rv = subDirectory -> GetDirName(&name);
448 if ( NS_FAILED( rv ) ) {
449 *pErrorId = STR_COULD_NOT_GET_DIRECTORY_NAME;
450 return NS_ERROR_FAILURE;
452 MTypeConverter::prUnicharToOUString(name, aTableName);
453 OSL_TRACE("TableName = >%s<\n", OUtoCStr( aTableName ) );
455 ODatabaseMetaDataResultSet::ORow aRow(3);
457 // Insert table into map
458 if ( aTableName.getLength() == 0 )
459 aTableName = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("AddressBook"));
461 OSL_TRACE("TableName = >%s<\n", OUtoCStr( aTableName ) );
463 rv = nmap->add( aTableName, subDirectory);
464 nsCOMPtr<nsIRDFResource> rdfResource = do_QueryInterface(subDirectory, &rv) ;
465 if (!NS_FAILED(rv))
466 rdfService->UnregisterResource(rdfResource);
468 if (!NS_FAILED(rv)) //failed means we have added this directory
470 //map mailing lists as views
471 _rStrings->push_back( aTableName ); // Table name
472 if (!bIsMailList) {
473 ::rtl::OUString aTableType(::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("TABLE")));
474 _rTypes->push_back( aTableType ); // Table type
476 else
478 ::rtl::OUString aTableType(::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("VIEW")));
479 _rTypes->push_back( aTableType ); // Table type
482 rv = subDirectories -> Next();
483 } while ( rv == NS_OK);
485 return( NS_OK );
487 sal_Bool MDatabaseMetaDataHelper::getTableStrings( OConnection* _pCon,
488 ::std::vector< ::rtl::OUString >& _rStrings,
489 ::std::vector< ::rtl::OUString >& _rTypes)
491 sal_Bool bGivenURI;
492 ::rtl::OUString sAbURI;
493 ::rtl::OString sAbURIString;
495 OSL_TRACE( "IN MDatabaseMetaDataHelper::getTableStrings( 0x%08X, %s)\n", _pCon, _pCon->getForceLoadTables()?"True":"False" );
497 ::osl::MutexGuard aGuard( m_aMetaMutex );
498 // Only do the query if we have to - performance degrades otherwise
499 if ( ! _pCon->getForceLoadTables() && m_aTableNames.size() > 0 ) {
500 _rStrings = m_aTableNames;
501 _rTypes = m_aTableTypes;
502 return( sal_True );
505 m_aTableNames.clear();
506 m_aTableTypes.clear();
508 sAbURI = _pCon->getMozURI();
510 OSL_TRACE("AbURI = %s\n", OUtoCStr( sAbURI ) );
511 if ( sAbURI.getLength() == 0 )
512 bGivenURI = sal_False;
513 else {
514 sAbURIString = ::rtl::OUStringToOString( sAbURI,
515 RTL_TEXTENCODING_ASCII_US);
516 bGivenURI = sal_True;
519 if ( bGivenURI )
520 OSL_TRACE("Using URI %s to getTables()\n", sAbURIString.getStr() );
521 else
522 OSL_TRACE("Using URI of ROOTDIR to getTables()\n");
524 // Get the mozilla addressbooks from the base directory.
525 nsresult rv = NS_OK;
526 nsCOMPtr<nsIEnumerator> subDirectories;
527 sal_Int32 nDirectoryType=0;
528 m_ProductType = MozillaProductType_Mozilla;
529 m_ProfileName = _pCon->getMozProfile();
532 if (_pCon->isThunderbird())
534 if (!bGivenURI)
535 sAbURIString = s_pADDRESSBOOKROOTDIR;
536 nDirectoryType = SDBCAddress::ThunderBird;
537 m_ProductType = MozillaProductType_Thunderbird;
539 else if (!bGivenURI)
541 sAbURIString = s_pADDRESSBOOKROOTDIR;
542 nDirectoryType = SDBCAddress::Mozilla;
544 else
546 if (_pCon->usesFactory())
548 nDirectoryType = SDBCAddress::Outlook;
550 else
552 if (_pCon->isLDAP())
554 nDirectoryType = SDBCAddress::LDAP;
556 else
558 sAbURIString = s_pADDRESSBOOKROOTDIR;
559 nDirectoryType = SDBCAddress::Mozilla;
564 if ( !_pCon->isLDAP() && !m_bProfileExists )
565 { // no need to boot up a Mozilla profile for an LDAP connection
566 Reference<XMozillaBootstrap> xMozillaBootstrap;
567 Reference<XMultiServiceFactory> xFactory = ::comphelper::getProcessServiceFactory();
568 OSL_ENSURE( xFactory.is(), "can't get service factory" );
569 Reference<XInterface> xInstance = xFactory->createInstance(::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.mozilla.MozillaBootstrap")) );
570 OSL_ENSURE( xInstance.is(), "failed to create instance" );
571 xMozillaBootstrap = Reference<XMozillaBootstrap>(xInstance,UNO_QUERY);
572 m_bProfileExists = sal_False;
573 //If there are no profiles for this product
574 //Or the given profile name does not found
575 //We will reaise a "No Addressbook Directories Exist" error
576 if ( xMozillaBootstrap->getProfileCount(m_ProductType) == 0 ||
577 ( m_ProfileName.getLength() && !(xMozillaBootstrap->getProfilePath(m_ProductType,m_ProfileName).getLength())))
578 m_bProfileExists = sal_False;
579 else
580 if (xMozillaBootstrap->bootupProfile(m_ProductType,m_ProfileName) > 0)
581 m_bProfileExists = sal_True;
584 if ( !m_bProfileExists
585 && !_pCon->isLDAP()
586 && ( ( nDirectoryType == SDBCAddress::Mozilla )
587 || ( nDirectoryType == SDBCAddress::ThunderBird )
591 lcl_setNoAddressBookFoundError( m_aError, *_pCon, m_ProductType, bGivenURI );
592 return sal_False;
595 MNameMapper *nmap = _pCon->getNameMapper();
596 nmap->reset();
598 sal_Int32 nErrorResourceId( 0 );
600 MNSMozabProxy xMProxy;
601 RunArgs args;
602 args.funcIndex = ProxiedFunc::FUNC_GET_TABLE_STRINGS;
603 args.argCount = 6;
604 args.arg1 = (void*)sAbURIString.getStr();
605 args.arg2 = (void*)&nDirectoryType;
606 args.arg3 = (void*)nmap;
607 args.arg4 = (void*)&m_aTableNames;
608 args.arg5 = (void*)&m_aTableTypes;
609 args.arg6 = (void*)&nErrorResourceId;
610 rv = xMProxy.StartProxy(&args,m_ProductType,m_ProfileName);
611 m_aError.setResId( static_cast<sal_uInt16>(nErrorResourceId) );
613 if (NS_FAILED(rv))
615 if ( nErrorResourceId == 0 )
616 m_aError.setResId( STR_UNSPECIFIED_ERROR );
617 return sal_False;
619 OSL_TRACE( "\tOUT MDatabaseMetaDataHelper::getTableStrings()\n" );
620 _rStrings = m_aTableNames;
621 _rTypes = m_aTableTypes;
623 _pCon->setForceLoadTables(sal_False);
624 return( sal_True );
627 sal_Bool MDatabaseMetaDataHelper::getTables( OConnection* _pCon,
628 const ::rtl::OUString& tableNamePattern,
629 const Sequence< ::rtl::OUString >& types,
630 ODatabaseMetaDataResultSet::ORows& _rRows)
633 static ODatabaseMetaDataResultSet::ORows aRows;
635 OSL_TRACE( "IN MDatabaseMetaDataHelper::getTables()\n" );
636 ::osl::MutexGuard aGuard( m_aMetaMutex );
638 ODatabaseMetaDataResultSet::ORows().swap(aRows); // this makes real clear where memory is freed as well
639 aRows.clear();
641 ::std::vector< ::rtl::OUString > tables;
642 ::std::vector< ::rtl::OUString > tabletypes;
643 ::rtl::OUString matchAny = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("%"));;
645 if ( !getTableStrings( _pCon, tables,tabletypes ) )
646 return sal_False;
648 for ( size_t i = 0; i < tables.size(); i++ ) {
649 ODatabaseMetaDataResultSet::ORow aRow(3);
651 ::rtl::OUString aTableName = tables[i];
652 ::rtl::OUString aTableType = tabletypes[i];
653 OSL_TRACE("TableName = >%s<\n", OUtoCStr( aTableName ) );
656 // return tables to caller
657 if (match( tableNamePattern, aTableName, '\0' ) &&
658 ( 0 == types.getLength() || //types is null or types include aTableType or types include "%"
659 0 != ::comphelper::findValue( types, aTableType, sal_True ).getLength() ||
660 0 != ::comphelper::findValue( types, matchAny, sal_True ).getLength())) {
661 if ( aTableName.getLength() == 0 ) {
662 aTableName = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("AddressBook"));
665 OSL_TRACE( "TableName = %s ; TableType = %s\n", OUtoCStr(aTableName), OUtoCStr(aTableType) );
667 aRow.push_back( new ORowSetValueDecorator( aTableName ) ); // Table name
668 aRow.push_back( new ORowSetValueDecorator( aTableType ) ); // Table type
669 aRow.push_back( ODatabaseMetaDataResultSet::getEmptyValue() ); // Remarks
670 aRows.push_back(aRow);
674 OSL_TRACE( "\tOUT MDatabaseMetaDataHelper::getTables()\n" );
675 _rRows = aRows;
676 return(sal_True);
680 sal_Bool
681 MDatabaseMetaDataHelper::testLDAPConnection( OConnection* _pCon )
683 const sal_Unicode QUERY_CHAR = '?';
684 const sal_Char* MOZ_SCHEMA = "moz-abldapdirectory://";
685 const sal_Char* LDAP_SCHEMA = "ldap://";
687 rtl::OString sAbURI;
688 rtl::OUString sAbBindDN;
689 rtl::OUString sAbPassword;
690 sal_Bool useSSL = _pCon->getUseSSL();
692 nsresult rv(0);
694 sAbURI = OUStringToOString( _pCon->getMozURI(), RTL_TEXTENCODING_ASCII_US );
695 sAbBindDN = _pCon->getBindDN();
696 sAbPassword = _pCon->getPassword();
699 sal_Int32 pos = sAbURI.indexOf( MOZ_SCHEMA );
700 if ( pos != -1 ) {
701 sAbURI = sAbURI.replaceAt (pos, rtl_str_getLength( MOZ_SCHEMA ), ::rtl::OString(LDAP_SCHEMA) );
704 pos = sAbURI.indexOf( QUERY_CHAR );
705 if ( pos != -1 ) {
706 sal_Int32 len = sAbURI.getLength();
707 sAbURI = sAbURI.replaceAt( pos, len - pos, ::rtl::OString("") );
709 const sal_Unicode* bindDN=nsnull;
710 if (sAbBindDN.getLength() != 0)
712 bindDN = sAbBindDN.getStr();
715 RunArgs args;
716 args.funcIndex = ProxiedFunc::FUNC_TESTLDAP_INIT_LDAP;
717 args.argCount = 4;
718 args.arg1 = (void*)sAbURI.getStr();
719 args.arg2 = (void*)bindDN;
720 args.arg3 = (void*)sAbPassword.getStr();
721 args.arg4 = (void*)&useSSL;
723 MNSMozabProxy xMProxy;
724 rv = xMProxy.StartProxy( &args, m_ProductType, ::rtl::OUString() );
725 if ( NS_SUCCEEDED( rv ) ) //Init LDAP,pass OUString() to StarProxy to ignore profile switch
727 args.funcIndex = ProxiedFunc::FUNC_TESTLDAP_IS_LDAP_CONNECTED;
728 TimeValue timeValue = { 1, 0 }; // 1 * 30 Seconds timeout
729 sal_Int32 times=0;
730 while ( times++ < 30 )
732 rv = xMProxy.StartProxy( &args, m_ProductType, ::rtl::OUString() );
733 if ( NS_SUCCEEDED( rv ) )
734 // connected successfully
735 break;
737 if ( rv == (nsresult)PR_NOT_CONNECTED_ERROR )
738 // connection failed
739 break;
741 // not yet decided - continue waiting
742 osl_waitThread( &timeValue );
745 m_aError.setResId( STR_COULD_NOT_CONNECT_LDAP );
746 return NS_SUCCEEDED( rv ) ? sal_True : sal_False;
749 sal_Bool MDatabaseMetaDataHelper::NewAddressBook(OConnection* _pCon,const ::rtl::OUString & aTableName)
751 sal_Bool bIsMozillaAB;
753 bIsMozillaAB = !_pCon->usesFactory() && ! _pCon->isLDAP();
755 if ( !bIsMozillaAB )
757 m_aError.setResId( STR_NO_TABLE_CREATION_SUPPORT );
758 return sal_False;
760 else
761 OSL_TRACE("Create table\n");
763 // Get the mozilla addressbooks from the base directory.
764 nsresult rv = NS_OK;
766 MNSMozabProxy xMProxy;
767 RunArgs args;
768 args.funcIndex = ProxiedFunc::FUNC_NEW_ADDRESS_BOOK;
769 args.argCount = 1;
770 args.arg1 = (void*)&aTableName;
771 rv = xMProxy.StartProxy(&args,m_ProductType,m_ProfileName);
773 _pCon->setForceLoadTables(sal_True); //force reload table next time
774 if (rv == NS_ERROR_FILE_IS_LOCKED)
776 m_aError.setResId( STR_MOZILLA_IS_RUNNING );
778 else if (NS_FAILED(rv))
780 m_aError.set( STR_COULD_NOT_CREATE_ADDRESSBOOK, 0, ::rtl::OUString::valueOf( sal_Int32(rv), 16 ) );
782 OSL_TRACE( "OUT MDatabaseMetaDataHelper::NewAddressBook()\n" );
783 return( NS_SUCCEEDED(rv) ? sal_True : sal_False );
785 nsresult NewAddressBook(const ::rtl::OUString * aName)
787 if (isProfileLocked(NULL))
788 return NS_ERROR_FILE_IS_LOCKED;
789 nsresult rv;
790 nsCOMPtr<nsIAbDirectoryProperties> aProperties = do_CreateInstance(NS_ABDIRECTORYPROPERTIES_CONTRACTID, &rv);
791 NS_ENSURE_ARG_POINTER(aProperties);
792 const ::rtl::OUString& uName = *aName;
793 nsString nsName;
794 MTypeConverter::ouStringToNsString(uName,nsName);
795 aProperties->SetDescription(nsName);
797 nsCOMPtr<nsIRDFService> rdfService = do_GetService (kRDFServiceCID, &rv);
798 NS_ENSURE_SUCCESS(rv, rv);
800 nsCOMPtr<nsIRDFResource> parentResource;
801 rv = rdfService->GetResource(NS_LITERAL_CSTRING(kAllDirectoryRoot), getter_AddRefs(parentResource));
802 NS_ENSURE_SUCCESS(rv, rv);
804 nsCOMPtr<nsIAbDirectory> parentDir = do_QueryInterface(parentResource, &rv);
805 NS_ENSURE_SUCCESS(rv, rv);
807 rv = parentDir->CreateNewDirectory(aProperties);
808 return rv;