File indexing completed on 2023-05-30 11:30:51
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 <QFormLayout> 0031 #include <QLabel> 0032 #include <QPushButton> 0033 0034 ScrobbleConfigDlg::ScrobbleConfigDlg(QWidget* parent) 0035 : QDialog(parent) 0036 , m_wallet(Scrobbler::openKWallet()) 0037 { 0038 setWindowTitle(i18n("Configure scrobbling...")); 0039 0040 m_passwordEdit = new KPasswordLineEdit(this); 0041 m_usernameEdit = new KLineEdit(this); 0042 m_testButton = new QPushButton(i18n("Test login..."), this); 0043 m_testFeedbackLabel = new QLabel(""); 0044 0045 auto vboxLayout = new QVBoxLayout(this); 0046 0047 QWidget *mainWidget = new QWidget(); 0048 QFormLayout *layout = new QFormLayout(mainWidget); 0049 QLabel *infoLabel = new QLabel(i18n("Please enter your <a href=\"https://last.fm/\">last.fm</a> login credentials:")); 0050 infoLabel->setOpenExternalLinks(true); 0051 infoLabel->setTextInteractionFlags(Qt::TextBrowserInteraction); 0052 layout->addRow(infoLabel); 0053 layout->addRow(new QLabel(i18n("Username:")), m_usernameEdit); 0054 layout->addRow(new QLabel(i18n("Password:")), m_passwordEdit); 0055 layout->addRow(m_testButton); 0056 layout->addRow(m_testFeedbackLabel); 0057 0058 auto dlgButtonBox = new QDialogButtonBox(QDialogButtonBox::Save | QDialogButtonBox::Cancel); 0059 vboxLayout->addWidget(mainWidget); 0060 vboxLayout->addStretch(); 0061 vboxLayout->addWidget(dlgButtonBox); 0062 0063 connect(m_passwordEdit, &KPasswordLineEdit::passwordChanged, this, &ScrobbleConfigDlg::valuesChanged); 0064 connect(m_usernameEdit, SIGNAL(textEdited(QString)), this, SLOT(valuesChanged())); 0065 connect(m_testButton, SIGNAL(clicked(bool)), this, SLOT(testLogin())); 0066 0067 connect(dlgButtonBox, &QDialogButtonBox::accepted, this, &ScrobbleConfigDlg::save); 0068 connect(dlgButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); 0069 0070 m_saveButton = dlgButtonBox->button(QDialogButtonBox::Save); 0071 0072 // Loading credentials using either KWallet or KConfigGroup. 0073 if (m_wallet) { 0074 QMap<QString, QString> scrobblingCredentials; 0075 m_wallet->readMap("Scrobbling", scrobblingCredentials); 0076 0077 if (scrobblingCredentials.contains("Username") && scrobblingCredentials.contains("Password")) { 0078 m_usernameEdit->setText(scrobblingCredentials.value("Username")); 0079 m_passwordEdit->setPassword(scrobblingCredentials.value("Password")); 0080 } 0081 } else { 0082 // Warning message, KWallet is safer than KConfig. 0083 KMessageBox::information(this, i18n("KWallet is unavailable, your Last.fm credentials will be stored without encryption."), i18n("KWallet is unavailable")); 0084 0085 KConfigGroup config(KSharedConfig::openConfig(), "Scrobbling"); 0086 m_usernameEdit->setText(config.readEntry("Username", "")); 0087 m_passwordEdit->setPassword(config.readEntry("Password", "")); 0088 } 0089 0090 if (m_passwordEdit->password().isEmpty() || m_usernameEdit->text().isEmpty()) { 0091 m_saveButton->setEnabled(false); 0092 m_testButton->setEnabled(false); 0093 } 0094 } 0095 0096 void ScrobbleConfigDlg::valuesChanged() 0097 { 0098 m_testButton->setEnabled( 0099 !m_usernameEdit->text().isEmpty() && 0100 !m_passwordEdit->password().isEmpty()); 0101 m_saveButton->setEnabled(false); 0102 } 0103 0104 void ScrobbleConfigDlg::save() 0105 { 0106 QDialog::accept(); 0107 0108 if (m_wallet) { 0109 QMap<QString, QString> scrobblingCredentials; 0110 scrobblingCredentials.insert("Username", m_usernameEdit->text()); 0111 scrobblingCredentials.insert("Password", m_passwordEdit->password()); 0112 0113 if (!m_wallet->writeMap("Scrobbling", scrobblingCredentials)) { 0114 qCCritical(JUK_LOG) << "Couldn't save Last.fm credentials using KWallet."; 0115 } 0116 0117 } else { 0118 KConfigGroup config(KSharedConfig::openConfig(), "Scrobbling"); 0119 config.writeEntry("Username", m_usernameEdit->text()); 0120 config.writeEntry("Password", m_passwordEdit->password()); 0121 } 0122 } 0123 0124 void ScrobbleConfigDlg::testLogin() 0125 { 0126 m_testFeedbackLabel->setText(i18n("Validating login...")); 0127 Scrobbler *scrobbler = new Scrobbler(this); 0128 connect(scrobbler, SIGNAL(validAuth()), this, SLOT(validLogin())); 0129 connect(scrobbler, SIGNAL(invalidAuth()), this, SLOT(invalidLogin())); 0130 setEnabled(false); 0131 scrobbler->getAuthToken(m_usernameEdit->text(), m_passwordEdit->password()); 0132 } 0133 0134 void ScrobbleConfigDlg::invalidLogin() 0135 { 0136 m_testFeedbackLabel->setText(i18n("Login invalid.")); 0137 setEnabled(true); 0138 sender()->deleteLater(); 0139 m_saveButton->setEnabled(false); 0140 } 0141 0142 void ScrobbleConfigDlg::validLogin() 0143 { 0144 m_testFeedbackLabel->setText(i18n("Login valid.")); 0145 setEnabled(true); 0146 sender()->deleteLater(); 0147 m_saveButton->setEnabled(true); 0148 }