lok: Hide file linking in section
[LibreOffice.git] / sw / source / ui / config / mailconfigpage.cxx
blob1e3946208233c164f175fb0a71917430d630d1ca
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 .
20 #include <swmodule.hxx>
21 #include <swtypes.hxx>
22 #include <mailconfigpage.hxx>
23 #include <mmconfigitem.hxx>
24 #include <mailmergehelper.hxx>
25 #include <cmdid.h>
26 #include <vcl/svapp.hxx>
27 #include <vcl/weld.hxx>
28 #include <comphelper/processfactory.hxx>
29 #include <com/sun/star/mail/MailServiceType.hpp>
30 #include <com/sun/star/mail/XMailService.hpp>
31 #include <com/sun/star/mail/MailServiceProvider.hpp>
32 #include <globals.hrc>
33 #include <dbui.hrc>
34 #include <strings.hrc>
35 #include <bitmaps.hlst>
37 using namespace ::com::sun::star;
38 using namespace ::com::sun::star::lang;
39 using namespace ::com::sun::star::mail;
40 using namespace ::com::sun::star::beans;
42 class SwTestAccountSettingsDialog : public SfxDialogController
44 ImplSVEvent* m_pPostedEvent;
45 OUString m_sCompleted;
46 OUString m_sFailed;
47 OUString m_sErrorServer;
48 bool m_bStop;
50 SwMailConfigPage* m_pParent;
52 std::unique_ptr<weld::Button> m_xStopPB;
53 std::unique_ptr<weld::TextView> m_xErrorsED;
54 std::unique_ptr<weld::Label> m_xEstablish;
55 std::unique_ptr<weld::Label> m_xFind;
56 std::unique_ptr<weld::Label> m_xResult1;
57 std::unique_ptr<weld::Label> m_xResult2;
58 std::unique_ptr<weld::Image> m_xImage1;
59 std::unique_ptr<weld::Image> m_xImage2;
60 std::unique_ptr<weld::Image> m_xImage3;
61 std::unique_ptr<weld::Image> m_xImage4;
63 void Test();
64 DECL_LINK(StopHdl, weld::Button&, void);
65 DECL_LINK(TestHdl, void*, void);
66 public:
67 explicit SwTestAccountSettingsDialog(SwMailConfigPage* pParent);
68 virtual ~SwTestAccountSettingsDialog() override;
71 class SwAuthenticationSettingsDialog : public SfxDialogController
73 SwMailMergeConfigItem& m_rConfigItem;
75 std::unique_ptr<weld::CheckButton> m_xAuthenticationCB;
76 std::unique_ptr<weld::RadioButton> m_xSeparateAuthenticationRB;
77 std::unique_ptr<weld::RadioButton> m_xSMTPAfterPOPRB;
78 std::unique_ptr<weld::Label> m_xOutgoingServerFT;
79 std::unique_ptr<weld::Label> m_xUserNameFT;
80 std::unique_ptr<weld::Entry> m_xUserNameED;
81 std::unique_ptr<weld::Label> m_xOutPasswordFT;
82 std::unique_ptr<weld::Entry> m_xOutPasswordED;
83 std::unique_ptr<weld::Label> m_xIncomingServerFT;
84 std::unique_ptr<weld::Label> m_xServerFT;
85 std::unique_ptr<weld::Entry> m_xServerED;
86 std::unique_ptr<weld::Label> m_xPortFT;
87 std::unique_ptr<weld::SpinButton> m_xPortNF;
88 std::unique_ptr<weld::Label> m_xProtocolFT;
89 std::unique_ptr<weld::RadioButton> m_xPOP3RB;
90 std::unique_ptr<weld::RadioButton> m_xIMAPRB;
91 std::unique_ptr<weld::Label> m_xInUsernameFT;
92 std::unique_ptr<weld::Entry> m_xInUsernameED;
93 std::unique_ptr<weld::Label> m_xInPasswordFT;
94 std::unique_ptr<weld::Entry> m_xInPasswordED;
95 std::unique_ptr<weld::Button> m_xOKPB;
97 DECL_LINK(OKHdl_Impl, weld::Button&, void);
98 DECL_LINK(CheckBoxHdl_Impl, weld::ToggleButton&, void);
99 DECL_LINK(RadioButtonHdl_Impl, weld::ToggleButton&, void);
100 DECL_LINK(InServerHdl_Impl, weld::Button&, void);
102 public:
103 SwAuthenticationSettingsDialog(weld::Window* pParent, SwMailMergeConfigItem& rItem);
106 SwMailConfigPage::SwMailConfigPage(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet& rSet)
107 : SfxTabPage(pPage, pController, "modules/swriter/ui/mailconfigpage.ui", "MailConfigPage", &rSet)
108 , m_pConfigItem(new SwMailMergeConfigItem)
109 , m_xDisplayNameED(m_xBuilder->weld_entry("displayname"))
110 , m_xAddressED(m_xBuilder->weld_entry("address"))
111 , m_xReplyToCB(m_xBuilder->weld_check_button("replytocb"))
112 , m_xReplyToFT(m_xBuilder->weld_label("replyto_label"))
113 , m_xReplyToED(m_xBuilder->weld_entry("replyto"))
114 , m_xServerED(m_xBuilder->weld_entry("server"))
115 , m_xPortNF(m_xBuilder->weld_spin_button("port"))
116 , m_xSecureCB(m_xBuilder->weld_check_button("secure"))
117 , m_xServerAuthenticationPB(m_xBuilder->weld_button("serverauthentication"))
118 , m_xTestPB(m_xBuilder->weld_button("test"))
120 m_xReplyToCB->connect_toggled(LINK(this, SwMailConfigPage, ReplyToHdl));
121 m_xServerAuthenticationPB->connect_clicked(LINK(this, SwMailConfigPage, AuthenticationHdl));
122 m_xTestPB->connect_clicked(LINK(this, SwMailConfigPage, TestHdl));
123 m_xSecureCB->connect_toggled(LINK(this, SwMailConfigPage, SecureHdl));
126 SwMailConfigPage::~SwMailConfigPage()
128 m_pConfigItem.reset();
131 std::unique_ptr<SfxTabPage> SwMailConfigPage::Create(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet* rAttrSet)
133 return std::make_unique<SwMailConfigPage>(pPage, pController, *rAttrSet);
136 bool SwMailConfigPage::FillItemSet( SfxItemSet* /*rSet*/ )
138 if (m_xDisplayNameED->get_value_changed_from_saved())
139 m_pConfigItem->SetMailDisplayName(m_xDisplayNameED->get_text());
140 if (m_xAddressED->get_value_changed_from_saved())
141 m_pConfigItem->SetMailAddress(m_xAddressED->get_text());
142 if (m_xReplyToCB->get_state_changed_from_saved() )
143 m_pConfigItem->SetMailReplyTo(m_xReplyToCB->get_active());
144 if (m_xReplyToED->get_value_changed_from_saved())
145 m_pConfigItem->SetMailReplyTo(m_xReplyToED->get_text());
146 if (m_xServerED->get_value_changed_from_saved())
147 m_pConfigItem->SetMailServer(m_xServerED->get_text());
149 m_pConfigItem->SetMailPort(m_xPortNF->get_value());
150 m_pConfigItem->SetSecureConnection(m_xSecureCB->get_active());
152 m_pConfigItem->Commit();
153 return true;
156 void SwMailConfigPage::Reset( const SfxItemSet* /*rSet*/ )
158 m_xDisplayNameED->set_text(m_pConfigItem->GetMailDisplayName());
159 m_xAddressED->set_text(m_pConfigItem->GetMailAddress());
161 m_xReplyToED->set_text(m_pConfigItem->GetMailReplyTo()) ;
162 m_xReplyToCB->set_active(m_pConfigItem->IsMailReplyTo());
163 ReplyToHdl(*m_xReplyToCB);
165 m_xServerED->set_text(m_pConfigItem->GetMailServer());
166 m_xPortNF->set_value(m_pConfigItem->GetMailPort());
168 m_xSecureCB->set_active(m_pConfigItem->IsSecureConnection());
170 m_xDisplayNameED->save_value();
171 m_xAddressED->save_value();
172 m_xReplyToCB->save_state();
173 m_xReplyToED->save_value();
174 m_xServerED->save_value();
175 m_xPortNF->save_value();
176 m_xSecureCB->save_state();
179 IMPL_LINK(SwMailConfigPage, ReplyToHdl, weld::ToggleButton&, rBox, void)
181 bool bEnable = rBox.get_active();
182 m_xReplyToFT->set_sensitive(bEnable);
183 m_xReplyToED->set_sensitive(bEnable);
186 IMPL_LINK_NOARG(SwMailConfigPage, AuthenticationHdl, weld::Button&, void)
188 m_pConfigItem->SetMailAddress(m_xAddressED->get_text());
190 SwAuthenticationSettingsDialog aDlg(GetFrameWeld(), *m_pConfigItem);
191 aDlg.run();
194 IMPL_LINK_NOARG(SwMailConfigPage, TestHdl, weld::Button&, void)
196 SwTestAccountSettingsDialog aDlg(this);
197 aDlg.run();
200 IMPL_LINK(SwMailConfigPage, SecureHdl, weld::ToggleButton&, rBox, void)
202 bool bEnable = rBox.get_active();
203 m_pConfigItem->SetSecureConnection(bEnable);
204 m_pConfigItem->SetMailPort(m_xPortNF->get_value());
205 m_xPortNF->set_value(m_pConfigItem->GetMailPort());
208 SwTestAccountSettingsDialog::SwTestAccountSettingsDialog(SwMailConfigPage* pParent)
209 : SfxDialogController(pParent->GetFrameWeld(), "modules/swriter/ui/testmailsettings.ui", "TestMailSettings")
210 , m_bStop(false)
211 , m_pParent(pParent)
212 , m_xStopPB(m_xBuilder->weld_button("stop"))
213 , m_xErrorsED(m_xBuilder->weld_text_view("errors"))
214 , m_xEstablish(m_xBuilder->weld_label("establish"))
215 , m_xFind(m_xBuilder->weld_label("find"))
216 , m_xResult1(m_xBuilder->weld_label("result1"))
217 , m_xResult2(m_xBuilder->weld_label("result2"))
218 , m_xImage1(m_xBuilder->weld_image("image1"))
219 , m_xImage2(m_xBuilder->weld_image("image2"))
220 , m_xImage3(m_xBuilder->weld_image("image3"))
221 , m_xImage4(m_xBuilder->weld_image("image4"))
223 m_xErrorsED->set_size_request(m_xErrorsED->get_approximate_digit_width() * 72,
224 m_xErrorsED->get_height_rows(8));
225 m_sErrorServer = m_xErrorsED->get_text();
226 m_xErrorsED->set_text("");
227 m_sCompleted = m_xResult1->get_label();
228 m_sFailed = m_xResult2->get_label();
230 m_xStopPB->connect_clicked(LINK(this, SwTestAccountSettingsDialog, StopHdl));
232 m_pPostedEvent = Application::PostUserEvent(LINK(this, SwTestAccountSettingsDialog, TestHdl));
235 SwTestAccountSettingsDialog::~SwTestAccountSettingsDialog()
237 if (m_pPostedEvent)
239 Application::RemoveUserEvent(m_pPostedEvent);
243 IMPL_LINK_NOARG(SwTestAccountSettingsDialog, StopHdl, weld::Button&, void)
245 m_bStop = true;
248 IMPL_LINK_NOARG(SwTestAccountSettingsDialog, TestHdl, void*, void)
250 m_pPostedEvent = nullptr;
251 weld::WaitObject aWait(m_xDialog.get());
252 Test();
255 void SwTestAccountSettingsDialog::Test()
257 uno::Reference<uno::XComponentContext> xContext = ::comphelper::getProcessComponentContext();
259 OUString sException;
261 bool bIsLoggedIn = false;
262 bool bIsServer = false;
265 uno::Reference< mail::XMailService > xInMailService;
266 uno::Reference< mail::XMailServiceProvider > xMailServiceProvider(
267 mail::MailServiceProvider::create(xContext) );
268 uno::Reference< mail::XMailService > xMailService =
269 xMailServiceProvider->create(
270 mail::MailServiceType_SMTP);
271 if(m_bStop)
272 return;
273 uno::Reference<XConnectionListener> xConnectionListener(new SwConnectionListener());
275 if(m_pParent->m_pConfigItem->IsAuthentication() &&
276 m_pParent->m_pConfigItem->IsSMTPAfterPOP())
278 xInMailService = xMailServiceProvider->create(
279 m_pParent->m_pConfigItem->IsInServerPOP() ?
280 mail::MailServiceType_POP3 : mail::MailServiceType_IMAP);
281 if(m_bStop)
282 return;
283 //authenticate at the POP or IMAP server first
284 uno::Reference<XAuthenticator> xAuthenticator =
285 new SwAuthenticator(
286 m_pParent->m_pConfigItem->GetInServerUserName(),
287 m_pParent->m_pConfigItem->GetInServerPassword(),
288 m_xDialog.get());
290 xInMailService->addConnectionListener(xConnectionListener);
291 //check connection
292 uno::Reference< uno::XCurrentContext> xConnectionContext =
293 new SwConnectionContext(
294 m_pParent->m_pConfigItem->GetInServerName(),
295 m_pParent->m_pConfigItem->GetInServerPort(),
296 "Insecure");
297 xInMailService->connect(xConnectionContext, xAuthenticator);
299 if(m_bStop)
300 return;
301 uno::Reference<XAuthenticator> xAuthenticator;
302 if(m_pParent->m_pConfigItem->IsAuthentication() &&
303 !m_pParent->m_pConfigItem->IsSMTPAfterPOP() &&
304 !m_pParent->m_pConfigItem->GetMailUserName().isEmpty())
305 xAuthenticator =
306 new SwAuthenticator(
307 m_pParent->m_pConfigItem->GetMailUserName(),
308 m_pParent->m_pConfigItem->GetMailPassword(),
309 m_xDialog.get());
310 else
311 xAuthenticator = new SwAuthenticator();
313 xMailService->addConnectionListener(xConnectionListener);
314 if(m_bStop)
315 return;
316 //just to check if the server exists
317 xMailService->getSupportedConnectionTypes();
318 if(m_bStop)
319 return;
320 bIsServer = true;
321 //check connection
322 uno::Reference< uno::XCurrentContext> xConnectionContext =
323 new SwConnectionContext(
324 m_pParent->m_xServerED->get_text(),
325 m_pParent->m_xPortNF->get_value(),
326 m_pParent->m_xSecureCB->get_active() ? OUString("Ssl") : OUString("Insecure"));
327 xMailService->connect(xConnectionContext, xAuthenticator);
328 bIsLoggedIn = xMailService->isConnected();
329 if( xInMailService.is() )
330 xInMailService->disconnect();
331 if( xMailService->isConnected())
332 xMailService->disconnect();
334 catch (const uno::Exception& e)
336 sException = e.Message;
339 m_xResult1->set_label(bIsServer ? m_sCompleted : m_sFailed);
340 m_xImage1->set_visible(!bIsServer);
341 m_xImage3->set_visible(bIsServer);
343 m_xResult2->set_label(bIsLoggedIn ? m_sCompleted : m_sFailed);
344 m_xImage2->set_visible(!bIsLoggedIn);
345 m_xImage4->set_visible(bIsLoggedIn);
347 if (!bIsServer || !bIsLoggedIn)
349 OUString aErrorMessage(m_sErrorServer);
350 if (!sException.isEmpty())
351 aErrorMessage += "\n--\n" + sException;
352 m_xErrorsED->set_text(aErrorMessage);
356 SwMailConfigDlg::SwMailConfigDlg(weld::Window* pParent, SfxItemSet& rSet)
357 : SfxSingleTabDialogController(pParent, &rSet)
359 // create TabPage
360 SetTabPage(SwMailConfigPage::Create(get_content_area(), this, &rSet));
361 m_xDialog->set_title(SwResId(STR_MAILCONFIG_DLG_TITLE));
364 SwAuthenticationSettingsDialog::SwAuthenticationSettingsDialog(
365 weld::Window* pParent, SwMailMergeConfigItem& rItem)
366 : SfxDialogController(pParent, "modules/swriter/ui/authenticationsettingsdialog.ui", "AuthenticationSettingsDialog")
367 , m_rConfigItem(rItem)
368 , m_xAuthenticationCB(m_xBuilder->weld_check_button("authentication"))
369 , m_xSeparateAuthenticationRB(m_xBuilder->weld_radio_button("separateauthentication"))
370 , m_xSMTPAfterPOPRB(m_xBuilder->weld_radio_button("smtpafterpop"))
371 , m_xOutgoingServerFT(m_xBuilder->weld_label("label1"))
372 , m_xUserNameFT(m_xBuilder->weld_label("username_label"))
373 , m_xUserNameED(m_xBuilder->weld_entry("username"))
374 , m_xOutPasswordFT(m_xBuilder->weld_label("outpassword_label"))
375 , m_xOutPasswordED(m_xBuilder->weld_entry("outpassword"))
376 , m_xIncomingServerFT(m_xBuilder->weld_label("label2"))
377 , m_xServerFT(m_xBuilder->weld_label("server_label"))
378 , m_xServerED(m_xBuilder->weld_entry("server"))
379 , m_xPortFT(m_xBuilder->weld_label("port_label"))
380 , m_xPortNF(m_xBuilder->weld_spin_button("port"))
381 , m_xProtocolFT(m_xBuilder->weld_label("label3"))
382 , m_xPOP3RB(m_xBuilder->weld_radio_button("pop3"))
383 , m_xIMAPRB(m_xBuilder->weld_radio_button("imap"))
384 , m_xInUsernameFT(m_xBuilder->weld_label("inusername_label"))
385 , m_xInUsernameED(m_xBuilder->weld_entry("inusername"))
386 , m_xInPasswordFT(m_xBuilder->weld_label("inpassword_label"))
387 , m_xInPasswordED(m_xBuilder->weld_entry("inpassword"))
388 , m_xOKPB(m_xBuilder->weld_button("ok"))
390 m_xAuthenticationCB->connect_toggled( LINK( this, SwAuthenticationSettingsDialog, CheckBoxHdl_Impl));
391 Link<weld::ToggleButton&,void> aRBLink = LINK( this, SwAuthenticationSettingsDialog, RadioButtonHdl_Impl );
392 m_xSeparateAuthenticationRB->connect_toggled( aRBLink );
393 m_xSMTPAfterPOPRB->connect_toggled( aRBLink );
394 m_xOKPB->connect_clicked( LINK( this, SwAuthenticationSettingsDialog, OKHdl_Impl));
395 Link<weld::Button&,void> aInServerLink = LINK( this, SwAuthenticationSettingsDialog, InServerHdl_Impl );
396 m_xPOP3RB->connect_clicked( aInServerLink );
397 m_xIMAPRB->connect_clicked( aInServerLink );
399 m_xAuthenticationCB->set_active(m_rConfigItem.IsAuthentication());
400 if (m_rConfigItem.IsSMTPAfterPOP())
401 m_xSMTPAfterPOPRB->set_active(true);
402 else
403 m_xSeparateAuthenticationRB->set_active(true);
404 m_xUserNameED->set_text(m_rConfigItem.GetMailUserName());
405 m_xOutPasswordED->set_text(m_rConfigItem.GetMailPassword());
407 m_xServerED->set_text(m_rConfigItem.GetInServerName());
408 m_xPortNF->set_value(m_rConfigItem.GetInServerPort());
409 if (m_rConfigItem.IsInServerPOP())
410 m_xPOP3RB->set_active(true);
411 else
412 m_xIMAPRB->set_active(true);
413 m_xInUsernameED->set_text(m_rConfigItem.GetInServerUserName());
414 m_xInPasswordED->set_text(m_rConfigItem.GetInServerPassword());
416 CheckBoxHdl_Impl(*m_xAuthenticationCB);
419 IMPL_LINK_NOARG(SwAuthenticationSettingsDialog, OKHdl_Impl, weld::Button&, void)
421 m_rConfigItem.SetAuthentication( m_xAuthenticationCB->get_active() );
422 m_rConfigItem.SetSMTPAfterPOP(m_xSMTPAfterPOPRB->get_active());
423 m_rConfigItem.SetMailUserName(m_xUserNameED->get_text());
424 m_rConfigItem.SetMailPassword(m_xOutPasswordED->get_text());
425 m_rConfigItem.SetInServerName(m_xServerED->get_text());
426 m_rConfigItem.SetInServerPort(m_xPortNF->get_value());
427 m_rConfigItem.SetInServerPOP(m_xPOP3RB->get_active());
428 m_rConfigItem.SetInServerUserName(m_xInUsernameED->get_text());
429 m_rConfigItem.SetInServerPassword(m_xInPasswordED->get_text());
430 m_xDialog->response(RET_OK);
433 IMPL_LINK( SwAuthenticationSettingsDialog, CheckBoxHdl_Impl, weld::ToggleButton&, rBox, void)
435 bool bChecked = rBox.get_active();
436 m_xSeparateAuthenticationRB->set_sensitive(bChecked);
437 m_xSMTPAfterPOPRB->set_sensitive(bChecked);
438 RadioButtonHdl_Impl(*m_xSeparateAuthenticationRB);
441 IMPL_LINK_NOARG(SwAuthenticationSettingsDialog, RadioButtonHdl_Impl, weld::ToggleButton&, void)
443 bool bSeparate = m_xSeparateAuthenticationRB->get_active();
444 bool bIsEnabled = m_xSeparateAuthenticationRB->get_sensitive();
445 bool bNotSeparate = !bSeparate && bIsEnabled;
446 bSeparate &= bIsEnabled;
448 if (bSeparate && m_xUserNameED->get_text().isEmpty())
449 m_xUserNameED->set_text(m_rConfigItem.GetMailAddress());
450 else if (!bSeparate && m_xUserNameED->get_text() == m_rConfigItem.GetMailAddress())
451 m_xUserNameED->set_text("");
453 if (bNotSeparate && m_xInUsernameED->get_text().isEmpty())
454 m_xInUsernameED->set_text(m_rConfigItem.GetMailAddress());
455 else if (!bNotSeparate && m_xInUsernameED->get_text() == m_rConfigItem.GetMailAddress())
456 m_xInUsernameED->set_text("");
458 m_xOutgoingServerFT->set_sensitive(bSeparate);
459 m_xUserNameFT->set_sensitive(bSeparate);
460 m_xUserNameED->set_sensitive(bSeparate);
461 m_xOutPasswordFT->set_sensitive(bSeparate);
462 m_xOutPasswordED->set_sensitive(bSeparate);
464 m_xIncomingServerFT->set_sensitive(bNotSeparate);
465 m_xServerFT->set_sensitive(bNotSeparate);
466 m_xServerED->set_sensitive(bNotSeparate);
467 m_xPortFT->set_sensitive(bNotSeparate);
468 m_xPortNF->set_sensitive(bNotSeparate);
469 m_xInUsernameFT->set_sensitive(bNotSeparate);
470 m_xInUsernameED->set_sensitive(bNotSeparate);
471 m_xProtocolFT->set_sensitive(bNotSeparate);
472 m_xPOP3RB->set_sensitive(bNotSeparate);
473 m_xIMAPRB->set_sensitive(bNotSeparate);
474 m_xInPasswordFT->set_sensitive(bNotSeparate);
475 m_xInPasswordED->set_sensitive(bNotSeparate);
478 IMPL_LINK_NOARG( SwAuthenticationSettingsDialog, InServerHdl_Impl, weld::Button&, void)
480 bool bPOP = m_xPOP3RB->get_active();
481 m_rConfigItem.SetInServerPOP(bPOP);
482 m_xPortNF->set_value(m_rConfigItem.GetInServerPort());
485 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */