From f4c5b9adbb0b688335f1ad783e801e241a39ec71 Mon Sep 17 00:00:00 2001 From: Klaas Freitag Date: Thu, 6 Oct 2011 16:55:28 +0200 Subject: [PATCH] added check for the owncloud folder - WIP --- src/CMakeLists.txt | 1 + src/mirall/folderwizard.cpp | 39 ++++++++++++++++++++++++ src/mirall/folderwizard.h | 13 ++++++++ src/mirall/ownclouddircheck.cpp | 54 +++++++++++++++++++++++++++++++++ src/mirall/ownclouddircheck.h | 31 +++++++++++++++++++ src/mirall/owncloudinfo.cpp | 21 +++++++++++-- src/mirall/owncloudinfo.h | 4 ++- 7 files changed, 159 insertions(+), 4 deletions(-) create mode 100644 src/mirall/ownclouddircheck.cpp create mode 100644 src/mirall/ownclouddircheck.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a50416ae1c..7d3b6e0300 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -36,6 +36,7 @@ mirall/statusdialog.cpp mirall/owncloudwizard.cpp mirall/owncloudsetup.cpp mirall/owncloudinfo.cpp +mirall/ownclouddircheck.cpp ) if(CSYNC_FOUND) set(mirall_SRCS diff --git a/src/mirall/folderwizard.cpp b/src/mirall/folderwizard.cpp index 0263cb2d04..fce6da896f 100644 --- a/src/mirall/folderwizard.cpp +++ b/src/mirall/folderwizard.cpp @@ -20,10 +20,12 @@ #include #include #include + #include #include "mirall/folderwizard.h" #include "mirall/owncloudinfo.h" +#include "mirall/ownclouddircheck.h" namespace Mirall @@ -72,8 +74,45 @@ FolderWizardTargetPage::FolderWizardTargetPage() registerField("targetLocalFolder", _ui.localFolder2LineEdit); registerField("targetURLFolder", _ui.urlFolderLineEdit); registerField("targetOCFolder", _ui.OCFolderLineEdit); + + connect( _ui.OCFolderLineEdit, SIGNAL(textChanged(QString)), + SLOT(slotFolderTextChanged(QString))); + + _timer = new QTimer(this); + _timer->setSingleShot( true ); + connect( _timer, SIGNAL(timeout()), SLOT(slotTimerFires())); + + _ownCloudInfo = new ownCloudInfo( this ); + _ownCloudDirCheck = new ownCloudDirCheck( this ); + + connect( _ownCloudDirCheck, SIGNAL(directoryExists(QString,bool)), + SLOT(slotInfoReply(QString,bool))); } +void FolderWizardTargetPage::slotFolderTextChanged( const QString& t) +{ + if( t.isEmpty() ) { + _timer->stop(); + return; + } + qDebug() << "XX new folder string: " << t; + _timer->start(500); +} + +void FolderWizardTargetPage::slotTimerFires() +{ + const QString folder = _ui.OCFolderLineEdit->text(); + qDebug() << "Querying folder " << folder; + + _ownCloudDirCheck->checkDirectory( folder ); +} + +void FolderWizardTargetPage::slotInfoReply(const QString &url, bool exists ) +{ + qDebug() << "Got reply from ownCloudInfo: " << url << " :" << exists; +} + + FolderWizardTargetPage::~FolderWizardTargetPage() { diff --git a/src/mirall/folderwizard.h b/src/mirall/folderwizard.h index 067485e63a..0b5e5d7208 100644 --- a/src/mirall/folderwizard.h +++ b/src/mirall/folderwizard.h @@ -16,6 +16,8 @@ #define MIRALL_FOLDERWIZARD_H #include +#include +#include #include "ui_folderwizardsourcepage.h" #include "ui_folderwizardtargetpage.h" @@ -25,6 +27,9 @@ namespace Mirall { +class ownCloudInfo; +class ownCloudDirCheck; + /** * page to ask for the local source folder */ @@ -73,8 +78,16 @@ protected slots: void slotOwnCloudFound( const QString&, const QString& ); void slotNoOwnCloudFound(); + + void slotFolderTextChanged( const QString& ); + void slotTimerFires(); + void slotInfoReply( const QString&, bool ); + private: Ui_FolderWizardTargetPage _ui; + QTimer *_timer; + ownCloudInfo *_ownCloudInfo; + ownCloudDirCheck *_ownCloudDirCheck; }; class FolderWizardNetworkPage : public QWizardPage diff --git a/src/mirall/ownclouddircheck.cpp b/src/mirall/ownclouddircheck.cpp new file mode 100644 index 0000000000..0c76cd9d52 --- /dev/null +++ b/src/mirall/ownclouddircheck.cpp @@ -0,0 +1,54 @@ +#include + +#include "ownclouddircheck.h" +#include "owncloudinfo.h" + +namespace Mirall { + +ownCloudDirCheck::ownCloudDirCheck(QObject *parent) : + QObject(parent), + _manager( new QNetworkAccessManager ), + _reply(0) +{ + connect( _manager, SIGNAL(finished(QNetworkReply*)), + this, SLOT(slotReplyFinished(QNetworkReply*))); +} + +bool ownCloudDirCheck::checkDirectory( const QString& dir ) +{ + if( dir.isEmpty() ) { + // assume the root exists on the ownCloud + emit directoryExists( dir, true ); + return true; + } + + ownCloudInfo info; + + if( _reply && _reply->isRunning() ) _reply->abort(); + + QNetworkRequest request; + request.setUrl( QUrl( info.url() + "/files/webdav.php/"+dir ) ); + request.setRawHeader( "User-Agent", "mirall" ); + + QString concatenated = info.user() + ":" + info.password(); + QByteArray data = concatenated.toLocal8Bit().toBase64(); + QString headerData = "Basic " + data; + request.setRawHeader("Authorization", headerData.toLocal8Bit()); + + _reply = _manager->get( request ); + +} + +void ownCloudDirCheck::slotReplyFinished( QNetworkReply *reply ) +{ + bool re = true; + if( reply->error() != QNetworkReply::NoError ) { + qDebug() << "Error in ownCloudDirCheck: " << reply->error(); + re = false; + } + qDebug() << "ownCloudDirCheck ret code: " << reply->error(); + emit directoryExists( reply->url().toString(), re ); +} + +} +#include "ownclouddircheck.moc" diff --git a/src/mirall/ownclouddircheck.h b/src/mirall/ownclouddircheck.h new file mode 100644 index 0000000000..dfdc50e0d7 --- /dev/null +++ b/src/mirall/ownclouddircheck.h @@ -0,0 +1,31 @@ +#ifndef OWNCLOUDDIRCHECK_H +#define OWNCLOUDDIRCHECK_H + +#include +#include + +namespace Mirall { + +class ownCloudDirCheck : public QObject +{ + Q_OBJECT +public: + explicit ownCloudDirCheck(QObject *parent = 0); + + bool checkDirectory( const QString& ); + +signals: + void directoryExists( const QString&, bool ); + +protected slots: + void slotReplyFinished( QNetworkReply* ); + +private: + QNetworkAccessManager *_manager; + QNetworkReply *_reply; + +}; + +} + +#endif // OWNCLOUDDIRCHECK_H diff --git a/src/mirall/owncloudinfo.cpp b/src/mirall/owncloudinfo.cpp index bc006e90a6..162525dd81 100644 --- a/src/mirall/owncloudinfo.cpp +++ b/src/mirall/owncloudinfo.cpp @@ -13,8 +13,11 @@ */ #include "owncloudinfo.h" -#include "QtCore" -#include "QtGui" + +#include +#include +#include + namespace Mirall { @@ -45,7 +48,9 @@ bool ownCloudInfo::isConfigured() QString ownCloudInfo::url() const { QSettings settings( configFile(), QSettings::IniFormat ); - return settings.value("ownCloud/url" ).toString(); + QString url = settings.value("ownCloud/url").toString(); + if( url.endsWith( QChar('/')) ) url.remove( -1, 1); + return url; } QString ownCloudInfo::user() const @@ -74,6 +79,14 @@ void ownCloudInfo::checkInstallation() connect( _reply, SIGNAL( readyRead()), this, SLOT(slotReadyRead())); } +void ownCloudInfo::slotAuthentication( QNetworkReply*, QAuthenticator *auth ) +{ + if( auth ) { + qDebug() << "Authenticating request!"; + auth->setUser( user() ); + auth->setPassword( password() ); + } +} void ownCloudInfo::slotReplyFinished( QNetworkReply *reply ) @@ -81,6 +94,8 @@ void ownCloudInfo::slotReplyFinished( QNetworkReply *reply ) const QString version( _readBuffer ); const QString url = reply->url().toString(); + emit ownCloudInfoReply( url, reply->error() ); + QString info( version ); info.remove(0,1); // remove first char which is a "{" diff --git a/src/mirall/owncloudinfo.h b/src/mirall/owncloudinfo.h index 715722eeba..95a4fdbf79 100644 --- a/src/mirall/owncloudinfo.h +++ b/src/mirall/owncloudinfo.h @@ -40,13 +40,15 @@ signals: void ownCloudInfoFound( const QString&, const QString& ); void noOwncloudFound(); + void ownCloudInfoReply( const QString&, QNetworkReply::NetworkError ); + public slots: protected slots: void slotReplyFinished( QNetworkReply* ); void slotReadyRead(); void slotError( QNetworkReply::NetworkError ); - + void slotAuthentication( QNetworkReply*, QAuthenticator *); private: QNetworkReply *_reply; QByteArray _readBuffer;