Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / extensions / source / bibliography / bibconfig.cxx
blob986b9f700eaeb13388ace8a7ea9de7a0ae084dd1
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 static 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 = OUStringLiteral(cDataSourceHistory) + "/" + pNodeNames[nNode] + "/";
130 pHistoryNames[0] = sPrefix + "DataSourceName";
131 pHistoryNames[1] = sPrefix + "Command";
132 pHistoryNames[2] = sPrefix + "CommandType";
134 Sequence<Any> aHistoryValues = GetProperties( aHistoryNames );
135 const Any* pHistoryValues = aHistoryValues.getConstArray();
137 if(aHistoryValues.getLength() == aHistoryNames.getLength())
139 Mapping* pMapping = new Mapping;
140 pHistoryValues[0] >>= pMapping->sURL;
141 pHistoryValues[1] >>= pMapping->sTableName;
142 pHistoryValues[2] >>= pMapping->nCommandType;
143 //field assignment is contained in another set
144 sPrefix += "Fields";
145 Sequence< OUString > aAssignmentNodeNames = GetNodeNames(sPrefix);
146 const OUString* pAssignmentNodeNames = aAssignmentNodeNames.getConstArray();
147 Sequence<OUString> aAssignmentPropertyNames(aAssignmentNodeNames.getLength() * 2);
148 OUString* pAssignmentPropertyNames = aAssignmentPropertyNames.getArray();
149 sal_Int16 nFieldIdx = 0;
150 for(sal_Int32 nField = 0; nField < aAssignmentNodeNames.getLength(); nField++)
152 OUString sSubPrefix = sPrefix + "/" + pAssignmentNodeNames[nField];
153 pAssignmentPropertyNames[nFieldIdx] = sSubPrefix;
154 pAssignmentPropertyNames[nFieldIdx++] += "/ProgrammaticFieldName";
155 pAssignmentPropertyNames[nFieldIdx] = sSubPrefix;
156 pAssignmentPropertyNames[nFieldIdx++] += "/AssignedFieldName";
158 Sequence<Any> aAssignmentValues = GetProperties(aAssignmentPropertyNames);
159 const Any* pAssignmentValues = aAssignmentValues.getConstArray();
160 OUString sTempLogical;
161 OUString sTempReal;
162 sal_Int16 nSetMapping = 0;
163 nFieldIdx = 0;
164 for(sal_Int32 nFieldVal = 0; nFieldVal < aAssignmentValues.getLength() / 2; nFieldVal++)
166 pAssignmentValues[nFieldIdx++] >>= sTempLogical;
167 pAssignmentValues[nFieldIdx++] >>= sTempReal;
168 if(!(sTempLogical.isEmpty() || sTempReal.isEmpty()))
170 pMapping->aColumnPairs[nSetMapping].sLogicalColumnName = sTempLogical;
171 pMapping->aColumnPairs[nSetMapping++].sRealColumnName = sTempReal;
174 mvMappings.push_back(std::unique_ptr<Mapping>(pMapping));
179 BibConfig::~BibConfig()
181 assert(!IsModified()); // should have been committed
184 BibDBDescriptor BibConfig::GetBibliographyURL()
186 BibDBDescriptor aRet;
187 aRet.sDataSource = sDataSource;
188 aRet.sTableOrQuery = sTableOrQuery;
189 aRet.nCommandType = nTblOrQuery;
190 return aRet;
193 void BibConfig::SetBibliographyURL(const BibDBDescriptor& rDesc)
195 sDataSource = rDesc.sDataSource;
196 sTableOrQuery = rDesc.sTableOrQuery;
197 nTblOrQuery = rDesc.nCommandType;
198 SetModified();
201 void BibConfig::Notify( const css::uno::Sequence<OUString>& )
205 void BibConfig::ImplCommit()
207 PutProperties(
208 GetPropertyNames(),
209 {css::uno::Any(sDataSource), css::uno::Any(sTableOrQuery),
210 css::uno::Any(nTblOrQuery), css::uno::Any(nBeamerSize),
211 css::uno::Any(nViewSize), css::uno::Any(sQueryText),
212 css::uno::Any(sQueryField),
213 css::uno::Any(bShowColumnAssignmentWarning)});
214 ClearNodeSet(cDataSourceHistory);
215 Sequence< PropertyValue > aNodeValues(mvMappings.size() * 3);
216 PropertyValue* pNodeValues = aNodeValues.getArray();
218 sal_Int32 nIndex = 0;
219 for(sal_Int32 i = 0; i < static_cast<sal_Int32>(mvMappings.size()); i++)
221 const Mapping* pMapping = mvMappings[i].get();
222 OUString sPrefix = OUStringLiteral(cDataSourceHistory) + "/_" + OUString::number(i) + "/";
223 pNodeValues[nIndex].Name = sPrefix + "DataSourceName";
224 pNodeValues[nIndex++].Value <<= pMapping->sURL;
225 pNodeValues[nIndex].Name = sPrefix + "Command";
226 pNodeValues[nIndex++].Value <<= pMapping->sTableName;
227 pNodeValues[nIndex].Name = sPrefix + "CommandType";
228 pNodeValues[nIndex++].Value <<= pMapping->nCommandType;
229 SetSetProperties(cDataSourceHistory, aNodeValues);
231 sPrefix += "Fields";
232 sal_Int32 nFieldAssignment = 0;
233 OUString sFieldName = "/ProgrammaticFieldName";
234 OUString sDatabaseFieldName = "/AssignedFieldName";
235 ClearNodeSet( sPrefix );
237 while(nFieldAssignment < COLUMN_COUNT &&
238 !pMapping->aColumnPairs[nFieldAssignment].sLogicalColumnName.isEmpty())
240 OUString sSubPrefix = sPrefix + "/_" + OUString::number(nFieldAssignment);
241 Sequence< PropertyValue > aAssignmentValues(2);
242 PropertyValue* pAssignmentValues = aAssignmentValues.getArray();
243 pAssignmentValues[0].Name = sSubPrefix;
244 pAssignmentValues[0].Name += sFieldName;
245 pAssignmentValues[0].Value <<= pMapping->aColumnPairs[nFieldAssignment].sLogicalColumnName;
246 pAssignmentValues[1].Name = sSubPrefix;
247 pAssignmentValues[1].Name += sDatabaseFieldName;
248 pAssignmentValues[1].Value <<= pMapping->aColumnPairs[nFieldAssignment].sRealColumnName;
249 SetSetProperties( sPrefix, aAssignmentValues );
250 nFieldAssignment++;
255 const Mapping* BibConfig::GetMapping(const BibDBDescriptor& rDesc) const
257 for(std::unique_ptr<Mapping> const & i : mvMappings)
259 Mapping& rMapping = *i;
260 bool bURLEqual = rDesc.sDataSource == rMapping.sURL;
261 if(rDesc.sTableOrQuery == rMapping.sTableName && bURLEqual)
262 return &rMapping;
264 return nullptr;
267 void BibConfig::SetMapping(const BibDBDescriptor& rDesc, const Mapping* pSetMapping)
269 for(size_t i = 0; i < mvMappings.size(); i++)
271 Mapping& rMapping = *mvMappings[i];
272 bool bURLEqual = rDesc.sDataSource == rMapping.sURL;
273 if(rDesc.sTableOrQuery == rMapping.sTableName && bURLEqual)
275 mvMappings.erase(mvMappings.begin()+i);
276 break;
279 mvMappings.push_back(std::make_unique<Mapping>(*pSetMapping));
280 SetModified();
283 DBChangeDialogConfig_Impl::DBChangeDialogConfig_Impl()
287 DBChangeDialogConfig_Impl::~DBChangeDialogConfig_Impl()
291 const Sequence<OUString>& DBChangeDialogConfig_Impl::GetDataSourceNames()
293 if(!aSourceNames.hasElements())
295 Reference< XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
296 Reference<XDatabaseContext> xDBContext = DatabaseContext::create(xContext);
297 aSourceNames = xDBContext->getElementNames();
299 return aSourceNames;
302 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */