2
This file is part of Choqok, the KDE micro-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.
15
This program is distributed in the hope that it will be useful,
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
GNU General Public License for more details.
20
You should have received a copy of the GNU General Public License
21
along with this program; if not, see http://www.gnu.org/licenses/
24
#include "accountswizard.h"
25
#include "accountmanager.h"
27
#include <QProgressBar>
28
#include <KMessageBox>
32
AccountsWizard::AccountsWizard( QString alias, QWidget *parent )
33
: KDialog( parent ), progress(0), verifyTimer(0)
36
QWidget *dialog = new QWidget( this );
38
dialog->setAttribute( Qt::WA_DeleteOnClose );
39
this->setMainWidget( dialog );
40
slotServiceChanged( ui.kcfg_service->currentIndex() );
41
connect( ui.kcfg_service, SIGNAL( currentIndexChanged(int) ), this, SLOT( slotServiceChanged(int) ) );
43
if ( alias.isEmpty() ) {
44
this->setCaption( i18n( "Add a new account" ) );
47
this->setCaption( i18n( "Modify existing account" ) );
55
void AccountsWizard::loadAccount( QString &alias )
57
kDebug() << "Loading account " << alias;
58
mAccount = AccountManager::self()->findAccount( alias );
59
if ( mAccount.isError() ) {
60
kError() << "Error on Loading Account with alias " << alias;
63
ui.kcfg_username->setText( mAccount.username() );
64
ui.kcfg_alias->setText( mAccount.alias() );
65
ui.kcfg_password->setText( mAccount.password() );
66
ui.kcfg_direction->setCurrentIndex(( mAccount.direction() == Qt::RightToLeft ) ? 1 : 0 );
67
ui.kcfg_service->setCurrentIndex( mAccount.serviceType() );
68
ui.kcfg_homepage->setText( mAccount.homepage() );
71
void AccountsWizard::slotButtonClicked( int button )
74
if ( button == KDialog::Ok ) {
75
QString home = ui.kcfg_homepage->text();
76
if( home.at( home.count() - 1 ) != '/' )
77
ui.kcfg_homepage->setText( home.append('/') );
78
ui.kcfg_username->setText(ui.kcfg_username->text().toLower());
82
progress->deleteLater();
85
progress = new QProgressBar( this );
86
progress->setMinimum( 0 );
87
progress->setMaximum( 0 );
88
QGridLayout* grid = qobject_cast<QGridLayout*>( this->mainWidget()->layout() );
91
grid->addWidget( progress, grid->rowCount(), 0, grid->rowCount(), 2 );
93
mAccount.setServiceType( (Account::Service) ui.kcfg_service->currentIndex(),
94
ui.kcfg_homepage->text() );
95
mAccount.setUsername( ui.kcfg_username->text() );
96
mAccount.setPassword( ui.kcfg_password->text() );
97
mAccount.setDirection(( Qt::LayoutDirection )ui.kcfg_direction->currentIndex() );
98
mAccount.setAlias( ui.kcfg_alias->text() );
100
Backend *b = new Backend( &mAccount, this );
101
connect( b, SIGNAL( userVerified( Account* ) ), this, SLOT( slotUserVerified( Account* ) ) );
102
connect( b, SIGNAL( sigError( const QString& ) ), this, SLOT( slotError( const QString& ) ) );
103
b->verifyCredential();
105
verifyTimer->deleteLater();
108
verifyTimer = new QTimer( this );
109
verifyTimer->setSingleShot( true );
110
connect ( verifyTimer, SIGNAL(timeout()), this, SLOT( handleVerifyTimeout() ) );
111
verifyTimer->start( 45000 );
112
this->setCursor(Qt::BusyCursor);
114
KDialog::slotButtonClicked( button );
117
void AccountsWizard::slotUserVerified( Account * userAccount )
121
sender()->deleteLater();
123
progress->deleteLater();
128
if ( !userAccount ) {
129
kError() << "userAccount is NULL";
132
mAccount = *userAccount;
134
mAccount = AccountManager::self()->modifyAccount( mAccount, mAlias );
136
mAccount = AccountManager::self()->addAccount( mAccount );
138
if ( mAccount.isError() ) {
139
kError() << "Cannot add or modify account with alias " << mAccount.alias();
140
KMessageBox::detailedError(this, i18n( "An error occurred when adding this account." ),
141
AccountManager::self()->lastError());
146
emit accountEdited( mAccount );
148
emit accountAdded( mAccount );
153
void AccountsWizard::slotError( const QString & errMsg )
157
progress->deleteLater();
162
KMessageBox::detailedError( this, i18n( "Authentication failed, please check your credentials." ), errMsg );
165
void AccountsWizard::handleVerifyTimeout()
169
progress->deleteLater();
174
KMessageBox::sorry(this, i18n( "Verification progress timed out. "
175
"Check your Internet connection and credentials then try again." ) , i18n( "Timeout" ) );
179
void AccountsWizard::slotServiceChanged( int index )
182
ui.lblHomepage->setVisible( true );
183
ui.kcfg_homepage->setVisible( true );
185
ui.lblHomepage->setVisible( false );
186
ui.kcfg_homepage->setVisible( false );
190
#include "accountswizard.moc"