File indexing completed on 2024-10-06 04:35:38

0001 /*
0002  *  Dialog to choose a user for a 'homes' share
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 "smb4khomesuserdialog.h"
0010 #include "core/smb4khomesshareshandler.h"
0011 #include "core/smb4ksettings.h"
0012 
0013 // Qt includes
0014 #include <QDialogButtonBox>
0015 #include <QGridLayout>
0016 #include <QIcon>
0017 #include <QLabel>
0018 #include <QVBoxLayout>
0019 #include <QWindow>
0020 
0021 // KDE includes
0022 #include <KConfigGroup>
0023 #include <KIconLoader>
0024 #include <KLineEdit>
0025 #include <KLocalizedString>
0026 #include <KWindowConfig>
0027 
0028 Smb4KHomesUserDialog::Smb4KHomesUserDialog(QWidget *parent)
0029     : QDialog(parent)
0030 {
0031     setWindowTitle(i18n("Specify User"));
0032     setAttribute(Qt::WA_DeleteOnClose);
0033 
0034     QVBoxLayout *layout = new QVBoxLayout(this);
0035 
0036     QWidget *descriptionWidget = new QWidget(this);
0037     QHBoxLayout *descriptionWidgetLayout = new QHBoxLayout(descriptionWidget);
0038 
0039     QLabel *descriptionPixmap = new QLabel(descriptionWidget);
0040     descriptionPixmap->setPixmap(KDE::icon(QStringLiteral("user")).pixmap(KIconLoader::SizeHuge));
0041     descriptionPixmap->setAlignment(Qt::AlignBottom);
0042     descriptionPixmap->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
0043 
0044     descriptionWidgetLayout->addWidget(descriptionPixmap);
0045 
0046     m_descriptionText = new QLabel(this);
0047     m_descriptionText->setText(i18n("Please specify a username."));
0048     m_descriptionText->setWordWrap(true);
0049     m_descriptionText->setAlignment(Qt::AlignBottom);
0050     m_descriptionText->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
0051 
0052     descriptionWidgetLayout->addWidget(m_descriptionText);
0053 
0054     layout->addWidget(descriptionWidget);
0055     layout->addSpacing(layout->spacing());
0056 
0057     QWidget *inputWidget = new QWidget(this);
0058     QGridLayout *inputWidgetLayout = new QGridLayout(inputWidget);
0059     inputWidgetLayout->setContentsMargins(0, 0, 0, 0);
0060     inputWidgetLayout->setColumnStretch(0, 0);
0061     inputWidgetLayout->setColumnStretch(1, 1);
0062 
0063     QLabel *userNameLabel = new QLabel(i18n("Username:"), inputWidget);
0064 
0065     m_userNameInput = new KComboBox(inputWidget);
0066     m_userNameInput->setEditable(true);
0067 
0068     connect(m_userNameInput->lineEdit(), &KLineEdit::editingFinished, this, &Smb4KHomesUserDialog::slotHomesUserNameEntered);
0069     connect(m_userNameInput, &KComboBox::currentTextChanged, this, &Smb4KHomesUserDialog::slotHomesUserNameChanged);
0070 
0071     inputWidgetLayout->addWidget(userNameLabel, 0, 0);
0072     inputWidgetLayout->addWidget(m_userNameInput, 0, 1);
0073 
0074     layout->addWidget(inputWidget);
0075 
0076     QDialogButtonBox *buttonBox = new QDialogButtonBox(this);
0077     m_okButton = buttonBox->addButton(QDialogButtonBox::Ok);
0078     m_okButton->setShortcut(QKeySequence::Save);
0079     m_okButton->setEnabled(false);
0080     m_cancelButton = buttonBox->addButton(QDialogButtonBox::Cancel);
0081     m_cancelButton->setShortcut(QKeySequence::Cancel);
0082     m_cancelButton->setDefault(true);
0083 
0084     connect(m_okButton, &QPushButton::clicked, this, &Smb4KHomesUserDialog::slotOkClicked);
0085     connect(m_cancelButton, &QPushButton::clicked, this, &Smb4KHomesUserDialog::reject);
0086 
0087     layout->addWidget(buttonBox);
0088 
0089     create();
0090 
0091     KConfigGroup dialogGroup(Smb4KSettings::self()->config(), QStringLiteral("HomesUserDialog"));
0092     QSize dialogSize;
0093 
0094     if (dialogGroup.exists()) {
0095         KWindowConfig::restoreWindowSize(windowHandle(), dialogGroup);
0096         dialogSize = windowHandle()->size();
0097     } else {
0098         dialogSize = sizeHint();
0099     }
0100 
0101     resize(dialogSize); // workaround for QTBUG-40584
0102 
0103     KConfigGroup completionGroup(Smb4KSettings::self()->config(), QStringLiteral("CompletionItems"));
0104 
0105     if (completionGroup.exists()) {
0106         m_userNameInput->completionObject()->setItems(completionGroup.readEntry("HomesUsersCompletion", QStringList()));
0107     }
0108 }
0109 
0110 Smb4KHomesUserDialog::~Smb4KHomesUserDialog() noexcept
0111 {
0112 }
0113 
0114 bool Smb4KHomesUserDialog::setShare(SharePtr homesShare)
0115 {
0116     if (!homesShare->isHomesShare()) {
0117         return false;
0118     }
0119 
0120     m_share = homesShare;
0121     m_descriptionText->setText(i18n("Please specify a username for share<br><b>%1</b>.", m_share->displayString()));
0122     m_userNameInput->addItems(Smb4KHomesSharesHandler::self()->homesUsers(m_share));
0123     m_userNameInput->setCurrentItem(QStringLiteral(""));
0124 
0125     return true;
0126 }
0127 
0128 void Smb4KHomesUserDialog::slotHomesUserNameEntered()
0129 {
0130     if (!m_userNameInput->currentText().isEmpty()) {
0131         m_userNameInput->completionObject()->addItem(m_userNameInput->currentText());
0132     }
0133 }
0134 
0135 void Smb4KHomesUserDialog::slotHomesUserNameChanged(const QString &text)
0136 {
0137     m_okButton->setEnabled(!text.isEmpty());
0138 }
0139 
0140 void Smb4KHomesUserDialog::slotOkClicked()
0141 {
0142     QString userName = m_userNameInput->currentText();
0143 
0144     if (!userName.isEmpty()) {
0145         if (!m_share->userName().isEmpty() && m_share->userName() != userName) {
0146             m_share->setPassword(QString());
0147         }
0148 
0149         m_share->setUserName(userName);
0150     }
0151 
0152     QStringList homesUsers;
0153 
0154     for (int i = 0; i < m_userNameInput->count(); ++i) {
0155         homesUsers << m_userNameInput->itemText(i);
0156     }
0157 
0158     if (!homesUsers.contains(m_userNameInput->currentText())) {
0159         homesUsers << m_userNameInput->currentText();
0160     }
0161 
0162     Smb4KHomesSharesHandler::self()->addHomesUsers(m_share, homesUsers);
0163 
0164     KConfigGroup dialogGroup(Smb4KSettings::self()->config(), QStringLiteral("HomesUserDialog"));
0165     KWindowConfig::saveWindowSize(windowHandle(), dialogGroup);
0166 
0167     KConfigGroup completionGroup(Smb4KSettings::self()->config(), QStringLiteral("CompletionItems"));
0168     completionGroup.writeEntry("HomesUsersCompletion", m_userNameInput->completionObject()->items());
0169 
0170     accept();
0171 }