00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "authentication.h"
00021 #include "atticamanager.h"
00022 #include <core/singleton.h>
00023
00024 #include <QDebug>
00025
00026 using namespace GluonPlayer;
00027
00028 template<> GLUON_PLAYER_EXPORT Authentication* GluonCore::Singleton<Authentication>::m_instance = 0;
00029
00030 Authentication::Authentication()
00031 : m_initialized( false )
00032 , m_loggedIn( false )
00033 {
00034 init();
00035 }
00036
00037 Authentication::~Authentication()
00038 {
00039 }
00040
00041 void Authentication::init()
00042 {
00043 if( !AtticaManager::instance()->isProviderValid() )
00044 {
00045 connect( AtticaManager::instance(), SIGNAL( gotProvider() ), SLOT( finishInit() ) );
00046 connect( AtticaManager::instance(), SIGNAL( failedToFetchProvider() ), SIGNAL( initFailed() ) );
00047 }
00048 }
00049
00050 bool Authentication::isInitialized()
00051 {
00052 return m_initialized;
00053 }
00054
00055 QString Authentication::username()
00056 {
00057 return m_username;
00058 }
00059
00060 QString Authentication::password()
00061 {
00062 return m_password;
00063 }
00064
00065 void Authentication::onRegisterClicked( const QString& username, const QString& password, const QString& mail,
00066 const QString& firstName, const QString& lastName)
00067 {
00068
00069
00070 if( AtticaManager::instance()->isProviderValid() )
00071 {
00072 m_registerJob = AtticaManager::instance()->provider().registerAccount(username, password, mail, firstName, lastName);
00073 connect(m_registerJob, SIGNAL(finished(Attica::BaseJob*)), SLOT(onRegisterAccountFinished(Attica::BaseJob*)));
00074 m_registerJob->start();
00075 }
00076 }
00077
00078 bool Authentication::login( const QString& username, const QString& password )
00079 {
00080 m_username = username;
00081 m_password = password;
00082
00083 if( AtticaManager::instance()->isProviderValid() )
00084 {
00085 m_checkLoginJob = AtticaManager::instance()->provider().checkLogin( m_username, m_password );
00086 connect( m_checkLoginJob, SIGNAL( finished( Attica::BaseJob* ) ), SLOT( checkLoginResult( Attica::BaseJob* ) ) );
00087 m_checkLoginJob->start();
00088 return true;
00089 }
00090
00091 return false;
00092 }
00093
00094 bool Authentication::isLoggedIn()
00095 {
00096 return m_loggedIn;
00097 }
00098
00099 bool Authentication::hasCredentials()
00100 {
00101 return AtticaManager::instance()->provider().hasCredentials();
00102 }
00103
00104 void Authentication::finishInit()
00105 {
00106 AtticaManager::instance()->provider().loadCredentials( m_username, m_password );
00107 m_initialized = true;
00108 emit initialized();
00109 }
00110
00111 void Authentication::showRegisterError(const Attica::Metadata& metadata)
00112 {
00113 if (metadata.error() == Attica::Metadata::NetworkError) {
00114 qWarning() << tr("Failed to register new account.");
00115 } else {
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 QWidget* widgetToHighlight = 0;
00129 QString hint;
00130 switch (metadata.statusCode()) {
00131 case 100:
00132 hint = tr("Registration succeeded.");
00133 break;
00134 case 101:
00135 hint = tr("Failed to register new account: please specify all mandatory fields.");
00136 case 102:
00137 hint = tr("Failed to register new account: invalid password.");
00138 break;
00139 case 103:
00140 hint = tr("Failed to register new account: invalid username.");
00141 break;
00142 case 104:
00143 hint = tr("Failed to register new account: the requested username is already taken.");
00144 break;
00145 case 105:
00146 hint = tr("Failed to register new account: the specified email address is already taken.");
00147 break;
00148 case 106:
00149 hint = tr("Failed to register new account: the specified email address is invalid.");
00150 default:
00151 hint = tr("Failed to register new account.");
00152 break;
00153 }
00154 qDebug() << hint;
00155 }
00156 }
00157
00158 void Authentication::onRegisterAccountFinished(Attica::BaseJob* job)
00159 {
00160 Attica::PostJob* postJob = static_cast<Attica::PostJob*>(job);
00161
00162 if (postJob->metadata().error() == Attica::Metadata::NoError)
00163 {
00164 AtticaManager::instance()->provider().saveCredentials( m_username, m_password );
00165 emit registered();
00166 }
00167 else
00168 {
00169
00170
00171 showRegisterError( postJob->metadata() );
00172 }
00173 }
00174
00175 void Authentication::checkLoginResult( Attica::BaseJob* baseJob )
00176 {
00177 Attica::PostJob* job = qobject_cast<Attica::PostJob*>( baseJob );
00178
00179 if( job->metadata().error() == Attica::Metadata::NoError )
00180 {
00181 qDebug() << "Login OK" << endl;
00182 AtticaManager::instance()->provider().saveCredentials( m_username, m_password );
00183 m_loggedIn = true;
00184 emit loggedIn();
00185 }
00186 else
00187 {
00188 qDebug() << "Login error" << endl;
00189 m_loggedIn = false;
00190 emit loginFailed();
00191 }
00192 }
00193
00194 #include "authentication.moc"