bump product version to 6.3.0.0.beta1
[LibreOffice.git] / extensions / source / bibliography / bibconfig.cxx
blobdaaa3702b03b65131a8b18dfc27a9a231f4812b5
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>
31 using namespace ::com::sun::star::uno;
32 using namespace ::com::sun::star::beans;
33 using namespace ::com::sun::star::container;
34 using namespace ::com::sun::star::lang;
35 using namespace ::com::sun::star::sdb;
38 const char cDataSourceHistory[] = "DataSourceHistory";
40 Sequence<OUString> const & BibConfig::GetPropertyNames()
42 static Sequence<OUString> aNames =
44 "CurrentDataSource/DataSourceName",
45 "CurrentDataSource/Command",
46 "CurrentDataSource/CommandType",
47 "BeamerHeight",
48 "ViewHeight",
49 "QueryText",
50 "QueryField",
51 "ShowColumnAssignmentWarning"
53 return aNames;
56 BibConfig::BibConfig()
57 : ConfigItem("Office.DataAccess/Bibliography", ConfigItemMode::NONE)
58 , nTblOrQuery(0)
59 , nBeamerSize(0)
60 , nViewSize(0)
61 , bShowColumnAssignmentWarning(false)
63 //Names of the default columns
64 aColumnDefaults[0] = "Identifier";
65 aColumnDefaults[1] = "BibliographyType";
66 aColumnDefaults[2] = "Author";
67 aColumnDefaults[3] = "Title";
68 aColumnDefaults[4] = "Year";
69 aColumnDefaults[5] = "ISBN";
70 aColumnDefaults[6] = "Booktitle";
71 aColumnDefaults[7] = "Chapter";
72 aColumnDefaults[8] = "Edition";
73 aColumnDefaults[9] = "Editor";
74 aColumnDefaults[10] = "Howpublished";
75 aColumnDefaults[11] = "Institution";
76 aColumnDefaults[12] = "Journal";
77 aColumnDefaults[13] = "Month";
78 aColumnDefaults[14] = "Note";
79 aColumnDefaults[15] = "Annote";
80 aColumnDefaults[16] = "Number";
81 aColumnDefaults[17] = "Organizations";
82 aColumnDefaults[18] = "Pages";
83 aColumnDefaults[19] = "Publisher";
84 aColumnDefaults[20] = "Address";
85 aColumnDefaults[21] = "School";
86 aColumnDefaults[22] = "Series";
87 aColumnDefaults[23] = "ReportType";
88 aColumnDefaults[24] = "Volume";
89 aColumnDefaults[25] = "URL";
90 aColumnDefaults[26] = "Custom1";
91 aColumnDefaults[27] = "Custom2";
92 aColumnDefaults[28] = "Custom3";
93 aColumnDefaults[29] = "Custom4";
94 aColumnDefaults[30] = "Custom5";
97 const Sequence< OUString > aPropertyNames = GetPropertyNames();
98 const Sequence<Any> aPropertyValues = GetProperties( aPropertyNames );
99 const Any* pValues = aPropertyValues.getConstArray();
100 if(aPropertyValues.getLength() == aPropertyNames.getLength())
102 for(int nProp = 0; nProp < aPropertyNames.getLength(); nProp++)
104 if(pValues[nProp].hasValue())
106 switch(nProp)
108 case 0: pValues[nProp] >>= sDataSource; break;
109 case 1: pValues[nProp] >>= sTableOrQuery; break;
110 case 2: pValues[nProp] >>= nTblOrQuery; break;
111 case 3: pValues[nProp] >>= nBeamerSize; break;
112 case 4: pValues[nProp] >>= nViewSize ; break;
113 case 5: pValues[nProp] >>= sQueryText ; break;
114 case 6: pValues[nProp] >>= sQueryField; break;
115 case 7:
116 bShowColumnAssignmentWarning = *o3tl::doAccess<bool>(pValues[nProp]);
117 break;
122 Sequence< OUString > aNodeNames = GetNodeNames(cDataSourceHistory);
123 const OUString* pNodeNames = aNodeNames.getConstArray();
124 for(sal_Int32 nNode = 0; nNode < aNodeNames.getLength(); nNode++)
126 Sequence<OUString> aHistoryNames(3);
127 OUString* pHistoryNames = aHistoryNames.getArray();
129 OUString sPrefix(cDataSourceHistory);
130 sPrefix += "/";
131 sPrefix += pNodeNames[nNode];
132 sPrefix += "/";
133 pHistoryNames[0] = sPrefix + "DataSourceName";
134 pHistoryNames[1] = sPrefix + "Command";
135 pHistoryNames[2] = sPrefix + "CommandType";
137 Sequence<Any> aHistoryValues = GetProperties( aHistoryNames );
138 const Any* pHistoryValues = aHistoryValues.getConstArray();
140 if(aHistoryValues.getLength() == aHistoryNames.getLength())
142 Mapping* pMapping = new Mapping;
143 pHistoryValues[0] >>= pMapping->sURL;
144 pHistoryValues[1] >>= pMapping->sTableName;
145 pHistoryValues[2] >>= pMapping->nCommandType;
146 //field assignment is contained in another set
147 sPrefix += "Fields";
148 Sequence< OUString > aAssignmentNodeNames = GetNodeNames(sPrefix);
149 const OUString* pAssignmentNodeNames = aAssignmentNodeNames.getConstArray();
150 Sequence<OUString> aAssignmentPropertyNames(aAssignmentNodeNames.getLength() * 2);
151 OUString* pAssignmentPropertyNames = aAssignmentPropertyNames.getArray();
152 sal_Int16 nFieldIdx = 0;
153 for(sal_Int32 nField = 0; nField < aAssignmentNodeNames.getLength(); nField++)
155 OUString sSubPrefix(sPrefix);
156 sSubPrefix += "/";
157 sSubPrefix += pAssignmentNodeNames[nField];
158 pAssignmentPropertyNames[nFieldIdx] = sSubPrefix;
159 pAssignmentPropertyNames[nFieldIdx++] += "/ProgrammaticFieldName";
160 pAssignmentPropertyNames[nFieldIdx] = sSubPrefix;
161 pAssignmentPropertyNames[nFieldIdx++] += "/AssignedFieldName";
163 Sequence<Any> aAssignmentValues = GetProperties(aAssignmentPropertyNames);
164 const Any* pAssignmentValues = aAssignmentValues.getConstArray();
165 OUString sTempLogical;
166 OUString sTempReal;
167 sal_Int16 nSetMapping = 0;
168 nFieldIdx = 0;
169 for(sal_Int32 nFieldVal = 0; nFieldVal < aAssignmentValues.getLength() / 2; nFieldVal++)
171 pAssignmentValues[nFieldIdx++] >>= sTempLogical;
172 pAssignmentValues[nFieldIdx++] >>= sTempReal;
173 if(!(sTempLogical.isEmpty() || sTempReal.isEmpty()))
175 pMapping->aColumnPairs[nSetMapping].sLogicalColumnName = sTempLogical;
176 pMapping->aColumnPairs[nSetMapping++].sRealColumnName = sTempReal;
179 mvMappings.push_back(std::unique_ptr<Mapping>(pMapping));
184 BibConfig::~BibConfig()
186 assert(!IsModified()); // should have been committed
189 BibDBDescriptor BibConfig::GetBibliographyURL()
191 BibDBDescriptor aRet;
192 aRet.sDataSource = sDataSource;
193 aRet.sTableOrQuery = sTableOrQuery;
194 aRet.nCommandType = nTblOrQuery;
195 return aRet;
198 void BibConfig::SetBibliographyURL(const BibDBDescriptor& rDesc)
200 sDataSource = rDesc.sDataSource;
201 sTableOrQuery = rDesc.sTableOrQuery;
202 nTblOrQuery = rDesc.nCommandType;
203 SetModified();
206 void BibConfig::Notify( const css::uno::Sequence<OUString>& )
210 void BibConfig::ImplCommit()
212 PutProperties(
213 GetPropertyNames(),
214 {css::uno::Any(sDataSource), css::uno::Any(sTableOrQuery),
215 css::uno::Any(nTblOrQuery), css::uno::Any(nBeamerSize),
216 css::uno::Any(nViewSize), css::uno::Any(sQueryText),
217 css::uno::Any(sQueryField),
218 css::uno::Any(bShowColumnAssignmentWarning)});
219 ClearNodeSet(cDataSourceHistory);
220 Sequence< PropertyValue > aNodeValues(mvMappings.size() * 3);
221 PropertyValue* pNodeValues = aNodeValues.getArray();
223 sal_Int32 nIndex = 0;
224 for(sal_Int32 i = 0; i < static_cast<sal_Int32>(mvMappings.size()); i++)
226 const Mapping* pMapping = mvMappings[i].get();
227 OUString sPrefix(cDataSourceHistory);
228 sPrefix += "/_";
229 sPrefix += OUString::number(i);
230 sPrefix += "/";
231 pNodeValues[nIndex].Name = sPrefix + "DataSourceName";
232 pNodeValues[nIndex++].Value <<= pMapping->sURL;
233 pNodeValues[nIndex].Name = sPrefix + "Command";
234 pNodeValues[nIndex++].Value <<= pMapping->sTableName;
235 pNodeValues[nIndex].Name = sPrefix + "CommandType";
236 pNodeValues[nIndex++].Value <<= pMapping->nCommandType;
237 SetSetProperties(cDataSourceHistory, aNodeValues);
239 sPrefix += "Fields";
240 sal_Int32 nFieldAssignment = 0;
241 OUString sFieldName = "/ProgrammaticFieldName";
242 OUString sDatabaseFieldName = "/AssignedFieldName";
243 ClearNodeSet( sPrefix );
245 while(nFieldAssignment < COLUMN_COUNT &&
246 !pMapping->aColumnPairs[nFieldAssignment].sLogicalColumnName.isEmpty())
248 OUString sSubPrefix(sPrefix);
249 sSubPrefix += "/_";
250 sSubPrefix += OUString::number(nFieldAssignment);
251 Sequence< PropertyValue > aAssignmentValues(2);
252 PropertyValue* pAssignmentValues = aAssignmentValues.getArray();
253 pAssignmentValues[0].Name = sSubPrefix;
254 pAssignmentValues[0].Name += sFieldName;
255 pAssignmentValues[0].Value <<= pMapping->aColumnPairs[nFieldAssignment].sLogicalColumnName;
256 pAssignmentValues[1].Name = sSubPrefix;
257 pAssignmentValues[1].Name += sDatabaseFieldName;
258 pAssignmentValues[1].Value <<= pMapping->aColumnPairs[nFieldAssignment].sRealColumnName;
259 SetSetProperties( sPrefix, aAssignmentValues );
260 nFieldAssignment++;
265 const Mapping* BibConfig::GetMapping(const BibDBDescriptor& rDesc) const
267 for(std::unique_ptr<Mapping> const & i : mvMappings)
269 Mapping& rMapping = *i;
270 bool bURLEqual = rDesc.sDataSource == rMapping.sURL;
271 if(rDesc.sTableOrQuery == rMapping.sTableName && bURLEqual)
272 return &rMapping;
274 return nullptr;
277 void BibConfig::SetMapping(const BibDBDescriptor& rDesc, const Mapping* pSetMapping)
279 for(size_t i = 0; i < mvMappings.size(); i++)
281 Mapping& rMapping = *mvMappings[i];
282 bool bURLEqual = rDesc.sDataSource == rMapping.sURL;
283 if(rDesc.sTableOrQuery == rMapping.sTableName && bURLEqual)
285 mvMappings.erase(mvMappings.begin()+i);
286 break;
289 mvMappings.push_back(std::make_unique<Mapping>(*pSetMapping));
290 SetModified();
293 DBChangeDialogConfig_Impl::DBChangeDialogConfig_Impl()
297 DBChangeDialogConfig_Impl::~DBChangeDialogConfig_Impl()
301 const Sequence<OUString>& DBChangeDialogConfig_Impl::GetDataSourceNames()
303 if(!aSourceNames.hasElements())
305 Reference< XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
306 Reference<XDatabaseContext> xDBContext = DatabaseContext::create(xContext);
307 aSourceNames = xDBContext->getElementNames();
309 return aSourceNames;
312 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */