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

« back to all changes in this revision

Viewing changes to choqok/accountswizard.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi, Alessandro Ghersi, Christian Mangold
  • Date: 2009-08-11 22:29:59 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20090811222959-7tnr98ptcnyx3pjg
Tags: 0.6.6-0ubuntu1
[Alessandro Ghersi]
* New upstream release
  - Bump Standards-Version to 3.8.2
  - Long description formatted to fit in 80 characters

[Christian Mangold]
* Improve long description
* Update Maintainer, package is in main now
* Add a watch file

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    This file is part of Choqok, the KDE micro-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
 
 
14
 
 
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.
 
19
 
 
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/
 
22
 
 
23
*/
 
24
#include "accountswizard.h"
 
25
#include "accountmanager.h"
 
26
#include <kdebug.h>
 
27
#include <QProgressBar>
 
28
#include <KMessageBox>
 
29
#include "backend.h"
 
30
#include <QTimer>
 
31
 
 
32
AccountsWizard::AccountsWizard( QString alias, QWidget *parent )
 
33
        : KDialog( parent ), progress(0), verifyTimer(0)
 
34
{
 
35
    kDebug();
 
36
    QWidget *dialog = new QWidget( this );
 
37
    ui.setupUi( dialog );
 
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) ) );
 
42
 
 
43
    if ( alias.isEmpty() ) {
 
44
        this->setCaption( i18n( "Add a new account" ) );
 
45
        isEdit = false;
 
46
    } else {
 
47
        this->setCaption( i18n( "Modify existing account" ) );
 
48
        isEdit = true;
 
49
        this->mAlias = alias;
 
50
        loadAccount( alias );
 
51
    }
 
52
 
 
53
}
 
54
 
 
55
void AccountsWizard::loadAccount( QString &alias )
 
56
{
 
57
    kDebug() << "Loading account " << alias;
 
58
    mAccount = AccountManager::self()->findAccount( alias );
 
59
    if ( mAccount.isError() ) {
 
60
        kError() << "Error on Loading Account with alias " << alias;
 
61
        return;
 
62
    }
 
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() );
 
69
}
 
70
 
 
71
void AccountsWizard::slotButtonClicked( int button )
 
72
{
 
73
    kDebug();
 
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());
 
79
        ///Show ProgressBar:
 
80
        if ( progress ) {
 
81
            progress->hide();
 
82
            progress->deleteLater();
 
83
            progress = 0L;
 
84
        }
 
85
        progress = new QProgressBar( this );
 
86
        progress->setMinimum( 0 );
 
87
        progress->setMaximum( 0 );
 
88
        QGridLayout* grid = qobject_cast<QGridLayout*>( this->mainWidget()->layout() );
 
89
        if ( !grid )
 
90
            return;
 
91
        grid->addWidget( progress, grid->rowCount(), 0, grid->rowCount(), 2 );
 
92
        ///Check for account
 
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() );
 
99
 
 
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();
 
104
        if( verifyTimer ) {
 
105
            verifyTimer->deleteLater();
 
106
            verifyTimer = 0;
 
107
        }
 
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);
 
113
    } else
 
114
        KDialog::slotButtonClicked( button );
 
115
}
 
116
 
 
117
void AccountsWizard::slotUserVerified( Account * userAccount )
 
118
{
 
119
    kDebug();
 
120
    if(sender())
 
121
        sender()->deleteLater();
 
122
    if ( progress ) {
 
123
        progress->deleteLater();
 
124
        progress = 0;
 
125
    }
 
126
    verifyTimer->stop();
 
127
    this->unsetCursor();
 
128
    if ( !userAccount ) {
 
129
        kError() << "userAccount is NULL";
 
130
        return;
 
131
    }
 
132
    mAccount = *userAccount;
 
133
    if ( isEdit ) {
 
134
        mAccount = AccountManager::self()->modifyAccount( mAccount, mAlias );
 
135
    } else {
 
136
        mAccount = AccountManager::self()->addAccount( mAccount );
 
137
    }
 
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());
 
142
        return;
 
143
    }
 
144
 
 
145
    if ( isEdit ) {
 
146
        emit accountEdited( mAccount );
 
147
    } else {
 
148
        emit accountAdded( mAccount );
 
149
    }
 
150
    accept();
 
151
}
 
152
 
 
153
void AccountsWizard::slotError( const QString & errMsg )
 
154
{
 
155
    kDebug();
 
156
    if ( progress ) {
 
157
        progress->deleteLater();
 
158
        progress = 0;
 
159
    }
 
160
    this->unsetCursor();
 
161
    verifyTimer->stop();
 
162
    KMessageBox::detailedError( this, i18n( "Authentication failed, please check your credentials." ), errMsg );
 
163
}
 
164
 
 
165
void AccountsWizard::handleVerifyTimeout()
 
166
{
 
167
    kDebug();
 
168
    if ( progress ) {
 
169
        progress->deleteLater();
 
170
        progress = 0;
 
171
    }
 
172
    this->unsetCursor();
 
173
    verifyTimer->stop();
 
174
    KMessageBox::sorry(this, i18n( "Verification progress timed out. "
 
175
                                   "Check your Internet connection and credentials then try again." ) , i18n( "Timeout" ) );
 
176
 
 
177
}
 
178
 
 
179
void AccountsWizard::slotServiceChanged( int index )
 
180
{
 
181
    if( index == 2 ) {
 
182
        ui.lblHomepage->setVisible( true );
 
183
        ui.kcfg_homepage->setVisible( true );
 
184
    } else {
 
185
        ui.lblHomepage->setVisible( false );
 
186
        ui.kcfg_homepage->setVisible( false );
 
187
    }
 
188
}
 
189
 
 
190
#include "accountswizard.moc"