daily pastebin goal
84%
SHARE
TWEET

harmattan sso

a guest Apr 3rd, 2012 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. readitlaterprovider.h
  2.  
  3. ---------------------
  4.  
  5. #ifndef READITLATERPROVIDER_H
  6. #define READITLATERPROVIDER_H
  7.  
  8. #include <SignOn/AuthPluginInterface>
  9.  
  10. class QNetworkAccessManager;
  11.  
  12. class ReadItLaterPlugin : public AuthPluginInterface
  13. {
  14.     Q_OBJECT
  15.     Q_INTERFACES(AuthPluginInterface)
  16.  
  17. public:
  18.     ReadItLaterPlugin(QObject *parent = 0);
  19.     virtual ~ReadItLaterPlugin();
  20.  
  21. public Q_SLOTS:
  22.     QString type() const;
  23.     QStringList mechanisms() const;
  24.  
  25.     void cancel();
  26.     void abort();
  27.  
  28.     void process(const SignOn::SessionData &inData, const QString &mechanism = QString());
  29.     void authenticationFinished();
  30.  
  31. private:
  32.     QNetworkAccessManager *m_networkManager;
  33.     SignOn::SessionData m_data;
  34. };
  35.  
  36. #endif // READITLATERPROVIDER_H
  37.  
  38. -----------------------
  39. readitlaterprovider.cpp
  40. -----------------------
  41.  
  42.  
  43. #include "readitlaterprovider.h"
  44.  
  45. #include <SignOn/Error>
  46.  
  47. #include <QNetworkAccessManager>
  48. #include <QNetworkReply>
  49.  
  50. ReadItLaterPlugin::ReadItLaterPlugin(QObject *parent) :
  51.     AuthPluginInterface(parent), m_networkManager(new QNetworkAccessManager(this))
  52. {
  53. }
  54.  
  55. ReadItLaterPlugin::~ReadItLaterPlugin()
  56. {
  57. }
  58.  
  59.  
  60. QString ReadItLaterPlugin::type() const
  61. {
  62.     return "readitlater";
  63. }
  64.  
  65. QStringList ReadItLaterPlugin::mechanisms() const
  66. {
  67.     return QStringList() << "ReadItLater";
  68. }
  69.  
  70. void ReadItLaterPlugin::cancel()
  71. {
  72. }
  73.  
  74. void ReadItLaterPlugin::abort()
  75. {
  76. }
  77.  
  78. void ReadItLaterPlugin::process(const SignOn::SessionData &inData, const QString &mechanism)
  79. {
  80.     if (mechanism == "ReadItLater") {
  81.         QUrl query;
  82.         query.addQueryItem("username", inData.getProperty("username").toString());
  83.         query.addQueryItem("password", inData.getProperty("password").toString());
  84.  
  85.         QNetworkRequest request(QUrl("https://readitlaterlist.com/v2/auth"));
  86.  
  87.         QNetworkReply *reply = m_networkManager->post(request, query.encodedQuery());
  88.         connect(reply, SIGNAL(finished()), SLOT(authenticationFinished()));
  89.  
  90.         m_data = inData;
  91.     }
  92.  
  93.  
  94. }
  95.  
  96. void ReadItLaterPlugin::authenticationFinished()
  97. {
  98.     QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
  99.     switch (reply->error()) {
  100.     case QNetworkReply::NoError:
  101.         emit result(m_data);
  102.         break;
  103.     case QNetworkReply::ConnectionRefusedError:
  104.         emit error(SignOn::Error(SignOn::Error::InternalServer, reply->errorString()));
  105.         break;
  106.     case QNetworkReply::RemoteHostClosedError:
  107.         emit error(SignOn::Error(SignOn::Error::InternalServer, reply->errorString()));
  108.         break;
  109.     case QNetworkReply::HostNotFoundError:
  110.         emit error(SignOn::Error(SignOn::Error::InternalCommunication, reply->errorString()));
  111.         break;
  112.     case QNetworkReply::TimeoutError:
  113.         emit error(SignOn::Error(SignOn::Error::TimedOut, reply->errorString()));
  114.         break;
  115.     case QNetworkReply::OperationCanceledError:
  116.         emit error(SignOn::Error(SignOn::Error::IdentityOperationCanceled, reply->errorString()));
  117.         break;
  118.     case QNetworkReply::SslHandshakeFailedError:
  119.         emit error(SignOn::Error(SignOn::Error::Ssl, reply->errorString()));
  120.         break;
  121.     case QNetworkReply::TemporaryNetworkFailureError:
  122.         emit error(SignOn::Error(SignOn::Error::Network, reply->errorString()));
  123.         break;
  124.     case QNetworkReply::ContentAccessDenied:
  125.         // Important: wrong username/password
  126.         emit error(SignOn::Error(SignOn::Error::NotAuthorized, reply->errorString()));
  127.         break;
  128.     default:
  129.         emit error(SignOn::Error(SignOn::Error::Unknown, reply->errorString()));
  130.         break;
  131.     }
  132. }
  133.  
  134. SIGNON_DECL_AUTH_PLUGIN(ReadItLaterPlugin)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top