File indexing completed on 2024-05-05 16:13:56

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 2000 Alexander Neundorf <neundorf@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 // Own
0009 #include "smbrodlg.h"
0010 
0011 // Qt
0012 #include <QGridLayout>
0013 #include <QLabel>
0014 
0015 // KDE
0016 #include <KConfig>
0017 #include <KConfigGroup>
0018 #include <KLocalizedString>
0019 #include <KPluginFactory>
0020 
0021 K_PLUGIN_CLASS_WITH_JSON(SMBRoOptions, "smb.json")
0022 
0023 SMBRoOptions::SMBRoOptions(QWidget *parent, const QVariantList &args)
0024     : KCModule(parent, args)
0025 {
0026     QGridLayout *layout = new QGridLayout(this);
0027     QLabel *label = new QLabel(i18n("These settings apply to network browsing only."), this);
0028     layout->addWidget(label, 0, 0, 1, 2);
0029 
0030     m_userLe = new QLineEdit(this);
0031     label = new QLabel(i18n("Default user name:"), this);
0032     label->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
0033     label->setBuddy(m_userLe);
0034     layout->addWidget(label, 1, 0);
0035     layout->addWidget(m_userLe, 1, 1);
0036 
0037     m_passwordLe = new QLineEdit(this);
0038     m_passwordLe->setEchoMode(QLineEdit::Password);
0039     label = new QLabel(i18n("Default password:"), this);
0040     label->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
0041     label->setBuddy(m_passwordLe);
0042     layout->addWidget(label, 2, 0);
0043     layout->addWidget(m_passwordLe, 2, 1);
0044 
0045     layout->addWidget(new QWidget(this), 4, 0);
0046 
0047     connect(m_userLe, &QLineEdit::textChanged, this, &SMBRoOptions::changed);
0048     connect(m_passwordLe, &QLineEdit::textChanged, this, &SMBRoOptions::changed);
0049 
0050     layout->setRowStretch(4, 1);
0051 }
0052 
0053 SMBRoOptions::~SMBRoOptions() = default;
0054 
0055 void SMBRoOptions::load()
0056 {
0057     KConfig *cfg = new KConfig(QStringLiteral("kioslaverc"));
0058 
0059     KConfigGroup group = cfg->group("Browser Settings/SMBro");
0060     m_userLe->setText(group.readEntry("User"));
0061 
0062     // unscramble
0063     QString scrambled = group.readEntry("Password");
0064     QString password;
0065     const int passwordLength = scrambled.length() / 3;
0066     password.reserve(passwordLength);
0067     for (int i = 0; i < passwordLength; ++i) {
0068         QChar qc1 = scrambled[i * 3];
0069         QChar qc2 = scrambled[i * 3 + 1];
0070         QChar qc3 = scrambled[i * 3 + 2];
0071         unsigned int a1 = qc1.toLatin1() - '0';
0072         unsigned int a2 = qc2.toLatin1() - 'A';
0073         unsigned int a3 = qc3.toLatin1() - '0';
0074         unsigned int num = ((a1 & 0x3F) << 10) | ((a2 & 0x1F) << 5) | (a3 & 0x1F);
0075         password[i] = QLatin1Char((num - 17) ^ 173); // restore
0076     }
0077     m_passwordLe->setText(password);
0078 
0079     delete cfg;
0080 }
0081 
0082 void SMBRoOptions::save()
0083 {
0084     KConfig *cfg = new KConfig(QStringLiteral("kioslaverc"));
0085 
0086     KConfigGroup group = cfg->group("Browser Settings/SMBro");
0087     group.writeEntry("User", m_userLe->text());
0088 
0089     // taken from Nicola Brodu's smb ioslave
0090     // it's not really secure, but at
0091     // least better than storing the plain password
0092     QString password(m_passwordLe->text());
0093     QString scrambled;
0094     for (const QChar c : std::as_const(password)) {
0095         unsigned int num = (c.unicode() ^ 173) + 17;
0096         unsigned int a1 = (num & 0xFC00) >> 10;
0097         unsigned int a2 = (num & 0x3E0) >> 5;
0098         unsigned int a3 = (num & 0x1F);
0099         scrambled += QLatin1Char((char)(a1 + '0'));
0100         scrambled += QLatin1Char((char)(a2 + 'A'));
0101         scrambled += QLatin1Char((char)(a3 + '0'));
0102     }
0103     group.writeEntry("Password", scrambled);
0104 
0105     delete cfg;
0106 }
0107 
0108 void SMBRoOptions::defaults()
0109 {
0110     m_userLe->setText(QString());
0111     m_passwordLe->setText(QString());
0112 }
0113 
0114 void SMBRoOptions::changed()
0115 {
0116     Q_EMIT KCModule::changed(true);
0117 }
0118 
0119 QString SMBRoOptions::quickHelp() const
0120 {
0121     return i18n(
0122         "<h1>Windows Shares</h1><p>Applications using the "
0123         "SMB KIO worker (like Konqueror) are able to access shared Microsoft "
0124         "Windows file systems, if properly configured.</p><p>You can specify "
0125         "here the credentials used to access the shared resources. "
0126         "Passwords will be stored locally, and scrambled so as to render them "
0127         "unreadable to the human eye. For security reasons, you may not want to "
0128         "do that, as entries with passwords are clearly indicated as such.</p>");
0129 }
0130 
0131 #include "moc_smbrodlg.cpp"
0132 #include "smbrodlg.moc"