Git commit ec8d20a35da7d46cd316d4ec229643923d5db54c by Michael Pyne. Committed on 30/09/2017 at 20:36. Pushed by mpyne into branch 'frameworks'. Port scrobbler code away from KDialog. M +29 -36 scrobbleconfigdlg.cpp M +14 -8 scrobbleconfigdlg.h M +24 -40 scrobbler.cpp M +8 -4 scrobbler.h https://commits.kde.org/juk/ec8d20a35da7d46cd316d4ec229643923d5db54c diff --git a/scrobbleconfigdlg.cpp b/scrobbleconfigdlg.cpp index 8402a19..da95543 100644 --- a/scrobbleconfigdlg.cpp +++ b/scrobbleconfigdlg.cpp @@ -20,34 +20,33 @@ #include "juk_debug.h" = #include -#include #include #include #include +#include = +#include #include #include #include = - -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 =3D new KLineEdit(this); m_passwordEdit->setPasswordMode(true); m_usernameEdit =3D new KLineEdit(this); m_testButton =3D new QPushButton(i18n("Test login..."), this); m_testFeedbackLabel =3D new QLabel(""); - = + + auto vboxLayout =3D new QVBoxLayout(this); + QWidget *mainWidget =3D new QWidget(); - QFormLayout *layout =3D new QFormLayout(); - mainWidget->setLayout(layout); - QLabel *infoLabel =3D new QLabel(i18n("Please enter your last.fm login credentials:")); + QFormLayout *layout =3D new QFormLayout(mainWidget); + QLabel *infoLabel =3D new QLabel(i18n("Please enter your last.fm login credentials:")); infoLabel->setOpenExternalLinks(true); infoLabel->setTextInteractionFlags(Qt::TextBrowserInteraction); layout->addRow(infoLabel); @@ -55,19 +54,23 @@ ScrobbleConfigDlg::ScrobbleConfigDlg(QWidget* parent, Q= t::WindowFlags f) layout->addRow(new QLabel(i18n("Password:")), m_passwordEdit); layout->addRow(m_testButton); layout->addRow(m_testFeedbackLabel); - = + + auto dlgButtonBox =3D new QDialogButtonBox(QDialogButtonBox::Save | QD= ialogButtonBox::Cancel); + vboxLayout->addWidget(mainWidget); + vboxLayout->addStretch(); + vboxLayout->addWidget(dlgButtonBox); + connect(m_passwordEdit, SIGNAL(textEdited(QString)), this, SLOT(values= Changed())); connect(m_usernameEdit, SIGNAL(textEdited(QString)), this, SLOT(values= Changed())); 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 =3D Scrobbler::openKWallet(); + connect(dlgButtonBox, &QDialogButtonBox::accepted, this, &ScrobbleConf= igDlg::save); + connect(dlgButtonBox, &QDialogButtonBox::rejected, this, &QDialog::rej= ect); = - if (m_wallet) { + m_saveButton =3D dlgButtonBox->button(QDialogButtonBox::Save); = + // Loading credentials using either KWallet or KConfigGroup. + if (m_wallet) { QMap 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, Q= t::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); } diff --git a/scrobbleconfigdlg.h b/scrobbleconfigdlg.h index db135cd..a8403de 100644 --- a/scrobbleconfigdlg.h +++ b/scrobbleconfigdlg.h @@ -15,22 +15,27 @@ * this program. If not, see . */ = -#ifndef SCROBBLESETTINGS_H -#define SCROBBLESETTINGS_H +#ifndef JUK_SCROBBLESETTINGS_H +#define JUK_SCROBBLESETTINGS_H = -#include +#include #include = +#include + +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 =3D 0, Qt::WindowFlags f = =3D 0); - ~ScrobbleConfigDlg(); + explicit ScrobbleConfigDlg(QWidget* parent =3D 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 m_wallet; }; = -#endif//SCROBBLESETTINGS_H +#endif //JUK_SCROBBLESETTINGS_H diff --git a/scrobbler.cpp b/scrobbler.cpp index 9bdc21f..521b678 100644 --- a/scrobbler.cpp +++ b/scrobbler.cpp @@ -29,24 +29,22 @@ #include #include = +#include + #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 =3D 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 =3D config.readEntry("Username", ""); password =3D config.readEntry("Password", ""); - } else { - - KWallet::Wallet* wallet =3D Scrobbler::openKWallet(); - + auto wallet =3D Scrobbler::openKWallet(); if (wallet) { - QMap scrobblingCredentials; wallet->readMap("Scrobbling", scrobblingCredentials); = if (scrobblingCredentials.contains("Username") && scrobblingCr= edentials.contains("Password")) { - username =3D scrobblingCredentials["Username"]; password =3D scrobblingCredentials["Password"]; } - - delete wallet; } } = return (!username.isEmpty() && !password.isEmpty()); } = -KWallet::Wallet* Scrobbler::openKWallet() // static +std::unique_ptr Scrobbler::openKWallet() // static { - const QString walletFolderName =3D "JuK"; - - KWallet::Wallet* wallet =3D KWallet::Wallet::openWallet(KWallet::Walle= t::LocalWallet(), 0); - - if (wallet) { + using KWallet::Wallet; = - if (!wallet->hasFolder(walletFolderName)) { - - if (!wallet->createFolder(walletFolderName)) { - - delete wallet; - return 0; - } + const QString walletFolderName =3D "JuK"; + std::unique_ptr 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; } } = diff --git a/scrobbler.h b/scrobbler.h index 80b208c..acefa25 100644 --- a/scrobbler.h +++ b/scrobbler.h @@ -24,8 +24,12 @@ = #include = +#include + #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 =3D 0); - virtual ~Scrobbler(); + explicit Scrobbler(QObject* parent =3D nullptr); = static bool isScrobblingEnabled(); - static KWallet::Wallet* openKWallet(); + static std::unique_ptr 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 m_wallet; }; = #endif /* JUK_SCROBBLER_H */