File indexing completed on 2021-12-21 13:27:59

0001 /**
0002  * Copyright (C) 2012 Martin Sandsmark <martin.sandsmark@kde.org>
0003  * Copyright (C) 2014 Arnold Dumas <contact@arnolddumas.fr>
0004  *
0005  * This program is free software; you can redistribute it and/or modify it under
0006  * the terms of the GNU General Public License as published by the Free Software
0007  * Foundation; either version 2 of the License, or (at your option) any later
0008  * version.
0009  *
0010  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0012  * PARTICULAR PURPOSE. See the GNU General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU General Public License along with
0015  * this program.  If not, see <http://www.gnu.org/licenses/>.
0016  */
0017 
0018 #include "scrobbleconfigdlg.h"
0019 #include "scrobbler.h"
0020 #include "juk_debug.h"
0021 
0022 #include <KLineEdit>
0023 #include <KPasswordLineEdit>
0024 #include <KLocalizedString>
0025 #include <KMessageBox>
0026 #include <KConfigGroup>
0027 #include <KSharedConfig>
0028 #include <KWallet>
0029 
0030 #include <QPushButton>
0031 #include <QLayout>
0032 #include <QLabel>
0033 #include <QFormLayout>
0034 
0035 ScrobbleConfigDlg::ScrobbleConfigDlg(QWidget* parent)
0036   : QDialog(parent)
0037   , m_wallet(Scrobbler::openKWallet())
0038 {
0039     setWindowTitle(i18n("Configure scrobbling..."));
0040 
0041     m_passwordEdit = new KPasswordLineEdit(this);
0042     m_usernameEdit = new KLineEdit(this);
0043     m_testButton = new QPushButton(i18n("Test login..."), this);
0044     m_testFeedbackLabel = new QLabel("");
0045 
0046     auto vboxLayout = new QVBoxLayout(this);
0047 
0048     QWidget *mainWidget = new QWidget();
0049     QFormLayout *layout = new QFormLayout(mainWidget);
0050     QLabel *infoLabel = new QLabel(i18n("Please enter your <a href=\"https://last.fm/\">last.fm</a> login credentials:"));
0051     infoLabel->setOpenExternalLinks(true);
0052     infoLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
0053     layout->addRow(infoLabel);
0054     layout->addRow(new QLabel(i18n("Username:")), m_usernameEdit);
0055     layout->addRow(new QLabel(i18n("Password:")), m_passwordEdit);
0056     layout->addRow(m_testButton);
0057     layout->addRow(m_testFeedbackLabel);
0058 
0059     auto dlgButtonBox = new QDialogButtonBox(QDialogButtonBox::Save | QDialogButtonBox::Cancel);
0060     vboxLayout->addWidget(mainWidget);
0061     vboxLayout->addStretch();
0062     vboxLayout->addWidget(dlgButtonBox);
0063 
0064     connect(m_passwordEdit, &KPasswordLineEdit::passwordChanged, this, &ScrobbleConfigDlg::valuesChanged);
0065     connect(m_usernameEdit, SIGNAL(textEdited(QString)), this, SLOT(valuesChanged()));
0066     connect(m_testButton, SIGNAL(clicked(bool)), this, SLOT(testLogin()));
0067 
0068     connect(dlgButtonBox, &QDialogButtonBox::accepted, this, &ScrobbleConfigDlg::save);
0069     connect(dlgButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0070 
0071     m_saveButton = dlgButtonBox->button(QDialogButtonBox::Save);
0072 
0073     // Loading credentials using either KWallet or KConfigGroup.
0074     if (m_wallet) {
0075         QMap<QString, QString> scrobblingCredentials;
0076         m_wallet->readMap("Scrobbling", scrobblingCredentials);
0077 
0078         if (scrobblingCredentials.contains("Username") && scrobblingCredentials.contains("Password")) {
0079             m_usernameEdit->setText(scrobblingCredentials.value("Username"));
0080             m_passwordEdit->setPassword(scrobblingCredentials.value("Password"));
0081         }
0082     } else {
0083         // Warning message, KWallet is safer than KConfig.
0084         KMessageBox::information(this, i18n("KWallet is unavailable, your Last.fm credentials will be stored without encryption."), i18n("KWallet is unavailable"));
0085 
0086         KConfigGroup config(KSharedConfig::openConfig(), "Scrobbling");
0087         m_usernameEdit->setText(config.readEntry("Username", ""));
0088         m_passwordEdit->setPassword(config.readEntry("Password", ""));
0089     }
0090 
0091     if (m_passwordEdit->password().isEmpty() || m_usernameEdit->text().isEmpty()) {
0092         m_saveButton->setEnabled(false);
0093         m_testButton->setEnabled(false);
0094     }
0095 }
0096 
0097 void ScrobbleConfigDlg::valuesChanged()
0098 {
0099     m_testButton->setEnabled(
0100             !m_usernameEdit->text().isEmpty() &&
0101             !m_passwordEdit->password().isEmpty());
0102     m_saveButton->setEnabled(false);
0103 }
0104 
0105 void ScrobbleConfigDlg::save()
0106 {
0107     QDialog::accept();
0108 
0109     if (m_wallet) {
0110         QMap<QString, QString> scrobblingCredentials;
0111         scrobblingCredentials.insert("Username", m_usernameEdit->text());
0112         scrobblingCredentials.insert("Password", m_passwordEdit->password());
0113 
0114         if (!m_wallet->writeMap("Scrobbling", scrobblingCredentials)) {
0115             qCCritical(JUK_LOG) << "Couldn't save Last.fm credentials using KWallet.";
0116         }
0117 
0118     } else {
0119         KConfigGroup config(KSharedConfig::openConfig(), "Scrobbling");
0120         config.writeEntry("Username", m_usernameEdit->text());
0121         config.writeEntry("Password", m_passwordEdit->password());
0122     }
0123 }
0124 
0125 void ScrobbleConfigDlg::testLogin()
0126 {
0127     m_testFeedbackLabel->setText(i18n("Validating login..."));
0128     Scrobbler *scrobbler = new Scrobbler(this);
0129     connect(scrobbler, SIGNAL(validAuth()), this, SLOT(validLogin()));
0130     connect(scrobbler, SIGNAL(invalidAuth()), this, SLOT(invalidLogin()));
0131     setEnabled(false);
0132     scrobbler->getAuthToken(m_usernameEdit->text(), m_passwordEdit->password());
0133 }
0134 
0135 void ScrobbleConfigDlg::invalidLogin()
0136 {
0137     m_testFeedbackLabel->setText(i18n("Login invalid."));
0138     setEnabled(true);
0139     sender()->deleteLater();
0140     m_saveButton->setEnabled(false);
0141 }
0142 
0143 void ScrobbleConfigDlg::validLogin()
0144 {
0145     m_testFeedbackLabel->setText(i18n("Login valid."));
0146     setEnabled(true);
0147     sender()->deleteLater();
0148     m_saveButton->setEnabled(true);
0149 }