2
This file is part of choqoK, the KDE Twitter client
4
Copyright (C) 2008 Mehrdad Momeny <mehrdad.momeny@gmail.com>
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 3 of the License, or
9
(at your option) any later version.
2
This file is part of choqoK, the KDE mono-blogging client
4
Copyright (C) 2008-2009 Mehrdad Momeny <mehrdad.momeny@gmail.com>
6
This program is free software; you can redistribute it and/or
7
modify it under the terms of the GNU General Public License as
8
published by the Free Software Foundation; either version 2 of
9
the License or (at your option) version 3 or any later version
10
accepted by the membership of KDE e.V. (or its successor approved
11
by the membership of KDE e.V.), which shall act as a proxy
12
defined in Section 14 of version 3 of the license.
11
15
This program is distributed in the hope that it will be useful,
12
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
18
GNU General Public License for more details.
16
20
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
along with this program; if not, see http://www.gnu.org/licenses/
21
24
#include "accountmanager.h"
22
25
#include <kdebug.h>
24
27
#include <KConfigGroup>
26
// #include <kio/job.h>
27
// #include <kio/deletejob.h>
28
// #include <kio/netaccess.h>
28
#include <kio/deletejob.h>
29
29
#include <kwallet.h>
30
30
#include <kstandarddirs.h>
32
AccountManager::AccountManager(QObject* parent):
33
QObject(parent), mWallet(0)
33
AccountManager::AccountManager( QObject* parent ):
34
QObject( parent ), mWallet( 0 )
36
mWallet = KWallet::Wallet::openWallet( "kdewallet", 0 );
37
mWallet = KWallet::Wallet::openWallet( "kdewallet", 0 );
38
if(!mWallet->setFolder( "choqok" )){
39
if ( !mWallet->setFolder( "choqok" ) ) {
39
40
mWallet->createFolder( "choqok" );
40
41
mWallet->setFolder( "choqok" );
42
kDebug() << "Wallet successfully opened.";
43
kDebug() << "Wallet successfully opened.";
44
45
conf = new KConfig( );
49
49
AccountManager::~AccountManager()
70
Account AccountManager::findAccount( QString &alias )
71
Account AccountManager::findAccount( const QString &alias )
72
kDebug()<<"Finding: "<<alias;
73
kDebug() << "Finding: " << alias;
73
74
int count = mAccounts.count();
74
for ( int i=0; i<count; ++i )
76
if ( mAccounts[i].alias == alias ){
77
mAccounts[i].isError = false;
75
for ( int i = 0; i < count; ++i ) {
76
if ( mAccounts[i].alias() == alias ) {
77
mAccounts[i].setError( false );
78
78
return mAccounts[i];
86
bool AccountManager::removeAccount(const QString &alias)
86
bool AccountManager::removeAccount( const QString &alias )
88
kDebug()<<"Removing "<<alias;
88
kDebug() << "Removing " << alias;
89
89
int count = mAccounts.count();
90
for(int i=0; i<count; ++i){
91
if(mAccounts[i].alias == alias){
92
conf->deleteGroup( QString::fromLatin1("Account%1").arg(alias) );
90
for ( int i = 0; i < count; ++i ) {
91
if ( mAccounts[i].alias() == alias ) {
92
conf->deleteGroup( QString::fromLatin1( "Account%1" ).arg( alias ) );
94
mAccounts.removeAt(i);
96
if(mWallet->removeEntry(alias)==0)
97
kDebug()<<"Password successfully removed from kde wallet";
100
// tmpFile = KStandardDirs::locate("data", QString::fromLatin1("%1_homestatuslistrc").arg(alias));
101
// kDebug()<<"Will remove "<<tmpFile;
102
// const KUrl homePath(tmpFile);
103
// DeleteJob * delJob = KIO::del(homePath, KIO::HideProgressInfo);
104
// KIO::NetAccess::synchronousRun(delJob, 0);
105
// tmpFile = KStandardDirs::locate("data", QString::fromLatin1("%1_replystatuslistrc").arg(alias));
106
// kDebug()<<"Will remove "<<tmpFile;
107
// const KUrl replyPath(tmpFile);
108
// delJob = KIO::del(replyPath, KIO::HideProgressInfo);
109
// KIO::NetAccess::synchronousRun(delJob, 0);
110
///TODO Remove statuslist rc files.
111
emit accountRemoved(alias);
94
mAccounts.removeAt( i );
96
if ( mWallet->removeEntry( alias ) == 0 ) {
97
kDebug() << "Password successfully removed from kde wallet";
100
for ( int i = Backend::HomeTimeLine; i <= Backend::OutboxTimeLine; ++i ) {
102
tmpFile = KStandardDirs::locate( "data", "choqok/" + generateStatusBackupFileName( alias, ( Backend::TimeLineType )i ) );
103
kDebug() << "Will remove " << tmpFile;
104
const KUrl path( tmpFile );
105
KIO::DeleteJob * delJob = KIO::del( path, KIO::HideProgressInfo );
108
emit accountRemoved( alias );
118
Account & AccountManager::addAccount(Account & account)
115
Account & AccountManager::addAccount( Account & account )
120
kDebug()<<"Adding: "<<account.alias;
122
if( account.alias.isEmpty() )
124
account.isError = true;
117
kDebug() << "Adding: " << account.alias();
119
if ( account.alias().isEmpty() ) {
120
account.setError( true );
128
124
// If this account already exists, do nothing
129
125
QListIterator<Account> it( mAccounts );
130
while ( it.hasNext() )
126
while ( it.hasNext() ) {
132
127
Account curracc = it.next();
133
if ( account.alias == curracc.alias )
135
account.isError = true;
128
if ( account.alias() == curracc.alias() ) {
129
account.setError( true );
139
133
mAccounts.append( account );
140
KConfigGroup acConf ( conf, QString::fromLatin1("Account%1").arg(account.alias) );
141
acConf.writeEntry ( "alias", account.alias );
142
acConf.writeEntry ( "username", account.username );
143
acConf.writeEntry ( "service", account.serviceName );
144
acConf.writeEntry ( "api_path", account.apiPath );
145
acConf.writeEntry ( "direction", ( account.direction == Qt::RightToLeft ) ? "rtl" : "ltr" );
146
if(mWallet && mWallet->writePassword(account.serviceName+'_'+account.username, account.password)==0){
147
kDebug()<<"Password stored to kde wallet";
134
KConfigGroup acConf( conf, QString::fromLatin1( "Account%1" ).arg( account.alias() ) );
135
acConf.writeEntry( "alias", account.alias() );
136
acConf.writeEntry( "username", account.username() );
137
acConf.writeEntry( "userId", account.userId() );
138
acConf.writeEntry( "service_type", (int)account.serviceType() );
139
// acConf.writeEntry( "service", account.serviceName() );
140
// acConf.writeEntry( "api_path", account.apiPath() );
141
acConf.writeEntry( "direction", ( account.direction() == Qt::RightToLeft ) ? "rtl" : "ltr" );
142
if ( mWallet && mWallet->writePassword( account.serviceName() + '_' + account.username(), account.password() ) == 0 ) {
143
kDebug() << "Password stored to kde wallet";
149
acConf.writeEntry ( "password", account.password );
150
kDebug()<<"Password stored to config file";
145
acConf.writeEntry( "password", account.password() );
146
kDebug() << "Password stored to config file";
153
emit accountAdded(account);
154
account.isError = false;
149
emit accountAdded( account );
150
account.setError( false );
158
Account & AccountManager::modifyAccount(Account & account, const QString & previousAlias)
154
Account & AccountManager::modifyAccount( Account & account, const QString & previousAlias )
160
kDebug()<<"Modifing: "<<previousAlias;
162
if(removeAccount(previousAlias))
163
return addAccount(account);
165
account.isError = true;
156
kDebug() << "Modifying: " << previousAlias;
158
if ( removeAccount( previousAlias ) )
159
return addAccount( account );
161
account.setError( true );
172
168
QStringList list = conf->groupList();
173
169
int count = list.count();
174
for(int i=0; i<count; ++i){
175
if(list[i].contains("Account")){
170
for ( int i = 0; i < count; ++i ) {
171
if ( list[i].contains( "Account" ) ) {
177
KConfigGroup accountGrp(conf, list[i]);
178
a.username = accountGrp.readEntry("username", QString());
179
a.alias = accountGrp.readEntry("alias", QString());
180
a.serviceName = accountGrp.readEntry("service", QString());
181
a.apiPath = accountGrp.readEntry("api_path", QString());
182
a.direction = (accountGrp.readEntry("direction", "ltr") == "rtl") ? Qt::RightToLeft : Qt::LeftToRight;
173
KConfigGroup accountGrp( conf, list[i] );
174
a.setUsername( accountGrp.readEntry( "username", QString() ) );
175
a.setUserId( accountGrp.readEntry( "userId", uint( -1 ) ) );
176
a.setAlias( accountGrp.readEntry( "alias", QString() ) );
177
int service_type = accountGrp.readEntry( "service_type", -1 );
178
if(service_type == -1){///For compatibility with previous versions (e.g. 0.3.1 )
179
QString service = accountGrp.readEntry( "service", QString() );
180
if( service.toLower() == QString(IDENTICA_SERVICE_TEXT).toLower() ) {
181
a.setServiceType(Account::Identica);
183
a.setServiceType(Account::Twitter);
186
a.setServiceType( (Account::Service) service_type );
188
a.setDirection(( accountGrp.readEntry( "direction", "ltr" ) == "rtl" ) ? Qt::RightToLeft : Qt::LeftToRight );
184
if(mWallet && mWallet->readPassword( a.serviceName+'_'+a.username, buffer )==0 && !buffer.isEmpty()){
186
kDebug()<<"Password loaded from kde wallet.";
190
if ( mWallet && mWallet->readPassword( a.serviceName() + '_' + a.username(), buffer ) == 0 && !buffer.isEmpty() ) {
191
a.setPassword( buffer );
192
kDebug() << "Password loaded from kde wallet.";
188
a.password = accountGrp.readEntry("password", QString());
189
kDebug()<<"Password loaded from config file.";
194
a.setPassword( accountGrp.readEntry( "password", QString() ) );
195
kDebug() << "Password loaded from config file.";
198
if ( a.userId() == ( uint ) - 1 ) {///Just for compatibility with previous versions
199
Account *account = new Account( a );
200
Backend *b = new Backend( account );
201
connect( b, SIGNAL( userVerified( Account* ) ), this, SLOT( userVerified( Account* ) ) );
202
b->verifyCredential();
204
mAccounts.append( a );
195
kDebug()<<mAccounts.count()<<" accounts loaded.";
207
kDebug() << mAccounts.count() << " accounts loaded.";
210
void AccountManager::userVerified( Account * userAccount )
212
this->modifyAccount( *userAccount, userAccount->alias() );
215
QStringList AccountManager::listFriends( const QString & alias )
217
KConfigGroup accountGrp( conf, "Account" + alias );
218
QStringList list = accountGrp.readEntry( "friends", QStringList() );
222
void AccountManager::saveFriendsList( const QString & alias, const QStringList & list )
224
// if(findAccount( alias ).isError())
226
KConfigGroup accountGrp( conf, "Account" + alias );
227
accountGrp.writeEntry( "friends", list );
231
QString AccountManager::generateStatusBackupFileName( const QString &alias, Backend::TimeLineType type )
233
QString name = alias;
237
case Backend::HomeTimeLine:
240
case Backend::ReplyTimeLine:
243
case Backend::InboxTimeLine:
246
case Backend::OutboxTimeLine:
250
name += QString::number( type );
253
name += "statuslistrc";
198
257
#include "accountmanager.moc"