1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include <MNameMapper.hxx>
22 #if OSL_DEBUG_LEVEL > 0
23 # define OUtoCStr( x ) ( OUStringToOString ( (x), RTL_TEXTENCODING_ASCII_US).getStr())
24 #else /* OSL_DEBUG_LEVEL */
25 # define OUtoCStr( x ) ("dummy")
26 #endif /* OSL_DEBUG_LEVEL */
29 using namespace connectivity::mozab
;
32 MNameMapper::ltstr::operator()( const OUString
&s1
, const OUString
&s2
) const
34 return s1
.compareTo(s2
) < 0;
37 MNameMapper::MNameMapper()
39 mDirMap
= new MNameMapper::dirMap
;
40 mUriMap
= new MNameMapper::uriMap
;
42 MNameMapper::~MNameMapper()
47 void MNameMapper::reset()
50 mDirMap
= new MNameMapper::dirMap
;
51 mUriMap
= new MNameMapper::uriMap
;
53 void MNameMapper::clear()
55 if ( mUriMap
!= NULL
) {
58 if ( mDirMap
!= NULL
) {
59 MNameMapper::dirMap::iterator iter
;
60 for (iter
= mDirMap
-> begin(); iter
!= mDirMap
-> end(); ++iter
) {
61 NS_IF_RELEASE(((*iter
).second
));
66 const char * getURI(const nsIAbDirectory
* directory
)
69 nsCOMPtr
<nsIRDFResource
> rdfResource
= do_QueryInterface((nsISupports
*)directory
, &retCode
) ;
70 if (NS_FAILED(retCode
)) { return NULL
; }
72 retCode
=rdfResource
->GetValueConst(&uri
);
73 if (NS_FAILED(retCode
)) { return NULL
; }
77 // May modify the name passed in so that it's unique
79 MNameMapper::add( OUString
& str
, nsIAbDirectory
* abook
)
81 MNameMapper::dirMap::iterator iter
;
83 OSL_TRACE( "IN MNameMapper::add()" );
85 if ( abook
== NULL
) {
86 OSL_TRACE( "\tOUT MNameMapper::add() called with null abook" );
87 return NS_ERROR_NULL_POINTER
;
90 OUString ouUri
=OUString::createFromAscii(getURI(abook
));
91 if ( mUriMap
->find (ouUri
) != mUriMap
->end() ) //There's already an entry with same uri
93 return NS_ERROR_FILE_NOT_FOUND
;
95 mUriMap
->insert( MNameMapper::uriMap::value_type( ouUri
, abook
) );
99 while ( mDirMap
->find( tempStr
) != mDirMap
->end() ) {
101 tempStr
= str
+ OUString::number(count
);
106 mDirMap
->insert( MNameMapper::dirMap::value_type( str
, abook
) );
107 OSL_TRACE( "\tOUT MNameMapper::add()" );
112 MNameMapper::getDir( const OUString
& str
, nsIAbDirectory
* *abook
)
114 MNameMapper::dirMap::iterator iter
;
116 OSL_TRACE( "IN MNameMapper::getDir( %s )", OUtoCStr(str
)?OUtoCStr(str
):"NULL" );
118 if ( (iter
= mDirMap
->find( str
)) != mDirMap
->end() ) {
119 *abook
= (*iter
).second
;
120 NS_IF_ADDREF(*abook
);
125 OSL_TRACE( "\tOUT MNameMapper::getDir() : %s", (*abook
)?"True":"False" );
127 return( (*abook
) != NULL
);
130 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */