1 /* GUI_LastFmPreferences.cpp */
2
3 /* Copyright (C) 2011-2020 Michael Lugmair (Lucio Carreras)
4 *
5 * This file is part of sayonara player
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21
22 /*
23 * GUI_LastFmPreferences.cpp
24 *
25 * Created on: Apr 21, 2011
26 * Author: Michael Lugmair (Lucio Carreras)
27 */
28
29 #include "GUI_LastFmPreferences.h"
30 #include "Gui/Preferences/ui_GUI_LastFmPreferences.h"
31 #include "Gui/Utils/Style.h"
32
33 #include "Components/Streaming/LastFM/LastFM.h"
34
35 #include "Utils/Language/Language.h"
36 #include "Utils/Settings/Settings.h"
37 #include "Utils/Utils.h"
38 #include "Utils/Crypt.h"
39
40 struct GUI_LastFmPreferences::Private
41 {
42 LastFM::Base* lfm=nullptr;
43
PrivateGUI_LastFmPreferences::Private44 Private(LastFM::Base* lfm) :
45 lfm(lfm)
46 {}
47
~PrivateGUI_LastFmPreferences::Private48 ~Private()
49 {
50 delete lfm; lfm = nullptr;
51 }
52 };
53
GUI_LastFmPreferences(const QString & identifier,LastFM::Base * lfm)54 GUI_LastFmPreferences::GUI_LastFmPreferences(const QString& identifier, LastFM::Base* lfm) :
55 Base(identifier)
56 {
57 m = Pimpl::make<Private>(lfm);
58 }
59
~GUI_LastFmPreferences()60 GUI_LastFmPreferences::~GUI_LastFmPreferences()
61 {
62 if(ui)
63 {
64 delete ui; ui=nullptr;
65 }
66 }
67
initUi()68 void GUI_LastFmPreferences::initUi()
69 {
70 setupParent(this, &ui);
71
72 revert();
73
74 loginFinished(m->lfm->isLoggedIn());
75
76 connect(ui->btnLogin, &QPushButton::clicked, this, &GUI_LastFmPreferences::loginClicked);
77 connect(ui->cbActivate, &QCheckBox::toggled, this, &GUI_LastFmPreferences::activeChanged);
78 connect(m->lfm, &LastFM::Base::sigLoggedIn, this, &GUI_LastFmPreferences::loginFinished);
79 }
80
actionName() const81 QString GUI_LastFmPreferences::actionName() const
82 {
83 return "Last.fm";
84 }
85
retranslate()86 void GUI_LastFmPreferences::retranslate()
87 {
88 ui->retranslateUi(this);
89 ui->labActivate->setText(Lang::get(Lang::Active));
90 ui->labSeconds->setText(Lang::get(Lang::Seconds));
91
92 loginFinished(m->lfm->isLoggedIn());
93 }
94
commit()95 bool GUI_LastFmPreferences::commit()
96 {
97 bool active = ui->cbActivate->isChecked();
98 QString username = ui->leUsername->text();
99 QString password = ui->lePassword->text();
100
101 SetSetting(Set::LFM_Username, username);
102 SetSetting(Set::LFM_Password, Util::Crypt::encrypt(password));
103 SetSetting(Set::LFM_ScrobbleTimeSec, ui->sbScrobbleTime->value());
104 SetSetting(Set::LFM_Active, active);
105
106 return true;
107 }
108
revert()109 void GUI_LastFmPreferences::revert()
110 {
111 bool active = GetSetting(Set::LFM_Active);
112 const QString username = GetSetting(Set::LFM_Username);
113 const QString password = Util::Crypt::decrypt(GetSetting(Set::LFM_Password));
114
115 activeChanged(active);
116 loginFinished(m->lfm->isLoggedIn());
117
118 ui->leUsername->setText(username);
119 ui->lePassword->setText(password);
120 ui->sbScrobbleTime->setValue( GetSetting(Set::LFM_ScrobbleTimeSec) );
121 ui->cbActivate->setChecked(active);
122
123 const QString link = Util::createLink("https://last.fm", Style::isDark(), true);
124 ui->labWebsite->setText(link);
125 }
126
loginClicked()127 void GUI_LastFmPreferences::loginClicked()
128 {
129 if(ui->leUsername->text().length() < 3) {
130 return;
131 }
132
133 if(ui->lePassword->text().length() < 3) {
134 return;
135 }
136
137 ui->btnLogin->setEnabled(false);
138
139 const QString username = ui->leUsername->text();
140 const QString password = ui->lePassword->text();
141
142 m->lfm->login(username, password);
143 }
144
activeChanged(bool active)145 void GUI_LastFmPreferences::activeChanged(bool active)
146 {
147 if(!isUiInitialized()){
148 return;
149 }
150
151 ui->leUsername->setEnabled(active);
152 ui->lePassword->setEnabled(active);
153 }
154
loginFinished(bool success)155 void GUI_LastFmPreferences::loginFinished(bool success)
156 {
157 if(!isUiInitialized()){
158 return;
159 }
160
161 if(success){
162 ui->labStatus->setText(tr("Logged in"));
163 }
164
165 else{
166 ui->labStatus->setText(tr("Not logged in"));
167 }
168
169 ui->btnLogin->setEnabled(true);
170 }
171