Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / extensions / source / bibliography / bibconfig.cxx
blobaf33237173ee977521772629f14f4c18b0e0d451
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 <memory>
22 #include <bibconfig.hxx>
23 #include <com/sun/star/uno/Sequence.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>
29 #include <o3tl/any.hxx>
30 #include <o3tl/make_unique.hxx>
32 using namespace ::com::sun::star::uno;
33 using namespace ::com::sun::star::beans;
34 using namespace ::com::sun::star::container;
35 using namespace ::com::sun::star::lang;
36 using namespace ::com::sun::star::sdb;
39 const char cDataSourceHistory[] = "DataSourceHistory";
41 Sequence<OUString> const & BibConfig::GetPropertyNames()
43 static Sequence<OUString> aNames;
44 if(!aNames.getLength())
46 aNames.realloc(8);
47 OUString* pNames = aNames.getArray();
48 pNames[0] = "CurrentDataSource/DataSourceName";
49 pNames[1] = "CurrentDataSource/Command";
50 pNames[2] = "CurrentDataSource/CommandType";
51 pNames[3] = "BeamerHeight";
52 pNames[4] = "ViewHeight";
53 pNames[5] = "QueryText";
54 pNames[6] = "QueryField";
55 pNames[7] = "ShowColumnAssignmentWarning";
57 return aNames;
60 BibConfig::BibConfig()
61 : ConfigItem("Office.DataAccess/Bibliography", ConfigItemMode::DelayedUpdate)
62 , nTblOrQuery(0)
63 , pMappingsArr(new MappingArray)
64 , nBeamerSize(0)
65 , nViewSize(0)
66 , bShowColumnAssignmentWarning(false)
68 //Names of the default columns
69 aColumnDefaults[0] = "Identifier";
70 aColumnDefaults[1] = "BibliographyType";
71 aColumnDefaults[2] = "Author";
72 aColumnDefaults[3] = "Title";
73 aColumnDefaults[4] = "Year";
74 aColumnDefaults[5] = "ISBN";
75 aColumnDefaults[6] = "Booktitle";
76 aColumnDefaults[7] = "Chapter";
77 aColumnDefaults[8] = "Edition";
78 aColumnDefaults[9] = "Editor";
79 aColumnDefaults[10] = "Howpublished";
80 aColumnDefaults[11] = "Institution";
81 aColumnDefaults[12] = "Journal";
82 aColumnDefaults[13] = "Month";
83 aColumnDefaults[14] = "Note";
84 aColumnDefaults[15] = "Annote";
85 aColumnDefaults[16] = "Number";
86 aColumnDefaults[17] = "Organizations";
87 aColumnDefaults[18] = "Pages";
88 aColumnDefaults[19] = "Publisher";
89 aColumnDefaults[20] = "Address";
90 aColumnDefaults[21] = "School";
91 aColumnDefaults[22] = "Series";
92 aColumnDefaults[23] = "ReportType";
93 aColumnDefaults[24] = "Volume";
94 aColumnDefaults[25] = "URL";
95 aColumnDefaults[26] = "Custom1";
96 aColumnDefaults[27] = "Custom2";
97 aColumnDefaults[28] = "Custom3";
98 aColumnDefaults[29] = "Custom4";
99 aColumnDefaults[30] = "Custom5";
102 const Sequence< OUString > aPropertyNames = GetPropertyNames();
103 const Sequence<Any> aPropertyValues = GetProperties( aPropertyNames );
104 const Any* pValues = aPropertyValues.getConstArray();
105 if(aPropertyValues.getLength() == aPropertyNames.getLength())
107 for(int nProp = 0; nProp < aPropertyNames.getLength(); nProp++)
109 if(pValues[nProp].hasValue())
111 switch(nProp)
113 case 0: pValues[nProp] >>= sDataSource; break;
114 case 1: pValues[nProp] >>= sTableOrQuery; break;
115 case 2: pValues[nProp] >>= nTblOrQuery; break;
116 case 3: pValues[nProp] >>= nBeamerSize; break;
117 case 4: pValues[nProp] >>= nViewSize ; break;
118 case 5: pValues[nProp] >>= sQueryText ; break;
119 case 6: pValues[nProp] >>= sQueryField; break;
120 case 7:
121 bShowColumnAssignmentWarning = *o3tl::doAccess<bool>(pValues[nProp]);
122 break;
127 OUString sName("DataSourceName");
128 OUString sTable("Command");
129 OUString sCommandType("CommandType");
130 Sequence< OUString > aNodeNames = GetNodeNames(cDataSourceHistory);
131 const OUString* pNodeNames = aNodeNames.getConstArray();
132 for(sal_Int32 nNode = 0; nNode < aNodeNames.getLength(); nNode++)
134 Sequence<OUString> aHistoryNames(3);
135 OUString* pHistoryNames = aHistoryNames.getArray();
137 OUString sPrefix(cDataSourceHistory);
138 sPrefix += "/";
139 sPrefix += pNodeNames[nNode];
140 sPrefix += "/";
141 pHistoryNames[0] = sPrefix;
142 pHistoryNames[0] += sName;
143 pHistoryNames[1] = sPrefix;
144 pHistoryNames[1] += sTable;
145 pHistoryNames[2] = sPrefix;
146 pHistoryNames[2] += sCommandType;
148 Sequence<Any> aHistoryValues = GetProperties( aHistoryNames );
149 const Any* pHistoryValues = aHistoryValues.getConstArray();
151 if(aHistoryValues.getLength() == aHistoryNames.getLength())
153 Mapping* pMapping = new Mapping;
154 pHistoryValues[0] >>= pMapping->sURL;
155 pHistoryValues[1] >>= pMapping->sTableName;
156 pHistoryValues[2] >>= pMapping->nCommandType;
157 //field assignment is contained in another set
158 sPrefix += "Fields";
159 Sequence< OUString > aAssignmentNodeNames = GetNodeNames(sPrefix);
160 const OUString* pAssignmentNodeNames = aAssignmentNodeNames.getConstArray();
161 Sequence<OUString> aAssignmentPropertyNames(aAssignmentNodeNames.getLength() * 2);
162 OUString* pAssignmentPropertyNames = aAssignmentPropertyNames.getArray();
163 sal_Int16 nFieldIdx = 0;
164 for(sal_Int32 nField = 0; nField < aAssignmentNodeNames.getLength(); nField++)
166 OUString sSubPrefix(sPrefix);
167 sSubPrefix += "/";
168 sSubPrefix += pAssignmentNodeNames[nField];
169 pAssignmentPropertyNames[nFieldIdx] = sSubPrefix;
170 pAssignmentPropertyNames[nFieldIdx++] += "/ProgrammaticFieldName";
171 pAssignmentPropertyNames[nFieldIdx] = sSubPrefix;
172 pAssignmentPropertyNames[nFieldIdx++] += "/AssignedFieldName";
174 Sequence<Any> aAssignmentValues = GetProperties(aAssignmentPropertyNames);
175 const Any* pAssignmentValues = aAssignmentValues.getConstArray();
176 OUString sTempLogical;
177 OUString sTempReal;
178 sal_Int16 nSetMapping = 0;
179 nFieldIdx = 0;
180 for(sal_Int16 nFieldVal = 0; nFieldVal < aAssignmentValues.getLength() / 2; nFieldVal++)
182 pAssignmentValues[nFieldIdx++] >>= sTempLogical;
183 pAssignmentValues[nFieldIdx++] >>= sTempReal;
184 if(!(sTempLogical.isEmpty() || sTempReal.isEmpty()))
186 pMapping->aColumnPairs[nSetMapping].sLogicalColumnName = sTempLogical;
187 pMapping->aColumnPairs[nSetMapping++].sRealColumnName = sTempReal;
190 pMappingsArr->push_back(std::unique_ptr<Mapping>(pMapping));
195 BibConfig::~BibConfig()
197 assert(!IsModified()); // should have been committed
198 delete pMappingsArr;
201 BibDBDescriptor BibConfig::GetBibliographyURL()
203 BibDBDescriptor aRet;
204 aRet.sDataSource = sDataSource;
205 aRet.sTableOrQuery = sTableOrQuery;
206 aRet.nCommandType = nTblOrQuery;
207 return aRet;
210 void BibConfig::SetBibliographyURL(const BibDBDescriptor& rDesc)
212 sDataSource = rDesc.sDataSource;
213 sTableOrQuery = rDesc.sTableOrQuery;
214 nTblOrQuery = rDesc.nCommandType;
215 SetModified();
218 void BibConfig::Notify( const css::uno::Sequence<OUString>& )
222 void BibConfig::ImplCommit()
224 PutProperties(
225 GetPropertyNames(),
226 {css::uno::Any(sDataSource), css::uno::Any(sTableOrQuery),
227 css::uno::Any(nTblOrQuery), css::uno::Any(nBeamerSize),
228 css::uno::Any(nViewSize), css::uno::Any(sQueryText),
229 css::uno::Any(sQueryField),
230 css::uno::Any(bShowColumnAssignmentWarning)});
231 ClearNodeSet(cDataSourceHistory);
232 Sequence< PropertyValue > aNodeValues(pMappingsArr->size() * 3);
233 PropertyValue* pNodeValues = aNodeValues.getArray();
235 sal_Int32 nIndex = 0;
236 OUString sName("DataSourceName");
237 OUString sTable("Command");
238 OUString sCommandType("CommandType");
239 for(sal_Int32 i = 0; i < (sal_Int32)pMappingsArr->size(); i++)
241 const Mapping* pMapping = (*pMappingsArr)[i].get();
242 OUString sPrefix(cDataSourceHistory);
243 sPrefix += "/_";
244 sPrefix += OUString::number(i);
245 sPrefix += "/";
246 pNodeValues[nIndex].Name = sPrefix;
247 pNodeValues[nIndex].Name += sName;
248 pNodeValues[nIndex++].Value <<= pMapping->sURL;
249 pNodeValues[nIndex].Name = sPrefix;
250 pNodeValues[nIndex].Name += sTable;
251 pNodeValues[nIndex++].Value <<= pMapping->sTableName;
252 pNodeValues[nIndex].Name = sPrefix;
253 pNodeValues[nIndex].Name += sCommandType;
254 pNodeValues[nIndex++].Value <<= pMapping->nCommandType;
255 SetSetProperties(cDataSourceHistory, aNodeValues);
257 sPrefix += "Fields";
258 sal_Int32 nFieldAssignment = 0;
259 OUString sFieldName = "/ProgrammaticFieldName";
260 OUString sDatabaseFieldName = "/AssignedFieldName";
261 ClearNodeSet( sPrefix );
263 while(nFieldAssignment < COLUMN_COUNT &&
264 !pMapping->aColumnPairs[nFieldAssignment].sLogicalColumnName.isEmpty())
266 OUString sSubPrefix(sPrefix);
267 sSubPrefix += "/_";
268 sSubPrefix += OUString::number(nFieldAssignment);
269 Sequence< PropertyValue > aAssignmentValues(2);
270 PropertyValue* pAssignmentValues = aAssignmentValues.getArray();
271 pAssignmentValues[0].Name = sSubPrefix;
272 pAssignmentValues[0].Name += sFieldName;
273 pAssignmentValues[0].Value <<= pMapping->aColumnPairs[nFieldAssignment].sLogicalColumnName;
274 pAssignmentValues[1].Name = sSubPrefix;
275 pAssignmentValues[1].Name += sDatabaseFieldName;
276 pAssignmentValues[1].Value <<= pMapping->aColumnPairs[nFieldAssignment].sRealColumnName;
277 SetSetProperties( sPrefix, aAssignmentValues );
278 nFieldAssignment++;
283 const Mapping* BibConfig::GetMapping(const BibDBDescriptor& rDesc) const
285 for(std::unique_ptr<Mapping> & i : *pMappingsArr)
287 Mapping& rMapping = *i.get();
288 bool bURLEqual = rDesc.sDataSource.equals(rMapping.sURL);
289 if(rDesc.sTableOrQuery == rMapping.sTableName && bURLEqual)
290 return &rMapping;
292 return nullptr;
295 void BibConfig::SetMapping(const BibDBDescriptor& rDesc, const Mapping* pSetMapping)
297 for(size_t i = 0; i < pMappingsArr->size(); i++)
299 Mapping& rMapping = *(*pMappingsArr)[i].get();
300 bool bURLEqual = rDesc.sDataSource.equals(rMapping.sURL);
301 if(rDesc.sTableOrQuery == rMapping.sTableName && bURLEqual)
303 pMappingsArr->erase(pMappingsArr->begin()+i);
304 break;
307 pMappingsArr->push_back(o3tl::make_unique<Mapping>(*pSetMapping));
308 SetModified();
311 DBChangeDialogConfig_Impl::DBChangeDialogConfig_Impl()
315 DBChangeDialogConfig_Impl::~DBChangeDialogConfig_Impl()
319 const Sequence<OUString>& DBChangeDialogConfig_Impl::GetDataSourceNames()
321 if(!aSourceNames.getLength())
323 Reference< XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
324 Reference<XDatabaseContext> xDBContext = DatabaseContext::create(xContext);
325 aSourceNames = xDBContext->getElementNames();
327 return aSourceNames;
330 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */