LCOV - code coverage report
Current view: top level - libreoffice/sw/source/ui/dbui - createaddresslistdialog.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 385 0.0 %
Date: 2012-12-27 Functions: 0 44 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <swtypes.hxx>
      21             : #include <createaddresslistdialog.hxx>
      22             : #include <customizeaddresslistdialog.hxx>
      23             : #include <mmconfigitem.hxx>
      24             : #include <comphelper/string.hxx>
      25             : #include <vcl/scrbar.hxx>
      26             : #include <vcl/msgbox.hxx>
      27             : #include <svtools/controldims.hrc>
      28             : #include <unotools/pathoptions.hxx>
      29             : #include <sfx2/filedlghelper.hxx>
      30             : #include <sfx2/docfilt.hxx>
      31             : #include <sfx2/fcontnr.hxx>
      32             : #include <sfx2/docfac.hxx>
      33             : #include <sfx2/docfile.hxx>
      34             : #include <rtl/textenc.h>
      35             : #include "com/sun/star/ui/dialogs/TemplateDescription.hpp"
      36             : #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
      37             : #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
      38             : #include <tools/urlobj.hxx>
      39             : #include <createaddresslistdialog.hrc>
      40             : #include <dbui.hrc>
      41             : #include <helpid.h>
      42             : #include <unomid.h>
      43             : 
      44             : 
      45             : using namespace ::com::sun::star;
      46             : using namespace ::com::sun::star::ui::dialogs;
      47             : using ::rtl::OUString;
      48             : 
      49             : class SwAddressControl_Impl : public Control
      50             : {
      51             :     ScrollBar                       m_aScrollBar;
      52             :     Window                          m_aWindow;
      53             : 
      54             :     ::std::vector<FixedText*>       m_aFixedTexts;
      55             :     ::std::vector<Edit*>            m_aEdits;
      56             : 
      57             :     SwCSVData*                      m_pData;
      58             :     Size                            m_aWinOutputSize;
      59             :     sal_Int32                       m_nLineHeight;
      60             :     sal_uInt32                      m_nCurrentDataSet;
      61             : 
      62             :     bool                            m_bNoDataSet;
      63             : 
      64             :     DECL_LINK(ScrollHdl_Impl, ScrollBar*);
      65             :     DECL_LINK(GotFocusHdl_Impl, Edit*);
      66             :     DECL_LINK(EditModifyHdl_Impl, Edit*);
      67             : 
      68             :     void                MakeVisible(const Rectangle& aRect);
      69             : 
      70             :     virtual long        PreNotify( NotifyEvent& rNEvt );
      71             :     virtual void        Command( const CommandEvent& rCEvt );
      72             : 
      73             :     using Window::SetData;
      74             : 
      75             : public:
      76             :     SwAddressControl_Impl(Window* pParent, const ResId& rResId );
      77             :     ~SwAddressControl_Impl();
      78             : 
      79             :     void        SetData(SwCSVData& rDBData);
      80             : 
      81             :     void        SetCurrentDataSet(sal_uInt32 nSet);
      82           0 :     sal_uInt32  GetCurrentDataSet() const { return m_nCurrentDataSet;}
      83             :     void        SetCursorTo(sal_uInt32 nElement);
      84             : };
      85             : 
      86           0 : SwAddressControl_Impl::SwAddressControl_Impl(Window* pParent, const ResId& rResId ) :
      87             :     Control(pParent, rResId),
      88             : #ifdef MSC
      89             : #pragma warning (disable : 4355)
      90             : #endif
      91           0 :     m_aScrollBar(this, ResId(SCR_1,*rResId.GetResMgr())),
      92           0 :     m_aWindow(this, ResId(WIN_DATA,*rResId.GetResMgr())),
      93             : #ifdef MSC
      94             : #pragma warning (default : 4355)
      95             : #endif
      96             :     m_pData(0),
      97           0 :     m_aWinOutputSize( m_aWindow.GetOutputSizePixel() ),
      98             :     m_nLineHeight(0),
      99             :     m_nCurrentDataSet(0),
     100           0 :     m_bNoDataSet(true)
     101             : {
     102           0 :     FreeResource();
     103           0 :     Link aScrollLink = LINK(this, SwAddressControl_Impl, ScrollHdl_Impl);
     104           0 :     m_aScrollBar.SetScrollHdl(aScrollLink);
     105           0 :     m_aScrollBar.SetEndScrollHdl(aScrollLink);
     106           0 :     m_aScrollBar.EnableDrag();
     107             : 
     108           0 : }
     109             : 
     110           0 : SwAddressControl_Impl::~SwAddressControl_Impl()
     111             : {
     112           0 :     ::std::vector<FixedText*>::iterator aTextIter;
     113           0 :     for(aTextIter = m_aFixedTexts.begin(); aTextIter != m_aFixedTexts.end(); ++aTextIter)
     114           0 :         delete *aTextIter;
     115           0 :     ::std::vector<Edit*>::iterator aEditIter;
     116           0 :     for(aEditIter = m_aEdits.begin(); aEditIter != m_aEdits.end(); ++aEditIter)
     117           0 :         delete *aEditIter;
     118           0 : }
     119             : 
     120           0 : void SwAddressControl_Impl::SetData(SwCSVData& rDBData)
     121             : {
     122           0 :     m_pData = &rDBData;
     123             :     //when the address data is updated then remove the controls an build again
     124           0 :     if(m_aFixedTexts.size())
     125             :     {
     126           0 :         ::std::vector<FixedText*>::iterator aTextIter;
     127           0 :         for(aTextIter = m_aFixedTexts.begin(); aTextIter != m_aFixedTexts.end(); ++aTextIter)
     128           0 :             delete *aTextIter;
     129           0 :         ::std::vector<Edit*>::iterator aEditIter;
     130           0 :         for(aEditIter = m_aEdits.begin(); aEditIter != m_aEdits.end(); ++aEditIter)
     131           0 :             delete *aEditIter;
     132           0 :         m_aFixedTexts.clear();
     133           0 :         m_aEdits.clear();
     134           0 :         m_bNoDataSet = true;
     135             :     }
     136             :     //now create appropriate controls
     137             : 
     138           0 :     ::std::vector< OUString >::iterator  aHeaderIter;
     139             : 
     140           0 :     long nFTXPos = m_aWindow.LogicToPixel(Point(RSC_SP_CTRL_X, RSC_SP_CTRL_X), MAP_APPFONT).X();
     141           0 :     long nFTHeight = m_aWindow.LogicToPixel(Size(RSC_BS_CHARHEIGHT, RSC_BS_CHARHEIGHT), MAP_APPFONT).Height();
     142           0 :     long nFTWidth = 0;
     143             : 
     144             :     //determine the width of the FixedTexts
     145           0 :     for(aHeaderIter = m_pData->aDBColumnHeaders.begin();
     146           0 :                 aHeaderIter != m_pData->aDBColumnHeaders.end();
     147             :                 ++aHeaderIter)
     148             :     {
     149           0 :         sal_Int32 nTemp = m_aWindow.GetTextWidth(*aHeaderIter);
     150           0 :         if(nTemp > nFTWidth)
     151           0 :           nFTWidth = nTemp;
     152             :     }
     153             :     //add some pixels
     154           0 :     nFTWidth += 2;
     155             :     long nEDXPos = nFTWidth + nFTXPos +
     156           0 :             m_aWindow.LogicToPixel(Size(RSC_SP_CTRL_DESC_X, RSC_SP_CTRL_DESC_X), MAP_APPFONT).Width();
     157           0 :     long nEDHeight = m_aWindow.LogicToPixel(Size(RSC_CD_TEXTBOX_HEIGHT, RSC_CD_TEXTBOX_HEIGHT), MAP_APPFONT).Height();
     158           0 :     long nEDWidth = m_aWinOutputSize.Width() - nEDXPos - nFTXPos;
     159           0 :     m_nLineHeight = nEDHeight + m_aWindow.LogicToPixel(Size(RSC_SP_CTRL_GROUP_Y, RSC_SP_CTRL_GROUP_Y), MAP_APPFONT).Height();
     160             : 
     161           0 :     long nEDYPos = m_aWindow.LogicToPixel(Size(RSC_SP_CTRL_DESC_Y, RSC_SP_CTRL_DESC_Y), MAP_APPFONT).Height();
     162           0 :     long nFTYPos = nEDYPos + nEDHeight - nFTHeight;
     163             : 
     164           0 :     Link aFocusLink = LINK(this, SwAddressControl_Impl, GotFocusHdl_Impl);
     165           0 :     Link aEditModifyLink = LINK(this, SwAddressControl_Impl, EditModifyHdl_Impl);
     166           0 :     Edit* pLastEdit = 0;
     167           0 :     sal_Int32 nVisibleLines = 0;
     168           0 :     sal_uIntPtr nLines = 0;
     169           0 :     for(aHeaderIter = m_pData->aDBColumnHeaders.begin();
     170           0 :                 aHeaderIter != m_pData->aDBColumnHeaders.end();
     171             :                 ++aHeaderIter, nEDYPos += m_nLineHeight, nFTYPos += m_nLineHeight, nLines++)
     172             :     {
     173           0 :         FixedText* pNewFT = new FixedText(&m_aWindow, WB_RIGHT);
     174           0 :         Edit* pNewED = new Edit(&m_aWindow, WB_BORDER);
     175             :         //set nLines a position identifier - used in the ModifyHdl
     176           0 :         pNewED->SetData((void*)nLines);
     177           0 :         pNewED->SetGetFocusHdl(aFocusLink);
     178           0 :         pNewED->SetModifyHdl(aEditModifyLink);
     179             : 
     180           0 :         pNewFT->SetPosSizePixel(Point(nFTXPos, nFTYPos), Size(nFTWidth, nFTHeight));
     181           0 :         pNewED->SetPosSizePixel(Point(nEDXPos, nEDYPos), Size(nEDWidth, nEDHeight));
     182           0 :         if(nEDYPos + nEDHeight < m_aWinOutputSize.Height())
     183           0 :             ++nVisibleLines;
     184             : 
     185           0 :         pNewFT->SetText(*aHeaderIter);
     186             : 
     187           0 :         pNewFT->Show();
     188           0 :         pNewED->Show();
     189           0 :         m_aFixedTexts.push_back(pNewFT);
     190           0 :         m_aEdits.push_back(pNewED);
     191           0 :         pLastEdit = pNewED;
     192             :     }
     193             :     //scrollbar adjustment
     194           0 :     if(pLastEdit)
     195             :     {
     196             :         //the m_aWindow has to be at least as high as the ScrollBar and it must include the last Edit
     197           0 :         sal_Int32 nContentHeight = pLastEdit->GetPosPixel().Y() + nEDHeight +
     198           0 :                 m_aWindow.LogicToPixel(Size(RSC_SP_CTRL_GROUP_Y, RSC_SP_CTRL_GROUP_Y), MAP_APPFONT).Height();
     199           0 :         if(nContentHeight < m_aScrollBar.GetSizePixel().Height())
     200             :         {
     201           0 :             nContentHeight = m_aScrollBar.GetSizePixel().Height();
     202           0 :             m_aScrollBar.Enable(sal_False);
     203             :         }
     204             :         else
     205             :         {
     206           0 :             m_aScrollBar.Enable(sal_True);
     207           0 :             m_aScrollBar.SetRange(Range(0, nLines));
     208           0 :             m_aScrollBar.SetThumbPos(0);
     209           0 :             m_aScrollBar.SetVisibleSize(nVisibleLines);
     210             :         }
     211           0 :         Size aWinOutputSize(m_aWinOutputSize);
     212           0 :         aWinOutputSize.Height() = nContentHeight;
     213           0 :         m_aWindow.SetOutputSizePixel(aWinOutputSize);
     214             : 
     215             :     }
     216           0 : }
     217             : 
     218           0 : void SwAddressControl_Impl::SetCurrentDataSet(sal_uInt32 nSet)
     219             : {
     220           0 :     if(m_bNoDataSet || m_nCurrentDataSet != nSet)
     221             :     {
     222           0 :         m_bNoDataSet = false;
     223           0 :         m_nCurrentDataSet = nSet;
     224             :         OSL_ENSURE(m_pData->aDBData.size() > m_nCurrentDataSet, "wrong data set index");
     225           0 :         if(m_pData->aDBData.size() > m_nCurrentDataSet)
     226             :         {
     227           0 :             ::std::vector<Edit*>::iterator aEditIter;
     228           0 :             sal_uInt32 nIndex = 0;
     229           0 :             for(aEditIter = m_aEdits.begin(); aEditIter != m_aEdits.end(); ++aEditIter, ++nIndex)
     230             :             {
     231             :                 OSL_ENSURE(nIndex < m_pData->aDBData[m_nCurrentDataSet].size(),
     232             :                             "number of colums doesn't match number of Edits");
     233           0 :                 (*aEditIter)->SetText(m_pData->aDBData[m_nCurrentDataSet][nIndex]);
     234             :             }
     235             :         }
     236             :     }
     237           0 : }
     238             : 
     239           0 : IMPL_LINK(SwAddressControl_Impl, ScrollHdl_Impl, ScrollBar*, pScroll)
     240             : {
     241           0 :     long nThumb = pScroll->GetThumbPos();
     242           0 :     m_aWindow.SetPosPixel(Point(0, - (m_nLineHeight * nThumb)));
     243             : 
     244           0 :     return 0;
     245             : }
     246             : 
     247           0 : IMPL_LINK(SwAddressControl_Impl, GotFocusHdl_Impl, Edit*, pEdit)
     248             : {
     249           0 :     if(0 != (GETFOCUS_TAB & pEdit->GetGetFocusFlags()))
     250             :     {
     251           0 :         Rectangle aRect(pEdit->GetPosPixel(), pEdit->GetSizePixel());
     252           0 :         MakeVisible(aRect);
     253             :     }
     254           0 :     return 0;
     255             : }
     256             : 
     257           0 : void SwAddressControl_Impl::MakeVisible(const Rectangle & rRect)
     258             : {
     259           0 :     long nThumb = m_aScrollBar.GetThumbPos();
     260             :     //determine range of visible positions
     261           0 :     long nMinVisiblePos = - m_aWindow.GetPosPixel().Y();
     262           0 :     long nMaxVisiblePos = m_aScrollBar.GetSizePixel().Height() + nMinVisiblePos;
     263           0 :     if( rRect.TopLeft().Y() < nMinVisiblePos)
     264             :     {
     265           0 :         nThumb -= 1 + ((nMinVisiblePos - rRect.TopLeft().Y()) / m_nLineHeight);
     266             :     }
     267           0 :     else if(rRect.BottomLeft().Y() > nMaxVisiblePos)
     268             :     {
     269           0 :         nThumb += 1 + ((nMaxVisiblePos - rRect.BottomLeft().Y()) / m_nLineHeight);
     270             :     }
     271           0 :     if(nThumb != m_aScrollBar.GetThumbPos())
     272             :     {
     273           0 :         m_aScrollBar.SetThumbPos(nThumb);
     274           0 :         ScrollHdl_Impl(&m_aScrollBar);
     275             :     }
     276           0 : }
     277             : 
     278             : // copy data changes into database
     279           0 : IMPL_LINK(SwAddressControl_Impl, EditModifyHdl_Impl, Edit*, pEdit)
     280             : {
     281             :     //get the data element number of the current set
     282           0 :     sal_Int32 nIndex = (sal_Int32)(sal_IntPtr)pEdit->GetData();
     283             :     //get the index of the set
     284             :     OSL_ENSURE(m_pData->aDBData.size() > m_nCurrentDataSet, "wrong data set index" );
     285           0 :     if(m_pData->aDBData.size() > m_nCurrentDataSet)
     286             :     {
     287           0 :         m_pData->aDBData[m_nCurrentDataSet][nIndex] = pEdit->GetText();
     288             :     }
     289           0 :     return 0;
     290             : }
     291             : 
     292           0 : void SwAddressControl_Impl::SetCursorTo(sal_uInt32 nElement)
     293             : {
     294           0 :     if(nElement < m_aEdits.size())
     295             :     {
     296           0 :         Edit* pEdit = m_aEdits[nElement];
     297           0 :         pEdit->GrabFocus();
     298           0 :         Rectangle aRect(pEdit->GetPosPixel(), pEdit->GetSizePixel());
     299           0 :         MakeVisible(aRect);
     300             :     }
     301             : 
     302           0 : }
     303             : 
     304           0 : void SwAddressControl_Impl::Command( const CommandEvent& rCEvt )
     305             : {
     306           0 :     switch ( rCEvt.GetCommand() )
     307             :     {
     308             :         case COMMAND_WHEEL:
     309             :         case COMMAND_STARTAUTOSCROLL:
     310             :         case COMMAND_AUTOSCROLL:
     311             :         {
     312           0 :             const CommandWheelData* pWheelData = rCEvt.GetWheelData();
     313           0 :             if(pWheelData && !pWheelData->IsHorz() && COMMAND_WHEEL_ZOOM != pWheelData->GetMode())
     314             :             {
     315           0 :                 HandleScrollCommand( rCEvt, 0, &m_aScrollBar );
     316             :             }
     317             :         }
     318           0 :         break;
     319             :         default:
     320           0 :             Control::Command(rCEvt);
     321             :     }
     322           0 : }
     323             : 
     324           0 : long SwAddressControl_Impl::PreNotify( NotifyEvent& rNEvt )
     325             : {
     326           0 :     if(rNEvt.GetType() == EVENT_COMMAND)
     327             :     {
     328           0 :         const CommandEvent* pCEvt = rNEvt.GetCommandEvent();
     329           0 :         sal_uInt16 nCmd = pCEvt->GetCommand();
     330           0 :         if( COMMAND_WHEEL == nCmd )
     331             :         {
     332           0 :             Command(*pCEvt);
     333           0 :             return 1;
     334             :         }
     335             :     }
     336           0 :     return Control::PreNotify(rNEvt);
     337             : }
     338             : 
     339           0 : SwCreateAddressListDialog::SwCreateAddressListDialog(
     340             :         Window* pParent, const String& rURL, SwMailMergeConfigItem& rConfig) :
     341             :     SfxModalDialog(pParent, SW_RES(DLG_MM_CREATEADDRESSLIST)),
     342             : #ifdef MSC
     343             : #pragma warning (disable : 4355)
     344             : #endif
     345             :     m_aAddressInformation( this, SW_RES(  FI_ADDRESSINFORMATION)),
     346           0 :     m_pAddressControl(new SwAddressControl_Impl(this, SW_RES(CT_ADDRESS))),
     347             :     m_aNewPB( this, SW_RES(               PB_NEW)),
     348             :     m_aDeletePB( this, SW_RES(            PB_DELETE)),
     349             :     m_aFindPB( this, SW_RES(              PB_FIND)),
     350             :     m_aCustomizePB( this, SW_RES(         PB_CUSTOMIZE)),
     351             : 
     352             :     m_aViewEntriesFI( this, SW_RES(       FI_VIEWENTRIES)),
     353             :     m_aStartPB( this, SW_RES(             PB_START)),
     354             :     m_aPrevPB( this, SW_RES(              PB_PREV)),
     355             :     m_aSetNoNF( this, SW_RES(             NF_SETNO)),
     356             :     m_aNextPB( this, SW_RES(              PB_NEXT  )),
     357             :     m_aEndPB( this, SW_RES(               PB_END)),
     358             : 
     359             :     m_aSeparatorFL( this, SW_RES(         FL_SEPARATOR)),
     360             : 
     361             :     m_aOK( this, SW_RES(                  PB_OK)),
     362             :     m_aCancel( this, SW_RES(              PB_CANCEL)),
     363             :     m_aHelp( this, SW_RES(                PB_HELP)),
     364             : #ifdef MSC
     365             : #pragma warning (default : 4355)
     366             : #endif
     367             :     m_sAddressListFilterName( SW_RES(    ST_FILTERNAME)),
     368             :     m_sURL(rURL),
     369           0 :     m_pCSVData( new SwCSVData ),
     370           0 :     m_pFindDlg(0)
     371             : {
     372           0 :     FreeResource();
     373           0 :     m_aNewPB.SetClickHdl(LINK(this, SwCreateAddressListDialog, NewHdl_Impl));
     374           0 :     m_aDeletePB.SetClickHdl(LINK(this, SwCreateAddressListDialog, DeleteHdl_Impl));
     375           0 :     m_aFindPB.SetClickHdl(LINK(this, SwCreateAddressListDialog, FindHdl_Impl));
     376           0 :     m_aCustomizePB.SetClickHdl(LINK(this, SwCreateAddressListDialog, CustomizeHdl_Impl));
     377           0 :     m_aOK.SetClickHdl(LINK(this, SwCreateAddressListDialog, OkHdl_Impl));
     378             : 
     379           0 :     Link aLk = LINK(this, SwCreateAddressListDialog, DBCursorHdl_Impl);
     380           0 :     m_aStartPB.SetClickHdl(aLk);
     381           0 :     m_aPrevPB.SetClickHdl(aLk);
     382           0 :     m_aSetNoNF.SetModifyHdl(LINK(this, SwCreateAddressListDialog, DBNumCursorHdl_Impl));
     383           0 :     m_aNextPB.SetClickHdl(aLk);
     384           0 :     m_aEndPB.SetClickHdl(aLk);
     385             : 
     386           0 :     if(m_sURL.Len())
     387             :     {
     388             :         //file exists, has to be loaded here
     389           0 :         SfxMedium aMedium( m_sURL, STREAM_READ );
     390           0 :         SvStream* pStream = aMedium.GetInStream();
     391           0 :         if(pStream)
     392             :         {
     393           0 :             pStream->SetLineDelimiter( LINEEND_LF );
     394           0 :             pStream->SetStreamCharSet(RTL_TEXTENCODING_UTF8);
     395             : 
     396           0 :             OUString sQuote('"');
     397           0 :             String sTempMiddle(sQuote);
     398           0 :             sTempMiddle += sal_Unicode(9);
     399             : 
     400           0 :             String sLine;
     401           0 :             sal_Bool bRead = pStream->ReadByteStringLine( sLine, RTL_TEXTENCODING_UTF8 );
     402             : 
     403           0 :             if(bRead)
     404             :             {
     405             :                 //header line
     406           0 :                 xub_StrLen nHeaders = comphelper::string::getTokenCount(sLine, '\t');
     407           0 :                 xub_StrLen nIndex = 0;
     408           0 :                 for( xub_StrLen nToken = 0; nToken < nHeaders; ++nToken)
     409             :                 {
     410           0 :                     String sHeader = sLine.GetToken( 0, '\t', nIndex );
     411             :                     OSL_ENSURE(sHeader.Len() > 2 &&
     412             :                             sHeader.GetChar(0) == '\"' && sHeader.GetChar(sHeader.Len() - 1) == '\"',
     413             :                             "Wrong format of header");
     414           0 :                     if(sHeader.Len() > 2)
     415             :                     {
     416           0 :                         m_pCSVData->aDBColumnHeaders.push_back( sHeader.Copy(1, sHeader.Len() -2));
     417             :                     }
     418           0 :                 }
     419             :             }
     420           0 :             while(pStream->ReadByteStringLine( sLine, RTL_TEXTENCODING_UTF8 ))
     421             :             {
     422           0 :                 ::std::vector<OUString> aNewData;
     423             :                 //analyze data line
     424           0 :                 xub_StrLen nDataCount = comphelper::string::getTokenCount(sLine, '\t');
     425           0 :                 xub_StrLen nIndex = 0;
     426           0 :                 for( xub_StrLen nToken = 0; nToken < nDataCount; ++nToken)
     427             :                 {
     428           0 :                     String sData = sLine.GetToken( 0, '\t', nIndex );
     429             :                     OSL_ENSURE(sData.Len() >= 2 &&
     430             :                                 sData.GetChar(0) == '\"' && sData.GetChar(sData.Len() - 1) == '\"',
     431             :                             "Wrong format of line");
     432           0 :                     if(sData.Len() >= 2)
     433           0 :                         aNewData.push_back(sData.Copy(1, sData.Len() - 2));
     434             :                     else
     435           0 :                         aNewData.push_back(sData);
     436           0 :                 }
     437           0 :                 m_pCSVData->aDBData.push_back( aNewData );
     438           0 :             }
     439           0 :         }
     440             :     }
     441             :     else
     442             :     {
     443             :         //database has to be created
     444           0 :         const ResStringArray& rAddressHeader = rConfig.GetDefaultAddressHeaders();
     445           0 :         sal_uInt32 nCount = rAddressHeader.Count();
     446           0 :         for(sal_uInt16 nHeader = 0; nHeader < nCount; ++nHeader)
     447           0 :             m_pCSVData->aDBColumnHeaders.push_back( rAddressHeader.GetString(nHeader));
     448           0 :         ::std::vector<OUString> aNewData;
     449           0 :         String sTemp;
     450           0 :         aNewData.insert(aNewData.begin(), nCount, sTemp);
     451           0 :         m_pCSVData->aDBData.push_back(aNewData);
     452             :     }
     453             :     //now fill the address control
     454           0 :     m_pAddressControl->SetData(*m_pCSVData);
     455           0 :     m_pAddressControl->SetCurrentDataSet(0);
     456           0 :     m_aSetNoNF.SetMax(m_pCSVData->aDBData.size());
     457           0 :     UpdateButtons();
     458           0 : }
     459             : 
     460           0 : SwCreateAddressListDialog::~SwCreateAddressListDialog()
     461             : {
     462           0 :     delete m_pAddressControl;
     463           0 :     delete m_pCSVData;
     464           0 :     delete m_pFindDlg;
     465           0 : }
     466             : 
     467           0 : IMPL_LINK_NOARG(SwCreateAddressListDialog, NewHdl_Impl)
     468             : {
     469           0 :     sal_uInt32 nCurrent = m_pAddressControl->GetCurrentDataSet();
     470           0 :     ::std::vector<OUString> aNewData;
     471           0 :     String sTemp;
     472           0 :     aNewData.insert(aNewData.begin(), m_pCSVData->aDBColumnHeaders.size(), sTemp);
     473           0 :     m_pCSVData->aDBData.insert(m_pCSVData->aDBData.begin() + ++nCurrent, aNewData);
     474           0 :     m_aSetNoNF.SetMax(m_pCSVData->aDBData.size());
     475             :     //the NumericField start at 1
     476           0 :     m_aSetNoNF.SetValue(nCurrent + 1);
     477             :     //the address control starts at 0
     478           0 :     m_pAddressControl->SetCurrentDataSet(nCurrent);
     479           0 :     UpdateButtons();
     480           0 :     return 0;
     481             : }
     482             : 
     483           0 : IMPL_LINK_NOARG(SwCreateAddressListDialog, DeleteHdl_Impl)
     484             : {
     485           0 :     sal_uInt32 nCurrent = m_pAddressControl->GetCurrentDataSet();
     486           0 :     if(m_pCSVData->aDBData.size() > 1)
     487             :     {
     488           0 :         m_pCSVData->aDBData.erase(m_pCSVData->aDBData.begin() + nCurrent);
     489           0 :         if(nCurrent)
     490           0 :             --nCurrent;
     491             :     }
     492             :     else
     493             :     {
     494             :         // if only one set is available then clear the data
     495           0 :         String sTemp;
     496           0 :         m_pCSVData->aDBData[0].assign(m_pCSVData->aDBData[0].size(), sTemp);
     497           0 :         m_aDeletePB.Enable(sal_False);
     498             :     }
     499           0 :     m_pAddressControl->SetCurrentDataSet(nCurrent);
     500           0 :     m_aSetNoNF.SetMax(m_pCSVData->aDBData.size());
     501           0 :     UpdateButtons();
     502           0 :     return 0;
     503             : }
     504             : 
     505           0 : IMPL_LINK_NOARG(SwCreateAddressListDialog, FindHdl_Impl)
     506             : {
     507           0 :     if(!m_pFindDlg)
     508             :     {
     509           0 :         m_pFindDlg = new SwFindEntryDialog(this);
     510           0 :         ListBox& rColumnBox = m_pFindDlg->GetFieldsListBox();
     511           0 :         ::std::vector< OUString >::iterator  aHeaderIter;
     512           0 :         for(aHeaderIter = m_pCSVData->aDBColumnHeaders.begin();
     513           0 :                     aHeaderIter != m_pCSVData->aDBColumnHeaders.end();
     514             :                     ++aHeaderIter)
     515           0 :             rColumnBox.InsertEntry(*aHeaderIter);
     516           0 :         rColumnBox.SelectEntryPos( 0 );
     517             :     }
     518             :     else
     519           0 :         m_pFindDlg->Show(!m_pFindDlg->IsVisible());
     520           0 :     return 0;
     521             : }
     522             : 
     523           0 : IMPL_LINK(SwCreateAddressListDialog, CustomizeHdl_Impl, PushButton*, pButton)
     524             : {
     525           0 :     SwCustomizeAddressListDialog* pDlg = new SwCustomizeAddressListDialog(pButton, *m_pCSVData);
     526           0 :     if(RET_OK == pDlg->Execute())
     527             :     {
     528           0 :         delete m_pCSVData;
     529           0 :         m_pCSVData = pDlg->GetNewData();
     530           0 :         m_pAddressControl->SetData(*m_pCSVData);
     531           0 :         m_pAddressControl->SetCurrentDataSet(m_pAddressControl->GetCurrentDataSet());
     532             :     }
     533           0 :     delete pDlg;
     534             : 
     535             :     //update find dialog
     536           0 :     if(m_pFindDlg)
     537             :     {
     538           0 :         ListBox& rColumnBox = m_pFindDlg->GetFieldsListBox();
     539           0 :         rColumnBox.Clear();
     540           0 :         ::std::vector< OUString >::iterator  aHeaderIter;
     541           0 :         for(aHeaderIter = m_pCSVData->aDBColumnHeaders.begin();
     542           0 :                     aHeaderIter != m_pCSVData->aDBColumnHeaders.end();
     543             :                     ++aHeaderIter)
     544           0 :             rColumnBox.InsertEntry(*aHeaderIter);
     545             :     }
     546           0 :     return 0;
     547             : }
     548             : 
     549           0 : IMPL_LINK_NOARG(SwCreateAddressListDialog, OkHdl_Impl)
     550             : {
     551           0 :     if(!m_sURL.Len())
     552             :     {
     553           0 :         sfx2::FileDialogHelper aDlgHelper( TemplateDescription::FILESAVE_SIMPLE, 0 );
     554           0 :         uno::Reference < XFilePicker > xFP = aDlgHelper.GetFilePicker();
     555             : 
     556             :         String sPath( SvtPathOptions().SubstituteVariable(
     557           0 :                     rtl::OUString("$(userurl)/database") ));
     558           0 :         aDlgHelper.SetDisplayDirectory( sPath );
     559           0 :         uno::Reference< XFilterManager > xFltMgr(xFP, uno::UNO_QUERY);
     560           0 :         ::rtl::OUString sCSV("*.csv");
     561           0 :         xFltMgr->appendFilter( m_sAddressListFilterName, sCSV );
     562           0 :         xFltMgr->setCurrentFilter( m_sAddressListFilterName ) ;
     563             : 
     564           0 :         if( ERRCODE_NONE == aDlgHelper.Execute() )
     565             :         {
     566           0 :             m_sURL = xFP->getFiles().getConstArray()[0];
     567           0 :             INetURLObject aResult( m_sURL );
     568           0 :             aResult.setExtension(rtl::OUString("csv"));
     569           0 :             m_sURL = aResult.GetMainURL(INetURLObject::NO_DECODE);
     570           0 :         }
     571             :     }
     572           0 :     if(m_sURL.Len())
     573             :     {
     574           0 :         SfxMedium aMedium( m_sURL, STREAM_READWRITE|STREAM_TRUNC );
     575           0 :         SvStream* pStream = aMedium.GetOutStream();
     576           0 :         pStream->SetLineDelimiter( LINEEND_LF );
     577           0 :         pStream->SetStreamCharSet(RTL_TEXTENCODING_UTF8);
     578             : 
     579           0 :         OUString sQuote('"');
     580           0 :         String sTempMiddle(sQuote);
     581           0 :         sTempMiddle += sal_Unicode(9);
     582           0 :         OUString sMiddle(sTempMiddle);
     583           0 :         sMiddle += sQuote;
     584             : 
     585             :         //create a string for the header line
     586           0 :         OUString sLine(sQuote);
     587           0 :         ::std::vector< OUString >::iterator  aHeaderIter;
     588           0 :         for(aHeaderIter = m_pCSVData->aDBColumnHeaders.begin();
     589           0 :                     aHeaderIter != m_pCSVData->aDBColumnHeaders.end();
     590             :                     ++aHeaderIter)
     591             :         {
     592           0 :             sLine += *aHeaderIter;
     593           0 :             sLine += sMiddle;
     594             :         }
     595             :         //remove tab and quote
     596           0 :         sLine = sLine.copy( 0, sLine.getLength() - 2 );
     597           0 :         pStream->WriteByteStringLine( sLine, RTL_TEXTENCODING_UTF8 );
     598             : 
     599           0 :         ::std::vector< ::std::vector< OUString > >::iterator aDataIter;
     600           0 :         for( aDataIter = m_pCSVData->aDBData.begin(); aDataIter != m_pCSVData->aDBData.end(); ++aDataIter)
     601             :         {
     602           0 :             sLine = sQuote;
     603           0 :             ::std::vector< OUString >::iterator  aColumnIter;
     604           0 :             for(aColumnIter = aDataIter->begin(); aColumnIter != aDataIter->end(); ++aColumnIter)
     605             :             {
     606           0 :                 sLine += *aColumnIter;
     607           0 :                 sLine += sMiddle;
     608             :             }
     609             :             //remove tab and quote
     610           0 :             sLine = sLine.copy( 0, sLine.getLength() - 2 );
     611           0 :             pStream->WriteByteStringLine( sLine, RTL_TEXTENCODING_UTF8 );
     612             :         }
     613           0 :         aMedium.Commit();
     614           0 :         EndDialog(RET_OK);
     615             :     }
     616             : 
     617           0 :     return 0;
     618             : }
     619             : 
     620           0 : IMPL_LINK(SwCreateAddressListDialog, DBCursorHdl_Impl, PushButton*, pButton)
     621             : {
     622           0 :     sal_uInt32 nValue = static_cast< sal_uInt32 >(m_aSetNoNF.GetValue());
     623             : 
     624           0 :     if(pButton == &m_aStartPB)
     625           0 :         nValue = 1;
     626           0 :     else if(pButton == &m_aPrevPB)
     627             :     {
     628           0 :         if(nValue > 1)
     629           0 :             --nValue;
     630             :     }
     631           0 :     else if(pButton == &m_aNextPB)
     632             :     {
     633           0 :         if(nValue < (sal_uInt32)m_aSetNoNF.GetMax())
     634           0 :             ++nValue;
     635             :     }
     636             :     else //m_aEndPB
     637           0 :         nValue = static_cast< sal_uInt32 >(m_aSetNoNF.GetMax());
     638           0 :     if(nValue != m_aSetNoNF.GetValue())
     639             :     {
     640           0 :         m_aSetNoNF.SetValue(nValue);
     641           0 :         DBNumCursorHdl_Impl(&m_aSetNoNF);
     642             :     }
     643           0 :     return 0;
     644             : }
     645             : 
     646           0 : IMPL_LINK_NOARG(SwCreateAddressListDialog, DBNumCursorHdl_Impl)
     647             : {
     648           0 :     m_pAddressControl->SetCurrentDataSet( static_cast< sal_uInt32 >(m_aSetNoNF.GetValue() - 1) );
     649           0 :     UpdateButtons();
     650           0 :     return 0;
     651             : }
     652             : 
     653           0 : void SwCreateAddressListDialog::UpdateButtons()
     654             : {
     655           0 :     sal_uInt32 nCurrent = static_cast< sal_uInt32 >(m_aSetNoNF.GetValue() );
     656           0 :     sal_uInt32 nSize = (sal_uInt32 )m_pCSVData->aDBData.size();
     657           0 :     m_aStartPB.Enable(nCurrent != 1);
     658           0 :     m_aPrevPB.Enable(nCurrent != 1);
     659           0 :     m_aNextPB.Enable(nCurrent != nSize);
     660           0 :     m_aEndPB.Enable(nCurrent != nSize);
     661           0 :     m_aDeletePB.Enable(nSize > 0);
     662           0 : }
     663             : 
     664           0 : void SwCreateAddressListDialog::Find(const String& rSearch, sal_Int32 nColumn)
     665             : {
     666           0 :     OUString sSearch = OUString(rSearch).toAsciiLowerCase();
     667           0 :     sal_uInt32 nCurrent = m_pAddressControl->GetCurrentDataSet();
     668             :     //search forward
     669           0 :     bool bFound = false;
     670           0 :     sal_uInt32 nStart = nCurrent + 1;
     671           0 :     sal_uInt32 nEnd = m_pCSVData->aDBData.size();
     672           0 :     sal_uInt32 nElement = 0;
     673           0 :     sal_uInt32 nPos = 0;
     674           0 :     for(short nTemp = 0; nTemp < 2 && !bFound; nTemp++)
     675             :     {
     676           0 :         for(nPos = nStart; nPos < nEnd; ++nPos)
     677             :         {
     678           0 :             ::std::vector< OUString> aData = m_pCSVData->aDBData[nPos];
     679           0 :             if(nColumn >=0)
     680           0 :                 bFound = -1 != aData[(sal_uInt32)nColumn].toAsciiLowerCase().indexOf(sSearch);
     681             :             else
     682             :             {
     683           0 :                 for( nElement = 0; nElement < aData.size(); ++nElement)
     684             :                 {
     685           0 :                     bFound = -1 != aData[nElement].toAsciiLowerCase().indexOf(sSearch);
     686           0 :                     if(bFound)
     687             :                     {
     688           0 :                         nColumn = nElement;
     689           0 :                         break;
     690             :                     }
     691             :                 }
     692             :             }
     693           0 :             if(bFound)
     694             :                 break;
     695           0 :         }
     696           0 :         nStart = 0;
     697           0 :         nEnd = nCurrent + 1;
     698             :     }
     699           0 :     if(bFound)
     700             :     {
     701           0 :         m_pAddressControl->SetCurrentDataSet(nPos);
     702           0 :         m_aSetNoNF.SetValue( nPos + 1 );
     703           0 :         UpdateButtons();
     704           0 :         m_pAddressControl->SetCursorTo(nElement);
     705           0 :     }
     706           0 : }
     707             : 
     708           0 : SwFindEntryDialog::SwFindEntryDialog(SwCreateAddressListDialog* pParent) :
     709             :     ModelessDialog(pParent, SW_RES(DLG_MM_FIND_ENTRY)),
     710             : #ifdef MSC
     711             : #pragma warning (disable : 4355)
     712             : #endif
     713             :     m_aFindFT( this, SW_RES(      FT_FIND      )),
     714             :     m_aFindED( this, SW_RES(      ED_FIND      )),
     715             :     m_aFindOnlyCB( this, SW_RES(  CB_FINDONLY )),
     716             :     m_aFindOnlyLB( this, SW_RES(  LB_FINDONLY  )),
     717             :     m_aFindPB( this, SW_RES(      PB_FIND)),
     718             :     m_aCancel( this, SW_RES(      PB_CANCEL)),
     719             :     m_aHelp( this, SW_RES(        PB_HELP)),
     720             : #ifdef MSC
     721             : #pragma warning (default : 4355)
     722             : #endif
     723           0 :    m_pParent(pParent)
     724             : {
     725           0 :     FreeResource();
     726           0 :     m_aFindPB.SetClickHdl(LINK(this, SwFindEntryDialog, FindHdl_Impl));
     727           0 :     m_aFindED.SetModifyHdl(LINK(this, SwFindEntryDialog, FindEnableHdl_Impl));
     728           0 :     m_aCancel.SetClickHdl(LINK(this, SwFindEntryDialog, CloseHdl_Impl));
     729           0 : }
     730             : 
     731           0 : SwFindEntryDialog::~SwFindEntryDialog()
     732             : {
     733           0 : }
     734             : 
     735           0 : IMPL_LINK_NOARG(SwFindEntryDialog, FindHdl_Impl)
     736             : {
     737           0 :     sal_Int32 nColumn = -1;
     738           0 :     if(m_aFindOnlyCB.IsChecked())
     739           0 :         nColumn = m_aFindOnlyLB.GetSelectEntryPos();
     740           0 :     if(nColumn != LISTBOX_ENTRY_NOTFOUND)
     741           0 :         m_pParent->Find(m_aFindED.GetText(), nColumn);
     742           0 :     return 0;
     743             : }
     744             : 
     745           0 : IMPL_LINK_NOARG(SwFindEntryDialog, FindEnableHdl_Impl)
     746             : {
     747           0 :     m_aFindPB.Enable(m_aFindED.GetText().Len() > 0);
     748           0 :     return 0;
     749             : }
     750             : 
     751           0 : IMPL_LINK_NOARG(SwFindEntryDialog, CloseHdl_Impl)
     752             : {
     753           0 :     Show(sal_False);
     754           0 :     return 0;
     755             : }
     756             : 
     757             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10