bump product version to 4.1.6.2
[LibreOffice.git] / extensions / source / bibliography / bibconfig.cxx
blobada5cc721c89b2bee70993282168bd87e1ac6ee7
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 #include <bibconfig.hxx>
22 #include <com/sun/star/uno/Sequence.hxx>
23 #include <com/sun/star/uno/Any.hxx>
24 #include <com/sun/star/beans/PropertyValue.hpp>
25 #include <com/sun/star/container/XNameAccess.hpp>
26 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
27 #include <com/sun/star/sdb/DatabaseContext.hpp>
28 #include <comphelper/processfactory.hxx>
30 using namespace ::com::sun::star::uno;
31 using namespace ::com::sun::star::beans;
32 using namespace ::com::sun::star::container;
33 using namespace ::com::sun::star::lang;
34 using namespace ::com::sun::star::sdb;
37 const char cDataSourceHistory[] = "DataSourceHistory";
39 Sequence<OUString> BibConfig::GetPropertyNames()
41 static Sequence<OUString> aNames;
42 if(!aNames.getLength())
44 aNames.realloc(8);
45 OUString* pNames = aNames.getArray();
46 pNames[0] = "CurrentDataSource/DataSourceName";
47 pNames[1] = "CurrentDataSource/Command";
48 pNames[2] = "CurrentDataSource/CommandType";
49 pNames[3] = "BeamerHeight";
50 pNames[4] = "ViewHeight";
51 pNames[5] = "QueryText";
52 pNames[6] = "QueryField";
53 pNames[7] = "ShowColumnAssignmentWarning";
55 return aNames;
58 BibConfig::BibConfig() :
59 ConfigItem("Office.DataAccess/Bibliography", CONFIG_MODE_DELAYED_UPDATE),
60 pMappingsArr(new MappingArray),
61 nBeamerSize(0),
62 nViewSize(0),
63 bShowColumnAssignmentWarning(sal_False)
65 //Names of the default columns
66 aColumnDefaults[0] = "Identifier";
67 aColumnDefaults[1] = "BibliographyType";
68 aColumnDefaults[2] = "Author";
69 aColumnDefaults[3] = "Title";
70 aColumnDefaults[4] = "Year";
71 aColumnDefaults[5] = "ISBN";
72 aColumnDefaults[6] = "Booktitle";
73 aColumnDefaults[7] = "Chapter";
74 aColumnDefaults[8] = "Edition";
75 aColumnDefaults[9] = "Editor";
76 aColumnDefaults[10] = "Howpublished";
77 aColumnDefaults[11] = "Institution";
78 aColumnDefaults[12] = "Journal";
79 aColumnDefaults[13] = "Month";
80 aColumnDefaults[14] = "Note";
81 aColumnDefaults[15] = "Annote";
82 aColumnDefaults[16] = "Number";
83 aColumnDefaults[17] = "Organizations";
84 aColumnDefaults[18] = "Pages";
85 aColumnDefaults[19] = "Publisher";
86 aColumnDefaults[20] = "Address";
87 aColumnDefaults[21] = "School";
88 aColumnDefaults[22] = "Series";
89 aColumnDefaults[23] = "ReportType";
90 aColumnDefaults[24] = "Volume";
91 aColumnDefaults[25] = "URL";
92 aColumnDefaults[26] = "Custom1";
93 aColumnDefaults[27] = "Custom2";
94 aColumnDefaults[28] = "Custom3";
95 aColumnDefaults[29] = "Custom4";
96 aColumnDefaults[30] = "Custom5";
99 const Sequence< OUString > aPropertyNames = GetPropertyNames();
100 const Sequence<Any> aPropertyValues = GetProperties( aPropertyNames );
101 const Any* pValues = aPropertyValues.getConstArray();
102 if(aPropertyValues.getLength() == aPropertyNames.getLength())
104 for(int nProp = 0; nProp < aPropertyNames.getLength(); nProp++)
106 if(pValues[nProp].hasValue())
108 switch(nProp)
110 case 0: pValues[nProp] >>= sDataSource; break;
111 case 1: pValues[nProp] >>= sTableOrQuery; break;
112 case 2: pValues[nProp] >>= nTblOrQuery; break;
113 case 3: pValues[nProp] >>= nBeamerSize; break;
114 case 4: pValues[nProp] >>= nViewSize ; break;
115 case 5: pValues[nProp] >>= sQueryText ; break;
116 case 6: pValues[nProp] >>= sQueryField; break;
117 case 7:
118 bShowColumnAssignmentWarning = *(sal_Bool*)pValues[nProp].getValue();
119 break;
124 OUString sName("DataSourceName");
125 OUString sTable("Command");
126 OUString sCommandType("CommandType");
127 Sequence< OUString > aNodeNames = GetNodeNames(cDataSourceHistory);
128 const OUString* pNodeNames = aNodeNames.getConstArray();
129 for(sal_Int32 nNode = 0; nNode < aNodeNames.getLength(); nNode++)
131 Sequence<OUString> aHistoryNames(3);
132 OUString* pHistoryNames = aHistoryNames.getArray();
134 OUString sPrefix(cDataSourceHistory);
135 sPrefix += "/";
136 sPrefix += pNodeNames[nNode];
137 sPrefix += "/";
138 pHistoryNames[0] = sPrefix;
139 pHistoryNames[0] += sName;
140 pHistoryNames[1] = sPrefix;
141 pHistoryNames[1] += sTable;
142 pHistoryNames[2] = sPrefix;
143 pHistoryNames[2] += sCommandType;
145 Sequence<Any> aHistoryValues = GetProperties( aHistoryNames );
146 const Any* pHistoryValues = aHistoryValues.getConstArray();
148 if(aHistoryValues.getLength() == aHistoryNames.getLength())
150 Mapping* pMapping = new Mapping;
151 pHistoryValues[0] >>= pMapping->sURL;
152 pHistoryValues[1] >>= pMapping->sTableName;
153 pHistoryValues[2] >>= pMapping->nCommandType;
154 //field assignment is contained in another set
155 sPrefix += "Fields";
156 Sequence< OUString > aAssignmentNodeNames = GetNodeNames(sPrefix);
157 const OUString* pAssignmentNodeNames = aAssignmentNodeNames.getConstArray();
158 Sequence<OUString> aAssignmentPropertyNames(aAssignmentNodeNames.getLength() * 2);
159 OUString* pAssignmentPropertyNames = aAssignmentPropertyNames.getArray();
160 sal_Int16 nFieldIdx = 0;
161 for(sal_Int16 nField = 0; nField < aAssignmentNodeNames.getLength(); nField++)
163 OUString sSubPrefix(sPrefix);
164 sSubPrefix += "/";
165 sSubPrefix += pAssignmentNodeNames[nField];
166 pAssignmentPropertyNames[nFieldIdx] = sSubPrefix;
167 pAssignmentPropertyNames[nFieldIdx++] += "/ProgrammaticFieldName";
168 pAssignmentPropertyNames[nFieldIdx] = sSubPrefix;
169 pAssignmentPropertyNames[nFieldIdx++] += "/AssignedFieldName";
171 Sequence<Any> aAssignmentValues = GetProperties(aAssignmentPropertyNames);
172 const Any* pAssignmentValues = aAssignmentValues.getConstArray();
173 OUString sTempLogical;
174 OUString sTempReal;
175 sal_Int16 nSetMapping = 0;
176 nFieldIdx = 0;
177 for(sal_Int16 nFieldVal = 0; nFieldVal < aAssignmentValues.getLength() / 2; nFieldVal++)
179 pAssignmentValues[nFieldIdx++] >>= sTempLogical;
180 pAssignmentValues[nFieldIdx++] >>= sTempReal;
181 if(!(sTempLogical.isEmpty() || sTempReal.isEmpty()))
183 pMapping->aColumnPairs[nSetMapping].sLogicalColumnName = sTempLogical;
184 pMapping->aColumnPairs[nSetMapping++].sRealColumnName = sTempReal;
187 pMappingsArr->push_back(pMapping);
192 BibConfig::~BibConfig()
194 if(IsModified())
195 Commit();
196 delete pMappingsArr;
199 BibDBDescriptor BibConfig::GetBibliographyURL()
201 BibDBDescriptor aRet;
202 aRet.sDataSource = sDataSource;
203 aRet.sTableOrQuery = sTableOrQuery;
204 aRet.nCommandType = nTblOrQuery;
205 return aRet;
208 void BibConfig::SetBibliographyURL(const BibDBDescriptor& rDesc)
210 sDataSource = rDesc.sDataSource;
211 sTableOrQuery = rDesc.sTableOrQuery;
212 nTblOrQuery = rDesc.nCommandType;
213 SetModified();
215 //---------------------------------------------------------------------------
216 void BibConfig::Notify( const com::sun::star::uno::Sequence<OUString>& )
220 void BibConfig::Commit()
222 const Sequence<OUString> aPropertyNames = GetPropertyNames();
223 Sequence<Any> aValues(aPropertyNames.getLength());
224 Any* pValues = aValues.getArray();
226 for(int nProp = 0; nProp < aPropertyNames.getLength(); nProp++)
228 switch(nProp)
230 case 0: pValues[nProp] <<= sDataSource; break;
231 case 1: pValues[nProp] <<= sTableOrQuery; break;
232 case 2: pValues[nProp] <<= nTblOrQuery; break;
233 case 3: pValues[nProp] <<= nBeamerSize; break;
234 case 4: pValues[nProp] <<= nViewSize; break;
235 case 5: pValues[nProp] <<= sQueryText; break;
236 case 6: pValues[nProp] <<= sQueryField; break;
237 case 7:
238 pValues[nProp].setValue(&bShowColumnAssignmentWarning, ::getBooleanCppuType());
239 break;
242 PutProperties(aPropertyNames, aValues);
243 ClearNodeSet(cDataSourceHistory);
244 Sequence< PropertyValue > aNodeValues(pMappingsArr->size() * 3);
245 PropertyValue* pNodeValues = aNodeValues.getArray();
247 sal_Int32 nIndex = 0;
248 OUString sName("DataSourceName");
249 OUString sTable("Command");
250 OUString sCommandType("CommandType");
251 for(sal_Int32 i = 0; i < (sal_Int32)pMappingsArr->size(); i++)
253 const Mapping* pMapping = &(*pMappingsArr)[i];
254 OUString sPrefix(cDataSourceHistory);
255 sPrefix += "/_";
256 sPrefix += OUString::valueOf(i);
257 sPrefix += "/";
258 pNodeValues[nIndex].Name = sPrefix;
259 pNodeValues[nIndex].Name += sName;
260 pNodeValues[nIndex++].Value <<= pMapping->sURL;
261 pNodeValues[nIndex].Name = sPrefix;
262 pNodeValues[nIndex].Name += sTable;
263 pNodeValues[nIndex++].Value <<= pMapping->sTableName;
264 pNodeValues[nIndex].Name = sPrefix;
265 pNodeValues[nIndex].Name += sCommandType;
266 pNodeValues[nIndex++].Value <<= pMapping->nCommandType;
267 SetSetProperties(cDataSourceHistory, aNodeValues);
269 sPrefix += "Fields";
270 sal_Int32 nFieldAssignment = 0;
271 OUString sFieldName = "/ProgrammaticFieldName";
272 OUString sDatabaseFieldName = "/AssignedFieldName";
273 ClearNodeSet( sPrefix );
275 while(nFieldAssignment < COLUMN_COUNT &&
276 !pMapping->aColumnPairs[nFieldAssignment].sLogicalColumnName.isEmpty())
278 OUString sSubPrefix(sPrefix);
279 sSubPrefix += "/_";
280 sSubPrefix += OUString::valueOf(nFieldAssignment);
281 Sequence< PropertyValue > aAssignmentValues(2);
282 PropertyValue* pAssignmentValues = aAssignmentValues.getArray();
283 pAssignmentValues[0].Name = sSubPrefix;
284 pAssignmentValues[0].Name += sFieldName;
285 pAssignmentValues[0].Value <<= pMapping->aColumnPairs[nFieldAssignment].sLogicalColumnName;
286 pAssignmentValues[1].Name = sSubPrefix;
287 pAssignmentValues[1].Name += sDatabaseFieldName;
288 pAssignmentValues[1].Value <<= pMapping->aColumnPairs[nFieldAssignment].sRealColumnName;
289 SetSetProperties( sPrefix, aAssignmentValues );
290 nFieldAssignment++;
295 const Mapping* BibConfig::GetMapping(const BibDBDescriptor& rDesc) const
297 for(sal_uInt16 i = 0; i < pMappingsArr->size(); i++)
299 Mapping& rMapping = (*pMappingsArr)[i];
300 sal_Bool bURLEqual = rDesc.sDataSource.equals(rMapping.sURL);
301 if(rDesc.sTableOrQuery == rMapping.sTableName && bURLEqual)
302 return &rMapping;
304 return 0;
307 void BibConfig::SetMapping(const BibDBDescriptor& rDesc, const Mapping* pSetMapping)
309 for(sal_uInt16 i = 0; i < pMappingsArr->size(); i++)
311 Mapping& rMapping = (*pMappingsArr)[i];
312 sal_Bool bURLEqual = rDesc.sDataSource.equals(rMapping.sURL);
313 if(rDesc.sTableOrQuery == rMapping.sTableName && bURLEqual)
315 pMappingsArr->erase(pMappingsArr->begin()+i);
316 break;
319 Mapping* pNew = new Mapping(*pSetMapping);
320 pMappingsArr->push_back(pNew);
321 SetModified();
324 DBChangeDialogConfig_Impl::DBChangeDialogConfig_Impl()
328 DBChangeDialogConfig_Impl::~DBChangeDialogConfig_Impl()
332 const Sequence<OUString>& DBChangeDialogConfig_Impl::GetDataSourceNames()
334 if(!aSourceNames.getLength())
336 Reference< XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
337 Reference<XDatabaseContext> xDBContext = DatabaseContext::create(xContext);
338 aSourceNames = xDBContext->getElementNames();
340 return aSourceNames;
343 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */