aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/General/scrobbler/settingsdialog.cpp
blob: c5ca85cc77fb75c04fb209b650bd72f68532a3c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/***************************************************************************
 *   Copyright (C) 2008-2013 by Ilya Kotov                                 *
 *   forkotov02@hotmail.ru                                                 *
 *                                                                         *
 *   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, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/

#include <QSettings>
#include <QMessageBox>
#include <qmmp/qmmp.h>
#include "lastfmscrobbler.h"
#include "settingsdialog.h"

SettingsDialog::SettingsDialog(QWidget *parent) : QDialog(parent)
{
    m_ui.setupUi(this);
    m_lastfmAuth = new LastfmAuth(this);
    connect(m_lastfmAuth, SIGNAL(tokenRequestFinished(int)), SLOT(processTokenResponse(int)));
    connect(m_lastfmAuth, SIGNAL(sessionRequestFinished(int)), SLOT(processSessionResponse(int)));
    connect(m_lastfmAuth, SIGNAL(checkSessionFinished(int)), SLOT(processCheckResponse(int)));
    QSettings settings(Qmmp::configFile(), QSettings::IniFormat);
    settings.beginGroup("Scrobbler");
    m_ui.lastfmGroupBox->setChecked(settings.value("use_lastfm", false).toBool());
    m_ui.sessionLineEdit_lastfm->setText(settings.value("lastfm_session").toString());
    m_ui.librefmGroupBox->setChecked(settings.value("use_librefm", false).toBool());
    m_ui.userLineEdit_libre->setText(settings.value("librefm_login").toString());
    m_ui.passwordLineEdit_libre->setText(settings.value("librefm_password").toString());
    settings.endGroup();
}

SettingsDialog::~SettingsDialog()
{}

void SettingsDialog::accept()
{
    QSettings settings(Qmmp::configFile(), QSettings::IniFormat);
    settings.beginGroup("Scrobbler");
    settings.setValue("use_lastfm", m_ui.lastfmGroupBox->isChecked());
    settings.setValue("lastfm_session",m_ui.sessionLineEdit_lastfm->text());
    settings.setValue("use_librefm", m_ui.librefmGroupBox->isChecked());
    settings.setValue("librefm_login",m_ui.userLineEdit_libre->text());
    settings.setValue("librefm_password", m_ui.passwordLineEdit_libre->text());
    settings.endGroup();
    QDialog::accept();
}

void SettingsDialog::on_newSessionButton_lastfm_clicked()
{
    m_ui.newSessionButton_lastfm->setEnabled(false);
    m_lastfmAuth->getToken();
}

void SettingsDialog::processTokenResponse(int error)
{
    m_ui.newSessionButton_lastfm->setEnabled(true);
    switch(error)
    {
    case LastfmAuth::NO_ERROR:
        QMessageBox::information(this,
                                 tr("Message"),
                                 tr("1. Wait for browser startup.") + "\n" +
                                 tr("2. Allow Qmmp to scrobble tracks to your Last.fm account.") + "\n" +
                                 tr("3. Press \"OK\"."));
        m_ui.newSessionButton_lastfm->setEnabled(false);
        m_lastfmAuth->getSession();
        break;
    case LastfmAuth::NETWORK_ERROR:
        QMessageBox::warning(this, tr("Error"), tr("Network error."));
        break;
    case LastfmAuth::LASTFM_ERROR:
    default:
        QMessageBox::warning(this, tr("Error"), tr("Unable to register new session."));
    }
}

void SettingsDialog::processSessionResponse(int error)
{
    m_ui.newSessionButton_lastfm->setEnabled(true);
    switch(error)
    {
    case LastfmAuth::NO_ERROR:
    {
        QMessageBox::information(this, tr("Message"), tr("New session has been received successfully."));
        m_ui.sessionLineEdit_lastfm->setText(m_lastfmAuth->session());

        QSettings settings(Qmmp::configFile(), QSettings::IniFormat);
        settings.setValue("Scrobbler/lastfm_session",m_ui.sessionLineEdit_lastfm->text());
        break;
    }
    case LastfmAuth::NETWORK_ERROR:
        QMessageBox::warning(this, tr("Error"), tr("Network error."));
        break;
    case LastfmAuth::LASTFM_ERROR:
    default:
        QMessageBox::warning(this, tr("Error"), tr("Unable to register new session."));
    }
}

void SettingsDialog::on_checkButton_lastfm_clicked()
{
    if(!m_ui.sessionLineEdit_lastfm->text().isEmpty())
    {
        m_ui.checkButton_lastfm->setEnabled(false);
        m_lastfmAuth->checkSession(m_ui.sessionLineEdit_lastfm->text());
    }
}

void SettingsDialog::processCheckResponse(int error)
{
    m_ui.checkButton_lastfm->setEnabled(true);
    switch(error)
    {
    case LastfmAuth::NO_ERROR:
    {
        QMessageBox::information(this, tr("Message"), tr("Permission granted."));
        m_ui.sessionLineEdit_lastfm->setText(m_lastfmAuth->session());
        break;
    }
    case LastfmAuth::NETWORK_ERROR:
        QMessageBox::warning(this, tr("Error"), tr("Network error."));
        break;
    case LastfmAuth::LASTFM_ERROR:
    default:
        QMessageBox::warning(this, tr("Error"), tr("Permission denied."));
    }
}