File indexing completed on 2024-05-19 04:50:10

0001 /****************************************************************************************
0002  * Copyright (c) 2010     Ian Monroe <ian@monroe.nu>                                   *
0003  *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) version 3 or        *
0007  * any later version accepted by the membership of KDE e.V. (or its successor approved  *
0008  * by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of  *
0009  * version 3 of the license.                                                            *
0010  *                                                                                      *
0011  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0012  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0013  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0014  *                                                                                      *
0015  * You should have received a copy of the GNU General Public License along with         *
0016  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0017  ****************************************************************************************/
0018 
0019 #include "AddServerDialog.h"
0020 
0021 #include "AmpacheAccountLogin.h"
0022 #include "ui_NewServerWidget.h"
0023 
0024 #include <QDialogButtonBox>
0025 #include <QVBoxLayout>
0026 
0027 AddServerDialog::AddServerDialog()
0028     : QDialog()
0029     , m_widgets( new Ui::NewServerWidget )
0030 {
0031     QWidget* widget = new QWidget();
0032     m_widgets->setupUi(widget);
0033 
0034     setLayout(new QVBoxLayout);
0035     layout()->addWidget(widget);
0036 
0037     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0038     connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0039     connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0040     buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
0041     layout()->addWidget(buttonBox);
0042     
0043     m_widgets->verifyButton->setEnabled(false);
0044     setWindowTitle(i18n("Add new Ampache server"));
0045     
0046     connect( m_widgets->verifyButton, &QPushButton::released, this, &AddServerDialog::verifyData);
0047     QList<QLineEdit*> inputs;
0048     inputs << m_widgets->nameLineEdit << m_widgets->serverAddressLineEdit
0049            << m_widgets->userNameLineEdit << m_widgets-> passwordLineEdit;
0050     foreach(QLineEdit* line, inputs)
0051         connect( line, &QLineEdit::textEdited, this, &AddServerDialog::anyTextEdited);
0052 }
0053 
0054 AddServerDialog::~AddServerDialog()
0055 {
0056     delete m_widgets;
0057 }
0058 
0059 void
0060 AddServerDialog::anyTextEdited()
0061 {
0062    bool minimumData = (!(name().isEmpty() || url().isEmpty()
0063                                       || password().isEmpty()
0064                                       || username().isEmpty() ));
0065    findChild<QDialogButtonBox*>()->button(QDialogButtonBox::Ok)->setEnabled(minimumData);
0066    m_widgets->verifyButton->setEnabled(minimumData);
0067 }
0068 
0069 void AddServerDialog::verifyData()
0070 {
0071     m_widgets->verifyButton->setEnabled(false);
0072     delete m_login; //should always be null at this point.
0073     m_login = new AmpacheAccountLogin( url(), username(), password(), this );
0074     connect(m_login, &AmpacheAccountLogin::finished, this, &AddServerDialog::loginResult);
0075 }
0076 
0077 void AddServerDialog::loginResult()
0078 {
0079     QLabel* label = m_widgets->verifyLabel;
0080     QPalette pal = label->palette();
0081     if( m_login->authenticated() )
0082     {
0083         label->setText( i18n("Successfully connected") );
0084         pal.setColor( QPalette::WindowText, Qt::darkGreen );
0085     }
0086     else
0087     {
0088         label->setText( i18n("Connection failure") );
0089         pal.setColor( QPalette::WindowText, Qt::red );
0090     }
0091     label->setPalette(pal);
0092     delete m_login;
0093     m_widgets->verifyButton->setEnabled(true);
0094 }
0095 
0096 QString
0097 AddServerDialog::name()
0098 {
0099     return m_widgets->nameLineEdit->text();
0100 }
0101 
0102 QUrl
0103 AddServerDialog::url()
0104 {
0105     return QUrl::fromUserInput( m_widgets->serverAddressLineEdit->text() );
0106 }
0107 
0108 QString
0109 AddServerDialog::password()
0110 {
0111     return m_widgets->passwordLineEdit->text();
0112 }
0113 
0114 QString
0115 AddServerDialog::username()
0116 {
0117     return m_widgets->userNameLineEdit->text();
0118 }