Update ooo320-m1
[ooovba.git] / sw / source / ui / dbui / createaddresslistdialog.cxx
blob527847b39a00e207f4e28de59647ad8acca0bd7f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: createaddresslistdialog.cxx,v $
10 * $Revision: 1.16 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
33 #ifdef SW_DLLIMPLEMENTATION
34 #undef SW_DLLIMPLEMENTATION
35 #endif
36 #include <swtypes.hxx>
37 #include <createaddresslistdialog.hxx>
38 #include <customizeaddresslistdialog.hxx>
39 #include <mmconfigitem.hxx>
40 #include <vcl/scrbar.hxx>
41 #include <vcl/msgbox.hxx>
42 #ifndef _SVT_CONTROLDIMS_HRC_
43 #include <svtools/controldims.hrc>
44 #endif
45 #include <svtools/pathoptions.hxx>
46 #include <sfx2/filedlghelper.hxx>
47 #include <sfx2/docfilt.hxx>
48 #include <sfx2/fcontnr.hxx>
49 #include <sfx2/docfac.hxx>
50 #include <sfx2/docfile.hxx>
51 #include <rtl/textenc.h>
52 #include "com/sun/star/ui/dialogs/TemplateDescription.hpp"
53 #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
54 #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
55 #include <tools/urlobj.hxx>
56 #include <createaddresslistdialog.hrc>
57 #include <dbui.hrc>
58 #include <helpid.h>
59 #include <unomid.h>
62 using namespace ::com::sun::star;
63 using namespace ::com::sun::star::ui::dialogs;
64 using ::rtl::OUString;
66 /*-- 19.04.2004 12:19:50---------------------------------------------------
68 -----------------------------------------------------------------------*/
69 class SwAddressControl_Impl : public Control
71 ScrollBar m_aScrollBar;
72 Window m_aWindow;
74 ::std::vector<FixedText*> m_aFixedTexts;
75 ::std::vector<Edit*> m_aEdits;
77 SwCSVData* m_pData;
78 Size m_aWinOutputSize;
79 sal_Int32 m_nLineHeight;
80 sal_uInt32 m_nCurrentDataSet;
82 bool m_bNoDataSet;
84 DECL_LINK(ScrollHdl_Impl, ScrollBar*);
85 DECL_LINK(GotFocusHdl_Impl, Edit*);
86 DECL_LINK(EditModifyHdl_Impl, Edit*);
88 void MakeVisible(const Rectangle& aRect);
90 virtual long PreNotify( NotifyEvent& rNEvt );
91 virtual void Command( const CommandEvent& rCEvt );
93 using Window::SetData;
95 public:
96 SwAddressControl_Impl(Window* pParent, const ResId& rResId );
97 ~SwAddressControl_Impl();
99 void SetData(SwCSVData& rDBData);
101 void SetCurrentDataSet(sal_uInt32 nSet);
102 sal_uInt32 GetCurrentDataSet() const { return m_nCurrentDataSet;}
103 void SetCursorTo(sal_uInt32 nElement);
106 /*-- 13.04.2004 10:09:42---------------------------------------------------
108 -----------------------------------------------------------------------*/
109 SwAddressControl_Impl::SwAddressControl_Impl(Window* pParent, const ResId& rResId ) :
110 Control(pParent, rResId),
111 #ifdef MSC
112 #pragma warning (disable : 4355)
113 #endif
114 m_aScrollBar(this, ResId(SCR_1,*rResId.GetResMgr())),
115 m_aWindow(this, ResId(WIN_DATA,*rResId.GetResMgr())),
116 #ifdef MSC
117 #pragma warning (default : 4355)
118 #endif
119 m_pData(0),
120 m_aWinOutputSize( m_aWindow.GetOutputSizePixel() ),
121 m_nLineHeight(0),
122 m_nCurrentDataSet(0),
123 m_bNoDataSet(true)
125 FreeResource();
126 Link aScrollLink = LINK(this, SwAddressControl_Impl, ScrollHdl_Impl);
127 m_aScrollBar.SetScrollHdl(aScrollLink);
128 m_aScrollBar.SetEndScrollHdl(aScrollLink);
129 m_aScrollBar.EnableDrag();
132 /*-- 13.04.2004 10:09:43---------------------------------------------------
134 -----------------------------------------------------------------------*/
135 SwAddressControl_Impl::~SwAddressControl_Impl()
137 ::std::vector<FixedText*>::iterator aTextIter;
138 for(aTextIter = m_aFixedTexts.begin(); aTextIter != m_aFixedTexts.end(); ++aTextIter)
139 delete *aTextIter;
140 ::std::vector<Edit*>::iterator aEditIter;
141 for(aEditIter = m_aEdits.begin(); aEditIter != m_aEdits.end(); ++aEditIter)
142 delete *aEditIter;
144 /*-- 19.04.2004 12:22:41---------------------------------------------------
146 -----------------------------------------------------------------------*/
147 void SwAddressControl_Impl::SetData(SwCSVData& rDBData)
149 m_pData = &rDBData;
150 //when the address data is updated then remove the controls an build again
151 if(m_aFixedTexts.size())
153 ::std::vector<FixedText*>::iterator aTextIter;
154 for(aTextIter = m_aFixedTexts.begin(); aTextIter != m_aFixedTexts.end(); ++aTextIter)
155 delete *aTextIter;
156 ::std::vector<Edit*>::iterator aEditIter;
157 for(aEditIter = m_aEdits.begin(); aEditIter != m_aEdits.end(); ++aEditIter)
158 delete *aEditIter;
159 m_aFixedTexts.clear();
160 m_aEdits.clear();
161 m_bNoDataSet = true;
163 //now create appropriate controls
165 ::std::vector< OUString >::iterator aHeaderIter;
167 long nFTXPos = m_aWindow.LogicToPixel(Point(RSC_SP_CTRL_X, RSC_SP_CTRL_X), MAP_APPFONT).X();
168 long nFTHeight = m_aWindow.LogicToPixel(Size(RSC_BS_CHARHEIGHT, RSC_BS_CHARHEIGHT), MAP_APPFONT).Height();
169 long nFTWidth = 0;
171 //determine the width of the FixedTexts
172 for(aHeaderIter = m_pData->aDBColumnHeaders.begin();
173 aHeaderIter != m_pData->aDBColumnHeaders.end();
174 ++aHeaderIter)
176 sal_Int32 nTemp = m_aWindow.GetTextWidth(*aHeaderIter);
177 if(nTemp > nFTWidth)
178 nFTWidth = nTemp;
180 //add some pixels
181 nFTWidth += 2;
182 long nEDXPos = nFTWidth + nFTXPos +
183 m_aWindow.LogicToPixel(Size(RSC_SP_CTRL_DESC_X, RSC_SP_CTRL_DESC_X), MAP_APPFONT).Width();
184 long nEDHeight = m_aWindow.LogicToPixel(Size(RSC_CD_TEXTBOX_HEIGHT, RSC_CD_TEXTBOX_HEIGHT), MAP_APPFONT).Height();
185 long nEDWidth = m_aWinOutputSize.Width() - nEDXPos - nFTXPos;
186 m_nLineHeight = nEDHeight + m_aWindow.LogicToPixel(Size(RSC_SP_CTRL_GROUP_Y, RSC_SP_CTRL_GROUP_Y), MAP_APPFONT).Height();
188 long nEDYPos = m_aWindow.LogicToPixel(Size(RSC_SP_CTRL_DESC_Y, RSC_SP_CTRL_DESC_Y), MAP_APPFONT).Height();
189 long nFTYPos = nEDYPos + nEDHeight - nFTHeight;
191 Link aFocusLink = LINK(this, SwAddressControl_Impl, GotFocusHdl_Impl);
192 Link aEditModifyLink = LINK(this, SwAddressControl_Impl, EditModifyHdl_Impl);
193 Edit* pLastEdit = 0;
194 sal_Int32 nVisibleLines = 0;
195 sal_Int32 nLines = 0;
196 for(aHeaderIter = m_pData->aDBColumnHeaders.begin();
197 aHeaderIter != m_pData->aDBColumnHeaders.end();
198 ++aHeaderIter, nEDYPos += m_nLineHeight, nFTYPos += m_nLineHeight, nLines++)
200 FixedText* pNewFT = new FixedText(&m_aWindow, WB_RIGHT);
201 Edit* pNewED = new Edit(&m_aWindow, WB_BORDER);
202 //set nLines a position identifier - used in the ModifyHdl
203 pNewED->SetData((void*)nLines);
204 pNewED->SetGetFocusHdl(aFocusLink);
205 pNewED->SetModifyHdl(aEditModifyLink);
207 pNewFT->SetPosSizePixel(Point(nFTXPos, nFTYPos), Size(nFTWidth, nFTHeight));
208 pNewED->SetPosSizePixel(Point(nEDXPos, nEDYPos), Size(nEDWidth, nEDHeight));
209 if(nEDYPos + nEDHeight < m_aWinOutputSize.Height())
210 ++nVisibleLines;
212 pNewFT->SetText(*aHeaderIter);
214 pNewFT->Show();
215 pNewED->Show();
216 m_aFixedTexts.push_back(pNewFT);
217 m_aEdits.push_back(pNewED);
218 pLastEdit = pNewED;
220 //scrollbar adjustment
221 if(pLastEdit)
223 //the m_aWindow has to be at least as high as the ScrollBar and it must include the last Edit
224 sal_Int32 nContentHeight = pLastEdit->GetPosPixel().Y() + nEDHeight +
225 m_aWindow.LogicToPixel(Size(RSC_SP_CTRL_GROUP_Y, RSC_SP_CTRL_GROUP_Y), MAP_APPFONT).Height();
226 if(nContentHeight < m_aScrollBar.GetSizePixel().Height())
228 nContentHeight = m_aScrollBar.GetSizePixel().Height();
229 m_aScrollBar.Enable(FALSE);
231 else
233 m_aScrollBar.Enable(TRUE);
234 m_aScrollBar.SetRange(Range(0, nLines));
235 m_aScrollBar.SetThumbPos(0);
236 m_aScrollBar.SetVisibleSize(nVisibleLines);
238 Size aWinOutputSize(m_aWinOutputSize);
239 aWinOutputSize.Height() = nContentHeight;
240 m_aWindow.SetOutputSizePixel(aWinOutputSize);
244 /*-- 21.04.2004 11:37:09---------------------------------------------------
246 -----------------------------------------------------------------------*/
247 void SwAddressControl_Impl::SetCurrentDataSet(sal_uInt32 nSet)
249 if(m_bNoDataSet || m_nCurrentDataSet != nSet)
251 m_bNoDataSet = false;
252 m_nCurrentDataSet = nSet;
253 DBG_ASSERT(m_pData->aDBData.size() > m_nCurrentDataSet, "wrong data set index");
254 if(m_pData->aDBData.size() > m_nCurrentDataSet)
256 ::std::vector<Edit*>::iterator aEditIter;
257 sal_uInt32 nIndex = 0;
258 for(aEditIter = m_aEdits.begin(); aEditIter != m_aEdits.end(); ++aEditIter, ++nIndex)
260 DBG_ASSERT(nIndex < m_pData->aDBData[m_nCurrentDataSet].size(),
261 "number of colums doesn't match number of Edits");
262 (*aEditIter)->SetText(m_pData->aDBData[m_nCurrentDataSet][nIndex]);
268 /*-- 19.04.2004 14:17:50---------------------------------------------------
270 -----------------------------------------------------------------------*/
271 IMPL_LINK(SwAddressControl_Impl, ScrollHdl_Impl, ScrollBar*, pScroll)
273 long nThumb = pScroll->GetThumbPos();
274 m_aWindow.SetPosPixel(Point(0, - (m_nLineHeight * nThumb)));
276 return 0;
278 /*-- 19.04.2004 16:16:25---------------------------------------------------
280 -----------------------------------------------------------------------*/
281 IMPL_LINK(SwAddressControl_Impl, GotFocusHdl_Impl, Edit*, pEdit)
283 if(0 != (GETFOCUS_TAB & pEdit->GetGetFocusFlags()))
285 Rectangle aRect(pEdit->GetPosPixel(), pEdit->GetSizePixel());
286 MakeVisible(aRect);
288 return 0;
290 /*-- 21.04.2004 14:56:54---------------------------------------------------
292 -----------------------------------------------------------------------*/
293 void SwAddressControl_Impl::MakeVisible(const Rectangle & rRect)
295 long nThumb = m_aScrollBar.GetThumbPos();
296 //determine range of visible positions
297 long nMinVisiblePos = - m_aWindow.GetPosPixel().Y();
298 long nMaxVisiblePos = m_aScrollBar.GetSizePixel().Height() + nMinVisiblePos;
299 if( rRect.TopLeft().Y() < nMinVisiblePos)
301 nThumb -= 1 + ((nMinVisiblePos - rRect.TopLeft().Y()) / m_nLineHeight);
303 else if(rRect.BottomLeft().Y() > nMaxVisiblePos)
305 nThumb += 1 + ((nMaxVisiblePos - rRect.BottomLeft().Y()) / m_nLineHeight);
307 if(nThumb != m_aScrollBar.GetThumbPos())
309 m_aScrollBar.SetThumbPos(nThumb);
310 ScrollHdl_Impl(&m_aScrollBar);
313 /*-- 19.04.2004 16:16:25---------------------------------------------------
314 copy data changes into database
315 -----------------------------------------------------------------------*/
316 IMPL_LINK(SwAddressControl_Impl, EditModifyHdl_Impl, Edit*, pEdit)
318 //get the data element number of the current set
319 sal_Int32 nIndex = (sal_Int32)(sal_IntPtr)pEdit->GetData();
320 //get the index of the set
321 DBG_ASSERT(m_pData->aDBData.size() > m_nCurrentDataSet, "wrong data set index" );
322 if(m_pData->aDBData.size() > m_nCurrentDataSet)
324 m_pData->aDBData[m_nCurrentDataSet][nIndex] = pEdit->GetText();
326 return 0;
328 /*-- 21.04.2004 14:51:54---------------------------------------------------
330 -----------------------------------------------------------------------*/
331 void SwAddressControl_Impl::SetCursorTo(sal_uInt32 nElement)
333 if(nElement < m_aEdits.size())
335 Edit* pEdit = m_aEdits[nElement];
336 pEdit->GrabFocus();
337 Rectangle aRect(pEdit->GetPosPixel(), pEdit->GetSizePixel());
338 MakeVisible(aRect);
342 /*-- 19.04.2004 16:16:25---------------------------------------------------
344 -----------------------------------------------------------------------*/
345 void SwAddressControl_Impl::Command( const CommandEvent& rCEvt )
347 switch ( rCEvt.GetCommand() )
349 case COMMAND_WHEEL:
350 case COMMAND_STARTAUTOSCROLL:
351 case COMMAND_AUTOSCROLL:
353 const CommandWheelData* pWheelData = rCEvt.GetWheelData();
354 if(pWheelData && !pWheelData->IsHorz() && COMMAND_WHEEL_ZOOM != pWheelData->GetMode())
356 HandleScrollCommand( rCEvt, 0, &m_aScrollBar );
359 break;
360 default:
361 Control::Command(rCEvt);
364 /*-- 19.04.2004 16:16:25---------------------------------------------------
366 -----------------------------------------------------------------------*/
367 long SwAddressControl_Impl::PreNotify( NotifyEvent& rNEvt )
369 if(rNEvt.GetType() == EVENT_COMMAND)
371 const CommandEvent* pCEvt = rNEvt.GetCommandEvent();
372 USHORT nCmd = pCEvt->GetCommand();
373 if( COMMAND_WHEEL == nCmd )
375 Command(*pCEvt);
376 return 1;
379 return Control::PreNotify(rNEvt);
381 /*-- 13.04.2004 10:08:59---------------------------------------------------
383 -----------------------------------------------------------------------*/
384 SwCreateAddressListDialog::SwCreateAddressListDialog(
385 Window* pParent, const String& rURL, SwMailMergeConfigItem& rConfig) :
386 SfxModalDialog(pParent, SW_RES(DLG_MM_CREATEADDRESSLIST)),
387 #ifdef MSC
388 #pragma warning (disable : 4355)
389 #endif
390 m_aAddressInformation( this, SW_RES( FI_ADDRESSINFORMATION)),
391 m_pAddressControl(new SwAddressControl_Impl(this, SW_RES(CT_ADDRESS))),
392 m_aNewPB( this, SW_RES( PB_NEW)),
393 m_aDeletePB( this, SW_RES( PB_DELETE)),
394 m_aFindPB( this, SW_RES( PB_FIND)),
395 m_aCustomizePB( this, SW_RES( PB_CUSTOMIZE)),
397 m_aViewEntriesFI( this, SW_RES( FI_VIEWENTRIES)),
398 m_aStartPB( this, SW_RES( PB_START)),
399 m_aPrevPB( this, SW_RES( PB_PREV)),
400 m_aSetNoNF( this, SW_RES( NF_SETNO)),
401 m_aNextPB( this, SW_RES( PB_NEXT )),
402 m_aEndPB( this, SW_RES( PB_END)),
404 m_aSeparatorFL( this, SW_RES( FL_SEPARATOR)),
406 m_aOK( this, SW_RES( PB_OK)),
407 m_aCancel( this, SW_RES( PB_CANCEL)),
408 m_aHelp( this, SW_RES( PB_HELP)),
409 #ifdef MSC
410 #pragma warning (default : 4355)
411 #endif
412 m_sAddressListFilterName( SW_RES( ST_FILTERNAME)),
413 m_sURL(rURL),
414 m_pCSVData( new SwCSVData ),
415 m_pFindDlg(0)
417 FreeResource();
418 m_aNewPB.SetClickHdl(LINK(this, SwCreateAddressListDialog, NewHdl_Impl));
419 m_aDeletePB.SetClickHdl(LINK(this, SwCreateAddressListDialog, DeleteHdl_Impl));
420 m_aFindPB.SetClickHdl(LINK(this, SwCreateAddressListDialog, FindHdl_Impl));
421 m_aCustomizePB.SetClickHdl(LINK(this, SwCreateAddressListDialog, CustomizeHdl_Impl));
422 m_aOK.SetClickHdl(LINK(this, SwCreateAddressListDialog, OkHdl_Impl));
424 Link aLk = LINK(this, SwCreateAddressListDialog, DBCursorHdl_Impl);
425 m_aStartPB.SetClickHdl(aLk);
426 m_aPrevPB.SetClickHdl(aLk);
427 m_aSetNoNF.SetModifyHdl(LINK(this, SwCreateAddressListDialog, DBNumCursorHdl_Impl));
428 m_aNextPB.SetClickHdl(aLk);
429 m_aEndPB.SetClickHdl(aLk);
431 if(m_sURL.Len())
433 //file exists, has to be loaded here
434 SfxMedium aMedium( m_sURL, STREAM_READ, TRUE );
435 SvStream* pStream = aMedium.GetInStream();
436 if(pStream)
438 pStream->SetLineDelimiter( LINEEND_LF );
439 pStream->SetStreamCharSet(RTL_TEXTENCODING_UTF8);
441 OUString sSemi(';');
442 OUString sQuote('"');
443 String sTempMiddle(sQuote);
444 sTempMiddle += sal_Unicode(9);
445 OUString sMiddle(sTempMiddle);
447 String sLine;
448 BOOL bRead = pStream->ReadUniOrByteStringLine( sLine, RTL_TEXTENCODING_UTF8 );
450 if(bRead)
452 //header line
453 xub_StrLen nHeaders = sLine.GetTokenCount('\t');
454 xub_StrLen nIndex = 0;
455 for( xub_StrLen nToken = 0; nToken < nHeaders; ++nToken)
457 String sHeader = sLine.GetToken( 0, '\t', nIndex );
458 DBG_ASSERT(sHeader.Len() > 2 &&
459 sHeader.GetChar(0) == '\"' && sHeader.GetChar(sHeader.Len() - 1) == '\"',
460 "Wrong format of header");
461 if(sHeader.Len() > 2)
463 m_pCSVData->aDBColumnHeaders.push_back( sHeader.Copy(1, sHeader.Len() -2));
467 while(pStream->ReadUniOrByteStringLine( sLine, RTL_TEXTENCODING_UTF8 ))
469 ::std::vector<OUString> aNewData;
470 //analyze data line
471 xub_StrLen nDataCount = sLine.GetTokenCount('\t');
472 xub_StrLen nIndex = 0;
473 for( xub_StrLen nToken = 0; nToken < nDataCount; ++nToken)
475 String sData = sLine.GetToken( 0, '\t', nIndex );
476 DBG_ASSERT(sData.Len() >= 2 &&
477 sData.GetChar(0) == '\"' && sData.GetChar(sData.Len() - 1) == '\"',
478 "Wrong format of line");
479 if(sData.Len() >= 2)
480 aNewData.push_back(sData.Copy(1, sData.Len() - 2));
481 else
482 aNewData.push_back(sData);
484 m_pCSVData->aDBData.push_back( aNewData );
488 else
490 //database has to be created
491 const ResStringArray& rAddressHeader = rConfig.GetDefaultAddressHeaders();
492 sal_uInt32 nCount = rAddressHeader.Count();
493 for(USHORT nHeader = 0; nHeader < nCount; ++nHeader)
494 m_pCSVData->aDBColumnHeaders.push_back( rAddressHeader.GetString(nHeader));
495 ::std::vector<OUString> aNewData;
496 String sTemp;
497 aNewData.insert(aNewData.begin(), nCount, sTemp);
498 m_pCSVData->aDBData.push_back(aNewData);
500 //now fill the address control
501 m_pAddressControl->SetData(*m_pCSVData);
502 m_pAddressControl->SetCurrentDataSet(0);
503 m_aSetNoNF.SetMax(m_pCSVData->aDBData.size());
504 UpdateButtons();
506 /*-- 13.04.2004 10:08:59---------------------------------------------------
508 -----------------------------------------------------------------------*/
509 SwCreateAddressListDialog::~SwCreateAddressListDialog()
511 delete m_pAddressControl;
512 delete m_pCSVData;
513 delete m_pFindDlg;
515 /*-- 13.04.2004 10:08:59---------------------------------------------------
516 add a new data set of empty strings and set the address input control
517 to that new set
518 -----------------------------------------------------------------------*/
519 IMPL_LINK(SwCreateAddressListDialog, NewHdl_Impl, PushButton*, EMPTYARG)
521 sal_uInt32 nCurrent = m_pAddressControl->GetCurrentDataSet();
522 ::std::vector<OUString> aNewData;
523 String sTemp;
524 aNewData.insert(aNewData.begin(), m_pCSVData->aDBColumnHeaders.size(), sTemp);
525 m_pCSVData->aDBData.insert(m_pCSVData->aDBData.begin() + ++nCurrent, aNewData);
526 m_aSetNoNF.SetMax(m_pCSVData->aDBData.size());
527 //the NumericField start at 1
528 m_aSetNoNF.SetValue(nCurrent + 1);
529 //the address control starts at 0
530 m_pAddressControl->SetCurrentDataSet(nCurrent);
531 UpdateButtons();
532 return 0;
534 /*-- 13.04.2004 10:09:00---------------------------------------------------
536 -----------------------------------------------------------------------*/
537 IMPL_LINK(SwCreateAddressListDialog, DeleteHdl_Impl, PushButton*, EMPTYARG)
539 sal_uInt32 nCurrent = m_pAddressControl->GetCurrentDataSet();
540 if(m_pCSVData->aDBData.size() > 1)
542 m_pCSVData->aDBData.erase(m_pCSVData->aDBData.begin() + nCurrent);
543 if(nCurrent)
544 --nCurrent;
546 else
548 // if only one set is available then clear the data
549 String sTemp;
550 m_pCSVData->aDBData[0].assign(m_pCSVData->aDBData[0].size(), sTemp);
551 m_aDeletePB.Enable(FALSE);
553 m_pAddressControl->SetCurrentDataSet(nCurrent);
554 m_aSetNoNF.SetMax(m_pCSVData->aDBData.size());
555 UpdateButtons();
556 return 0;
558 /*-- 13.04.2004 10:09:00---------------------------------------------------
560 -----------------------------------------------------------------------*/
561 IMPL_LINK(SwCreateAddressListDialog, FindHdl_Impl, PushButton*, EMPTYARG)
563 if(!m_pFindDlg)
565 m_pFindDlg = new SwFindEntryDialog(this);
566 ListBox& rColumnBox = m_pFindDlg->GetFieldsListBox();
567 ::std::vector< OUString >::iterator aHeaderIter;
568 for(aHeaderIter = m_pCSVData->aDBColumnHeaders.begin();
569 aHeaderIter != m_pCSVData->aDBColumnHeaders.end();
570 ++aHeaderIter)
571 rColumnBox.InsertEntry(*aHeaderIter);
572 rColumnBox.SelectEntryPos( 0 );
574 else
575 m_pFindDlg->Show(!m_pFindDlg->IsVisible());
576 return 0;
578 /*-- 13.04.2004 10:09:00---------------------------------------------------
580 -----------------------------------------------------------------------*/
581 IMPL_LINK(SwCreateAddressListDialog, CustomizeHdl_Impl, PushButton*, pButton)
583 SwCustomizeAddressListDialog* pDlg = new SwCustomizeAddressListDialog(pButton, *m_pCSVData);
584 if(RET_OK == pDlg->Execute())
586 delete m_pCSVData;
587 m_pCSVData = pDlg->GetNewData();
588 m_pAddressControl->SetData(*m_pCSVData);
589 m_pAddressControl->SetCurrentDataSet(m_pAddressControl->GetCurrentDataSet());
591 delete pDlg;
593 //update find dialog
594 if(m_pFindDlg)
596 ListBox& rColumnBox = m_pFindDlg->GetFieldsListBox();
597 rColumnBox.Clear();
598 ::std::vector< OUString >::iterator aHeaderIter;
599 for(aHeaderIter = m_pCSVData->aDBColumnHeaders.begin();
600 aHeaderIter != m_pCSVData->aDBColumnHeaders.end();
601 ++aHeaderIter)
602 rColumnBox.InsertEntry(*aHeaderIter);
604 return 0;
606 /*-- 23.04.2004 09:02:51---------------------------------------------------
607 writes the data into a .csv file
608 encoding is UTF8, separator is tab, strings are enclosed into "
609 -----------------------------------------------------------------------*/
610 IMPL_LINK(SwCreateAddressListDialog, OkHdl_Impl, PushButton*, EMPTYARG)
612 if(!m_sURL.Len())
614 sfx2::FileDialogHelper aDlgHelper( TemplateDescription::FILESAVE_SIMPLE, 0 );
615 uno::Reference < XFilePicker > xFP = aDlgHelper.GetFilePicker();
617 String sPath( SvtPathOptions().SubstituteVariable(
618 String::CreateFromAscii("$(userurl)/database") ));
619 aDlgHelper.SetDisplayDirectory( sPath );
620 uno::Reference< XFilterManager > xFltMgr(xFP, uno::UNO_QUERY);
621 ::rtl::OUString sCSV(C2U("*.csv"));
622 xFltMgr->appendFilter( m_sAddressListFilterName, sCSV );
623 xFltMgr->setCurrentFilter( m_sAddressListFilterName ) ;
625 if( ERRCODE_NONE == aDlgHelper.Execute() )
627 m_sURL = xFP->getFiles().getConstArray()[0];
628 INetURLObject aResult( m_sURL );
629 aResult.setExtension(String::CreateFromAscii("csv"));
630 m_sURL = aResult.GetMainURL(INetURLObject::NO_DECODE);
633 if(m_sURL.Len())
635 SfxMedium aMedium( m_sURL, STREAM_READWRITE|STREAM_TRUNC, TRUE );
636 SvStream* pStream = aMedium.GetOutStream();
637 pStream->SetLineDelimiter( LINEEND_LF );
638 pStream->SetStreamCharSet(RTL_TEXTENCODING_UTF8);
640 OUString sSemi(';');
641 OUString sQuote('"');
642 String sTempMiddle(sQuote);
643 sTempMiddle += sal_Unicode(9);
644 OUString sMiddle(sTempMiddle);
645 sMiddle += sQuote;
647 //create a string for the header line
648 OUString sLine(sQuote);
649 ::std::vector< OUString >::iterator aHeaderIter;
650 for(aHeaderIter = m_pCSVData->aDBColumnHeaders.begin();
651 aHeaderIter != m_pCSVData->aDBColumnHeaders.end();
652 ++aHeaderIter)
654 sLine += *aHeaderIter;
655 sLine += sMiddle;
657 //remove tab and quote
658 sLine = sLine.copy( 0, sLine.getLength() - 2 );
659 pStream->WriteUnicodeOrByteText( sLine, RTL_TEXTENCODING_UTF8 );
660 endl(*pStream);
662 ::std::vector< ::std::vector< OUString > >::iterator aDataIter;
663 for( aDataIter = m_pCSVData->aDBData.begin(); aDataIter != m_pCSVData->aDBData.end(); ++aDataIter)
665 sLine = sQuote;
666 ::std::vector< OUString >::iterator aColumnIter;
667 for(aColumnIter = aDataIter->begin(); aColumnIter != aDataIter->end(); ++aColumnIter)
669 sLine += *aColumnIter;
670 sLine += sMiddle;
672 //remove tab and quote
673 sLine = sLine.copy( 0, sLine.getLength() - 2 );
674 pStream->WriteUnicodeOrByteText( sLine, RTL_TEXTENCODING_UTF8 );
675 endl(*pStream);
677 aMedium.Commit();
678 EndDialog(RET_OK);
681 return 0;
683 /*-- 13.04.2004 10:09:01---------------------------------------------------
685 -----------------------------------------------------------------------*/
686 IMPL_LINK(SwCreateAddressListDialog, DBCursorHdl_Impl, PushButton*, pButton)
688 sal_uInt32 nValue = static_cast< sal_uInt32 >(m_aSetNoNF.GetValue());
690 if(pButton == &m_aStartPB)
691 nValue = 1;
692 else if(pButton == &m_aPrevPB)
694 if(nValue > 1)
695 --nValue;
697 else if(pButton == &m_aNextPB)
699 if(nValue < (sal_uInt32)m_aSetNoNF.GetMax())
700 ++nValue;
702 else //m_aEndPB
703 nValue = static_cast< sal_uInt32 >(m_aSetNoNF.GetMax());
704 if(nValue != m_aSetNoNF.GetValue())
706 m_aSetNoNF.SetValue(nValue);
707 DBNumCursorHdl_Impl(&m_aSetNoNF);
709 return 0;
711 /*-- 21.04.2004 12:06:47---------------------------------------------------
713 -----------------------------------------------------------------------*/
714 IMPL_LINK(SwCreateAddressListDialog, DBNumCursorHdl_Impl, NumericField*, EMPTYARG)
716 m_pAddressControl->SetCurrentDataSet( static_cast< sal_uInt32 >(m_aSetNoNF.GetValue() - 1) );
717 UpdateButtons();
718 return 0;
720 /*-- 21.04.2004 13:22:27---------------------------------------------------
722 -----------------------------------------------------------------------*/
723 void SwCreateAddressListDialog::UpdateButtons()
725 sal_uInt32 nCurrent = static_cast< sal_uInt32 >(m_aSetNoNF.GetValue() );
726 sal_uInt32 nSize = (sal_uInt32 )m_pCSVData->aDBData.size();
727 m_aStartPB.Enable(nCurrent != 1);
728 m_aPrevPB.Enable(nCurrent != 1);
729 m_aNextPB.Enable(nCurrent != nSize);
730 m_aEndPB.Enable(nCurrent != nSize);
731 m_aDeletePB.Enable(nSize > 0);
733 /*-- 21.04.2004 13:22:27---------------------------------------------------
735 -----------------------------------------------------------------------*/
736 void SwCreateAddressListDialog::Find(const String& rSearch, sal_Int32 nColumn)
738 OUString sSearch = rSearch;
739 sSearch.toAsciiLowerCase();
740 sal_uInt32 nCurrent = m_pAddressControl->GetCurrentDataSet();
741 //search forward
742 bool bFound = false;
743 sal_uInt32 nStart = nCurrent + 1;
744 sal_uInt32 nEnd = m_pCSVData->aDBData.size();
745 sal_uInt32 nElement = 0;
746 sal_uInt32 nPos = 0;
747 for(short nTemp = 0; nTemp < 2 && !bFound; nTemp++)
749 for(nPos = nStart; nPos < nEnd; ++nPos)
751 ::std::vector< OUString> aData = m_pCSVData->aDBData[nPos];
752 if(nColumn >=0)
753 bFound = -1 != aData[(sal_uInt32)nColumn].toAsciiLowerCase().indexOf(sSearch);
754 else
756 for( nElement = 0; nElement < aData.size(); ++nElement)
758 bFound = -1 != aData[nElement].toAsciiLowerCase().indexOf(sSearch);
759 if(bFound)
761 nColumn = nElement;
762 break;
766 if(bFound)
767 break;
769 nStart = 0;
770 nEnd = nCurrent + 1;
772 if(bFound)
774 m_pAddressControl->SetCurrentDataSet(nPos);
775 m_aSetNoNF.SetValue( nPos + 1 );
776 UpdateButtons();
777 m_pAddressControl->SetCursorTo(nElement);
780 /*-- 13.04.2004 13:48:38---------------------------------------------------
782 -----------------------------------------------------------------------*/
783 SwFindEntryDialog::SwFindEntryDialog(SwCreateAddressListDialog* pParent) :
784 ModelessDialog(pParent, SW_RES(DLG_MM_FIND_ENTRY)),
785 #ifdef MSC
786 #pragma warning (disable : 4355)
787 #endif
788 m_aFindFT( this, SW_RES( FT_FIND )),
789 m_aFindED( this, SW_RES( ED_FIND )),
790 m_aFindOnlyCB( this, SW_RES( CB_FINDONLY )),
791 m_aFindOnlyLB( this, SW_RES( LB_FINDONLY )),
792 m_aFindPB( this, SW_RES( PB_FIND)),
793 m_aCancel( this, SW_RES( PB_CANCEL)),
794 m_aHelp( this, SW_RES( PB_HELP)),
795 #ifdef MSC
796 #pragma warning (default : 4355)
797 #endif
798 m_pParent(pParent)
800 FreeResource();
801 m_aFindPB.SetClickHdl(LINK(this, SwFindEntryDialog, FindHdl_Impl));
802 m_aFindED.SetModifyHdl(LINK(this, SwFindEntryDialog, FindEnableHdl_Impl));
803 m_aCancel.SetClickHdl(LINK(this, SwFindEntryDialog, CloseHdl_Impl));
805 /*-- 13.04.2004 13:48:38---------------------------------------------------
807 -----------------------------------------------------------------------*/
808 SwFindEntryDialog::~SwFindEntryDialog()
811 /*-- 21.04.2004 13:37:46---------------------------------------------------
813 -----------------------------------------------------------------------*/
814 IMPL_LINK(SwFindEntryDialog, FindHdl_Impl, PushButton*, EMPTYARG)
816 sal_Int32 nColumn = -1;
817 if(m_aFindOnlyCB.IsChecked())
818 nColumn = m_aFindOnlyLB.GetSelectEntryPos();
819 if(nColumn != LISTBOX_ENTRY_NOTFOUND)
820 m_pParent->Find(m_aFindED.GetText(), nColumn);
821 return 0;
823 /*-- 21.04.2004 13:37:46---------------------------------------------------
825 -----------------------------------------------------------------------*/
826 IMPL_LINK(SwFindEntryDialog, FindEnableHdl_Impl, Edit*, EMPTYARG)
828 m_aFindPB.Enable(m_aFindED.GetText().Len() > 0);
829 return 0;
831 /*-- 21.04.2004 15:36:36---------------------------------------------------
833 -----------------------------------------------------------------------*/
834 IMPL_LINK(SwFindEntryDialog, CloseHdl_Impl, PushButton*, EMPTYARG)
836 Show(FALSE);
837 return 0;