[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [juk/frameworks] /: Port scrobbler code away from KDialog.
From:       Michael Pyne <null () kde ! org>
Date:       2017-09-30 21:33:49
Message-ID: E1dyPOP-0007iK-Eu () code ! kde ! org
[Download RAW message or body]

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 <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);
 }
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 <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
diff --git a/scrobbler.cpp b/scrobbler.cpp
index 9bdc21f..521b678 100644
--- a/scrobbler.cpp
+++ b/scrobbler.cpp
@@ -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;
         }
     }
 
diff --git a/scrobbler.h b/scrobbler.h
index 80b208c..acefa25 100644
--- a/scrobbler.h
+++ b/scrobbler.h
@@ -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 */


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic