File indexing completed on 2024-11-10 07:35:54
0001 /* 0002 * Password dialog 0003 * 0004 * SPDX-FileCopyrightText: 2023 Alexander Reinholdt <alexander.reinholdt@kdemail.net> 0005 * SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 // application specific includes 0009 #include "smb4kpassworddialog.h" 0010 #include "core/smb4khomesshareshandler.h" 0011 #include "core/smb4kwalletmanager.h" 0012 0013 // KDE includes 0014 #include <KLocalizedString> 0015 0016 Smb4KPasswordDialog::Smb4KPasswordDialog(QWidget *parent) 0017 : KPasswordDialog(parent, KPasswordDialog::ShowUsernameLine) 0018 { 0019 } 0020 0021 Smb4KPasswordDialog::~Smb4KPasswordDialog() noexcept 0022 { 0023 } 0024 0025 bool Smb4KPasswordDialog::setNetworkItem(const NetworkItemPtr &networkItem) 0026 { 0027 bool success = false; 0028 0029 if (networkItem) { 0030 m_networkItem = networkItem; 0031 0032 switch (m_networkItem->type()) { 0033 case Host: { 0034 HostPtr host = m_networkItem.staticCast<Smb4KHost>(); 0035 0036 if (host) { 0037 Smb4KWalletManager::self()->readLoginCredentials(host); 0038 0039 setPrompt(i18n("Please enter a username and a password for the host <b>%1</b>.", host->hostName())); 0040 setUsername(host->userName()); 0041 setPassword(host->password()); 0042 0043 success = true; 0044 } 0045 0046 break; 0047 } 0048 case Share: { 0049 SharePtr share = m_networkItem.staticCast<Smb4KShare>(); 0050 0051 if (share) { 0052 setPrompt(i18n("Please enter a username and a password for the share <b>%1</b>.", share->displayString())); 0053 0054 if (share->isHomesShare()) { 0055 QStringList homesUsers = Smb4KHomesSharesHandler::self()->homesUsers(share); 0056 QMap<QString, QString> knownLogins; 0057 0058 for (const QString &user : homesUsers) { 0059 SharePtr tempShare = SharePtr(new Smb4KShare(*share.data())); 0060 tempShare->setUserName(user); 0061 0062 Smb4KWalletManager::self()->readLoginCredentials(tempShare); 0063 0064 knownLogins.insert(tempShare->userName(), tempShare->password()); 0065 0066 tempShare.clear(); 0067 } 0068 0069 setKnownLogins(knownLogins); 0070 } else { 0071 Smb4KWalletManager::self()->readLoginCredentials(share); 0072 setUsername(share->userName()); 0073 setPassword(share->password()); 0074 } 0075 0076 success = true; 0077 } 0078 0079 break; 0080 } 0081 default: { 0082 break; 0083 } 0084 } 0085 } 0086 0087 return success; 0088 } 0089 0090 void Smb4KPasswordDialog::accept() 0091 { 0092 QUrl url = m_networkItem->url(); 0093 url.setUserName(username()); 0094 url.setPassword(password()); 0095 0096 m_networkItem->setUrl(url); 0097 Smb4KWalletManager::self()->writeLoginCredentials(m_networkItem); 0098 0099 KPasswordDialog::accept(); 0100 }