1 /****************************************************************************************
2  * Copyright (c) 2007 Shane King <kde@dontletsstart.com>                                *
3  * Copyright (c) 2009 Leo Franchi <lfranchi@kde.org>                                    *
4  * Copyright (c) 2010 Stefan Derkits <stefan@derkits.at>                                *
5  * Copyright (c) 2010 Christian Wagner <christian.wagner86@gmx.at>                      *
6  * Copyright (c) 2010 Felix Winter <ixos01@gmail.com>                                   *
7  *                                                                                      *
8  * This program is free software; you can redistribute it and/or modify it under        *
9  * the terms of the GNU General Public License as published by the Free Software        *
10  * Foundation; either version 2 of the License, or (at your option) any later           *
11  * version.                                                                             *
12  *                                                                                      *
13  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
14  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
15  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
16  *                                                                                      *
17  * You should have received a copy of the GNU General Public License along with         *
18  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
19  ****************************************************************************************/
20 
21 #define DEBUG_PREFIX "GPodderConfig"
22 
23 #include "GpodderServiceConfig.h"
24 
25 #include "App.h"
26 #include "core/support/Amarok.h"
27 #include "core/support/Debug.h"
28 
29 #include <QLabel>
30 #include <QMessageBox>
31 
32 #include <KConfigGroup>
33 #include <KLocalizedString>
34 #include <KWallet>
35 
36 
GpodderServiceConfig()37 GpodderServiceConfig::GpodderServiceConfig()
38     : m_enableProvider( false )
39     , m_ignoreWallet( false )
40     , m_isDataLoaded( false )
41     , m_askDiag( nullptr )
42     , m_wallet( nullptr )
43 {
44     DEBUG_BLOCK
45 
46     load();
47 }
48 
~GpodderServiceConfig()49 GpodderServiceConfig::~GpodderServiceConfig()
50 {
51     DEBUG_BLOCK
52 
53     if( m_askDiag )
54         m_askDiag->deleteLater();
55 
56     if( m_wallet )
57         m_wallet->deleteLater();
58 }
59 
60 void
load()61 GpodderServiceConfig::load()
62 {
63     DEBUG_BLOCK
64     debug() << "Load config";
65 
66     KConfigGroup config = Amarok::config( configSectionName() );
67 
68     m_enableProvider = config.readEntry( "enableProvider", false );
69     m_ignoreWallet = config.readEntry( "ignoreWallet", false );
70 
71     //We only want to load the wallet if the user has enabled features that require a user/pass
72     tryToOpenWallet();
73 
74     if( m_wallet )
75     {
76         if( !m_wallet->hasFolder( "Amarok" ) )
77             m_wallet->createFolder( "Amarok" );
78 
79         // do a one-time transfer
80         // can remove at some point in the future, post-2.2
81         m_wallet->setFolder( "Amarok" );
82 
83         if( m_wallet->readPassword( "gpodder_password", m_password ) != 0 )
84             debug() << "Failed to read gpodder.net password from kwallet!";
85         else
86         {
87             QByteArray rawUsername;
88 
89             if( m_wallet->readEntry( "gpodder_username", rawUsername ) != 0 )
90                 debug() << "Failed to read gpodder.net username from kwallet.. :(";
91             else
92                 m_username = QString::fromUtf8( rawUsername );
93         }
94     }
95     else if( m_ignoreWallet )
96     {
97         m_username = config.readEntry( "username", QString() );
98         m_password = config.readEntry( "password", QString() );
99     }
100     else
101         debug() << "Failed to load the data.";
102 
103     m_isDataLoaded = !( m_username.isEmpty() || m_password.isEmpty() );
104 }
105 
106 void
save()107 GpodderServiceConfig::save()
108 {
109     DEBUG_BLOCK
110 
111     debug() << "Save config";
112 
113     KConfigGroup config = Amarok::config( configSectionName() );
114 
115     config.writeEntry( "enableProvider", m_enableProvider );
116     config.writeEntry( "ignoreWallet", m_ignoreWallet );
117 
118     //Whenever this function is called, we'll assume the user wants to
119     //change something, so blow away the subscription timestamp key
120     config.writeEntry( "subscriptionTimestamp", 0 );
121 
122     //Maybe the wallet had already closed or m_enableProvider and m_ignoreWallet
123     //could had changed also. So we try to reopen the wallet if it's not open.
124     tryToOpenWallet();
125 
126     if( m_wallet )
127     {
128         m_wallet->setFolder( "Amarok" );
129 
130         if( m_wallet->writeEntry( "gpodder_username", m_username.toUtf8() ) != 0 )
131             debug() << "Failed to save gpodder.net username to kwallet!";
132 
133         if( m_wallet->writePassword( "gpodder_password", m_password ) != 0 )
134             debug() << "Failed to save gpodder.net pw to kwallet!";
135     }
136     else
137     {
138         if( m_enableProvider )
139         {
140             debug() << "Couldn't access the wallet to save the gpodder.net credentials";
141             askAboutMissingKWallet();
142         }
143         else
144             debug() << "There isn't valid credentials to be saved";
145     }
146 
147     config.sync();
148 }
149 
150 void
askAboutMissingKWallet()151 GpodderServiceConfig::askAboutMissingKWallet()
152 {
153     if ( !m_askDiag )
154     {
155         m_askDiag = new QMessageBox( nullptr );
156 
157         m_askDiag->setWindowTitle( i18n( "gpodder.net credentials" ) );
158         m_askDiag->setText( i18n( "No running KWallet found. Would you like Amarok to save your gpodder.net credentials in plaintext?" ) );
159         m_askDiag->setStandardButtons( QMessageBox::Yes | QMessageBox::No );
160         m_askDiag->setModal( true );
161 
162         connect( m_askDiag, &QMessageBox::accepted, this, &GpodderServiceConfig::textDialogYes );
163         connect( m_askDiag, &QMessageBox::rejected, this, &GpodderServiceConfig::textDialogNo );
164     }
165 
166     m_askDiag->exec();
167 }
168 
tryToOpenWallet()169 void GpodderServiceConfig::tryToOpenWallet()
170 {
171     DEBUG_BLOCK
172 
173     //We only want to load the wallet if the user has enabled features
174     //that require a user/pass
175     if( ( m_enableProvider ) && ( !m_ignoreWallet ) )
176     {
177         debug() << "Opening wallet";
178 
179         //Open wallet unless explicitly told not to
180         m_wallet = KWallet::Wallet::openWallet(
181                        KWallet::Wallet::NetworkWallet(),
182                        0 );
183     }
184     else
185     {
186         debug() << "The wallet was ignored or is not needed.";
187         m_wallet = nullptr;
188     }
189 }
190 
191 void
reset()192 GpodderServiceConfig::reset()
193 {
194     debug() << "Reset config";
195 
196     m_username = "";
197     m_password = "";
198     m_enableProvider = false;
199     m_ignoreWallet = false;
200 }
201 
202 void
textDialogYes()203 GpodderServiceConfig::textDialogYes() //SLOT
204 {
205     DEBUG_BLOCK
206 
207     if ( !m_ignoreWallet )
208     {
209         KConfigGroup config = Amarok::config( configSectionName() );
210 
211         m_ignoreWallet = true;
212         config.writeEntry( "ignoreWallet", m_ignoreWallet );
213         config.writeEntry( "username", m_username );
214         config.writeEntry( "password", m_password );
215 
216         config.sync();
217     }
218 }
219 
220 void
textDialogNo()221 GpodderServiceConfig::textDialogNo() //SLOT
222 {
223     DEBUG_BLOCK
224 
225     if ( m_ignoreWallet )
226     {
227         KConfigGroup config = Amarok::config( configSectionName() );
228 
229         m_ignoreWallet = false;
230         config.writeEntry( "ignoreWallet", m_ignoreWallet );
231         config.writeEntry( "username", QString() );
232         config.writeEntry( "password", QString() );
233 
234         config.sync();
235     }
236 }
237 
238