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

0001 /****************************************************************************************
0002  * Copyright (c) 2007 Shane King <kde@dontletsstart.com>                                *
0003  * Copyright (c) 2010 Stefan Derkits <stefan@derkits.at>                                *
0004  * Copyright (c) 2010 Christian Wagner <christian.wagner86@gmx.at>                      *
0005  * Copyright (c) 2010 Felix Winter <ixos01@gmail.com>                                   *
0006  *                                                                                      *
0007  * This program is free software; you can redistribute it and/or modify it under        *
0008  * the terms of the GNU General Public License as published by the Free Software        *
0009  * Foundation; either version 2 of the License, or (at your option) any later           *
0010  * version.                                                                             *
0011  *                                                                                      *
0012  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0013  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0014  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0015  *                                                                                      *
0016  * You should have received a copy of the GNU General Public License along with         *
0017  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0018  ****************************************************************************************/
0019 
0020 #define DEBUG_PREFIX "GpodderServiceSettings"
0021 
0022 #include "GpodderServiceSettings.h"
0023 
0024 #include "core/podcasts/PodcastProvider.h"
0025 #include "core/support/Amarok.h"
0026 #include "core/support/Debug.h"
0027 #include "NetworkAccessManagerProxy.h"
0028 #include "playlistmanager/PlaylistManager.h"
0029 #include "ui_GpodderConfigWidget.h"
0030 
0031 #include <KMessageBox>
0032 #include <KPluginFactory>
0033 
0034 #include <QHostInfo>
0035 #include <QNetworkAccessManager>
0036 #include <QNetworkReply>
0037 #include <QRegExpValidator>
0038 
0039 
0040 K_PLUGIN_FACTORY_WITH_JSON( GpodderServiceSettingsFactory, "amarok_service_gpodder_config.json", registerPlugin<GpodderServiceSettings>(); )
0041 
0042 GpodderServiceSettings::GpodderServiceSettings( QWidget *parent, const QVariantList &args )
0043         : KCModule( parent, args )
0044         , m_enableProvider( false )
0045         , m_createDevice( nullptr )
0046 {
0047     debug() << "Creating gpodder.net config object";
0048 
0049     m_configDialog = new Ui::GpodderConfigWidget;
0050     m_configDialog->setupUi( this );
0051 
0052     connect( m_configDialog->kcfg_GpodderUsername, &QLineEdit::textChanged,
0053              this, &GpodderServiceSettings::settingsChanged );
0054     connect( m_configDialog->kcfg_GpodderPassword, &QLineEdit::textChanged,
0055              this, &GpodderServiceSettings::settingsChanged );
0056     connect( m_configDialog->testLogin, &QPushButton::clicked,
0057              this, &GpodderServiceSettings::testLogin );
0058 
0059     load();
0060 }
0061 
0062 GpodderServiceSettings::~GpodderServiceSettings()
0063 {
0064     if( m_createDevice )
0065         m_createDevice->deleteLater();
0066 
0067     if( m_devices )
0068         m_devices->deleteLater();
0069 
0070     delete m_configDialog;
0071 }
0072 
0073 void
0074 GpodderServiceSettings::save()
0075 {
0076     m_config.setUsername( m_configDialog->kcfg_GpodderUsername->text() );
0077     m_config.setPassword( m_configDialog->kcfg_GpodderPassword->text() );
0078     m_config.setEnableProvider( m_enableProvider );
0079     m_config.setIgnoreWallet( false );
0080 
0081     m_config.save();
0082     KCModule::save();
0083 }
0084 
0085 void
0086 GpodderServiceSettings::testLogin()
0087 {
0088     DEBUG_BLOCK
0089 
0090     if ( ( !m_configDialog->kcfg_GpodderUsername->text().isEmpty() ) &&
0091          ( !m_configDialog->kcfg_GpodderPassword->text().isEmpty() ) )
0092     {
0093 
0094         m_configDialog->testLogin->setEnabled( false );
0095         m_configDialog->testLogin->setText( i18n( "Testing..." ) );
0096 
0097         mygpo::ApiRequest api( m_configDialog->kcfg_GpodderUsername->text(),
0098                                m_configDialog->kcfg_GpodderPassword->text(),
0099                                The::networkAccessManager() );
0100         m_devices = api.listDevices( m_configDialog->kcfg_GpodderUsername->text() );
0101 
0102         connect( m_devices.data(), &mygpo::DeviceList::finished,
0103                  this, &GpodderServiceSettings::finished );
0104         connect( m_devices.data(), &mygpo::DeviceList::requestError,
0105                  this, &GpodderServiceSettings::onError );
0106         connect( m_devices.data(), &mygpo::DeviceList::parseError,
0107                  this, &GpodderServiceSettings::onParseError );
0108     }
0109     else
0110     {
0111         KMessageBox::error( this,
0112                             i18n( "Either the username or the password is empty, please correct and try again." ),
0113                             i18n( "Failed" ) );
0114     }
0115 }
0116 
0117 void
0118 GpodderServiceSettings::finished()
0119 {
0120     DEBUG_BLOCK
0121 
0122     debug() << "Authentication worked, got List of Devices, searching for Amarok Device";
0123 
0124     m_configDialog->testLogin->setText( i18nc( "The operation completed as expected", "Success" ) );
0125     m_configDialog->testLogin->setEnabled( false );
0126 
0127     bool deviceExists = false;
0128     QList<mygpo::DevicePtr> ptrList = m_devices->devicesList();
0129     mygpo::DevicePtr devPtr;
0130 
0131     QString hostname = QHostInfo::localHostName();
0132     QString deviceID = QLatin1String( "amarok-" ) % hostname;
0133 
0134     foreach( devPtr, ptrList )
0135     {
0136         if( devPtr->id().compare( deviceID ) == 0 )
0137         {
0138             deviceExists = true;
0139             break;
0140         }
0141     }
0142     if( !deviceExists )
0143     {
0144         debug() << "Create new device " % deviceID;
0145 
0146         mygpo::ApiRequest api( m_configDialog->kcfg_GpodderUsername->text(),
0147                                m_configDialog->kcfg_GpodderPassword->text(),
0148                                The::networkAccessManager() );
0149 
0150         m_createDevice = api.renameDevice( m_configDialog->kcfg_GpodderUsername->text(),
0151                                            deviceID,
0152                                            QLatin1String( "Amarok on " ) % hostname,
0153                                            mygpo::Device::OTHER );
0154 
0155         connect( m_createDevice, &QNetworkReply::finished,
0156                  this, &GpodderServiceSettings::deviceCreationFinished );
0157         connect( m_createDevice, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::errorOccurred),
0158                  this, &GpodderServiceSettings::deviceCreationError );
0159     }
0160     else
0161     {
0162         debug() << "Amarok device was found and everything looks perfect";
0163     }
0164 }
0165 
0166 void
0167 GpodderServiceSettings::onError( QNetworkReply::NetworkError code )
0168 {
0169     DEBUG_BLOCK
0170 
0171     debug() << code;
0172 
0173     if( code == QNetworkReply::NoError )
0174         debug() << "No Error was found, but onError was called - should not happen";
0175     else if( code == QNetworkReply::AuthenticationRequiredError )
0176     {
0177         debug() << "Authentication failed";
0178 
0179         KMessageBox::error( this,
0180             i18n( "Either the username or the password is incorrect, please correct and try again" ),
0181                             i18n( "Failed" ) );
0182 
0183         m_configDialog->testLogin->setText( i18n( "&Test Login" ) );
0184         m_configDialog->testLogin->setEnabled( true );
0185     }
0186     else
0187     {
0188         KMessageBox::error( this,
0189             i18n( "Unable to connect to gpodder.net service or other error occurred." ),
0190                             i18n( "Failed" ) );
0191 
0192         m_configDialog->testLogin->setText( i18n( "&Test Login" ) );
0193         m_configDialog->testLogin->setEnabled( true );
0194     }
0195 }
0196 
0197 void
0198 GpodderServiceSettings::onParseError()
0199 {
0200     debug() << "Couldn't parse DeviceList, should not happen if gpodder.net is working correctly";
0201 
0202     m_configDialog->testLogin->setText( i18n( "&Test Login" ) );
0203     m_configDialog->testLogin->setEnabled( true );
0204 
0205     KMessageBox::error( this, i18n( "Error parsing the Reply, check if gpodder.net is working correctly and report a bug" ), i18n( "Failed" ) );
0206 }
0207 
0208 void
0209 GpodderServiceSettings::deviceCreationFinished()
0210 {
0211     debug() << "Creation of Amarok Device finished";
0212 }
0213 
0214 void
0215 GpodderServiceSettings::deviceCreationError( QNetworkReply::NetworkError code )
0216 {
0217     debug() << "Error creating Amarok Device";
0218     debug() << code;
0219 
0220     m_configDialog->testLogin->setText( i18n( "&Test Login" ) );
0221     m_configDialog->testLogin->setEnabled( true );
0222 }
0223 
0224 void
0225 GpodderServiceSettings::load()
0226 {
0227     m_config.load();
0228 
0229     m_configDialog->kcfg_GpodderUsername->setText( m_config.username() );
0230     m_configDialog->kcfg_GpodderPassword->setText( m_config.password() );
0231     m_enableProvider = m_config.enableProvider();
0232 
0233     KCModule::load();
0234 }
0235 
0236 void
0237 GpodderServiceSettings::defaults()
0238 {
0239     m_config.reset();
0240 
0241     m_configDialog->kcfg_GpodderUsername->clear();
0242     m_configDialog->kcfg_GpodderPassword->clear();
0243     m_enableProvider = false;
0244 }
0245 
0246 void
0247 GpodderServiceSettings::settingsChanged()
0248 {
0249     m_configDialog->testLogin->setText( i18n( "&Test Login" ) );
0250     m_configDialog->testLogin->setEnabled( true );
0251 
0252     m_enableProvider = true;
0253     emit changed( true );
0254 }
0255 
0256 #include "GpodderServiceSettings.moc"