From 6b5b9db20a71436d8a62f2d7781b2fffaedbc5ca Mon Sep 17 00:00:00 2001 From: Klaas Freitag Date: Fri, 5 Jul 2013 18:46:43 +0200 Subject: [PATCH] Handle progress through ProgressDispatcher class, WIP. --- src/CMakeLists.txt | 2 + src/mirall/accountsettings.cpp | 39 ++++++++++--------- src/mirall/accountsettings.h | 3 +- src/mirall/application.cpp | 2 + src/mirall/csyncthread.cpp | 23 ++++++++--- src/mirall/csyncthread.h | 11 ++++-- src/mirall/owncloudfolder.cpp | 23 ++++++++++- src/mirall/owncloudfolder.h | 4 ++ src/mirall/progressdispatcher.cpp | 46 ++++++++++++++++++++++ src/mirall/progressdispatcher.h | 63 +++++++++++++++++++++++++++++++ src/mirall/settingsdialog.cpp | 13 +++++-- src/mirall/settingsdialog.h | 4 +- 12 files changed, 198 insertions(+), 35 deletions(-) create mode 100644 src/mirall/progressdispatcher.cpp create mode 100644 src/mirall/progressdispatcher.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ddcf9294c..ca1437802 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -76,6 +76,7 @@ set(libsync_SRCS mirall/logger.cpp mirall/utility.cpp mirall/connectionvalidator.cpp + mirall/progressdispatcher.cpp ) set(libsync_HEADERS mirall/folderman.h @@ -89,6 +90,7 @@ set(libsync_HEADERS mirall/credentialstore.h mirall/logger.h mirall/connectionvalidator.h + mirall/progressdispatcher.h ) IF( INOTIFY_FOUND ) diff --git a/src/mirall/accountsettings.cpp b/src/mirall/accountsettings.cpp index 58c874ba3..add793140 100644 --- a/src/mirall/accountsettings.cpp +++ b/src/mirall/accountsettings.cpp @@ -459,25 +459,28 @@ void AccountSettings::slotOpenOC() QDesktopServices::openUrl( _OCUrl ); } -void AccountSettings::slotSetProgress( const QString& folder, const QString& file, long p1, long p2 ) +void AccountSettings::slotSetProgress( Progress::Kind, const QString& folder, const QString& file, long p1, long p2 ) { -// if( p1 == 0 && p2 > 0 ) { -// // sync start -// ui->progressBar->setMaximum( p2 ); -// ui->progressBar->setValue( p1 ); -// ui->progressBar->setEnabled(true); -// ui->fileProgressLabel->setText(tr("Uploading %1").arg(file)); -// // ui->progressBar->show(); -// } else if( p1 == p2 ) { -// // sync end -// // ui->progressBar->setMaximum(0); -// ui->progressBar->setValue(0); -// ui->progressBar->setEnabled(false); -// ui->fileProgressLabel->setText(tr("No activity.")); -// // ui->progressBar->hide(); -// } else { -// ui->progressBar->setValue( p1 ); -// } + qDebug() << "================================> Progress for folder " << folder << " file " << file << ": "<< p1; +#if 0 + if( p1 == 0 && p2 > 0 ) { + // sync start + ui->progressBar->setMaximum( p2 ); + ui->progressBar->setValue( p1 ); + ui->progressBar->setEnabled(true); + ui->fileProgressLabel->setText(tr("Uploading %1").arg(file)); + // ui->progressBar->show(); + } else if( p1 == p2 ) { + // sync end + // ui->progressBar->setMaximum(0); + ui->progressBar->setValue(0); + ui->progressBar->setEnabled(false); + ui->fileProgressLabel->setText(tr("No activity.")); + // ui->progressBar->hide(); + } else { + ui->progressBar->setValue( p1 ); + } +#endif } void AccountSettings::slotUpdateQuota(qint64 total, qint64 used) diff --git a/src/mirall/accountsettings.h b/src/mirall/accountsettings.h index fc1bff1fd..f4099ffa6 100644 --- a/src/mirall/accountsettings.h +++ b/src/mirall/accountsettings.h @@ -19,6 +19,7 @@ #include #include "mirall/folder.h" +#include "mirall/progressdispatcher.h" class QStandardItemModel; class QModelIndex; @@ -61,7 +62,7 @@ public slots: void slotOCInfoFail( QNetworkReply* ); void slotDoubleClicked( const QModelIndex& ); void slotFolderOpenAction( const QString& ); - void slotSetProgress( const QString&, const QString&, long, long ); + void slotSetProgress( Progress::Kind, const QString&, const QString&, long, long ); void slotUpdateQuota( qint64,qint64 ); protected slots: diff --git a/src/mirall/application.cpp b/src/mirall/application.cpp index 9f124747a..ac84d5648 100644 --- a/src/mirall/application.cpp +++ b/src/mirall/application.cpp @@ -134,6 +134,8 @@ Application::Application(int &argc, char **argv) : setQuitOnLastWindowClosed(false); + qRegisterMetaType("Progress::Kind"); + #if 0 #if QT_VERSION >= 0x040700 qDebug() << "* Network is" << (_networkMgr->isOnline() ? "online" : "offline"); diff --git a/src/mirall/csyncthread.cpp b/src/mirall/csyncthread.cpp index 09397d2fa..8293bd09c 100644 --- a/src/mirall/csyncthread.cpp +++ b/src/mirall/csyncthread.cpp @@ -315,11 +315,12 @@ void CSyncThread::startSync() // cleans up behind us and emits finished() to ease error handling CSyncRunScopeHelper helper(_csync_ctx, this); + csync_set_progress_callback( _csync_ctx, cb_progress ); + csync_set_module_property(_csync_ctx, "csync_context", _csync_ctx); csync_set_userdata(_csync_ctx, this); // csync_set_auth_callback( _csync_ctx, getauth ); - csync_set_progress_callback( _csync_ctx, progress ); qDebug() << "#### Update start #################################################### >>"; if( csync_update(_csync_ctx) < 0 ) { @@ -373,14 +374,24 @@ void CSyncThread::startSync() qDebug() << Q_FUNC_INFO << "Sync finished"; } -void CSyncThread::progress(const char *remote_url, enum csync_notify_type_e kind, - long long o1, long long o2, void *userdata) +void CSyncThread::cb_progress(const char *remote_url, enum csync_notify_type_e kind, + long long o1, long long o2, void *userdata) { - (void) o1; (void) o2; + QString path = QUrl::fromEncoded(remote_url).toString(); + CSyncThread *thread = static_cast(userdata); + if (kind == CSYNC_NOTIFY_FINISHED_DOWNLOAD) { - QString path = QUrl::fromEncoded(remote_url).toString(); - CSyncThread *thread = static_cast(userdata); thread->fileReceived(path); + } else if( kind == CSYNC_NOTIFY_START_UPLOAD ) { + thread->transmissionProgress( Progress::StartUpload, path, (long)0, (long)0 ); // indicate the upload start. + } else if( kind == CSYNC_NOTIFY_PROGRESS ) { + thread->transmissionProgress( Progress::Context, path, o1, o2 ); + } else if( kind == CSYNC_NOTIFY_FINISHED_UPLOAD ) { + thread->transmissionProgress( Progress::EndUpload, path, o2, o2 ); + } else if( kind == CSYNC_NOTIFY_START_DOWNLOAD ) { + thread->transmissionProgress( Progress::StartDownload, path, 0, 0 ); + } else if( kind == CSYNC_NOTIFY_FINISHED_DOWNLOAD ) { + thread->transmissionProgress( Progress::EndDownload, path, o2, o2 ); } } diff --git a/src/mirall/csyncthread.h b/src/mirall/csyncthread.h index c6caadb5a..350e2f4ab 100644 --- a/src/mirall/csyncthread.h +++ b/src/mirall/csyncthread.h @@ -26,6 +26,7 @@ #include #include "mirall/syncfileitem.h" +#include "mirall/progressdispatcher.h" class QProcess; @@ -50,6 +51,8 @@ signals: void csyncUnavailable(); void treeWalkResult(const SyncFileItemVector&); + void transmissionProgress( Progress::Kind, const QString&, long, long); + void csyncStateDbFile( const QString& ); void wipeDb(); @@ -60,10 +63,10 @@ signals: private: void handleSyncError(CSYNC *ctx, const char *state); - static void progress(const char *remote_url, - enum csync_notify_type_e kind, - long long o1, long long o2, - void *userdata); + static void cb_progress(const char *remote_url, + enum csync_notify_type_e kind, + long long o1, long long o2, + void *userdata); static int treewalkLocal( TREE_WALK_FILE*, void *); static int treewalkRemote( TREE_WALK_FILE*, void *); diff --git a/src/mirall/owncloudfolder.cpp b/src/mirall/owncloudfolder.cpp index c526e7af6..f8a0455a4 100644 --- a/src/mirall/owncloudfolder.cpp +++ b/src/mirall/owncloudfolder.cpp @@ -19,6 +19,7 @@ #include "mirall/credentialstore.h" #include "mirall/logger.h" #include "mirall/utility.h" +#include "mirall/progressdispatcher.h" #include @@ -152,8 +153,6 @@ void ownCloudFolder::setProxy() csync_set_module_property(_csync_ctx, "proxy_port", &proxyPort ); csync_set_module_property(_csync_ctx, "proxy_user", proxy.user().toUtf8().data() ); csync_set_module_property(_csync_ctx, "proxy_pwd" , proxy.password().toUtf8().data() ); - - csync_set_module_property(_csync_ctx, "csync_context", _csync_ctx); } } @@ -307,6 +306,8 @@ void ownCloudFolder::startSync(const QStringList &pathList) //blocking connection so the message box happens in this thread, but block the csync thread. connect(_csync, SIGNAL(aboutToRemoveAllFiles(SyncFileItem::Direction,bool*)), SLOT(slotAboutToRemoveAllFiles(SyncFileItem::Direction,bool*)), Qt::BlockingQueuedConnection); + connect(_csync, SIGNAL(transmissionProgress(Progress::Kind, QString,long,long)), + SLOT(slotTransmissionProgress(Progress::Kind, QString,long,long))); _thread->start(); QMetaObject::invokeMethod(_csync, "startSync", Qt::QueuedConnection); @@ -411,6 +412,24 @@ void ownCloudFolder::slotLocalPathChanged( const QString& dir ) } } +void ownCloudFolder::slotTransmissionProgress(Progress::Kind kind, const QString& file ,long p1, long p2) +{ + if( kind == Progress::StartDownload ) { + _progressKind = Progress::Download; + } + if( kind == Progress::StartUpload ) { + _progressKind = Progress::Upload; + } + + // qDebug() << "Upload Progress: " << file << p1 << p2; + ProgressDispatcher::instance()->setFolderProgress( _progressKind, alias(), file, p1, p2 ); + + if( kind == Progress::EndDownload || kind == Progress::EndUpload ) { + _progressKind = Progress::Inactive; + } +} + + // This removes the csync File database if the sync folder definition is removed // permanentely. This is needed to provide a clean startup again in case another // local folder is synced to the same ownCloud. diff --git a/src/mirall/owncloudfolder.h b/src/mirall/owncloudfolder.h index f564d37e8..adf543fa6 100644 --- a/src/mirall/owncloudfolder.h +++ b/src/mirall/owncloudfolder.h @@ -21,6 +21,7 @@ #include "mirall/folder.h" #include "mirall/csyncthread.h" +#include "mirall/progressdispatcher.h" class QProcess; class QTimer; @@ -89,6 +90,8 @@ private slots: void slotCsyncUnavailable(); void slotCSyncFinished(); + void slotTransmissionProgress(Progress::Kind, const QString&,long, long); + private: static int getauth(const char *prompt, char *buf, @@ -109,6 +112,7 @@ private: bool _csyncUnavail; bool _wipeDb; SyncFileItemVector _items; + Progress::Kind _progressKind; CSYNC *_csync_ctx; }; diff --git a/src/mirall/progressdispatcher.cpp b/src/mirall/progressdispatcher.cpp new file mode 100644 index 000000000..848bbe202 --- /dev/null +++ b/src/mirall/progressdispatcher.cpp @@ -0,0 +1,46 @@ +/* + * Copyright (C) by Klaas Freitag + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#include "progressdispatcher.h" + +#include + + +namespace Mirall { + +ProgressDispatcher* ProgressDispatcher::_instance = 0; + +ProgressDispatcher* ProgressDispatcher::instance() { + if (!_instance) { + _instance = new ProgressDispatcher(); + } + return _instance; +} + +ProgressDispatcher::ProgressDispatcher(QObject *parent) : + QObject(parent) +{ + +} + +ProgressDispatcher::~ProgressDispatcher() +{ + +} + +void ProgressDispatcher::setFolderProgress( Progress::Kind kind, const QString& alias, const QString& folder, long p1, long p2) +{ + emit folderProgress( kind, alias, folder, p1, p2 ); +} + +} diff --git a/src/mirall/progressdispatcher.h b/src/mirall/progressdispatcher.h new file mode 100644 index 000000000..b03c3783d --- /dev/null +++ b/src/mirall/progressdispatcher.h @@ -0,0 +1,63 @@ +/* + * Copyright (C) by Klaas Freitag + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#ifndef PROGRESSDISPATCHER_H +#define PROGRESSDISPATCHER_H + +#include + +namespace Mirall { + + +/** + * @brief The FolderScheduler class schedules folders for sync + */ +class Progress +{ +public: + enum ProgressKind_s { + Download, + Upload, + Context, + Inactive, + StartDownload, + StartUpload, + EndDownload, + EndUpload + }; + typedef ProgressKind_s Kind; +}; + +class ProgressDispatcher : public QObject +{ + Q_OBJECT +public: + static ProgressDispatcher* instance(); + ~ProgressDispatcher(); + +public: + void setFolderProgress( Progress::Kind, const QString&, const QString&, long, long ); + +signals: + void folderProgress( Progress::Kind, const QString&, const QString&, long, long ); + +public slots: + +private: + ProgressDispatcher(QObject* parent = 0); + + static ProgressDispatcher* _instance; +}; + +} +#endif // PROGRESSDISPATCHER_H diff --git a/src/mirall/settingsdialog.cpp b/src/mirall/settingsdialog.cpp index 4f3510c6e..634fea87c 100644 --- a/src/mirall/settingsdialog.cpp +++ b/src/mirall/settingsdialog.cpp @@ -20,6 +20,7 @@ #include "mirall/application.h" #include "mirall/ignorelisteditor.h" #include "mirall/mirallconfigfile.h" +#include "mirall/progressdispatcher.h" #include #include @@ -71,6 +72,12 @@ SettingsDialog::SettingsDialog(Application *app, QWidget *parent) : connect( _accountSettings, SIGNAL(openFolderAlias(const QString&)), app, SLOT(slotFolderOpenAction(QString))); + connect( ProgressDispatcher::instance(), SIGNAL(folderProgress(Progress::Kind, QString,QString,long,long)), + this, SLOT(slotFolderProgress(Progress::Kind, QString,QString,long,long))); + + connect(ProgressDispatcher::instance(), SIGNAL(shortFolderProgress(QString, QString)), + this, SLOT(slotShortFolderProgress(QString, QString))); + _ui->labelWidget->setCurrentRow(_ui->labelWidget->row(general)); connect(_ui->labelWidget, SIGNAL(currentRowChanged(int)), @@ -98,10 +105,10 @@ void SettingsDialog::addAccount(const QString &title, QWidget *widget) } -void SettingsDialog::slotFolderUploadProgress( const QString& folderAlias, const QString& file, long p1, long p2) +void SettingsDialog::slotFolderProgress( Progress::Kind kind, const QString& folderAlias, const QString& file, long p1, long p2) { - qDebug() << " SettingsDialog: XX - File " << file << p1 << p2; - _accountSettings->slotSetProgress(folderAlias, file, p1, p2); + qDebug() << " SettingsDialog: XXOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO - File " << file << p1 << p2; + _accountSettings->slotSetProgress(kind, folderAlias, file, p1, p2); } void SettingsDialog::closeEvent(QCloseEvent *event) diff --git a/src/mirall/settingsdialog.h b/src/mirall/settingsdialog.h index 6f0f4478b..22ed2544d 100644 --- a/src/mirall/settingsdialog.h +++ b/src/mirall/settingsdialog.h @@ -17,6 +17,8 @@ #include #include +#include "mirall/progressdispatcher.h" + class QStandardItemModel; class QListWidgetItem; @@ -41,7 +43,7 @@ public slots: // Progress, parameter is // - filename // - progress bytes, overall size. - void slotFolderUploadProgress( const QString&, const QString&, long, long ); + void slotFolderProgress( Progress::Kind, const QString&, const QString&, long, long ); protected: void closeEvent(QCloseEvent *event);