tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / extensions / source / bibliography / bibconfig.cxx
blob47244a595763315ffa3ccc0b98961a4dace417b0
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/sdb/DatabaseContext.hpp>
26 #include <comphelper/processfactory.hxx>
27 #include <comphelper/propertyvalue.hxx>
28 #include <o3tl/any.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::sdb;
36 constexpr OUString cDataSourceHistory = u"DataSourceHistory"_ustr;
38 Sequence<OUString> const & BibConfig::GetPropertyNames()
40 static Sequence<OUString> aNames =
42 u"CurrentDataSource/DataSourceName"_ustr,
43 u"CurrentDataSource/Command"_ustr,
44 u"CurrentDataSource/CommandType"_ustr,
45 u"BeamerHeight"_ustr,
46 u"ViewHeight"_ustr,
47 u"QueryText"_ustr,
48 u"QueryField"_ustr,
49 u"ShowColumnAssignmentWarning"_ustr
51 return aNames;
54 BibConfig::BibConfig()
55 : ConfigItem(u"Office.DataAccess/Bibliography"_ustr, ConfigItemMode::NONE)
56 , nTblOrQuery(0)
57 , nBeamerSize(0)
58 , nViewSize(0)
59 , bShowColumnAssignmentWarning(false)
61 //Names of the default columns
62 aColumnDefaults[0] = "Identifier";
63 aColumnDefaults[1] = "BibliographyType";
64 aColumnDefaults[2] = "Author";
65 aColumnDefaults[3] = "Title";
66 aColumnDefaults[4] = "Year";
67 aColumnDefaults[5] = "ISBN";
68 aColumnDefaults[6] = "Booktitle";
69 aColumnDefaults[7] = "Chapter";
70 aColumnDefaults[8] = "Edition";
71 aColumnDefaults[9] = "Editor";
72 aColumnDefaults[10] = "Howpublished";
73 aColumnDefaults[11] = "Institution";
74 aColumnDefaults[12] = "Journal";
75 aColumnDefaults[13] = "Month";
76 aColumnDefaults[14] = "Note";
77 aColumnDefaults[15] = "Annote";
78 aColumnDefaults[16] = "Number";
79 aColumnDefaults[17] = "Organizations";
80 aColumnDefaults[18] = "Pages";
81 aColumnDefaults[19] = "Publisher";
82 aColumnDefaults[20] = "Address";
83 aColumnDefaults[21] = "School";
84 aColumnDefaults[22] = "Series";
85 aColumnDefaults[23] = "ReportType";
86 aColumnDefaults[24] = "Volume";
87 aColumnDefaults[25] = "URL";
88 aColumnDefaults[26] = "Custom1";
89 aColumnDefaults[27] = "Custom2";
90 aColumnDefaults[28] = "Custom3";
91 aColumnDefaults[29] = "Custom4";
92 aColumnDefaults[30] = "Custom5";
93 aColumnDefaults[31] = "LocalURL";
96 const Sequence< OUString > aPropertyNames = GetPropertyNames();
97 const Sequence<Any> aPropertyValues = GetProperties( aPropertyNames );
98 if(aPropertyValues.getLength() == aPropertyNames.getLength())
100 for(int nProp = 0; nProp < aPropertyNames.getLength(); nProp++)
102 if (aPropertyValues[nProp].hasValue())
104 switch(nProp)
106 case 0: aPropertyValues[nProp] >>= sDataSource; break;
107 case 1: aPropertyValues[nProp] >>= sTableOrQuery; break;
108 case 2: aPropertyValues[nProp] >>= nTblOrQuery; break;
109 case 3: aPropertyValues[nProp] >>= nBeamerSize; break;
110 case 4: aPropertyValues[nProp] >>= nViewSize ; break;
111 case 5: aPropertyValues[nProp] >>= sQueryText ; break;
112 case 6: aPropertyValues[nProp] >>= sQueryField; break;
113 case 7:
114 bShowColumnAssignmentWarning = *o3tl::doAccess<bool>(aPropertyValues[nProp]);
115 break;
120 const Sequence< OUString > aNodeNames = GetNodeNames(cDataSourceHistory);
121 for(OUString const & nodeName : aNodeNames)
123 Sequence<OUString> aHistoryNames(3);
124 OUString* pHistoryNames = aHistoryNames.getArray();
126 OUString sPrefix = OUString::Concat(cDataSourceHistory) + "/" + nodeName + "/";
127 pHistoryNames[0] = sPrefix + "DataSourceName";
128 pHistoryNames[1] = sPrefix + "Command";
129 pHistoryNames[2] = sPrefix + "CommandType";
131 Sequence<Any> aHistoryValues = GetProperties( aHistoryNames );
133 if(aHistoryValues.getLength() == aHistoryNames.getLength())
135 Mapping* pMapping = new Mapping;
136 aHistoryValues[0] >>= pMapping->sURL;
137 aHistoryValues[1] >>= pMapping->sTableName;
138 aHistoryValues[2] >>= pMapping->nCommandType;
139 //field assignment is contained in another set
140 sPrefix += "Fields";
141 const Sequence< OUString > aAssignmentNodeNames = GetNodeNames(sPrefix);
142 Sequence<OUString> aAssignmentPropertyNames(aAssignmentNodeNames.getLength() * 2);
143 OUString* pAssignmentPropertyNames = aAssignmentPropertyNames.getArray();
144 sal_Int16 nFieldIdx = 0;
145 for(OUString const & assignName : aAssignmentNodeNames)
147 OUString sSubPrefix = sPrefix + "/" + assignName;
148 pAssignmentPropertyNames[nFieldIdx] = sSubPrefix;
149 pAssignmentPropertyNames[nFieldIdx++] += "/ProgrammaticFieldName";
150 pAssignmentPropertyNames[nFieldIdx] = sSubPrefix;
151 pAssignmentPropertyNames[nFieldIdx++] += "/AssignedFieldName";
153 Sequence<Any> aAssignmentValues = GetProperties(aAssignmentPropertyNames);
154 OUString sTempLogical;
155 OUString sTempReal;
156 sal_Int16 nSetMapping = 0;
157 nFieldIdx = 0;
158 for(sal_Int32 nFieldVal = 0; nFieldVal < aAssignmentValues.getLength() / 2; nFieldVal++)
160 aAssignmentValues[nFieldIdx++] >>= sTempLogical;
161 aAssignmentValues[nFieldIdx++] >>= sTempReal;
162 if(!(sTempLogical.isEmpty() || sTempReal.isEmpty()))
164 pMapping->aColumnPairs[nSetMapping].sLogicalColumnName = sTempLogical;
165 pMapping->aColumnPairs[nSetMapping++].sRealColumnName = sTempReal;
168 mvMappings.push_back(std::unique_ptr<Mapping>(pMapping));
173 BibConfig::~BibConfig()
175 assert(!IsModified()); // should have been committed
178 BibDBDescriptor BibConfig::GetBibliographyURL()
180 BibDBDescriptor aRet;
181 aRet.sDataSource = sDataSource;
182 aRet.sTableOrQuery = sTableOrQuery;
183 aRet.nCommandType = nTblOrQuery;
184 return aRet;
187 void BibConfig::SetBibliographyURL(const BibDBDescriptor& rDesc)
189 sDataSource = rDesc.sDataSource;
190 sTableOrQuery = rDesc.sTableOrQuery;
191 nTblOrQuery = rDesc.nCommandType;
192 SetModified();
195 void BibConfig::Notify( const css::uno::Sequence<OUString>& )
199 void BibConfig::ImplCommit()
201 PutProperties(
202 GetPropertyNames(),
203 {css::uno::Any(sDataSource), css::uno::Any(sTableOrQuery),
204 css::uno::Any(nTblOrQuery), css::uno::Any(nBeamerSize),
205 css::uno::Any(nViewSize), css::uno::Any(sQueryText),
206 css::uno::Any(sQueryField),
207 css::uno::Any(bShowColumnAssignmentWarning)});
208 ClearNodeSet(cDataSourceHistory);
209 Sequence< PropertyValue > aNodeValues(mvMappings.size() * 3);
210 PropertyValue* pNodeValues = aNodeValues.getArray();
212 sal_Int32 nIndex = 0;
213 for(sal_Int32 i = 0; i < static_cast<sal_Int32>(mvMappings.size()); i++)
215 const Mapping* pMapping = mvMappings[i].get();
216 OUString sPrefix = OUString::Concat(cDataSourceHistory) + "/_" + OUString::number(i) + "/";
217 pNodeValues[nIndex].Name = sPrefix + "DataSourceName";
218 pNodeValues[nIndex++].Value <<= pMapping->sURL;
219 pNodeValues[nIndex].Name = sPrefix + "Command";
220 pNodeValues[nIndex++].Value <<= pMapping->sTableName;
221 pNodeValues[nIndex].Name = sPrefix + "CommandType";
222 pNodeValues[nIndex++].Value <<= pMapping->nCommandType;
223 SetSetProperties(cDataSourceHistory, aNodeValues);
225 sPrefix += "Fields";
226 sal_Int32 nFieldAssignment = 0;
227 OUString sFieldName = u"/ProgrammaticFieldName"_ustr;
228 OUString sDatabaseFieldName = u"/AssignedFieldName"_ustr;
229 ClearNodeSet( sPrefix );
231 while(nFieldAssignment < COLUMN_COUNT &&
232 !pMapping->aColumnPairs[nFieldAssignment].sLogicalColumnName.isEmpty())
234 OUString sSubPrefix = sPrefix + "/_" + OUString::number(nFieldAssignment);
235 Sequence< PropertyValue > aAssignmentValues
237 comphelper::makePropertyValue(sSubPrefix + sFieldName, pMapping->aColumnPairs[nFieldAssignment].sLogicalColumnName),
238 comphelper::makePropertyValue(sSubPrefix + sDatabaseFieldName, pMapping->aColumnPairs[nFieldAssignment].sRealColumnName)
240 SetSetProperties( sPrefix, aAssignmentValues );
241 nFieldAssignment++;
246 const Mapping* BibConfig::GetMapping(const BibDBDescriptor& rDesc) const
248 for(std::unique_ptr<Mapping> const & i : mvMappings)
250 Mapping& rMapping = *i;
251 bool bURLEqual = rDesc.sDataSource == rMapping.sURL;
252 if(rDesc.sTableOrQuery == rMapping.sTableName && bURLEqual)
253 return &rMapping;
255 return nullptr;
258 void BibConfig::SetMapping(const BibDBDescriptor& rDesc, const Mapping* pSetMapping)
260 for(size_t i = 0; i < mvMappings.size(); i++)
262 Mapping& rMapping = *mvMappings[i];
263 bool bURLEqual = rDesc.sDataSource == rMapping.sURL;
264 if(rDesc.sTableOrQuery == rMapping.sTableName && bURLEqual)
266 mvMappings.erase(mvMappings.begin()+i);
267 break;
270 mvMappings.push_back(std::make_unique<Mapping>(*pSetMapping));
271 SetModified();
274 DBChangeDialogConfig_Impl::DBChangeDialogConfig_Impl()
278 DBChangeDialogConfig_Impl::~DBChangeDialogConfig_Impl()
282 const Sequence<OUString>& DBChangeDialogConfig_Impl::GetDataSourceNames()
284 if(!aSourceNames.hasElements())
286 const Reference< XComponentContext >& xContext( ::comphelper::getProcessComponentContext() );
287 Reference<XDatabaseContext> xDBContext = DatabaseContext::create(xContext);
288 aSourceNames = xDBContext->getElementNames();
290 return aSourceNames;
293 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */