merge the formfield patch from ooo-build
[ooovba.git] / connectivity / source / drivers / mozab / mozillasrc / MNameMapper.cxx
blob3e8c63d467487b1aac71e52d3e0f669c55e77ea0
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: MNameMapper.cxx,v $
10 * $Revision: 1.9 $
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"
35 #include <MNameMapper.hxx>
37 #if OSL_DEBUG_LEVEL > 0
38 # define OUtoCStr( x ) ( ::rtl::OUStringToOString ( (x), RTL_TEXTENCODING_ASCII_US).getStr())
39 #else /* OSL_DEBUG_LEVEL */
40 # define OUtoCStr( x ) ("dummy")
41 #endif /* OSL_DEBUG_LEVEL */
44 using namespace connectivity::mozab;
45 using namespace rtl;
47 bool
48 MNameMapper::ltstr::operator()( const ::rtl::OUString &s1, const ::rtl::OUString &s2) const
50 return s1.compareTo(s2) < 0;
53 MNameMapper::MNameMapper()
55 mDirMap = new MNameMapper::dirMap;
56 mUriMap = new MNameMapper::uriMap;
58 MNameMapper::~MNameMapper()
60 clear();
63 void MNameMapper::reset()
65 clear();
66 mDirMap = new MNameMapper::dirMap;
67 mUriMap = new MNameMapper::uriMap;
69 void MNameMapper::clear()
71 if ( mUriMap != NULL ) {
72 delete mUriMap;
74 if ( mDirMap != NULL ) {
75 MNameMapper::dirMap::iterator iter;
76 for (iter = mDirMap -> begin(); iter != mDirMap -> end(); ++iter) {
77 NS_IF_RELEASE(((*iter).second));
79 delete mDirMap;
82 const char * getURI(const nsIAbDirectory* directory)
84 nsresult retCode;
85 nsCOMPtr<nsIRDFResource> rdfResource = do_QueryInterface((nsISupports *)directory, &retCode) ;
86 if (NS_FAILED(retCode)) { return NULL; }
87 const char * uri;
88 retCode=rdfResource->GetValueConst(&uri);
89 if (NS_FAILED(retCode)) { return NULL; }
90 return uri;
93 // May modify the name passed in so that it's unique
94 nsresult
95 MNameMapper::add( ::rtl::OUString& str, nsIAbDirectory* abook )
97 MNameMapper::dirMap::iterator iter;
99 OSL_TRACE( "IN MNameMapper::add()\n" );
101 if ( abook == NULL ) {
102 OSL_TRACE( "\tOUT MNameMapper::add() called with null abook\n" );
103 return NS_ERROR_NULL_POINTER;
106 ::rtl::OUString ouUri=::rtl::OUString::createFromAscii(getURI(abook));
107 if ( mUriMap->find (ouUri) != mUriMap->end() ) //There's already an entry with same uri
109 return NS_ERROR_FILE_NOT_FOUND;
111 mUriMap->insert( MNameMapper::uriMap::value_type( ouUri, abook ) );
113 ::rtl::OUString tempStr=str;
114 long count =1;
115 while ( mDirMap->find( tempStr ) != mDirMap->end() ) {
117 tempStr = str + ::rtl::OUString::valueOf(count);;
118 count ++;
120 str = tempStr;
121 NS_IF_ADDREF(abook);
122 mDirMap->insert( MNameMapper::dirMap::value_type( str, abook ) );
123 OSL_TRACE( "\tOUT MNameMapper::add()\n" );
124 return 0;
127 bool
128 MNameMapper::getDir( const ::rtl::OUString& str, nsIAbDirectory* *abook )
130 MNameMapper::dirMap::iterator iter;
132 OSL_TRACE( "IN MNameMapper::getDir( %s )\n", OUtoCStr(str)?OUtoCStr(str):"NULL" );
134 if ( (iter = mDirMap->find( str )) != mDirMap->end() ) {
135 *abook = (*iter).second;
136 NS_IF_ADDREF(*abook);
137 } else {
138 *abook = NULL;
141 OSL_TRACE( "\tOUT MNameMapper::getDir() : %s\n", (*abook)?"True":"False" );
143 return( (*abook) != NULL );