File indexing completed on 2025-01-19 03:55:37

0001 #include <QNetworkAccessManager>
0002 #include <QNetworkRequest>
0003 #include <QNetworkReply>
0004 #include <QDesktopServices>
0005 #include <QMetaEnum>
0006 #include <QDebug>
0007 #include <QRegExp>
0008 
0009 #include "vimeodemo.h"
0010 #include "o0globals.h"
0011 #include "o0settingsstore.h"
0012 #include "o2requestor.h"
0013 
0014 const char VIMEO_APP_KEY[] = "YOUR_VIMEO_APP_KEY";
0015 const char VIMEO_APP_SECRET[] = "YOUT_VIMEO_APP_SECRET";
0016 const char VIMEO_SCOPE[] = "public";
0017 const char VIMEO_USER_INFO_URL[] = "https://api.vimeo.com/me";
0018 
0019 const int localPort = 8888;
0020 
0021 #define QENUM_NAME(o,e,v) (o::staticMetaObject.enumerator(o::staticMetaObject.indexOfEnumerator(#e)).valueToKey((v)))
0022 #define GRANTFLOW_STR(v) QString(QENUM_NAME(O2, GrantFlow, v))
0023 
0024 VimeoDemo::VimeoDemo(QObject *parent) :
0025     QObject(parent), requestId_(0) {
0026     o2Vimeo_ = new O2Vimeo(this);
0027 
0028     o2Vimeo_->setClientId(VIMEO_APP_KEY);
0029     o2Vimeo_->setClientSecret(VIMEO_APP_SECRET);
0030     o2Vimeo_->setLocalPort(localPort);
0031     o2Vimeo_->setScope(VIMEO_SCOPE);
0032 
0033     // Create a store object for writing the received tokens
0034     O0SettingsStore *store = new O0SettingsStore(O2_ENCRYPTION_KEY);
0035     store->setGroupKey("google");
0036     o2Vimeo_->setStore(store);
0037 
0038     connect(o2Vimeo_, SIGNAL(linkedChanged()), this, SLOT(onLinkedChanged()));
0039     connect(o2Vimeo_, SIGNAL(linkingFailed()), this, SIGNAL(linkingFailed()));
0040     connect(o2Vimeo_, SIGNAL(linkingSucceeded()), this, SLOT(onLinkingSucceeded()));
0041     connect(o2Vimeo_, SIGNAL(openBrowser(QUrl)), this, SLOT(onOpenBrowser(QUrl)));
0042     connect(o2Vimeo_, SIGNAL(closeBrowser()), this, SLOT(onCloseBrowser()));
0043 }
0044 
0045 void VimeoDemo::doOAuth(O2::GrantFlow grantFlowType) {
0046     qDebug() << "Starting OAuth 2 with grant flow type" << GRANTFLOW_STR(grantFlowType) << "...";
0047     o2Vimeo_->setGrantFlow(grantFlowType);
0048     o2Vimeo_->unlink();
0049     o2Vimeo_->link();
0050 }
0051 
0052 void VimeoDemo::getUserName() {
0053     if (!o2Vimeo_->linked()) {
0054         qWarning() << "ERROR: Application is not linked!";
0055         emit linkingFailed();
0056         return;
0057     }
0058 
0059     QString userInfoURL = QString(VIMEO_USER_INFO_URL);
0060     QNetworkRequest request = QNetworkRequest(QUrl(userInfoURL));
0061     QNetworkAccessManager *mgr = new QNetworkAccessManager(this);
0062     O2Requestor *requestor = new O2Requestor(mgr, o2Vimeo_, this);
0063     requestId_ = requestor->get(request);
0064     connect(requestor, SIGNAL(finished(int, QNetworkReply::NetworkError, QByteArray)),
0065         this, SLOT(onFinished(int, QNetworkReply::NetworkError, QByteArray))
0066     );
0067     qDebug() << "Getting user channel info... Please wait.";
0068 }
0069 
0070 void VimeoDemo::onOpenBrowser(const QUrl &url) {
0071     QDesktopServices::openUrl(url);
0072 }
0073 
0074 void VimeoDemo::onCloseBrowser() {
0075 }
0076 
0077 void VimeoDemo::onLinkedChanged() {
0078     qDebug() << "Link changed!";
0079 }
0080 
0081 void VimeoDemo::onLinkingSucceeded() {
0082     if (!o2Vimeo_->linked()) {
0083         return;
0084     }
0085     QVariantMap extraTokens = o2Vimeo_->extraTokens();
0086     if (!extraTokens.isEmpty()) {
0087         emit extraTokensReady(extraTokens);
0088         qDebug() << "Extra tokens in response:";
0089         foreach (QString key, extraTokens.keys()) {
0090             qDebug() << "\t" << key << ":" << (extraTokens.value(key).toString().left(3) + "...");
0091         }
0092     }
0093     emit linkingSucceeded();
0094 }
0095 
0096 void VimeoDemo::onFinished(int requestId, QNetworkReply::NetworkError error, QByteArray replyData) {
0097     if (requestId != requestId_)
0098         return;
0099 
0100     if (error != QNetworkReply::NoError) {
0101         qWarning() << "Reply error:" << error;
0102         emit userNameFailed();
0103         return;
0104     }
0105 
0106     QString reply(replyData);
0107     bool errorFound = reply.contains("error");
0108     if (errorFound) {
0109         qDebug() << "Request failed";
0110         emit userNameFailed();
0111         return;
0112     }
0113 
0114     QRegExp nameRE("\"name\":\"([^\"]+)\"");
0115     if (nameRE.indexIn(reply) == -1) {
0116         qDebug() << "Can not parse reply:" << reply;
0117         emit userNameFailed();
0118         return;
0119     }
0120 
0121     qInfo() << "User name: " << nameRE.cap(1);
0122     emit userNameReceived();
0123 }
0124 
0125 #include "moc_vimeodemo.cpp"