android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / ui / config / mailconfigpage.cxx
blob4f2d996b6f56b9e6ec88a43172cc7c27887e747d
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 <swtypes.hxx>
21 #include <mailconfigpage.hxx>
22 #include <mmconfigitem.hxx>
23 #include <mailmergehelper.hxx>
24 #include <vcl/svapp.hxx>
25 #include <vcl/weld.hxx>
26 #include <comphelper/processfactory.hxx>
27 #include <com/sun/star/mail/MailServiceType.hpp>
28 #include <com/sun/star/mail/XMailService.hpp>
29 #include <com/sun/star/mail/MailServiceProvider.hpp>
30 #include <strings.hrc>
32 using namespace ::com::sun::star;
33 using namespace ::com::sun::star::lang;
34 using namespace ::com::sun::star::mail;
35 using namespace ::com::sun::star::beans;
37 class SwTestAccountSettingsDialog : public SfxDialogController
39 ImplSVEvent* m_pPostedEvent;
40 OUString m_sCompleted;
41 OUString m_sFailed;
42 OUString m_sErrorServer;
43 bool m_bStop;
45 SwMailConfigPage* m_pParent;
47 std::unique_ptr<weld::Button> m_xStopPB;
48 std::unique_ptr<weld::TextView> m_xErrorsED;
49 std::unique_ptr<weld::Label> m_xResult1;
50 std::unique_ptr<weld::Label> m_xResult2;
51 std::unique_ptr<weld::Image> m_xImage1;
52 std::unique_ptr<weld::Image> m_xImage2;
53 std::unique_ptr<weld::Image> m_xImage3;
54 std::unique_ptr<weld::Image> m_xImage4;
56 void Test();
57 DECL_LINK(StopHdl, weld::Button&, void);
58 DECL_LINK(TestHdl, void*, void);
59 public:
60 explicit SwTestAccountSettingsDialog(SwMailConfigPage* pParent);
61 virtual ~SwTestAccountSettingsDialog() override;
64 namespace {
66 class SwAuthenticationSettingsDialog : public SfxDialogController
68 SwMailMergeConfigItem& m_rConfigItem;
70 std::unique_ptr<weld::CheckButton> m_xAuthenticationCB;
71 std::unique_ptr<weld::RadioButton> m_xSeparateAuthenticationRB;
72 std::unique_ptr<weld::RadioButton> m_xSMTPAfterPOPRB;
73 std::unique_ptr<weld::Label> m_xOutgoingServerFT;
74 std::unique_ptr<weld::Label> m_xUserNameFT;
75 std::unique_ptr<weld::Entry> m_xUserNameED;
76 std::unique_ptr<weld::Label> m_xOutPasswordFT;
77 std::unique_ptr<weld::Entry> m_xOutPasswordED;
78 std::unique_ptr<weld::Label> m_xIncomingServerFT;
79 std::unique_ptr<weld::Label> m_xServerFT;
80 std::unique_ptr<weld::Entry> m_xServerED;
81 std::unique_ptr<weld::Label> m_xPortFT;
82 std::unique_ptr<weld::SpinButton> m_xPortNF;
83 std::unique_ptr<weld::Label> m_xProtocolFT;
84 std::unique_ptr<weld::RadioButton> m_xPOP3RB;
85 std::unique_ptr<weld::RadioButton> m_xIMAPRB;
86 std::unique_ptr<weld::Label> m_xInUsernameFT;
87 std::unique_ptr<weld::Entry> m_xInUsernameED;
88 std::unique_ptr<weld::Label> m_xInPasswordFT;
89 std::unique_ptr<weld::Entry> m_xInPasswordED;
90 std::unique_ptr<weld::Button> m_xOKPB;
92 DECL_LINK(OKHdl_Impl, weld::Button&, void);
93 DECL_LINK(CheckBoxHdl_Impl, weld::Toggleable&, void);
94 DECL_LINK(RadioButtonHdl_Impl, weld::Toggleable&, void);
95 DECL_LINK(InServerHdl_Impl, weld::Toggleable&, void);
97 public:
98 SwAuthenticationSettingsDialog(weld::Window* pParent, SwMailMergeConfigItem& rItem);
103 SwMailConfigPage::SwMailConfigPage(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet& rSet)
104 : SfxTabPage(pPage, pController, "modules/swriter/ui/mailconfigpage.ui", "MailConfigPage", &rSet)
105 , m_pConfigItem(new SwMailMergeConfigItem)
106 , m_xDisplayNameED(m_xBuilder->weld_entry("displayname"))
107 , m_xAddressED(m_xBuilder->weld_entry("address"))
108 , m_xReplyToCB(m_xBuilder->weld_check_button("replytocb"))
109 , m_xReplyToFT(m_xBuilder->weld_label("replyto_label"))
110 , m_xReplyToED(m_xBuilder->weld_entry("replyto"))
111 , m_xServerED(m_xBuilder->weld_entry("server"))
112 , m_xPortNF(m_xBuilder->weld_spin_button("port"))
113 , m_xSecureCB(m_xBuilder->weld_check_button("secure"))
114 , m_xServerAuthenticationPB(m_xBuilder->weld_button("serverauthentication"))
115 , m_xTestPB(m_xBuilder->weld_button("test"))
117 m_xReplyToCB->connect_toggled(LINK(this, SwMailConfigPage, ReplyToHdl));
118 m_xServerAuthenticationPB->connect_clicked(LINK(this, SwMailConfigPage, AuthenticationHdl));
119 m_xTestPB->connect_clicked(LINK(this, SwMailConfigPage, TestHdl));
120 m_xSecureCB->connect_toggled(LINK(this, SwMailConfigPage, SecureHdl));
123 SwMailConfigPage::~SwMailConfigPage()
125 m_pConfigItem.reset();
128 std::unique_ptr<SfxTabPage> SwMailConfigPage::Create(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet* rAttrSet)
130 return std::make_unique<SwMailConfigPage>(pPage, pController, *rAttrSet);
133 bool SwMailConfigPage::FillItemSet( SfxItemSet* /*rSet*/ )
135 if (m_xDisplayNameED->get_value_changed_from_saved())
136 m_pConfigItem->SetMailDisplayName(m_xDisplayNameED->get_text());
137 if (m_xAddressED->get_value_changed_from_saved())
138 m_pConfigItem->SetMailAddress(m_xAddressED->get_text());
139 if (m_xReplyToCB->get_state_changed_from_saved() )
140 m_pConfigItem->SetMailReplyTo(m_xReplyToCB->get_active());
141 if (m_xReplyToED->get_value_changed_from_saved())
142 m_pConfigItem->SetMailReplyTo(m_xReplyToED->get_text());
143 if (m_xServerED->get_value_changed_from_saved())
144 m_pConfigItem->SetMailServer(m_xServerED->get_text());
146 m_pConfigItem->SetMailPort(m_xPortNF->get_value());
147 m_pConfigItem->SetSecureConnection(m_xSecureCB->get_active());
149 m_pConfigItem->Commit();
150 return true;
153 void SwMailConfigPage::Reset( const SfxItemSet* /*rSet*/ )
155 m_xDisplayNameED->set_text(m_pConfigItem->GetMailDisplayName());
156 m_xAddressED->set_text(m_pConfigItem->GetMailAddress());
158 m_xReplyToED->set_text(m_pConfigItem->GetMailReplyTo()) ;
159 m_xReplyToCB->set_active(m_pConfigItem->IsMailReplyTo());
160 ReplyToHdl(*m_xReplyToCB);
162 m_xServerED->set_text(m_pConfigItem->GetMailServer());
163 m_xPortNF->set_value(m_pConfigItem->GetMailPort());
165 m_xSecureCB->set_active(m_pConfigItem->IsSecureConnection());
167 m_xDisplayNameED->save_value();
168 m_xAddressED->save_value();
169 m_xReplyToCB->save_state();
170 m_xReplyToED->save_value();
171 m_xServerED->save_value();
172 m_xPortNF->save_value();
173 m_xSecureCB->save_state();
176 IMPL_LINK(SwMailConfigPage, ReplyToHdl, weld::Toggleable&, rBox, void)
178 bool bEnable = rBox.get_active();
179 m_xReplyToFT->set_sensitive(bEnable);
180 m_xReplyToED->set_sensitive(bEnable);
183 IMPL_LINK_NOARG(SwMailConfigPage, AuthenticationHdl, weld::Button&, void)
185 m_pConfigItem->SetMailAddress(m_xAddressED->get_text());
187 SwAuthenticationSettingsDialog aDlg(GetFrameWeld(), *m_pConfigItem);
188 aDlg.run();
191 IMPL_LINK_NOARG(SwMailConfigPage, TestHdl, weld::Button&, void)
193 SwTestAccountSettingsDialog aDlg(this);
194 aDlg.run();
197 IMPL_LINK(SwMailConfigPage, SecureHdl, weld::Toggleable&, rBox, void)
199 bool bEnable = rBox.get_active();
200 m_pConfigItem->SetSecureConnection(bEnable);
201 m_pConfigItem->SetMailPort(m_xPortNF->get_value());
202 m_xPortNF->set_value(m_pConfigItem->GetMailPort());
205 SwTestAccountSettingsDialog::SwTestAccountSettingsDialog(SwMailConfigPage* pParent)
206 : SfxDialogController(pParent->GetFrameWeld(), "modules/swriter/ui/testmailsettings.ui", "TestMailSettings")
207 , m_bStop(false)
208 , m_pParent(pParent)
209 , m_xStopPB(m_xBuilder->weld_button("stop"))
210 , m_xErrorsED(m_xBuilder->weld_text_view("errors"))
211 , m_xResult1(m_xBuilder->weld_label("result1"))
212 , m_xResult2(m_xBuilder->weld_label("result2"))
213 , m_xImage1(m_xBuilder->weld_image("image1"))
214 , m_xImage2(m_xBuilder->weld_image("image2"))
215 , m_xImage3(m_xBuilder->weld_image("image3"))
216 , m_xImage4(m_xBuilder->weld_image("image4"))
218 m_xErrorsED->set_size_request(m_xErrorsED->get_approximate_digit_width() * 72,
219 m_xErrorsED->get_height_rows(8));
220 m_sErrorServer = m_xErrorsED->get_text();
221 m_xErrorsED->set_text("");
222 m_sCompleted = m_xResult1->get_label();
223 m_sFailed = m_xResult2->get_label();
225 m_xStopPB->connect_clicked(LINK(this, SwTestAccountSettingsDialog, StopHdl));
227 m_pPostedEvent = Application::PostUserEvent(LINK(this, SwTestAccountSettingsDialog, TestHdl));
230 SwTestAccountSettingsDialog::~SwTestAccountSettingsDialog()
232 if (m_pPostedEvent)
234 Application::RemoveUserEvent(m_pPostedEvent);
238 IMPL_LINK_NOARG(SwTestAccountSettingsDialog, StopHdl, weld::Button&, void)
240 m_bStop = true;
243 IMPL_LINK_NOARG(SwTestAccountSettingsDialog, TestHdl, void*, void)
245 m_pPostedEvent = nullptr;
246 weld::WaitObject aWait(m_xDialog.get());
247 Test();
250 void SwTestAccountSettingsDialog::Test()
252 uno::Reference<uno::XComponentContext> xContext = ::comphelper::getProcessComponentContext();
254 OUString sException;
256 bool bIsLoggedIn = false;
257 bool bIsServer = false;
260 uno::Reference< mail::XMailService > xInMailService;
261 uno::Reference< mail::XMailServiceProvider > xMailServiceProvider(
262 mail::MailServiceProvider::create(xContext) );
263 uno::Reference< mail::XMailService > xMailService =
264 xMailServiceProvider->create(
265 mail::MailServiceType_SMTP);
266 if(m_bStop)
267 return;
268 uno::Reference<XConnectionListener> xConnectionListener(new SwConnectionListener);
270 if(m_pParent->m_pConfigItem->IsAuthentication() &&
271 m_pParent->m_pConfigItem->IsSMTPAfterPOP())
273 xInMailService = xMailServiceProvider->create(
274 m_pParent->m_pConfigItem->IsInServerPOP() ?
275 mail::MailServiceType_POP3 : mail::MailServiceType_IMAP);
276 if(m_bStop)
277 return;
278 //authenticate at the POP or IMAP server first
279 uno::Reference<XAuthenticator> xAuthenticator =
280 new SwAuthenticator(
281 m_pParent->m_pConfigItem->GetInServerUserName(),
282 m_pParent->m_pConfigItem->GetInServerPassword(),
283 m_xDialog.get());
285 xInMailService->addConnectionListener(xConnectionListener);
286 //check connection
287 uno::Reference< uno::XCurrentContext> xConnectionContext =
288 new SwConnectionContext(
289 m_pParent->m_pConfigItem->GetInServerName(),
290 m_pParent->m_pConfigItem->GetInServerPort(),
291 "Insecure");
292 xInMailService->connect(xConnectionContext, xAuthenticator);
294 if(m_bStop)
295 return;
296 uno::Reference<XAuthenticator> xAuthenticator;
297 if(m_pParent->m_pConfigItem->IsAuthentication() &&
298 !m_pParent->m_pConfigItem->IsSMTPAfterPOP() &&
299 !m_pParent->m_pConfigItem->GetMailUserName().isEmpty())
300 xAuthenticator =
301 new SwAuthenticator(
302 m_pParent->m_pConfigItem->GetMailUserName(),
303 m_pParent->m_pConfigItem->GetMailPassword(),
304 m_xDialog.get());
305 else
306 xAuthenticator = new SwAuthenticator();
308 xMailService->addConnectionListener(xConnectionListener);
309 if(m_bStop)
310 return;
311 //just to check if the server exists
312 xMailService->getSupportedConnectionTypes();
313 if(m_bStop)
314 return;
315 bIsServer = true;
316 //check connection
317 uno::Reference< uno::XCurrentContext> xConnectionContext =
318 new SwConnectionContext(
319 m_pParent->m_xServerED->get_text(),
320 m_pParent->m_xPortNF->get_value(),
321 m_pParent->m_xSecureCB->get_active() ? OUString("Ssl") : OUString("Insecure"));
322 xMailService->connect(xConnectionContext, xAuthenticator);
323 bIsLoggedIn = xMailService->isConnected();
324 if( xInMailService.is() )
325 xInMailService->disconnect();
326 if( xMailService->isConnected())
327 xMailService->disconnect();
329 catch (const uno::Exception& e)
331 sException = e.Message;
334 m_xResult1->set_label(bIsServer ? m_sCompleted : m_sFailed);
335 m_xImage1->set_visible(!bIsServer);
336 m_xImage3->set_visible(bIsServer);
338 m_xResult2->set_label(bIsLoggedIn ? m_sCompleted : m_sFailed);
339 m_xImage2->set_visible(!bIsLoggedIn);
340 m_xImage4->set_visible(bIsLoggedIn);
342 if (!bIsServer || !bIsLoggedIn)
344 OUString aErrorMessage(m_sErrorServer);
345 if (!sException.isEmpty())
346 aErrorMessage += "\n--\n" + sException;
347 m_xErrorsED->set_text(aErrorMessage);
351 SwMailConfigDlg::SwMailConfigDlg(weld::Window* pParent, const SfxItemSet& rSet)
352 : SfxSingleTabDialogController(pParent, &rSet)
354 // create TabPage
355 SetTabPage(SwMailConfigPage::Create(get_content_area(), this, &rSet));
356 m_xDialog->set_title(SwResId(STR_MAILCONFIG_DLG_TITLE));
359 SwAuthenticationSettingsDialog::SwAuthenticationSettingsDialog(
360 weld::Window* pParent, SwMailMergeConfigItem& rItem)
361 : SfxDialogController(pParent, "modules/swriter/ui/authenticationsettingsdialog.ui", "AuthenticationSettingsDialog")
362 , m_rConfigItem(rItem)
363 , m_xAuthenticationCB(m_xBuilder->weld_check_button("authentication"))
364 , m_xSeparateAuthenticationRB(m_xBuilder->weld_radio_button("separateauthentication"))
365 , m_xSMTPAfterPOPRB(m_xBuilder->weld_radio_button("smtpafterpop"))
366 , m_xOutgoingServerFT(m_xBuilder->weld_label("label1"))
367 , m_xUserNameFT(m_xBuilder->weld_label("username_label"))
368 , m_xUserNameED(m_xBuilder->weld_entry("username"))
369 , m_xOutPasswordFT(m_xBuilder->weld_label("outpassword_label"))
370 , m_xOutPasswordED(m_xBuilder->weld_entry("outpassword"))
371 , m_xIncomingServerFT(m_xBuilder->weld_label("label2"))
372 , m_xServerFT(m_xBuilder->weld_label("server_label"))
373 , m_xServerED(m_xBuilder->weld_entry("server"))
374 , m_xPortFT(m_xBuilder->weld_label("port_label"))
375 , m_xPortNF(m_xBuilder->weld_spin_button("port"))
376 , m_xProtocolFT(m_xBuilder->weld_label("label3"))
377 , m_xPOP3RB(m_xBuilder->weld_radio_button("pop3"))
378 , m_xIMAPRB(m_xBuilder->weld_radio_button("imap"))
379 , m_xInUsernameFT(m_xBuilder->weld_label("inusername_label"))
380 , m_xInUsernameED(m_xBuilder->weld_entry("inusername"))
381 , m_xInPasswordFT(m_xBuilder->weld_label("inpassword_label"))
382 , m_xInPasswordED(m_xBuilder->weld_entry("inpassword"))
383 , m_xOKPB(m_xBuilder->weld_button("ok"))
385 m_xAuthenticationCB->connect_toggled( LINK( this, SwAuthenticationSettingsDialog, CheckBoxHdl_Impl));
386 Link<weld::Toggleable&,void> aRBLink = LINK( this, SwAuthenticationSettingsDialog, RadioButtonHdl_Impl );
387 m_xSeparateAuthenticationRB->connect_toggled( aRBLink );
388 m_xSMTPAfterPOPRB->connect_toggled( aRBLink );
389 m_xOKPB->connect_clicked( LINK( this, SwAuthenticationSettingsDialog, OKHdl_Impl));
390 m_xPOP3RB->connect_toggled(LINK(this, SwAuthenticationSettingsDialog, InServerHdl_Impl));
392 m_xAuthenticationCB->set_active(m_rConfigItem.IsAuthentication());
393 if (m_rConfigItem.IsSMTPAfterPOP())
394 m_xSMTPAfterPOPRB->set_active(true);
395 else
396 m_xSeparateAuthenticationRB->set_active(true);
397 m_xUserNameED->set_text(m_rConfigItem.GetMailUserName());
398 m_xOutPasswordED->set_text(m_rConfigItem.GetMailPassword());
400 m_xServerED->set_text(m_rConfigItem.GetInServerName());
401 m_xPortNF->set_value(m_rConfigItem.GetInServerPort());
402 if (m_rConfigItem.IsInServerPOP())
403 m_xPOP3RB->set_active(true);
404 else
405 m_xIMAPRB->set_active(true);
406 m_xInUsernameED->set_text(m_rConfigItem.GetInServerUserName());
407 m_xInPasswordED->set_text(m_rConfigItem.GetInServerPassword());
409 CheckBoxHdl_Impl(*m_xAuthenticationCB);
412 IMPL_LINK_NOARG(SwAuthenticationSettingsDialog, OKHdl_Impl, weld::Button&, void)
414 m_rConfigItem.SetAuthentication( m_xAuthenticationCB->get_active() );
415 m_rConfigItem.SetSMTPAfterPOP(m_xSMTPAfterPOPRB->get_active());
416 m_rConfigItem.SetMailUserName(m_xUserNameED->get_text());
417 m_rConfigItem.SetMailPassword(m_xOutPasswordED->get_text());
418 m_rConfigItem.SetInServerName(m_xServerED->get_text());
419 m_rConfigItem.SetInServerPort(m_xPortNF->get_value());
420 m_rConfigItem.SetInServerPOP(m_xPOP3RB->get_active());
421 m_rConfigItem.SetInServerUserName(m_xInUsernameED->get_text());
422 m_rConfigItem.SetInServerPassword(m_xInPasswordED->get_text());
423 m_xDialog->response(RET_OK);
426 IMPL_LINK( SwAuthenticationSettingsDialog, CheckBoxHdl_Impl, weld::Toggleable&, rBox, void)
428 bool bChecked = rBox.get_active();
429 m_xSeparateAuthenticationRB->set_sensitive(bChecked);
430 m_xSMTPAfterPOPRB->set_sensitive(bChecked);
431 RadioButtonHdl_Impl(*m_xSeparateAuthenticationRB);
434 IMPL_LINK_NOARG(SwAuthenticationSettingsDialog, RadioButtonHdl_Impl, weld::Toggleable&, void)
436 bool bSeparate = m_xSeparateAuthenticationRB->get_active();
437 bool bIsEnabled = m_xSeparateAuthenticationRB->get_sensitive();
438 bool bNotSeparate = !bSeparate && bIsEnabled;
439 bSeparate &= bIsEnabled;
441 if (bSeparate && m_xUserNameED->get_text().isEmpty())
442 m_xUserNameED->set_text(m_rConfigItem.GetMailAddress());
443 else if (!bSeparate && m_xUserNameED->get_text() == m_rConfigItem.GetMailAddress())
444 m_xUserNameED->set_text("");
446 if (bNotSeparate && m_xInUsernameED->get_text().isEmpty())
447 m_xInUsernameED->set_text(m_rConfigItem.GetMailAddress());
448 else if (!bNotSeparate && m_xInUsernameED->get_text() == m_rConfigItem.GetMailAddress())
449 m_xInUsernameED->set_text("");
451 m_xOutgoingServerFT->set_sensitive(bSeparate);
452 m_xUserNameFT->set_sensitive(bSeparate);
453 m_xUserNameED->set_sensitive(bSeparate);
454 m_xOutPasswordFT->set_sensitive(bSeparate);
455 m_xOutPasswordED->set_sensitive(bSeparate);
457 m_xIncomingServerFT->set_sensitive(bNotSeparate);
458 m_xServerFT->set_sensitive(bNotSeparate);
459 m_xServerED->set_sensitive(bNotSeparate);
460 m_xPortFT->set_sensitive(bNotSeparate);
461 m_xPortNF->set_sensitive(bNotSeparate);
462 m_xInUsernameFT->set_sensitive(bNotSeparate);
463 m_xInUsernameED->set_sensitive(bNotSeparate);
464 m_xProtocolFT->set_sensitive(bNotSeparate);
465 m_xPOP3RB->set_sensitive(bNotSeparate);
466 m_xIMAPRB->set_sensitive(bNotSeparate);
467 m_xInPasswordFT->set_sensitive(bNotSeparate);
468 m_xInPasswordED->set_sensitive(bNotSeparate);
471 IMPL_LINK_NOARG( SwAuthenticationSettingsDialog, InServerHdl_Impl, weld::Toggleable&, void)
473 bool bPOP = m_xPOP3RB->get_active();
474 m_rConfigItem.SetInServerPOP(bPOP);
475 m_xPortNF->set_value(m_rConfigItem.GetInServerPort());
478 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */