Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / connectivity / source / drivers / mozab / MColumnAlias.cxx
blob2a28ef569a957b6d995cfa5e85eabfd1d3f6a427
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 .
20 #include <sal/macros.h>
21 #include "MColumnAlias.hxx"
22 #include "MConnection.hxx"
23 #include "MExtConfigAccess.hxx"
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <com/sun/star/container/XNameAccess.hpp>
28 #include <tools/diagnose_ex.h>
30 #include <algorithm>
31 #include <functional>
33 using namespace ::connectivity;
34 using namespace ::connectivity::mozab;
35 using namespace ::com::sun::star::uno;
36 using namespace ::com::sun::star::lang;
37 using namespace ::com::sun::star::beans;
38 using namespace ::com::sun::star::container;
41 OColumnAlias::OColumnAlias( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxORB )
43 static const sal_Char* s_pProgrammaticNames[] =
45 "FirstName",
46 "LastName",
47 "DisplayName",
48 "NickName",
49 "PrimaryEmail",
50 "SecondEmail",
51 "PreferMailFormat",
52 "WorkPhone",
53 "HomePhone",
54 "FaxNumber",
55 "PagerNumber",
56 "CellularNumber",
57 "HomeAddress",
58 "HomeAddress2",
59 "HomeCity",
60 "HomeState",
61 "HomeZipCode",
62 "HomeCountry",
63 "WorkAddress",
64 "WorkAddress2",
65 "WorkCity",
66 "WorkState",
67 "WorkZipCode",
68 "WorkCountry",
69 "JobTitle",
70 "Department",
71 "Company",
72 "WebPage1",
73 "WebPage2",
74 "BirthYear",
75 "BirthMonth",
76 "BirthDay",
77 "Custom1",
78 "Custom2",
79 "Custom3",
80 "Custom4",
81 "Notes",
84 for ( size_t i = 0; i < sizeof( s_pProgrammaticNames ) / sizeof( s_pProgrammaticNames[0] ); ++i )
85 m_aAliasMap[ OUString::createFromAscii( s_pProgrammaticNames[i] ) ] = AliasEntry( s_pProgrammaticNames[i], i );
87 initialize( _rxORB );
91 void OColumnAlias::initialize( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxORB )
93 // open our driver settings config node
95 // the config path for our own driver's settings
96 Reference< XPropertySet > xDriverNode = createDriverConfigNode( _rxORB );
97 if ( xDriverNode.is() )
99 try
102 Reference< XNameAccess > xAliasesNode;
103 xDriverNode->getPropertyValue("ColumnAliases") >>= xAliasesNode;
104 OSL_ENSURE( xAliasesNode.is(), "OColumnAlias::setAlias: missing the aliases node!" );
106 // this is a set of string nodes
107 Sequence< OUString > aProgrammaticNames;
108 if ( xAliasesNode.is() )
109 aProgrammaticNames = xAliasesNode->getElementNames();
112 // travel through all the set elements
113 const OUString* pProgrammaticNames = aProgrammaticNames.getConstArray();
114 const OUString* pProgrammaticNamesEnd = pProgrammaticNames + aProgrammaticNames.getLength();
115 OUString sAssignedAlias;
117 for ( ; pProgrammaticNames < pProgrammaticNamesEnd; ++pProgrammaticNames )
119 OSL_VERIFY( xAliasesNode->getByName( *pProgrammaticNames ) >>= sAssignedAlias );
121 // normalize in case the config data is corrupted
122 // (what we really don't need is an empty alias ...)
123 if ( sAssignedAlias.isEmpty() )
124 sAssignedAlias = *pProgrammaticNames;
126 OString sAsciiProgrammaticName( OUStringToOString( *pProgrammaticNames, RTL_TEXTENCODING_ASCII_US ) );
128 #if OSL_DEBUG_LEVEL > 0
129 bool bFound = false;
130 #endif
131 for ( AliasMap::iterator search = m_aAliasMap.begin();
132 ( search != m_aAliasMap.end() );
133 ++search
136 if ( search->second.programmaticAsciiName.equals( sAsciiProgrammaticName ) )
138 AliasEntry entry( search->second );
139 m_aAliasMap.erase( search );
140 m_aAliasMap[ sAssignedAlias ] = entry;
142 #if OSL_DEBUG_LEVEL > 0
143 bFound = true;
144 #endif
146 break;
150 OSL_ENSURE( bFound, "OColumnAlias::setAlias: did not find a programmatic name which exists in the configuration!" );
153 catch( const Exception& )
155 DBG_UNHANDLED_EXCEPTION();
161 OString OColumnAlias::getProgrammaticNameOrFallbackToUTF8Alias( const OUString& _rAlias ) const
163 AliasMap::const_iterator pos = m_aAliasMap.find( _rAlias );
164 if ( pos == m_aAliasMap.end() )
166 OSL_FAIL( "OColumnAlias::getProgrammaticNameOrFallbackToUTF8Alias: no programmatic name for this alias!" );
167 return OUStringToOString( _rAlias, RTL_TEXTENCODING_UTF8 );
169 return pos->second.programmaticAsciiName;
173 bool OColumnAlias::isColumnSearchable( const OUString _alias ) const
175 OString sProgrammatic = getProgrammaticNameOrFallbackToUTF8Alias( _alias );
177 return ( !sProgrammatic.equals( "HomeCountry" )
178 && !sProgrammatic.equals( "WorkCountry" )
180 // for those, we know that they're not searchable in the Mozilla/LDAP implementation.
181 // There might be more ...
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */