~ubuntu-branches/ubuntu/karmic/choqok/karmic

« back to all changes in this revision

Viewing changes to src/accountmanager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Steve Stalcup
  • Date: 2009-02-11 20:31:26 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090211203126-7uqucqt5r2harpds
Tags: 0.4-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
    This file is part of choqoK, the KDE Twitter client
3
 
 
4
 
    Copyright (C) 2008 Mehrdad Momeny <mehrdad.momeny@gmail.com>
5
 
 
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
 
3
 
 
4
    Copyright (C) 2008-2009 Mehrdad Momeny <mehrdad.momeny@gmail.com>
 
5
 
 
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.
 
13
 
10
14
 
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.
15
19
 
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/
19
22
 
20
23
*/
21
24
#include "accountmanager.h"
22
25
#include <kdebug.h>
23
26
#include <KConfig>
24
27
#include <KConfigGroup>
25
 
// #include <kjob.h>
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>
 
31
#include "backend.h"
31
32
 
32
 
AccountManager::AccountManager(QObject* parent):
33
 
                QObject(parent), mWallet(0)
 
33
AccountManager::AccountManager( QObject* parent ):
 
34
        QObject( parent ), mWallet( 0 )
34
35
{
35
 
        kDebug();
36
 
        mWallet = KWallet::Wallet::openWallet( "kdewallet", 0 );
 
36
    kDebug();
 
37
    mWallet = KWallet::Wallet::openWallet( "kdewallet", 0 );
37
38
    if ( mWallet ) {
38
 
        if(!mWallet->setFolder( "choqok" )){
 
39
        if ( !mWallet->setFolder( "choqok" ) ) {
39
40
            mWallet->createFolder( "choqok" );
40
41
            mWallet->setFolder( "choqok" );
41
42
        }
42
 
                kDebug() << "Wallet successfully opened.";
43
 
        }
 
43
        kDebug() << "Wallet successfully opened.";
 
44
    }
44
45
    conf = new KConfig( );
45
46
    loadAccounts();
46
47
}
47
48
 
48
 
 
49
49
AccountManager::~AccountManager()
50
50
{
 
51
    kDebug();
51
52
    mSelf = 0L;
52
53
    conf->sync();
53
54
    delete conf;
57
58
 
58
59
AccountManager * AccountManager::self()
59
60
{
60
 
    if( !mSelf )
 
61
    if ( !mSelf )
61
62
        mSelf = new AccountManager;
62
63
    return mSelf;
63
64
}
67
68
    return mAccounts;
68
69
}
69
70
 
70
 
Account AccountManager::findAccount( QString &alias )
 
71
Account AccountManager::findAccount( const QString &alias )
71
72
{
72
 
    kDebug()<<"Finding: "<<alias;
 
73
    kDebug() << "Finding: " << alias;
73
74
    int count = mAccounts.count();
74
 
    for ( int i=0; i<count; ++i )
75
 
    {
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];
79
79
        }
80
80
    }
81
81
    Account a;
82
 
    a.isError = true;
 
82
    a.setError( true );
83
83
    return a;
84
84
}
85
85
 
86
 
bool AccountManager::removeAccount(const QString &alias)
 
86
bool AccountManager::removeAccount( const QString &alias )
87
87
{
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 ) );
93
93
            conf->sync();
94
 
            mAccounts.removeAt(i);
95
 
            if(mWallet){
96
 
                if(mWallet->removeEntry(alias)==0)
97
 
                    kDebug()<<"Password successfully removed from kde wallet";
98
 
            }
99
 
//             QString tmpFile;
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 );
 
95
            if ( mWallet ) {
 
96
                if ( mWallet->removeEntry( alias ) == 0 ) {
 
97
                    kDebug() << "Password successfully removed from kde wallet";
 
98
                }
 
99
            }
 
100
            for ( int i = Backend::HomeTimeLine; i <= Backend::OutboxTimeLine; ++i ) {
 
101
                QString tmpFile;
 
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 );
 
106
                delJob->start();
 
107
            }
 
108
            emit accountRemoved( alias );
112
109
            return true;
113
110
        }
114
111
    }
115
112
    return false;
116
113
}
117
114
 
118
 
Account & AccountManager::addAccount(Account & account)
 
115
Account & AccountManager::addAccount( Account & account )
119
116
{
120
 
    kDebug()<<"Adding: "<<account.alias;
121
 
    
122
 
    if( account.alias.isEmpty() )
123
 
    {
124
 
        account.isError = true;
 
117
    kDebug() << "Adding: " << account.alias();
 
118
 
 
119
    if ( account.alias().isEmpty() ) {
 
120
        account.setError( true );
125
121
        return account;
126
122
    }
127
 
    
 
123
 
128
124
    // If this account already exists, do nothing
129
125
    QListIterator<Account> it( mAccounts );
130
 
    while ( it.hasNext() )
131
 
    {
 
126
    while ( it.hasNext() ) {
132
127
        Account curracc = it.next();
133
 
        if ( account.alias == curracc.alias )
134
 
        {
135
 
            account.isError = true;
 
128
        if ( account.alias() == curracc.alias() ) {
 
129
            account.setError( true );
136
130
            return account;
137
131
        }
138
132
    }
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";
148
144
    } else {
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";
151
147
    }
152
148
    conf->sync();
153
 
    emit accountAdded(account);
154
 
    account.isError = false;
 
149
    emit accountAdded( account );
 
150
    account.setError( false );
155
151
    return account;
156
152
}
157
153
 
158
 
Account & AccountManager::modifyAccount(Account & account, const QString & previousAlias)
 
154
Account & AccountManager::modifyAccount( Account & account, const QString & previousAlias )
159
155
{
160
 
    kDebug()<<"Modifing: "<<previousAlias;
161
 
    
162
 
    if(removeAccount(previousAlias))
163
 
        return addAccount(account);
164
 
    
165
 
    account.isError = true;
 
156
    kDebug() << "Modifying: " << previousAlias;
 
157
 
 
158
    if ( removeAccount( previousAlias ) )
 
159
        return addAccount( account );
 
160
 
 
161
    account.setError( true );
166
162
    return account;
167
163
}
168
164
 
171
167
    kDebug();
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" ) ) {
176
172
            Account a;
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);
 
182
                } else {
 
183
                    a.setServiceType(Account::Twitter);
 
184
                }
 
185
            } else {
 
186
                a.setServiceType( (Account::Service) service_type );
 
187
            }
 
188
            a.setDirection(( accountGrp.readEntry( "direction", "ltr" ) == "rtl" ) ? Qt::RightToLeft : Qt::LeftToRight );
183
189
            QString buffer;
184
 
            if(mWallet && mWallet->readPassword( a.serviceName+'_'+a.username, buffer )==0 && !buffer.isEmpty()){
185
 
                a.password = buffer;
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.";
187
193
            } else {
188
 
                a.password = accountGrp.readEntry("password", QString());
189
 
                kDebug()<<"Password loaded from config file.";
190
 
            }
191
 
            a.isError = false;
192
 
            mAccounts.append(a);
 
194
                a.setPassword( accountGrp.readEntry( "password", QString() ) );
 
195
                kDebug() << "Password loaded from config file.";
 
196
            }
 
197
            a.setError( false );
 
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();
 
203
            }
 
204
            mAccounts.append( a );
193
205
        }
194
206
    }
195
 
    kDebug()<<mAccounts.count()<<" accounts loaded.";
 
207
    kDebug() << mAccounts.count() << " accounts loaded.";
 
208
}
 
209
 
 
210
void AccountManager::userVerified( Account * userAccount )
 
211
{
 
212
    this->modifyAccount( *userAccount, userAccount->alias() );
 
213
}
 
214
 
 
215
QStringList AccountManager::listFriends( const QString & alias )
 
216
{
 
217
    KConfigGroup accountGrp( conf, "Account" + alias );
 
218
    QStringList list = accountGrp.readEntry( "friends", QStringList() );
 
219
    return list;
 
220
}
 
221
 
 
222
void AccountManager::saveFriendsList( const QString & alias, const QStringList & list )
 
223
{
 
224
//     if(findAccount( alias ).isError())
 
225
//         return;
 
226
    KConfigGroup accountGrp( conf, "Account" + alias );
 
227
    accountGrp.writeEntry( "friends", list );
 
228
    accountGrp.sync();
 
229
}
 
230
 
 
231
QString AccountManager::generateStatusBackupFileName( const QString &alias, Backend::TimeLineType type )
 
232
{
 
233
    QString name = alias;
 
234
    name += '_';
 
235
 
 
236
    switch ( type ) {
 
237
    case Backend::HomeTimeLine:
 
238
        name += "home";
 
239
        break;
 
240
    case Backend::ReplyTimeLine:
 
241
        name += "reply";
 
242
        break;
 
243
    case Backend::InboxTimeLine:
 
244
        name += "inbox";
 
245
        break;
 
246
    case Backend::OutboxTimeLine:
 
247
        name += "outbox";
 
248
        break;
 
249
    default:
 
250
        name += QString::number( type );
 
251
        break;
 
252
    };
 
253
    name += "statuslistrc";
 
254
    return name;
196
255
}
197
256
 
198
257
#include "accountmanager.moc"