Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / uui / source / passworddlg.cxx
blob436b5a7a8743bcd8a2056365d69f1159f0aefcce
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 "passworddlg.hxx"
21 #include <strings.hrc>
23 #include <unotools/resmgr.hxx>
24 #include <tools/urlobj.hxx>
25 #include <vcl/svapp.hxx>
26 #include <vcl/weld.hxx>
28 using namespace ::com::sun::star;
30 PasswordDialog::PasswordDialog(weld::Window* pParent,
31 task::PasswordRequestMode nDialogMode, const std::locale& rLocale,
32 const OUString& aDocURL, bool bOpenToModify, bool bIsSimplePasswordRequest)
33 : GenericDialogController(pParent, "uui/ui/password.ui", "PasswordDialog")
34 , m_xFTPassword(m_xBuilder->weld_label("newpassFT"))
35 , m_xEDPassword(m_xBuilder->weld_entry("newpassEntry"))
36 , m_xFTConfirmPassword(m_xBuilder->weld_label("confirmpassFT"))
37 , m_xEDConfirmPassword(m_xBuilder->weld_entry("confirmpassEntry"))
38 , m_xOKBtn(m_xBuilder->weld_button("ok"))
39 , nMinLen(1)
40 , aPasswdMismatch(Translate::get(STR_PASSWORD_MISMATCH, rLocale))
41 , rResLocale(rLocale)
43 if( nDialogMode == task::PasswordRequestMode_PASSWORD_REENTER )
45 const char* pOpenToModifyErrStrId = bOpenToModify ? STR_ERROR_PASSWORD_TO_MODIFY_WRONG : STR_ERROR_PASSWORD_TO_OPEN_WRONG;
46 const char* pErrStrId = bIsSimplePasswordRequest ? STR_ERROR_SIMPLE_PASSWORD_WRONG : pOpenToModifyErrStrId;
47 OUString aErrorMsg(Translate::get(pErrStrId, rResLocale));
48 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(pParent,
49 VclMessageType::Warning, VclButtonsType::Ok, aErrorMsg));
50 xBox->run();
53 // default settings for enter password or reenter passwd...
54 OUString aTitle(Translate::get(STR_TITLE_ENTER_PASSWORD, rResLocale));
55 m_xFTConfirmPassword->hide();
56 m_xEDConfirmPassword->hide();
57 m_xFTConfirmPassword->set_sensitive(false);
58 m_xEDConfirmPassword->set_sensitive(false);
60 // settings for create password
61 if (nDialogMode == task::PasswordRequestMode_PASSWORD_CREATE)
63 aTitle = Translate::get(STR_TITLE_CREATE_PASSWORD, rResLocale);
65 m_xFTConfirmPassword->set_label(Translate::get(STR_CONFIRM_SIMPLE_PASSWORD, rResLocale));
67 m_xFTConfirmPassword->show();
68 m_xEDConfirmPassword->show();
69 m_xFTConfirmPassword->set_sensitive(true);
70 m_xEDConfirmPassword->set_sensitive(true);
73 m_xDialog->set_title(aTitle);
75 const char* pStrId = bOpenToModify ? STR_ENTER_PASSWORD_TO_MODIFY : STR_ENTER_PASSWORD_TO_OPEN;
76 OUString aMessage(Translate::get(pStrId, rResLocale));
77 INetURLObject url(aDocURL);
78 aMessage += url.HasError()
79 ? aDocURL : url.GetMainURL(INetURLObject::DecodeMechanism::Unambiguous);
80 m_xFTPassword->set_label(aMessage);
82 if (bIsSimplePasswordRequest)
84 DBG_ASSERT( aDocURL.isEmpty(), "A simple password request should not have a document URL! Use document password request instead." );
85 m_xFTPassword->set_label(Translate::get(STR_ENTER_SIMPLE_PASSWORD, rResLocale));
88 m_xOKBtn->connect_clicked(LINK(this, PasswordDialog, OKHdl_Impl));
91 IMPL_LINK_NOARG(PasswordDialog, OKHdl_Impl, weld::Button&, void)
93 bool bEDPasswdValid = m_xEDPassword->get_text().getLength() >= nMinLen;
94 bool bPasswdMismatch = m_xEDConfirmPassword->get_text() != m_xEDPassword->get_text();
95 bool bValid = (!m_xEDConfirmPassword->get_visible() && bEDPasswdValid) ||
96 (m_xEDConfirmPassword->get_visible() && bEDPasswdValid && !bPasswdMismatch);
98 if (m_xEDConfirmPassword->get_visible() && bPasswdMismatch)
100 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(),
101 VclMessageType::Warning, VclButtonsType::Ok,
102 aPasswdMismatch));
103 xBox->run();
105 else if (bValid)
106 m_xDialog->response(RET_OK);
109 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */