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