Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit ec8d20a3 authored by Michael Pyne's avatar Michael Pyne

Port scrobbler code away from KDialog.

parent cf75c584
......@@ -20,34 +20,33 @@
#include "juk_debug.h"
#include <KLineEdit>
#include <QPushButton>
#include <KLocalizedString>
#include <KMessageBox>
#include <kglobal.h>
#include <KConfigGroup>
#include <QPushButton>
#include <QLayout>
#include <QLabel>
#include <QFormLayout>
ScrobbleConfigDlg::ScrobbleConfigDlg(QWidget* parent, Qt::WindowFlags f)
: KDialog(parent, f)
, m_wallet(0)
ScrobbleConfigDlg::ScrobbleConfigDlg(QWidget* parent)
: QDialog(parent)
, m_wallet(Scrobbler::openKWallet())
{
setWindowTitle(i18n("Configure scrobbling..."));
setButtons(Apply | Cancel);
m_passwordEdit = new KLineEdit(this);
m_passwordEdit->setPasswordMode(true);
m_usernameEdit = new KLineEdit(this);
m_testButton = new QPushButton(i18n("Test login..."), this);
m_testFeedbackLabel = new QLabel("");
auto vboxLayout = new QVBoxLayout(this);
QWidget *mainWidget = new QWidget();
QFormLayout *layout = new QFormLayout();
mainWidget->setLayout(layout);
QLabel *infoLabel = new QLabel(i18n("Please enter your <a href=\"http://last.fm/\">last.fm</a> login credentials:"));
QFormLayout *layout = new QFormLayout(mainWidget);
QLabel *infoLabel = new QLabel(i18n("Please enter your <a href=\"https://last.fm/\">last.fm</a> login credentials:"));
infoLabel->setOpenExternalLinks(true);
infoLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
layout->addRow(infoLabel);
......@@ -55,19 +54,23 @@ ScrobbleConfigDlg::ScrobbleConfigDlg(QWidget* parent, Qt::WindowFlags f)
layout->addRow(new QLabel(i18n("Password:")), m_passwordEdit);
layout->addRow(m_testButton);
layout->addRow(m_testFeedbackLabel);
auto dlgButtonBox = new QDialogButtonBox(QDialogButtonBox::Save | QDialogButtonBox::Cancel);
vboxLayout->addWidget(mainWidget);
vboxLayout->addStretch();
vboxLayout->addWidget(dlgButtonBox);
connect(m_passwordEdit, SIGNAL(textEdited(QString)), this, SLOT(valuesChanged()));
connect(m_usernameEdit, SIGNAL(textEdited(QString)), this, SLOT(valuesChanged()));
connect(m_testButton, SIGNAL(clicked(bool)), this, SLOT(testLogin()));
connect(this, SIGNAL(applyClicked()), this, SLOT(save()));
setMainWidget(mainWidget);
// Loading credentials using either KWallet or KConfigGroup.
m_wallet = Scrobbler::openKWallet();
connect(dlgButtonBox, &QDialogButtonBox::accepted, this, &ScrobbleConfigDlg::save);
connect(dlgButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
if (m_wallet) {
m_saveButton = dlgButtonBox->button(QDialogButtonBox::Save);
// Loading credentials using either KWallet or KConfigGroup.
if (m_wallet) {
QMap<QString, QString> scrobblingCredentials;
m_wallet->readMap("Scrobbling", scrobblingCredentials);
......@@ -75,9 +78,7 @@ ScrobbleConfigDlg::ScrobbleConfigDlg(QWidget* parent, Qt::WindowFlags f)
m_usernameEdit->setText(scrobblingCredentials.value("Username"));
m_passwordEdit->setText(scrobblingCredentials.value("Password"));
}
} else {
// Warning message, KWallet is safer than KConfig.
KMessageBox::information(this, i18n("KWallet is unavailable, your Last.fm credentials will be stored without encryption."), i18n("KWallet is unavailable"));
......@@ -87,25 +88,17 @@ ScrobbleConfigDlg::ScrobbleConfigDlg(QWidget* parent, Qt::WindowFlags f)
}
if (m_passwordEdit->text().isEmpty() || m_usernameEdit->text().isEmpty()) {
button(Apply)->setEnabled(false);
m_saveButton->setEnabled(false);
m_testButton->setEnabled(false);
}
}
ScrobbleConfigDlg::~ScrobbleConfigDlg()
{
delete m_wallet;
}
void ScrobbleConfigDlg::valuesChanged()
{
if (m_usernameEdit->text().isEmpty() || m_passwordEdit->text().isEmpty())
m_testButton->setEnabled(false);
else
m_testButton->setEnabled(true);
button(Apply)->setEnabled(false);
m_testButton->setEnabled(
!m_usernameEdit->text().isEmpty() &&
!m_passwordEdit->text().isEmpty());
m_saveButton->setEnabled(false);
}
void ScrobbleConfigDlg::save()
......@@ -146,7 +139,7 @@ void ScrobbleConfigDlg::invalidLogin()
m_testFeedbackLabel->setText(i18n("Login invalid."));
setEnabled(true);
sender()->deleteLater();
button(Apply)->setEnabled(false);
m_saveButton->setEnabled(false);
}
void ScrobbleConfigDlg::validLogin()
......@@ -154,5 +147,5 @@ void ScrobbleConfigDlg::validLogin()
m_testFeedbackLabel->setText(i18n("Login valid."));
setEnabled(true);
sender()->deleteLater();
button(Apply)->setEnabled(true);
m_saveButton->setEnabled(true);
}
......@@ -15,22 +15,27 @@
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SCROBBLESETTINGS_H
#define SCROBBLESETTINGS_H
#ifndef JUK_SCROBBLESETTINGS_H
#define JUK_SCROBBLESETTINGS_H
#include <KDialog>
#include <QDialog>
#include <KWallet/Wallet>
#include <memory>
using namespace KWallet;
class KLineEdit;
class QAbstractButton;
class QPushButton;
class QLabel;
class ScrobbleConfigDlg : public KDialog
class ScrobbleConfigDlg : public QDialog
{
Q_OBJECT
public:
explicit ScrobbleConfigDlg(QWidget* parent = 0, Qt::WindowFlags f = 0);
~ScrobbleConfigDlg();
explicit ScrobbleConfigDlg(QWidget* parent = nullptr);
private slots:
void testLogin();
......@@ -43,9 +48,10 @@ private:
KLineEdit *m_usernameEdit;
KLineEdit *m_passwordEdit;
QPushButton *m_testButton;
QAbstractButton *m_saveButton;
QLabel *m_testFeedbackLabel;
KWallet::Wallet *m_wallet;
std::unique_ptr<Wallet> m_wallet;
};
#endif//SCROBBLESETTINGS_H
#endif //JUK_SCROBBLESETTINGS_H
......@@ -29,24 +29,22 @@
#include <kconfiggroup.h>
#include <KSharedConfig>
#include <memory>
#include "tag.h"
#include "juk.h"
#include "juk_debug.h"
Scrobbler::Scrobbler(QObject* parent)
: QObject(parent)
, m_networkAccessManager(0)
, m_wallet(0)
, m_networkAccessManager(nullptr)
, m_wallet(Scrobbler::openKWallet())
{
QByteArray sessionKey;
m_wallet = Scrobbler::openKWallet();
if (m_wallet) {
m_wallet->readEntry("SessionKey", sessionKey);
} else {
KConfigGroup config(KSharedConfig::openConfig(), "Scrobbling");
sessionKey.append(config.readEntry("SessionKey", ""));
}
......@@ -55,65 +53,51 @@ Scrobbler::Scrobbler(QObject* parent)
getAuthToken();
}
Scrobbler::~Scrobbler()
{
delete m_wallet;
}
bool Scrobbler::isScrobblingEnabled()
bool Scrobbler::isScrobblingEnabled() // static
{
QString username, password;
if (KWallet::Wallet::folderDoesNotExist(KWallet::Wallet::LocalWallet(), "JuK")) {
// checks without prompting to open the wallet
if (Wallet::folderDoesNotExist(Wallet::LocalWallet(), "JuK")) {
KConfigGroup config(KSharedConfig::openConfig(), "Scrobbling");
username = config.readEntry("Username", "");
password = config.readEntry("Password", "");
} else {
KWallet::Wallet* wallet = Scrobbler::openKWallet();
auto wallet = Scrobbler::openKWallet();
if (wallet) {
QMap<QString, QString> scrobblingCredentials;
wallet->readMap("Scrobbling", scrobblingCredentials);
if (scrobblingCredentials.contains("Username") && scrobblingCredentials.contains("Password")) {
username = scrobblingCredentials["Username"];
password = scrobblingCredentials["Password"];
}
delete wallet;
}
}
return (!username.isEmpty() && !password.isEmpty());
}
KWallet::Wallet* Scrobbler::openKWallet() // static
std::unique_ptr<KWallet::Wallet> Scrobbler::openKWallet() // static
{
const QString walletFolderName = "JuK";
KWallet::Wallet* wallet = KWallet::Wallet::openWallet(KWallet::Wallet::LocalWallet(), 0);
if (wallet) {
using KWallet::Wallet;
if (!wallet->hasFolder(walletFolderName)) {
if (!wallet->createFolder(walletFolderName)) {
delete wallet;
return 0;
}
const QString walletFolderName = "JuK";
std::unique_ptr<Wallet> wallet(Wallet::openWallet(
Wallet::LocalWallet(),
JuK::JuKInstance()->winId()));
if(wallet) {
if(!wallet->hasFolder(walletFolderName) &&
!wallet->createFolder(walletFolderName))
{
return nullptr;
}
if (!wallet->setFolder(walletFolderName)) {
delete wallet;
return 0;
if(!wallet->setFolder(walletFolderName))
{
return nullptr;
}
}
......
......@@ -24,8 +24,12 @@
#include <KWallet/Wallet>
#include <memory>
#include "filehandle.h"
using namespace KWallet;
class QByteArray;
class QNetworkAccessManager;
......@@ -34,12 +38,12 @@ class QNetworkAccessManager;
*/
class Scrobbler : public QObject {
Q_OBJECT
public:
explicit Scrobbler(QObject* parent = 0);
virtual ~Scrobbler();
explicit Scrobbler(QObject* parent = nullptr);
static bool isScrobblingEnabled();
static KWallet::Wallet* openKWallet();
static std::unique_ptr<Wallet> openKWallet();
public slots:
void nowPlaying(const FileHandle&);
......@@ -64,7 +68,7 @@ private:
FileHandle m_file;
QNetworkAccessManager *m_networkAccessManager;
KWallet::Wallet *m_wallet;
std::unique_ptr<Wallet> m_wallet;
};
#endif /* JUK_SCROBBLER_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment