Permalink
Browse files

can upload file

  • Loading branch information...
1 parent 8f6ebae commit 1ea95cd1514a7fb91c2f0044d98edec3aa3241d8 @wengxt committed Apr 18, 2012
View
@@ -15,6 +15,7 @@ add_definitions(${KDE4_DEFINITIONS})
find_package(QCA2 REQUIRED)
find_package(QOAuth REQUIRED)
find_package(LibArchive REQUIRED)
+find_package(QJson REQUIRED)
set(CMAKE_C_FLAGS "-Wall -Wextra -Wno-sign-compare -Wno-unused-parameter -fvisibility=hidden ${CMAKE_C_FLAGS}")
set(CMAKE_CXX_FLAGS "-Wall -Wextra -Wno-sign-compare -Wno-unused-parameter -fvisibility=hidden ${CMAKE_CXX_FLAGS}")
@@ -2,6 +2,7 @@ include_directories(${QT_QTCORE_INCLUDE_DIR}
${QOAuth_INCLUDE_DIR}
${QT_QTGUI_INCLUDE_DIR}
${QCA2_INCLUDE_DIR}
+ ${QJSON_INCLUDE_DIR}
)
set(dropbox_SRCS dropbox.cpp plugin.cpp)
@@ -20,6 +21,7 @@ add_library(qcloud_dropbox MODULE ${dropbox_SRCS} ${dropbox_MOC_SRCS})
target_link_libraries(qcloud_dropbox
${QT_QTCORE_LIBRARY}
${QT_QTGUI_LIBRARY}
+ ${QJSON_LIBRARY}
qcloud
)
@@ -1,12 +1,22 @@
#include <QtOAuth/QtOAuth>
#include <QUrl>
+#include <QLocale>
+#include <QEventLoop>
+#include <QNetworkRequest>
+#include <QNetworkAccessManager>
+#include <QNetworkReply>
+#include <QDebug>
+#include <QFile>
+#include <qjson/parser.h>
#include "dropbox.h"
#include "app.h"
#include "oauthwidget.h"
#include "authdialog.h"
#include "qcloud_utils.h"
Dropbox::Dropbox (QObject* parent) : OAuthBackend (parent)
+ ,m_parser(new QJson::Parser)
+ ,m_globalAccess(false)
{
m_requestTokenUrl = "https://api.dropbox.com/1/oauth/request_token";
m_authorizeUrl = "https://www.dropbox.com/1/oauth/authorize";
@@ -24,6 +34,7 @@ void Dropbox::setApp (QCloud::App* app)
m_app = app;
QString key = app->settings()->value ("Dropbox/AppKey").toString();
QString secret = app->settings()->value ("Dropbox/AppSecret").toString();
+ m_globalAccess = app->settings()->value("Dropbox/GlobalAccess").toBool();
if (!key.isEmpty() && !secret.isEmpty()) {
setAppKey (key);
setAppSecret (secret);
@@ -42,7 +53,16 @@ bool Dropbox::authorize (QWidget* parent)
dialog.setWindowTitle(tr("Dropbox Authentication"));
dialog.setWindowState (Qt::WindowMaximized);
int result = dialog.exec();
- return result == QDialog::Accepted;
+ if (result == QDialog::Accepted) {
+ QOAuth::ParamMap reply = m_oauth->accessToken("https://api.dropbox.com/1/oauth/access_token", QOAuth::POST, m_oauthToken, m_oauthTokenSecret);
+ if (m_oauth->error() == QOAuth::NoError) {
+ m_oauthToken = reply.value(QOAuth::tokenParameterName());
+ m_oauthTokenSecret = reply.value(QOAuth::tokenSecretParameterName());
+ m_uid = reply.value("uid");
+ return true;
+ }
+ }
+ return false;
}
void Dropbox::startAuth (QCloud::OAuthWidget* widget)
@@ -55,14 +75,53 @@ void Dropbox::startAuth (QCloud::OAuthWidget* widget)
}
-bool Dropbox::uploadFile (const QString& filename)
+bool Dropbox::uploadFile (const QString& filename, const QString& filepath)
{
- return false;
+ QFile file(filename);
+ if (!file.open(QIODevice::ReadOnly))
+ return false;
+
+ QString surl;
+ if (m_globalAccess)
+ surl = "https://api-content.dropbox.com/1/files_put/dropbox/%1";
+ else
+ surl = "https://api-content.dropbox.com/1/files_put/sandbox/%1";
+ QUrl url(surl.arg(filepath));
+ QNetworkRequest request(url);
+ request.setRawHeader("Authorization", authorizationHeader(url, QOAuth::PUT));
+ QNetworkReply* reply = m_networkAccessManager->put(request, &file);
+
+ QEventLoop loop;
+ QObject::connect(reply, SIGNAL(readyRead()), &loop, SLOT(quit()));
+
+ // Execute the event loop here, now we will wait here until readyRead() signal is emitted
+ // which in turn will trigger event loop quit.
+ loop.exec();
+
+ // Lets print the HTTP GET response.
+ QVariant result = m_parser->parse(reply->readAll());
+ qDebug() << result;
+
+
+ return true;
}
void Dropbox::loadAccountInfo()
{
+ QUrl url("https://api.dropbox.com/1/account/info");
+ QNetworkRequest request(url);
+ request.setRawHeader("Authorization", authorizationHeader(url, QOAuth::GET));
+ QNetworkReply* reply = m_networkAccessManager->get(request);
+
+ QEventLoop loop;
+ QObject::connect(reply, SIGNAL(readyRead()), &loop, SLOT(quit()));
+
+ // Execute the event loop here, now we will wait here until readyRead() signal is emitted
+ // which in turn will trigger event loop quit.
+ loop.exec();
+ // Lets print the HTTP GET response.
+ qDebug() << QString::fromUtf8(reply->readAll());
}
void Dropbox::saveAccountInfo()
View
@@ -3,6 +3,11 @@
#include "oauthbackend.h"
+namespace QJson
+{
+class Parser;
+}
+
namespace QCloud
{
class App;
@@ -16,10 +21,15 @@ class Dropbox : public QCloud::OAuthBackend
virtual ~Dropbox();
virtual void setApp (QCloud::App* app);
virtual bool authorize (QWidget* parent = 0);
- virtual bool uploadFile (const QString& filename);
+ virtual bool uploadFile (const QString& filename, const QString& filepath);
virtual void startAuth (QCloud::OAuthWidget* widget);
virtual void loadAccountInfo();
virtual void saveAccountInfo();
+
+private:
+ QJson::Parser* m_parser;
+ QByteArray m_uid;
+ bool m_globalAccess;
};
#endif
View
@@ -0,0 +1,39 @@
+# - Try to find the QJson library
+# Once done this will define
+#
+# QJSON_FOUND - system has the QJson library
+# QJSON_INCLUDE_DIR - the QJson include directory
+# QJSON_LIBRARY - Link this to use the QJson library
+#
+# Copyright (c) 2010, Pino Toscano, <toscano.pino@tiscali.it>
+#
+# Redistribution and use is allowed according to the terms of the BSD license.
+# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
+
+if (QJSON_INCLUDE_DIR AND QJSON_LIBRARY)
+
+ # in cache already
+ set(QJSON_FOUND TRUE)
+
+else (QJSON_INCLUDE_DIR AND QJSON_LIBRARY)
+ if (NOT WIN32)
+ find_package(PkgConfig)
+ pkg_check_modules(PC_QJSON QJson)
+ endif(NOT WIN32)
+
+ find_path(QJSON_INCLUDE_DIR qjson/parser.h
+ HINTS
+ ${PC_QJSON_INCLUDE_DIRS}
+ )
+
+ find_library(QJSON_LIBRARY NAMES qjson
+ HINTS
+ ${PC_QJSON_LIBRARY_DIRS}
+ )
+
+ include(FindPackageHandleStandardArgs)
+ find_package_handle_standard_args(QJson DEFAULT_MSG QJSON_LIBRARY QJSON_INCLUDE_DIR)
+
+ mark_as_advanced(QJSON_INCLUDE_DIR QJSON_LIBRARY)
+
+endif (QJSON_INCLUDE_DIR AND QJSON_LIBRARY)
View
@@ -21,7 +21,7 @@ class QCLOUD_EXPORT IBackend : public QObject
virtual void setApp (App* app) = 0;
virtual bool authorize (QWidget* widget = 0) = 0;
virtual void setNetworkAccessManager (QNetworkAccessManager* manager);
- virtual bool uploadFile (const QString& filename) = 0;
+ virtual bool uploadFile (const QString& filename, const QString& filepath) = 0;
virtual void saveAccountInfo () = 0;
virtual void loadAccountInfo () = 0;
QNetworkAccessManager* networkAccessManager();
View
@@ -31,16 +31,19 @@ class QCLOUD_EXPORT OAuthBackend : public IBackend
void setAppKey (const QString& appkey);
QString appSecret() const;
void setAppSecret (const QString& appsecret);
- QString oauthToken() const;
- QString oauthTokenSecret() const;
+ QByteArray oauthToken() const;
+ QByteArray oauthTokenSecret() const;
QString requestTokenUrl() const;
uint timeout() const;
void setAuthUrl (const QUrl& url);
+
protected:
+ QByteArray authorizationHeader(const QUrl& url, QOAuth::HttpMethod method, QOAuth::ParamMap params = QOAuth::ParamMap());
+
QString m_appKey;
QString m_appSecret;
- QString m_oauthToken;
- QString m_oauthTokenSecret;
+ QByteArray m_oauthToken;
+ QByteArray m_oauthTokenSecret;
QString m_requestTokenUrl;
QString m_authorizeUrl;
QString m_accessTokenUrl;
View
@@ -34,12 +34,12 @@ int OAuthBackend::error() const
return 0;
}
-QString OAuthBackend::oauthToken() const
+QByteArray OAuthBackend::oauthToken() const
{
return m_oauthToken;
}
-QString OAuthBackend::oauthTokenSecret() const
+QByteArray OAuthBackend::oauthTokenSecret() const
{
return m_oauthTokenSecret;
}
@@ -101,4 +101,9 @@ void OAuthBackend::setAuthUrl(const QUrl& url)
m_authUrl = url;
}
+QByteArray OAuthBackend::authorizationHeader(const QUrl& url, QOAuth::HttpMethod method, QOAuth::ParamMap params)
+{
+ return m_oauth->createParametersString(url.toString(), method, m_oauthToken, m_oauthTokenSecret, QOAuth::HMAC_SHA1, params, QOAuth::ParseForHeaderArguments);
+}
+
}
@@ -2,6 +2,7 @@ include_directories(${QT_QTCORE_INCLUDE_DIR}
${QOAuth_INCLUDE_DIR}
${QT_QTGUI_INCLUDE_DIR}
${QCA2_INCLUDE_DIR}
+ ${QJSON_INCLUDE_DIR}
${PROJECT_SOURCE_DIR}/backend/dropbox/
)
@@ -18,5 +19,6 @@ add_executable(qcloud_dropbox_test
target_link_libraries(qcloud_dropbox_test
${QT_QTCORE_LIBRARY}
${QT_QTGUI_LIBRARY}
+ ${QJSON_LIBRARY}
qcloud
)
@@ -4,15 +4,30 @@
#include "dropbox.h"
#include "oauthwidget.h"
#include "factory.h"
+#include "app.h"
int main (int argc, char* argv[])
{
QApplication app (argc, argv);
+
+ if (argc <= 2) {
+ return 0;
+ }
Dropbox* dropbox = new Dropbox;
dropbox->setAppKey ("d2anwsztkcu1upz");
dropbox->setAppSecret ("kt9a7tuph615hzs");
dropbox->setNetworkAccessManager (QCloud::Factory::instance()->createNetwork ("kde"));
+ QCloud::App a(argv[1]);
+
+ dropbox->setApp(&a);
+
+ bool result = dropbox->authorize();
+
+ qDebug() << "Authorize result" << result;
- qDebug() << "Authorize result" << dropbox->authorize();
+ if (result) {
+ dropbox->loadAccountInfo();
+ dropbox->uploadFile(argv[2], "test_file");
+ }
return 0;
}

0 comments on commit 1ea95cd

Please sign in to comment.