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 <dbui.hrc>
40 : #include <helpid.h>
41 : #include <unomid.h>
42 : #include <boost/scoped_ptr.hpp>
43 :
44 : using namespace ::com::sun::star;
45 : using namespace ::com::sun::star::ui::dialogs;
46 :
47 : class SwAddressControl_Impl : public Control
48 : {
49 : ScrollBar *m_pScrollBar;
50 : Window *m_pWindow;
51 :
52 : ::std::vector<FixedText*> m_aFixedTexts;
53 : ::std::vector<Edit*> m_aEdits;
54 :
55 : SwCSVData* m_pData;
56 : Size m_aWinOutputSize;
57 : sal_Int32 m_nLineHeight;
58 : sal_uInt32 m_nCurrentDataSet;
59 :
60 : bool m_bNoDataSet;
61 :
62 : DECL_LINK(ScrollHdl_Impl, ScrollBar*);
63 : DECL_LINK(GotFocusHdl_Impl, Edit*);
64 : DECL_LINK(EditModifyHdl_Impl, Edit*);
65 :
66 : void MakeVisible(const Rectangle& aRect);
67 :
68 : virtual bool PreNotify( NotifyEvent& rNEvt ) SAL_OVERRIDE;
69 : virtual void Command( const CommandEvent& rCEvt ) SAL_OVERRIDE;
70 : virtual Size GetOptimalSize() const SAL_OVERRIDE;
71 :
72 : using Window::SetData;
73 :
74 : public:
75 : SwAddressControl_Impl(vcl::Window* pParent , WinBits nBits );
76 : virtual ~SwAddressControl_Impl();
77 :
78 : void SetData(SwCSVData& rDBData);
79 :
80 : void SetCurrentDataSet(sal_uInt32 nSet);
81 0 : sal_uInt32 GetCurrentDataSet() const { return m_nCurrentDataSet;}
82 : void SetCursorTo(sal_uInt32 nElement);
83 : virtual void Resize() SAL_OVERRIDE;
84 : };
85 :
86 0 : SwAddressControl_Impl::SwAddressControl_Impl(vcl::Window* pParent, WinBits nBits ) :
87 : Control(pParent, nBits),
88 0 : m_pScrollBar(new ScrollBar(this)),
89 0 : m_pWindow(new vcl::Window(this, WB_DIALOGCONTROL)),
90 : m_pData(0),
91 : m_nLineHeight(0),
92 : m_nCurrentDataSet(0),
93 0 : m_bNoDataSet(true)
94 : {
95 0 : long nScrollBarWidth = m_pScrollBar->GetOutputSize().Width();
96 0 : Size aSize = GetOutputSizePixel();
97 :
98 0 : m_pWindow->SetSizePixel(Size(aSize.Width() - nScrollBarWidth, aSize.Height()));
99 0 : m_aWinOutputSize = m_pWindow->GetOutputSizePixel();
100 0 : m_pWindow->Show();
101 0 : m_pScrollBar->Show();
102 :
103 0 : Link aScrollLink = LINK(this, SwAddressControl_Impl, ScrollHdl_Impl);
104 0 : m_pScrollBar->SetScrollHdl(aScrollLink);
105 0 : m_pScrollBar->SetEndScrollHdl(aScrollLink);
106 0 : m_pScrollBar->EnableDrag();
107 0 : }
108 :
109 0 : extern "C" SAL_DLLPUBLIC_EXPORT vcl::Window* SAL_CALL makeSwAddressControlImpl(vcl::Window *pParent, VclBuilder::stringmap &)
110 : {
111 0 : return new SwAddressControl_Impl(pParent, WB_BORDER | WB_DIALOGCONTROL);
112 : }
113 :
114 0 : SwAddressControl_Impl::~SwAddressControl_Impl()
115 : {
116 0 : ::std::vector<FixedText*>::iterator aTextIter;
117 0 : for(aTextIter = m_aFixedTexts.begin(); aTextIter != m_aFixedTexts.end(); ++aTextIter)
118 0 : delete *aTextIter;
119 0 : ::std::vector<Edit*>::iterator aEditIter;
120 0 : for(aEditIter = m_aEdits.begin(); aEditIter != m_aEdits.end(); ++aEditIter)
121 0 : delete *aEditIter;
122 0 : delete m_pScrollBar;
123 0 : delete m_pWindow;
124 0 : }
125 :
126 0 : void SwAddressControl_Impl::SetData(SwCSVData& rDBData)
127 : {
128 0 : m_pData = &rDBData;
129 : //when the address data is updated then remove the controls an build again
130 0 : if(m_aFixedTexts.size())
131 : {
132 0 : ::std::vector<FixedText*>::iterator aTextIter;
133 0 : for(aTextIter = m_aFixedTexts.begin(); aTextIter != m_aFixedTexts.end(); ++aTextIter)
134 0 : delete *aTextIter;
135 0 : ::std::vector<Edit*>::iterator aEditIter;
136 0 : for(aEditIter = m_aEdits.begin(); aEditIter != m_aEdits.end(); ++aEditIter)
137 0 : delete *aEditIter;
138 0 : m_aFixedTexts.clear();
139 0 : m_aEdits.clear();
140 0 : m_bNoDataSet = true;
141 : }
142 : //now create appropriate controls
143 :
144 0 : ::std::vector< OUString >::iterator aHeaderIter;
145 :
146 0 : long nFTXPos = m_pWindow->LogicToPixel(Point(RSC_SP_CTRL_X, RSC_SP_CTRL_X), MAP_APPFONT).X();
147 0 : long nFTHeight = m_pWindow->LogicToPixel(Size(RSC_BS_CHARHEIGHT, RSC_BS_CHARHEIGHT), MAP_APPFONT).Height();
148 0 : long nFTWidth = 0;
149 :
150 : //determine the width of the FixedTexts
151 0 : for(aHeaderIter = m_pData->aDBColumnHeaders.begin();
152 0 : aHeaderIter != m_pData->aDBColumnHeaders.end();
153 : ++aHeaderIter)
154 : {
155 0 : sal_Int32 nTemp = m_pWindow->GetTextWidth(*aHeaderIter);
156 0 : if(nTemp > nFTWidth)
157 0 : nFTWidth = nTemp;
158 : }
159 : //add some pixels
160 0 : nFTWidth += 2;
161 0 : long nEDXPos = nFTWidth + nFTXPos +
162 0 : m_pWindow->LogicToPixel(Size(RSC_SP_CTRL_DESC_X, RSC_SP_CTRL_DESC_X), MAP_APPFONT).Width();
163 0 : long nEDHeight = m_pWindow->LogicToPixel(Size(RSC_CD_TEXTBOX_HEIGHT, RSC_CD_TEXTBOX_HEIGHT), MAP_APPFONT).Height();
164 0 : long nEDWidth = m_aWinOutputSize.Width() - nEDXPos - nFTXPos;
165 0 : m_nLineHeight = nEDHeight + m_pWindow->LogicToPixel(Size(RSC_SP_CTRL_GROUP_Y, RSC_SP_CTRL_GROUP_Y), MAP_APPFONT).Height();
166 :
167 0 : long nEDYPos = m_pWindow->LogicToPixel(Size(RSC_SP_CTRL_DESC_Y, RSC_SP_CTRL_DESC_Y), MAP_APPFONT).Height();
168 0 : long nFTYPos = nEDYPos + nEDHeight - nFTHeight;
169 :
170 0 : Link aFocusLink = LINK(this, SwAddressControl_Impl, GotFocusHdl_Impl);
171 0 : Link aEditModifyLink = LINK(this, SwAddressControl_Impl, EditModifyHdl_Impl);
172 0 : Edit* pLastEdit = 0;
173 0 : sal_Int32 nVisibleLines = 0;
174 0 : sal_uIntPtr nLines = 0;
175 0 : for(aHeaderIter = m_pData->aDBColumnHeaders.begin();
176 0 : aHeaderIter != m_pData->aDBColumnHeaders.end();
177 : ++aHeaderIter, nEDYPos += m_nLineHeight, nFTYPos += m_nLineHeight, nLines++)
178 : {
179 0 : FixedText* pNewFT = new FixedText(m_pWindow, WB_RIGHT);
180 0 : Edit* pNewED = new Edit(m_pWindow, WB_BORDER);
181 : //set nLines a position identifier - used in the ModifyHdl
182 0 : pNewED->SetData((void*)nLines);
183 0 : pNewED->SetGetFocusHdl(aFocusLink);
184 0 : pNewED->SetModifyHdl(aEditModifyLink);
185 :
186 0 : pNewFT->SetPosSizePixel(Point(nFTXPos, nFTYPos), Size(nFTWidth, nFTHeight));
187 0 : pNewED->SetPosSizePixel(Point(nEDXPos, nEDYPos), Size(nEDWidth, nEDHeight));
188 0 : if(nEDYPos + nEDHeight < m_aWinOutputSize.Height())
189 0 : ++nVisibleLines;
190 :
191 0 : pNewFT->SetText(*aHeaderIter);
192 :
193 0 : pNewFT->Show();
194 0 : pNewED->Show();
195 0 : m_aFixedTexts.push_back(pNewFT);
196 0 : m_aEdits.push_back(pNewED);
197 0 : pLastEdit = pNewED;
198 : }
199 : //scrollbar adjustment
200 0 : if(pLastEdit)
201 : {
202 : //the m_aWindow has to be at least as high as the ScrollBar and it must include the last Edit
203 0 : sal_Int32 nContentHeight = pLastEdit->GetPosPixel().Y() + nEDHeight +
204 0 : m_pWindow->LogicToPixel(Size(RSC_SP_CTRL_GROUP_Y, RSC_SP_CTRL_GROUP_Y), MAP_APPFONT).Height();
205 0 : if(nContentHeight < m_pScrollBar->GetSizePixel().Height())
206 : {
207 0 : nContentHeight = m_pScrollBar->GetSizePixel().Height();
208 : // Reset the scrollbar's thumb to the top before it is disabled.
209 0 : m_pScrollBar->DoScroll(0);
210 0 : m_pScrollBar->SetThumbPos(0);
211 0 : m_pScrollBar->Enable(false);
212 : }
213 : else
214 : {
215 0 : m_pScrollBar->Enable(true);
216 0 : m_pScrollBar->SetRange(Range(0, nLines));
217 0 : m_pScrollBar->SetThumbPos(0);
218 0 : m_pScrollBar->SetVisibleSize(nVisibleLines);
219 : // Reset the scroll bar position (especially if items deleted)
220 0 : m_pScrollBar->DoScroll(m_pScrollBar->GetRangeMax());
221 0 : m_pScrollBar->DoScroll(0);
222 : }
223 0 : Size aWinOutputSize(m_aWinOutputSize);
224 0 : aWinOutputSize.Height() = nContentHeight;
225 0 : m_pWindow->SetOutputSizePixel(aWinOutputSize);
226 :
227 : }
228 : // Even if no items in m_aEdits, the scrollbar will still exist;
229 : // we might as well disable it.
230 0 : if (m_aEdits.size() < 1) {
231 0 : m_pScrollBar->DoScroll(0);
232 0 : m_pScrollBar->SetThumbPos(0);
233 0 : m_pScrollBar->Enable(false);
234 : }
235 0 : Resize();
236 0 : }
237 :
238 0 : void SwAddressControl_Impl::SetCurrentDataSet(sal_uInt32 nSet)
239 : {
240 0 : if(m_bNoDataSet || m_nCurrentDataSet != nSet)
241 : {
242 0 : m_bNoDataSet = false;
243 0 : m_nCurrentDataSet = nSet;
244 : OSL_ENSURE(m_pData->aDBData.size() > m_nCurrentDataSet, "wrong data set index");
245 0 : if(m_pData->aDBData.size() > m_nCurrentDataSet)
246 : {
247 0 : ::std::vector<Edit*>::iterator aEditIter;
248 0 : sal_uInt32 nIndex = 0;
249 0 : for(aEditIter = m_aEdits.begin(); aEditIter != m_aEdits.end(); ++aEditIter, ++nIndex)
250 : {
251 : OSL_ENSURE(nIndex < m_pData->aDBData[m_nCurrentDataSet].size(),
252 : "number of columns doesn't match number of Edits");
253 0 : (*aEditIter)->SetText(m_pData->aDBData[m_nCurrentDataSet][nIndex]);
254 : }
255 : }
256 : }
257 0 : }
258 :
259 0 : IMPL_LINK(SwAddressControl_Impl, ScrollHdl_Impl, ScrollBar*, pScroll)
260 : {
261 0 : long nThumb = pScroll->GetThumbPos();
262 0 : m_pWindow->SetPosPixel(Point(0, - (m_nLineHeight * nThumb)));
263 :
264 0 : return 0;
265 : }
266 :
267 0 : IMPL_LINK(SwAddressControl_Impl, GotFocusHdl_Impl, Edit*, pEdit)
268 : {
269 0 : if(0 != (GETFOCUS_TAB & pEdit->GetGetFocusFlags()))
270 : {
271 0 : Rectangle aRect(pEdit->GetPosPixel(), pEdit->GetSizePixel());
272 0 : MakeVisible(aRect);
273 : }
274 0 : return 0;
275 : }
276 :
277 0 : void SwAddressControl_Impl::MakeVisible(const Rectangle & rRect)
278 : {
279 0 : long nThumb = m_pScrollBar->GetThumbPos();
280 : //determine range of visible positions
281 0 : long nMinVisiblePos = - m_pWindow->GetPosPixel().Y();
282 0 : long nMaxVisiblePos = m_pScrollBar->GetSizePixel().Height() + nMinVisiblePos;
283 0 : if( rRect.TopLeft().Y() < nMinVisiblePos)
284 : {
285 0 : nThumb -= 1 + ((nMinVisiblePos - rRect.TopLeft().Y()) / m_nLineHeight);
286 : }
287 0 : else if(rRect.BottomLeft().Y() > nMaxVisiblePos)
288 : {
289 0 : nThumb += 1 + ((nMaxVisiblePos - rRect.BottomLeft().Y()) / m_nLineHeight);
290 : }
291 0 : if(nThumb != m_pScrollBar->GetThumbPos())
292 : {
293 0 : m_pScrollBar->SetThumbPos(nThumb);
294 0 : ScrollHdl_Impl(m_pScrollBar);
295 : }
296 0 : }
297 :
298 : // copy data changes into database
299 0 : IMPL_LINK(SwAddressControl_Impl, EditModifyHdl_Impl, Edit*, pEdit)
300 : {
301 : //get the data element number of the current set
302 0 : sal_Int32 nIndex = (sal_Int32)(sal_IntPtr)pEdit->GetData();
303 : //get the index of the set
304 : OSL_ENSURE(m_pData->aDBData.size() > m_nCurrentDataSet, "wrong data set index" );
305 0 : if(m_pData->aDBData.size() > m_nCurrentDataSet)
306 : {
307 0 : m_pData->aDBData[m_nCurrentDataSet][nIndex] = pEdit->GetText();
308 : }
309 0 : return 0;
310 : }
311 :
312 0 : void SwAddressControl_Impl::SetCursorTo(sal_uInt32 nElement)
313 : {
314 0 : if(nElement < m_aEdits.size())
315 : {
316 0 : Edit* pEdit = m_aEdits[nElement];
317 0 : pEdit->GrabFocus();
318 0 : Rectangle aRect(pEdit->GetPosPixel(), pEdit->GetSizePixel());
319 0 : MakeVisible(aRect);
320 : }
321 :
322 0 : }
323 :
324 0 : void SwAddressControl_Impl::Command( const CommandEvent& rCEvt )
325 : {
326 0 : switch ( rCEvt.GetCommand() )
327 : {
328 : case COMMAND_WHEEL:
329 : case COMMAND_STARTAUTOSCROLL:
330 : case COMMAND_AUTOSCROLL:
331 : {
332 0 : const CommandWheelData* pWheelData = rCEvt.GetWheelData();
333 0 : if(pWheelData && !pWheelData->IsHorz() && CommandWheelMode::ZOOM != pWheelData->GetMode())
334 : {
335 0 : HandleScrollCommand( rCEvt, 0, m_pScrollBar );
336 : }
337 : }
338 0 : break;
339 : default:
340 0 : Control::Command(rCEvt);
341 : }
342 0 : }
343 :
344 0 : bool SwAddressControl_Impl::PreNotify( NotifyEvent& rNEvt )
345 : {
346 0 : if(rNEvt.GetType() == EVENT_COMMAND)
347 : {
348 0 : const CommandEvent* pCEvt = rNEvt.GetCommandEvent();
349 0 : const sal_uInt16 nCmd = pCEvt->GetCommand();
350 0 : if( COMMAND_WHEEL == nCmd )
351 : {
352 0 : Command(*pCEvt);
353 0 : return true;
354 : }
355 : }
356 0 : return Control::PreNotify(rNEvt);
357 : }
358 :
359 0 : Size SwAddressControl_Impl::GetOptimalSize() const
360 : {
361 0 : return LogicToPixel(Size(250, 160), MAP_APPFONT);
362 : }
363 :
364 0 : void SwAddressControl_Impl::Resize()
365 : {
366 0 : Window::Resize();
367 0 : m_pScrollBar->SetSizePixel(Size(m_pScrollBar->GetOutputSizePixel().Width(), GetOutputSizePixel().Height()));
368 :
369 0 : if(m_nLineHeight)
370 0 : m_pScrollBar->SetVisibleSize(m_pScrollBar->GetOutputSize().Height() / m_nLineHeight);
371 0 : m_pScrollBar->DoScroll(0);
372 :
373 0 : long nScrollBarWidth = m_pScrollBar->GetOutputSize().Width();
374 0 : Size aSize = GetOutputSizePixel();
375 :
376 0 : m_pWindow->SetSizePixel(Size(aSize.Width() - nScrollBarWidth, m_pWindow->GetOutputSizePixel().Height()));
377 0 : m_pScrollBar->SetPosPixel(Point(aSize.Width() - nScrollBarWidth, 0));
378 :
379 0 : if(m_aEdits.size())
380 : {
381 0 : long nNewEditSize = aSize.Width() - (*m_aEdits.begin())->GetPosPixel().X() - nScrollBarWidth - 6;
382 :
383 0 : ::std::vector<Edit*>::iterator aEditIter;
384 0 : for(aEditIter = m_aEdits.begin(); aEditIter != m_aEdits.end(); ++aEditIter)
385 : {
386 0 : (*aEditIter)->SetSizePixel(Size(nNewEditSize, (*aEditIter)->GetSizePixel().Height()));
387 : }
388 : }
389 :
390 0 : }
391 :
392 0 : SwCreateAddressListDialog::SwCreateAddressListDialog(
393 : vcl::Window* pParent, const OUString& rURL, SwMailMergeConfigItem& rConfig) :
394 : SfxModalDialog(pParent, "CreateAddressList", "modules/swriter/ui/createaddresslist.ui"),
395 : m_sAddressListFilterName( SW_RES( ST_FILTERNAME)),
396 : m_sURL(rURL),
397 0 : m_pCSVData( new SwCSVData ),
398 0 : m_pFindDlg(0)
399 : {
400 0 : get(m_pNewPB, "NEW");
401 0 : get(m_pDeletePB, "DELETE");
402 0 : get(m_pFindPB, "FIND");
403 0 : get(m_pCustomizePB, "CUSTOMIZE");
404 0 : get(m_pStartPB, "START");
405 0 : get(m_pPrevPB, "PREV");
406 0 : get(m_pSetNoNF, "SETNO-nospin");
407 0 : m_pSetNoNF->SetFirst(1);
408 0 : m_pSetNoNF->SetMin(1);
409 0 : get(m_pNextPB, "NEXT");
410 0 : get(m_pEndPB, "END");
411 0 : get(m_pOK, "ok");
412 0 : get(m_pAddressControl, "CONTAINER");
413 :
414 0 : m_pNewPB->SetClickHdl(LINK(this, SwCreateAddressListDialog, NewHdl_Impl));
415 0 : m_pDeletePB->SetClickHdl(LINK(this, SwCreateAddressListDialog, DeleteHdl_Impl));
416 0 : m_pFindPB->SetClickHdl(LINK(this, SwCreateAddressListDialog, FindHdl_Impl));
417 0 : m_pCustomizePB->SetClickHdl(LINK(this, SwCreateAddressListDialog, CustomizeHdl_Impl));
418 0 : m_pOK->SetClickHdl(LINK(this, SwCreateAddressListDialog, OkHdl_Impl));
419 :
420 0 : Link aLk = LINK(this, SwCreateAddressListDialog, DBCursorHdl_Impl);
421 0 : m_pStartPB->SetClickHdl(aLk);
422 0 : m_pPrevPB->SetClickHdl(aLk);
423 0 : m_pSetNoNF->SetModifyHdl(LINK(this, SwCreateAddressListDialog, DBNumCursorHdl_Impl));
424 0 : m_pNextPB->SetClickHdl(aLk);
425 0 : m_pEndPB->SetClickHdl(aLk);
426 :
427 0 : if(!m_sURL.isEmpty())
428 : {
429 : //file exists, has to be loaded here
430 0 : SfxMedium aMedium( m_sURL, STREAM_READ );
431 0 : SvStream* pStream = aMedium.GetInStream();
432 0 : if(pStream)
433 : {
434 0 : pStream->SetLineDelimiter( LINEEND_LF );
435 0 : pStream->SetStreamCharSet(RTL_TEXTENCODING_UTF8);
436 :
437 0 : OUString sLine;
438 0 : bool bRead = pStream->ReadByteStringLine( sLine, RTL_TEXTENCODING_UTF8 );
439 :
440 0 : if(bRead)
441 : {
442 : //header line
443 0 : sal_Int32 nHeaders = comphelper::string::getTokenCount(sLine, '\t');
444 0 : sal_Int32 nIndex = 0;
445 0 : for( sal_Int32 nToken = 0; nToken < nHeaders; ++nToken)
446 : {
447 0 : const OUString sHeader = sLine.getToken( 0, '\t', nIndex );
448 : OSL_ENSURE(sHeader.getLength() > 2 &&
449 : sHeader.startsWith("\"") && sHeader.endsWith("\""),
450 : "Wrong format of header");
451 0 : if(sHeader.getLength() > 2)
452 : {
453 0 : m_pCSVData->aDBColumnHeaders.push_back( sHeader.copy(1, sHeader.getLength() -2));
454 : }
455 0 : }
456 : }
457 0 : while(pStream->ReadByteStringLine( sLine, RTL_TEXTENCODING_UTF8 ))
458 : {
459 0 : ::std::vector<OUString> aNewData;
460 : //analyze data line
461 0 : sal_Int32 nDataCount = comphelper::string::getTokenCount(sLine, '\t');
462 0 : sal_Int32 nIndex = 0;
463 0 : for( sal_Int32 nToken = 0; nToken < nDataCount; ++nToken)
464 : {
465 0 : const OUString sData = sLine.getToken( 0, '\t', nIndex );
466 : OSL_ENSURE( sData.startsWith("\"") && sData.endsWith("\""),
467 : "Wrong format of line");
468 0 : if(sData.getLength() >= 2)
469 0 : aNewData.push_back(sData.copy(1, sData.getLength() - 2));
470 : else
471 0 : aNewData.push_back(sData);
472 0 : }
473 0 : m_pCSVData->aDBData.push_back( aNewData );
474 0 : }
475 0 : }
476 : }
477 : else
478 : {
479 : //database has to be created
480 0 : const ResStringArray& rAddressHeader = rConfig.GetDefaultAddressHeaders();
481 0 : const sal_uInt32 nCount = rAddressHeader.Count();
482 0 : for(sal_uInt32 nHeader = 0; nHeader < nCount; ++nHeader)
483 0 : m_pCSVData->aDBColumnHeaders.push_back( rAddressHeader.GetString(nHeader));
484 0 : ::std::vector<OUString> aNewData;
485 0 : OUString sTemp;
486 0 : aNewData.insert(aNewData.begin(), nCount, sTemp);
487 0 : m_pCSVData->aDBData.push_back(aNewData);
488 : }
489 : //now fill the address control
490 0 : m_pAddressControl->SetData(*m_pCSVData);
491 0 : m_pAddressControl->SetCurrentDataSet(0);
492 0 : m_pSetNoNF->SetMax(m_pCSVData->aDBData.size());
493 0 : UpdateButtons();
494 0 : }
495 :
496 0 : SwCreateAddressListDialog::~SwCreateAddressListDialog()
497 : {
498 0 : delete m_pCSVData;
499 0 : delete m_pFindDlg;
500 0 : }
501 :
502 0 : IMPL_LINK_NOARG(SwCreateAddressListDialog, NewHdl_Impl)
503 : {
504 0 : sal_uInt32 nCurrent = m_pAddressControl->GetCurrentDataSet();
505 0 : ::std::vector<OUString> aNewData;
506 0 : OUString sTemp;
507 0 : aNewData.insert(aNewData.begin(), m_pCSVData->aDBColumnHeaders.size(), sTemp);
508 0 : m_pCSVData->aDBData.insert(m_pCSVData->aDBData.begin() + ++nCurrent, aNewData);
509 0 : m_pSetNoNF->SetMax(m_pCSVData->aDBData.size());
510 : //the NumericField start at 1
511 0 : m_pSetNoNF->SetValue(nCurrent + 1);
512 : //the address control starts at 0
513 0 : m_pAddressControl->SetCurrentDataSet(nCurrent);
514 0 : UpdateButtons();
515 0 : return 0;
516 : }
517 :
518 0 : IMPL_LINK_NOARG(SwCreateAddressListDialog, DeleteHdl_Impl)
519 : {
520 0 : sal_uInt32 nCurrent = m_pAddressControl->GetCurrentDataSet();
521 0 : if(m_pCSVData->aDBData.size() > 1)
522 : {
523 0 : m_pCSVData->aDBData.erase(m_pCSVData->aDBData.begin() + nCurrent);
524 0 : if(nCurrent)
525 0 : --nCurrent;
526 : }
527 : else
528 : {
529 : // if only one set is available then clear the data
530 0 : OUString sTemp;
531 0 : m_pCSVData->aDBData[0].assign(m_pCSVData->aDBData[0].size(), sTemp);
532 0 : m_pDeletePB->Enable(false);
533 : }
534 0 : m_pAddressControl->SetCurrentDataSet(nCurrent);
535 0 : m_pSetNoNF->SetMax(m_pCSVData->aDBData.size());
536 0 : UpdateButtons();
537 0 : return 0;
538 : }
539 :
540 0 : IMPL_LINK_NOARG(SwCreateAddressListDialog, FindHdl_Impl)
541 : {
542 0 : if(!m_pFindDlg)
543 : {
544 0 : m_pFindDlg = new SwFindEntryDialog(this);
545 0 : ListBox& rColumnBox = m_pFindDlg->GetFieldsListBox();
546 0 : ::std::vector< OUString >::iterator aHeaderIter;
547 0 : for(aHeaderIter = m_pCSVData->aDBColumnHeaders.begin();
548 0 : aHeaderIter != m_pCSVData->aDBColumnHeaders.end();
549 : ++aHeaderIter)
550 0 : rColumnBox.InsertEntry(*aHeaderIter);
551 0 : rColumnBox.SelectEntryPos( 0 );
552 0 : m_pFindDlg->Show();
553 : }
554 : else
555 0 : m_pFindDlg->Show(!m_pFindDlg->IsVisible());
556 0 : return 0;
557 : }
558 :
559 0 : IMPL_LINK(SwCreateAddressListDialog, CustomizeHdl_Impl, PushButton*, pButton)
560 : {
561 0 : boost::scoped_ptr<SwCustomizeAddressListDialog> pDlg(new SwCustomizeAddressListDialog(pButton, *m_pCSVData));
562 0 : if(RET_OK == pDlg->Execute())
563 : {
564 0 : delete m_pCSVData;
565 0 : m_pCSVData = pDlg->GetNewData();
566 0 : m_pAddressControl->SetData(*m_pCSVData);
567 0 : m_pAddressControl->SetCurrentDataSet(m_pAddressControl->GetCurrentDataSet());
568 : }
569 0 : pDlg.reset();
570 :
571 : //update find dialog
572 0 : if(m_pFindDlg)
573 : {
574 0 : ListBox& rColumnBox = m_pFindDlg->GetFieldsListBox();
575 0 : rColumnBox.Clear();
576 0 : ::std::vector< OUString >::iterator aHeaderIter;
577 0 : for(aHeaderIter = m_pCSVData->aDBColumnHeaders.begin();
578 0 : aHeaderIter != m_pCSVData->aDBColumnHeaders.end();
579 : ++aHeaderIter)
580 0 : rColumnBox.InsertEntry(*aHeaderIter);
581 : }
582 0 : return 0;
583 : }
584 :
585 : namespace
586 : {
587 :
588 0 : void lcl_WriteValues(const ::std::vector<OUString> *pFields, SvStream* pStream)
589 : {
590 0 : OUString sLine;
591 0 : const ::std::vector< OUString >::const_iterator aBegin = pFields->begin();
592 0 : const ::std::vector< OUString >::const_iterator aEnd = pFields->end();
593 0 : for(::std::vector< OUString >::const_iterator aIter = aBegin; aIter != aEnd; ++aIter)
594 : {
595 0 : if (aIter==aBegin)
596 : {
597 0 : sLine += "\"" + *aIter + "\"";
598 : }
599 : else
600 : {
601 0 : sLine += "\t\"" + *aIter + "\"";
602 : }
603 : }
604 0 : pStream->WriteByteStringLine( sLine, RTL_TEXTENCODING_UTF8 );
605 0 : }
606 :
607 : }
608 :
609 0 : IMPL_LINK_NOARG(SwCreateAddressListDialog, OkHdl_Impl)
610 : {
611 0 : if(m_sURL.isEmpty())
612 : {
613 0 : sfx2::FileDialogHelper aDlgHelper( TemplateDescription::FILESAVE_SIMPLE, 0 );
614 0 : uno::Reference < XFilePicker > xFP = aDlgHelper.GetFilePicker();
615 :
616 0 : const OUString sPath( SvtPathOptions().SubstituteVariable("$(userurl)/database") );
617 0 : aDlgHelper.SetDisplayDirectory( sPath );
618 0 : uno::Reference< XFilterManager > xFltMgr(xFP, uno::UNO_QUERY);
619 0 : xFltMgr->appendFilter( m_sAddressListFilterName, "*.csv" );
620 0 : xFltMgr->setCurrentFilter( m_sAddressListFilterName ) ;
621 :
622 0 : if( ERRCODE_NONE == aDlgHelper.Execute() )
623 : {
624 0 : m_sURL = xFP->getFiles().getConstArray()[0];
625 0 : INetURLObject aResult( m_sURL );
626 0 : aResult.setExtension("csv");
627 0 : m_sURL = aResult.GetMainURL(INetURLObject::NO_DECODE);
628 0 : }
629 : }
630 0 : if(!m_sURL.isEmpty())
631 : {
632 0 : SfxMedium aMedium( m_sURL, STREAM_READWRITE|STREAM_TRUNC );
633 0 : SvStream* pStream = aMedium.GetOutStream();
634 0 : pStream->SetLineDelimiter( LINEEND_LF );
635 0 : pStream->SetStreamCharSet(RTL_TEXTENCODING_UTF8);
636 :
637 0 : lcl_WriteValues(&(m_pCSVData->aDBColumnHeaders), pStream);
638 :
639 0 : ::std::vector< ::std::vector< OUString > >::iterator aDataIter;
640 0 : for( aDataIter = m_pCSVData->aDBData.begin(); aDataIter != m_pCSVData->aDBData.end(); ++aDataIter)
641 : {
642 0 : lcl_WriteValues(&(*aDataIter), pStream);
643 : }
644 0 : aMedium.Commit();
645 0 : EndDialog(RET_OK);
646 : }
647 :
648 0 : return 0;
649 : }
650 :
651 0 : IMPL_LINK(SwCreateAddressListDialog, DBCursorHdl_Impl, PushButton*, pButton)
652 : {
653 0 : sal_uInt32 nValue = static_cast< sal_uInt32 >(m_pSetNoNF->GetValue());
654 :
655 0 : if(pButton == m_pStartPB)
656 0 : nValue = 1;
657 0 : else if(pButton == m_pPrevPB)
658 : {
659 0 : if(nValue > 1)
660 0 : --nValue;
661 : }
662 0 : else if(pButton == m_pNextPB)
663 : {
664 0 : if(nValue < (sal_uInt32)m_pSetNoNF->GetMax())
665 0 : ++nValue;
666 : }
667 : else //m_aEndPB
668 0 : nValue = static_cast< sal_uInt32 >(m_pSetNoNF->GetMax());
669 0 : if(nValue != m_pSetNoNF->GetValue())
670 : {
671 0 : m_pSetNoNF->SetValue(nValue);
672 0 : DBNumCursorHdl_Impl(m_pSetNoNF);
673 : }
674 0 : return 0;
675 : }
676 :
677 0 : IMPL_LINK_NOARG(SwCreateAddressListDialog, DBNumCursorHdl_Impl)
678 : {
679 0 : m_pAddressControl->SetCurrentDataSet( static_cast< sal_uInt32 >(m_pSetNoNF->GetValue() - 1) );
680 0 : UpdateButtons();
681 0 : return 0;
682 : }
683 :
684 0 : void SwCreateAddressListDialog::UpdateButtons()
685 : {
686 0 : sal_uInt32 nCurrent = static_cast< sal_uInt32 >(m_pSetNoNF->GetValue() );
687 0 : sal_uInt32 nSize = (sal_uInt32 )m_pCSVData->aDBData.size();
688 0 : m_pStartPB->Enable(nCurrent != 1);
689 0 : m_pPrevPB->Enable(nCurrent != 1);
690 0 : m_pNextPB->Enable(nCurrent != nSize);
691 0 : m_pEndPB->Enable(nCurrent != nSize);
692 0 : m_pDeletePB->Enable(nSize > 0);
693 0 : }
694 :
695 0 : void SwCreateAddressListDialog::Find(const OUString& rSearch, sal_Int32 nColumn)
696 : {
697 0 : const OUString sSearch = rSearch.toAsciiLowerCase();
698 0 : sal_uInt32 nCurrent = m_pAddressControl->GetCurrentDataSet();
699 : //search forward
700 0 : bool bFound = false;
701 0 : sal_uInt32 nStart = nCurrent + 1;
702 0 : sal_uInt32 nEnd = m_pCSVData->aDBData.size();
703 0 : sal_uInt32 nElement = 0;
704 0 : sal_uInt32 nPos = 0;
705 0 : for(short nTemp = 0; nTemp < 2 && !bFound; nTemp++)
706 : {
707 0 : for(nPos = nStart; nPos < nEnd; ++nPos)
708 : {
709 0 : ::std::vector< OUString> aData = m_pCSVData->aDBData[nPos];
710 0 : if(nColumn >=0)
711 0 : bFound = -1 != aData[(sal_uInt32)nColumn].toAsciiLowerCase().indexOf(sSearch);
712 : else
713 : {
714 0 : for( nElement = 0; nElement < aData.size(); ++nElement)
715 : {
716 0 : bFound = -1 != aData[nElement].toAsciiLowerCase().indexOf(sSearch);
717 0 : if(bFound)
718 : {
719 0 : nColumn = nElement;
720 0 : break;
721 : }
722 : }
723 : }
724 0 : if(bFound)
725 0 : break;
726 0 : }
727 0 : nStart = 0;
728 0 : nEnd = nCurrent + 1;
729 : }
730 0 : if(bFound)
731 : {
732 0 : m_pAddressControl->SetCurrentDataSet(nPos);
733 0 : m_pSetNoNF->SetValue( nPos + 1 );
734 0 : UpdateButtons();
735 0 : m_pAddressControl->SetCursorTo(nElement);
736 0 : }
737 0 : }
738 :
739 0 : SwFindEntryDialog::SwFindEntryDialog(SwCreateAddressListDialog* pParent)
740 : : ModelessDialog(pParent, "FindEntryDialog",
741 : "modules/swriter/ui/findentrydialog.ui")
742 0 : , m_pParent(pParent)
743 : {
744 0 : get(m_pCancel, "cancel");
745 0 : get(m_pFindPB, "find");
746 0 : get(m_pFindOnlyLB, "area");
747 0 : get(m_pFindOnlyCB, "findin");
748 0 : get(m_pFindED, "entry");
749 0 : m_pFindPB->SetClickHdl(LINK(this, SwFindEntryDialog, FindHdl_Impl));
750 0 : m_pFindED->SetModifyHdl(LINK(this, SwFindEntryDialog, FindEnableHdl_Impl));
751 0 : m_pCancel->SetClickHdl(LINK(this, SwFindEntryDialog, CloseHdl_Impl));
752 0 : }
753 :
754 0 : IMPL_LINK_NOARG(SwFindEntryDialog, FindHdl_Impl)
755 : {
756 0 : sal_Int32 nColumn = -1;
757 0 : if(m_pFindOnlyCB->IsChecked())
758 0 : nColumn = m_pFindOnlyLB->GetSelectEntryPos();
759 0 : if(nColumn != LISTBOX_ENTRY_NOTFOUND)
760 0 : m_pParent->Find(m_pFindED->GetText(), nColumn);
761 0 : return 0;
762 : }
763 :
764 0 : IMPL_LINK_NOARG(SwFindEntryDialog, FindEnableHdl_Impl)
765 : {
766 0 : m_pFindPB->Enable(!m_pFindED->GetText().isEmpty());
767 0 : return 0;
768 : }
769 :
770 0 : IMPL_LINK_NOARG(SwFindEntryDialog, CloseHdl_Impl)
771 : {
772 0 : Show(false);
773 0 : return 0;
774 0 : }
775 :
776 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|