Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / connectivity / source / drivers / mozab / mozillasrc / MDatabaseMetaDataHelper.cxx
blob04b25a247ad69cda18dddd96bf235174b571ebbd
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 // Mozilla includes.
22 #include "MNSInclude.hxx"
24 #include "resource/mozab_res.hrc"
25 #include "MDatabaseMetaDataHelper.hxx"
26 #include "MTypeConverter.hxx"
27 #include "MConfigAccess.hxx"
28 #include <connectivity/dbexception.hxx>
29 #include <comphelper/uno3.hxx>
30 #include <comphelper/sequence.hxx>
31 #include <osl/mutex.hxx>
32 #include <osl/conditn.hxx>
34 #include "pre_include_mozilla.h"
35 #include <nsIAbDirFactoryService.h>
36 #include "post_include_mozilla.h"
38 #include "bootstrap/MNSInit.hxx"
39 #include <MNameMapper.hxx>
40 #include "MNSMozabProxy.hxx"
41 #include <MNSDeclares.hxx>
43 static ::osl::Mutex m_aMetaMutex;
45 #include <osl/diagnose.h>
46 #include <com/sun/star/uno/Reference.hxx>
47 #include <com/sun/star/uno/Sequence.hxx>
48 #include <com/sun/star/uno/XInterface.hpp>
49 #include <com/sun/star/uno/Exception.hpp>
50 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
51 #include <com/sun/star/beans/Property.hpp>
52 #include <com/sun/star/beans/XPropertySet.hpp>
53 #include <com/sun/star/beans/XPropertySetInfo.hpp>
54 #include <com/sun/star/beans/PropertyAttribute.hpp>
55 #include <com/sun/star/sdb/ErrorCondition.hpp>
56 #include <comphelper/processfactory.hxx>
57 #include <com/sun/star/mozilla/XMozillaBootstrap.hpp>
60 #if OSL_DEBUG_LEVEL > 0
61 # define OUtoCStr( x ) ( OUStringToOString ( (x), RTL_TEXTENCODING_ASCII_US).getStr())
62 #else /* OSL_DEBUG_LEVEL */
63 # define OUtoCStr( x ) ("dummy")
64 #endif /* OSL_DEBUG_LEVEL */
67 static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
68 static NS_DEFINE_CID(kAbDirFactoryServiceCID, NS_ABDIRFACTORYSERVICE_CID);
69 static NS_DEFINE_CID(kAddrBookSessionCID, NS_ADDRBOOKSESSION_CID);
71 using namespace connectivity::mozab;
72 using namespace connectivity;
73 using namespace ::com::sun::star::uno;
74 using namespace com::sun::star::lang;
75 using namespace ::com::sun::star::beans;
76 using namespace ::com::sun::star::sdbc;
77 using namespace com::sun::star::mozilla;
78 using namespace com::sun::star::sdb;
80 namespace connectivity
82 namespace mozab
84 static const char * const s_pADDRESSBOOKROOTDIR = "moz-abdirectory://";
88 extern sal_Bool MNS_Init(sal_Bool& aProfileExists);
91 MDatabaseMetaDataHelper::MDatabaseMetaDataHelper()
92 :m_bProfileExists(sal_False)
94 OSL_TRACE( "IN MDatabaseMetaDataHelper::MDatabaseMetaDataHelper()" );
96 OSL_TRACE( "\tOUT MDatabaseMetaDataHelper::MDatabaseMetaDataHelper()" );
100 MDatabaseMetaDataHelper::~MDatabaseMetaDataHelper()
102 m_aTableNames.clear();
103 m_aTableTypes.clear();
105 if (m_bProfileExists)
107 Reference<XMozillaBootstrap> xMozillaBootstrap;
108 Reference<XMultiServiceFactory> xFactory = ::comphelper::getProcessServiceFactory();
109 OSL_ENSURE( xFactory.is(), "can't get service factory" );
111 Reference<XInterface> xInstance = xFactory->createInstance("com.sun.star.mozilla.MozillaBootstrap");
112 OSL_ENSURE( xInstance.is(), "failed to create instance" );
113 xMozillaBootstrap = Reference<XMozillaBootstrap>(xInstance,UNO_QUERY);
114 m_bProfileExists = xMozillaBootstrap->shutdownProfile() > 0;
117 OSL_TRACE( "IN/OUT MDatabaseMetaDataHelper::~MDatabaseMetaDataHelper()" );
120 static nsresult enumSubs(nsISimpleEnumerator * subDirs,nsISupportsArray * array);
121 //enum all sub folders
122 static nsresult enumSubs(nsIAbDirectory * parentDir,nsISupportsArray * array)
124 nsCOMPtr<nsISimpleEnumerator> subDirectories;
125 nsresult rv = parentDir->GetChildNodes(getter_AddRefs(subDirectories));
126 if (NS_FAILED(rv)) { return rv; }
128 rv = enumSubs(subDirectories,array);
129 return rv;
132 #include <prmem.h>
133 static nsresult insertPABDescription()
135 nsresult rv = NS_OK ;
136 // mozilla loads the descriptions of pab and history from a file named addressbook.properties
137 // we never deliver it with OOo,so mozab does not auot load personal addressbook and Collected addresses
138 // If we want to deliver addressbook.properties with OOo,we have to deal with hole mozilla chrome mechanism.
139 // So I insert the descriptiones as user prefes,they will cover the default value.
141 //"Personal Address Book" "Collected Addresses" are loaded from officecfg
142 nsCOMPtr<nsIPref> prefs = do_GetService(NS_PREF_CONTRACTID, &rv);
143 NS_ENSURE_SUCCESS(rv, rv);
145 nsCAutoString pabPrefName(NS_LITERAL_CSTRING("ldap_2.servers.pab.description"));
147 sal_Bool bSetted=sal_False;
148 char *value = nsnull;
149 if (0 == prefs->CopyCharPref(pabPrefName.get(), &value))
151 if (!(value == nsnull || value[0] == 0))
153 bSetted = sal_True;
156 PR_FREEIF (value);
157 value=nsnull;
158 if (bSetted == sal_True)
160 const char* pPabDescription = static_cast< const char* >( getPabDescription( ) );
161 rv = prefs->SetCharPref (pabPrefName.get(), pPabDescription);
162 NS_ENSURE_SUCCESS(rv, rv);
165 nsCAutoString hisPrefName(NS_LITERAL_CSTRING("ldap_2.servers.history.description"));
167 bSetted=sal_False;
168 if (0 == prefs->CopyCharPref(hisPrefName.get(), &value))
170 if (!(value == nsnull || value[0] == 0))
172 bSetted = sal_True;
175 PR_FREEIF (value);
176 value=nsnull;
177 if (bSetted == sal_True)
179 const char* pHisDescription = static_cast< const char* >( getHisDescription( ) );
181 rv = prefs->SetCharPref (hisPrefName.get(), pHisDescription);
182 NS_ENSURE_SUCCESS(rv, rv);
184 return rv;
187 // nsAbDirectoryDataSource
189 #define NS_RDF_CONTRACTID "@mozilla.org/rdf"
190 #define NS_RDF_DATASOURCE_CONTRACTID NS_RDF_CONTRACTID "/datasource;1"
191 #define NS_RDF_DATASOURCE_CONTRACTID_PREFIX NS_RDF_DATASOURCE_CONTRACTID "?name="
192 #define NS_ABDIRECTORYDATASOURCE_CONTRACTID \
193 NS_RDF_DATASOURCE_CONTRACTID_PREFIX "addressdirectory"
194 #define database_uri "@mozilla.org/rdf/datasource;1?name=addressdirectory"
195 #define NS_ABDIRECTORYDATASOURCE_CID \
196 { /* 0A79186D-F754-11d2-A2DA-001083003D0C */ \
197 0xa79186d, 0xf754, 0x11d2, \
198 {0xa2, 0xda, 0x0, 0x10, 0x83, 0x0, 0x3d, 0xc} \
202 // Case where we get a parent uri, and need to list its children.
203 static nsresult getSubsFromParent(const OString& aParent, nsIEnumerator **aSubs)
206 if (aSubs == nsnull) { return NS_ERROR_NULL_POINTER ; }
207 *aSubs = nsnull ;
208 nsCOMPtr<nsISupportsArray> array ;
210 NS_NewISupportsArray(getter_AddRefs(array)) ;
212 nsresult retCode = insertPABDescription();
213 NS_ENSURE_SUCCESS(retCode, retCode) ;
215 nsCOMPtr<nsIRDFService> rdfService(do_GetService(kRDFServiceCID, &retCode)) ;
216 NS_ENSURE_SUCCESS(retCode, retCode) ;
217 nsCOMPtr<nsIRDFResource> rdfResource ;
219 nsCOMPtr<nsIRDFDataSource> rdfDirectory ;
221 OString dir("rdf:addressdirectory");
222 retCode = rdfService->GetDataSource(dir.getStr(),getter_AddRefs(rdfDirectory)) ;
226 OSL_TRACE("uri: %s\n", aParent.getStr()) ;
227 retCode = rdfService->GetResource(nsDependentCString(aParent.getStr(),aParent.getLength()), getter_AddRefs(rdfResource)) ;
228 NS_ENSURE_SUCCESS(retCode, retCode) ;
229 nsCOMPtr<nsIAbDirectory> directory = do_QueryInterface(rdfResource, &retCode) ;
230 nsCOMPtr<nsISimpleEnumerator> tempEnum ;
232 NS_ENSURE_SUCCESS(retCode, retCode) ;
233 retCode = directory->GetChildNodes(getter_AddRefs(tempEnum)) ;
234 NS_ENSURE_SUCCESS(retCode, retCode) ;
236 PRBool hasMore = PR_TRUE ;
237 while (NS_SUCCEEDED(tempEnum->HasMoreElements(&hasMore)) && hasMore) {
238 nsCOMPtr<nsIAbDirectory> element ;
239 retCode = tempEnum->GetNext(getter_AddRefs(element)) ;
240 if (NS_SUCCEEDED(retCode))
242 array->AppendElement(element) ;
243 enumSubs(element,array);
247 array->Enumerate(aSubs) ;
248 return retCode ;
251 static nsresult enumSubs(nsISimpleEnumerator * subDirs,nsISupportsArray * array)
253 PRBool hasMore = PR_TRUE ;
254 nsresult rv = NS_OK ;
255 while (NS_SUCCEEDED(subDirs->HasMoreElements(&hasMore)) && hasMore) {
256 nsCOMPtr<nsISupports> newDirSupports ;
258 rv = subDirs->GetNext(getter_AddRefs(newDirSupports)) ;
259 NS_ENSURE_SUCCESS(rv, rv);
260 nsCOMPtr<nsIAbDirectory> childDir = do_QueryInterface(newDirSupports, &rv) ;
261 NS_ENSURE_SUCCESS(rv, rv);
262 array->AppendElement(childDir);
264 rv = enumSubs(childDir,array);
266 return rv;
269 // Case where we get a factory uri and need to have it build the directories.
270 static nsresult getSubsFromFactory(const OString& aFactory, nsIEnumerator **aSubs)
272 if (aSubs == nsnull) { return NS_ERROR_NULL_POINTER ; }
273 *aSubs = nsnull ;
274 nsresult retCode = NS_OK ;
276 nsCOMPtr<nsIAbDirFactoryService> dirFactoryService = do_GetService(NS_ABDIRFACTORYSERVICE_CONTRACTID, &retCode);
277 NS_ENSURE_SUCCESS(retCode, retCode) ;
278 nsCOMPtr<nsIAbDirFactory> dirFactory ;
280 retCode = dirFactoryService->GetDirFactory(aFactory.getStr(), getter_AddRefs(dirFactory)) ;
281 NS_ENSURE_SUCCESS (retCode, retCode) ;
282 nsCOMPtr <nsIAbDirectoryProperties> properties;
283 properties = do_CreateInstance(NS_ABDIRECTORYPROPERTIES_CONTRACTID, &retCode);
284 NS_ENSURE_SUCCESS(retCode, retCode) ;
286 retCode = properties->SetURI(aFactory.getStr());
287 NS_ENSURE_SUCCESS(retCode,retCode);
289 nsCOMPtr<nsISimpleEnumerator> subDirs ;
290 retCode = dirFactory->CreateDirectory(properties, getter_AddRefs(subDirs));
291 NS_ENSURE_SUCCESS(retCode, retCode) ;
292 nsCOMPtr<nsISupportsArray> array ;
294 NS_NewISupportsArray(getter_AddRefs(array)) ;
295 retCode = enumSubs(subDirs,array);
296 NS_ENSURE_SUCCESS(retCode, retCode) ;
297 array->Enumerate(aSubs) ;
298 return retCode ;
301 // Case where the uri itself is the directory we're looking for.
302 static nsresult getSubsFromURI(const OString& aUri, nsIEnumerator **aSubs)
304 if (aSubs == nsnull) { return NS_ERROR_NULL_POINTER ; }
305 *aSubs = nsnull ;
306 nsresult retCode = NS_OK ;
308 nsCOMPtr<nsIRDFService> rdfService(do_GetService(kRDFServiceCID, &retCode)) ;
309 NS_ENSURE_SUCCESS(retCode, retCode) ;
310 nsCOMPtr<nsIRDFResource> rdfResource ;
312 retCode = rdfService->GetResource(nsDependentCString(aUri.getStr(),aUri.getLength()), getter_AddRefs(rdfResource)) ;
313 NS_ENSURE_SUCCESS(retCode, retCode) ;
314 retCode = rdfService->UnregisterResource(rdfResource) ;
315 NS_ENSURE_SUCCESS(retCode, retCode) ;
317 nsCOMPtr<nsIAbDirectory> directory = do_QueryInterface(rdfResource, &retCode) ;
319 NS_ENSURE_SUCCESS(retCode, retCode) ;
320 nsAutoString nameAB ;
322 nameAB.AssignWithConversion("LDAP Directory") ;
323 directory->SetDirName(nameAB.get()) ;
324 nsCOMPtr<nsISupportsArray> array ;
326 NS_NewISupportsArray(getter_AddRefs(array)) ;
327 array->AppendElement(directory) ;
328 array->Enumerate(aSubs) ;
329 return retCode ;
332 namespace
334 static void lcl_setNoAddressBookFoundError( ErrorDescriptor& _rError, OConnection& _rCon, MozillaProductType _eProductType,
335 sal_Bool bGivenURI )
337 sal_uInt16 nAddressBookNameRes = 0;
338 if ( !bGivenURI && _eProductType == MozillaProductType_Mozilla)
340 nAddressBookNameRes = STR_MOZILLA_ADDRESSBOOKS;
342 else
344 if ( _eProductType == MozillaProductType_Thunderbird )
346 nAddressBookNameRes = STR_THUNDERBIRD_ADDRESSBOOKS;
348 else
350 if ( _rCon.usesFactory() )
352 if ( _rCon.isOutlookExpress() )
354 nAddressBookNameRes = STR_OE_ADDRESSBOOK;
356 else
358 nAddressBookNameRes = STR_OUTLOOK_MAPI_ADDRESSBOOK;
361 else
363 OSL_ENSURE( !_rCon.isLDAP(), "lcl_setNoAddressBookFoundError: not to be called for LDAP connections!" );
364 nAddressBookNameRes = STR_MOZILLA_ADDRESSBOOKS;
368 _rError.set( 0, ErrorCondition::AB_ADDRESSBOOK_NOT_FOUND, _rCon.getResources().getResourceString( nAddressBookNameRes ) );
372 nsresult getTableStringsProxied(const sal_Char* sAbURI, sal_Int32 *nDirectoryType,MNameMapper *nmap,
373 ::std::vector< OUString >* _rStrings,
374 ::std::vector< OUString >* _rTypes,
375 sal_Int32* pErrorId )
377 if (!sAbURI || !nmap || !_rStrings || !_rTypes || !pErrorId)
379 return NS_ERROR_NULL_POINTER;
381 OSL_TRACE("Using URI %s to getTables()",sAbURI );
383 // Get the mozilla addressbooks from the base directory.
384 nsresult rv = NS_OK;
385 nsCOMPtr<nsIEnumerator> subDirectories;
387 switch(*nDirectoryType)
389 case SDBCAddress::Mozilla:
390 rv = getSubsFromParent(sAbURI, getter_AddRefs(subDirectories)) ;
391 break;
392 case SDBCAddress::Outlook:
393 case SDBCAddress::OutlookExp:
394 rv = getSubsFromFactory(sAbURI, getter_AddRefs(subDirectories)) ;
395 break;
396 case SDBCAddress::LDAP:
397 rv = getSubsFromURI(sAbURI, getter_AddRefs(subDirectories)) ;
398 break;
399 default:
400 rv = getSubsFromParent(sAbURI, getter_AddRefs(subDirectories));
402 NS_ENSURE_SUCCESS(rv, rv);
404 // At this point we have a list containing the nsIAbDirectory we need to map as tables
405 rv = subDirectories -> First();
406 NS_ENSURE_SUCCESS(rv, rv);
408 PRUnichar *name = nsnull;
409 PRBool bIsMailList = PR_FALSE;
411 OUString aTableName;
412 nsCOMPtr<nsIRDFService> rdfService(do_GetService(kRDFServiceCID, &rv)) ;
413 NS_ENSURE_SUCCESS(rv, rv) ;
415 nmap->reset();
416 do {
417 nsCOMPtr<nsIAbDirectory> subDirectory;
419 nsCOMPtr<nsISupports> item;
420 rv = subDirectories -> CurrentItem(getter_AddRefs(item));
421 if ( NS_FAILED( rv ) )
423 *pErrorId = STR_COULD_NOT_RETRIEVE_AB_ENTRY;
424 return NS_ERROR_FAILURE;
427 subDirectory = do_QueryInterface(item, &rv);
429 subDirectory -> GetIsMailList(&bIsMailList);
430 // For now we're not interested in mailing lists.
431 rv = subDirectory -> GetDirName(&name);
432 if ( NS_FAILED( rv ) ) {
433 *pErrorId = STR_COULD_NOT_GET_DIRECTORY_NAME;
434 return NS_ERROR_FAILURE;
436 MTypeConverter::prUnicharToOUString(name, aTableName);
437 OSL_TRACE("TableName = >%s<", OUtoCStr( aTableName ) );
439 ODatabaseMetaDataResultSet::ORow aRow(3);
441 // Insert table into map
442 if ( aTableName.isEmpty() )
443 aTableName = "AddressBook";
445 OSL_TRACE("TableName = >%s<", OUtoCStr( aTableName ) );
447 rv = nmap->add( aTableName, subDirectory);
448 nsCOMPtr<nsIRDFResource> rdfResource = do_QueryInterface(subDirectory, &rv) ;
449 if (!NS_FAILED(rv))
450 rdfService->UnregisterResource(rdfResource);
452 if (!NS_FAILED(rv)) //failed means we have added this directory
454 //map mailing lists as views
455 _rStrings->push_back( aTableName ); // Table name
456 if (!bIsMailList) {
457 OUString aTableType("TABLE");
458 _rTypes->push_back( aTableType ); // Table type
460 else
462 OUString aTableType("VIEW");
463 _rTypes->push_back( aTableType ); // Table type
466 rv = subDirectories -> Next();
467 } while ( rv == NS_OK);
469 return( NS_OK );
471 sal_Bool MDatabaseMetaDataHelper::getTableStrings( OConnection* _pCon,
472 ::std::vector< OUString >& _rStrings,
473 ::std::vector< OUString >& _rTypes)
475 sal_Bool bGivenURI;
476 OUString sAbURI;
477 OString sAbURIString;
479 OSL_TRACE( "IN MDatabaseMetaDataHelper::getTableStrings( 0x%08X, %s)", _pCon, _pCon->getForceLoadTables()?"True":"False" );
481 ::osl::MutexGuard aGuard( m_aMetaMutex );
482 // Only do the query if we have to - performance degrades otherwise
483 if ( ! _pCon->getForceLoadTables() && m_aTableNames.size() > 0 ) {
484 _rStrings = m_aTableNames;
485 _rTypes = m_aTableTypes;
486 return( sal_True );
489 m_aTableNames.clear();
490 m_aTableTypes.clear();
492 sAbURI = _pCon->getMozURI();
494 OSL_TRACE("AbURI = %s", OUtoCStr( sAbURI ) );
495 if ( sAbURI.isEmpty() )
496 bGivenURI = sal_False;
497 else {
498 sAbURIString = OUStringToOString( sAbURI,
499 RTL_TEXTENCODING_ASCII_US);
500 bGivenURI = sal_True;
503 if ( bGivenURI )
504 OSL_TRACE("Using URI %s to getTables()", sAbURIString.getStr() );
505 else
506 OSL_TRACE("Using URI of ROOTDIR to getTables()");
508 // Get the mozilla addressbooks from the base directory.
509 nsresult rv = NS_OK;
510 nsCOMPtr<nsIEnumerator> subDirectories;
511 sal_Int32 nDirectoryType=0;
512 m_ProductType = MozillaProductType_Mozilla;
513 m_ProfileName = _pCon->getMozProfile();
516 if (_pCon->isThunderbird())
518 if (!bGivenURI)
519 sAbURIString = s_pADDRESSBOOKROOTDIR;
520 nDirectoryType = SDBCAddress::ThunderBird;
521 m_ProductType = MozillaProductType_Thunderbird;
523 else if (!bGivenURI)
525 sAbURIString = s_pADDRESSBOOKROOTDIR;
526 nDirectoryType = SDBCAddress::Mozilla;
528 else
530 if (_pCon->usesFactory())
532 nDirectoryType = SDBCAddress::Outlook;
534 else
536 if (_pCon->isLDAP())
538 nDirectoryType = SDBCAddress::LDAP;
540 else
542 sAbURIString = s_pADDRESSBOOKROOTDIR;
543 nDirectoryType = SDBCAddress::Mozilla;
548 if ( !_pCon->isLDAP() && !m_bProfileExists )
549 { // no need to boot up a Mozilla profile for an LDAP connection
550 Reference<XMozillaBootstrap> xMozillaBootstrap;
551 Reference<XMultiServiceFactory> xFactory = ::comphelper::getProcessServiceFactory();
552 OSL_ENSURE( xFactory.is(), "can't get service factory" );
553 Reference<XInterface> xInstance = xFactory->createInstance("com.sun.star.mozilla.MozillaBootstrap");
554 OSL_ENSURE( xInstance.is(), "failed to create instance" );
555 xMozillaBootstrap = Reference<XMozillaBootstrap>(xInstance,UNO_QUERY);
556 m_bProfileExists = sal_False;
557 //If there are no profiles for this product
558 //Or the given profile name does not found
559 //We will reaise a "No Addressbook Directories Exist" error
560 if ( xMozillaBootstrap->getProfileCount(m_ProductType) == 0 ||
561 ( !m_ProfileName.isEmpty() && xMozillaBootstrap->getProfilePath(m_ProductType,m_ProfileName).isEmpty()))
562 m_bProfileExists = sal_False;
563 else
564 if (xMozillaBootstrap->bootupProfile(m_ProductType,m_ProfileName) > 0)
565 m_bProfileExists = sal_True;
568 if ( !m_bProfileExists
569 && !_pCon->isLDAP()
570 && ( ( nDirectoryType == SDBCAddress::Mozilla )
571 || ( nDirectoryType == SDBCAddress::ThunderBird )
575 lcl_setNoAddressBookFoundError( m_aError, *_pCon, m_ProductType, bGivenURI );
576 return sal_False;
579 MNameMapper *nmap = _pCon->getNameMapper();
580 nmap->reset();
582 sal_Int32 nErrorResourceId( 0 );
584 MNSMozabProxy xMProxy;
585 RunArgs args;
586 args.funcIndex = ProxiedFunc::FUNC_GET_TABLE_STRINGS;
587 args.argCount = 6;
588 args.arg1 = (void*)sAbURIString.getStr();
589 args.arg2 = (void*)&nDirectoryType;
590 args.arg3 = (void*)nmap;
591 args.arg4 = (void*)&m_aTableNames;
592 args.arg5 = (void*)&m_aTableTypes;
593 args.arg6 = (void*)&nErrorResourceId;
594 rv = xMProxy.StartProxy(&args,m_ProductType,m_ProfileName);
595 m_aError.setResId( static_cast<sal_uInt16>(nErrorResourceId) );
597 if (NS_FAILED(rv))
599 if ( nErrorResourceId == 0 )
600 m_aError.setResId( STR_UNSPECIFIED_ERROR );
601 return sal_False;
603 OSL_TRACE( "\tOUT MDatabaseMetaDataHelper::getTableStrings()" );
604 _rStrings = m_aTableNames;
605 _rTypes = m_aTableTypes;
607 _pCon->setForceLoadTables(sal_False);
608 return( sal_True );
611 sal_Bool MDatabaseMetaDataHelper::getTables( OConnection* _pCon,
612 const OUString& tableNamePattern,
613 const Sequence< OUString >& types,
614 ODatabaseMetaDataResultSet::ORows& _rRows)
617 static ODatabaseMetaDataResultSet::ORows aRows;
619 OSL_TRACE( "IN MDatabaseMetaDataHelper::getTables()" );
620 ::osl::MutexGuard aGuard( m_aMetaMutex );
622 ODatabaseMetaDataResultSet::ORows().swap(aRows); // this makes real clear where memory is freed as well
623 aRows.clear();
625 ::std::vector< OUString > tables;
626 ::std::vector< OUString > tabletypes;
627 OUString matchAny = "%";
629 if ( !getTableStrings( _pCon, tables,tabletypes ) )
630 return sal_False;
632 for ( size_t i = 0; i < tables.size(); i++ ) {
633 ODatabaseMetaDataResultSet::ORow aRow(3);
635 OUString aTableName = tables[i];
636 OUString aTableType = tabletypes[i];
637 OSL_TRACE("TableName = >%s<", OUtoCStr( aTableName ) );
640 // return tables to caller
641 if (match( tableNamePattern, aTableName, '\0' ) &&
642 ( 0 == types.getLength() || //types is null or types include aTableType or types include "%"
643 0 != ::comphelper::findValue( types, aTableType, sal_True ).getLength() ||
644 0 != ::comphelper::findValue( types, matchAny, sal_True ).getLength())) {
645 if ( aTableName.isEmpty() ) {
646 aTableName = "AddressBook";
649 OSL_TRACE( "TableName = %s ; TableType = %s", OUtoCStr(aTableName), OUtoCStr(aTableType) );
651 aRow.push_back( new ORowSetValueDecorator( aTableName ) ); // Table name
652 aRow.push_back( new ORowSetValueDecorator( aTableType ) ); // Table type
653 aRow.push_back( ODatabaseMetaDataResultSet::getEmptyValue() ); // Remarks
654 aRows.push_back(aRow);
658 OSL_TRACE( "\tOUT MDatabaseMetaDataHelper::getTables()" );
659 _rRows = aRows;
660 return(sal_True);
664 sal_Bool
665 MDatabaseMetaDataHelper::testLDAPConnection( OConnection* _pCon )
667 const sal_Unicode QUERY_CHAR = '?';
668 const sal_Char* MOZ_SCHEMA = "moz-abldapdirectory://";
669 const sal_Char* LDAP_SCHEMA = "ldap://";
671 OString sAbURI;
672 OUString sAbBindDN;
673 OUString sAbPassword;
674 sal_Bool useSSL = _pCon->getUseSSL();
676 nsresult rv(0);
678 sAbURI = OUStringToOString( _pCon->getMozURI(), RTL_TEXTENCODING_ASCII_US );
679 sAbBindDN = _pCon->getBindDN();
680 sAbPassword = _pCon->getPassword();
683 sal_Int32 pos = sAbURI.indexOf( MOZ_SCHEMA );
684 if ( pos != -1 ) {
685 sAbURI = sAbURI.replaceAt (pos, rtl_str_getLength( MOZ_SCHEMA ), OString(LDAP_SCHEMA) );
688 pos = sAbURI.indexOf( QUERY_CHAR );
689 if ( pos != -1 ) {
690 sal_Int32 len = sAbURI.getLength();
691 sAbURI = sAbURI.replaceAt( pos, len - pos, OString("") );
693 const sal_Unicode* bindDN=nsnull;
694 if (!sAbBindDN.isEmpty())
696 bindDN = sAbBindDN.getStr();
699 RunArgs args;
700 args.funcIndex = ProxiedFunc::FUNC_TESTLDAP_INIT_LDAP;
701 args.argCount = 4;
702 args.arg1 = (void*)sAbURI.getStr();
703 args.arg2 = (void*)bindDN;
704 args.arg3 = (void*)sAbPassword.getStr();
705 args.arg4 = (void*)&useSSL;
707 MNSMozabProxy xMProxy;
708 rv = xMProxy.StartProxy( &args, m_ProductType, OUString() );
709 if ( NS_SUCCEEDED( rv ) ) //Init LDAP,pass OUString() to StarProxy to ignore profile switch
711 args.funcIndex = ProxiedFunc::FUNC_TESTLDAP_IS_LDAP_CONNECTED;
712 TimeValue timeValue = { 1, 0 }; // 1 * 30 Seconds timeout
713 sal_Int32 times=0;
714 while ( times++ < 30 )
716 rv = xMProxy.StartProxy( &args, m_ProductType, OUString() );
717 if ( NS_SUCCEEDED( rv ) )
718 // connected successfully
719 break;
721 if ( rv == (nsresult)PR_NOT_CONNECTED_ERROR )
722 // connection failed
723 break;
725 // not yet decided - continue waiting
726 osl_waitThread( &timeValue );
729 m_aError.setResId( STR_COULD_NOT_CONNECT_LDAP );
730 return NS_SUCCEEDED( rv ) ? sal_True : sal_False;
733 sal_Bool MDatabaseMetaDataHelper::NewAddressBook(OConnection* _pCon,const OUString & aTableName)
735 sal_Bool bIsMozillaAB;
737 bIsMozillaAB = !_pCon->usesFactory() && ! _pCon->isLDAP();
739 if ( !bIsMozillaAB )
741 m_aError.setResId( STR_NO_TABLE_CREATION_SUPPORT );
742 return sal_False;
744 else
745 OSL_TRACE("Create table");
747 // Get the mozilla addressbooks from the base directory.
748 MNSMozabProxy xMProxy;
749 RunArgs args;
750 args.funcIndex = ProxiedFunc::FUNC_NEW_ADDRESS_BOOK;
751 args.argCount = 1;
752 args.arg1 = (void*)&aTableName;
753 nsresult rv = xMProxy.StartProxy(&args,m_ProductType,m_ProfileName);
755 _pCon->setForceLoadTables(sal_True); //force reload table next time
756 if (rv == NS_ERROR_FILE_IS_LOCKED)
758 m_aError.setResId( STR_MOZILLA_IS_RUNNING );
760 else if (NS_FAILED(rv))
762 m_aError.set( STR_COULD_NOT_CREATE_ADDRESSBOOK, 0, OUString::number( sal_Int32(rv), 16 ) );
764 OSL_TRACE( "OUT MDatabaseMetaDataHelper::NewAddressBook()" );
765 return( NS_SUCCEEDED(rv) ? sal_True : sal_False );
767 nsresult NewAddressBook(const OUString * aName)
769 if (isProfileLocked(NULL))
770 return NS_ERROR_FILE_IS_LOCKED;
771 nsresult rv;
772 nsCOMPtr<nsIAbDirectoryProperties> aProperties = do_CreateInstance(NS_ABDIRECTORYPROPERTIES_CONTRACTID, &rv);
773 NS_ENSURE_ARG_POINTER(aProperties);
774 const OUString& uName = *aName;
775 nsString nsName;
776 MTypeConverter::ouStringToNsString(uName,nsName);
777 aProperties->SetDescription(nsName);
779 nsCOMPtr<nsIRDFService> rdfService = do_GetService (kRDFServiceCID, &rv);
780 NS_ENSURE_SUCCESS(rv, rv);
782 nsCOMPtr<nsIRDFResource> parentResource;
783 rv = rdfService->GetResource(NS_LITERAL_CSTRING(kAllDirectoryRoot), getter_AddRefs(parentResource));
784 NS_ENSURE_SUCCESS(rv, rv);
786 nsCOMPtr<nsIAbDirectory> parentDir = do_QueryInterface(parentResource, &rv);
787 NS_ENSURE_SUCCESS(rv, rv);
789 rv = parentDir->CreateNewDirectory(aProperties);
790 return rv;
793 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */