Commit b2577168 authored by Arnold Dumas's avatar Arnold Dumas

Use KWallet to store scrobbling credentials.

REVIEW: 116763
parent bc37a3dd
/**
* Copyright (C) 2012 Martin Sandsmark <martin.sandsmark@kde.org>
* Copyright (C) 2014 Arnold Dumas <contact@arnolddumas.fr>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
......@@ -16,9 +17,13 @@
#include "scrobbleconfigdlg.h"
#include "scrobbler.h"
#include <KDebug>
#include <KLineEdit>
#include <KPushButton>
#include <KLocalizedString>
#include <KMessageBox>
#include <QLayout>
#include <QLabel>
#include <QFormLayout>
......@@ -27,6 +32,7 @@
ScrobbleConfigDlg::ScrobbleConfigDlg(QWidget* parent, Qt::WindowFlags f)
: KDialog(parent, f)
, m_wallet(0)
{
setWindowTitle(i18n("Configure scrobbling..."));
......@@ -56,15 +62,39 @@ ScrobbleConfigDlg::ScrobbleConfigDlg(QWidget* parent, Qt::WindowFlags f)
connect(this, SIGNAL(applyClicked()), this, SLOT(save()));
setMainWidget(mainWidget);
KConfigGroup config(KGlobal::config(), "Scrobbling");
m_usernameEdit->setText(config.readEntry("Username", ""));//TODO: use kwallet
m_passwordEdit->setText(config.readEntry("Password", ""));//TODO: use kwallet
// Loading credentials using either KWallet or KConfigGroup.
m_wallet = Scrobbler::openKWallet();
if (m_wallet) {
QMap<QString, QString> scrobblingCredentials;
m_wallet->readMap("Scrobbling", scrobblingCredentials);
if (scrobblingCredentials.contains("Username") && scrobblingCredentials.contains("Password")) {
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"));
KConfigGroup config(KGlobal::config(), "Scrobbling");
m_usernameEdit->setText(config.readEntry("Username", ""));
m_passwordEdit->setText(config.readEntry("Password", ""));
}
if (m_passwordEdit->text().isEmpty() || m_usernameEdit->text().isEmpty())
button(Apply)->setEnabled(false);
}
ScrobbleConfigDlg::~ScrobbleConfigDlg()
{
delete m_wallet;
}
void ScrobbleConfigDlg::valuesChanged()
{
button(Apply)->setEnabled(false);
......@@ -73,10 +103,24 @@ void ScrobbleConfigDlg::valuesChanged()
void ScrobbleConfigDlg::save()
{
QDialog::accept();
KConfigGroup config(KGlobal::config(), "Scrobbling");
config.writeEntry("Username", m_usernameEdit->text());
config.writeEntry("Password", m_passwordEdit->text());
if (m_wallet) {
QMap<QString, QString> scrobblingCredentials;
scrobblingCredentials.insert("Username", m_usernameEdit->text());
scrobblingCredentials.insert("Password", m_passwordEdit->text());
if (!m_wallet->writeMap("Scrobbling", scrobblingCredentials)) {
kError() << "Couldn't save Last.fm credentials using KWallet.";
}
} else {
KConfigGroup config(KGlobal::config(), "Scrobbling");
config.writeEntry("Username", m_usernameEdit->text());
config.writeEntry("Password", m_passwordEdit->text());
}
}
void ScrobbleConfigDlg::testLogin()
......
/**
* Copyright (C) 2012 Martin Sandsmark <martin.sandsmark@kde.org>
* Copyright (C) 2014 Arnold Dumas <contact@arnolddumas.fr>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
......@@ -18,6 +19,7 @@
#define SCROBBLESETTINGS_H
#include <KDialog>
#include <KWallet/Wallet>
class KLineEdit;
class KPushButton;
......@@ -28,6 +30,7 @@ class ScrobbleConfigDlg : public KDialog
Q_OBJECT
public:
explicit ScrobbleConfigDlg(QWidget* parent = 0, Qt::WindowFlags f = 0);
~ScrobbleConfigDlg();
private slots:
void testLogin();
......@@ -41,6 +44,8 @@ private:
KLineEdit *m_passwordEdit;
KPushButton *m_testButton;
QLabel *m_testFeedbackLabel;
KWallet::Wallet *m_wallet;
};
#endif//SCROBBLESETTINGS_H
/**
* Copyright (C) 2012 Martin Sandsmark <martin.sandsmark@kde.org>
* Copyright (C) 2014 Arnold Dumas <contact@arnolddumas.fr>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
......@@ -35,11 +36,21 @@
Scrobbler::Scrobbler(QObject* parent)
: QObject(parent)
, m_networkAccessManager(0)
, m_wallet(0)
{
KConfigGroup config(KGlobal::config(), "Scrobbling");
QByteArray sessionKey;
//TODO: use kwallet
QString sessionKey = config.readEntry("SessionKey", "");
m_wallet = Scrobbler::openKWallet();
if (m_wallet) {
m_wallet->readEntry("SessionKey", sessionKey);
} else {
KConfigGroup config(KGlobal::config(), "Scrobbling");
sessionKey.append(config.readEntry("SessionKey", ""));
}
if(sessionKey.isEmpty())
getAuthToken();
......@@ -47,19 +58,69 @@ Scrobbler::Scrobbler(QObject* parent)
Scrobbler::~Scrobbler()
{
delete m_wallet;
}
bool Scrobbler::isScrobblingEnabled()
{
KConfigGroup config(KGlobal::config(), "Scrobbling");
QString username, password;
if (KWallet::Wallet::folderDoesNotExist(KWallet::Wallet::LocalWallet(), "JuK")) {
KConfigGroup config(KGlobal::config(), "Scrobbling");
username = config.readEntry("Username", "");
password = config.readEntry("Password", "");
} else {
KWallet::Wallet* wallet = Scrobbler::openKWallet();
if (wallet) {
QMap<QString, QString> scrobblingCredentials;
wallet->readMap("Scrobbling", scrobblingCredentials);
// TODO: use kwallet
QString username = config.readEntry("Username", "");
QString password = config.readEntry("Password", "");
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
{
const QString walletFolderName = "JuK";
KWallet::Wallet* wallet = KWallet::Wallet::openWallet(KWallet::Wallet::LocalWallet(), 0);
if (wallet) {
if (!wallet->hasFolder(walletFolderName)) {
if (!wallet->createFolder(walletFolderName)) {
delete wallet;
return 0;
}
}
if (!wallet->setFolder(walletFolderName)) {
delete wallet;
return 0;
}
}
return wallet;
}
QByteArray Scrobbler::md5(QByteArray data)
{
return QCryptographicHash::hash(data, QCryptographicHash::Md5)
......@@ -111,11 +172,25 @@ void Scrobbler::getAuthToken(QString username, QString password)
void Scrobbler::getAuthToken()
{
KConfigGroup config(KGlobal::config(), "Scrobbling");
QString username, password;
if (m_wallet) {
QMap<QString, QString> scrobblingCredentials;
m_wallet->readMap("Scrobbling", scrobblingCredentials);
if (scrobblingCredentials.contains("Username") && scrobblingCredentials.contains("Password")) {
// TODO: use kwallet
QString username = config.readEntry("Username", "");
QString password = config.readEntry("Password", "");
username = scrobblingCredentials["Username"];
password = scrobblingCredentials["Password"];
}
} else {
KConfigGroup config(KGlobal::config(), "Scrobbling");
username = config.readEntry("Username", "");
password = config.readEntry("Password", "");
}
if(username.isEmpty() || password.isEmpty())
return;
......@@ -148,20 +223,33 @@ void Scrobbler::handleAuthenticationReply()
return;
}
KConfigGroup config(KGlobal::config(), "Scrobbling");
config.writeEntry("SessionKey", sessionKey);
if (m_wallet) {
m_wallet->writeEntry("SessionKey", sessionKey.toUtf8());
} else {
KConfigGroup config(KGlobal::config(), "Scrobbling");
config.writeEntry("SessionKey", sessionKey);
}
emit validAuth();
}
void Scrobbler::nowPlaying(const FileHandle& file)
{
KConfigGroup config(KGlobal::config(), "Scrobbling");
QString sessionKey;
//TODO: use kwallet
QString sessionKey = config.readEntry("SessionKey", "");
if(sessionKey.isEmpty()) {
getAuthToken();
return;
if (m_wallet) {
QByteArray sessionKeyByteArray;
m_wallet->readEntry("SessionKey", sessionKeyByteArray);
sessionKey = QString::fromLatin1(sessionKeyByteArray);
} else {
KConfigGroup config(KGlobal::config(), "Scrobbling");
sessionKey = config.readEntry("SessionKey", "");
}
if (!m_file.isNull()) {
......@@ -186,10 +274,19 @@ void Scrobbler::nowPlaying(const FileHandle& file)
void Scrobbler::scrobble()
{
KConfigGroup config(KGlobal::config(), "Scrobbling");
QString sessionKey;
if (m_wallet) {
//TODO: use kwallet
QString sessionKey = config.readEntry("SessionKey", "");
QByteArray sessionKeyByteArray;
m_wallet->readEntry("SessionKey", sessionKeyByteArray);
sessionKey = QString::fromLatin1(sessionKeyByteArray);
} else {
KConfigGroup config(KGlobal::config(), "Scrobbling");
sessionKey = config.readEntry("SessionKey", "");
}
if(sessionKey.isEmpty()) {
getAuthToken();
......
/**
* Copyright (C) 2012 Martin Sandsmark <martin.sandsmark@kde.org>
* Copyright (C) 2014 Arnold Dumas <contact@arnolddumas.fr>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
......@@ -21,6 +22,8 @@
#include <QMap>
#include <QDateTime>
#include <KWallet/Wallet>
#include "filehandle.h"
class QByteArray;
......@@ -36,6 +39,7 @@ public:
virtual ~Scrobbler();
static bool isScrobblingEnabled();
static KWallet::Wallet* openKWallet();
public slots:
void nowPlaying(const FileHandle&);
......@@ -59,6 +63,8 @@ private:
QDateTime m_playbackTimer;
FileHandle m_file;
QNetworkAccessManager *m_networkAccessManager;
KWallet::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