Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "dbinsdlg.hxx"
21 :
22 : #include <float.h>
23 :
24 : #include <hintids.hxx>
25 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 : #include <com/sun/star/container/XNameAccess.hpp>
27 : #include <com/sun/star/sdbc/XDataSource.hpp>
28 : #include <com/sun/star/sdbc/XRow.hpp>
29 : #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
30 : #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
31 : #include <com/sun/star/sdb/XQueriesSupplier.hpp>
32 : #include <com/sun/star/sdb/CommandType.hpp>
33 : #include <com/sun/star/sdb/XColumn.hpp>
34 : #include <com/sun/star/sdb/XDatabaseAccess.hpp>
35 : #include <com/sun/star/sdbc/DataType.hpp>
36 : #include <com/sun/star/sdbc/ResultSetType.hpp>
37 : #include <com/sun/star/beans/XPropertySet.hpp>
38 : #include <com/sun/star/util/NumberFormatter.hpp>
39 : #include <com/sun/star/util/XNumberFormatTypes.hpp>
40 : #include <com/sun/star/sdbc/XRowSet.hpp>
41 : #include <comphelper/processfactory.hxx>
42 : #include <editeng/langitem.hxx>
43 : #include <svl/numuno.hxx>
44 : #include <svl/stritem.hxx>
45 : #include <vcl/msgbox.hxx>
46 : #include <vcl/svapp.hxx>
47 : #include <vcl/mnemonic.hxx>
48 : #include <svl/style.hxx>
49 : #include <svl/zformat.hxx>
50 : #include <sfx2/htmlmode.hxx>
51 : #include <editeng/unolingu.hxx>
52 : #include <sfx2/app.hxx>
53 : #include <svl/itemset.hxx>
54 : #include <editeng/brushitem.hxx>
55 : #include <editeng/boxitem.hxx>
56 : #include <svx/rulritem.hxx>
57 : #include <swdbtoolsclient.hxx>
58 : #include <tabledlg.hxx>
59 : #include <fmtclds.hxx>
60 : #include <tabcol.hxx>
61 : #include <uiitems.hxx>
62 : #include <viewopt.hxx>
63 : #include <uitool.hxx>
64 : #include <wrtsh.hxx>
65 : #include <wview.hxx>
66 : #include <docsh.hxx>
67 : #include <dbmgr.hxx>
68 : #include <tblafmt.hxx>
69 : #include <cellatr.hxx>
70 : #include <swtable.hxx>
71 : #include <dbfld.hxx>
72 : #include <fmtcol.hxx>
73 : #include <section.hxx>
74 : #include <swwait.hxx>
75 : #include <modcfg.hxx>
76 : #include <swmodule.hxx>
77 : #include <poolfmt.hxx>
78 : #include <crsskip.hxx>
79 :
80 : #include <dbui.hrc>
81 :
82 : #include <cmdid.h>
83 : #include <helpid.h>
84 : #include <cfgid.h>
85 : #include <SwStyleNameMapper.hxx>
86 : #include <comphelper/uno3.hxx>
87 : #include "tabsh.hxx"
88 : #include "swabstdlg.hxx"
89 : #include "table.hrc"
90 : #include <unomid.h>
91 : #include <IDocumentMarkAccess.hxx>
92 :
93 : #include <boost/noncopyable.hpp>
94 : #include <boost/scoped_ptr.hpp>
95 : #include <swuiexp.hxx>
96 :
97 : using namespace ::com::sun::star;
98 : using namespace ::com::sun::star::beans;
99 : using namespace ::com::sun::star::container;
100 : using namespace ::com::sun::star::lang;
101 : using namespace ::com::sun::star::sdb;
102 : using namespace ::com::sun::star::sdbc;
103 : using namespace ::com::sun::star::sdbcx;
104 : using namespace ::com::sun::star::uno;
105 :
106 : const char cDBFldStart = '<';
107 : const char cDBFldEnd = '>';
108 :
109 : // Helper structure for adding database rows as fields or text
110 : struct _DB_Column
111 : {
112 : enum ColType { DB_FILLTEXT, DB_COL_FIELD, DB_COL_TEXT, DB_SPLITPARA } eColType;
113 :
114 : union {
115 : OUString* pText;
116 : SwField* pField;
117 : sal_uLong nFormat;
118 : } DB_ColumnData;
119 : const SwInsDBColumn* pColInfo;
120 :
121 0 : _DB_Column()
122 : {
123 0 : pColInfo = 0;
124 0 : DB_ColumnData.pText = 0;
125 0 : eColType = DB_SPLITPARA;
126 0 : }
127 :
128 0 : _DB_Column( const OUString& rTxt )
129 : {
130 0 : pColInfo = 0;
131 0 : DB_ColumnData.pText = new OUString( rTxt );
132 0 : eColType = DB_FILLTEXT;
133 0 : }
134 :
135 0 : _DB_Column( const SwInsDBColumn& rInfo, sal_uLong nFormat )
136 : {
137 0 : pColInfo = &rInfo;
138 0 : DB_ColumnData.nFormat = nFormat;
139 0 : eColType = DB_COL_TEXT;
140 0 : }
141 :
142 0 : _DB_Column( const SwInsDBColumn& rInfo, SwDBField& rFld )
143 : {
144 0 : pColInfo = &rInfo;
145 0 : DB_ColumnData.pField = &rFld;
146 0 : eColType = DB_COL_FIELD;
147 0 : }
148 :
149 0 : ~_DB_Column()
150 : {
151 0 : if( DB_COL_FIELD == eColType )
152 0 : delete DB_ColumnData.pField;
153 0 : else if( DB_FILLTEXT == eColType )
154 0 : delete DB_ColumnData.pText;
155 0 : }
156 : };
157 :
158 : struct _DB_ColumnConfigData: private boost::noncopyable
159 : {
160 : SwInsDBColumns aDBColumns;
161 : OUString sSource;
162 : OUString sTable;
163 : OUString sEdit;
164 : OUString sTblList;
165 : OUString sTmplNm;
166 : OUString sTAutoFmtNm;
167 : bool bIsTable : 1,
168 : bIsField : 1,
169 : bIsHeadlineOn : 1,
170 : bIsEmptyHeadln : 1;
171 :
172 0 : _DB_ColumnConfigData()
173 0 : {
174 0 : bIsTable = bIsHeadlineOn = true;
175 0 : bIsField = bIsEmptyHeadln = false;
176 0 : }
177 :
178 : ~_DB_ColumnConfigData();
179 : };
180 :
181 0 : bool SwInsDBColumn::operator<( const SwInsDBColumn& rCmp ) const
182 : {
183 0 : return 0 > GetAppCollator().compareString( sColumn, rCmp.sColumn );
184 : }
185 :
186 0 : SwInsertDBColAutoPilot::SwInsertDBColAutoPilot( SwView& rView,
187 : Reference<XDataSource> xDataSource,
188 : Reference<sdbcx::XColumnsSupplier> xColSupp,
189 : const SwDBData& rData )
190 : : SfxModalDialog(rView.GetWindow(), "InsertDbColumnsDialog",
191 : "modules/swriter/ui/insertdbcolumnsdialog.ui")
192 : , ConfigItem("Office.Writer/InsertData/DataSet",
193 : CONFIG_MODE_DELAYED_UPDATE)
194 : , aDBData(rData)
195 : , sNoTmpl(SW_RESSTR(SW_STR_NONE))
196 : , pView(&rView)
197 : , pTAutoFmt(0)
198 : , pTblSet(0)
199 0 : , pRep(0)
200 : {
201 0 : get(m_pRbAsTable, "astable");
202 0 : get(m_pRbAsField, "asfields");
203 0 : get(m_pRbAsText, "astext");
204 0 : get(m_pHeadFrame, "dbframe");
205 0 : get(m_pLbTblDbColumn, "tabledbcols");
206 0 : get(m_pLbTxtDbColumn, "tabletxtcols");
207 0 : m_pLbTblDbColumn->SetDropDownLineCount(8);
208 0 : m_pLbTxtDbColumn->SetDropDownLineCount(8);
209 0 : get(m_pFormatFrame, "formatframe");
210 0 : get(m_pRbDbFmtFromDb, "fromdatabase");
211 0 : get(m_pRbDbFmtFromUsr, "userdefined");
212 0 : get(m_pLbDbFmtFromUsr, "numformat");
213 0 : aOldNumFmtLnk = m_pLbDbFmtFromUsr->GetSelectHdl();
214 0 : get(m_pIbDbcolToEdit, "toedit");
215 0 : get(m_pEdDbText, "textview");
216 0 : m_pEdDbText->set_width_request(m_pEdDbText->approximate_char_width() * 46);
217 0 : get(m_pFtDbParaColl, "parastylelabel");
218 0 : get(m_pLbDbParaColl, "parastyle");
219 0 : m_pLbDbParaColl->SetStyle(m_pLbDbParaColl->GetStyle() | WB_SORT);
220 0 : get(m_pIbDbcolAllTo, "oneright");
221 0 : get(m_pIbDbcolOneTo, "allright");
222 0 : get(m_pIbDbcolOneFrom, "oneleft");
223 0 : get(m_pIbDbcolAllFrom, "allleft");
224 0 : get(m_pFtTableCol, "tablecolft");
225 0 : get(m_pLbTableCol, "tablecols");
226 0 : m_pLbTableCol->SetDropDownLineCount(8);
227 0 : get(m_pCbTableHeadon, "tableheading");
228 0 : get(m_pRbHeadlColnms, "columnname");
229 0 : get(m_pRbHeadlEmpty, "rowonly");
230 0 : get(m_pPbTblFormat, "tableformat");
231 0 : get(m_pPbTblAutofmt, "autoformat");
232 :
233 0 : nGBFmtLen = m_pFormatFrame->get_label().getLength();
234 :
235 0 : if(xColSupp.is())
236 : {
237 0 : SwWrtShell& rSh = pView->GetWrtShell();
238 0 : SvNumberFormatter* pNumFmtr = rSh.GetNumberFormatter();
239 0 : SvNumberFormatsSupplierObj* pNumFmt = new SvNumberFormatsSupplierObj( pNumFmtr );
240 0 : Reference< util::XNumberFormatsSupplier > xDocNumFmtsSupplier = pNumFmt;
241 0 : Reference< util::XNumberFormats > xDocNumberFormats = xDocNumFmtsSupplier->getNumberFormats();
242 0 : Reference< util::XNumberFormatTypes > xDocNumberFormatTypes(xDocNumberFormats, UNO_QUERY);
243 :
244 0 : Reference<XPropertySet> xSourceProps(xDataSource, UNO_QUERY);
245 0 : Reference< util::XNumberFormats > xNumberFormats;
246 0 : if(xSourceProps.is())
247 : {
248 0 : Any aFormats = xSourceProps->getPropertyValue("NumberFormatsSupplier");
249 0 : if(aFormats.hasValue())
250 : {
251 0 : Reference< util::XNumberFormatsSupplier> xSuppl;
252 0 : aFormats >>= xSuppl;
253 0 : if(xSuppl.is())
254 : {
255 0 : xNumberFormats = xSuppl->getNumberFormats( );
256 0 : }
257 0 : }
258 : }
259 0 : Reference <XNameAccess> xCols = xColSupp->getColumns();
260 0 : Sequence<OUString> aColNames = xCols->getElementNames();
261 0 : const OUString* pColNames = aColNames.getConstArray();
262 0 : sal_Int32 nCount = aColNames.getLength();
263 0 : for (sal_Int32 n = 0; n < nCount; ++n)
264 : {
265 0 : SwInsDBColumn* pNew = new SwInsDBColumn( pColNames[n], (sal_uInt16)n );
266 0 : Any aCol = xCols->getByName(pColNames[n]);
267 0 : Reference <XPropertySet> xCol;
268 0 : aCol >>= xCol;
269 0 : Any aType = xCol->getPropertyValue("Type");
270 0 : sal_Int32 eDataType = 0;
271 0 : aType >>= eDataType;
272 0 : switch(eDataType)
273 : {
274 : case DataType::BIT:
275 : case DataType::BOOLEAN:
276 : case DataType::TINYINT:
277 : case DataType::SMALLINT:
278 : case DataType::INTEGER:
279 : case DataType::BIGINT:
280 : case DataType::FLOAT:
281 : case DataType::REAL:
282 : case DataType::DOUBLE:
283 : case DataType::NUMERIC:
284 : case DataType::DECIMAL:
285 : case DataType::DATE:
286 : case DataType::TIME:
287 : case DataType::TIMESTAMP:
288 : {
289 0 : pNew->bHasFmt = true;
290 0 : Any aFormat = xCol->getPropertyValue("FormatKey");
291 0 : if(aFormat.hasValue())
292 : {
293 0 : sal_Int32 nFmt = 0;
294 0 : aFormat >>= nFmt;
295 0 : if(xNumberFormats.is())
296 : {
297 : try
298 : {
299 0 : Reference<XPropertySet> xNumProps = xNumberFormats->getByKey( nFmt );
300 0 : Any aFormatVal = xNumProps->getPropertyValue("FormatString");
301 0 : Any aLocale = xNumProps->getPropertyValue("Locale");
302 0 : OUString sFormat;
303 0 : aFormatVal >>= sFormat;
304 0 : lang::Locale aLoc;
305 0 : aLocale >>= aLoc;
306 0 : long nKey = xDocNumberFormats->queryKey( sFormat, aLoc, sal_True);
307 0 : if(nKey < 0)
308 : {
309 0 : nKey = xDocNumberFormats->addNew( sFormat, aLoc );
310 : }
311 0 : pNew->nDBNumFmt = nKey;
312 : }
313 0 : catch (const Exception&)
314 : {
315 : OSL_FAIL("illegal number format key");
316 : }
317 : }
318 : }
319 : else
320 : {
321 0 : pNew->nDBNumFmt = SwDBManager::GetDbtoolsClient().getDefaultNumberFormat(xCol,
322 0 : xDocNumberFormatTypes, LanguageTag( rSh.GetCurLang() ).getLocale());
323 0 : }
324 :
325 : }
326 0 : break;
327 : }
328 0 : if( !aDBColumns.insert( pNew ).second )
329 : {
330 : OSL_ENSURE( false, "Spaltenname mehrfach vergeben?" );
331 0 : delete pNew;
332 : }
333 0 : }
334 : }
335 :
336 : // fill paragraph templates-ListBox
337 : {
338 0 : SfxStyleSheetBasePool* pPool = pView->GetDocShell()->GetStyleSheetPool();
339 0 : pPool->SetSearchMask( SFX_STYLE_FAMILY_PARA, SFXSTYLEBIT_ALL );
340 0 : m_pLbDbParaColl->InsertEntry( sNoTmpl );
341 :
342 0 : const SfxStyleSheetBase* pBase = pPool->First();
343 0 : while( pBase )
344 : {
345 0 : m_pLbDbParaColl->InsertEntry( pBase->GetName() );
346 0 : pBase = pPool->Next();
347 : }
348 0 : m_pLbDbParaColl->SelectEntryPos( 0 );
349 : }
350 :
351 : // when the cursor is inside of a table, table must NEVER be selectable
352 0 : if( pView->GetWrtShell().GetTableFmt() )
353 : {
354 0 : m_pRbAsTable->Enable( false );
355 0 : m_pRbAsField->Check( true );
356 0 : m_pRbDbFmtFromDb->Check( true );
357 : }
358 : else
359 : {
360 0 : m_pRbAsTable->Check( true );
361 0 : m_pRbDbFmtFromDb->Check( true );
362 0 : m_pIbDbcolOneFrom->Enable( false );
363 0 : m_pIbDbcolAllFrom->Enable( false );
364 : }
365 :
366 0 : m_pRbAsTable->SetClickHdl( LINK(this, SwInsertDBColAutoPilot, PageHdl ));
367 0 : m_pRbAsField->SetClickHdl( LINK(this, SwInsertDBColAutoPilot, PageHdl ));
368 0 : m_pRbAsText->SetClickHdl( LINK(this, SwInsertDBColAutoPilot, PageHdl ));
369 :
370 0 : m_pRbDbFmtFromDb->SetClickHdl( LINK(this, SwInsertDBColAutoPilot, DBFormatHdl ));
371 0 : m_pRbDbFmtFromUsr->SetClickHdl( LINK(this, SwInsertDBColAutoPilot, DBFormatHdl ));
372 :
373 0 : m_pPbTblFormat->SetClickHdl(LINK(this, SwInsertDBColAutoPilot, TblFmtHdl ));
374 0 : m_pPbTblAutofmt->SetClickHdl(LINK(this, SwInsertDBColAutoPilot, AutoFmtHdl ));
375 :
376 0 : m_pIbDbcolAllTo->SetClickHdl( LINK(this, SwInsertDBColAutoPilot, TblToFromHdl ));
377 0 : m_pIbDbcolOneTo->SetClickHdl( LINK(this, SwInsertDBColAutoPilot, TblToFromHdl ));
378 0 : m_pIbDbcolOneFrom->SetClickHdl( LINK(this, SwInsertDBColAutoPilot, TblToFromHdl ));
379 0 : m_pIbDbcolAllFrom->SetClickHdl( LINK(this, SwInsertDBColAutoPilot, TblToFromHdl ));
380 0 : m_pIbDbcolToEdit->SetClickHdl( LINK(this, SwInsertDBColAutoPilot, TblToFromHdl ));
381 :
382 0 : m_pCbTableHeadon->SetClickHdl( LINK(this, SwInsertDBColAutoPilot, HeaderHdl ));
383 0 : m_pRbHeadlColnms->SetClickHdl( LINK(this, SwInsertDBColAutoPilot, HeaderHdl ));
384 0 : m_pRbHeadlEmpty->SetClickHdl( LINK(this, SwInsertDBColAutoPilot, HeaderHdl ));
385 :
386 0 : m_pLbTxtDbColumn->SetSelectHdl( LINK( this, SwInsertDBColAutoPilot, SelectHdl ));
387 0 : m_pLbTblDbColumn->SetSelectHdl( LINK( this, SwInsertDBColAutoPilot, SelectHdl ));
388 0 : m_pLbDbFmtFromUsr->SetSelectHdl( LINK( this, SwInsertDBColAutoPilot, SelectHdl ));
389 0 : m_pLbTableCol->SetSelectHdl( LINK( this, SwInsertDBColAutoPilot, SelectHdl ));
390 :
391 0 : m_pLbTxtDbColumn->SetDoubleClickHdl( LINK( this, SwInsertDBColAutoPilot, DblClickHdl ));
392 0 : m_pLbTblDbColumn->SetDoubleClickHdl( LINK( this, SwInsertDBColAutoPilot, DblClickHdl ));
393 0 : m_pLbTableCol->SetDoubleClickHdl( LINK( this, SwInsertDBColAutoPilot, DblClickHdl ));
394 :
395 0 : for( size_t n = 0; n < aDBColumns.size(); ++n )
396 : {
397 0 : const OUString& rS = aDBColumns[ n ]->sColumn;
398 0 : m_pLbTblDbColumn->InsertEntry( rS, static_cast<sal_Int32>(n) );
399 0 : m_pLbTxtDbColumn->InsertEntry( rS, static_cast<sal_Int32>(n) );
400 : }
401 0 : m_pLbTxtDbColumn->SelectEntryPos( 0 );
402 0 : m_pLbTblDbColumn->SelectEntryPos( 0 );
403 :
404 : // read configuration
405 0 : Load();
406 :
407 : // lock size to widest config
408 0 : m_pHeadFrame->set_width_request(m_pHeadFrame->get_preferred_size().Width());
409 : // initialise Controls:
410 0 : PageHdl( m_pRbAsTable->IsChecked() ? m_pRbAsTable : m_pRbAsField );
411 0 : }
412 :
413 0 : SwInsertDBColAutoPilot::~SwInsertDBColAutoPilot()
414 : {
415 0 : delete pTblSet;
416 0 : delete pRep;
417 :
418 0 : delete pTAutoFmt;
419 0 : }
420 :
421 0 : IMPL_LINK( SwInsertDBColAutoPilot, PageHdl, Button*, pButton )
422 : {
423 0 : bool bShowTbl = pButton == m_pRbAsTable;
424 :
425 0 : m_pHeadFrame->set_label(MnemonicGenerator::EraseAllMnemonicChars(pButton->GetText()));
426 :
427 0 : m_pLbTxtDbColumn->Show( !bShowTbl );
428 0 : m_pIbDbcolToEdit->Show( !bShowTbl );
429 0 : m_pEdDbText->Show( !bShowTbl );
430 0 : m_pFtDbParaColl->Show( !bShowTbl );
431 0 : m_pLbDbParaColl->Show( !bShowTbl );
432 :
433 0 : m_pLbTblDbColumn->Show( bShowTbl );
434 0 : m_pIbDbcolAllTo->Show( bShowTbl );
435 0 : m_pIbDbcolOneTo->Show( bShowTbl );
436 0 : m_pIbDbcolOneFrom->Show( bShowTbl );
437 0 : m_pIbDbcolAllFrom->Show( bShowTbl );
438 0 : m_pFtTableCol->Show( bShowTbl );
439 0 : m_pLbTableCol->Show( bShowTbl );
440 0 : m_pCbTableHeadon->Show( bShowTbl );
441 0 : m_pRbHeadlColnms->Show( bShowTbl );
442 0 : m_pRbHeadlEmpty->Show( bShowTbl );
443 0 : m_pPbTblFormat->Show( bShowTbl );
444 0 : m_pPbTblAutofmt->Show( bShowTbl );
445 :
446 0 : if( bShowTbl )
447 0 : m_pPbTblFormat->Enable( 0 != m_pLbTableCol->GetEntryCount() );
448 :
449 0 : SelectHdl( bShowTbl ? m_pLbTblDbColumn : m_pLbTxtDbColumn );
450 :
451 0 : return 0;
452 : }
453 :
454 0 : IMPL_LINK( SwInsertDBColAutoPilot, DBFormatHdl, Button*, pButton )
455 : {
456 0 : ListBox& rBox = m_pRbAsTable->IsChecked()
457 0 : ? ( 0 == m_pLbTableCol->GetEntryData( 0 )
458 : ? *m_pLbTblDbColumn
459 : : *m_pLbTableCol )
460 0 : : *m_pLbTxtDbColumn;
461 :
462 0 : SwInsDBColumn aSrch( rBox.GetSelectEntry(), 0 );
463 0 : SwInsDBColumns::const_iterator it = aDBColumns.find( &aSrch );
464 :
465 0 : bool bFromDB = m_pRbDbFmtFromDb == pButton;
466 0 : (*it)->bIsDBFmt = bFromDB;
467 0 : m_pLbDbFmtFromUsr->Enable( !bFromDB );
468 :
469 0 : return 0;
470 : }
471 :
472 0 : IMPL_LINK( SwInsertDBColAutoPilot, TblToFromHdl, Button*, pButton )
473 : {
474 0 : bool bChgEnable = true, bEnableTo = true, bEnableFrom = true;
475 0 : m_pLbTblDbColumn->SetUpdateMode( false );
476 0 : m_pLbTableCol->SetUpdateMode( false );
477 :
478 0 : if( pButton == m_pIbDbcolAllTo )
479 : {
480 0 : bEnableTo = false;
481 :
482 0 : sal_Int32 n, nInsPos = m_pLbTableCol->GetSelectEntryPos(),
483 0 : nCnt = m_pLbTblDbColumn->GetEntryCount();
484 0 : if( LISTBOX_APPEND == nInsPos )
485 0 : for( n = 0; n < nCnt; ++n )
486 : m_pLbTableCol->InsertEntry( m_pLbTblDbColumn->GetEntry( n ),
487 0 : LISTBOX_APPEND );
488 : else
489 0 : for( n = 0; n < nCnt; ++n, ++nInsPos )
490 0 : m_pLbTableCol->InsertEntry( m_pLbTblDbColumn->GetEntry( n ), nInsPos );
491 0 : m_pLbTblDbColumn->Clear();
492 0 : m_pLbTableCol->SelectEntryPos( nInsPos );
493 0 : m_pLbTblDbColumn->SelectEntryPos( LISTBOX_APPEND );
494 : }
495 0 : else if( pButton == m_pIbDbcolOneTo &&
496 0 : LISTBOX_ENTRY_NOTFOUND != m_pLbTblDbColumn->GetSelectEntryPos() )
497 : {
498 0 : sal_Int32 nInsPos = m_pLbTableCol->GetSelectEntryPos(),
499 0 : nDelPos = m_pLbTblDbColumn->GetSelectEntryPos(),
500 0 : nTopPos = m_pLbTblDbColumn->GetTopEntry();
501 0 : m_pLbTableCol->InsertEntry( m_pLbTblDbColumn->GetEntry( nDelPos ), nInsPos );
502 0 : m_pLbTblDbColumn->RemoveEntry( nDelPos );
503 :
504 0 : m_pLbTableCol->SelectEntryPos( nInsPos );
505 0 : if( nDelPos >= m_pLbTblDbColumn->GetEntryCount() )
506 0 : nDelPos = m_pLbTblDbColumn->GetEntryCount() - 1;
507 0 : m_pLbTblDbColumn->SelectEntryPos( nDelPos );
508 0 : m_pLbTblDbColumn->SetTopEntry( nTopPos );
509 :
510 0 : bEnableTo = 0 != m_pLbTblDbColumn->GetEntryCount();
511 : }
512 0 : else if( pButton == m_pIbDbcolOneFrom )
513 : {
514 0 : if( LISTBOX_ENTRY_NOTFOUND != m_pLbTableCol->GetSelectEntryPos() )
515 : {
516 : sal_Int32 nInsPos,
517 0 : nDelPos = m_pLbTableCol->GetSelectEntryPos(),
518 0 : nTopPos = m_pLbTableCol->GetTopEntry();
519 :
520 : // look for the right InsertPos!!
521 0 : SwInsDBColumn aSrch( m_pLbTableCol->GetEntry( nDelPos ), 0 );
522 0 : SwInsDBColumns::const_iterator it = aDBColumns.find( &aSrch );
523 0 : if( it == aDBColumns.begin() || (it+1) == aDBColumns.end() )
524 0 : nInsPos = it - aDBColumns.begin();
525 : else
526 : {
527 0 : nInsPos = LISTBOX_ENTRY_NOTFOUND;
528 0 : while( ++it != aDBColumns.end() &&
529 : LISTBOX_ENTRY_NOTFOUND == (nInsPos = m_pLbTblDbColumn->
530 0 : GetEntryPos( (*it)->sColumn )) )
531 : ;
532 : }
533 :
534 0 : m_pLbTblDbColumn->InsertEntry( aSrch.sColumn, nInsPos );
535 0 : m_pLbTableCol->RemoveEntry( nDelPos );
536 :
537 0 : if( nInsPos >= m_pLbTblDbColumn->GetEntryCount() )
538 0 : nInsPos = m_pLbTblDbColumn->GetEntryCount() - 1;
539 0 : m_pLbTblDbColumn->SelectEntryPos( nInsPos );
540 :
541 0 : if( nDelPos >= m_pLbTableCol->GetEntryCount() )
542 0 : nDelPos = m_pLbTableCol->GetEntryCount() - 1;
543 0 : m_pLbTableCol->SelectEntryPos( nDelPos );
544 0 : m_pLbTableCol->SetTopEntry( nTopPos );
545 : }
546 : else
547 0 : bEnableTo = 0 != m_pLbTblDbColumn->GetEntryCount();
548 :
549 0 : bEnableFrom = 0 != m_pLbTableCol->GetEntryCount();
550 : }
551 0 : else if( pButton == m_pIbDbcolAllFrom )
552 : {
553 0 : bEnableFrom = false;
554 :
555 0 : m_pLbTblDbColumn->Clear();
556 0 : m_pLbTableCol->Clear();
557 0 : for( size_t n = 0; n < aDBColumns.size(); ++n )
558 0 : m_pLbTblDbColumn->InsertEntry( aDBColumns[ n ]->sColumn,
559 0 : static_cast<sal_Int32>(n) );
560 0 : m_pLbTblDbColumn->SelectEntryPos( 0 );
561 : }
562 0 : else if( pButton == m_pIbDbcolToEdit )
563 : {
564 0 : bChgEnable = false;
565 : // move data to Edit:
566 0 : OUString aFld( m_pLbTxtDbColumn->GetSelectEntry() );
567 0 : if( !aFld.isEmpty() )
568 : {
569 0 : OUString aStr( m_pEdDbText->GetText() );
570 0 : sal_Int32 nPos = static_cast<sal_Int32>(m_pEdDbText->GetSelection().Min());
571 0 : const sal_Int32 nSel = static_cast<sal_Int32>(m_pEdDbText->GetSelection().Max()) - nPos;
572 0 : if( nSel )
573 : // first delete the existing selection
574 0 : aStr = aStr.replaceAt( nPos, nSel, "" );
575 :
576 0 : aFld = OUString(cDBFldStart) + aFld + OUString(cDBFldEnd);
577 0 : if( !aStr.isEmpty() )
578 : {
579 0 : if( nPos ) // one blank in front
580 : {
581 0 : sal_Unicode c = aStr[ nPos-1 ];
582 0 : if( '\n' != c && '\r' != c )
583 0 : aFld = " " + aFld;
584 : }
585 0 : if( nPos < aStr.getLength() ) // one blank behind
586 : {
587 0 : sal_Unicode c = aStr[ nPos ];
588 0 : if( '\n' != c && '\r' != c )
589 0 : aFld += " ";
590 : }
591 : }
592 :
593 0 : m_pEdDbText->SetText( aStr.replaceAt( nPos, 0, aFld ) );
594 0 : nPos += aFld.getLength();
595 0 : m_pEdDbText->SetSelection( Selection( nPos ));
596 0 : }
597 : }
598 :
599 0 : if( bChgEnable )
600 : {
601 0 : m_pIbDbcolOneTo->Enable( bEnableTo );
602 0 : m_pIbDbcolAllTo->Enable( bEnableTo );
603 0 : m_pIbDbcolOneFrom->Enable( bEnableFrom );
604 0 : m_pIbDbcolAllFrom->Enable( bEnableFrom );
605 :
606 0 : m_pRbDbFmtFromDb->Enable( false );
607 0 : m_pRbDbFmtFromUsr->Enable( false );
608 0 : m_pLbDbFmtFromUsr->Enable( false );
609 :
610 0 : m_pPbTblFormat->Enable( bEnableFrom );
611 : }
612 0 : m_pLbTblDbColumn->SetUpdateMode( true );
613 0 : m_pLbTableCol->SetUpdateMode( true );
614 :
615 0 : return 0;
616 : }
617 :
618 0 : IMPL_LINK( SwInsertDBColAutoPilot, DblClickHdl, ListBox*, pBox )
619 : {
620 0 : Button* pButton = 0;
621 0 : if( pBox == m_pLbTxtDbColumn )
622 0 : pButton = m_pIbDbcolToEdit;
623 0 : else if( pBox == m_pLbTblDbColumn && m_pIbDbcolOneTo->IsEnabled() )
624 0 : pButton = m_pIbDbcolOneTo;
625 0 : else if( pBox == m_pLbTableCol && m_pIbDbcolOneFrom->IsEnabled() )
626 0 : pButton = m_pIbDbcolOneFrom;
627 :
628 0 : if( pButton )
629 0 : TblToFromHdl( pButton );
630 :
631 0 : return 0;
632 : }
633 :
634 0 : IMPL_LINK( SwInsertDBColAutoPilot, TblFmtHdl, PushButton*, pButton )
635 : {
636 0 : SwWrtShell& rSh = pView->GetWrtShell();
637 0 : bool bNewSet = false;
638 0 : if( !pTblSet )
639 : {
640 0 : bNewSet = true;
641 0 : pTblSet = new SfxItemSet( rSh.GetAttrPool(), SwuiGetUITableAttrRange() );
642 :
643 : // At first acquire the simple attributes
644 0 : pTblSet->Put( SfxStringItem( FN_PARAM_TABLE_NAME, rSh.GetUniqueTblName() ));
645 0 : pTblSet->Put( SfxUInt16Item( FN_PARAM_TABLE_HEADLINE, 1 ) );
646 :
647 : pTblSet->Put( SfxUInt16Item( SID_BACKGRND_DESTINATION,
648 0 : rSh.GetViewOptions()->GetTblDest() ));
649 :
650 0 : SvxBrushItem aBrush( RES_BACKGROUND );
651 0 : pTblSet->Put( aBrush );
652 0 : pTblSet->Put( aBrush, SID_ATTR_BRUSH_ROW );
653 0 : pTblSet->Put( aBrush, SID_ATTR_BRUSH_TABLE );
654 :
655 0 : SvxBoxInfoItem aBoxInfo( SID_ATTR_BORDER_INNER );
656 : // table variant, when multiple table cells are selected
657 0 : aBoxInfo.SetTable( true );
658 : // always show gap field
659 0 : aBoxInfo.SetDist( true);
660 : // set minimum size in tables and paragraphs
661 0 : aBoxInfo.SetMinDist( false );
662 : // always set default-gap
663 0 : aBoxInfo.SetDefDist( MIN_BORDER_DIST );
664 : // Single lines can have DontCare-status only in tables
665 0 : aBoxInfo.SetValid( VALID_DISABLE, true );
666 0 : pTblSet->Put( aBoxInfo );
667 :
668 0 : SwGetCurColNumPara aPara;
669 0 : const sal_uInt16 nNum = rSh.GetCurColNum( &aPara );
670 : long nWidth;
671 :
672 0 : if( nNum )
673 : {
674 0 : nWidth = aPara.pPrtRect->Width();
675 0 : const SwFmtCol& rCol = aPara.pFrmFmt->GetCol();
676 0 : const SwColumns& rCols = rCol.GetColumns();
677 :
678 : // initialise nStart und nEnd for nNum == 0
679 0 : long nWidth1 = 0,
680 0 : nStart1 = 0,
681 0 : nEnd1 = nWidth;
682 0 : for( sal_uInt16 i = 0; i < nNum; ++i )
683 : {
684 0 : const SwColumn* pCol = &rCols[i];
685 0 : nStart1 = pCol->GetLeft() + nWidth1;
686 0 : nWidth1 += (long)rCol.CalcColWidth( i, (sal_uInt16)nWidth );
687 0 : nEnd1 = nWidth1 - pCol->GetRight();
688 : }
689 0 : if(nStart1 || nEnd1 != nWidth)
690 0 : nWidth = nEnd1 - nStart1;
691 : }
692 : else
693 : nWidth = rSh.GetAnyCurRect(
694 0 : FRMTYPE_FLY_ANY & rSh.GetFrmType( 0, true )
695 : ? RECT_FLY_PRT_EMBEDDED
696 0 : : RECT_PAGE_PRT ).Width();
697 :
698 0 : SwTabCols aTabCols;
699 0 : aTabCols.SetRight( nWidth );
700 0 : aTabCols.SetRightMax( nWidth );
701 0 : pRep = new SwTableRep( aTabCols );
702 0 : pRep->SetAlign( text::HoriOrientation::NONE );
703 0 : pRep->SetSpace( nWidth );
704 0 : pRep->SetWidth( nWidth );
705 0 : pRep->SetWidthPercent( 100 );
706 0 : pTblSet->Put( SwPtrItem( FN_TABLE_REP, pRep ));
707 :
708 : pTblSet->Put( SfxUInt16Item( SID_HTML_MODE,
709 0 : ::GetHtmlMode( pView->GetDocShell() )));
710 : }
711 :
712 0 : if( m_pLbTableCol->GetEntryCount() != pRep->GetAllColCount() )
713 : {
714 : // Number of columns has changed: then the TabCols have to be adjusted
715 0 : long nWidth = pRep->GetWidth();
716 0 : const sal_Int32 nCols = m_pLbTableCol->GetEntryCount() - 1;
717 0 : SwTabCols aTabCols( nCols );
718 0 : aTabCols.SetRight( nWidth );
719 0 : aTabCols.SetRightMax( nWidth );
720 0 : if( nCols )
721 : {
722 0 : const sal_Int32 nStep = nWidth / (nCols+1);
723 0 : for( sal_Int32 n = 0; n < nCols; ++n )
724 : {
725 0 : aTabCols.Insert( nStep*(n+1), false, n );
726 : }
727 : }
728 0 : delete pRep;
729 0 : pRep = new SwTableRep( aTabCols );
730 0 : pRep->SetAlign( text::HoriOrientation::NONE );
731 0 : pRep->SetSpace( nWidth );
732 0 : pRep->SetWidth( nWidth );
733 0 : pRep->SetWidthPercent( 100 );
734 0 : pTblSet->Put( SwPtrItem( FN_TABLE_REP, pRep ));
735 : }
736 :
737 0 : SwAbstractDialogFactory* pFact = swui::GetFactory();
738 : OSL_ENSURE(pFact, "SwAbstractDialogFactory fail!");
739 :
740 0 : boost::scoped_ptr<SfxAbstractTabDialog> pDlg(pFact->CreateSwTableTabDlg(pButton, rSh.GetAttrPool(), pTblSet, &rSh));
741 : OSL_ENSURE(pDlg, "Dialog creation failed!");
742 0 : if( RET_OK == pDlg->Execute() )
743 0 : pTblSet->Put( *pDlg->GetOutputItemSet() );
744 0 : else if( bNewSet )
745 : {
746 0 : delete pTblSet, pTblSet = 0;
747 0 : delete pRep, pRep = 0;
748 : }
749 :
750 0 : return 0;
751 : }
752 :
753 0 : IMPL_LINK( SwInsertDBColAutoPilot, AutoFmtHdl, PushButton*, pButton )
754 : {
755 0 : SwAbstractDialogFactory* pFact = swui::GetFactory();
756 : OSL_ENSURE(pFact, "SwAbstractDialogFactory fail!");
757 :
758 0 : boost::scoped_ptr<AbstractSwAutoFormatDlg> pDlg(pFact->CreateSwAutoFormatDlg(pButton, pView->GetWrtShellPtr(), false, pTAutoFmt));
759 : OSL_ENSURE(pDlg, "Dialog creation failed!");
760 0 : if( RET_OK == pDlg->Execute())
761 0 : pDlg->FillAutoFmtOfIndex( pTAutoFmt );
762 0 : return 0;
763 : }
764 :
765 0 : IMPL_LINK( SwInsertDBColAutoPilot, SelectHdl, ListBox*, pBox )
766 : {
767 0 : ListBox* pGetBox = pBox == m_pLbDbFmtFromUsr
768 0 : ? ( m_pRbAsTable->IsChecked()
769 0 : ? ( 0 == m_pLbTableCol->GetEntryData( 0 )
770 : ? m_pLbTblDbColumn
771 : : m_pLbTableCol )
772 : : m_pLbTxtDbColumn )
773 0 : : pBox;
774 :
775 0 : SwInsDBColumn aSrch( pGetBox->GetSelectEntry(), 0 );
776 0 : SwInsDBColumns::const_iterator it = aDBColumns.find( &aSrch );
777 :
778 0 : if( pBox == m_pLbDbFmtFromUsr )
779 : {
780 0 : if( !aSrch.sColumn.isEmpty() )
781 : {
782 0 : aOldNumFmtLnk.Call( pBox );
783 0 : (*it)->nUsrNumFmt = m_pLbDbFmtFromUsr->GetFormat();
784 : }
785 : }
786 : else
787 : {
788 : // set the selected FieldName at the FormatGroupBox, so that
789 : // it's clear what field is configured by the format!
790 0 : OUString sTxt( m_pFormatFrame->get_label().copy( 0, nGBFmtLen ));
791 0 : if( aSrch.sColumn.isEmpty() )
792 : {
793 0 : m_pRbDbFmtFromDb->Enable( false );
794 0 : m_pRbDbFmtFromUsr->Enable( false );
795 0 : m_pLbDbFmtFromUsr->Enable( false );
796 : }
797 : else
798 : {
799 0 : bool bEnableFmt = (*it)->bHasFmt;
800 0 : m_pRbDbFmtFromDb->Enable( bEnableFmt );
801 0 : m_pRbDbFmtFromUsr->Enable( bEnableFmt );
802 :
803 0 : if( bEnableFmt )
804 : {
805 0 : sTxt += " (" + aSrch.sColumn + ")";
806 : }
807 :
808 0 : bool bIsDBFmt = (*it)->bIsDBFmt;
809 0 : m_pRbDbFmtFromDb->Check( bIsDBFmt );
810 0 : m_pRbDbFmtFromUsr->Check( !bIsDBFmt );
811 0 : m_pLbDbFmtFromUsr->Enable( !bIsDBFmt );
812 0 : if( !bIsDBFmt )
813 0 : m_pLbDbFmtFromUsr->SetDefFormat( (*it)->nUsrNumFmt );
814 : }
815 :
816 0 : m_pFormatFrame->set_label(sTxt);
817 :
818 : // to know later on, what ListBox was the "active", a Flag
819 : // is remembered in the 1st entry
820 0 : void* pPtr = pBox == m_pLbTableCol ? m_pLbTableCol : 0;
821 0 : m_pLbTableCol->SetEntryData( 0, pPtr );
822 : }
823 0 : return 0;
824 : }
825 :
826 0 : IMPL_LINK( SwInsertDBColAutoPilot, HeaderHdl, Button*, pButton )
827 : {
828 0 : if( pButton == m_pCbTableHeadon )
829 : {
830 0 : bool bEnable = m_pCbTableHeadon->IsChecked();
831 :
832 0 : m_pRbHeadlColnms->Enable( bEnable );
833 0 : m_pRbHeadlEmpty->Enable( bEnable );
834 : }
835 0 : return 0;
836 : }
837 :
838 0 : static void lcl_InsTextInArr( const OUString& rTxt, _DB_Columns& rColArr )
839 : {
840 : _DB_Column* pNew;
841 0 : sal_Int32 nSttPos = 0, nFndPos;
842 0 : while( -1 != ( nFndPos = rTxt.indexOf( '\x0A', nSttPos )) )
843 : {
844 0 : if( 1 < nFndPos )
845 : {
846 0 : pNew = new _DB_Column( rTxt.copy( nSttPos, nFndPos -1 ) );
847 0 : rColArr.push_back( pNew );
848 : }
849 0 : pNew = new _DB_Column;
850 0 : rColArr.push_back( pNew );
851 0 : nSttPos = nFndPos + 1;
852 : }
853 0 : if( nSttPos < rTxt.getLength() )
854 : {
855 0 : pNew = new _DB_Column( rTxt.copy( nSttPos ) );
856 0 : rColArr.push_back( pNew );
857 : }
858 0 : }
859 :
860 0 : bool SwInsertDBColAutoPilot::SplitTextToColArr( const OUString& rTxt,
861 : _DB_Columns& rColArr,
862 : bool bInsField )
863 : {
864 : // create each of the database columns from the text again
865 : // and then save in an array
866 : // database columns are in <> and must be present in the columns' array:
867 0 : OUString sTxt( rTxt );
868 0 : sal_Int32 nFndPos, nEndPos, nSttPos = 0;
869 :
870 0 : while( -1 != ( nFndPos = sTxt.indexOf( cDBFldStart, nSttPos )))
871 : {
872 0 : nSttPos = nFndPos + 1;
873 0 : if( -1 != ( nEndPos = sTxt.indexOf( cDBFldEnd, nSttPos+1 )))
874 : {
875 : // Text in <> brackets found: what is it:
876 0 : SwInsDBColumn aSrch( sTxt.copy( nSttPos, nEndPos - nSttPos ), 0);
877 0 : SwInsDBColumns::const_iterator it = aDBColumns.find( &aSrch );
878 0 : if( it != aDBColumns.end() )
879 : {
880 : // that is a valid field
881 : // so surely the text "before":
882 0 : const SwInsDBColumn& rFndCol = **it;
883 :
884 : _DB_Column* pNew;
885 :
886 0 : if( 1 < nSttPos )
887 : {
888 0 : ::lcl_InsTextInArr( sTxt.copy( 0, nSttPos-1 ), rColArr );
889 0 : sTxt = sTxt.copy( nSttPos-1 );
890 : }
891 :
892 0 : sTxt = sTxt.copy( rFndCol.sColumn.getLength() + 2 );
893 0 : nSttPos = 0;
894 :
895 0 : sal_uInt16 nSubType = 0;
896 : sal_uLong nFormat;
897 0 : if( rFndCol.bHasFmt )
898 : {
899 0 : if( rFndCol.bIsDBFmt )
900 0 : nFormat = rFndCol.nDBNumFmt;
901 : else
902 : {
903 0 : nFormat = rFndCol.nUsrNumFmt;
904 0 : nSubType = nsSwExtendedSubType::SUB_OWN_FMT;
905 : }
906 : }
907 : else
908 0 : nFormat = 0;
909 :
910 0 : if( bInsField )
911 : {
912 0 : SwWrtShell& rSh = pView->GetWrtShell();
913 : SwDBFieldType aFldType( rSh.GetDoc(), aSrch.sColumn,
914 0 : aDBData );
915 : pNew = new _DB_Column( rFndCol, *new SwDBField(
916 0 : (SwDBFieldType*)rSh.InsertFldType( aFldType ),
917 0 : nFormat ) );
918 0 : if( nSubType )
919 0 : pNew->DB_ColumnData.pField->SetSubType( nSubType );
920 : }
921 : else
922 0 : pNew = new _DB_Column( rFndCol, nFormat );
923 :
924 0 : rColArr.push_back( pNew );
925 0 : }
926 : }
927 : }
928 :
929 : // don't forget the last text
930 0 : if( !sTxt.isEmpty() )
931 0 : ::lcl_InsTextInArr( sTxt, rColArr );
932 :
933 0 : return !rColArr.empty();
934 : }
935 :
936 0 : void SwInsertDBColAutoPilot::DataToDoc( const Sequence<Any>& rSelection,
937 : Reference< XDataSource> xSource,
938 : Reference< XConnection> xConnection,
939 : Reference< sdbc::XResultSet > xResultSet )
940 : {
941 0 : const Any* pSelection = rSelection.getLength() ? rSelection.getConstArray() : 0;
942 0 : SwWrtShell& rSh = pView->GetWrtShell();
943 :
944 : //with the drag and drop interface no result set is initially available
945 0 : bool bDisposeResultSet = false;
946 : // we don't have a cursor, so we have to create our own RowSet
947 0 : if ( !xResultSet.is() )
948 : {
949 0 : xResultSet = SwDBManager::createCursor(aDBData.sDataSource,aDBData.sCommand,aDBData.nCommandType,xConnection);
950 0 : bDisposeResultSet = xResultSet.is();
951 : }
952 :
953 0 : Reference< sdbc::XRow > xRow(xResultSet, UNO_QUERY);
954 0 : if ( !xRow.is() )
955 0 : return;
956 :
957 0 : rSh.StartAllAction();
958 0 : bool bUndo = rSh.DoesUndo();
959 0 : if( bUndo )
960 0 : rSh.StartUndo( UNDO_EMPTY );
961 :
962 0 : bool bAsTable = m_pRbAsTable->IsChecked();
963 0 : SvNumberFormatter& rNumFmtr = *rSh.GetNumberFormatter();
964 :
965 0 : if( rSh.HasSelection() )
966 0 : rSh.DelRight();
967 :
968 0 : boost::scoped_ptr<SwWait> pWait;
969 :
970 0 : Reference< XColumnsSupplier > xColsSupp( xResultSet, UNO_QUERY );
971 0 : Reference <XNameAccess> xCols = xColsSupp->getColumns();
972 :
973 : do{ // middle checked loop!!
974 0 : if( bAsTable ) // Daten als Tabelle einfuegen
975 : {
976 0 : rSh.DoUndo( false );
977 :
978 0 : sal_Int32 nCols = m_pLbTableCol->GetEntryCount();
979 0 : sal_Int32 nRows = 0;
980 0 : if( m_pCbTableHeadon->IsChecked() )
981 0 : nRows++;
982 :
983 0 : if( pSelection )
984 0 : nRows += rSelection.getLength();
985 : else
986 0 : ++nRows;
987 :
988 : // prepare the array for the selected columns
989 0 : std::vector<SwInsDBColumn*> aColFlds;
990 0 : for( sal_Int32 n = 0; n < nCols; ++n )
991 : {
992 0 : SwInsDBColumn aSrch( m_pLbTableCol->GetEntry( n ), 0 );
993 0 : SwInsDBColumns::const_iterator it = aDBColumns.find( &aSrch );
994 0 : if (it != aDBColumns.end())
995 0 : aColFlds.push_back(*it);
996 : else {
997 : OSL_ENSURE( false, "database column not found" );
998 : }
999 0 : }
1000 :
1001 0 : if( static_cast<size_t>(nCols) != aColFlds.size() )
1002 : {
1003 : OSL_ENSURE( false, "not all database columns found" );
1004 0 : nCols = static_cast<sal_Int32>(aColFlds.size());
1005 : }
1006 :
1007 0 : if(!nRows || !nCols)
1008 : {
1009 : OSL_ENSURE( false, "wrong parameters" );
1010 0 : break;
1011 : }
1012 :
1013 0 : const SwModuleOptions* pModOpt = SW_MOD()->GetModuleConfig();
1014 :
1015 0 : bool bHTML = 0 != (::GetHtmlMode( pView->GetDocShell() ) & HTMLMODE_ON);
1016 : rSh.InsertTable(
1017 0 : pModOpt->GetInsTblFlags(bHTML),
1018 0 : nRows, nCols, text::HoriOrientation::FULL, (pSelection ? pTAutoFmt : 0) );
1019 0 : rSh.MoveTable( GetfnTablePrev(), GetfnTableStart() );
1020 :
1021 0 : if( pSelection && pTblSet )
1022 0 : SetTabSet();
1023 :
1024 0 : SfxItemSet aTblSet( rSh.GetAttrPool(), RES_BOXATR_FORMAT,
1025 0 : RES_BOXATR_VALUE );
1026 0 : bool bIsAutoUpdateCells = rSh.IsAutoUpdateCells();
1027 0 : rSh.SetAutoUpdateCells( false );
1028 :
1029 0 : if( m_pCbTableHeadon->IsChecked() )
1030 : {
1031 0 : for( sal_Int32 n = 0; n < nCols; ++n )
1032 : {
1033 0 : if( m_pRbHeadlColnms->IsChecked() )
1034 : {
1035 0 : rSh.SwEditShell::Insert2( aColFlds[ n ]->sColumn );
1036 : }
1037 0 : rSh.GoNextCell();
1038 : }
1039 : }
1040 : else
1041 0 : rSh.SetRowsToRepeat( 0 );
1042 :
1043 0 : for( sal_Int32 i = 0 ; ; ++i )
1044 : {
1045 0 : bool bBreak = false;
1046 : try
1047 : {
1048 0 : if(pSelection)
1049 : {
1050 0 : sal_Int32 nPos = 0;
1051 0 : pSelection[i] >>= nPos;
1052 0 : bBreak = !xResultSet->absolute(nPos);
1053 : }
1054 0 : else if(!i)
1055 0 : bBreak = !xResultSet->first();
1056 : }
1057 0 : catch (const Exception&)
1058 : {
1059 0 : bBreak = true;
1060 : }
1061 0 : if(bBreak)
1062 0 : break;
1063 :
1064 0 : for( sal_Int32 n = 0; n < nCols; ++n )
1065 : {
1066 : // at the very first time, NO GoNextCell, because we're
1067 : // already in it. Also no GoNextCell after the Insert,
1068 : // because an empty line is added at the end.
1069 0 : if( i || n )
1070 0 : rSh.GoNextCell();
1071 :
1072 0 : const SwInsDBColumn* pEntry = aColFlds[ n ];
1073 :
1074 0 : Reference< XColumn > xColumn;
1075 0 : xCols->getByName(pEntry->sColumn) >>= xColumn;
1076 0 : Reference< XPropertySet > xColumnProps( xColumn, UNO_QUERY );
1077 0 : sal_Int32 eDataType = 0;
1078 0 : if( xColumnProps.is() )
1079 : {
1080 0 : Any aType = xColumnProps->getPropertyValue("Type");
1081 0 : aType >>= eDataType;
1082 : }
1083 : try
1084 : {
1085 0 : if( pEntry->bHasFmt )
1086 : {
1087 : SwTblBoxNumFormat aNumFmt(
1088 : pEntry->bIsDBFmt ? pEntry->nDBNumFmt
1089 0 : : pEntry->nUsrNumFmt );
1090 0 : aTblSet.Put(aNumFmt);
1091 0 : if( xColumn.is() )
1092 : {
1093 0 : double fVal = xColumn->getDouble();
1094 0 : if( xColumn->wasNull() )
1095 0 : aTblSet.ClearItem( RES_BOXATR_VALUE );
1096 : else
1097 : {
1098 0 : if(rNumFmtr.GetType(aNumFmt.GetValue()) & NUMBERFORMAT_DATE)
1099 : {
1100 0 : ::Date aStandard(1,1,1900);
1101 0 : if (*rNumFmtr.GetNullDate() != aStandard)
1102 0 : fVal += (aStandard - *rNumFmtr.GetNullDate());
1103 : }
1104 0 : aTblSet.Put( SwTblBoxValue( fVal ));
1105 : }
1106 : }
1107 : else
1108 0 : aTblSet.ClearItem( RES_BOXATR_VALUE );
1109 0 : rSh.SetTblBoxFormulaAttrs( aTblSet );
1110 : }
1111 : //#i60207# don't insert binary data as string - creates a loop
1112 0 : else if( DataType::BINARY == eDataType ||
1113 0 : DataType::VARBINARY == eDataType ||
1114 0 : DataType::LONGVARBINARY== eDataType ||
1115 0 : DataType::SQLNULL == eDataType ||
1116 0 : DataType::OTHER == eDataType ||
1117 0 : DataType::OBJECT == eDataType ||
1118 0 : DataType::DISTINCT == eDataType ||
1119 0 : DataType::STRUCT == eDataType ||
1120 0 : DataType::ARRAY == eDataType ||
1121 0 : DataType::BLOB == eDataType ||
1122 0 : DataType::CLOB == eDataType ||
1123 0 : DataType::REF == eDataType
1124 : )
1125 : {
1126 : // do nothing
1127 : }
1128 : else
1129 : {
1130 0 : const OUString sVal = xColumn->getString();
1131 0 : if(!xColumn->wasNull())
1132 : {
1133 0 : rSh.SwEditShell::Insert2( sVal );
1134 0 : }
1135 : }
1136 : }
1137 0 : catch (const Exception& rExcept)
1138 : {
1139 : OSL_FAIL(OUStringToOString(rExcept.Message, osl_getThreadTextEncoding()).getStr());
1140 : (void)rExcept;
1141 : }
1142 0 : }
1143 :
1144 0 : if( !pSelection )
1145 : {
1146 0 : if ( !xResultSet->next() )
1147 0 : break;
1148 : }
1149 0 : else if( i+1 >= rSelection.getLength() )
1150 0 : break;
1151 :
1152 0 : if( 10 == i )
1153 0 : pWait.reset(new SwWait( *pView->GetDocShell(), true ));
1154 0 : }
1155 :
1156 0 : rSh.MoveTable( GetfnTableCurr(), GetfnTableStart() );
1157 0 : if( !pSelection && ( pTblSet || pTAutoFmt ))
1158 : {
1159 0 : if( pTblSet )
1160 0 : SetTabSet();
1161 :
1162 0 : if( pTAutoFmt )
1163 0 : rSh.SetTableAutoFmt( *pTAutoFmt );
1164 : }
1165 0 : rSh.SetAutoUpdateCells( bIsAutoUpdateCells );
1166 : }
1167 : else // add data as fields/text
1168 : {
1169 0 : _DB_Columns aColArr;
1170 0 : if( SplitTextToColArr( m_pEdDbText->GetText(), aColArr, m_pRbAsField->IsChecked() ) )
1171 : {
1172 : // now for each data set, we can iterate over the array
1173 : // and add the data
1174 :
1175 0 : if( !rSh.IsSttPara() )
1176 0 : rSh.SwEditShell::SplitNode();
1177 0 : if( !rSh.IsEndPara() )
1178 : {
1179 0 : rSh.SwEditShell::SplitNode();
1180 0 : rSh.SwCrsrShell::Left(1,CRSR_SKIP_CHARS);
1181 : }
1182 :
1183 0 : rSh.DoUndo( false );
1184 :
1185 0 : SwTxtFmtColl* pColl = 0;
1186 : {
1187 0 : const OUString sTmplNm( m_pLbDbParaColl->GetSelectEntry() );
1188 0 : if( sNoTmpl != sTmplNm )
1189 : {
1190 0 : pColl = rSh.FindTxtFmtCollByName( sTmplNm );
1191 0 : if( !pColl )
1192 : {
1193 : const sal_uInt16 nId = SwStyleNameMapper::GetPoolIdFromUIName(
1194 0 : sTmplNm, nsSwGetPoolIdFromName::GET_POOLID_TXTCOLL );
1195 0 : if( USHRT_MAX != nId )
1196 0 : pColl = rSh.GetTxtCollFromPool( nId );
1197 : else
1198 0 : pColl = rSh.MakeTxtFmtColl( sTmplNm );
1199 : }
1200 0 : rSh.SetTxtFmtColl( pColl );
1201 0 : }
1202 : }
1203 :
1204 : // for adding as fields -> insert a "NextField" after
1205 : // every data set
1206 0 : SwDBFormatData aDBFormatData;
1207 0 : Reference< XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
1208 0 : aDBFormatData.xFormatter = Reference<util::XNumberFormatter>(util::NumberFormatter::create(xContext), UNO_QUERY_THROW) ;
1209 :
1210 0 : Reference<XPropertySet> xSourceProps(xSource, UNO_QUERY);
1211 0 : if(xSourceProps.is())
1212 : {
1213 0 : Any aFormats = xSourceProps->getPropertyValue("NumberFormatsSupplier");
1214 0 : if(aFormats.hasValue())
1215 : {
1216 0 : Reference< util::XNumberFormatsSupplier> xSuppl;
1217 0 : aFormats >>= xSuppl;
1218 0 : if(xSuppl.is())
1219 : {
1220 0 : Reference< XPropertySet > xSettings = xSuppl->getNumberFormatSettings();
1221 0 : Any aNull = xSettings->getPropertyValue("NullDate");
1222 0 : aNull >>= aDBFormatData.aNullDate;
1223 0 : if(aDBFormatData.xFormatter.is())
1224 0 : aDBFormatData.xFormatter->attachNumberFormatsSupplier(xSuppl);
1225 0 : }
1226 0 : }
1227 : }
1228 0 : aDBFormatData.aLocale = LanguageTag( rSh.GetCurLang() ).getLocale();
1229 : SwDBNextSetField aNxtDBFld( (SwDBNextSetFieldType*)rSh.
1230 0 : GetFldType( 0, RES_DBNEXTSETFLD ),
1231 0 : "1", "", aDBData );
1232 :
1233 0 : bool bSetCrsr = true;
1234 0 : const size_t nCols = aColArr.size();
1235 0 : ::sw::mark::IMark* pMark = NULL;
1236 0 : for( sal_Int32 i = 0 ; ; ++i )
1237 : {
1238 0 : bool bBreak = false;
1239 : try
1240 : {
1241 0 : if(pSelection)
1242 : {
1243 0 : sal_Int32 nPos = 0;
1244 0 : pSelection[i] >>= nPos;
1245 0 : bBreak = !xResultSet->absolute(nPos);
1246 : }
1247 0 : else if(!i)
1248 0 : bBreak = !xResultSet->first();
1249 : }
1250 0 : catch (const Exception&)
1251 : {
1252 0 : bBreak = true;
1253 : }
1254 :
1255 0 : if(bBreak)
1256 0 : break;
1257 :
1258 0 : for( size_t n = 0; n < nCols; ++n )
1259 : {
1260 0 : _DB_Column* pDBCol = &aColArr[ n ];
1261 0 : OUString sIns;
1262 0 : switch( pDBCol->eColType )
1263 : {
1264 : case _DB_Column::DB_FILLTEXT:
1265 0 : sIns = *pDBCol->DB_ColumnData.pText;
1266 0 : break;
1267 :
1268 : case _DB_Column::DB_SPLITPARA:
1269 0 : rSh.SplitNode();
1270 : // when the template is not the same as the follow template,
1271 : // the selected has to be set newly
1272 0 : if( pColl && &pColl->GetNextTxtFmtColl() != pColl )
1273 0 : rSh.SetTxtFmtColl( pColl );
1274 0 : break;
1275 :
1276 : case _DB_Column::DB_COL_FIELD:
1277 : {
1278 : boost::scoped_ptr<SwDBField> pFld(static_cast<SwDBField *>(
1279 0 : pDBCol->DB_ColumnData.pField->CopyField()));
1280 0 : double nValue = DBL_MAX;
1281 :
1282 0 : Reference< XPropertySet > xColumnProps;
1283 0 : xCols->getByName(pDBCol->pColInfo->sColumn) >>= xColumnProps;
1284 :
1285 : pFld->SetExpansion( SwDBManager::GetDBField(
1286 : xColumnProps,
1287 : aDBFormatData,
1288 0 : &nValue ) );
1289 0 : if( DBL_MAX != nValue )
1290 : {
1291 0 : Any aType = xColumnProps->getPropertyValue("Type");
1292 0 : sal_Int32 eDataType = 0;
1293 0 : aType >>= eDataType;
1294 0 : if( DataType::DATE == eDataType || DataType::TIME == eDataType ||
1295 0 : DataType::TIMESTAMP == eDataType)
1296 :
1297 : {
1298 0 : ::Date aStandard(1,1,1900);
1299 : ::Date aCompare(aDBFormatData.aNullDate.Day ,
1300 : aDBFormatData.aNullDate.Month,
1301 0 : aDBFormatData.aNullDate.Year);
1302 0 : if(aStandard != aCompare)
1303 0 : nValue += (aStandard - aCompare);
1304 : }
1305 0 : pFld->ChgValue( nValue, true );
1306 : }
1307 0 : pFld->SetInitialized();
1308 :
1309 0 : rSh.Insert( *pFld );
1310 : }
1311 0 : break;
1312 :
1313 : case _DB_Column::DB_COL_TEXT:
1314 : {
1315 0 : double nValue = DBL_MAX;
1316 0 : Reference< XPropertySet > xColumnProps;
1317 0 : xCols->getByName(pDBCol->pColInfo->sColumn) >>= xColumnProps;
1318 0 : sIns = SwDBManager::GetDBField(
1319 : xColumnProps,
1320 : aDBFormatData,
1321 0 : &nValue );
1322 0 : if( pDBCol->DB_ColumnData.nFormat &&
1323 0 : DBL_MAX != nValue )
1324 : {
1325 : Color* pCol;
1326 0 : if(rNumFmtr.GetType(pDBCol->DB_ColumnData.nFormat) & NUMBERFORMAT_DATE)
1327 : {
1328 0 : ::Date aStandard(1,1,1900);
1329 0 : if (*rNumFmtr.GetNullDate() != aStandard)
1330 0 : nValue += (aStandard - *rNumFmtr.GetNullDate());
1331 : }
1332 : rNumFmtr.GetOutputString( nValue,
1333 : pDBCol->DB_ColumnData.nFormat,
1334 0 : sIns, &pCol );
1335 0 : }
1336 : }
1337 0 : break;
1338 : }
1339 :
1340 0 : if( !sIns.isEmpty() )
1341 : {
1342 0 : rSh.Insert( sIns );
1343 :
1344 0 : if( bSetCrsr)
1345 : {
1346 : // to the beginning and set a mark, so that
1347 : // the cursor can be set to the initial position
1348 : // at the end.
1349 :
1350 : rSh.SwCrsrShell::MovePara(
1351 0 : GetfnParaCurr(), GetfnParaStart() );
1352 : pMark = rSh.SetBookmark(
1353 : vcl::KeyCode(),
1354 : OUString(),
1355 0 : OUString(), IDocumentMarkAccess::UNO_BOOKMARK );
1356 : rSh.SwCrsrShell::MovePara(
1357 0 : GetfnParaCurr(), GetfnParaEnd() );
1358 0 : bSetCrsr = false;
1359 : }
1360 : }
1361 0 : }
1362 :
1363 0 : if( !pSelection )
1364 : {
1365 0 : bool bNext = xResultSet->next();
1366 0 : if(!bNext)
1367 0 : break;
1368 : }
1369 0 : else if( i+1 >= rSelection.getLength() )
1370 0 : break;
1371 :
1372 0 : if( m_pRbAsField->IsChecked() )
1373 0 : rSh.Insert( aNxtDBFld );
1374 :
1375 0 : if( !rSh.IsSttPara() )
1376 0 : rSh.SwEditShell::SplitNode();
1377 :
1378 0 : if( 10 == i )
1379 0 : pWait.reset(new SwWait( *pView->GetDocShell(), true ));
1380 0 : }
1381 :
1382 0 : if( !bSetCrsr && pMark != NULL)
1383 : {
1384 0 : rSh.SetMark();
1385 0 : rSh.GotoMark( pMark );
1386 0 : rSh.getIDocumentMarkAccess()->deleteMark( pMark );
1387 0 : break;
1388 0 : }
1389 0 : }
1390 : }
1391 : // write configuration
1392 0 : Commit();
1393 : }while( false ); // middle checked loop
1394 :
1395 0 : if( bUndo )
1396 : {
1397 0 : rSh.DoUndo( true );
1398 0 : rSh.AppendUndoForInsertFromDB( bAsTable );
1399 0 : rSh.EndUndo( UNDO_EMPTY );
1400 : }
1401 0 : rSh.ClearMark();
1402 0 : rSh.EndAllAction();
1403 :
1404 0 : if ( bDisposeResultSet )
1405 0 : ::comphelper::disposeComponent(xResultSet);
1406 : }
1407 :
1408 0 : void SwInsertDBColAutoPilot::SetTabSet()
1409 : {
1410 0 : SwWrtShell& rSh = pView->GetWrtShell();
1411 : const SfxPoolItem* pItem;
1412 :
1413 0 : if( pTAutoFmt )
1414 : {
1415 0 : if( pTAutoFmt->IsFrame() )
1416 : {
1417 : // border is from AutoFormat
1418 0 : pTblSet->ClearItem( RES_BOX );
1419 0 : pTblSet->ClearItem( SID_ATTR_BORDER_INNER );
1420 : }
1421 0 : if( pTAutoFmt->IsBackground() )
1422 : {
1423 0 : pTblSet->ClearItem( RES_BACKGROUND );
1424 0 : pTblSet->ClearItem( SID_ATTR_BRUSH_ROW );
1425 0 : pTblSet->ClearItem( SID_ATTR_BRUSH_TABLE );
1426 : }
1427 : }
1428 : else
1429 : {
1430 : // remove the defaults again, it makes no sense to set them
1431 0 : SvxBrushItem aBrush( RES_BACKGROUND );
1432 : static const sal_uInt16 aIds[3] =
1433 : { RES_BACKGROUND, SID_ATTR_BRUSH_ROW, SID_ATTR_BRUSH_TABLE };
1434 0 : for( int i = 0; i < 3; ++i )
1435 0 : if( SfxItemState::SET == pTblSet->GetItemState( aIds[ i ],
1436 0 : false, &pItem ) && *pItem == aBrush )
1437 0 : pTblSet->ClearItem( aIds[ i ] );
1438 : }
1439 :
1440 0 : if( SfxItemState::SET == pTblSet->GetItemState( FN_PARAM_TABLE_NAME, false,
1441 0 : &pItem ) && ((const SfxStringItem*)pItem)->GetValue() ==
1442 0 : rSh.GetTableFmt()->GetName() )
1443 0 : pTblSet->ClearItem( FN_PARAM_TABLE_NAME );
1444 :
1445 0 : rSh.MoveTable( GetfnTableCurr(), GetfnTableStart() );
1446 0 : rSh.SetMark();
1447 0 : rSh.MoveTable( GetfnTableCurr(), GetfnTableEnd() );
1448 :
1449 0 : ItemSetToTableParam( *pTblSet, rSh );
1450 :
1451 0 : rSh.ClearMark();
1452 0 : rSh.MoveTable( GetfnTableCurr(), GetfnTableStart() );
1453 0 : }
1454 :
1455 0 : _DB_ColumnConfigData::~_DB_ColumnConfigData() {}
1456 :
1457 0 : static Sequence<OUString> lcl_createSourceNames(const OUString& rNodeName)
1458 : {
1459 0 : Sequence<OUString> aSourceNames(11);
1460 0 : OUString* pNames = aSourceNames.getArray();
1461 0 : pNames[0] = rNodeName + "/DataSource";
1462 0 : pNames[1] = rNodeName + "/Command";
1463 0 : pNames[2] = rNodeName + "/CommandType";
1464 0 : pNames[3] = rNodeName + "/ColumnsToText";
1465 0 : pNames[4] = rNodeName + "/ColumnsToTable";
1466 0 : pNames[5] = rNodeName + "/ParaStyle";
1467 0 : pNames[6] = rNodeName + "/TableAutoFormat";
1468 0 : pNames[7] = rNodeName + "/IsTable";
1469 0 : pNames[8] = rNodeName + "/IsField";
1470 0 : pNames[9] = rNodeName + "/IsHeadlineOn";
1471 0 : pNames[10] = rNodeName + "/IsEmptyHeadline";
1472 0 : return aSourceNames;
1473 : }
1474 :
1475 0 : static Sequence<OUString> lcl_CreateSubNames(const OUString& rSubNodeName)
1476 : {
1477 0 : Sequence<OUString> aSubSourceNames(6);
1478 0 : OUString* pNames = aSubSourceNames.getArray();
1479 0 : pNames[0] = rSubNodeName + "/ColumnName";
1480 0 : pNames[1] = rSubNodeName + "/ColumnIndex";
1481 0 : pNames[2] = rSubNodeName + "/IsNumberFormat";
1482 0 : pNames[3] = rSubNodeName + "/IsNumberFormatFromDataBase";
1483 0 : pNames[4] = rSubNodeName + "/NumberFormat";
1484 0 : pNames[5] = rSubNodeName + "/NumberFormatLocale";
1485 0 : return aSubSourceNames;
1486 : }
1487 :
1488 0 : static OUString lcl_CreateUniqueName(const Sequence<OUString>& aNames)
1489 : {
1490 0 : const sal_Int32 nNames = aNames.getLength();
1491 0 : sal_Int32 nIdx = nNames;
1492 0 : const OUString* pNames = aNames.getConstArray();
1493 : while(true)
1494 : {
1495 0 : const OUString sRet = "_" + OUString::number(nIdx++);
1496 0 : sal_Int32 i = 0;
1497 0 : while ( i < nNames && pNames[i] != sRet )
1498 : {
1499 0 : ++i;
1500 : }
1501 0 : if ( i >= nNames )
1502 0 : return sRet; // No match found, return unique name
1503 0 : }
1504 : }
1505 :
1506 0 : void SwInsertDBColAutoPilot::Notify( const ::com::sun::star::uno::Sequence< OUString >& ) {}
1507 :
1508 0 : void SwInsertDBColAutoPilot::Commit()
1509 : {
1510 0 : Sequence <OUString> aNames = GetNodeNames(OUString());
1511 0 : const OUString* pNames = aNames.getArray();
1512 : //remove entries that contain this data source + table at first
1513 0 : for(sal_Int32 nNode = 0; nNode < aNames.getLength(); nNode++)
1514 : {
1515 0 : Sequence<OUString> aSourceNames(2);
1516 0 : OUString* pSourceNames = aSourceNames.getArray();
1517 0 : pSourceNames[0] = pNames[nNode] + "/DataSource";
1518 0 : pSourceNames[1] = pNames[nNode] + "/Command";
1519 0 : Sequence<Any> aSourceProperties = GetProperties(aSourceNames);
1520 0 : const Any* pSourceProps = aSourceProperties.getArray();
1521 0 : OUString sSource, sCommand;
1522 0 : pSourceProps[0] >>= sSource;
1523 0 : pSourceProps[1] >>= sCommand;
1524 0 : if(sSource==aDBData.sDataSource && sCommand==aDBData.sCommand)
1525 : {
1526 0 : Sequence<OUString> aElements(1);
1527 0 : aElements.getArray()[0] = pNames[nNode];
1528 0 : ClearNodeElements(OUString(), aElements);
1529 : }
1530 0 : }
1531 :
1532 0 : aNames = GetNodeNames(OUString());
1533 0 : OUString sNewNode = lcl_CreateUniqueName(aNames);
1534 0 : Sequence<OUString> aNodeNames = lcl_createSourceNames(sNewNode);
1535 0 : Sequence<PropertyValue> aValues(aNodeNames.getLength());
1536 0 : PropertyValue* pValues = aValues.getArray();
1537 0 : const OUString* pNodeNames = aNodeNames.getConstArray();
1538 0 : for(sal_Int32 i = 0; i < aNodeNames.getLength(); i++)
1539 : {
1540 0 : pValues[i].Name = "/" + pNodeNames[i];
1541 : }
1542 :
1543 0 : pValues[0].Value <<= aDBData.sDataSource;
1544 0 : pValues[1].Value <<= aDBData.sCommand;
1545 0 : pValues[2].Value <<= aDBData.nCommandType;
1546 0 : pValues[3].Value <<= m_pEdDbText->GetText();
1547 :
1548 0 : OUString sTmp;
1549 0 : const sal_Int32 nCnt = m_pLbTableCol->GetEntryCount();
1550 0 : for( sal_Int32 n = 0; n < nCnt; ++n )
1551 0 : sTmp += m_pLbTableCol->GetEntry(n) + "\x0a";
1552 :
1553 0 : if (!sTmp.isEmpty())
1554 0 : pValues[4].Value <<= sTmp;
1555 :
1556 0 : if( sNoTmpl != (sTmp = m_pLbDbParaColl->GetSelectEntry()) )
1557 0 : pValues[5].Value <<= sTmp;
1558 :
1559 0 : if( pTAutoFmt )
1560 0 : pValues[6].Value <<= pTAutoFmt->GetName();
1561 :
1562 0 : const Type& rBoolType = ::getBooleanCppuType();
1563 0 : sal_Bool bTmp = m_pRbAsTable->IsChecked();
1564 0 : pValues[7].Value.setValue(&bTmp, rBoolType);
1565 :
1566 0 : bTmp = m_pRbAsField->IsChecked();
1567 0 : pValues[8].Value.setValue(&bTmp, rBoolType);
1568 :
1569 0 : bTmp = m_pCbTableHeadon->IsChecked();
1570 0 : pValues[9].Value.setValue(&bTmp, rBoolType);
1571 :
1572 0 : bTmp = m_pRbHeadlEmpty->IsChecked();
1573 0 : pValues[10].Value.setValue(&bTmp, rBoolType);
1574 :
1575 0 : SetSetProperties(OUString(), aValues);
1576 :
1577 0 : sNewNode += "/ColumnSet";
1578 :
1579 0 : LanguageType ePrevLang = (LanguageType)-1;
1580 :
1581 0 : SvNumberFormatter& rNFmtr = *pView->GetWrtShell().GetNumberFormatter();
1582 0 : for(size_t nCol = 0; nCol < aDBColumns.size(); nCol++)
1583 : {
1584 0 : SwInsDBColumn* pColumn = aDBColumns[nCol];
1585 0 : OUString sColumnInsertNode(sNewNode + "/__");
1586 0 : if( nCol < 10 )
1587 0 : sColumnInsertNode += "00";
1588 0 : else if( nCol < 100 )
1589 0 : sColumnInsertNode += "0";
1590 0 : sColumnInsertNode += OUString::number( nCol );
1591 :
1592 0 : Sequence <OUString> aSubNodeNames = lcl_CreateSubNames(sColumnInsertNode);
1593 0 : Sequence<PropertyValue> aSubValues(aSubNodeNames.getLength());
1594 0 : PropertyValue* pSubValues = aSubValues.getArray();
1595 0 : const OUString* pSubNodeNames = aSubNodeNames.getConstArray();
1596 : sal_Int32 i;
1597 :
1598 0 : for( i = 0; i < aSubNodeNames.getLength(); i++)
1599 0 : pSubValues[i].Name = pSubNodeNames[i];
1600 0 : pSubValues[0].Value <<= pColumn->sColumn;
1601 0 : pSubValues[1].Value <<= i;
1602 :
1603 0 : sal_Bool bVal = pColumn->bHasFmt;
1604 0 : pSubValues[2].Value.setValue(&bVal, rBoolType);
1605 0 : bVal = pColumn->bIsDBFmt;
1606 0 : pSubValues[3].Value.setValue(&bVal, rBoolType);
1607 :
1608 0 : SwStyleNameMapper::FillUIName( RES_POOLCOLL_STANDARD, sTmp );
1609 0 : const SvNumberformat* pNF = rNFmtr.GetEntry( pColumn->nUsrNumFmt );
1610 : LanguageType eLang;
1611 0 : if( pNF )
1612 : {
1613 0 : pSubValues[4].Value <<= pNF->GetFormatstring();
1614 0 : eLang = pNF->GetLanguage();
1615 : }
1616 : else
1617 : {
1618 0 : pSubValues[4].Value <<= sTmp;
1619 0 : eLang = GetAppLanguage();
1620 : }
1621 :
1622 0 : OUString sPrevLang;
1623 0 : if( eLang != ePrevLang )
1624 : {
1625 0 : sPrevLang = LanguageTag::convertToBcp47( eLang );
1626 0 : ePrevLang = eLang;
1627 : }
1628 :
1629 0 : pSubValues[5].Value <<= sPrevLang;
1630 0 : SetSetProperties(sNewNode, aSubValues);
1631 0 : }
1632 0 : }
1633 :
1634 0 : void SwInsertDBColAutoPilot::Load()
1635 : {
1636 0 : Sequence <OUString> aNames = GetNodeNames(OUString());
1637 0 : const OUString* pNames = aNames.getArray();
1638 0 : SvNumberFormatter& rNFmtr = *pView->GetWrtShell().GetNumberFormatter();
1639 0 : for(sal_Int32 nNode = 0; nNode < aNames.getLength(); nNode++)
1640 : {
1641 : //search for entries with the appropriate data source and table
1642 0 : Sequence<OUString> aSourceNames = lcl_createSourceNames(pNames[nNode]);
1643 :
1644 0 : Sequence< Any> aDataSourceProps = GetProperties(aSourceNames);
1645 0 : const Any* pDataSourceProps = aDataSourceProps.getConstArray();
1646 0 : OUString sSource, sCommand;
1647 : sal_Int16 nCommandType;
1648 0 : pDataSourceProps[0] >>= sSource;
1649 0 : pDataSourceProps[1] >>= sCommand;
1650 0 : pDataSourceProps[2] >>= nCommandType;
1651 0 : if(sSource.equals(aDBData.sDataSource) && sCommand.equals(aDBData.sCommand))
1652 : {
1653 0 : boost::scoped_ptr<_DB_ColumnConfigData> pNewData(new _DB_ColumnConfigData);
1654 0 : pNewData->sSource = sSource;
1655 0 : pNewData->sTable = sCommand;
1656 :
1657 0 : pDataSourceProps[3] >>= pNewData->sEdit;
1658 0 : pDataSourceProps[4] >>= pNewData->sTblList;
1659 0 : pDataSourceProps[5] >>= pNewData->sTmplNm;
1660 0 : pDataSourceProps[6] >>= pNewData->sTAutoFmtNm;
1661 0 : if(pDataSourceProps[7].hasValue())
1662 0 : pNewData->bIsTable = *(sal_Bool*)pDataSourceProps[7].getValue();
1663 0 : if(pDataSourceProps[8].hasValue())
1664 0 : pNewData->bIsField = *(sal_Bool*)pDataSourceProps[8].getValue();
1665 0 : if(pDataSourceProps[9].hasValue())
1666 0 : pNewData->bIsHeadlineOn = *(sal_Bool*)pDataSourceProps[9].getValue();
1667 0 : if(pDataSourceProps[10].hasValue())
1668 0 : pNewData->bIsEmptyHeadln = *(sal_Bool*)pDataSourceProps[10].getValue();
1669 :
1670 0 : const OUString sSubNodeName(pNames[nNode] + "/ColumnSet/");
1671 0 : Sequence <OUString> aSubNames = GetNodeNames(sSubNodeName);
1672 0 : const OUString* pSubNames = aSubNames.getConstArray();
1673 0 : for(sal_Int32 nSub = 0; nSub < aSubNames.getLength(); nSub++)
1674 : {
1675 : Sequence <OUString> aSubNodeNames =
1676 0 : lcl_CreateSubNames(sSubNodeName + pSubNames[nSub]);
1677 0 : Sequence< Any> aSubProps = GetProperties(aSubNodeNames);
1678 0 : const Any* pSubProps = aSubProps.getConstArray();
1679 :
1680 0 : OUString sColumn;
1681 0 : pSubProps[0] >>= sColumn;
1682 : // check for existence of the loaded column name
1683 0 : bool bFound = false;
1684 0 : for(size_t nRealColumn = 0; nRealColumn < aDBColumns.size(); ++nRealColumn)
1685 : {
1686 0 : if(aDBColumns[nRealColumn]->sColumn == sColumn)
1687 : {
1688 0 : bFound = true;
1689 0 : break;
1690 : }
1691 : }
1692 0 : if(!bFound)
1693 0 : continue;
1694 0 : sal_Int16 nIndex = 0;
1695 0 : pSubProps[1] >>= nIndex;
1696 0 : SwInsDBColumn* pInsDBColumn = new SwInsDBColumn(sColumn, nIndex);
1697 0 : if(pSubProps[2].hasValue())
1698 0 : pInsDBColumn->bHasFmt = *(sal_Bool*)pSubProps[2].getValue();
1699 0 : if(pSubProps[3].hasValue())
1700 0 : pInsDBColumn->bIsDBFmt = *(sal_Bool*)pSubProps[3].getValue();
1701 :
1702 0 : pSubProps[4] >>= pInsDBColumn->sUsrNumFmt;
1703 0 : OUString sNumberFormatLocale;
1704 0 : pSubProps[5] >>= sNumberFormatLocale;
1705 :
1706 : /* XXX Earlier versions wrote a Country-Language string in
1707 : * SwInsertDBColAutoPilot::Commit() that here was read as
1708 : * Language-Country with 2 characters copied to language,
1709 : * 1 character separator and unconditionally 2 characters read
1710 : * as country. So for 'DE-de' and locales that have similar
1711 : * case-insensitive equal language/country combos that may have
1712 : * worked, for all others not. FIXME if you need to read old
1713 : * data that you were never able to read before. */
1714 0 : pInsDBColumn->eUsrNumFmtLng = LanguageTag::convertToLanguageType( sNumberFormatLocale );
1715 :
1716 : pInsDBColumn->nUsrNumFmt = rNFmtr.GetEntryKey( pInsDBColumn->sUsrNumFmt,
1717 0 : pInsDBColumn->eUsrNumFmtLng );
1718 :
1719 0 : pNewData->aDBColumns.insert(pInsDBColumn);
1720 0 : }
1721 0 : OUString sTmp( pNewData->sTblList );
1722 0 : if( !sTmp.isEmpty() )
1723 : {
1724 0 : sal_Int32 n = 0;
1725 0 : do {
1726 0 : const OUString sEntry( sTmp.getToken( 0, '\x0a', n ) );
1727 : //preselect column - if they still exist!
1728 0 : if(m_pLbTblDbColumn->GetEntryPos(sEntry) != LISTBOX_ENTRY_NOTFOUND)
1729 : {
1730 0 : m_pLbTableCol->InsertEntry( sEntry );
1731 0 : m_pLbTblDbColumn->RemoveEntry( sEntry );
1732 0 : }
1733 0 : } while( n>=0 );
1734 :
1735 0 : if( !m_pLbTblDbColumn->GetEntryCount() )
1736 : {
1737 0 : m_pIbDbcolAllTo->Enable( false );
1738 0 : m_pIbDbcolOneTo->Enable( false );
1739 : }
1740 0 : m_pIbDbcolOneFrom->Enable( true );
1741 0 : m_pIbDbcolAllFrom->Enable( true );
1742 : }
1743 0 : m_pEdDbText->SetText( pNewData->sEdit );
1744 :
1745 0 : sTmp = pNewData->sTmplNm;
1746 0 : if( !sTmp.isEmpty() )
1747 0 : m_pLbDbParaColl->SelectEntry( sTmp );
1748 : else
1749 0 : m_pLbDbParaColl->SelectEntryPos( 0 );
1750 :
1751 0 : delete pTAutoFmt, pTAutoFmt = 0;
1752 0 : sTmp = pNewData->sTAutoFmtNm;
1753 0 : if( !sTmp.isEmpty() )
1754 : {
1755 : // then load the AutoFmt file and look for Autoformat first
1756 0 : SwTableAutoFmtTbl aAutoFmtTbl;
1757 0 : aAutoFmtTbl.Load();
1758 0 : for( size_t nAutoFmt = aAutoFmtTbl.size(); nAutoFmt; )
1759 0 : if( sTmp == aAutoFmtTbl[ --nAutoFmt ].GetName() )
1760 : {
1761 0 : pTAutoFmt = new SwTableAutoFmt( aAutoFmtTbl[ nAutoFmt ] );
1762 0 : break;
1763 0 : }
1764 : }
1765 :
1766 0 : m_pRbAsTable->Check( pNewData->bIsTable );
1767 0 : m_pRbAsField->Check( pNewData->bIsField );
1768 0 : m_pRbAsText->Check( !pNewData->bIsTable && !pNewData->bIsField );
1769 :
1770 0 : m_pCbTableHeadon->Check( pNewData->bIsHeadlineOn );
1771 0 : m_pRbHeadlColnms->Check( !pNewData->bIsEmptyHeadln );
1772 0 : m_pRbHeadlEmpty->Check( pNewData->bIsEmptyHeadln );
1773 0 : HeaderHdl(m_pCbTableHeadon);
1774 :
1775 : // now copy the user defined Numberformat strings to the
1776 : // Shell. Then only these are available as ID
1777 0 : for( size_t n = 0; n < aDBColumns.size() ; ++n )
1778 : {
1779 0 : SwInsDBColumn& rSet = *aDBColumns[ n ];
1780 0 : for( size_t m = 0; m < pNewData->aDBColumns.size() ; ++m )
1781 : {
1782 0 : SwInsDBColumn& rGet = *pNewData->aDBColumns[ m ];
1783 0 : if(rGet.sColumn == rSet.sColumn)
1784 : {
1785 0 : if( rGet.bHasFmt && !rGet.bIsDBFmt )
1786 : {
1787 0 : rSet.bIsDBFmt = false;
1788 : rSet.nUsrNumFmt = rNFmtr.GetEntryKey( rGet.sUsrNumFmt,
1789 0 : rGet.eUsrNumFmtLng );
1790 0 : if( NUMBERFORMAT_ENTRY_NOT_FOUND == rSet.nUsrNumFmt )
1791 : {
1792 : sal_Int32 nCheckPos;
1793 : short nType;
1794 : rNFmtr.PutEntry( rGet.sUsrNumFmt, nCheckPos, nType,
1795 0 : rSet.nUsrNumFmt, rGet.eUsrNumFmtLng );
1796 : }
1797 : }
1798 0 : break;
1799 : }
1800 : }
1801 : }
1802 :
1803 : // when the cursor is inside of a table, table must NEVER be selectable
1804 0 : if( !m_pRbAsTable->IsEnabled() && m_pRbAsTable->IsChecked() )
1805 0 : m_pRbAsField->Check( true );
1806 0 : break;
1807 : }
1808 0 : }
1809 0 : }
1810 :
1811 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|