Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / source / uibase / dbui / mmconfigitem.cxx
blob40c9aac78da1da9a5413e8ac9f796f634c464f0d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <mmconfigitem.hxx>
22 #include <comphelper/propertyvalue.hxx>
23 #include <vector>
24 #include <swtypes.hxx>
25 #include <com/sun/star/uno/Any.hxx>
26 #include <com/sun/star/beans/PropertyValue.hpp>
27 #include <com/sun/star/frame/XDispatch.hpp>
28 #include <com/sun/star/sdbc/XDataSource.hpp>
29 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include <com/sun/star/beans/XPropertySet.hpp>
32 #include <com/sun/star/sdbc/XRowSet.hpp>
33 #include <com/sun/star/view/XSelectionSupplier.hpp>
34 #include <comphelper/processfactory.hxx>
35 #include <comphelper/types.hxx>
36 #include <com/sun/star/sdb/CommandType.hpp>
37 #include <comphelper/sequence.hxx>
38 #include <rtl/ustrbuf.hxx>
39 #include <sal/log.hxx>
40 #include <unotools/configitem.hxx>
41 #include <comphelper/diagnose_ex.hxx>
42 #include <mailmergehelper.hxx>
43 #include <swunohelper.hxx>
44 #include <dbmgr.hxx>
45 #include <view.hxx>
46 #include <unodispatch.hxx>
47 #include <wrtsh.hxx>
48 #include <dbui.hrc>
50 using namespace utl;
51 using namespace ::com::sun::star;
52 using namespace ::com::sun::star::uno;
53 using namespace ::com::sun::star::lang;
54 using namespace ::com::sun::star::beans;
55 using namespace ::com::sun::star::sdb;
56 using namespace ::com::sun::star::sdbc;
57 using namespace ::com::sun::star::sdbcx;
59 constexpr OUStringLiteral cAddressDataAssignments = u"AddressDataAssignments";
60 const char cDBColumnAssignments[] = "DBColumnAssignments";
61 const char cDataSourceName[] = "DataSource/DataSourceName";
62 const char cDataTableName[] = "DataSource/DataTableName" ;
63 const char cDataCommandType[] = "DataSource/DataCommandType";
65 #define SECURE_PORT 587
66 #define DEFAULT_PORT 25
67 #define POP_PORT 110
68 #define POP_SECURE_PORT 995
69 #define IMAP_PORT 143
70 #define IMAP_SECURE_PORT 993
72 namespace {
74 struct DBAddressDataAssignment
76 SwDBData aDBData;
77 Sequence< OUString> aDBColumnAssignments;
78 //if loaded the name of the node has to be saved
79 OUString sConfigNodeName;
80 //all created or changed assignments need to be stored
81 bool bColumnAssignmentsChanged;
83 DBAddressDataAssignment() :
84 bColumnAssignmentsChanged(false)
90 class SwMailMergeConfigItem_Impl : public utl::ConfigItem
92 friend class SwMailMergeConfigItem;
93 Reference< XDataSource> m_xSource;
94 SharedConnection m_xConnection;
95 Reference< XColumnsSupplier> m_xColumnsSupplier;
96 Reference< XResultSet> m_xResultSet;
97 SwDBData m_aDBData;
98 OUString m_sFilter;
99 sal_Int32 m_nResultSetCursorPos;
101 std::vector<DBAddressDataAssignment> m_aAddressDataAssignments;
102 std::vector< OUString> m_aAddressBlocks;
103 sal_Int32 m_nCurrentAddressBlock;
104 bool m_bIsAddressBlock;
105 bool m_bIsHideEmptyParagraphs;
107 bool m_bIsOutputToLetter;
108 bool m_bIncludeCountry;
109 OUString m_sExcludeCountry;
111 bool m_bIsGreetingLine;
112 bool m_bIsIndividualGreetingLine;
113 std::vector< OUString> m_aFemaleGreetingLines;
114 sal_Int32 m_nCurrentFemaleGreeting;
115 std::vector< OUString> m_aMaleGreetingLines;
116 sal_Int32 m_nCurrentMaleGreeting;
117 std::vector< OUString> m_aNeutralGreetingLines;
118 sal_Int32 m_nCurrentNeutralGreeting;
119 OUString m_sFemaleGenderValue;
120 uno::Sequence< OUString> m_aSavedDocuments;
122 bool m_bIsGreetingLineInMail;
123 bool m_bIsIndividualGreetingLineInMail;
125 //mail settings
126 OUString m_sMailDisplayName;
127 OUString m_sMailAddress;
128 OUString m_sMailReplyTo;
129 OUString m_sMailServer;
130 OUString m_sMailUserName;
131 OUString m_sMailPassword;
133 bool m_bIsSMPTAfterPOP;
134 OUString m_sInServerName;
135 sal_Int16 m_nInServerPort;
136 bool m_bInServerPOP;
137 OUString m_sInServerUserName;
138 OUString m_sInServerPassword;
140 sal_Int16 m_nMailPort;
141 bool m_bIsMailReplyTo;
142 bool m_bIsSecureConnection;
143 bool m_bIsAuthentication;
145 bool m_bIsEMailSupported;
147 std::vector<std::pair<OUString, int>> m_AddressHeaderSA;
149 //these addresses are not stored in the configuration
150 std::vector< SwDocMergeInfo > m_aMergeInfos;
152 //we do overwrite the usersettings in a special case
153 //then we do remind the usersettings here
154 bool m_bUserSettingWereOverwritten;
155 bool m_bIsAddressBlock_LastUserSetting;
156 bool m_bIsGreetingLineInMail_LastUserSetting;
157 bool m_bIsGreetingLine_LastUserSetting;
159 static const Sequence< OUString>& GetPropertyNames();
161 virtual void ImplCommit() override;
163 public:
164 SwMailMergeConfigItem_Impl();
166 virtual void Notify( const css::uno::Sequence< OUString >& aPropertyNames ) override;
167 Sequence< OUString> GetAddressBlocks(bool bConvertToConfig = false) const;
168 void SetAddressBlocks(
169 const Sequence< OUString>& rBlocks,
170 bool bConvertFromConfig = false);
171 uno::Sequence< OUString>
172 GetGreetings(SwMailMergeConfigItem::Gender eType,
173 bool bConvertToConfig = false) const;
174 void SetGreetings(SwMailMergeConfigItem::Gender eType,
175 const uno::Sequence< OUString>& rBlocks,
176 bool bConvertFromConfig = false);
178 void SetCurrentAddressBlockIndex( sal_Int32 nSet );
179 sal_Int32 GetCurrentAddressBlockIndex() const
180 { return m_nCurrentAddressBlock; }
181 sal_Int32 GetCurrentGreeting(SwMailMergeConfigItem::Gender eType) const;
182 void SetCurrentGreeting(SwMailMergeConfigItem::Gender eType, sal_Int32 nIndex);
186 SwMailMergeConfigItem_Impl::SwMailMergeConfigItem_Impl() :
187 ConfigItem("Office.Writer/MailMergeWizard", ConfigItemMode::NONE),
188 m_nResultSetCursorPos(-1),
189 m_nCurrentAddressBlock(0),
190 m_bIsAddressBlock(true),
191 m_bIsHideEmptyParagraphs(false),
192 m_bIsOutputToLetter(true),
193 m_bIncludeCountry(false),
194 m_bIsGreetingLine(true),
195 m_bIsIndividualGreetingLine(false),
196 m_nCurrentFemaleGreeting(0),
197 m_nCurrentMaleGreeting(0),
198 m_nCurrentNeutralGreeting(0),
199 m_bIsGreetingLineInMail(false),
200 m_bIsIndividualGreetingLineInMail(false),
201 m_bIsSMPTAfterPOP(false),
202 m_nInServerPort( POP_SECURE_PORT ),
203 m_bInServerPOP( true ),
204 m_nMailPort(SECURE_PORT),
205 m_bIsMailReplyTo(false),
206 m_bIsSecureConnection(true),
207 m_bIsAuthentication(false),
209 m_bIsEMailSupported(false),
210 m_bUserSettingWereOverwritten(false),
211 m_bIsAddressBlock_LastUserSetting(false),
212 m_bIsGreetingLineInMail_LastUserSetting(false),
213 m_bIsGreetingLine_LastUserSetting(false)
215 for (auto const& [aName, aID] : SA_ADDRESS_HEADER)
217 m_AddressHeaderSA.emplace_back(SwResId(aName), aID);
220 const Sequence<OUString>& rNames = GetPropertyNames();
221 Sequence<Any> aValues = GetProperties(rNames);
222 const Any* pValues = aValues.getConstArray();
223 assert(aValues.getLength() == rNames.getLength());
224 if(aValues.getLength() == rNames.getLength())
226 for(int nProp = 0; nProp < rNames.getLength(); nProp++)
228 switch(nProp)
230 case 0: pValues[nProp] >>= m_bIsOutputToLetter; break;
231 case 1: pValues[nProp] >>= m_bIncludeCountry; break;
232 case 2: pValues[nProp] >>= m_sExcludeCountry; break;
233 case 3:
235 Sequence< OUString> aBlocks;
236 pValues[nProp] >>= aBlocks;
237 SetAddressBlocks(aBlocks, true);
239 break;
240 case 4: pValues[nProp] >>= m_bIsAddressBlock; break;
241 case 5: pValues[nProp] >>= m_bIsGreetingLine; break;
242 case 6: pValues[nProp] >>= m_bIsIndividualGreetingLine; break;
243 case 7 :
244 case 8 :
245 case 9 :
247 Sequence< OUString> aGreetings;
248 pValues[nProp] >>= aGreetings;
249 SetGreetings(SwMailMergeConfigItem::Gender(
250 SwMailMergeConfigItem::FEMALE + nProp - 7), aGreetings, true);
252 break;
254 case 10: pValues[nProp] >>= m_nCurrentFemaleGreeting; break;
255 case 11: pValues[nProp] >>= m_nCurrentMaleGreeting; break;
256 case 12: pValues[nProp] >>= m_nCurrentNeutralGreeting; break;
257 case 13: pValues[nProp] >>= m_sFemaleGenderValue; break;
258 case 14: pValues[nProp] >>= m_sMailDisplayName; break;
259 case 15: pValues[nProp] >>= m_sMailAddress; break;
260 case 16: pValues[nProp] >>= m_bIsMailReplyTo; break;
261 case 17: pValues[nProp] >>= m_sMailReplyTo; break;
262 case 18: pValues[nProp] >>= m_sMailServer; break;
263 case 19: pValues[nProp] >>= m_nMailPort; break;
264 case 20: pValues[nProp] >>= m_bIsSecureConnection; break;
265 case 21: pValues[nProp] >>= m_bIsAuthentication; break;
266 case 22: pValues[nProp] >>= m_sMailUserName; break;
267 case 23: pValues[nProp] >>= m_sMailPassword; break;
268 case 24 :pValues[nProp] >>= m_aDBData.sDataSource; break;
269 case 25 :pValues[nProp] >>= m_aDBData.sCommand; break;
270 case 26 :
272 short nTemp = 0;
273 if(pValues[nProp] >>= nTemp)
274 m_aDBData.nCommandType = nTemp;
276 break;
277 case 27: pValues[nProp] >>= m_sFilter; break;
278 case 28: pValues[nProp] >>= m_aSavedDocuments; break;
279 case 29:
280 pValues[nProp] >>= m_bIsEMailSupported;
281 break;
282 case 30: pValues[nProp] >>= m_bIsGreetingLineInMail; break;
283 case 31: pValues[nProp] >>= m_bIsIndividualGreetingLineInMail; break;
284 case 32: pValues[nProp] >>= m_bIsSMPTAfterPOP; break;
285 case 33: pValues[nProp] >>= m_sInServerName; break;
286 case 34: pValues[nProp] >>= m_nInServerPort; break;
287 case 35: pValues[nProp] >>= m_bInServerPOP; break;
288 case 36: pValues[nProp] >>= m_sInServerUserName; break;
289 case 37: pValues[nProp] >>= m_sInServerPassword; break;
290 case 38: pValues[nProp] >>= m_bIsHideEmptyParagraphs; break;
291 case 39: pValues[nProp] >>= m_nCurrentAddressBlock; break;
294 ClearModified();
296 //read the list of data base assignments
297 Sequence<OUString> aAssignments = GetNodeNames(cAddressDataAssignments);
298 if(aAssignments.hasElements())
300 //create a list of property names to load the URLs of all data bases
301 const OUString* pAssignments = aAssignments.getConstArray();
302 Sequence< OUString > aAssignProperties(4 * aAssignments.getLength());
303 OUString* pAssignProperties = aAssignProperties.getArray();
304 sal_Int32 nAssign;
305 for(nAssign = 0; nAssign < aAssignProperties.getLength(); nAssign += 4)
307 OUString sAssignPath = OUString::Concat(cAddressDataAssignments) +
308 "/" +
309 pAssignments[nAssign / 4] +
310 "/";
311 pAssignProperties[nAssign] = sAssignPath;
312 pAssignProperties[nAssign] += cDataSourceName;
313 pAssignProperties[nAssign + 1] = sAssignPath;
314 pAssignProperties[nAssign + 1] += cDataTableName;
315 pAssignProperties[nAssign + 2] = sAssignPath;
316 pAssignProperties[nAssign + 2] += cDataCommandType;
317 pAssignProperties[nAssign + 3] = sAssignPath;
318 pAssignProperties[nAssign + 3] += cDBColumnAssignments;
320 Sequence<Any> aAssignValues = GetProperties(aAssignProperties);
321 const Any* pAssignValues = aAssignValues.getConstArray();
322 for(nAssign = 0; nAssign < aAssignValues.getLength(); nAssign += 4 )
324 DBAddressDataAssignment aAssignment;
325 pAssignValues[nAssign] >>= aAssignment.aDBData.sDataSource;
326 pAssignValues[nAssign + 1] >>= aAssignment.aDBData.sCommand;
327 pAssignValues[nAssign + 2] >>= aAssignment.aDBData.nCommandType;
328 pAssignValues[nAssign + 3] >>= aAssignment.aDBColumnAssignments;
329 aAssignment.sConfigNodeName = pAssignments[nAssign / 4];
330 m_aAddressDataAssignments.push_back(aAssignment);
333 //check if the saved documents still exist
334 if(!m_aSavedDocuments.hasElements())
335 return;
337 uno::Sequence< OUString > aTempDocuments(m_aSavedDocuments.getLength());
338 auto begin = aTempDocuments.getArray();
339 OUString* pTempDocuments = std::copy_if(std::cbegin(m_aSavedDocuments), std::cend(m_aSavedDocuments), begin,
340 [](const OUString& rDoc) { return SWUnoHelper::UCB_IsFile( rDoc ); });
341 sal_Int32 nIndex = static_cast<sal_Int32>(std::distance(begin, pTempDocuments));
342 if(nIndex < m_aSavedDocuments.getLength())
344 m_aSavedDocuments.swap(aTempDocuments);
345 m_aSavedDocuments.realloc(nIndex);
349 void SwMailMergeConfigItem_Impl::SetCurrentAddressBlockIndex( sal_Int32 nSet )
351 if(m_aAddressBlocks.size() >= sal::static_int_cast<sal_uInt32, sal_Int32>(nSet))
353 m_nCurrentAddressBlock = nSet;
354 SetModified();
358 static OUString lcl_CreateNodeName(Sequence<OUString>& rAssignments )
360 sal_Int32 nStart = rAssignments.getLength();
361 OUString sNewName;
362 //search if the name exists
363 while(true)
365 sNewName = "_" + OUString::number(nStart++);
366 if(comphelper::findValue(rAssignments, sNewName) == -1)
367 break;
369 // add the new name to the array of existing names
370 rAssignments.realloc(rAssignments.getLength() + 1);
371 rAssignments.getArray()[rAssignments.getLength() - 1] = sNewName;
372 return sNewName;
375 static void lcl_ConvertToNumbers(OUString& rBlock, const std::vector<std::pair<OUString, int>>& rHeaders )
377 //convert the strings used for UI to numbers used for the configuration
378 OUString sBlock(rBlock.replaceAll("\n", "\\n"));
379 for (size_t i = 0; i < rHeaders.size(); ++i)
381 OUString sHeader = "<" + rHeaders[i].first + ">";
382 OUString sReplace = "<" + OUStringChar(sal_Unicode('0' + i)) + ">";
383 sBlock = sBlock.replaceAll(sHeader, sReplace);
385 rBlock = sBlock;
388 static void lcl_ConvertFromNumbers(OUString& rBlock, const std::vector<std::pair<OUString, int>>& rHeaders)
390 //convert the numbers used for the configuration to strings used for UI to numbers
391 //doesn't use ReplaceAll to prevent expansion of numbers inside of the headers
392 SwAddressIterator aGreetingIter(rBlock.replaceAll("\\n", "\n"));
393 OUStringBuffer sBlock;
394 while(aGreetingIter.HasMore())
396 SwMergeAddressItem aNext = aGreetingIter.Next();
397 if(aNext.bIsColumn)
399 //the text should be 1 characters long
400 sal_Unicode cChar = aNext.sText[0];
401 if(cChar >= '0' && cChar <= 'c')
403 sBlock.append("<");
404 sal_uInt16 nHeader = cChar - '0';
405 if(nHeader < rHeaders.size())
406 sBlock.append(rHeaders[nHeader].first);
407 sBlock.append(">");
409 else
411 SAL_WARN("sw.ui", "parse error in address block or greeting line");
414 else
415 sBlock.append(aNext.sText);
417 rBlock = sBlock.makeStringAndClear();
420 const Sequence<OUString>& SwMailMergeConfigItem_Impl::GetPropertyNames()
422 static Sequence<OUString> aNames {
423 "OutputToLetter", // 0
424 "IncludeCountry", // 1
425 "ExcludeCountry", // 2
426 "AddressBlockSettings", // 3
427 "IsAddressBlock", // 4
428 "IsGreetingLine", // 5
429 "IsIndividualGreetingLine", // 6
430 "FemaleGreetingLines", // 7
431 "MaleGreetingLines", // 8
432 "NeutralGreetingLines", // 9
433 "CurrentFemaleGreeting", // 10
434 "CurrentMaleGreeting", // 11
435 "CurrentNeutralGreeting", // 12
436 "FemaleGenderValue", // 13
437 "MailDisplayName", // 14
438 "MailAddress", // 15
439 "IsMailReplyTo", // 16
440 "MailReplyTo", // 17
441 "MailServer", // 18
442 "MailPort", // 19
443 "IsSecureConnection", // 20
444 "IsAuthentication", // 21
445 "MailUserName", // 22
446 "MailPassword", // 23
447 "DataSource/DataSourceName", // 24
448 "DataSource/DataTableName", // 25
449 "DataSource/DataCommandType",// 26
450 "Filter", // 27
451 "SavedDocuments", // 28
452 "EMailSupported", // 29
453 "IsEMailGreetingLine", //30
454 "IsEMailIndividualGreetingLine", //31
455 "IsSMPTAfterPOP", //32
456 "InServerName", //33
457 "InServerPort", //34
458 "InServerIsPOP", //35
459 "InServerUserName", //36
460 "InServerPassword", //37
461 "IsHideEmptyParagraphs", //38
462 "CurrentAddressBlock" //39
464 return aNames;
467 void SwMailMergeConfigItem_Impl::Notify( const css::uno::Sequence< OUString >& ) {}
469 void SwMailMergeConfigItem_Impl::ImplCommit()
471 Sequence<OUString> aNames = GetPropertyNames();
472 Sequence<Any> aValues(aNames.getLength());
473 Any* pValues = aValues.getArray();
475 for(int nProp = 0; nProp < aNames.getLength(); nProp++)
477 switch(nProp)
479 case 0: pValues[nProp] <<= m_bIsOutputToLetter; break;
480 case 1: pValues[nProp] <<= m_bIncludeCountry; break;
481 case 2: pValues[nProp] <<= m_sExcludeCountry; break;
482 case 3: pValues[nProp] <<= GetAddressBlocks(true); break;
483 case 4:
485 if( m_bUserSettingWereOverwritten)
486 pValues[nProp] <<= m_bIsAddressBlock_LastUserSetting;
487 else
488 pValues[nProp] <<= m_bIsAddressBlock;
489 break;
491 case 5:
493 if( m_bUserSettingWereOverwritten)
494 pValues[nProp] <<= m_bIsGreetingLine_LastUserSetting;
495 else
496 pValues[nProp] <<= m_bIsGreetingLine;
497 break;
499 case 6: pValues[nProp] <<= m_bIsIndividualGreetingLine; break;
500 case 7:
501 case 8:
502 case 9:
503 pValues[nProp] <<= GetGreetings(
504 SwMailMergeConfigItem::Gender(
505 SwMailMergeConfigItem::FEMALE + nProp - 7), true);
506 break;
507 case 10: pValues[nProp] <<= m_nCurrentFemaleGreeting; break;
508 case 11: pValues[nProp] <<= m_nCurrentMaleGreeting; break;
509 case 12: pValues[nProp] <<= m_nCurrentNeutralGreeting; break;
510 case 13: pValues[nProp] <<= m_sFemaleGenderValue; break;
511 case 14: pValues[nProp] <<= m_sMailDisplayName; break;
512 case 15: pValues[nProp] <<= m_sMailAddress; break;
513 case 16: pValues[nProp] <<= m_bIsMailReplyTo; break;
514 case 17: pValues[nProp] <<= m_sMailReplyTo; break;
515 case 18: pValues[nProp] <<= m_sMailServer; break;
516 case 19: pValues[nProp] <<= m_nMailPort; break;
517 case 20: pValues[nProp] <<= m_bIsSecureConnection; break;
518 case 21: pValues[nProp] <<= m_bIsAuthentication; break;
519 case 22: pValues[nProp] <<= m_sMailUserName; break;
520 case 23: pValues[nProp] <<= m_sMailPassword; break;
521 case 24 :pValues[nProp] <<= m_aDBData.sDataSource; break;
522 case 25 :pValues[nProp] <<= m_aDBData.sCommand; break;
523 case 26 :pValues[nProp] <<= m_aDBData.nCommandType; break;
524 case 27 :pValues[nProp] <<= m_sFilter; break;
525 case 28 :pValues[nProp] <<= m_aSavedDocuments; break;
526 case 29: pValues[nProp] <<= m_bIsEMailSupported; break;
527 case 30:
529 if( m_bUserSettingWereOverwritten)
530 pValues[nProp] <<= m_bIsGreetingLineInMail_LastUserSetting;
531 else
532 pValues[nProp] <<= m_bIsGreetingLineInMail;
533 break;
535 case 31: pValues[nProp] <<= m_bIsIndividualGreetingLineInMail; break;
536 case 32: pValues[nProp] <<= m_bIsSMPTAfterPOP; break;
537 case 33: pValues[nProp] <<= m_sInServerName; break;
538 case 34: pValues[nProp] <<= m_nInServerPort; break;
539 case 35: pValues[nProp] <<= m_bInServerPOP; break;
540 case 36: pValues[nProp] <<= m_sInServerUserName; break;
541 case 37: pValues[nProp] <<= m_sInServerPassword; break;
542 case 38: pValues[nProp] <<= m_bIsHideEmptyParagraphs; break;
543 case 39: pValues[nProp] <<= m_nCurrentAddressBlock; break;
546 PutProperties(aNames, aValues);
547 //store the changed / new assignments
549 //load the existing node names to find new names
550 Sequence<OUString> aAssignments = GetNodeNames(cAddressDataAssignments);
552 for(const auto& rAssignment : m_aAddressDataAssignments)
554 if(rAssignment.bColumnAssignmentsChanged)
556 //create a new node name
557 OUString sNewNode = !rAssignment.sConfigNodeName.isEmpty() ?
558 rAssignment.sConfigNodeName :
559 lcl_CreateNodeName(aAssignments);
560 OUString sSlash = "/";
561 OUString sNodePath = cAddressDataAssignments + sSlash + sNewNode + sSlash;
562 //only one new entry is written
563 Sequence< PropertyValue > aNewValues
565 comphelper::makePropertyValue(sNodePath + cDataSourceName, rAssignment.aDBData.sDataSource),
566 comphelper::makePropertyValue(sNodePath + cDataTableName, rAssignment.aDBData.sCommand),
567 comphelper::makePropertyValue(sNodePath + cDataCommandType, rAssignment.aDBData.nCommandType),
568 comphelper::makePropertyValue(sNodePath + cDBColumnAssignments, rAssignment.aDBColumnAssignments)
570 SetSetProperties(cAddressDataAssignments, aNewValues);
574 m_bUserSettingWereOverwritten = false;
577 Sequence< OUString> SwMailMergeConfigItem_Impl::GetAddressBlocks(
578 bool bConvertToConfig) const
580 Sequence< OUString> aRet(m_aAddressBlocks.size());
581 std::transform(m_aAddressBlocks.begin(), m_aAddressBlocks.end(), aRet.getArray(),
582 [this, bConvertToConfig](const OUString& rBlock) -> OUString {
583 OUString sBlock = rBlock;
584 if(bConvertToConfig)
585 lcl_ConvertToNumbers(sBlock, m_AddressHeaderSA);
586 return sBlock;
588 return aRet;
591 void SwMailMergeConfigItem_Impl::SetAddressBlocks(
592 const Sequence< OUString>& rBlocks,
593 bool bConvertFromConfig)
595 m_aAddressBlocks.clear();
596 std::transform(rBlocks.begin(), rBlocks.end(), std::back_inserter(m_aAddressBlocks),
597 [this, bConvertFromConfig](const OUString& rBlock) -> OUString {
598 OUString sBlock = rBlock;
599 if(bConvertFromConfig)
600 lcl_ConvertFromNumbers(sBlock, m_AddressHeaderSA);
601 return sBlock;
603 m_nCurrentAddressBlock = 0;
604 SetModified();
607 Sequence< OUString> SwMailMergeConfigItem_Impl::GetGreetings(
608 SwMailMergeConfigItem::Gender eType, bool bConvertToConfig) const
610 const std::vector< OUString>& rGreetings =
611 eType == SwMailMergeConfigItem::FEMALE ? m_aFemaleGreetingLines :
612 eType == SwMailMergeConfigItem::MALE ? m_aMaleGreetingLines :
613 m_aNeutralGreetingLines;
614 Sequence< OUString> aRet(rGreetings.size());
615 std::transform(rGreetings.begin(), rGreetings.end(), aRet.getArray(),
616 [this, bConvertToConfig](const OUString& rGreeting) -> OUString {
617 OUString sGreeting = rGreeting;
618 if(bConvertToConfig)
619 lcl_ConvertToNumbers(sGreeting, m_AddressHeaderSA);
620 return sGreeting;
622 return aRet;
625 void SwMailMergeConfigItem_Impl::SetGreetings(
626 SwMailMergeConfigItem::Gender eType,
627 const Sequence< OUString>& rSetGreetings,
628 bool bConvertFromConfig)
630 std::vector< OUString>& rGreetings =
631 eType == SwMailMergeConfigItem::FEMALE ? m_aFemaleGreetingLines :
632 eType == SwMailMergeConfigItem::MALE ? m_aMaleGreetingLines :
633 m_aNeutralGreetingLines;
635 rGreetings.clear();
636 std::transform(rSetGreetings.begin(), rSetGreetings.end(), std::back_inserter(rGreetings),
637 [this, bConvertFromConfig](const OUString& rGreeting) -> OUString {
638 OUString sGreeting = rGreeting;
639 if(bConvertFromConfig)
640 lcl_ConvertFromNumbers(sGreeting, m_AddressHeaderSA);
641 return sGreeting;
643 SetModified();
646 sal_Int32 SwMailMergeConfigItem_Impl::GetCurrentGreeting(
647 SwMailMergeConfigItem::Gender eType) const
649 sal_Int32 nRet;
650 switch(eType)
652 case SwMailMergeConfigItem::FEMALE: nRet = m_nCurrentFemaleGreeting ; break;
653 case SwMailMergeConfigItem::MALE: nRet = m_nCurrentMaleGreeting ; break;
654 default: nRet = m_nCurrentNeutralGreeting; break;
656 return nRet;
659 void SwMailMergeConfigItem_Impl::SetCurrentGreeting(
660 SwMailMergeConfigItem::Gender eType, sal_Int32 nIndex)
662 bool bChanged = false;
663 switch(eType)
665 case SwMailMergeConfigItem::FEMALE:
666 bChanged = m_nCurrentFemaleGreeting != nIndex;
667 m_nCurrentFemaleGreeting = nIndex;
668 break;
669 case SwMailMergeConfigItem::MALE:
670 bChanged = m_nCurrentMaleGreeting != nIndex;
671 m_nCurrentMaleGreeting = nIndex;
672 break;
673 default:
674 bChanged = m_nCurrentNeutralGreeting != nIndex;
675 m_nCurrentNeutralGreeting = nIndex;
677 if(bChanged)
678 SetModified();
681 SwMailMergeConfigItem::SwMailMergeConfigItem() :
682 m_pImpl(new SwMailMergeConfigItem_Impl),
683 m_bAddressInserted(false),
684 m_bGreetingInserted(false),
685 m_nGreetingMoves(0),
686 m_nBegin(0),
687 m_nEnd(0),
688 m_pSourceView(nullptr),
689 m_pTargetView(nullptr)
693 void SwMailMergeConfigItem::stopDBChangeListening()
695 if (m_xDBChangedListener.is())
697 uno::Reference<view::XSelectionSupplier> xSupplier = m_pSourceView->GetUNOObject();
698 xSupplier->removeSelectionChangeListener(m_xDBChangedListener);
699 m_xDBChangedListener.clear();
703 void SwMailMergeConfigItem::updateCurrentDBDataFromDocument()
705 const SwDBData& rDBData = m_pSourceView->GetWrtShell().GetDBData();
706 SetCurrentDBData(rDBData);
709 SwMailMergeConfigItem::~SwMailMergeConfigItem()
711 stopDBChangeListening();
714 void SwMailMergeConfigItem::Commit()
716 if(m_pImpl->IsModified())
717 m_pImpl->Commit();
720 const std::vector<std::pair<OUString, int>>& SwMailMergeConfigItem::GetDefaultAddressHeaders() const
722 return m_pImpl->m_AddressHeaderSA;
725 void SwMailMergeConfigItem::SetAddressBlocks(
726 const Sequence< OUString>& rBlocks)
728 m_pImpl->SetAddressBlocks(rBlocks);
731 Sequence< OUString> SwMailMergeConfigItem::GetAddressBlocks() const
733 return m_pImpl->GetAddressBlocks();
736 bool SwMailMergeConfigItem::IsAddressBlock()const
738 return m_pImpl->m_bIsAddressBlock && IsOutputToLetter();
741 void SwMailMergeConfigItem::SetAddressBlock(bool bSet)
743 m_pImpl->m_bUserSettingWereOverwritten = false;
744 if(m_pImpl->m_bIsAddressBlock != bSet)
746 m_pImpl->m_bIsAddressBlock = bSet;
747 m_pImpl->SetModified();
751 bool SwMailMergeConfigItem::IsHideEmptyParagraphs() const
753 return m_pImpl->m_bIsHideEmptyParagraphs;
756 void SwMailMergeConfigItem::SetHideEmptyParagraphs(bool bSet)
758 if(m_pImpl->m_bIsHideEmptyParagraphs != bSet)
760 m_pImpl->m_bIsHideEmptyParagraphs = bSet;
761 m_pImpl->SetModified();
765 bool SwMailMergeConfigItem::IsIncludeCountry() const
767 return m_pImpl->m_bIncludeCountry;
770 OUString& SwMailMergeConfigItem::GetExcludeCountry() const
772 return m_pImpl->m_sExcludeCountry;
775 void SwMailMergeConfigItem::SetCountrySettings(bool bSet, const OUString& rCountry)
777 if(m_pImpl->m_sExcludeCountry != rCountry ||
778 m_pImpl->m_bIncludeCountry != bSet)
780 m_pImpl->m_bIncludeCountry = bSet;
781 m_pImpl->m_sExcludeCountry = bSet ? rCountry : OUString();
782 m_pImpl->SetModified();
786 void SwMailMergeConfigItem::SetCurrentConnection(
787 Reference< XDataSource> const & xSource,
788 const SharedConnection& rConnection,
789 Reference< XColumnsSupplier> const & xColumnsSupplier,
790 const SwDBData& rDBData)
792 m_pImpl->m_xSource = xSource ;
793 m_pImpl->m_xConnection = rConnection ;
794 m_pImpl->m_xColumnsSupplier = xColumnsSupplier;
795 m_pImpl->m_aDBData = rDBData;
796 m_pImpl->m_xResultSet = nullptr;
797 m_pImpl->m_nResultSetCursorPos = 0;
798 m_pImpl->SetModified();
801 Reference< XDataSource> const & SwMailMergeConfigItem::GetSource() const
803 return m_pImpl->m_xSource;
806 SharedConnection const & SwMailMergeConfigItem::GetConnection() const
808 return m_pImpl->m_xConnection;
811 Reference< XColumnsSupplier> const & SwMailMergeConfigItem::GetColumnsSupplier()
813 if(!m_pImpl->m_xColumnsSupplier.is() && m_pImpl->m_xConnection.is())
815 m_pImpl->m_xColumnsSupplier = SwDBManager::GetColumnSupplier(m_pImpl->m_xConnection,
816 m_pImpl->m_aDBData.sCommand,
817 m_pImpl->m_aDBData.nCommandType == CommandType::TABLE ?
818 SwDBSelect::TABLE : SwDBSelect::QUERY );
820 return m_pImpl->m_xColumnsSupplier;
823 const SwDBData& SwMailMergeConfigItem::GetCurrentDBData() const
825 return m_pImpl->m_aDBData;
828 void SwMailMergeConfigItem::SetCurrentDBData( const SwDBData& rDBData)
830 if(m_pImpl->m_aDBData != rDBData)
832 m_pImpl->m_aDBData = rDBData;
833 m_pImpl->m_xConnection.clear();
834 m_pImpl->m_xSource = nullptr;
835 m_pImpl->m_xResultSet = nullptr;
836 m_pImpl->m_xColumnsSupplier = nullptr;
837 m_pImpl->SetModified();
841 Reference< XResultSet> const & SwMailMergeConfigItem::GetResultSet() const
843 if(!m_pImpl->m_xConnection.is() && !m_pImpl->m_aDBData.sDataSource.isEmpty())
845 m_pImpl->m_xConnection.reset(
846 SwDBManager::GetConnection(m_pImpl->m_aDBData.sDataSource, m_pImpl->m_xSource, m_pSourceView),
847 SharedConnection::TakeOwnership
850 if(!m_pImpl->m_xResultSet.is() && m_pImpl->m_xConnection.is())
854 Reference< XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
856 Reference<XRowSet> xRowSet( xMgr->createInstance("com.sun.star.sdb.RowSet"), UNO_QUERY );
857 Reference<XPropertySet> xRowProperties(xRowSet, UNO_QUERY);
858 xRowProperties->setPropertyValue("DataSourceName", Any(m_pImpl->m_aDBData.sDataSource));
859 xRowProperties->setPropertyValue("Command", Any(m_pImpl->m_aDBData.sCommand));
860 xRowProperties->setPropertyValue("CommandType", Any(m_pImpl->m_aDBData.nCommandType));
861 xRowProperties->setPropertyValue("FetchSize", Any(sal_Int32(10)));
862 xRowProperties->setPropertyValue("ActiveConnection", Any(m_pImpl->m_xConnection.getTyped()));
865 xRowProperties->setPropertyValue("ApplyFilter", Any(!m_pImpl->m_sFilter.isEmpty()));
866 xRowProperties->setPropertyValue("Filter", Any(m_pImpl->m_sFilter));
868 catch (const Exception&)
870 TOOLS_WARN_EXCEPTION("sw.ui", "");
872 xRowSet->execute();
873 m_pImpl->m_xResultSet = xRowSet.get();
874 m_pImpl->m_xResultSet->first();
875 m_pImpl->m_nResultSetCursorPos = 1;
877 catch (const Exception&)
879 TOOLS_WARN_EXCEPTION("sw.ui", "SwMailMergeConfigItem::GetResultSet()");
882 return m_pImpl->m_xResultSet;
885 void SwMailMergeConfigItem::DisposeResultSet()
887 m_pImpl->m_xConnection.clear();
888 if(m_pImpl->m_xResultSet.is())
890 ::comphelper::disposeComponent( m_pImpl->m_xResultSet );
894 OUString& SwMailMergeConfigItem::GetFilter() const
896 return m_pImpl->m_sFilter;
899 void SwMailMergeConfigItem::SetFilter(OUString const & rFilter)
901 if(m_pImpl->m_sFilter == rFilter)
902 return;
904 m_pImpl->m_sFilter = rFilter;
905 m_pImpl->SetModified();
906 Reference<XPropertySet> xRowProperties(m_pImpl->m_xResultSet, UNO_QUERY);
907 if(!xRowProperties.is())
908 return;
912 xRowProperties->setPropertyValue("ApplyFilter", Any(!m_pImpl->m_sFilter.isEmpty()));
913 xRowProperties->setPropertyValue("Filter", Any(m_pImpl->m_sFilter));
914 uno::Reference<XRowSet> xRowSet( m_pImpl->m_xResultSet, UNO_QUERY_THROW );
915 xRowSet->execute();
917 catch (const Exception&)
919 TOOLS_WARN_EXCEPTION("sw.ui", "SwMailMergeConfigItem::SetFilter()");
923 sal_Int32 SwMailMergeConfigItem::MoveResultSet(sal_Int32 nTarget)
925 if(!m_pImpl->m_xResultSet.is())
926 GetResultSet();
927 if(m_pImpl->m_xResultSet.is())
931 //no action if the resultset is already at the right position
932 if(m_pImpl->m_xResultSet->getRow() != nTarget)
934 if(nTarget > 0)
936 bool bMoved = m_pImpl->m_xResultSet->absolute(nTarget);
937 if(!bMoved)
939 if(nTarget > 1)
940 m_pImpl->m_xResultSet->last();
941 else if(nTarget == 1)
942 m_pImpl->m_xResultSet->first();
945 else if(nTarget == -1)
946 m_pImpl->m_xResultSet->last();
947 m_pImpl->m_nResultSetCursorPos = m_pImpl->m_xResultSet->getRow();
950 catch (const Exception&)
954 return m_pImpl->m_nResultSetCursorPos;
957 bool SwMailMergeConfigItem::IsResultSetFirstLast(bool& bIsFirst, bool& bIsLast)
959 bool bRet = false;
960 if(!m_pImpl->m_xResultSet.is())
961 GetResultSet();
962 if(m_pImpl->m_xResultSet.is())
966 bIsFirst = m_pImpl->m_xResultSet->isFirst();
967 bIsLast = m_pImpl->m_xResultSet->isLast();
968 bRet = true;
970 catch (const Exception&)
974 return bRet;
977 sal_Int32 SwMailMergeConfigItem::GetResultSetPosition() const
979 return m_pImpl->m_nResultSetCursorPos;
982 bool SwMailMergeConfigItem::IsRecordIncluded(sal_uInt32 nRecord) const
983 { return nRecord > m_nBegin && nRecord <= m_nEnd; }
985 bool SwMailMergeConfigItem::IsRecordExcluded(sal_uInt32 nRecord) const
986 { return m_aExcludedRecords.find(nRecord) != m_aExcludedRecords.end(); }
988 void SwMailMergeConfigItem::ExcludeRecord(sal_Int32 nRecord, bool bExclude)
990 if(bExclude)
991 m_aExcludedRecords.insert(nRecord);
992 else
993 m_aExcludedRecords.erase(nRecord);
996 uno::Sequence<uno::Any> SwMailMergeConfigItem::GetSelection() const
998 if(!m_pImpl->m_xResultSet.is())
999 GetResultSet();
1000 if(!m_pImpl->m_xResultSet.is())
1001 return {};
1002 m_pImpl->m_xResultSet->last();
1003 sal_uInt32 nResultSetSize = m_pImpl->m_xResultSet->getRow()+1;
1004 std::vector<uno::Any> vResult;
1005 for(sal_uInt32 nIdx=1; nIdx<nResultSetSize;++nIdx)
1006 if( !IsRecordExcluded(nIdx) && IsRecordIncluded(nIdx) )
1007 vResult.push_back(uno::Any(sal_uInt32(nIdx)));
1008 return comphelper::containerToSequence(vResult);
1012 const uno::Sequence< OUString>&
1013 SwMailMergeConfigItem::GetSavedDocuments() const
1015 return m_pImpl->m_aSavedDocuments;
1018 bool SwMailMergeConfigItem::IsOutputToLetter()const
1020 return m_pImpl->m_bIsOutputToLetter || !IsMailAvailable();
1023 void SwMailMergeConfigItem::SetOutputToLetter(bool bSet)
1025 if(m_pImpl->m_bIsOutputToLetter != bSet)
1027 m_pImpl->m_bIsOutputToLetter = bSet;
1028 m_pImpl->SetModified();
1032 bool SwMailMergeConfigItem::IsIndividualGreeting(bool bInEMail) const
1034 return bInEMail ?
1035 m_pImpl->m_bIsIndividualGreetingLineInMail :
1036 m_pImpl->m_bIsIndividualGreetingLine;
1039 void SwMailMergeConfigItem::SetIndividualGreeting(
1040 bool bSet, bool bInEMail)
1042 if(bInEMail)
1044 if(m_pImpl->m_bIsIndividualGreetingLineInMail != bSet)
1046 m_pImpl->m_bIsIndividualGreetingLineInMail = bSet;
1047 m_pImpl->SetModified();
1050 else
1052 if(m_pImpl->m_bIsIndividualGreetingLine != bSet)
1054 m_pImpl->m_bIsIndividualGreetingLine = bSet;
1055 m_pImpl->SetModified();
1060 bool SwMailMergeConfigItem::IsGreetingLine(bool bInEMail) const
1062 return bInEMail ? m_pImpl->m_bIsGreetingLineInMail : m_pImpl->m_bIsGreetingLine;
1065 void SwMailMergeConfigItem::SetGreetingLine(bool bSet, bool bInEMail)
1067 m_pImpl->m_bUserSettingWereOverwritten = false;
1068 if(bInEMail)
1070 if(m_pImpl->m_bIsGreetingLineInMail != bSet)
1072 m_pImpl->m_bIsGreetingLineInMail = bSet;
1073 m_pImpl->SetModified();
1076 else
1078 if(m_pImpl->m_bIsGreetingLine != bSet)
1080 m_pImpl->m_bIsGreetingLine = bSet;
1081 m_pImpl->SetModified();
1086 Sequence< OUString> SwMailMergeConfigItem::GetGreetings(
1087 Gender eType ) const
1089 return m_pImpl->GetGreetings(eType);
1092 void SwMailMergeConfigItem::SetGreetings(
1093 Gender eType, const Sequence< OUString>& rSetGreetings)
1095 m_pImpl->SetGreetings( eType, rSetGreetings);
1098 sal_Int32 SwMailMergeConfigItem::GetCurrentGreeting(
1099 SwMailMergeConfigItem::Gender eType) const
1101 return m_pImpl->GetCurrentGreeting(eType);
1104 void SwMailMergeConfigItem::SetCurrentGreeting(Gender eType, sal_Int32 nIndex)
1106 m_pImpl->SetCurrentGreeting(eType, nIndex);
1109 const OUString& SwMailMergeConfigItem::GetFemaleGenderValue() const
1111 return m_pImpl->m_sFemaleGenderValue;
1114 void SwMailMergeConfigItem::SetFemaleGenderValue(const OUString& rValue)
1116 if( m_pImpl->m_sFemaleGenderValue != rValue )
1118 m_pImpl->m_sFemaleGenderValue = rValue;
1119 m_pImpl->SetModified();
1123 Sequence< OUString> SwMailMergeConfigItem::GetColumnAssignment(
1124 const SwDBData& rDBData ) const
1126 Sequence< OUString> aRet;
1127 auto aAssignIter = std::find_if(m_pImpl->m_aAddressDataAssignments.begin(), m_pImpl->m_aAddressDataAssignments.end(),
1128 [&rDBData](const DBAddressDataAssignment& rAssignment) { return rAssignment.aDBData == rDBData; });
1129 if (aAssignIter != m_pImpl->m_aAddressDataAssignments.end())
1131 aRet = aAssignIter->aDBColumnAssignments;
1133 return aRet;
1136 // returns the name that is assigned as e-mail column of the current data base
1137 OUString SwMailMergeConfigItem::GetAssignedColumn(sal_uInt32 nColumn) const
1139 OUString sRet;
1140 Sequence< OUString> aAssignment = GetColumnAssignment( m_pImpl->m_aDBData );
1141 if(aAssignment.getLength() > sal::static_int_cast< sal_Int32, sal_uInt32>(nColumn) && !aAssignment[nColumn].isEmpty())
1142 sRet = aAssignment[nColumn];
1143 else if(nColumn < m_pImpl->m_AddressHeaderSA.size())
1144 sRet = m_pImpl->m_AddressHeaderSA[nColumn].first;
1145 return sRet;
1148 void SwMailMergeConfigItem::SetColumnAssignment( const SwDBData& rDBData,
1149 const Sequence< OUString>& rList)
1151 auto aAssignIter = std::find_if(m_pImpl->m_aAddressDataAssignments.begin(), m_pImpl->m_aAddressDataAssignments.end(),
1152 [&rDBData](const DBAddressDataAssignment& rAssignment) { return rAssignment.aDBData == rDBData; });
1153 if (aAssignIter != m_pImpl->m_aAddressDataAssignments.end())
1155 if(aAssignIter->aDBColumnAssignments != rList)
1157 aAssignIter->aDBColumnAssignments = rList;
1158 aAssignIter->bColumnAssignmentsChanged = true;
1161 else
1163 DBAddressDataAssignment aAssignment;
1164 aAssignment.aDBData = rDBData;
1165 aAssignment.aDBColumnAssignments = rList;
1166 aAssignment.bColumnAssignmentsChanged = true;
1167 m_pImpl->m_aAddressDataAssignments.push_back(aAssignment);
1169 m_pImpl->SetModified();
1172 bool SwMailMergeConfigItem::IsAddressFieldsAssigned() const
1174 bool bResult = true;
1175 Reference< XResultSet> xResultSet = GetResultSet();
1176 uno::Reference< XColumnsSupplier > xColsSupp( xResultSet, UNO_QUERY );
1177 if(!xColsSupp.is())
1178 return false;
1179 uno::Reference<container::XNameAccess> xCols = xColsSupp->getColumns();
1181 const std::vector<std::pair<OUString, int>>& rHeaders = GetDefaultAddressHeaders();
1182 Sequence< OUString> aAssignment =
1183 GetColumnAssignment( GetCurrentDBData() );
1184 const OUString* pAssignment = aAssignment.getConstArray();
1185 const Sequence< OUString> aBlocks = GetAddressBlocks();
1187 if(aBlocks.getLength() <= m_pImpl->GetCurrentAddressBlockIndex())
1188 return false;
1189 SwAddressIterator aIter(aBlocks[m_pImpl->GetCurrentAddressBlockIndex()]);
1190 while(aIter.HasMore())
1192 SwMergeAddressItem aItem = aIter.Next();
1193 if(aItem.bIsColumn)
1195 OUString sConvertedColumn = aItem.sText;
1196 auto nSize = std::min(sal_uInt32(rHeaders.size()), sal_uInt32(aAssignment.getLength()));
1197 for(sal_uInt32 nColumn = 0; nColumn < nSize; ++nColumn)
1199 if (rHeaders[nColumn].first == aItem.sText &&
1200 !pAssignment[nColumn].isEmpty())
1202 sConvertedColumn = pAssignment[nColumn];
1203 break;
1206 //find out if the column exists in the data base
1207 if(!xCols->hasByName(sConvertedColumn))
1209 bResult = false;
1210 break;
1214 return bResult;
1217 bool SwMailMergeConfigItem::IsGreetingFieldsAssigned() const
1219 bool bResult = true;
1221 if(!IsIndividualGreeting(false))
1222 return true;
1224 Reference< XResultSet> xResultSet = GetResultSet();
1225 uno::Reference< XColumnsSupplier > xColsSupp( xResultSet, UNO_QUERY );
1226 if(!xColsSupp.is())
1227 return false;
1228 const std::vector<std::pair<OUString, int>>& rHeaders = GetDefaultAddressHeaders();
1229 uno::Reference<container::XNameAccess> xCols = xColsSupp->getColumns();
1231 Sequence< OUString> aAssignment =
1232 GetColumnAssignment( GetCurrentDBData() );
1233 const OUString* pAssignment = aAssignment.getConstArray();
1235 const Sequence< OUString> rFemaleEntries = GetGreetings(SwMailMergeConfigItem::FEMALE);
1236 sal_Int32 nCurrentFemale = GetCurrentGreeting(SwMailMergeConfigItem::FEMALE);
1237 const Sequence< OUString> rMaleEntries = GetGreetings(SwMailMergeConfigItem::MALE);
1238 sal_Int32 nCurrentMale = GetCurrentGreeting(SwMailMergeConfigItem::MALE);
1239 OUString sMale, sFemale;
1240 if(rFemaleEntries.getLength() > nCurrentFemale)
1241 sFemale = rFemaleEntries[nCurrentFemale];
1242 if(rMaleEntries.getLength() > nCurrentMale)
1243 sMale = rMaleEntries[nCurrentMale];
1245 OUString sAddress = sFemale + sMale;
1246 SwAddressIterator aIter(sAddress);
1247 while(aIter.HasMore())
1249 SwMergeAddressItem aItem = aIter.Next();
1250 if(aItem.bIsColumn)
1252 OUString sConvertedColumn = aItem.sText;
1253 auto nSize = std::min(sal_uInt32(rHeaders.size()), sal_uInt32(aAssignment.getLength()));
1254 for(sal_uInt32 nColumn = 0; nColumn < nSize; ++nColumn)
1256 if (rHeaders[nColumn].first == aItem.sText &&
1257 !pAssignment[nColumn].isEmpty())
1259 sConvertedColumn = pAssignment[nColumn];
1260 break;
1263 //find out if the column exists in the data base
1264 if(!xCols->hasByName(sConvertedColumn))
1266 bResult = false;
1267 break;
1271 return bResult;
1274 OUString const & SwMailMergeConfigItem::GetMailDisplayName() const
1276 return m_pImpl->m_sMailDisplayName;
1279 void SwMailMergeConfigItem::SetMailDisplayName(const OUString& rName)
1281 if(m_pImpl->m_sMailDisplayName != rName)
1283 m_pImpl->m_sMailDisplayName = rName;
1284 m_pImpl->SetModified();
1288 OUString const & SwMailMergeConfigItem::GetMailAddress() const
1290 return m_pImpl->m_sMailAddress;
1293 void SwMailMergeConfigItem::SetMailAddress(const OUString& rAddress)
1295 if(m_pImpl->m_sMailAddress != rAddress )
1297 m_pImpl->m_sMailAddress = rAddress;
1298 m_pImpl->SetModified();
1302 bool SwMailMergeConfigItem::IsMailReplyTo() const
1304 return m_pImpl->m_bIsMailReplyTo;
1307 void SwMailMergeConfigItem::SetMailReplyTo(bool bSet)
1309 if(m_pImpl->m_bIsMailReplyTo != bSet)
1311 m_pImpl->m_bIsMailReplyTo = bSet;
1312 m_pImpl->SetModified();
1316 OUString const & SwMailMergeConfigItem::GetMailReplyTo() const
1318 return m_pImpl->m_sMailReplyTo;
1321 void SwMailMergeConfigItem::SetMailReplyTo(const OUString& rReplyTo)
1323 if(m_pImpl->m_sMailReplyTo != rReplyTo)
1325 m_pImpl->m_sMailReplyTo = rReplyTo;
1326 m_pImpl->SetModified();
1330 OUString const & SwMailMergeConfigItem::GetMailServer() const
1332 return m_pImpl->m_sMailServer;
1335 void SwMailMergeConfigItem::SetMailServer(const OUString& rAddress)
1337 if(m_pImpl->m_sMailServer != rAddress)
1339 m_pImpl->m_sMailServer = rAddress;
1340 m_pImpl->SetModified();
1344 sal_Int16 SwMailMergeConfigItem::GetMailPort() const
1346 // provide appropriate TCP port, based on SSL/STARTTLS status, if current port is one of the defaults
1347 switch (m_pImpl->m_nMailPort)
1349 case SECURE_PORT:
1350 case DEFAULT_PORT:
1351 return m_pImpl->m_bIsSecureConnection ? SECURE_PORT : DEFAULT_PORT;
1352 default:
1353 return m_pImpl->m_nMailPort;
1357 void SwMailMergeConfigItem::SetMailPort(sal_Int16 nSet)
1359 if(m_pImpl->m_nMailPort != nSet)
1361 m_pImpl->m_nMailPort = nSet;
1362 m_pImpl->SetModified();
1366 bool SwMailMergeConfigItem::IsSecureConnection() const
1368 return m_pImpl->m_bIsSecureConnection;
1371 void SwMailMergeConfigItem::SetSecureConnection(bool bSet)
1373 if(m_pImpl->m_bIsSecureConnection != bSet)
1375 m_pImpl->m_bIsSecureConnection = bSet;
1376 m_pImpl->SetModified();
1380 bool SwMailMergeConfigItem::IsAuthentication() const
1382 return m_pImpl->m_bIsAuthentication;
1385 void SwMailMergeConfigItem::SetAuthentication(bool bSet)
1387 if(m_pImpl->m_bIsAuthentication != bSet)
1389 m_pImpl->m_bIsAuthentication = bSet;
1390 m_pImpl->SetModified();
1394 OUString const & SwMailMergeConfigItem::GetMailUserName() const
1396 return m_pImpl->m_sMailUserName;
1399 void SwMailMergeConfigItem::SetMailUserName(const OUString& rName)
1401 if(m_pImpl->m_sMailUserName != rName)
1403 m_pImpl->m_sMailUserName = rName;
1404 m_pImpl->SetModified();
1408 OUString const & SwMailMergeConfigItem::GetMailPassword() const
1410 return m_pImpl->m_sMailPassword;
1413 void SwMailMergeConfigItem::SetMailPassword(const OUString& rPassword)
1415 if(m_pImpl->m_sMailPassword != rPassword)
1417 m_pImpl->m_sMailPassword = rPassword;
1418 m_pImpl->SetModified();
1422 bool SwMailMergeConfigItem::IsSMTPAfterPOP() const
1424 return m_pImpl->m_bIsSMPTAfterPOP;
1427 void SwMailMergeConfigItem::SetSMTPAfterPOP(bool bSet)
1429 if( m_pImpl->m_bIsSMPTAfterPOP != bSet)
1431 m_pImpl->m_bIsSMPTAfterPOP = bSet;
1432 m_pImpl->SetModified();
1436 OUString const & SwMailMergeConfigItem::GetInServerName() const
1438 return m_pImpl->m_sInServerName;
1441 void SwMailMergeConfigItem::SetInServerName(const OUString& rServer)
1443 if(m_pImpl->m_sInServerName != rServer)
1445 m_pImpl->m_sInServerName = rServer;
1446 m_pImpl->SetModified();
1450 sal_Int16 SwMailMergeConfigItem::GetInServerPort() const
1452 // provide appropriate TCP port as user toggles between POP/IMAP if current port is one of the defaults
1453 switch (m_pImpl->m_nInServerPort)
1455 case POP_SECURE_PORT:
1456 case POP_PORT:
1457 case IMAP_SECURE_PORT:
1458 case IMAP_PORT:
1459 if ( m_pImpl->m_bInServerPOP )
1460 return m_pImpl->m_bIsSecureConnection ? POP_SECURE_PORT : POP_PORT;
1461 else
1462 return m_pImpl->m_bIsSecureConnection ? IMAP_SECURE_PORT : IMAP_PORT;
1463 default:
1464 return m_pImpl->m_nInServerPort;
1468 void SwMailMergeConfigItem::SetInServerPort(sal_Int16 nSet)
1470 if( m_pImpl->m_nInServerPort != nSet)
1472 m_pImpl->m_nInServerPort = nSet;
1473 m_pImpl->SetModified();
1477 bool SwMailMergeConfigItem::IsInServerPOP() const
1479 return m_pImpl->m_bInServerPOP;
1482 void SwMailMergeConfigItem::SetInServerPOP(bool bSet)
1484 if( m_pImpl->m_bInServerPOP != bSet)
1486 m_pImpl->m_bInServerPOP = bSet;
1487 m_pImpl->SetModified();
1491 OUString const & SwMailMergeConfigItem::GetInServerUserName() const
1493 return m_pImpl->m_sInServerUserName;
1496 void SwMailMergeConfigItem::SetInServerUserName(const OUString& rName)
1498 if( m_pImpl->m_sInServerUserName != rName)
1500 m_pImpl->m_sInServerUserName = rName;
1501 m_pImpl->SetModified();
1505 OUString const & SwMailMergeConfigItem::GetInServerPassword() const
1507 return m_pImpl->m_sInServerPassword;
1510 void SwMailMergeConfigItem::SetInServerPassword(const OUString& rPassword)
1512 if(m_pImpl->m_sInServerPassword != rPassword)
1514 m_pImpl->m_sInServerPassword = rPassword;
1515 m_pImpl->SetModified();
1519 void SwMailMergeConfigItem::DocumentReloaded()
1521 m_bGreetingInserted = false;
1522 m_bAddressInserted = false;
1525 bool SwMailMergeConfigItem::IsMailAvailable() const
1527 return m_pImpl->m_bIsEMailSupported;
1530 void SwMailMergeConfigItem::AddMergedDocument(SwDocMergeInfo const & rInfo)
1532 m_pImpl->m_aMergeInfos.push_back(rInfo);
1535 SwDocMergeInfo& SwMailMergeConfigItem::GetDocumentMergeInfo(sal_uInt32 nDocument)
1537 assert(nDocument < m_pImpl->m_aMergeInfos.size());
1538 return m_pImpl->m_aMergeInfos[nDocument];
1542 sal_uInt32 SwMailMergeConfigItem::GetMergedDocumentCount()
1544 if(m_pTargetView)
1545 return m_pImpl->m_aMergeInfos.size();
1546 else
1548 sal_Int32 nRestore = GetResultSetPosition();
1549 MoveResultSet(-1);
1550 sal_Int32 nRet = GetResultSetPosition();
1551 MoveResultSet( nRestore );
1552 nRet -= m_aExcludedRecords.size();
1553 return nRet >= 0 ? nRet : 0;
1557 static SwView* lcl_ExistsView(SwView* pView)
1559 SfxViewShell* pViewShell = SfxViewShell::GetFirst( false, checkSfxViewShell<SwView> );
1560 while(pViewShell)
1562 if(pViewShell == pView)
1563 return pView;
1565 pViewShell = SfxViewShell::GetNext( *pViewShell, false, checkSfxViewShell<SwView> );
1567 return nullptr;
1570 SwView* SwMailMergeConfigItem::GetTargetView()
1572 //make sure that the pointer is really valid - the document may have been closed manually
1573 if(m_pTargetView)
1575 m_pTargetView = lcl_ExistsView(m_pTargetView);
1577 return m_pTargetView;
1580 void SwMailMergeConfigItem::SetTargetView(SwView* pView)
1582 m_pTargetView = pView;
1583 //reset the document merge counter
1584 if(!m_pTargetView)
1586 m_pImpl->m_aMergeInfos.clear();
1590 SwView* SwMailMergeConfigItem::GetSourceView()
1592 m_pSourceView = lcl_ExistsView(m_pSourceView);
1593 return m_pSourceView;
1596 namespace {
1598 //This implements XSelectionChangeListener and XDispatch because the
1599 //broadcaster uses this combo to determine if to send the database-changed
1600 //update. Its probably that listening to statusChanged at some other level is
1601 //equivalent to this. See the other call to SwXDispatch::GetDBChangeURL for
1602 //the broadcaster of the event.
1603 class DBChangeListener : public cppu::WeakImplHelper<css::view::XSelectionChangeListener, css::frame::XDispatch>
1605 SwMailMergeConfigItem& m_rParent;
1606 public:
1607 explicit DBChangeListener(SwMailMergeConfigItem& rParent)
1608 : m_rParent(rParent)
1612 virtual void SAL_CALL selectionChanged(const EventObject& /*rEvent*/) override
1616 virtual void SAL_CALL disposing(const EventObject&) override
1618 m_rParent.stopDBChangeListening();
1621 virtual void SAL_CALL dispatch(const css::util::URL& rURL, const css::uno::Sequence< css::beans::PropertyValue >& /*rArgs*/) override
1623 if (rURL.Complete.equalsAscii(SwXDispatch::GetDBChangeURL()))
1624 m_rParent.updateCurrentDBDataFromDocument();
1627 virtual void SAL_CALL addStatusListener(const css::uno::Reference< css::frame::XStatusListener >&, const css::util::URL&) override
1631 virtual void SAL_CALL removeStatusListener(const css::uno::Reference< css::frame::XStatusListener >&, const css::util::URL&) override
1638 void SwMailMergeConfigItem::SetSourceView(SwView* pView)
1640 if (m_xDBChangedListener.is())
1642 uno::Reference<view::XSelectionSupplier> xSupplier = m_pSourceView->GetUNOObject();
1643 xSupplier->removeSelectionChangeListener(m_xDBChangedListener);
1644 m_xDBChangedListener.clear();
1647 m_pSourceView = pView;
1649 if (!m_pSourceView)
1650 return;
1652 std::vector<OUString> aDBNameList;
1653 std::vector<OUString> aAllDBNames;
1654 m_pSourceView->GetWrtShell().GetAllUsedDB( aDBNameList, &aAllDBNames );
1655 if(!aDBNameList.empty())
1657 // if fields are available there is usually no need of an addressblock and greeting
1658 if(!m_pImpl->m_bUserSettingWereOverwritten)
1660 if( m_pImpl->m_bIsAddressBlock
1661 || m_pImpl->m_bIsGreetingLineInMail
1662 || m_pImpl->m_bIsGreetingLine )
1664 //store user settings
1665 m_pImpl->m_bUserSettingWereOverwritten = true;
1666 m_pImpl->m_bIsAddressBlock_LastUserSetting = m_pImpl->m_bIsAddressBlock;
1667 m_pImpl->m_bIsGreetingLineInMail_LastUserSetting = m_pImpl->m_bIsGreetingLineInMail;
1668 m_pImpl->m_bIsGreetingLine_LastUserSetting = m_pImpl->m_bIsGreetingLine;
1670 //set all to false
1671 m_pImpl->m_bIsAddressBlock = false;
1672 m_pImpl->m_bIsGreetingLineInMail = false;
1673 m_pImpl->m_bIsGreetingLine = false;
1675 m_pImpl->SetModified();
1679 else if( m_pImpl->m_bUserSettingWereOverwritten )
1681 //restore last user settings:
1682 m_pImpl->m_bIsAddressBlock = m_pImpl->m_bIsAddressBlock_LastUserSetting;
1683 m_pImpl->m_bIsGreetingLineInMail = m_pImpl->m_bIsGreetingLineInMail_LastUserSetting;
1684 m_pImpl->m_bIsGreetingLine = m_pImpl->m_bIsGreetingLine_LastUserSetting;
1686 m_pImpl->m_bUserSettingWereOverwritten = false;
1689 if (!m_xDBChangedListener.is())
1691 m_xDBChangedListener.set(new DBChangeListener(*this));
1694 uno::Reference<view::XSelectionSupplier> xSupplier = m_pSourceView->GetUNOObject();
1695 xSupplier->addSelectionChangeListener(m_xDBChangedListener);
1698 void SwMailMergeConfigItem::SetCurrentAddressBlockIndex( sal_Int32 nSet )
1700 m_pImpl->SetCurrentAddressBlockIndex( nSet );
1703 sal_Int32 SwMailMergeConfigItem::GetCurrentAddressBlockIndex() const
1705 return m_pImpl->GetCurrentAddressBlockIndex();
1708 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */