scrobbleconfigdlg.cpp 5.22 KB
Newer Older
1 2
/**
 * Copyright (C) 2012 Martin Sandsmark <martin.sandsmark@kde.org>
3
 * Copyright (C) 2014 Arnold Dumas <contact@arnolddumas.fr>
4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */
17 18 19

#include "scrobbleconfigdlg.h"
#include "scrobbler.h"
Michael Pyne's avatar
Michael Pyne committed
20
#include "juk_debug.h"
21

22
#include <KLineEdit>
23
#include <QPushButton>
24
#include <KLocalizedString>
25
#include <KMessageBox>
26
#include <kglobal.h>
27

28 29 30 31 32 33 34
#include <QLayout>
#include <QLabel>
#include <QFormLayout>


ScrobbleConfigDlg::ScrobbleConfigDlg(QWidget* parent, Qt::WindowFlags f)
    : KDialog(parent, f)
35
    , m_wallet(0)
36 37 38 39 40 41 42 43
{
    setWindowTitle(i18n("Configure scrobbling..."));
    
    setButtons(Apply | Cancel);
    
    m_passwordEdit = new KLineEdit(this);
    m_passwordEdit->setPasswordMode(true);
    m_usernameEdit = new KLineEdit(this);
44
    m_testButton = new QPushButton(i18n("Test login..."), this);
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    m_testFeedbackLabel = new QLabel("");
    
    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:"));
    infoLabel->setOpenExternalLinks(true);
    infoLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
    layout->addRow(infoLabel);
    layout->addRow(new QLabel(i18n("Username:")), m_usernameEdit);
    layout->addRow(new QLabel(i18n("Password:")), m_passwordEdit);
    layout->addRow(m_testButton);
    layout->addRow(m_testFeedbackLabel);
    
    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);
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

    // 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"));

84
        KConfigGroup config(KSharedConfig::openConfig(), "Scrobbling");
85 86 87 88
        m_usernameEdit->setText(config.readEntry("Username", ""));
        m_passwordEdit->setText(config.readEntry("Password", ""));
    }

89
    if (m_passwordEdit->text().isEmpty() || m_usernameEdit->text().isEmpty()) {
90
        button(Apply)->setEnabled(false);
91 92
        m_testButton->setEnabled(false);
    }
93 94
}

95 96 97 98 99
ScrobbleConfigDlg::~ScrobbleConfigDlg()
{
    delete m_wallet;
}

100 101
void ScrobbleConfigDlg::valuesChanged()
{
102 103 104 105 106 107
    if (m_usernameEdit->text().isEmpty() || m_passwordEdit->text().isEmpty())
        m_testButton->setEnabled(false);

    else
        m_testButton->setEnabled(true);

108 109 110 111 112 113
    button(Apply)->setEnabled(false);
}

void ScrobbleConfigDlg::save()
{
    QDialog::accept();
114 115 116 117 118 119 120 121 122

    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)) {

Michael Pyne's avatar
Michael Pyne committed
123
            qCCritical(JUK_LOG) << "Couldn't save Last.fm credentials using KWallet.";
124 125 126 127
        }

    } else {

128
        KConfigGroup config(KSharedConfig::openConfig(), "Scrobbling");
129 130 131
        config.writeEntry("Username", m_usernameEdit->text());
        config.writeEntry("Password", m_passwordEdit->text());
    }
132 133 134 135 136 137 138 139 140 141 142 143 144 145
}

void ScrobbleConfigDlg::testLogin()
{
    m_testFeedbackLabel->setText(i18n("Validating login..."));
    Scrobbler *scrobbler = new Scrobbler(this);
    connect(scrobbler, SIGNAL(validAuth()), this, SLOT(validLogin()));
    connect(scrobbler, SIGNAL(invalidAuth()), this, SLOT(invalidLogin()));
    setEnabled(false);
    scrobbler->getAuthToken(m_usernameEdit->text(), m_passwordEdit->text());
}

void ScrobbleConfigDlg::invalidLogin()
{
146
    m_testFeedbackLabel->setText(i18n("Login invalid."));
147 148 149 150 151 152 153
    setEnabled(true);
    sender()->deleteLater();
    button(Apply)->setEnabled(false);
}

void ScrobbleConfigDlg::validLogin()
{
154
    m_testFeedbackLabel->setText(i18n("Login valid."));
155 156 157 158
    setEnabled(true);
    sender()->deleteLater();
    button(Apply)->setEnabled(true);
}