xref: /AOO41X/main/sw/source/ui/dbui/dbinsdlg.cxx (revision 8ef2f12b1aeba1404ab3c221e6e26281826cc4fc)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sw.hxx"
26 #ifdef SW_DLLIMPLEMENTATION
27 #undef SW_DLLIMPLEMENTATION
28 #endif
29 
30 #include "dbinsdlg.hxx"
31 
32 #include <memory>
33 
34 #include <float.h>
35 
36 #include <hintids.hxx>
37 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
38 #include <com/sun/star/container/XNameAccess.hpp>
39 #include <com/sun/star/sdbc/XDataSource.hpp>
40 #include <com/sun/star/sdbc/XRow.hpp>
41 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
42 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
43 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
44 #include <com/sun/star/sdb/CommandType.hpp>
45 #include <com/sun/star/sdb/XColumn.hpp>
46 #include <com/sun/star/sdb/XDatabaseAccess.hpp>
47 #include <com/sun/star/sdbc/DataType.hpp>
48 #include <com/sun/star/sdbc/ResultSetType.hpp>
49 #include <com/sun/star/beans/XPropertySet.hpp>
50 #include <com/sun/star/util/XNumberFormatter.hpp>
51 #include <com/sun/star/util/XNumberFormatTypes.hpp>
52 #include <com/sun/star/sdbc/XRowSet.hpp>
53 #include <comphelper/processfactory.hxx>
54 #include <editeng/langitem.hxx>
55 #include <svl/numuno.hxx>
56 #include <svl/stritem.hxx>
57 #include <vcl/msgbox.hxx>
58 #include <vcl/svapp.hxx>
59 #include <vcl/mnemonic.hxx>
60 #include <svl/style.hxx>
61 #include <svl/zformat.hxx>
62 #include <svx/htmlmode.hxx>
63 #include <editeng/unolingu.hxx>
64 #include <sfx2/app.hxx>
65 #include <svl/itemset.hxx>
66 #include <editeng/brshitem.hxx>
67 #include <editeng/boxitem.hxx>
68 #include <svx/rulritem.hxx>
69 #include <swdbtoolsclient.hxx>
70 #include <tabledlg.hxx>
71 #include <fmtclds.hxx>
72 #include <tabcol.hxx>
73 #include <uiitems.hxx>
74 #include <viewopt.hxx>
75 #include <uitool.hxx>
76 #include <wrtsh.hxx>
77 #include <wview.hxx>
78 #include <docsh.hxx>
79 #include <dbmgr.hxx>
80 #include <tblafmt.hxx>
81 #include <cellatr.hxx>
82 #include <swtable.hxx>
83 #include <dbfld.hxx>
84 #include <fmtcol.hxx>
85 #include <section.hxx>
86 #include <swwait.hxx>
87 #include <modcfg.hxx>
88 #include <swmodule.hxx>
89 #include <poolfmt.hxx>
90 #include <crsskip.hxx>
91 
92 #include <dbinsdlg.hrc>
93 #include <dbui.hrc>
94 
95 #include <cmdid.h>
96 #include <helpid.h>
97 #include <cfgid.h>
98 #include <SwStyleNameMapper.hxx>
99 #include <comphelper/uno3.hxx>
100 #include "tabsh.hxx"
101 #include "swabstdlg.hxx"
102 #include "table.hrc"
103 #include <unomid.h>
104 #include <IDocumentMarkAccess.hxx>
105 
106 
107 namespace swui
108 {
109     SwAbstractDialogFactory * GetFactory();
110 }
111 
112 using namespace ::com::sun::star;
113 using namespace ::com::sun::star::uno;
114 using namespace ::com::sun::star::container;
115 using namespace ::com::sun::star::lang;
116 using namespace ::com::sun::star::sdb;
117 using namespace ::com::sun::star::sdbc;
118 using namespace ::com::sun::star::sdbcx;
119 using namespace ::com::sun::star::beans;
120 
121 // tblafmt.hxx
122 SV_IMPL_PTRARR( _SwTableAutoFmtTbl, SwTableAutoFmt* )
123 
124 const char cDBFldStart  = '<';
125 const char cDBFldEnd    = '>';
126 
127 // Hilfsstruktur fuers einfuegen von Datenbankspalten als Felder oder Text
128 struct _DB_Column
129 {
130     enum ColType { DB_FILLTEXT, DB_COL_FIELD, DB_COL_TEXT, DB_SPLITPARA } eColType;
131 
132     union {
133         String* pText;
134         SwField* pField;
135         sal_uLong nFormat;
136     } DB_ColumnData;
137     const SwInsDBColumn* pColInfo;
138 
_DB_Column_DB_Column139     _DB_Column()
140     {
141         pColInfo = 0;
142         DB_ColumnData.pText = 0;
143         eColType = DB_SPLITPARA;
144     }
145 
_DB_Column_DB_Column146     _DB_Column( const String& rTxt )
147     {
148         pColInfo = 0;
149         DB_ColumnData.pText = new String( rTxt );
150         eColType = DB_FILLTEXT;
151     }
152 
_DB_Column_DB_Column153     _DB_Column( const SwInsDBColumn& rInfo, sal_uLong nFormat )
154     {
155         pColInfo = &rInfo;
156         DB_ColumnData.nFormat = nFormat;
157         eColType = DB_COL_TEXT;
158     }
159 
_DB_Column_DB_Column160     _DB_Column( const SwInsDBColumn& rInfo, SwDBField& rFld )
161     {
162         pColInfo = &rInfo;
163         DB_ColumnData.pField = &rFld;
164         eColType = DB_COL_FIELD;
165     }
166 
~_DB_Column_DB_Column167     ~_DB_Column()
168     {
169         if( DB_COL_FIELD == eColType )
170             delete DB_ColumnData.pField;
171         else if( DB_FILLTEXT == eColType )
172             delete DB_ColumnData.pText;
173     }
174 };
175 
176 typedef _DB_Column* _DB_ColumnPtr;
177 SV_DECL_PTRARR_DEL( _DB_Columns, _DB_ColumnPtr, 32, 32 )
178 SV_IMPL_PTRARR( _DB_Columns, _DB_ColumnPtr )
179 
180 SV_IMPL_OP_PTRARR_SORT( SwInsDBColumns, SwInsDBColumnPtr )
181 
182 /*  */
183 
184 #define DBCOLUMN_CONFIG_VERSION1    1
185 #define DBCOLUMN_CONFIG_VERSION     DBCOLUMN_CONFIG_VERSION1
186 #define DBCOLUMN_MAXDATA            5
187 
188 struct _DB_ColumnConfigData
189 {
190     SwInsDBColumns aDBColumns;
191     rtl::OUString sSource, sTable, sEdit, sTblList, sTmplNm, sTAutoFmtNm;
192     sal_Bool bIsTable : 1,
193          bIsField : 1,
194          bIsHeadlineOn : 1,
195          bIsEmptyHeadln : 1;
196 
_DB_ColumnConfigData_DB_ColumnConfigData197     _DB_ColumnConfigData()
198     {
199         bIsTable = bIsHeadlineOn = sal_True;
200         bIsField = bIsEmptyHeadln = sal_False;
201     }
202 
203     ~_DB_ColumnConfigData();
204 private:
205     _DB_ColumnConfigData( const _DB_ColumnConfigData& );
206     _DB_ColumnConfigData& operator =( const _DB_ColumnConfigData& );
207 };
208 
209 /*  */
210 
operator <(const SwInsDBColumn & rCmp) const211 int SwInsDBColumn::operator<( const SwInsDBColumn& rCmp ) const
212 {
213     return 0 > GetAppCollator().compareString( sColumn, rCmp.sColumn );
214 }
215 /* ---------------------------------------------------------------------------
216 
217  ---------------------------------------------------------------------------*/
SwInsertDBColAutoPilot(SwView & rView,Reference<XDataSource> xDataSource,Reference<sdbcx::XColumnsSupplier> xColSupp,const SwDBData & rData)218 SwInsertDBColAutoPilot::SwInsertDBColAutoPilot( SwView& rView,
219         Reference<XDataSource> xDataSource,
220         Reference<sdbcx::XColumnsSupplier> xColSupp,
221         const SwDBData& rData )
222     : SfxModalDialog( rView.GetWindow(), SW_RES( DLG_AP_INSERT_DB_SEL )),
223     ConfigItem(C2U("Office.Writer/InsertData/DataSet"), CONFIG_MODE_DELAYED_UPDATE),
224     aFtInsertData( this, SW_RES( FT_INSERT_DATA )),
225     aRbAsTable( this, SW_RES( RB_AS_TABLE )),
226     aRbAsField( this, SW_RES( RB_AS_FIELD )),
227     aRbAsText( this, SW_RES( RB_AS_TEXT )),
228 
229     aFlHead( this, SW_RES( FL_HEAD )),
230     aFtDbColumn( this, SW_RES( FT_DB_COLUMN )),
231 
232     aLbTblDbColumn( this, SW_RES( LB_TBL_DB_COLUMN )),
233     aLbTxtDbColumn( this, SW_RES( LB_TXT_DB_COLUMN )),
234 
235     aFlFormat( this, SW_RES( FL_FORMAT )),
236     aRbDbFmtFromDb( this, SW_RES( RB_DBFMT_FROM_DB )),
237     aRbDbFmtFromUsr( this, SW_RES( RB_DBFMT_FROM_USR )),
238     aLbDbFmtFromUsr( this, &rView, SW_RES( LB_DBFMT_FROM_USR )),
239 
240     aIbDbcolToEdit( this, SW_RES( IB_DBCOL_TOEDIT )),
241     aEdDbText( this, SW_RES( ED_DB_TEXT )),
242     aFtDbParaColl( this, SW_RES( FT_DB_PARA_COLL )),
243     aLbDbParaColl( this, SW_RES( LB_DB_PARA_COLL )),
244 
245     aIbDbcolAllTo( this, SW_RES( IB_DBCOL_ALL_TO )),
246     aIbDbcolOneTo( this, SW_RES( IB_DBCOL_ONE_TO )),
247     aIbDbcolOneFrom( this, SW_RES( IB_DBCOL_ONE_FROM )),
248     aIbDbcolAllFrom( this, SW_RES( IB_DBCOL_ALL_FROM )),
249     aFtTableCol( this, SW_RES( FT_TABLE_COL )),
250     aLbTableCol( this, SW_RES( LB_TABLE_COL )),
251     aCbTableHeadon( this, SW_RES( CB_TABLE_HEADON )),
252     aRbHeadlColnms( this, SW_RES( RB_HEADL_COLNMS )),
253     aRbHeadlEmpty( this, SW_RES( RB_HEADL_EMPTY )),
254     aPbTblFormat( this, SW_RES( PB_TBL_FORMAT )),
255     aPbTblAutofmt( this, SW_RES( PB_TBL_AUTOFMT )),
256 
257     aBtOk( this, SW_RES( BT_OK )),
258     aBtCancel( this, SW_RES( BT_CANCEL )),
259     aBtHelp( this, SW_RES( BT_HELP )),
260 
261     aFlBottom( this, SW_RES( FL_BOTTOM )),
262 
263     aDBData(rData),
264 
265     aOldNumFmtLnk( aLbDbFmtFromUsr.GetSelectHdl() ),
266     sNoTmpl( SW_RES( STR_NOTEMPL )),
267     pView( &rView ),
268     pTAutoFmt( 0 ),
269     pTblSet( 0 ),
270     pRep( 0 )
271 {
272     FreeResource();
273 
274     nGBFmtLen = aFlFormat.GetText().Len();
275 
276     if(xColSupp.is())
277     {
278         SwWrtShell& rSh = pView->GetWrtShell();
279         Locale aDocLocale( SvxCreateLocale( rSh.GetCurLang() ));
280         SvNumberFormatter* pNumFmtr = rSh.GetNumberFormatter();
281         SvNumberFormatsSupplierObj* pNumFmt = new SvNumberFormatsSupplierObj( pNumFmtr );
282         Reference< util::XNumberFormatsSupplier >  xDocNumFmtsSupplier = pNumFmt;
283         Reference< util::XNumberFormats > xDocNumberFormats = xDocNumFmtsSupplier->getNumberFormats();
284         Reference< util::XNumberFormatTypes > xDocNumberFormatTypes(xDocNumberFormats, UNO_QUERY);
285 
286         Reference<XPropertySet> xSourceProps(xDataSource, UNO_QUERY);
287         Reference< util::XNumberFormats > xNumberFormats;
288         if(xSourceProps.is())
289         {
290             Any aFormats = xSourceProps->getPropertyValue(C2U("NumberFormatsSupplier"));
291             if(aFormats.hasValue())
292             {
293                 Reference< util::XNumberFormatsSupplier> xSuppl;
294                 aFormats >>= xSuppl;
295                 if(xSuppl.is())
296                 {
297                     xNumberFormats = xSuppl->getNumberFormats(  );
298                 }
299             }
300         }
301         Reference <XNameAccess> xCols = xColSupp->getColumns();
302         Sequence<rtl::OUString> aColNames = xCols->getElementNames();
303         const rtl::OUString* pColNames = aColNames.getConstArray();
304         long nCount = aColNames.getLength();
305         for (long n = 0; n < nCount; n++)
306         {
307             SwInsDBColumn* pNew = new SwInsDBColumn( pColNames[n], (sal_uInt16)n );
308             Any aCol = xCols->getByName(pColNames[n]);
309             Reference <XPropertySet> xCol;
310             aCol >>= xCol;
311             Any aType = xCol->getPropertyValue(C2S("Type"));
312             sal_Int32 eDataType = 0;
313             aType >>= eDataType;
314             switch(eDataType)
315             {
316                 case DataType::BIT:
317                 case DataType::BOOLEAN:
318                 case DataType::TINYINT:
319                 case DataType::SMALLINT:
320                 case DataType::INTEGER:
321                 case DataType::BIGINT:
322                 case DataType::FLOAT:
323                 case DataType::REAL:
324                 case DataType::DOUBLE:
325                 case DataType::NUMERIC:
326                 case DataType::DECIMAL:
327                 case DataType::DATE:
328                 case DataType::TIME:
329                 case DataType::TIMESTAMP:
330                 {
331                     pNew->bHasFmt = sal_True;
332                     Any aFormat = xCol->getPropertyValue(C2U("FormatKey"));
333                     if(aFormat.hasValue())
334                     {
335                         sal_Int32 nFmt = 0;
336                         aFormat >>= nFmt;
337                         if(xNumberFormats.is())
338                         {
339                             try
340                             {
341                                 Reference<XPropertySet> xNumProps = xNumberFormats->getByKey( nFmt );
342                                 Any aFormatVal = xNumProps->getPropertyValue(C2U("FormatString"));
343                                 Any aLocale = xNumProps->getPropertyValue(C2U("Locale"));
344                                 rtl::OUString sFormat;
345                                 aFormatVal >>= sFormat;
346                                 lang::Locale aLoc;
347                                 aLocale >>= aLoc;
348                                 long nKey = xDocNumberFormats->queryKey( sFormat, aLoc, sal_True);
349                                 if(nKey < 0)
350                                 {
351                                     nKey = xDocNumberFormats->addNew( sFormat, aLoc );
352                                 }
353                                 pNew->nDBNumFmt = nKey;
354                             }
355                             catch(const Exception& )
356                             {
357                                 DBG_ERROR("illegal number format key");
358                             }
359                         }
360                     }
361                     else
362                     {
363                         pNew->nDBNumFmt = SwNewDBMgr::GetDbtoolsClient().getDefaultNumberFormat(xCol,
364                                                     xDocNumberFormatTypes, aDocLocale);
365                     }
366 
367                 }
368                 break;
369             }
370             if( !aDBColumns.Insert( pNew ))
371             {
372                 ASSERT( !this, "Spaltenname mehrfach vergeben?" );
373                 delete pNew;
374             }
375         }
376     }
377 
378     // Absatzvorlagen-ListBox fuellen
379     {
380         SfxStyleSheetBasePool* pPool = pView->GetDocShell()->GetStyleSheetPool();
381         pPool->SetSearchMask( SFX_STYLE_FAMILY_PARA, SFXSTYLEBIT_ALL );
382         aLbDbParaColl.InsertEntry( sNoTmpl );
383 
384         const SfxStyleSheetBase* pBase = pPool->First();
385         while( pBase )
386         {
387             aLbDbParaColl.InsertEntry( pBase->GetName() );
388             pBase = pPool->Next();
389         }
390         aLbDbParaColl.SelectEntryPos( 0 );
391     }
392 
393     // steht der Cursor in einer Tabelle, darf NIE Tabelle auswaehlbar sein
394     if( pView->GetWrtShell().GetTableFmt() )
395     {
396         aRbAsTable.Enable( sal_False );
397         aRbAsField.Check( sal_True );
398         aRbDbFmtFromDb.Check( sal_True );
399     }
400     else
401     {
402         aRbAsTable.Check( sal_True );
403         aRbDbFmtFromDb.Check( sal_True );
404         aIbDbcolOneFrom.Enable( sal_False );
405         aIbDbcolAllFrom.Enable( sal_False );
406     }
407 
408     aRbAsTable.SetClickHdl( LINK(this, SwInsertDBColAutoPilot, PageHdl ));
409     aRbAsField.SetClickHdl( LINK(this, SwInsertDBColAutoPilot, PageHdl ));
410     aRbAsText.SetClickHdl( LINK(this, SwInsertDBColAutoPilot, PageHdl ));
411 
412     aRbDbFmtFromDb.SetClickHdl( LINK(this, SwInsertDBColAutoPilot, DBFormatHdl ));
413     aRbDbFmtFromUsr.SetClickHdl( LINK(this, SwInsertDBColAutoPilot, DBFormatHdl ));
414 
415     aPbTblFormat.SetClickHdl(LINK(this, SwInsertDBColAutoPilot, TblFmtHdl ));
416     aPbTblAutofmt.SetClickHdl(LINK(this, SwInsertDBColAutoPilot, AutoFmtHdl ));
417 
418     aIbDbcolAllTo.SetClickHdl( LINK(this, SwInsertDBColAutoPilot, TblToFromHdl ));
419     aIbDbcolOneTo.SetClickHdl( LINK(this, SwInsertDBColAutoPilot, TblToFromHdl ));
420     aIbDbcolOneFrom.SetClickHdl( LINK(this, SwInsertDBColAutoPilot, TblToFromHdl ));
421     aIbDbcolAllFrom.SetClickHdl( LINK(this, SwInsertDBColAutoPilot, TblToFromHdl ));
422     aIbDbcolToEdit.SetClickHdl( LINK(this, SwInsertDBColAutoPilot, TblToFromHdl ));
423 
424     aCbTableHeadon.SetClickHdl( LINK(this, SwInsertDBColAutoPilot, HeaderHdl ));
425     aRbHeadlColnms.SetClickHdl( LINK(this, SwInsertDBColAutoPilot, HeaderHdl ));
426     aRbHeadlEmpty.SetClickHdl( LINK(this, SwInsertDBColAutoPilot, HeaderHdl ));
427 
428     aLbTxtDbColumn.SetSelectHdl( LINK( this, SwInsertDBColAutoPilot, SelectHdl ));
429     aLbTblDbColumn.SetSelectHdl( LINK( this, SwInsertDBColAutoPilot, SelectHdl ));
430     aLbDbFmtFromUsr.SetSelectHdl( LINK( this, SwInsertDBColAutoPilot, SelectHdl ));
431     aLbTableCol.SetSelectHdl( LINK( this, SwInsertDBColAutoPilot, SelectHdl ));
432 
433     aLbTxtDbColumn.SetDoubleClickHdl( LINK( this, SwInsertDBColAutoPilot, DblClickHdl ));
434     aLbTblDbColumn.SetDoubleClickHdl( LINK( this, SwInsertDBColAutoPilot, DblClickHdl ));
435     aLbTableCol.SetDoubleClickHdl( LINK( this, SwInsertDBColAutoPilot, DblClickHdl ));
436 
437     for( sal_uInt16 n = 0; n < aDBColumns.Count(); ++n )
438     {
439         const String& rS = aDBColumns[ n ]->sColumn;
440         aLbTblDbColumn.InsertEntry( rS, n );
441         aLbTxtDbColumn.InsertEntry( rS, n );
442     }
443     aLbTxtDbColumn.SelectEntryPos( 0 );
444     aLbTblDbColumn.SelectEntryPos( 0 );
445 
446     // read configuration
447     Load();
448 
449     // Controls initialisieren:
450     PageHdl( aRbAsTable.IsChecked() ? &aRbAsTable : &aRbAsField );
451 }
452 /* ---------------------------------------------------------------------------
453 
454  ---------------------------------------------------------------------------*/
~SwInsertDBColAutoPilot()455 SwInsertDBColAutoPilot::~SwInsertDBColAutoPilot()
456 {
457     delete pTblSet;
458     delete pRep;
459 
460 //  delete pConfig;
461     delete pTAutoFmt;
462 }
463 /* ---------------------------------------------------------------------------
464 
465  ---------------------------------------------------------------------------*/
IMPL_LINK(SwInsertDBColAutoPilot,PageHdl,Button *,pButton)466 IMPL_LINK( SwInsertDBColAutoPilot, PageHdl, Button*, pButton )
467 {
468     sal_Bool bShowTbl = pButton == &aRbAsTable;
469 
470     String sTxt( pButton->GetText() );
471     aFlHead.SetText( MnemonicGenerator::EraseAllMnemonicChars( sTxt ) );
472 
473     aLbTxtDbColumn.Show( !bShowTbl );
474     aIbDbcolToEdit.Show( !bShowTbl );
475     aEdDbText.Show( !bShowTbl );
476     aFtDbParaColl.Show( !bShowTbl );
477     aLbDbParaColl.Show( !bShowTbl );
478 
479     aLbTblDbColumn.Show( bShowTbl );
480     aIbDbcolAllTo.Show( bShowTbl );
481     aIbDbcolOneTo.Show( bShowTbl );
482     aIbDbcolOneFrom.Show( bShowTbl );
483     aIbDbcolAllFrom.Show( bShowTbl );
484     aFtTableCol.Show( bShowTbl );
485     aLbTableCol.Show( bShowTbl );
486     aCbTableHeadon.Show( bShowTbl );
487     aRbHeadlColnms.Show( bShowTbl );
488     aRbHeadlEmpty.Show( bShowTbl );
489     aPbTblFormat.Show( bShowTbl );
490     aPbTblAutofmt.Show( bShowTbl );
491 
492     if( bShowTbl )
493         aPbTblFormat.Enable( 0 != aLbTableCol.GetEntryCount() );
494 
495     SelectHdl( bShowTbl ? &aLbTblDbColumn : &aLbTxtDbColumn );
496 
497     return 0;
498 }
499 /* ---------------------------------------------------------------------------
500 
501  ---------------------------------------------------------------------------*/
IMPL_LINK(SwInsertDBColAutoPilot,DBFormatHdl,Button *,pButton)502 IMPL_LINK( SwInsertDBColAutoPilot, DBFormatHdl, Button*, pButton )
503 {
504     sal_uInt16 nFndPos;
505     ListBox& rBox = aRbAsTable.IsChecked()
506                         ? ( 0 == aLbTableCol.GetEntryData( 0 )
507                             ? aLbTblDbColumn
508                             : aLbTableCol )
509                         : aLbTxtDbColumn;
510 
511     SwInsDBColumn aSrch( rBox.GetSelectEntry(), 0 );
512     aDBColumns.Seek_Entry( &aSrch, &nFndPos );
513 
514     sal_Bool bFromDB = &aRbDbFmtFromDb == pButton;
515     aDBColumns[ nFndPos ]->bIsDBFmt = bFromDB;
516     aLbDbFmtFromUsr.Enable( !bFromDB );
517 
518     return 0;
519 }
520 /* ---------------------------------------------------------------------------
521 
522  ---------------------------------------------------------------------------*/
IMPL_LINK(SwInsertDBColAutoPilot,TblToFromHdl,Button *,pButton)523 IMPL_LINK( SwInsertDBColAutoPilot, TblToFromHdl, Button*, pButton )
524 {
525     sal_Bool bChgEnable = sal_True, bEnableTo = sal_True, bEnableFrom = sal_True;
526     aLbTblDbColumn.SetUpdateMode( sal_False );
527     aLbTableCol.SetUpdateMode( sal_False );
528 
529     if( pButton == &aIbDbcolAllTo )
530     {
531         bEnableTo = sal_False;
532 
533         sal_uInt16 n, nInsPos = aLbTableCol.GetSelectEntryPos(),
534                nCnt = aLbTblDbColumn.GetEntryCount();
535         if( LISTBOX_APPEND == nInsPos )
536             for( n = 0; n < nCnt; ++n )
537                 aLbTableCol.InsertEntry( aLbTblDbColumn.GetEntry( n ),
538                                             LISTBOX_APPEND );
539         else
540             for( n = 0; n < nCnt; ++n, ++nInsPos )
541                 aLbTableCol.InsertEntry( aLbTblDbColumn.GetEntry( n ), nInsPos );
542         aLbTblDbColumn.Clear();
543         aLbTableCol.SelectEntryPos( nInsPos );
544         aLbTblDbColumn.SelectEntryPos( LISTBOX_APPEND );
545     }
546     else if( pButton == &aIbDbcolOneTo &&
547             LISTBOX_ENTRY_NOTFOUND != aLbTblDbColumn.GetSelectEntryPos() )
548     {
549         sal_uInt16 nInsPos = aLbTableCol.GetSelectEntryPos(),
550                nDelPos = aLbTblDbColumn.GetSelectEntryPos(),
551                nTopPos = aLbTblDbColumn.GetTopEntry();
552         aLbTableCol.InsertEntry( aLbTblDbColumn.GetEntry( nDelPos ), nInsPos );
553         aLbTblDbColumn.RemoveEntry( nDelPos );
554 
555         aLbTableCol.SelectEntryPos( nInsPos );
556         if( nDelPos >= aLbTblDbColumn.GetEntryCount() )
557             nDelPos = aLbTblDbColumn.GetEntryCount() - 1;
558         aLbTblDbColumn.SelectEntryPos( nDelPos );
559         aLbTblDbColumn.SetTopEntry( nTopPos );
560 
561         bEnableTo = 0 != aLbTblDbColumn.GetEntryCount();
562     }
563     else if( pButton == &aIbDbcolOneFrom )
564     {
565         if( LISTBOX_ENTRY_NOTFOUND != aLbTableCol.GetSelectEntryPos() )
566         {
567             sal_uInt16 nFndPos, nInsPos,
568                     nDelPos = aLbTableCol.GetSelectEntryPos(),
569                     nTopPos = aLbTableCol.GetTopEntry();
570 
571             // die richtige InsertPos suchen!!
572             SwInsDBColumn aSrch( aLbTableCol.GetEntry( nDelPos ), 0 );
573             aDBColumns.Seek_Entry( &aSrch, &nFndPos );
574             if( !nFndPos || nFndPos == aDBColumns.Count()-1 )
575                 nInsPos = nFndPos;
576             else
577             {
578                 nInsPos = LISTBOX_ENTRY_NOTFOUND;
579                 while( ++nFndPos < aDBColumns.Count() &&
580                         LISTBOX_ENTRY_NOTFOUND == (nInsPos = aLbTblDbColumn.
581                         GetEntryPos( String(aDBColumns[ nFndPos ]->sColumn ))) )
582                     ;
583             }
584 
585             aLbTblDbColumn.InsertEntry( aSrch.sColumn, nInsPos );
586             aLbTableCol.RemoveEntry( nDelPos );
587 
588             if( nInsPos >= aLbTblDbColumn.GetEntryCount() )
589                 nInsPos = aLbTblDbColumn.GetEntryCount() - 1;
590             aLbTblDbColumn.SelectEntryPos( nInsPos );
591 
592             if( nDelPos >= aLbTableCol.GetEntryCount() )
593                 nDelPos = aLbTableCol.GetEntryCount() - 1;
594             aLbTableCol.SelectEntryPos( nDelPos );
595             aLbTableCol.SetTopEntry( nTopPos );
596         }
597         else
598             bEnableTo = 0 != aLbTblDbColumn.GetEntryCount();
599 
600         bEnableFrom = 0 != aLbTableCol.GetEntryCount();
601     }
602     else if( pButton == &aIbDbcolAllFrom )
603     {
604         bEnableFrom = sal_False;
605 
606         aLbTblDbColumn.Clear();
607         aLbTableCol.Clear();
608         for( sal_uInt16 n = 0; n < aDBColumns.Count(); ++n )
609             aLbTblDbColumn.InsertEntry( aDBColumns[ n ]->sColumn, n );
610         aLbTblDbColumn.SelectEntryPos( 0 );
611     }
612     else if( pButton == &aIbDbcolToEdit )
613     {
614         bChgEnable = sal_False;
615         // Daten ins Edit moven:
616         String aFld( aLbTxtDbColumn.GetSelectEntry() );
617         if( aFld.Len() )
618         {
619             String aStr( aEdDbText.GetText() );
620             sal_uInt16 nPos = (sal_uInt16)aEdDbText.GetSelection().Min();
621             sal_uInt16 nSel = sal_uInt16(aEdDbText.GetSelection().Max()) - nPos;
622             if( nSel )
623                 // dann loesche erstmal die bestehende Selektion
624                 aStr.Erase( nPos, nSel );
625 
626             aFld.Insert( cDBFldStart, 0 );
627             aFld += cDBFldEnd;
628             if( aStr.Len() )
629             {
630                 if( nPos )                          // ein Space davor
631                 {
632                     sal_Unicode c = aStr.GetChar( nPos-1 );
633                     if( '\n' != c && '\r' != c )
634                         aFld.Insert( ' ', 0 );
635                 }
636                 if( nPos < aStr.Len() )             // ein Space dahinter
637                 {
638                     sal_Unicode c = aStr.GetChar( nPos );
639                     if( '\n' != c && '\r' != c )
640                         aFld += ' ';
641                 }
642             }
643 
644             aStr.Insert( aFld, nPos );
645             aEdDbText.SetText( aStr );
646             nPos = nPos + aFld.Len();
647             aEdDbText.SetSelection( Selection( nPos ));
648         }
649     }
650 
651     if( bChgEnable )
652     {
653         aIbDbcolOneTo.Enable( bEnableTo );
654         aIbDbcolAllTo.Enable( bEnableTo );
655         aIbDbcolOneFrom.Enable( bEnableFrom );
656         aIbDbcolAllFrom.Enable( bEnableFrom );
657 
658         aRbDbFmtFromDb.Enable( sal_False );
659         aRbDbFmtFromUsr.Enable( sal_False );
660         aLbDbFmtFromUsr.Enable( sal_False );
661 
662         aPbTblFormat.Enable( bEnableFrom );
663     }
664     aLbTblDbColumn.SetUpdateMode( sal_True );
665     aLbTableCol.SetUpdateMode( sal_True );
666 
667     return 0;
668 }
669 /* ---------------------------------------------------------------------------
670 
671  ---------------------------------------------------------------------------*/
IMPL_LINK(SwInsertDBColAutoPilot,DblClickHdl,ListBox *,pBox)672 IMPL_LINK( SwInsertDBColAutoPilot, DblClickHdl, ListBox*, pBox )
673 {
674     Button* pButton = 0;
675     if( pBox == &aLbTxtDbColumn )
676         pButton = &aIbDbcolToEdit;
677     else if( pBox == &aLbTblDbColumn && aIbDbcolOneTo.IsEnabled() )
678         pButton = &aIbDbcolOneTo;
679     else if( pBox == &aLbTableCol && aIbDbcolOneFrom.IsEnabled() )
680         pButton = &aIbDbcolOneFrom;
681 
682     if( pButton )
683         TblToFromHdl( pButton );
684 
685     return 0;
686 }
687 /* ---------------------------------------------------------------------------
688 
689  ---------------------------------------------------------------------------*/
IMPL_LINK(SwInsertDBColAutoPilot,TblFmtHdl,PushButton *,pButton)690 IMPL_LINK( SwInsertDBColAutoPilot, TblFmtHdl, PushButton*, pButton )
691 {
692     SwWrtShell& rSh = pView->GetWrtShell();
693     sal_Bool bNewSet = sal_False;
694     if( !pTblSet )
695     {
696         bNewSet = sal_True;
697         pTblSet = new SfxItemSet( rSh.GetAttrPool(), SwuiGetUITableAttrRange() );
698 
699         //Ersteinmal die einfachen Attribute besorgen.
700         pTblSet->Put( SfxStringItem( FN_PARAM_TABLE_NAME, rSh.GetUniqueTblName() ));
701         pTblSet->Put( SfxUInt16Item( FN_PARAM_TABLE_HEADLINE, 1 ) );
702 
703         pTblSet->Put( SfxUInt16Item( SID_BACKGRND_DESTINATION,
704                                     rSh.GetViewOptions()->GetTblDest() ));
705 
706         SvxBrushItem aBrush( RES_BACKGROUND );
707         pTblSet->Put( aBrush );
708         pTblSet->Put( aBrush, SID_ATTR_BRUSH_ROW );
709         pTblSet->Put( aBrush, SID_ATTR_BRUSH_TABLE );
710 
711         SvxBoxInfoItem aBoxInfo( SID_ATTR_BORDER_INNER );
712             // Tabellenvariante, wenn mehrere Tabellenzellen selektiert
713         aBoxInfo.SetTable( sal_True );
714             // Abstandsfeld immer anzeigen
715         aBoxInfo.SetDist( sal_True);
716             // Minimalgroesse in Tabellen und Absaetzen setzen
717         aBoxInfo.SetMinDist( sal_False );
718             // Default-Abstand immer setzen
719         aBoxInfo.SetDefDist( MIN_BORDER_DIST );
720             // Einzelne Linien koennen nur in Tabellen DontCare-Status haben
721         aBoxInfo.SetValid( VALID_DISABLE, sal_True );
722         pTblSet->Put( aBoxInfo );
723 
724         SwGetCurColNumPara aPara;
725         const sal_uInt16 nNum = rSh.GetCurColNum( &aPara );
726         long nWidth;
727 
728         if( nNum )
729         {
730             nWidth = aPara.pPrtRect->Width();
731             const SwFmtCol& rCol = aPara.pFrmFmt->GetCol();
732             const SwColumns& rCols = rCol.GetColumns();
733 
734             //nStart und nEnd initialisieren fuer nNum == 0
735             long nWidth1 = 0,
736                 nStart1 = 0,
737                 nEnd1 = nWidth;
738             for( sal_uInt16 i = 0; i < nNum; ++i )
739             {
740                 SwColumn* pCol = rCols[i];
741                 nStart1 = pCol->GetLeft() + nWidth1;
742                 nWidth1 += (long)rCol.CalcColWidth( i, (sal_uInt16)nWidth );
743                 nEnd1 = nWidth1 - pCol->GetRight();
744             }
745             if(nStart1 || nEnd1 != nWidth)
746                 nWidth = nEnd1 - nStart1;
747         }
748         else
749             nWidth = rSh.GetAnyCurRect(
750                                 FRMTYPE_FLY_ANY & rSh.GetFrmType( 0, sal_True )
751                                               ? RECT_FLY_PRT_EMBEDDED
752                                               : RECT_PAGE_PRT ).Width();
753 
754         SwTabCols aTabCols;
755         aTabCols.SetRight( nWidth );
756         aTabCols.SetRightMax( nWidth );
757         pRep = new SwTableRep( aTabCols, sal_False );
758         pRep->SetAlign( text::HoriOrientation::NONE );
759         pRep->SetSpace( nWidth );
760         pRep->SetWidth( nWidth );
761         pRep->SetWidthPercent( 100 );
762         pTblSet->Put( SwPtrItem( FN_TABLE_REP, pRep ));
763 
764         pTblSet->Put( SfxUInt16Item( SID_HTML_MODE,
765                     ::GetHtmlMode( pView->GetDocShell() )));
766     }
767 
768     if( aLbTableCol.GetEntryCount() != pRep->GetAllColCount() )
769     {
770         // Anzahl der Spalten hat sich geaendert: dann muessen die
771         // TabCols angepasst werden
772         long nWidth = pRep->GetWidth();
773         sal_uInt16 nCols = aLbTableCol.GetEntryCount() - 1;
774         SwTabCols aTabCols( nCols );
775         aTabCols.SetRight( nWidth  );
776         aTabCols.SetRightMax( nWidth );
777         if( nCols )
778             for( sal_uInt16 n = 0, nStep = (sal_uInt16)(nWidth / (nCols+1)), nW = nStep;
779                     n < nCols; ++n, nW = nW + nStep )
780             {
781                 aTabCols.Insert( nW, sal_False, n );
782             }
783         delete pRep;
784         pRep = new SwTableRep( aTabCols, sal_False );
785         pRep->SetAlign( text::HoriOrientation::NONE );
786         pRep->SetSpace( nWidth );
787         pRep->SetWidth( nWidth );
788         pRep->SetWidthPercent( 100 );
789         pTblSet->Put( SwPtrItem( FN_TABLE_REP, pRep ));
790     }
791 
792     SwAbstractDialogFactory* pFact = swui::GetFactory();
793     DBG_ASSERT(pFact, "SwAbstractDialogFactory fail!");
794 
795     SfxAbstractTabDialog* pDlg = pFact->CreateSwTableTabDlg(  pButton, rSh.GetAttrPool(),pTblSet, &rSh, DLG_FORMAT_TABLE );
796     DBG_ASSERT(pDlg, "Dialogdiet fail!");
797     if( RET_OK == pDlg->Execute() )
798         pTblSet->Put( *pDlg->GetOutputItemSet() );
799     else if( bNewSet )
800     {
801         delete pTblSet, pTblSet = 0;
802         delete pRep, pRep = 0;
803     }
804     delete pDlg;
805 
806     return 0;
807 }
808 /* ---------------------------------------------------------------------------
809 
810  ---------------------------------------------------------------------------*/
IMPL_LINK(SwInsertDBColAutoPilot,AutoFmtHdl,PushButton *,pButton)811 IMPL_LINK( SwInsertDBColAutoPilot, AutoFmtHdl, PushButton*, pButton )
812 {
813     SwAbstractDialogFactory* pFact = swui::GetFactory();
814     DBG_ASSERT(pFact, "SwAbstractDialogFactory fail!");
815 
816     AbstractSwAutoFormatDlg* pDlg = pFact->CreateSwAutoFormatDlg(pButton, pView->GetWrtShellPtr(),DLG_AUTOFMT_TABLE, sal_False, pTAutoFmt);
817     DBG_ASSERT(pDlg, "Dialogdiet fail!");
818     if( RET_OK == pDlg->Execute())
819         pDlg->FillAutoFmtOfIndex( pTAutoFmt );
820     delete pDlg;
821     return 0;
822 }
823 /* ---------------------------------------------------------------------------
824 
825  ---------------------------------------------------------------------------*/
IMPL_LINK(SwInsertDBColAutoPilot,SelectHdl,ListBox *,pBox)826 IMPL_LINK( SwInsertDBColAutoPilot, SelectHdl, ListBox*, pBox )
827 {
828     ListBox* pGetBox = pBox == &aLbDbFmtFromUsr
829                             ? ( aRbAsTable.IsChecked()
830                                     ? ( 0 == aLbTableCol.GetEntryData( 0 )
831                                         ? &aLbTblDbColumn
832                                         : &aLbTableCol )
833                                     : &aLbTxtDbColumn )
834                             : pBox;
835 
836     sal_uInt16 nFndPos;
837     SwInsDBColumn aSrch( pGetBox->GetSelectEntry(), 0 );
838     aDBColumns.Seek_Entry( &aSrch, &nFndPos );
839 
840     if( pBox == &aLbDbFmtFromUsr )
841     {
842         if( aSrch.sColumn.getLength() )
843         {
844             aOldNumFmtLnk.Call( pBox );
845             aDBColumns[ nFndPos ]->nUsrNumFmt = aLbDbFmtFromUsr.GetFormat();
846         }
847     }
848     else
849     {
850         // an der FormatGroupBox den ausgewaehlten FeldNamen setzen, damit
851         // klar ist, welches Feld ueber das Format eingestellt wird!
852         String sTxt( aFlFormat.GetText().Copy( 0, nGBFmtLen ));
853         if( !aSrch.sColumn.getLength() )
854         {
855             aRbDbFmtFromDb.Enable( sal_False );
856             aRbDbFmtFromUsr.Enable( sal_False );
857             aLbDbFmtFromUsr.Enable( sal_False );
858         }
859         else
860         {
861             sal_Bool bEnableFmt = aDBColumns[ nFndPos ]->bHasFmt;
862             aRbDbFmtFromDb.Enable( bEnableFmt );
863             aRbDbFmtFromUsr.Enable( bEnableFmt );
864 
865             if( bEnableFmt )
866             {
867                 (( sTxt += C2S(" (" )) += String(aSrch.sColumn) ) += (sal_Unicode)')';
868             }
869 
870             sal_Bool bIsDBFmt = aDBColumns[ nFndPos ]->bIsDBFmt;
871             aRbDbFmtFromDb.Check( bIsDBFmt );
872             aRbDbFmtFromUsr.Check( !bIsDBFmt );
873             aLbDbFmtFromUsr.Enable( !bIsDBFmt );
874             if( !bIsDBFmt )
875                 aLbDbFmtFromUsr.SetDefFormat( aDBColumns[ nFndPos ]->nUsrNumFmt );
876         }
877 
878         aFlFormat.SetText( sTxt );
879 
880         // um spaeter zu wissen, welche ListBox die "aktive" war, wird sich
881         // im 1. Eintrag ein Flag gemerkt,
882         void* pPtr = pBox == &aLbTableCol ? &aLbTableCol : 0;
883         aLbTableCol.SetEntryData( 0, pPtr );
884     }
885     return 0;
886 }
887 /* ---------------------------------------------------------------------------
888 
889  ---------------------------------------------------------------------------*/
IMPL_LINK(SwInsertDBColAutoPilot,HeaderHdl,Button *,pButton)890 IMPL_LINK( SwInsertDBColAutoPilot, HeaderHdl, Button*, pButton )
891 {
892     if( pButton == &aCbTableHeadon )
893     {
894         sal_Bool bEnable = aCbTableHeadon.IsChecked();
895 
896         aRbHeadlColnms.Enable( bEnable );
897         aRbHeadlEmpty.Enable( bEnable );
898     }
899     return 0;
900 }
901 /* ---------------------------------------------------------------------------
902 
903  ---------------------------------------------------------------------------*/
lcl_InsTextInArr(const String & rTxt,_DB_Columns & rColArr)904 static void lcl_InsTextInArr( const String& rTxt, _DB_Columns& rColArr )
905 {
906     _DB_Column* pNew;
907     sal_uInt16 nSttPos = 0, nFndPos;
908     while( STRING_NOTFOUND != ( nFndPos = rTxt.Search( '\x0A', nSttPos )) )
909     {
910         if( 1 < nFndPos )
911         {
912             pNew = new _DB_Column( rTxt.Copy( nSttPos, nFndPos -1 ) );
913             rColArr.Insert( pNew, rColArr.Count() );
914         }
915         pNew = new _DB_Column;
916         rColArr.Insert( pNew, rColArr.Count() );
917         nSttPos = nFndPos + 1;
918     }
919     if( nSttPos < rTxt.Len() )
920     {
921         pNew = new _DB_Column( rTxt.Copy( nSttPos ) );
922         rColArr.Insert( pNew, rColArr.Count() );
923     }
924 }
925 /* ---------------------------------------------------------------------------
926 
927  ---------------------------------------------------------------------------*/
SplitTextToColArr(const String & rTxt,_DB_Columns & rColArr,sal_Bool bInsField)928 sal_Bool SwInsertDBColAutoPilot::SplitTextToColArr( const String& rTxt,
929                                 _DB_Columns& rColArr,
930                                 sal_Bool bInsField )
931 {
932     // aus dem Text wieder die einzelnen Datenbank - Spalten erzeugen
933     // und dann in einem Array speichern
934     // Die Datenbankspalten stehen in <> und muessen im Array der Spalten
935     // vorhanden sein:
936     String sTxt( rTxt );
937     sal_uInt16 nFndPos, nEndPos, nSttPos = 0;
938 
939     while( STRING_NOTFOUND != ( nFndPos = sTxt.Search( cDBFldStart, nSttPos )))
940     {
941         nSttPos = nFndPos + 1;
942         if( STRING_NOTFOUND != ( nEndPos = sTxt.Search( cDBFldEnd, nSttPos+1 )))
943         {
944             // Text in <> geklammert gefunden: was ist es denn:
945             SwInsDBColumn aSrch( sTxt.Copy( nSttPos, nEndPos - nSttPos ), 0);
946             if( aDBColumns.Seek_Entry( &aSrch, &nFndPos ) )
947             {
948                 // das ist ein gueltiges Feld
949                 // also sicher den Text "davor":
950                 const SwInsDBColumn& rFndCol = *aDBColumns[ nFndPos ];
951 
952                 _DB_Column* pNew;
953 
954                 if( 1 < nSttPos )
955                 {
956                     ::lcl_InsTextInArr( sTxt.Copy( 0, nSttPos-1 ), rColArr );
957                     sTxt.Erase( 0, nSttPos-1 );
958                 }
959 
960                 sTxt.Erase( 0, (xub_StrLen)(rFndCol.sColumn.getLength() + 2) );
961                 nSttPos = 0;
962 
963                 sal_uInt16 nSubType = 0;
964                 sal_uLong nFormat;
965                 if( rFndCol.bHasFmt )
966                 {
967                     if( rFndCol.bIsDBFmt )
968                         nFormat =  rFndCol.nDBNumFmt;
969                     else
970                     {
971                         nFormat = rFndCol.nUsrNumFmt;
972                         nSubType = nsSwExtendedSubType::SUB_OWN_FMT;
973                     }
974                 }
975                 else
976                     nFormat = 0;
977 
978                 if( bInsField )
979                 {
980                     SwWrtShell& rSh = pView->GetWrtShell();
981                     SwDBFieldType aFldType( rSh.GetDoc(), aSrch.sColumn,
982                                             aDBData );
983                     pNew = new _DB_Column( rFndCol, *new SwDBField(
984                             (SwDBFieldType*)rSh.InsertFldType( aFldType ),
985                                                             nFormat ) );
986                     if( nSubType )
987                         pNew->DB_ColumnData.pField->SetSubType( nSubType );
988                 }
989                 else
990                     pNew = new _DB_Column( rFndCol, nFormat );
991 
992                 rColArr.Insert( pNew, rColArr.Count() );
993             }
994         }
995     }
996 
997     // den letzten Text nicht vergessen
998     if( sTxt.Len() )
999         ::lcl_InsTextInArr( sTxt, rColArr );
1000 
1001     return 0 != rColArr.Count();
1002 }
1003 /* ---------------------------------------------------------------------------
1004 
1005  ---------------------------------------------------------------------------*/
DataToDoc(const Sequence<Any> & rSelection,Reference<XDataSource> xSource,Reference<XConnection> xConnection,Reference<sdbc::XResultSet> xResultSet)1006 void SwInsertDBColAutoPilot::DataToDoc( const Sequence<Any>& rSelection,
1007     Reference< XDataSource> xSource,
1008     Reference< XConnection> xConnection,
1009     Reference< sdbc::XResultSet > xResultSet )
1010 {
1011     const Any* pSelection = rSelection.getLength() ? rSelection.getConstArray() : 0;
1012     SwWrtShell& rSh = pView->GetWrtShell();
1013 
1014     //with the drag and drop interface no result set is initially available
1015     sal_Bool bDisposeResultSet = sal_False;
1016     // we don't have a cursor, so we have to create our own RowSet
1017     if ( !xResultSet.is() )
1018     {
1019         xResultSet = SwNewDBMgr::createCursor(aDBData.sDataSource,aDBData.sCommand,aDBData.nCommandType,xConnection);
1020         bDisposeResultSet = xResultSet.is();
1021     }
1022 
1023     Reference< sdbc::XRow > xRow(xResultSet, UNO_QUERY);
1024     if ( !xRow.is() )
1025         return;
1026 
1027     rSh.StartAllAction();
1028     sal_Bool bUndo = rSh.DoesUndo();
1029     if( bUndo )
1030         rSh.StartUndo( UNDO_EMPTY );
1031 
1032     sal_Bool bAsTable = aRbAsTable.IsChecked();
1033     SvNumberFormatter& rNumFmtr = *rSh.GetNumberFormatter();
1034 
1035     if( rSh.HasSelection() )
1036         rSh.DelRight();
1037 
1038     ::std::auto_ptr<SwWait> pWait;
1039 
1040     Reference< XColumnsSupplier > xColsSupp( xResultSet, UNO_QUERY );
1041     Reference <XNameAccess> xCols = xColsSupp->getColumns();
1042 
1043     do{                                 // middle checked loop!!
1044     if( bAsTable )          // Daten als Tabelle einfuegen
1045     {
1046         rSh.DoUndo( sal_False );
1047 
1048         sal_uInt16 n, nRows = 0, nCols = aLbTableCol.GetEntryCount();
1049         if( aCbTableHeadon.IsChecked() )
1050             nRows++;
1051 
1052         if( pSelection )
1053             nRows = nRows + (sal_uInt16)rSelection.getLength();
1054         else
1055             ++nRows;
1056 
1057         // bereite das Array fuer die ausgewaehlten Spalten auf
1058         SwInsDBColumns_SAR aColFlds( 255 >= nCols ? (sal_uInt8)nCols : 255, 5 );
1059         for( n = 0; n < nCols; ++n )
1060         {
1061             sal_uInt16 nFndPos;
1062             SwInsDBColumn aSrch( aLbTableCol.GetEntry( n ), 0 );
1063             if( aDBColumns.Seek_Entry( &aSrch, &nFndPos ) )
1064                 aColFlds.Insert( aDBColumns[ nFndPos ], n );
1065             else {
1066                 ASSERT( !this, "Datenbankspalte nicht mehr gefunden" );
1067             }
1068         }
1069 
1070         if( nCols != aColFlds.Count() )
1071         {
1072             ASSERT( !this, "nicht alle Datenbankspalten gefunden" );
1073             nCols = aColFlds.Count();
1074         }
1075 
1076         if(!nRows || !nCols)
1077         {
1078             ASSERT( !this, "wrong parameters" );
1079             break;
1080         }
1081 
1082         const SwModuleOptions* pModOpt = SW_MOD()->GetModuleConfig();
1083 
1084         sal_Bool bHTML = 0 != (::GetHtmlMode( pView->GetDocShell() ) & HTMLMODE_ON);
1085         rSh.InsertTable(
1086             pModOpt->GetInsTblFlags(bHTML),
1087             nRows, nCols, text::HoriOrientation::FULL, (pSelection ? pTAutoFmt : 0) );
1088         rSh.MoveTable( GetfnTablePrev(), GetfnTableStart() );
1089 
1090         if( pSelection && pTblSet )
1091             SetTabSet();
1092 
1093         SfxItemSet aTblSet( rSh.GetAttrPool(), RES_BOXATR_FORMAT,
1094                                                 RES_BOXATR_VALUE );
1095         sal_Bool bIsAutoUpdateCells = rSh.IsAutoUpdateCells();
1096         rSh.SetAutoUpdateCells( sal_False );
1097 
1098 
1099         if( aCbTableHeadon.IsChecked() )
1100         {
1101             for( n = 0; n < nCols; ++n )
1102             {
1103                 if( aRbHeadlColnms.IsChecked() )
1104                 {
1105                     rSh.SwEditShell::Insert2( aColFlds[ n ]->sColumn );
1106                 }
1107                 rSh.GoNextCell();
1108             }
1109         }
1110         else
1111             rSh.SetRowsToRepeat( 0 );
1112 
1113         for( sal_Int32 i = 0 ; ; ++i )
1114         {
1115             sal_Bool bBreak = sal_False;
1116             try
1117             {
1118                 if(pSelection)
1119                 {
1120                     sal_Int32 nPos = 0;
1121                     pSelection[i] >>= nPos;
1122                     bBreak = !xResultSet->absolute(nPos);
1123                 }
1124                 else if(!i)
1125                     bBreak = !xResultSet->first();
1126             }
1127             catch(const Exception& )
1128             {
1129                 bBreak = sal_True;
1130             }
1131             if(bBreak)
1132                 break;
1133 
1134             for( n = 0; n < nCols; ++n )
1135             {
1136                 // beim aller erstenmal KEIN GoNextCell, weil wir schon
1137                 // drin stehen. Auch nicht nach dem Insert das GoNextCell,
1138                 // weil am Ende eine leere Zeile einfuegt wird.
1139                 if( i || n )
1140                     rSh.GoNextCell();
1141 
1142                 const SwInsDBColumn* pEntry = aColFlds[ n ];
1143 
1144                 Reference< XColumn > xColumn;
1145                 xCols->getByName(pEntry->sColumn) >>= xColumn;
1146                 Reference< XPropertySet > xColumnProps( xColumn, UNO_QUERY );
1147                 sal_Int32 eDataType = 0;
1148                 if( xColumnProps.is() )
1149                 {
1150                     Any aType = xColumnProps->getPropertyValue(C2U("Type"));
1151                     aType >>= eDataType;
1152                 }
1153                 try
1154                 {
1155                     if( pEntry->bHasFmt )
1156                     {
1157                         SwTblBoxNumFormat aNumFmt(
1158                                         pEntry->bIsDBFmt ? pEntry->nDBNumFmt
1159                                                         : pEntry->nUsrNumFmt );
1160                         aTblSet.Put(aNumFmt);
1161                         if( xColumn.is() )
1162                         {
1163                             double fVal = xColumn->getDouble();
1164                             if( xColumn->wasNull() )
1165                                 aTblSet.ClearItem( RES_BOXATR_VALUE );
1166                             else
1167                             {
1168                                 if(rNumFmtr.GetType(aNumFmt.GetValue()) & NUMBERFORMAT_DATE)
1169                                 {
1170                                     ::Date aStandard(1,1,1900);
1171                                     if (*rNumFmtr.GetNullDate() != aStandard)
1172                                         fVal += (aStandard - *rNumFmtr.GetNullDate());
1173                                 }
1174                                 aTblSet.Put( SwTblBoxValue( fVal ));
1175                             }
1176                         }
1177                         else
1178                             aTblSet.ClearItem( RES_BOXATR_VALUE );
1179                         rSh.SetTblBoxFormulaAttrs( aTblSet );
1180                     }
1181                     //#i60207# don't insert binary data as string - creates a loop
1182                     else if( DataType::BINARY       == eDataType ||
1183                              DataType::VARBINARY    == eDataType ||
1184                              DataType::LONGVARBINARY== eDataType ||
1185                              DataType::SQLNULL      == eDataType ||
1186                              DataType::OTHER        == eDataType ||
1187                              DataType::OBJECT       == eDataType ||
1188                              DataType::DISTINCT     == eDataType ||
1189                              DataType::STRUCT       == eDataType ||
1190                              DataType::ARRAY        == eDataType ||
1191                              DataType::BLOB         == eDataType ||
1192                              DataType::CLOB         == eDataType ||
1193                              DataType::REF          == eDataType
1194                              )
1195                     {
1196                         // do nothing
1197                     }
1198                     else
1199                     {
1200                         rtl::OUString sVal =  xColumn->getString();
1201                         if(!xColumn->wasNull())
1202                         {
1203                             rSh.SwEditShell::Insert2( sVal );
1204                         }
1205                     }
1206                 }
1207                 catch(Exception&
1208 #ifdef DBG_UTIL
1209                             aExcept
1210 #endif
1211                 )
1212                 {
1213                     DBG_ERROR(ByteString(String(aExcept.Message), gsl_getSystemTextEncoding()).GetBuffer());
1214                 }
1215             }
1216 
1217             if( !pSelection )
1218             {
1219                 if ( !xResultSet->next() )
1220                     break;
1221             }
1222             else if( i+1 >= rSelection.getLength() )
1223                 break;
1224 
1225             if( 10 == i )
1226                 pWait = ::std::auto_ptr<SwWait>(new SwWait( *pView->GetDocShell(), true ));
1227         }
1228 
1229         rSh.MoveTable( GetfnTableCurr(), GetfnTableStart() );
1230         if( !pSelection && ( pTblSet || pTAutoFmt ))
1231         {
1232             if( pTblSet )
1233                 SetTabSet();
1234 
1235             if( pTAutoFmt )
1236                 rSh.SetTableAutoFmt( *pTAutoFmt );
1237         }
1238         rSh.SetAutoUpdateCells( bIsAutoUpdateCells );
1239     }
1240     else                            // Daten als Felder/Text einfuegen
1241     {
1242         _DB_Columns aColArr;
1243         if( SplitTextToColArr( aEdDbText.GetText(), aColArr, aRbAsField.IsChecked() ) )
1244         {
1245             // jetzt kann bei jedem Datensatz einfach ueber das Array iteriert
1246             // und die Daten eingefuegt werden
1247 
1248             if( !rSh.IsSttPara() )
1249                 rSh.SwEditShell::SplitNode();
1250             if( !rSh.IsEndPara() )
1251             {
1252                 rSh.SwEditShell::SplitNode();
1253                 rSh.SwCrsrShell::Left(1,CRSR_SKIP_CHARS);
1254             }
1255 
1256             rSh.DoUndo( sal_False );
1257 
1258             SwTxtFmtColl* pColl = 0;
1259             {
1260                 String sTmplNm( aLbDbParaColl.GetSelectEntry() );
1261                 if( sNoTmpl != sTmplNm )
1262                 {
1263                     pColl = rSh.FindTxtFmtCollByName( sTmplNm );
1264                     if( !pColl )
1265                     {
1266                         sal_uInt16 nId = SwStyleNameMapper::GetPoolIdFromUIName( sTmplNm, nsSwGetPoolIdFromName::GET_POOLID_TXTCOLL );
1267                         if( USHRT_MAX != nId )
1268                             pColl = rSh.GetTxtCollFromPool( nId );
1269                         else
1270                             pColl = rSh.MakeTxtFmtColl( sTmplNm );
1271                     }
1272                     rSh.SetTxtFmtColl( pColl );
1273                 }
1274             }
1275 
1276             // fuers Einfuegen als Felder -> nach jedem Datensatz ein
1277             // "NextField" einfuegen
1278             SwDBFormatData aDBFormatData;
1279             Reference< XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
1280             if( xMgr.is() )
1281             {
1282                 Reference<XInterface> xInstance = xMgr->createInstance( C2U( "com.sun.star.util.NumberFormatter" ));
1283                 aDBFormatData.xFormatter = Reference<util::XNumberFormatter>(xInstance, UNO_QUERY) ;
1284             }
1285 
1286             Reference<XPropertySet> xSourceProps(xSource, UNO_QUERY);
1287             if(xSourceProps.is())
1288             {
1289               Any aFormats = xSourceProps->getPropertyValue(C2U("NumberFormatsSupplier"));
1290               if(aFormats.hasValue())
1291               {
1292                   Reference< util::XNumberFormatsSupplier> xSuppl;
1293                   aFormats >>= xSuppl;
1294                   if(xSuppl.is())
1295                   {
1296                         Reference< XPropertySet > xSettings = xSuppl->getNumberFormatSettings();
1297                         Any aNull = xSettings->getPropertyValue(C2U("NullDate"));
1298                         aNull >>= aDBFormatData.aNullDate;
1299                         if(aDBFormatData.xFormatter.is())
1300                             aDBFormatData.xFormatter->attachNumberFormatsSupplier(xSuppl);
1301                   }
1302               }
1303             }
1304             aDBFormatData.aLocale = SvxCreateLocale( rSh.GetCurLang() );
1305             SwDBNextSetField aNxtDBFld( (SwDBNextSetFieldType*)rSh.
1306                                         GetFldType( 0, RES_DBNEXTSETFLD ),
1307                                         C2S("1"), aEmptyStr, aDBData );
1308 
1309 
1310             sal_Bool bSetCrsr = sal_True;
1311             sal_uInt16 n = 0, nCols = aColArr.Count();
1312             ::sw::mark::IMark* pMark = NULL;
1313             for( sal_Int32 i = 0 ; ; ++i )
1314             {
1315                 sal_Bool bBreak = sal_False;
1316                 try
1317                 {
1318                     if(pSelection)
1319                     {
1320                         sal_Int32 nPos = 0;
1321                         pSelection[i] >>= nPos;
1322                         bBreak = !xResultSet->absolute(nPos);
1323                     }
1324                     else if(!i)
1325                         bBreak = !xResultSet->first();
1326                 }
1327                 catch(Exception&)
1328                 {
1329                     bBreak = sal_True;
1330                 }
1331 
1332                 if(bBreak)
1333                     break;
1334 
1335 
1336                 for( n = 0; n < nCols; ++n )
1337                 {
1338                     _DB_Column* pDBCol = aColArr[ n ];
1339                     String sIns;
1340                     switch( pDBCol->eColType )
1341                     {
1342                     case _DB_Column::DB_FILLTEXT:
1343                         sIns =  *pDBCol->DB_ColumnData.pText;
1344                         break;
1345 
1346                     case _DB_Column::DB_SPLITPARA:
1347                         rSh.SplitNode();
1348                         // wenn nicht die gleiche Vorlage die Follow Vorlage
1349                         // ist, dann muss die ausgewaehlte neu gesetzt werden
1350                         if( pColl && &pColl->GetNextTxtFmtColl() != pColl )
1351                             rSh.SetTxtFmtColl( pColl );
1352                         break;
1353 
1354                     case _DB_Column::DB_COL_FIELD:
1355                         {
1356                             SwDBField *const pFld = static_cast<SwDBField *>(
1357                                 pDBCol->DB_ColumnData.pField->CopyField());
1358                             double nValue = DBL_MAX;
1359 
1360                             Reference< XPropertySet > xColumnProps;
1361                             xCols->getByName(pDBCol->pColInfo->sColumn) >>= xColumnProps;
1362 
1363                             pFld->SetExpansion( SwNewDBMgr::GetDBField(
1364                                                 xColumnProps,
1365                                                 aDBFormatData,
1366                                                 &nValue ) );
1367                             if( DBL_MAX != nValue )
1368                             {
1369                                 Any aType = xColumnProps->getPropertyValue(C2U("Type"));
1370                                 sal_Int32 eDataType = 0;
1371                                 aType >>= eDataType;
1372                                 if( DataType::DATE == eDataType  || DataType::TIME == eDataType  ||
1373                                     DataType::TIMESTAMP  == eDataType)
1374 
1375                                 {
1376                                     ::Date aStandard(1,1,1900);
1377                                     ::Date aCompare(aDBFormatData.aNullDate.Day ,
1378                                                     aDBFormatData.aNullDate.Month,
1379                                                     aDBFormatData.aNullDate.Year);
1380                                     if(aStandard != aCompare)
1381                                         nValue += (aStandard - aCompare);
1382                                 }
1383                                 pFld->ChgValue( nValue, sal_True );
1384                             }
1385                             pFld->SetInitialized();
1386 
1387                             rSh.Insert( *pFld );
1388                             delete pFld;
1389                         }
1390                         break;
1391 
1392                     case _DB_Column::DB_COL_TEXT:
1393                         {
1394                             double nValue = DBL_MAX;
1395                             Reference< XPropertySet > xColumnProps;
1396                             xCols->getByName(pDBCol->pColInfo->sColumn) >>= xColumnProps;
1397                             sIns = SwNewDBMgr::GetDBField(
1398                                                 xColumnProps,
1399                                                 aDBFormatData,
1400                                                 &nValue );
1401                             if( pDBCol->DB_ColumnData.nFormat &&
1402                                 DBL_MAX != nValue )
1403                             {
1404                                 Color* pCol;
1405                                 if(rNumFmtr.GetType(pDBCol->DB_ColumnData.nFormat) & NUMBERFORMAT_DATE)
1406                                 {
1407                                     ::Date aStandard(1,1,1900);
1408                                     if (*rNumFmtr.GetNullDate() != aStandard)
1409                                         nValue += (aStandard - *rNumFmtr.GetNullDate());
1410                                 }
1411                                 rNumFmtr.GetOutputString( nValue,
1412                                             pDBCol->DB_ColumnData.nFormat,
1413                                             sIns, &pCol );
1414                             }
1415                         }
1416                         break;
1417                     }
1418 
1419                     if( sIns.Len() )
1420                         rSh.Insert( sIns );
1421 
1422                     if( bSetCrsr && sIns.Len() )
1423                     {
1424                         // zum Anfang und eine Mark setzen, damit der
1425                         // Cursor am Ende wieder auf Anfangsposition
1426                         // gesetzt werden kann.
1427 
1428                         // rSh.SwCrsrShell::MovePara( fnParaCurr, fnParaStart );
1429                         rSh.SwCrsrShell::MovePara(
1430                             GetfnParaCurr(), GetfnParaStart() );
1431                         pMark = rSh.SetBookmark(
1432                             KeyCode(),
1433                             ::rtl::OUString(),
1434                             ::rtl::OUString(), IDocumentMarkAccess::UNO_BOOKMARK );
1435                         // rSh.SwCrsrShell::MovePara( fnParaCurr, fnParaEnd );
1436                         rSh.SwCrsrShell::MovePara(
1437                             GetfnParaCurr(), GetfnParaEnd() );
1438                         bSetCrsr = sal_False;
1439                     }
1440                 }
1441 
1442                 if( !pSelection )
1443                 {
1444                     sal_Bool bNext = xResultSet->next();
1445                     if(!bNext)
1446                         break;
1447                 }
1448                 else if( i+1 >= rSelection.getLength() )
1449                     break;
1450 
1451                 if( aRbAsField.IsChecked() )
1452                     rSh.Insert( aNxtDBFld );
1453 
1454                 if( !rSh.IsSttPara() )
1455                     rSh.SwEditShell::SplitNode();
1456 
1457                 if( 10 == i )
1458                     pWait = ::std::auto_ptr<SwWait>(new SwWait( *pView->GetDocShell(), true ));
1459             }
1460 
1461             if( !bSetCrsr && pMark != NULL)
1462             {
1463                 rSh.SetMark();
1464                 rSh.GotoMark( pMark );
1465                 rSh.getIDocumentMarkAccess()->deleteMark( pMark );
1466                 break;
1467             }
1468         }
1469     }
1470     // write configuration
1471     Commit();
1472     }while( sal_False );                    // middle checked loop
1473 
1474     if( bUndo )
1475     {
1476         rSh.DoUndo( sal_True );
1477         rSh.AppendUndoForInsertFromDB( bAsTable );
1478         rSh.EndUndo( UNDO_EMPTY );
1479     }
1480     rSh.ClearMark();
1481     rSh.EndAllAction();
1482 
1483     if ( bDisposeResultSet )
1484         ::comphelper::disposeComponent(xResultSet);
1485 }
SetTabSet()1486 void SwInsertDBColAutoPilot::SetTabSet()
1487 {
1488     SwWrtShell& rSh = pView->GetWrtShell();
1489     const SfxPoolItem* pItem;
1490 
1491     if( pTAutoFmt )
1492     {
1493         if( pTAutoFmt->IsFrame() )
1494         {
1495             // Umrandung kommt vom AutoFormat
1496             pTblSet->ClearItem( RES_BOX );
1497             pTblSet->ClearItem( SID_ATTR_BORDER_INNER );
1498         }
1499         if( pTAutoFmt->IsBackground() )
1500         {
1501             pTblSet->ClearItem( RES_BACKGROUND );
1502             pTblSet->ClearItem( SID_ATTR_BRUSH_ROW );
1503             pTblSet->ClearItem( SID_ATTR_BRUSH_TABLE );
1504         }
1505     }
1506     else
1507     {
1508         // die Defaults wieder entfernen, es macht keinen Sinn sie zu setzen
1509         SvxBrushItem aBrush( RES_BACKGROUND );
1510         static sal_uInt16 __READONLY_DATA aIds[3] =
1511             { RES_BACKGROUND, SID_ATTR_BRUSH_ROW, SID_ATTR_BRUSH_TABLE };
1512         for( int i = 0; i < 3; ++i )
1513             if( SFX_ITEM_SET == pTblSet->GetItemState( aIds[ i ],
1514                 sal_False, &pItem ) && *pItem == aBrush )
1515                 pTblSet->ClearItem( aIds[ i ] );
1516     }
1517 
1518     if( SFX_ITEM_SET == pTblSet->GetItemState( FN_PARAM_TABLE_NAME, sal_False,
1519         &pItem ) && ((const SfxStringItem*)pItem)->GetValue() ==
1520                     rSh.GetTableFmt()->GetName() )
1521         pTblSet->ClearItem( FN_PARAM_TABLE_NAME );
1522 
1523     rSh.MoveTable( GetfnTableCurr(), GetfnTableStart() );
1524     rSh.SetMark();
1525     rSh.MoveTable( GetfnTableCurr(), GetfnTableEnd() );
1526 
1527     ItemSetToTableParam( *pTblSet, rSh );
1528 
1529     rSh.ClearMark();
1530     rSh.MoveTable( GetfnTableCurr(), GetfnTableStart() );
1531 }
1532 
1533 /*  */
1534 
1535 
~_DB_ColumnConfigData()1536 _DB_ColumnConfigData::~_DB_ColumnConfigData() {}
1537 
1538 /* -----------------------------05.12.00 16:15--------------------------------
1539 
1540  ---------------------------------------------------------------------------*/
lcl_createSourceNames(const String & rNodeName)1541 static Sequence<rtl::OUString> lcl_createSourceNames(const String& rNodeName)
1542 {
1543     Sequence<rtl::OUString> aSourceNames(11);
1544     rtl::OUString* pNames = aSourceNames.getArray();
1545 
1546     String sTmp( rNodeName );
1547     const xub_StrLen nPos = sTmp.Len();
1548     pNames[0] = sTmp.ReplaceAscii( nPos, STRING_MAXLEN,
1549                             RTL_CONSTASCII_STRINGPARAM( "/DataSource" ));
1550     pNames[1] = sTmp.ReplaceAscii( nPos, STRING_MAXLEN,
1551                             RTL_CONSTASCII_STRINGPARAM( "/Command" ));
1552     pNames[2] = sTmp.ReplaceAscii( nPos, STRING_MAXLEN,
1553                             RTL_CONSTASCII_STRINGPARAM( "/CommandType" ));
1554     pNames[3] = sTmp.ReplaceAscii( nPos, STRING_MAXLEN,
1555                             RTL_CONSTASCII_STRINGPARAM( "/ColumnsToText" ));
1556     pNames[4] = sTmp.ReplaceAscii( nPos, STRING_MAXLEN,
1557                             RTL_CONSTASCII_STRINGPARAM( "/ColumnsToTable" ));
1558     pNames[5] = sTmp.ReplaceAscii( nPos, STRING_MAXLEN,
1559                             RTL_CONSTASCII_STRINGPARAM( "/ParaStyle" ));
1560     pNames[6] = sTmp.ReplaceAscii( nPos, STRING_MAXLEN,
1561                             RTL_CONSTASCII_STRINGPARAM( "/TableAutoFormat" ));
1562     pNames[7] = sTmp.ReplaceAscii( nPos, STRING_MAXLEN,
1563                             RTL_CONSTASCII_STRINGPARAM( "/IsTable" ));
1564     pNames[8] = sTmp.ReplaceAscii( nPos, STRING_MAXLEN,
1565                             RTL_CONSTASCII_STRINGPARAM( "/IsField" ));
1566     pNames[9] = sTmp.ReplaceAscii( nPos, STRING_MAXLEN,
1567                             RTL_CONSTASCII_STRINGPARAM( "/IsHeadlineOn" ));
1568     pNames[10] = sTmp.ReplaceAscii( nPos, STRING_MAXLEN,
1569                             RTL_CONSTASCII_STRINGPARAM( "/IsEmptyHeadline" ));
1570     return aSourceNames;
1571 }
1572 /* -----------------------------05.12.00 16:25--------------------------------
1573 
1574  ---------------------------------------------------------------------------*/
lcl_CreateSubNames(const String & rSubNodeName)1575 static Sequence<rtl::OUString> lcl_CreateSubNames( const String& rSubNodeName )
1576 {
1577     Sequence<rtl::OUString> aSubSourceNames(6);
1578     rtl::OUString* pNames = aSubSourceNames.getArray();
1579     String sTmp( rSubNodeName );
1580     const xub_StrLen nPos = sTmp.Len();
1581     pNames[0] = sTmp.ReplaceAscii( nPos, STRING_MAXLEN,
1582                             RTL_CONSTASCII_STRINGPARAM( "/ColumnName" ));
1583     pNames[1] = sTmp.ReplaceAscii( nPos, STRING_MAXLEN,
1584                             RTL_CONSTASCII_STRINGPARAM( "/ColumnIndex" ));
1585     pNames[2] = sTmp.ReplaceAscii( nPos, STRING_MAXLEN,
1586                             RTL_CONSTASCII_STRINGPARAM( "/IsNumberFormat" ));
1587     pNames[3] = sTmp.ReplaceAscii( nPos, STRING_MAXLEN,
1588                             RTL_CONSTASCII_STRINGPARAM( "/IsNumberFormatFromDataBase" ));
1589     pNames[4] = sTmp.ReplaceAscii( nPos, STRING_MAXLEN,
1590                             RTL_CONSTASCII_STRINGPARAM( "/NumberFormat" ));
1591     pNames[5] = sTmp.ReplaceAscii( nPos, STRING_MAXLEN,
1592                             RTL_CONSTASCII_STRINGPARAM( "/NumberFormatLocale" ));
1593     return aSubSourceNames;
1594 }
1595 /* -----------------------------06.12.00 13:03--------------------------------
1596 
1597  ---------------------------------------------------------------------------*/
lcl_CreateUniqueName(const Sequence<rtl::OUString> & aNames)1598 static rtl::OUString lcl_CreateUniqueName(const Sequence<rtl::OUString>& aNames)
1599 {
1600     sal_Int32 nIdx = aNames.getLength();
1601     const rtl::OUString* pNames = aNames.getConstArray();
1602     rtl::OUString sTest(C2U("_"));
1603     rtl::OUString sRet;
1604     while(sal_True)
1605     {
1606         sRet = sTest; sRet += rtl::OUString::valueOf(nIdx++);
1607         sal_Bool bFound = sal_False;
1608         for(sal_Int32 i = 0; i < aNames.getLength(); i++)
1609         {
1610             if(pNames[i] == sRet)
1611             {
1612                 bFound = sal_True;
1613                 break;
1614             }
1615         }
1616         if(!bFound)
1617             break;
1618     }
1619     return sRet;
1620 }
1621 /* -----------------------------05.12.00 15:00--------------------------------
1622 
1623  ---------------------------------------------------------------------------*/
Notify(const::com::sun::star::uno::Sequence<rtl::OUString> &)1624 void SwInsertDBColAutoPilot::Notify( const ::com::sun::star::uno::Sequence< rtl::OUString >&  ) {}
1625 
Commit()1626 void SwInsertDBColAutoPilot::Commit()
1627 {
1628     Sequence <rtl::OUString> aNames = GetNodeNames(rtl::OUString());
1629     const rtl::OUString* pNames = aNames.getArray();
1630     //remove entries that contain this data source + table at first
1631     for(sal_Int32 nNode = 0; nNode < aNames.getLength(); nNode++)
1632     {
1633         Sequence<rtl::OUString> aSourceNames(2);
1634         rtl::OUString* pSourceNames = aSourceNames.getArray();
1635         pSourceNames[0] = pNames[nNode];
1636         pSourceNames[0] += C2U("/DataSource");
1637         pSourceNames[1] = pNames[nNode];
1638         pSourceNames[1] += C2U("/Command");
1639         Sequence<Any> aSourceProperties = GetProperties(aSourceNames);
1640         const Any* pSourceProps = aSourceProperties.getArray();
1641         rtl::OUString sSource, sCommand;
1642         pSourceProps[0] >>= sSource;
1643         pSourceProps[1] >>= sCommand;
1644         if(sSource.equals(aDBData.sDataSource) && sCommand.equals(aDBData.sCommand))
1645         {
1646             Sequence<rtl::OUString> aElements(1);
1647             aElements.getArray()[0] = pNames[nNode];
1648             ClearNodeElements(rtl::OUString(), aElements);
1649         }
1650     }
1651 
1652     aNames = GetNodeNames(rtl::OUString());
1653     rtl::OUString sNewNode = lcl_CreateUniqueName(aNames);
1654     Sequence<rtl::OUString> aNodeNames = lcl_createSourceNames(sNewNode);
1655     Sequence<PropertyValue> aValues(aNodeNames.getLength());
1656     PropertyValue* pValues = aValues.getArray();
1657     const rtl::OUString* pNodeNames = aNodeNames.getConstArray();
1658     rtl::OUString sSlash(C2U("/"));
1659     for(sal_Int32 i = 0; i < aNodeNames.getLength(); i++)
1660     {
1661         pValues[i].Name = sSlash;
1662         pValues[i].Name += pNodeNames[i];
1663     }
1664 
1665     pValues[0].Value <<= rtl::OUString(aDBData.sDataSource);
1666     pValues[1].Value <<= rtl::OUString(aDBData.sCommand);
1667     pValues[2].Value <<= aDBData.nCommandType;
1668     pValues[3].Value <<= rtl::OUString(aEdDbText.GetText());
1669 
1670     String sTmp;
1671     for( sal_uInt16 n = 0, nCnt = aLbTableCol.GetEntryCount(); n < nCnt; ++n )
1672         ( sTmp += aLbTableCol.GetEntry( n ) ) += '\x0a';
1673 
1674     if( sTmp.Len() )
1675         pValues[4].Value <<= rtl::OUString(sTmp);
1676 
1677     if( sNoTmpl != (sTmp = aLbDbParaColl.GetSelectEntry()) )
1678         pValues[5].Value <<= rtl::OUString(sTmp);
1679 
1680     if( pTAutoFmt )
1681         pValues[6].Value <<= rtl::OUString(pTAutoFmt->GetName());
1682 
1683     const Type& rBoolType = ::getBooleanCppuType();
1684     sal_Bool bTmp = aRbAsTable.IsChecked();
1685     pValues[7].Value.setValue(&bTmp, rBoolType);
1686 
1687     bTmp = aRbAsField.IsChecked();
1688     pValues[8].Value.setValue(&bTmp, rBoolType);
1689 
1690     bTmp = aCbTableHeadon.IsChecked();
1691     pValues[9].Value.setValue(&bTmp, rBoolType);
1692 
1693     bTmp = aRbHeadlEmpty.IsChecked();
1694     pValues[10].Value.setValue(&bTmp, rBoolType);
1695 
1696     SetSetProperties(rtl::OUString(), aValues);
1697 
1698     sNewNode += C2U("/ColumnSet");
1699     String sDelim( String::CreateFromAscii( "/__" ));
1700 
1701     LanguageType ePrevLang = (LanguageType)-1;
1702     rtl::OUString sPrevLang;
1703 
1704     SvNumberFormatter& rNFmtr = *pView->GetWrtShell().GetNumberFormatter();
1705     for(sal_uInt16 nCol = 0; nCol < aDBColumns.Count(); nCol++)
1706     {
1707         rtl::OUString sColumnNode = sNewNode;
1708         SwInsDBColumn* pColumn = aDBColumns[nCol];
1709         String sColumnInsertNode(sColumnNode);
1710         sColumnInsertNode += sDelim;
1711         if( nCol < 100 )
1712             sColumnInsertNode += '0';
1713         if( nCol < 10 )
1714             sColumnInsertNode += '0';
1715         sColumnInsertNode += String::CreateFromInt32(  nCol );
1716 
1717         Sequence <rtl::OUString> aSubNodeNames = lcl_CreateSubNames(sColumnInsertNode);
1718         Sequence<PropertyValue> aSubValues(aSubNodeNames.getLength());
1719         PropertyValue* pSubValues = aSubValues.getArray();
1720         const rtl::OUString* pSubNodeNames = aSubNodeNames.getConstArray();
1721         sal_Int32 i;
1722 
1723         for( i = 0; i < aSubNodeNames.getLength(); i++)
1724             pSubValues[i].Name = pSubNodeNames[i];
1725         pSubValues[0].Value <<= pColumn->sColumn;
1726         pSubValues[1].Value <<= i;
1727 
1728         sal_Bool bVal = pColumn->bHasFmt;
1729         pSubValues[2].Value.setValue(&bVal, rBoolType);
1730         bVal = pColumn->bIsDBFmt;
1731         pSubValues[3].Value.setValue(&bVal, rBoolType);
1732 
1733         SwStyleNameMapper::FillUIName( RES_POOLCOLL_STANDARD, sTmp );
1734         const SvNumberformat* pNF = rNFmtr.GetEntry( pColumn->nUsrNumFmt );
1735         LanguageType eLang;
1736         if( pNF )
1737         {
1738             pSubValues[4].Value <<= rtl::OUString(pNF->GetFormatstring());
1739             eLang = pNF->GetLanguage();
1740         }
1741         else
1742         {
1743             pSubValues[4].Value <<= rtl::OUString(sTmp);
1744             eLang = (LanguageType)GetAppLanguage();
1745         }
1746 
1747         if( eLang != ePrevLang )
1748         {
1749             Locale aLocale;
1750             aLocale = SvxLanguageToLocale( aLocale, eLang );
1751             (( sPrevLang = aLocale.Country ) += rtl::OUString( '-' )) += aLocale.Language;
1752             ePrevLang = eLang;
1753         }
1754 
1755         pSubValues[5].Value <<=  sPrevLang;
1756         SetSetProperties(sColumnNode, aSubValues);
1757     }
1758 }
1759 /* -----------------------------05.12.00 15:00--------------------------------
1760 
1761  ---------------------------------------------------------------------------*/
Load()1762 void SwInsertDBColAutoPilot::Load()
1763 {
1764     Sequence <rtl::OUString> aNames = GetNodeNames(rtl::OUString());
1765     const rtl::OUString* pNames = aNames.getArray();
1766     SvNumberFormatter& rNFmtr = *pView->GetWrtShell().GetNumberFormatter();
1767     for(sal_Int32 nNode = 0; nNode < aNames.getLength(); nNode++)
1768     {
1769         //search for entries with the appropriate data source and table
1770         Sequence<rtl::OUString> aSourceNames = lcl_createSourceNames(pNames[nNode]);
1771 
1772         Sequence< Any> aDataSourceProps = GetProperties(aSourceNames);
1773         const Any* pDataSourceProps = aDataSourceProps.getConstArray();
1774         rtl::OUString sSource, sCommand;
1775         sal_Int16 nCommandType;
1776         pDataSourceProps[0] >>= sSource;
1777         pDataSourceProps[1] >>= sCommand;
1778         pDataSourceProps[2] >>= nCommandType;
1779         if(sSource.equals(aDBData.sDataSource) && sCommand.equals(aDBData.sCommand))
1780         {
1781             _DB_ColumnConfigData* pNewData = new _DB_ColumnConfigData;
1782             pNewData->sSource = sSource;
1783             pNewData->sTable = sCommand;
1784 
1785             pDataSourceProps[3] >>= pNewData->sEdit;
1786             pDataSourceProps[4] >>= pNewData->sTblList;
1787             pDataSourceProps[5] >>= pNewData->sTmplNm;
1788             pDataSourceProps[6] >>= pNewData->sTAutoFmtNm;
1789             if(pDataSourceProps[7].hasValue())
1790                 pNewData->bIsTable = *(sal_Bool*)pDataSourceProps[7].getValue();
1791             if(pDataSourceProps[8].hasValue())
1792                 pNewData->bIsField = *(sal_Bool*)pDataSourceProps[8].getValue();
1793             if(pDataSourceProps[9].hasValue())
1794                 pNewData->bIsHeadlineOn = *(sal_Bool*)pDataSourceProps[9].getValue();
1795             if(pDataSourceProps[10].hasValue())
1796                 pNewData->bIsEmptyHeadln = *(sal_Bool*)pDataSourceProps[10].getValue();
1797 
1798             rtl::OUString sSubNodeName(pNames[nNode]);
1799             sSubNodeName += C2U("/ColumnSet/");
1800             Sequence <rtl::OUString> aSubNames = GetNodeNames(sSubNodeName);
1801             const rtl::OUString* pSubNames = aSubNames.getConstArray();
1802             for(sal_Int32 nSub = 0; nSub < aSubNames.getLength(); nSub++)
1803             {
1804                 rtl::OUString sSubSubNodeName(sSubNodeName);
1805                 sSubSubNodeName += pSubNames[nSub];
1806                 Sequence <rtl::OUString> aSubNodeNames = lcl_CreateSubNames(sSubSubNodeName);
1807                 Sequence< Any> aSubProps = GetProperties(aSubNodeNames);
1808                 const Any* pSubProps = aSubProps.getConstArray();
1809 
1810                 rtl::OUString sColumn;
1811                 pSubProps[0] >>= sColumn;
1812                 //check for existance of the loaded column name
1813                 sal_Bool bFound = sal_False;
1814                 for(sal_Int32 nRealColumn = 0; nRealColumn < aDBColumns.Count(); nRealColumn++)
1815                 {
1816                     if(aDBColumns[(sal_uInt16)nRealColumn]->sColumn == sColumn)
1817                     {
1818                         bFound = sal_True;
1819                         break;
1820                     }
1821                 }
1822                 if(!bFound)
1823                     continue;
1824                 sal_Int16 nIndex = 0;
1825                 pSubProps[1] >>= nIndex;
1826                 SwInsDBColumnPtr pInsDBColumn = new SwInsDBColumn(sColumn, nIndex);
1827                 if(pSubProps[2].hasValue())
1828                     pInsDBColumn->bHasFmt = *(sal_Bool*)pSubProps[2].getValue();
1829                 if(pSubProps[3].hasValue())
1830                     pInsDBColumn->bIsDBFmt = *(sal_Bool*)pSubProps[3].getValue();
1831 
1832                 pSubProps[4] >>= pInsDBColumn->sUsrNumFmt;
1833                 rtl::OUString sNumberFormatLocale;
1834                 pSubProps[5] >>= sNumberFormatLocale;
1835 
1836                 Locale aLocale;
1837                 aLocale.Language = sNumberFormatLocale.copy(0, 2);
1838                 aLocale.Country = sNumberFormatLocale.copy(3, 2);
1839                 pInsDBColumn->eUsrNumFmtLng = SvxLocaleToLanguage( aLocale );
1840 
1841                 pInsDBColumn->nUsrNumFmt = rNFmtr.GetEntryKey( pInsDBColumn->sUsrNumFmt,
1842                                                         pInsDBColumn->eUsrNumFmtLng );
1843 
1844 //              pInsDBColumn->nDBNumFmt
1845 
1846                 pNewData->aDBColumns.Insert(pInsDBColumn);
1847             }
1848             sal_uInt16 n = 0;
1849             String sTmp( pNewData->sTblList );
1850             if( sTmp.Len() )
1851             {
1852                 do {
1853                     String sEntry( sTmp.GetToken( 0, '\x0a', n ) );
1854                     //preselect column - if they still exist!
1855                     if(aLbTblDbColumn.GetEntryPos(sEntry) != LISTBOX_ENTRY_NOTFOUND)
1856                     {
1857                         aLbTableCol.InsertEntry( sEntry );
1858                         aLbTblDbColumn.RemoveEntry( sEntry );
1859                     }
1860                 } while( n < sTmp.Len() );
1861 
1862                 if( !aLbTblDbColumn.GetEntryCount() )
1863                 {
1864                     aIbDbcolAllTo.Enable( sal_False );
1865                     aIbDbcolOneTo.Enable( sal_False );
1866                 }
1867                 aIbDbcolOneFrom.Enable( sal_True );
1868                 aIbDbcolAllFrom.Enable( sal_True );
1869             }
1870             aEdDbText.SetText( pNewData->sEdit );
1871 
1872             sTmp = pNewData->sTmplNm;
1873             if( sTmp.Len() )
1874                 aLbDbParaColl.SelectEntry( sTmp );
1875             else
1876                 aLbDbParaColl.SelectEntryPos( 0 );
1877 
1878             if( pTAutoFmt )
1879                 delete pTAutoFmt, pTAutoFmt = 0;
1880             sTmp = pNewData->sTAutoFmtNm;
1881             if( sTmp.Len() )
1882             {
1883                 // dann erstmal die AutoFmt-Datei laden und das Autoformat suchen
1884                 SwTableAutoFmtTbl aAutoFmtTbl;
1885                 aAutoFmtTbl.Load();
1886                 for( sal_uInt16 nAutoFmt = aAutoFmtTbl.Count(); nAutoFmt; )
1887                     if( sTmp == aAutoFmtTbl[ --nAutoFmt ]->GetName() )
1888                     {
1889                         pTAutoFmt = new SwTableAutoFmt( *aAutoFmtTbl[ nAutoFmt ] );
1890                         break;
1891                     }
1892             }
1893 
1894             aRbAsTable.Check( pNewData->bIsTable );
1895             aRbAsField.Check( pNewData->bIsField );
1896             aRbAsText.Check( !pNewData->bIsTable && !pNewData->bIsField );
1897 
1898             aCbTableHeadon.Check( pNewData->bIsHeadlineOn );
1899             aRbHeadlColnms.Check( !pNewData->bIsEmptyHeadln );
1900             aRbHeadlEmpty.Check( pNewData->bIsEmptyHeadln );
1901             HeaderHdl(&aCbTableHeadon);
1902 
1903             // jetzt noch die benutzerdefinierten Numberformat Strings in die
1904             // Shell kopieren. Nur diese sind dann als ID verfuegbar
1905             for( n = 0; n < aDBColumns.Count() ; ++n )
1906             {
1907                 SwInsDBColumn& rSet = *aDBColumns[ n ];
1908                 for( sal_uInt16 m = 0; m < pNewData->aDBColumns.Count() ; ++m )
1909                 {
1910                     SwInsDBColumn& rGet = *pNewData->aDBColumns[ m ];
1911                     if(rGet.sColumn == rSet.sColumn)
1912                     {
1913                         if( rGet.bHasFmt && !rGet.bIsDBFmt )
1914                         {
1915                             rSet.bIsDBFmt = sal_False;
1916                             rSet.nUsrNumFmt = rNFmtr.GetEntryKey( rGet.sUsrNumFmt,
1917                                                                     rGet.eUsrNumFmtLng );
1918                             if( NUMBERFORMAT_ENTRY_NOT_FOUND == rSet.nUsrNumFmt )
1919                             {
1920                                 xub_StrLen nCheckPos;
1921                                 short nType;
1922                                 String sTmpFmt = rGet.sUsrNumFmt;
1923                                 rNFmtr.PutEntry( sTmpFmt, nCheckPos, nType,
1924                                                 rSet.nUsrNumFmt, rGet.eUsrNumFmtLng );
1925                                 rGet.sUsrNumFmt = sTmpFmt;
1926                             }
1927                         }
1928                         break;
1929                     }
1930                 }
1931             }
1932 
1933             // steht der Cursor in einer Tabelle, darf NIE Tabelle auswaehlbar sein
1934             if( !aRbAsTable.IsEnabled() && aRbAsTable.IsChecked() )
1935                 aRbAsField.Check( sal_True );
1936             delete pNewData;
1937             break;
1938         }
1939     }
1940 }
1941 
1942