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 "fieldmappingimpl.hxx"
21 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
22 #include <com/sun/star/beans/PropertyValue.hpp>
23 #include <com/sun/star/beans/XPropertySet.hpp>
24 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
25 #include <com/sun/star/ui/AddressBookSourceDialog.hpp>
26 #include <com/sun/star/awt/XWindow.hpp>
27 #include <com/sun/star/sdb/CommandType.hpp>
28 #include <tools/debug.hxx>
29 #include <toolkit/helper/vclunohelper.hxx>
30 #include <vcl/stdtext.hxx>
31 #include <com/sun/star/util/AliasProgrammaticPair.hpp>
32 #include <strings.hrc>
33 #include <componentmodule.hxx>
34 #include <unotools/confignode.hxx>
35 #include <sal/macros.h>
36 #include <sal/log.hxx>
37 #include <osl/diagnose.h>
44 using namespace ::utl
;
45 using namespace ::com::sun::star::uno
;
46 using namespace ::com::sun::star::awt
;
47 using namespace ::com::sun::star::util
;
48 using namespace ::com::sun::star::lang
;
49 using namespace ::com::sun::star::beans
;
50 using namespace ::com::sun::star::sdb
;
51 using namespace ::com::sun::star::ui
;
52 using namespace ::com::sun::star::ui::dialogs
;
55 static const char sDriverSettingsNodeName
[] = "/org.openoffice.Office.DataAccess/DriverSettings/com.sun.star.comp.sdbc.MozabDriver";
56 static const char sAddressBookNodeName
[] = "/org.openoffice.Office.DataAccess/AddressBook";
59 namespace fieldmapping
63 bool invokeDialog( const Reference
< XComponentContext
>& _rxORB
, class vcl::Window
* _pParent
,
64 const Reference
< XPropertySet
>& _rxDataSource
, AddressSettings
& _rSettings
)
66 _rSettings
.aFieldMapping
.clear();
68 DBG_ASSERT( _rxORB
.is(), "fieldmapping::invokeDialog: invalid service factory!" );
69 DBG_ASSERT( _rxDataSource
.is(), "fieldmapping::invokeDialog: invalid data source!" );
70 if ( !_rxORB
.is() || !_rxDataSource
.is() )
76 // create an instance of the dialog service
77 Reference
< XWindow
> xDialogParent
= VCLUnoHelper::GetInterface( _pParent
);
78 OUString
sTitle(compmodule::ModuleRes(RID_STR_FIELDDIALOGTITLE
));
79 Reference
< XExecutableDialog
> xDialog
= AddressBookSourceDialog::createWithDataSource(_rxORB
,
83 _rSettings
.bRegisterDataSource
? _rSettings
.sRegisteredDataSourceName
: _rSettings
.sDataSourceName
,
85 _rSettings
.sSelectedTable
,
89 if ( xDialog
->execute() )
91 // retrieve the field mapping as set by he user
92 Reference
< XPropertySet
> xDialogProps( xDialog
, UNO_QUERY
);
94 Sequence
< AliasProgrammaticPair
> aMapping
;
96 xDialogProps
->getPropertyValue("FieldMapping") >>= aMapping
;
97 DBG_ASSERT( bSuccess
, "fieldmapping::invokeDialog: invalid property type for FieldMapping!" );
99 // and copy it into the map
100 const AliasProgrammaticPair
* pMapping
= aMapping
.getConstArray();
101 const AliasProgrammaticPair
* pMappingEnd
= pMapping
+ aMapping
.getLength();
102 for (;pMapping
!= pMappingEnd
; ++pMapping
)
103 _rSettings
.aFieldMapping
[ pMapping
->ProgrammaticName
] = pMapping
->Alias
;
109 catch(const Exception
&)
111 OSL_FAIL("fieldmapping::invokeDialog: caught an exception while executing the dialog!");
117 void defaultMapping( const Reference
< XComponentContext
>& _rxContext
, MapString2String
& _rFieldAssignment
)
119 _rFieldAssignment
.clear();
124 // a) For the address data source, we need a mapping from programmatic names (1) to real column names
125 // b) The SDBC driver has a fixed set of columns, which, when returned, are named according to
126 // some configuration entries. E.g., the driver displays the field which it knows contains
127 // the first name as "First Name" - the latter string is stored in the config.
128 // For this, the driver uses programmatic names, too, but they differ from the programmatic names the
129 // template documents have.
130 // So what we need first is a mapping from programmatic names (1) to programmatic names (2)
131 const sal_Char
* pMappingProgrammatics
[] =
133 "FirstName", "FirstName",
134 "LastName", "LastName",
135 "Street", "HomeAddress",
136 "Zip", "HomeZipCode",
138 "State", "HomeState",
139 "Country", "HomeCountry",
140 "PhonePriv", "HomePhone",
141 "PhoneComp", "WorkPhone",
142 "PhoneCell", "CellularNumber",
143 "Pager", "PagerNumber",
145 "EMail", "PrimaryEmail",
148 "Altfield1", "Custom1",
149 "Altfield2", "Custom2",
150 "Altfield3", "Custom3",
151 "Altfield4", "Custom4",
153 "Company", "Company",
154 "Department", "Department"
156 // (this list is not complete: both lists of programmatic names are larger in real,
157 // but this list above is the intersection)
160 // access the configuration information which the driver uses for determining its column names
161 OUString
sDriverAliasesNodeName(
162 OUStringLiteral(sDriverSettingsNodeName
)
165 // create a config node for this
166 OConfigurationTreeRoot aDriverFieldAliasing
= OConfigurationTreeRoot::createWithComponentContext(
167 _rxContext
, sDriverAliasesNodeName
, -1, OConfigurationTreeRoot::CM_READONLY
);
169 // loop through all programmatic pairs
170 DBG_ASSERT( 0 == SAL_N_ELEMENTS( pMappingProgrammatics
) % 2,
171 "fieldmapping::defaultMapping: invalid programmatic map!" );
173 sal_Int32
const nIntersectedProgrammatics
= SAL_N_ELEMENTS( pMappingProgrammatics
) / 2;
175 const sal_Char
** pProgrammatic
= pMappingProgrammatics
;
176 OUString sAddressProgrammatic
;
177 OUString sDriverProgrammatic
;
180 i
< nIntersectedProgrammatics
;
184 sAddressProgrammatic
= OUString::createFromAscii( *pProgrammatic
++ );
185 sDriverProgrammatic
= OUString::createFromAscii( *pProgrammatic
++ );
187 if ( aDriverFieldAliasing
.hasByName( sDriverProgrammatic
) )
189 aDriverFieldAliasing
.getNodeValue( sDriverProgrammatic
) >>= sDriverUI
;
190 if ( 0 == sDriverUI
.getLength() )
192 OSL_FAIL( "fieldmapping::defaultMapping: invalid driver UI column name!");
195 _rFieldAssignment
[ sAddressProgrammatic
] = sDriverUI
;
199 OSL_FAIL( "fieldmapping::defaultMapping: invalid driver programmatic name!" );
203 catch( const Exception
& )
205 OSL_FAIL("fieldmapping::defaultMapping: code is assumed to throw no exceptions!");
206 // the config nodes we're using herein should not do this ....
211 void writeTemplateAddressFieldMapping( const Reference
< XComponentContext
>& _rxContext
, const MapString2String
& _rFieldAssignment
)
213 // want to have a non-const map for easier handling
214 MapString2String
aFieldAssignment( _rFieldAssignment
);
216 // access the configuration information which the driver uses for determining its column names
218 // create a config node for this
219 OConfigurationTreeRoot aAddressBookSettings
= OConfigurationTreeRoot::createWithComponentContext(
220 _rxContext
, sAddressBookNodeName
);
222 OConfigurationNode aFields
= aAddressBookSettings
.openNode( OUString( "Fields" ) );
224 // loop through all existent fields
225 Sequence
< OUString
> aExistentFields
= aFields
.getNodeNames();
226 const OUString
* pExistentFields
= aExistentFields
.getConstArray();
227 const OUString
* pExistentFieldsEnd
= pExistentFields
+ aExistentFields
.getLength();
229 const OUString
sProgrammaticNodeName( "ProgrammaticFieldName" );
230 const OUString
sAssignedNodeName( "AssignedFieldName" );
232 for ( ; pExistentFields
!= pExistentFieldsEnd
; ++pExistentFields
)
235 ((aFields
.openNode(*pExistentFields
)
236 .getNodeValue(sProgrammaticNodeName
).get
<OUString
>())
237 != *pExistentFields
),
238 "extensions.abpilot",
239 "fieldmapping::writeTemplateAddressFieldMapping: inconsistent config data!");
240 // there should be a redundancy in the config data .... if this asserts, there isn't anymore!
242 // do we have a new alias for the programmatic?
243 MapString2String::iterator aPos
= aFieldAssignment
.find( *pExistentFields
);
244 if ( aFieldAssignment
.end() != aPos
)
246 // -> set a new value
247 OConfigurationNode aExistentField
= aFields
.openNode( *pExistentFields
);
248 aExistentField
.setNodeValue( sAssignedNodeName
, makeAny( aPos
->second
) );
249 // and remove the mapping entry
250 aFieldAssignment
.erase( *pExistentFields
);
255 aFields
.removeNode( *pExistentFields
);
259 // now everything remaining in aFieldAssignment marks a mapping entry which was not present
260 // in the config before
261 for (auto const& elem
: aFieldAssignment
)
263 DBG_ASSERT( !aFields
.hasByName( elem
.first
),
264 "fieldmapping::writeTemplateAddressFieldMapping: inconsistence!" );
265 // in case the config node for the fields already has the node named <aNewMapping->first>,
266 // the entry should have been removed from aNewMapping (in the above loop)
267 OConfigurationNode aNewField
= aFields
.createNode( elem
.first
);
268 aNewField
.setNodeValue( sProgrammaticNodeName
, makeAny( elem
.first
) );
269 aNewField
.setNodeValue( sAssignedNodeName
, makeAny( elem
.second
) );
272 // commit the changes done
273 aAddressBookSettings
.commit();
277 } // namespace fieldmapping
280 namespace addressconfig
284 void writeTemplateAddressSource( const Reference
< XComponentContext
>& _rxContext
,
285 const OUString
& _rDataSourceName
, const OUString
& _rTableName
)
287 // access the configuration information which the driver uses for determining its column names
289 // create a config node for this
290 OConfigurationTreeRoot aAddressBookSettings
= OConfigurationTreeRoot::createWithComponentContext(
291 _rxContext
, sAddressBookNodeName
);
293 aAddressBookSettings
.setNodeValue( OUString( "DataSourceName" ), makeAny( _rDataSourceName
) );
294 aAddressBookSettings
.setNodeValue( OUString( "Command" ), makeAny( _rTableName
) );
295 aAddressBookSettings
.setNodeValue( OUString( "CommandType" ), makeAny( sal_Int16(CommandType::TABLE
) ) );
297 // commit the changes done
298 aAddressBookSettings
.commit();
302 void markPilotSuccess( const Reference
< XComponentContext
>& _rxContext
)
304 // access the configuration information which the driver uses for determining its column names
306 // create a config node for this
307 OConfigurationTreeRoot aAddressBookSettings
= OConfigurationTreeRoot::createWithComponentContext(
308 _rxContext
, sAddressBookNodeName
);
311 aAddressBookSettings
.setNodeValue( OUString( "AutoPilotCompleted" ), makeAny( true ) );
313 // commit the changes done
314 aAddressBookSettings
.commit();
318 } // namespace addressconfig
324 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */