File indexing completed on 2024-05-19 04:50:12
0001 /**************************************************************************************** 0002 * Copyright (c) 2007 Shane King <kde@dontletsstart.com> * 0003 * Copyright (c) 2009 Leo Franchi <lfranchi@kde.org> * 0004 * Copyright (c) 2010 Stefan Derkits <stefan@derkits.at> * 0005 * Copyright (c) 2010 Christian Wagner <christian.wagner86@gmx.at> * 0006 * Copyright (c) 2010 Felix Winter <ixos01@gmail.com> * 0007 * * 0008 * This program is free software; you can redistribute it and/or modify it under * 0009 * the terms of the GNU General Public License as published by the Free Software * 0010 * Foundation; either version 2 of the License, or (at your option) any later * 0011 * version. * 0012 * * 0013 * This program is distributed in the hope that it will be useful, but WITHOUT ANY * 0014 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * 0015 * PARTICULAR PURPOSE. See the GNU General Public License for more details. * 0016 * * 0017 * You should have received a copy of the GNU General Public License along with * 0018 * this program. If not, see <http://www.gnu.org/licenses/>. * 0019 ****************************************************************************************/ 0020 0021 #define DEBUG_PREFIX "GPodderConfig" 0022 0023 #include "GpodderServiceConfig.h" 0024 0025 #include "App.h" 0026 #include "core/support/Amarok.h" 0027 #include "core/support/Debug.h" 0028 0029 #include <QLabel> 0030 #include <QMessageBox> 0031 0032 #include <KConfigGroup> 0033 #include <KLocalizedString> 0034 #include <KWallet> 0035 0036 0037 GpodderServiceConfig::GpodderServiceConfig() 0038 : m_enableProvider( false ) 0039 , m_ignoreWallet( false ) 0040 , m_isDataLoaded( false ) 0041 , m_askDiag( nullptr ) 0042 , m_wallet( nullptr ) 0043 { 0044 DEBUG_BLOCK 0045 0046 load(); 0047 } 0048 0049 GpodderServiceConfig::~GpodderServiceConfig() 0050 { 0051 DEBUG_BLOCK 0052 0053 if( m_askDiag ) 0054 m_askDiag->deleteLater(); 0055 0056 if( m_wallet ) 0057 m_wallet->deleteLater(); 0058 } 0059 0060 void 0061 GpodderServiceConfig::load() 0062 { 0063 DEBUG_BLOCK 0064 debug() << "Load config"; 0065 0066 KConfigGroup config = Amarok::config( configSectionName() ); 0067 0068 m_enableProvider = config.readEntry( "enableProvider", false ); 0069 m_ignoreWallet = config.readEntry( "ignoreWallet", false ); 0070 0071 //We only want to load the wallet if the user has enabled features that require a user/pass 0072 tryToOpenWallet(); 0073 0074 if( m_wallet ) 0075 { 0076 if( !m_wallet->hasFolder( "Amarok" ) ) 0077 m_wallet->createFolder( "Amarok" ); 0078 0079 // do a one-time transfer 0080 // can remove at some point in the future, post-2.2 0081 m_wallet->setFolder( "Amarok" ); 0082 0083 if( m_wallet->readPassword( "gpodder_password", m_password ) != 0 ) 0084 debug() << "Failed to read gpodder.net password from kwallet!"; 0085 else 0086 { 0087 QByteArray rawUsername; 0088 0089 if( m_wallet->readEntry( "gpodder_username", rawUsername ) != 0 ) 0090 debug() << "Failed to read gpodder.net username from kwallet.. :("; 0091 else 0092 m_username = QString::fromUtf8( rawUsername ); 0093 } 0094 } 0095 else if( m_ignoreWallet ) 0096 { 0097 m_username = config.readEntry( "username", QString() ); 0098 m_password = config.readEntry( "password", QString() ); 0099 } 0100 else 0101 debug() << "Failed to load the data."; 0102 0103 m_isDataLoaded = !( m_username.isEmpty() || m_password.isEmpty() ); 0104 } 0105 0106 void 0107 GpodderServiceConfig::save() 0108 { 0109 DEBUG_BLOCK 0110 0111 debug() << "Save config"; 0112 0113 KConfigGroup config = Amarok::config( configSectionName() ); 0114 0115 config.writeEntry( "enableProvider", m_enableProvider ); 0116 config.writeEntry( "ignoreWallet", m_ignoreWallet ); 0117 0118 //Whenever this function is called, we'll assume the user wants to 0119 //change something, so blow away the subscription timestamp key 0120 config.writeEntry( "subscriptionTimestamp", 0 ); 0121 0122 //Maybe the wallet had already closed or m_enableProvider and m_ignoreWallet 0123 //could had changed also. So we try to reopen the wallet if it's not open. 0124 tryToOpenWallet(); 0125 0126 if( m_wallet ) 0127 { 0128 m_wallet->setFolder( "Amarok" ); 0129 0130 if( m_wallet->writeEntry( "gpodder_username", m_username.toUtf8() ) != 0 ) 0131 debug() << "Failed to save gpodder.net username to kwallet!"; 0132 0133 if( m_wallet->writePassword( "gpodder_password", m_password ) != 0 ) 0134 debug() << "Failed to save gpodder.net pw to kwallet!"; 0135 } 0136 else 0137 { 0138 if( m_enableProvider ) 0139 { 0140 debug() << "Couldn't access the wallet to save the gpodder.net credentials"; 0141 askAboutMissingKWallet(); 0142 } 0143 else 0144 debug() << "There isn't valid credentials to be saved"; 0145 } 0146 0147 config.sync(); 0148 } 0149 0150 void 0151 GpodderServiceConfig::askAboutMissingKWallet() 0152 { 0153 if ( !m_askDiag ) 0154 { 0155 m_askDiag = new QMessageBox( nullptr ); 0156 0157 m_askDiag->setWindowTitle( i18n( "gpodder.net credentials" ) ); 0158 m_askDiag->setText( i18n( "No running KWallet found. Would you like Amarok to save your gpodder.net credentials in plaintext?" ) ); 0159 m_askDiag->setStandardButtons( QMessageBox::Yes | QMessageBox::No ); 0160 m_askDiag->setModal( true ); 0161 0162 connect( m_askDiag, &QMessageBox::accepted, this, &GpodderServiceConfig::textDialogYes ); 0163 connect( m_askDiag, &QMessageBox::rejected, this, &GpodderServiceConfig::textDialogNo ); 0164 } 0165 0166 m_askDiag->exec(); 0167 } 0168 0169 void GpodderServiceConfig::tryToOpenWallet() 0170 { 0171 DEBUG_BLOCK 0172 0173 //We only want to load the wallet if the user has enabled features 0174 //that require a user/pass 0175 if( ( m_enableProvider ) && ( !m_ignoreWallet ) ) 0176 { 0177 debug() << "Opening wallet"; 0178 0179 //Open wallet unless explicitly told not to 0180 m_wallet = KWallet::Wallet::openWallet( 0181 KWallet::Wallet::NetworkWallet(), 0182 0 ); 0183 } 0184 else 0185 { 0186 debug() << "The wallet was ignored or is not needed."; 0187 m_wallet = nullptr; 0188 } 0189 } 0190 0191 void 0192 GpodderServiceConfig::reset() 0193 { 0194 debug() << "Reset config"; 0195 0196 m_username = ""; 0197 m_password = ""; 0198 m_enableProvider = false; 0199 m_ignoreWallet = false; 0200 } 0201 0202 void 0203 GpodderServiceConfig::textDialogYes() //SLOT 0204 { 0205 DEBUG_BLOCK 0206 0207 if ( !m_ignoreWallet ) 0208 { 0209 KConfigGroup config = Amarok::config( configSectionName() ); 0210 0211 m_ignoreWallet = true; 0212 config.writeEntry( "ignoreWallet", m_ignoreWallet ); 0213 config.writeEntry( "username", m_username ); 0214 config.writeEntry( "password", m_password ); 0215 0216 config.sync(); 0217 } 0218 } 0219 0220 void 0221 GpodderServiceConfig::textDialogNo() //SLOT 0222 { 0223 DEBUG_BLOCK 0224 0225 if ( m_ignoreWallet ) 0226 { 0227 KConfigGroup config = Amarok::config( configSectionName() ); 0228 0229 m_ignoreWallet = false; 0230 config.writeEntry( "ignoreWallet", m_ignoreWallet ); 0231 config.writeEntry( "username", QString() ); 0232 config.writeEntry( "password", QString() ); 0233 0234 config.sync(); 0235 } 0236 } 0237