merge the formfield patch from ooo-build
[ooovba.git] / extensions / source / bibliography / bibconfig.cxx
blob248fbe2175f7bbc6a1fc2af868e6ab6649c5b0a2
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: bibconfig.cxx,v $
10 * $Revision: 1.17 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_extensions.hxx"
34 #include <bibconfig.hxx>
35 #include <svtools/svarray.hxx>
36 #include <tools/debug.hxx>
37 #include <com/sun/star/uno/Sequence.hxx>
38 #include <com/sun/star/uno/Any.hxx>
39 #include <com/sun/star/beans/PropertyValue.hpp>
40 #include <com/sun/star/container/XNameAccess.hpp>
41 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
42 #include <comphelper/processfactory.hxx>
44 using namespace rtl;
45 using namespace ::com::sun::star::uno;
46 using namespace ::com::sun::star::beans;
47 using namespace ::com::sun::star::container;
48 using namespace ::com::sun::star::lang;
49 /* -----------------11.11.99 14:34-------------------
51 --------------------------------------------------*/
52 typedef Mapping* MappingPtr;
53 SV_DECL_PTRARR_DEL(MappingArray, MappingPtr, 2, 2)
54 SV_IMPL_PTRARR(MappingArray, MappingPtr);
56 #define C2U(cChar) OUString::createFromAscii(cChar)
58 const char* cDataSourceHistory = "DataSourceHistory";
59 /* -----------------------------13.11.00 12:21--------------------------------
61 ---------------------------------------------------------------------------*/
62 Sequence<OUString> BibConfig::GetPropertyNames()
64 static Sequence<OUString> aNames;
65 if(!aNames.getLength())
67 aNames.realloc(8);
68 OUString* pNames = aNames.getArray();
69 pNames[0] = C2U("CurrentDataSource/DataSourceName");
70 pNames[1] = C2U("CurrentDataSource/Command");
71 pNames[2] = C2U("CurrentDataSource/CommandType");
72 pNames[3] = C2U("BeamerHeight");
73 pNames[4] = C2U("ViewHeight");
74 pNames[5] = C2U("QueryText");
75 pNames[6] = C2U("QueryField");
76 pNames[7] = C2U("ShowColumnAssignmentWarning");
78 return aNames;
80 /* -----------------------------13.11.00 11:00--------------------------------
82 ---------------------------------------------------------------------------*/
83 BibConfig::BibConfig() :
84 ConfigItem(C2U("Office.DataAccess/Bibliography"), CONFIG_MODE_DELAYED_UPDATE),
85 pMappingsArr(new MappingArray),
86 nBeamerSize(0),
87 nViewSize(0),
88 bShowColumnAssignmentWarning(sal_False)
90 //Names of the default columns
91 aColumnDefaults[0] = C2U("Identifier");
92 aColumnDefaults[1] = C2U("BibliographyType");
93 aColumnDefaults[2] = C2U("Author");
94 aColumnDefaults[3] = C2U("Title");
95 aColumnDefaults[4] = C2U("Year");
96 aColumnDefaults[5] = C2U("ISBN");
97 aColumnDefaults[6] = C2U("Booktitle");
98 aColumnDefaults[7] = C2U("Chapter");
99 aColumnDefaults[8] = C2U("Edition");
100 aColumnDefaults[9] = C2U("Editor");
101 aColumnDefaults[10] = C2U("Howpublished");
102 aColumnDefaults[11] = C2U("Institution");
103 aColumnDefaults[12] = C2U("Journal");
104 aColumnDefaults[13] = C2U("Month");
105 aColumnDefaults[14] = C2U("Note");
106 aColumnDefaults[15] = C2U("Annote");
107 aColumnDefaults[16] = C2U("Number");
108 aColumnDefaults[17] = C2U("Organizations");
109 aColumnDefaults[18] = C2U("Pages");
110 aColumnDefaults[19] = C2U("Publisher");
111 aColumnDefaults[20] = C2U("Address");
112 aColumnDefaults[21] = C2U("School");
113 aColumnDefaults[22] = C2U("Series");
114 aColumnDefaults[23] = C2U("ReportType");
115 aColumnDefaults[24] = C2U("Volume");
116 aColumnDefaults[25] = C2U("URL");
117 aColumnDefaults[26] = C2U("Custom1");
118 aColumnDefaults[27] = C2U("Custom2");
119 aColumnDefaults[28] = C2U("Custom3");
120 aColumnDefaults[29] = C2U("Custom4");
121 aColumnDefaults[30] = C2U("Custom5");
124 const Sequence< OUString > aPropertyNames = GetPropertyNames();
125 const Sequence<Any> aPropertyValues = GetProperties( aPropertyNames );
126 const Any* pValues = aPropertyValues.getConstArray();
127 if(aPropertyValues.getLength() == aPropertyNames.getLength())
129 for(int nProp = 0; nProp < aPropertyNames.getLength(); nProp++)
131 if(pValues[nProp].hasValue())
133 switch(nProp)
135 case 0: pValues[nProp] >>= sDataSource; break;
136 case 1: pValues[nProp] >>= sTableOrQuery; break;
137 case 2: pValues[nProp] >>= nTblOrQuery; break;
138 case 3: pValues[nProp] >>= nBeamerSize; break;
139 case 4: pValues[nProp] >>= nViewSize ; break;
140 case 5: pValues[nProp] >>= sQueryText ; break;
141 case 6: pValues[nProp] >>= sQueryField; break;
142 case 7:
143 bShowColumnAssignmentWarning = *(sal_Bool*)pValues[nProp].getValue();
144 break;
149 OUString sName(C2U("DataSourceName"));
150 OUString sTable(C2U("Command"));
151 OUString sCommandType(C2U("CommandType"));
152 Sequence< OUString > aNodeNames = GetNodeNames(C2U(cDataSourceHistory));
153 const OUString* pNodeNames = aNodeNames.getConstArray();
154 for(sal_Int32 nNode = 0; nNode < aNodeNames.getLength(); nNode++)
156 Sequence<OUString> aHistoryNames(3);
157 OUString* pHistoryNames = aHistoryNames.getArray();
159 OUString sPrefix(C2U(cDataSourceHistory));
160 sPrefix += C2U("/");
161 sPrefix += pNodeNames[nNode];
162 sPrefix += C2U("/");
163 pHistoryNames[0] = sPrefix;
164 pHistoryNames[0] += sName;
165 pHistoryNames[1] = sPrefix;
166 pHistoryNames[1] += sTable;
167 pHistoryNames[2] = sPrefix;
168 pHistoryNames[2] += sCommandType;
170 Sequence<Any> aHistoryValues = GetProperties( aHistoryNames );
171 const Any* pHistoryValues = aHistoryValues.getConstArray();
173 if(aHistoryValues.getLength() == aHistoryNames.getLength())
175 Mapping* pMapping = new Mapping;
176 pHistoryValues[0] >>= pMapping->sURL;
177 pHistoryValues[1] >>= pMapping->sTableName;
178 pHistoryValues[2] >>= pMapping->nCommandType;
179 //field assignment is contained in another set
180 sPrefix += C2U("Fields");
181 Sequence< OUString > aAssignmentNodeNames = GetNodeNames(sPrefix);
182 const OUString* pAssignmentNodeNames = aAssignmentNodeNames.getConstArray();
183 Sequence<OUString> aAssignmentPropertyNames(aAssignmentNodeNames.getLength() * 2);
184 OUString* pAssignmentPropertyNames = aAssignmentPropertyNames.getArray();
185 sal_Int16 nFieldIdx = 0;
186 for(sal_Int16 nField = 0; nField < aAssignmentNodeNames.getLength(); nField++)
188 OUString sSubPrefix(sPrefix);
189 sSubPrefix += C2U("/");
190 sSubPrefix += pAssignmentNodeNames[nField];
191 pAssignmentPropertyNames[nFieldIdx] = sSubPrefix;
192 pAssignmentPropertyNames[nFieldIdx++] += C2U("/ProgrammaticFieldName");
193 pAssignmentPropertyNames[nFieldIdx] = sSubPrefix;
194 pAssignmentPropertyNames[nFieldIdx++] += C2U("/AssignedFieldName");
196 Sequence<Any> aAssignmentValues = GetProperties(aAssignmentPropertyNames);
197 const Any* pAssignmentValues = aAssignmentValues.getConstArray();
198 OUString sTempLogical;
199 OUString sTempReal;
200 sal_Int16 nSetMapping = 0;
201 nFieldIdx = 0;
202 for(sal_Int16 nFieldVal = 0; nFieldVal < aAssignmentValues.getLength() / 2; nFieldVal++)
204 pAssignmentValues[nFieldIdx++] >>= sTempLogical;
205 pAssignmentValues[nFieldIdx++] >>= sTempReal;
206 if(sTempLogical.getLength() && sTempReal.getLength())
208 pMapping->aColumnPairs[nSetMapping].sLogicalColumnName = sTempLogical;
209 pMapping->aColumnPairs[nSetMapping++].sRealColumnName = sTempReal;
212 pMappingsArr->Insert(pMapping, pMappingsArr->Count());
216 /* -----------------------------13.11.00 11:00--------------------------------
218 ---------------------------------------------------------------------------*/
219 BibConfig::~BibConfig()
221 if(IsModified())
222 Commit();
223 delete pMappingsArr;
225 /* -----------------------------13.11.00 12:08--------------------------------
227 ---------------------------------------------------------------------------*/
228 BibDBDescriptor BibConfig::GetBibliographyURL()
230 BibDBDescriptor aRet;
231 aRet.sDataSource = sDataSource;
232 aRet.sTableOrQuery = sTableOrQuery;
233 aRet.nCommandType = nTblOrQuery;
234 return aRet;
236 /* -----------------------------13.11.00 12:20--------------------------------
238 ---------------------------------------------------------------------------*/
239 void BibConfig::SetBibliographyURL(const BibDBDescriptor& rDesc)
241 sDataSource = rDesc.sDataSource;
242 sTableOrQuery = rDesc.sTableOrQuery;
243 nTblOrQuery = rDesc.nCommandType;
244 SetModified();
246 //---------------------------------------------------------------------------
247 void BibConfig::Commit()
249 const Sequence<OUString> aPropertyNames = GetPropertyNames();
250 Sequence<Any> aValues(aPropertyNames.getLength());
251 Any* pValues = aValues.getArray();
253 for(int nProp = 0; nProp < aPropertyNames.getLength(); nProp++)
255 switch(nProp)
257 case 0: pValues[nProp] <<= sDataSource; break;
258 case 1: pValues[nProp] <<= sTableOrQuery; break;
259 case 2: pValues[nProp] <<= nTblOrQuery; break;
260 case 3: pValues[nProp] <<= nBeamerSize; break;
261 case 4: pValues[nProp] <<= nViewSize; break;
262 case 5: pValues[nProp] <<= sQueryText; break;
263 case 6: pValues[nProp] <<= sQueryField; break;
264 case 7:
265 pValues[nProp].setValue(&bShowColumnAssignmentWarning, ::getBooleanCppuType());
266 break;
269 PutProperties(aPropertyNames, aValues);
270 ClearNodeSet( C2U(cDataSourceHistory));
271 OUString sEmpty;
272 Sequence< PropertyValue > aNodeValues(pMappingsArr->Count() * 3);
273 PropertyValue* pNodeValues = aNodeValues.getArray();
275 sal_Int32 nIndex = 0;
276 OUString sName(C2U("DataSourceName"));
277 OUString sTable(C2U("Command"));
278 OUString sCommandType(C2U("CommandType"));
279 for(sal_Int32 i = 0; i < pMappingsArr->Count(); i++)
281 const Mapping* pMapping = pMappingsArr->GetObject((USHORT)i);
282 OUString sPrefix(C2U(cDataSourceHistory));
283 sPrefix += C2U("/_");
284 sPrefix += OUString::valueOf(i);
285 sPrefix += C2U("/");
286 pNodeValues[nIndex].Name = sPrefix;
287 pNodeValues[nIndex].Name += sName;
288 pNodeValues[nIndex++].Value <<= pMapping->sURL;
289 pNodeValues[nIndex].Name = sPrefix;
290 pNodeValues[nIndex].Name += sTable;
291 pNodeValues[nIndex++].Value <<= pMapping->sTableName;
292 pNodeValues[nIndex].Name = sPrefix;
293 pNodeValues[nIndex].Name += sCommandType;
294 pNodeValues[nIndex++].Value <<= pMapping->nCommandType;
295 SetSetProperties( C2U(cDataSourceHistory), aNodeValues);
297 sPrefix += C2U("Fields");
298 sal_Int32 nFieldAssignment = 0;
299 OUString sFieldName = C2U("/ProgrammaticFieldName");
300 OUString sDatabaseFieldName = C2U("/AssignedFieldName");
301 ClearNodeSet( sPrefix );
303 while(nFieldAssignment < COLUMN_COUNT &&
304 pMapping->aColumnPairs[nFieldAssignment].sLogicalColumnName.getLength())
306 OUString sSubPrefix(sPrefix);
307 sSubPrefix += C2U("/_");
308 sSubPrefix += OUString::valueOf(nFieldAssignment);
309 Sequence< PropertyValue > aAssignmentValues(2);
310 PropertyValue* pAssignmentValues = aAssignmentValues.getArray();
311 pAssignmentValues[0].Name = sSubPrefix;
312 pAssignmentValues[0].Name += sFieldName;
313 pAssignmentValues[0].Value <<= pMapping->aColumnPairs[nFieldAssignment].sLogicalColumnName;
314 pAssignmentValues[1].Name = sSubPrefix;
315 pAssignmentValues[1].Name += sDatabaseFieldName;
316 pAssignmentValues[1].Value <<= pMapping->aColumnPairs[nFieldAssignment].sRealColumnName;
317 SetSetProperties( sPrefix, aAssignmentValues );
318 nFieldAssignment++;
322 /* -----------------------------13.11.00 12:23--------------------------------
324 ---------------------------------------------------------------------------*/
325 const Mapping* BibConfig::GetMapping(const BibDBDescriptor& rDesc) const
327 for(sal_uInt16 i = 0; i < pMappingsArr->Count(); i++)
329 const Mapping* pMapping = pMappingsArr->GetObject(i);
330 sal_Bool bURLEqual = rDesc.sDataSource.equals(pMapping->sURL);
331 if(rDesc.sTableOrQuery == pMapping->sTableName && bURLEqual)
332 return pMapping;
334 return 0;
336 /* -----------------------------13.11.00 12:23--------------------------------
338 ---------------------------------------------------------------------------*/
339 void BibConfig::SetMapping(const BibDBDescriptor& rDesc, const Mapping* pSetMapping)
341 for(sal_uInt16 i = 0; i < pMappingsArr->Count(); i++)
343 const Mapping* pMapping = pMappingsArr->GetObject(i);
344 sal_Bool bURLEqual = rDesc.sDataSource.equals(pMapping->sURL);
345 if(rDesc.sTableOrQuery == pMapping->sTableName && bURLEqual)
347 pMappingsArr->DeleteAndDestroy(i, 1);
348 break;
351 Mapping* pNew = new Mapping(*pSetMapping);
352 pMappingsArr->Insert(pNew, pMappingsArr->Count());
353 SetModified();
355 /* -----------------------------20.11.00 11:56--------------------------------
357 ---------------------------------------------------------------------------*/
358 DBChangeDialogConfig_Impl::DBChangeDialogConfig_Impl()
361 /* -----------------------------20.11.00 11:57--------------------------------
363 ---------------------------------------------------------------------------*/
364 DBChangeDialogConfig_Impl::~DBChangeDialogConfig_Impl()
367 /* -----------------------------14.03.01 12:53--------------------------------
369 ---------------------------------------------------------------------------*/
370 const Sequence<OUString>& DBChangeDialogConfig_Impl::GetDataSourceNames()
372 if(!aSourceNames.getLength())
374 Reference<XNameAccess> xDBContext;
375 Reference< XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
376 if( xMgr.is() )
378 Reference<XInterface> xInstance = xMgr->createInstance( C2U( "com.sun.star.sdb.DatabaseContext" ));
379 xDBContext = Reference<XNameAccess>(xInstance, UNO_QUERY) ;
381 if(xDBContext.is())
383 aSourceNames = xDBContext->getElementNames();
386 return aSourceNames;