Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / uui / source / passworddlg.cxx
blobfb4c95e623f6639ad9618a9bee20eb024e82f919
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 <unotools/configmgr.hxx>
25 #include <tools/urlobj.hxx>
26 #include <tools/debug.hxx>
27 #include <vcl/svapp.hxx>
28 #include <vcl/weld.hxx>
30 using namespace ::com::sun::star;
32 PasswordDialog::PasswordDialog(weld::Window* pParent,
33 task::PasswordRequestMode nDialogMode, const std::locale& rResLocale,
34 const OUString& aDocURL, bool bOpenToModify, bool bIsSimplePasswordRequest)
35 : GenericDialogController(pParent, "uui/ui/password.ui", "PasswordDialog")
36 , m_xFTPassword(m_xBuilder->weld_label("newpassFT"))
37 , m_xEDPassword(m_xBuilder->weld_entry("newpassEntry"))
38 , m_xFTConfirmPassword(m_xBuilder->weld_label("confirmpassFT"))
39 , m_xEDConfirmPassword(m_xBuilder->weld_entry("confirmpassEntry"))
40 , m_xOKBtn(m_xBuilder->weld_button("ok"))
41 , nMinLen(1)
42 , aPasswdMismatch(Translate::get(STR_PASSWORD_MISMATCH, rResLocale))
44 // tdf#115964 we can be launched before the parent has resized to its final size
45 m_xDialog->set_centered_on_parent(true);
47 if( nDialogMode == task::PasswordRequestMode_PASSWORD_REENTER )
49 TranslateId pOpenToModifyErrStrId = bOpenToModify ? STR_ERROR_PASSWORD_TO_MODIFY_WRONG : STR_ERROR_PASSWORD_TO_OPEN_WRONG;
50 TranslateId pErrStrId = bIsSimplePasswordRequest ? STR_ERROR_SIMPLE_PASSWORD_WRONG : pOpenToModifyErrStrId;
51 OUString aErrorMsg(Translate::get(pErrStrId, rResLocale));
52 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(pParent,
53 VclMessageType::Warning, VclButtonsType::Ok, aErrorMsg));
54 xBox->run();
57 // default settings for enter password or reenter passwd...
58 OUString aTitle(Translate::get(STR_TITLE_ENTER_PASSWORD, rResLocale));
59 m_xFTConfirmPassword->hide();
60 m_xEDConfirmPassword->hide();
61 m_xFTConfirmPassword->set_sensitive(false);
62 m_xEDConfirmPassword->set_sensitive(false);
64 // settings for create password
65 if (nDialogMode == task::PasswordRequestMode_PASSWORD_CREATE)
67 aTitle = Translate::get(STR_TITLE_CREATE_PASSWORD, rResLocale);
69 m_xFTConfirmPassword->set_label(Translate::get(STR_CONFIRM_SIMPLE_PASSWORD, rResLocale));
71 m_xFTConfirmPassword->show();
72 m_xEDConfirmPassword->show();
73 m_xFTConfirmPassword->set_sensitive(true);
74 m_xEDConfirmPassword->set_sensitive(true);
77 m_xDialog->set_title(aTitle);
79 TranslateId pStrId = bOpenToModify ? STR_ENTER_PASSWORD_TO_MODIFY : STR_ENTER_PASSWORD_TO_OPEN;
80 OUString aMessage(Translate::get(pStrId, rResLocale));
81 INetURLObject url(aDocURL);
83 // tdf#66553 - add file name to title bar for password managers
84 OUString aFileName = url.getName(INetURLObject::LAST_SEGMENT, true,
85 INetURLObject::DecodeMechanism::Unambiguous);
86 if (!aFileName.isEmpty())
87 aFileName += " - " + utl::ConfigManager::getProductName();
88 m_xDialog->set_title(aTitle + " - " + aFileName);
90 aMessage += url.HasError()
91 ? aDocURL : url.GetMainURL(INetURLObject::DecodeMechanism::Unambiguous);
92 m_xFTPassword->set_label(aMessage);
94 if (bIsSimplePasswordRequest)
96 DBG_ASSERT( aDocURL.isEmpty(), "A simple password request should not have a document URL! Use document password request instead." );
97 m_xFTPassword->set_label(Translate::get(STR_ENTER_SIMPLE_PASSWORD, rResLocale));
100 m_xOKBtn->connect_clicked(LINK(this, PasswordDialog, OKHdl_Impl));
103 IMPL_LINK_NOARG(PasswordDialog, OKHdl_Impl, weld::Button&, void)
105 bool bEDPasswdValid = m_xEDPassword->get_text().getLength() >= nMinLen;
106 bool bPasswdMismatch = m_xEDConfirmPassword->get_text() != m_xEDPassword->get_text();
107 bool bValid = (!m_xEDConfirmPassword->get_visible() && bEDPasswdValid) ||
108 (m_xEDConfirmPassword->get_visible() && bEDPasswdValid && !bPasswdMismatch);
110 if (m_xEDConfirmPassword->get_visible() && bPasswdMismatch)
112 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(),
113 VclMessageType::Warning, VclButtonsType::Ok,
114 aPasswdMismatch));
115 xBox->run();
117 else if (bValid)
118 m_xDialog->response(RET_OK);
121 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */