1 /*
2  * Cantata
3  *
4  * Copyright (c) 2011-2020 Craig Drummond <craig.p.drummond@gmail.com>
5  *
6  * ----
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; see the file COPYING.  If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 
24 #include "digitallyimportedsettings.h"
25 #include "models/digitallyimported.h"
26 #include "support/buddylabel.h"
27 #include "support/lineedit.h"
28 #include <QPushButton>
29 #include <QComboBox>
30 
DigitallyImportedSettings(QWidget * parent)31 DigitallyImportedSettings::DigitallyImportedSettings(QWidget *parent)
32     : Dialog(parent)
33 {
34     setButtons(Ok|Cancel);
35     setCaption(tr("Digitally Imported Settings"));
36     QWidget *mainWidet = new QWidget(this);
37     setupUi(mainWidet);
38     setMainWidget(mainWidet);
39 
40     audio->addItem(tr("MP3 256k"), 0);
41     audio->addItem(tr("AAC 64k"), 1);
42     audio->addItem(tr("AAC 128k"), 2);
43 
44     connect(loginButton, SIGNAL(clicked()), this, SLOT(login()));
45     connect(DigitallyImported::self(), SIGNAL(loginStatus(bool,QString)), SLOT(loginStatus(bool,QString)));
46 
47     int h=fontMetrics().height();
48     user->setMinimumWidth(h*20);
49     adjustSize();
50     setMinimumSize(size());
51 }
52 
show()53 void DigitallyImportedSettings::show()
54 {
55     prevUser=DigitallyImported::self()->user();
56     prevPass=DigitallyImported::self()->pass();
57     wasLoggedIn=DigitallyImported::self()->loggedIn();
58     prevAudioType=DigitallyImported::self()->audioType();
59 
60     setState();
61     user->setText(prevUser);
62     pass->setText(prevPass);
63 
64     for (int i=0; i<audio->count(); ++i) {
65         if (audio->itemData(i).toInt()==DigitallyImported::self()->audioType()) {
66             audio->setCurrentIndex(i);
67             break;
68         }
69     }
70 
71     loginStatusLabel->setText(DigitallyImported::self()->statusString());
72     if (QDialog::Accepted==Dialog::exec()) {
73         QString u=user->text().trimmed();
74         QString p=pass->text().trimmed();
75         int at=audio->itemData(audio->currentIndex()).toInt();
76         bool changed=false;
77         bool needToLogin=false;
78         if (u!=DigitallyImported::self()->user()) {
79             DigitallyImported::self()->setUser(u);
80             needToLogin=changed=true;
81         }
82         if (p!=DigitallyImported::self()->pass()) {
83             DigitallyImported::self()->setPass(p);
84             needToLogin=changed=true;
85         }
86         if (DigitallyImported::self()->audioType()!=at) {
87             DigitallyImported::self()->setAudioType(at);
88             changed=true;
89         }
90         if (needToLogin) {
91             DigitallyImported::self()->login();
92         }
93         if (changed) {
94             DigitallyImported::self()->save();
95         }
96     } else {
97         DigitallyImported::self()->setUser(prevUser);
98         DigitallyImported::self()->setPass(prevPass);
99         DigitallyImported::self()->setAudioType(prevAudioType);
100         if (wasLoggedIn) {
101             DigitallyImported::self()->login();
102         }
103     }
104 }
105 
login()106 void DigitallyImportedSettings::login()
107 {
108     if (DigitallyImported::self()->loggedIn()) {
109         loginStatusLabel->setText(tr("Not Authenticated"));
110         DigitallyImported::self()->logout();
111     } else {
112         loginStatusLabel->setText(tr("Authenticating..."));
113         messageWidget->close();
114         DigitallyImported::self()->setUser(user->text().trimmed());
115         DigitallyImported::self()->setPass(pass->text().trimmed());
116         DigitallyImported::self()->login();
117     }
118 }
119 
loginStatus(bool status,const QString & msg)120 void DigitallyImportedSettings::loginStatus(bool status, const QString &msg)
121 {
122     loginStatusLabel->setText(status ? tr("Authenticated") : tr("Not Authenticated"));
123     if (status) {
124         messageWidget->close();
125     } else {
126         messageWidget->setError(msg, true);
127         adjustSize();
128     }
129     setState();
130 }
131 
setState()132 void DigitallyImportedSettings::setState()
133 {
134     if (DigitallyImported::self()->sessionExpiry().isValid()) {
135         expiry->setText(DigitallyImported::self()->sessionExpiry().toString(Qt::ISODate));
136     } else {
137         loginButton->setText(tr("Login"));
138         expiry->setText(QString());
139     }
140 
141     expiry->setVisible(DigitallyImported::self()->sessionExpiry().isValid());
142     expiryLabel->setVisible(expiry->isVisible());
143     loginButton->setText(DigitallyImported::self()->loggedIn() ? tr("Logout") : tr("Login"));
144 }
145 
146 #include "moc_digitallyimportedsettings.cpp"
147