From af5b99cfb3c641c5597db56fef191995cd662f09 Mon Sep 17 00:00:00 2001 From: shibakaneki Date: Fri, 4 Nov 2011 14:21:16 +0100 Subject: [PATCH] New version od the download manager coded --- resources/style.qss | 2 +- src/board/UBBoardController.cpp | 24 +- src/board/UBBoardController.h | 83 +------ src/board/UBBoardView.cpp | 7 +- src/core/UBDownloadManager.cpp | 366 ++++++++++++++++++++++++++++++ src/core/UBDownloadManager.h | 106 +++++++++ src/core/UBDownloadThread.cpp | 115 ++++++++++ src/core/UBDownloadThread.h | 56 +++++ src/core/UBPersistenceManager.cpp | 4 - src/core/core.pri | 8 +- src/gui/UBDownloadWidget.cpp | 203 +++++++++++++++++ src/gui/UBDownloadWidget.h | 69 ++++++ src/gui/UBLibWebView.cpp | 1 - src/gui/UBMainWindow.cpp | 25 +- src/gui/UBMainWindow.h | 7 + src/gui/gui.pri | 6 +- 16 files changed, 987 insertions(+), 95 deletions(-) create mode 100644 src/core/UBDownloadManager.cpp create mode 100644 src/core/UBDownloadManager.h create mode 100644 src/core/UBDownloadThread.cpp create mode 100644 src/core/UBDownloadThread.h create mode 100644 src/gui/UBDownloadWidget.cpp create mode 100644 src/gui/UBDownloadWidget.h diff --git a/resources/style.qss b/resources/style.qss index 8288ce28..d905628e 100644 --- a/resources/style.qss +++ b/resources/style.qss @@ -19,7 +19,7 @@ QWidget#UBLibWebView QWebView#SearchEngineView { - background:#EEEEEE; + background:white; } QColorDialog diff --git a/src/board/UBBoardController.cpp b/src/board/UBBoardController.cpp index 6efd102d..059883e0 100644 --- a/src/board/UBBoardController.cpp +++ b/src/board/UBBoardController.cpp @@ -27,6 +27,7 @@ #include "core/UBApplicationController.h" #include "core/UBDocumentManager.h" #include "core/UBMimeData.h" +#include "core/UBDownloadManager.h" #include "network/UBHttpGet.h" @@ -128,6 +129,9 @@ void UBBoardController::init() connect(UBApplication::app(), SIGNAL(lastWindowClosed()) , this, SLOT(lastWindowClosed())); + connect(UBDownloadManager::downloadManager(), SIGNAL(downloadModalFinished()), this, SLOT(onDownloadModalFinished())); + connect(UBDownloadManager::downloadManager(), SIGNAL(addDownloadedFileToBoard(bool,QUrl,QString,QByteArray,QPointF,QSize,bool)), this, SLOT(downloadFinished(bool,QUrl,QString,QByteArray,QPointF,QSize,bool))); + UBDocumentProxy* doc = UBPersistenceManager::persistenceManager()->createDocument(); setActiveDocumentScene(doc); @@ -751,13 +755,24 @@ void UBBoardController::downloadURL(const QUrl& url, const QPointF& pPos, const } else { - UBHttpGet *http = new UBHttpGet(mActiveScene); + // When we fall there, it means that we are dropping something from the web to the board + sDownloadFileDesc desc; + desc.modal = true; + desc.url = url.toString(); + desc.currentSize = 0; + desc.name = QFileInfo(url.toString()).fileName(); + desc.totalSize = 0; // The total size will be retrieved during the download + desc.pos = pPos; + desc.size = pSize; + desc.isBackground = isBackground; - showMessage(tr("Downloading content from %1").arg(url.toString()), true); + // INFO: DO NOT UNCOMMENT THE NEXT LINE! DEVELOPMENT IN PROGRESS +// UBDownloadManager::downloadManager()->addFileToDownload(desc); + UBHttpGet *http = new UBHttpGet(mActiveScene); + showMessage(tr("Downloading content from %1").arg(url.toString()), true); connect(http, SIGNAL(downloadFinished(bool, QUrl, QString, QByteArray, QPointF, QSize, bool)), this, SLOT(downloadFinished(bool, QUrl, QString, QByteArray, QPointF, QSize, bool))); - http->get(url, pPos, pSize, isBackground); } } @@ -1980,5 +1995,8 @@ void UBBoardController::notifyPageChanged() emit pageChanged(); } +void UBBoardController::onDownloadModalFinished() +{ +} diff --git a/src/board/UBBoardController.h b/src/board/UBBoardController.h index ef2564f6..18283c8c 100644 --- a/src/board/UBBoardController.h +++ b/src/board/UBBoardController.h @@ -49,15 +49,12 @@ class UBBoardController : public QObject void init(); void setupLayout(); - UBDocumentProxy* activeDocument() const; UBGraphicsScene* activeScene() const; int activeSceneIndex() const; - QSize displayViewport(); QSize controlViewport(); QRectF controlGeometry(); - void closing(); UBDocumentProxy* activeDocument() @@ -137,21 +134,13 @@ class UBBoardController : public QObject { return mSystemScaleFactor; } - qreal currentZoom(); - void persistCurrentScene(); - void showNewVersionAvailable(bool automatic, const UBVersion &installedVersion, const UBSoftwareUpdate &softwareUpdate); - void setBoxing(QRect displayRect); - void setToolbarTexts(); - static QUrl expandWidgetToTempDir(const QByteArray& pZipedData, const QString& pExtension = QString("wgt")); - void setPageSize(QSize newSize); - UBBoardPaletteManager *paletteManager() { return mPaletteManager; @@ -161,102 +150,68 @@ class UBBoardController : public QObject void notifyPageChanged(); public slots: - void setActiveDocumentScene(UBDocumentProxy* pDocumentProxy, int pSceneIndex = 0); - void showDocumentsDialog(); - void showKeyboard(bool show); - void togglePodcast(bool checked); - void blackout(); - void addScene(); void addScene(UBDocumentProxy* proxy, int sceneIndex, bool replaceActiveIfEmpty = false); void addScene(UBGraphicsScene* scene, bool replaceActiveIfEmpty = false); void duplicateScene(); void importPage(); - void clearScene(); void clearSceneItems(); void clearSceneAnnotation(); - void zoomIn(QPointF scenePoint = QPointF(0,0)); void zoomOut(QPointF scenePoint = QPointF(0,0)); void zoomRestore(); void centerRestore(); void centerOn(QPointF scenePoint = QPointF(0,0)); - void zoom(const qreal ratio, QPointF scenePoint); void handScroll(qreal dx, qreal dy); - void previousScene(); void nextScene(); void firstScene(); void lastScene(); - - void downloadURL(const QUrl& url, const QPointF& pPos = QPointF(0.0, 0.0), - const QSize& pSize = QSize(), bool isBackground = false); - - void downloadFinished(bool pSuccess, QUrl sourceUrl, QString pHeader, - QByteArray pData, QPointF pPos, QSize pSize, bool isBackground = false); - + void downloadURL(const QUrl& url, const QPointF& pPos = QPointF(0.0, 0.0), const QSize& pSize = QSize(), bool isBackground = false); + void downloadFinished(bool pSuccess, QUrl sourceUrl, QString pHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground = false); void changeBackground(bool isDark, bool isCrossed); - void setToolCursor(int tool); - void showMessage(const QString& message, bool showSpinningWheel); void hideMessage(); - void setDisabled(bool disable); - void setColorIndex(int pColorIndex); - UBToolWidget* addTool(const QUrl& toolUrl, QPointF scenePos); UBToolWidget* addTool(const QUrl& toolUrl); void removeTool(UBToolWidget* toolWidget); - void hide(); void show(); - void setWidePageSize(bool checked); void setRegularPageSize(bool checked); - void stylusToolChanged(int tool); - void grabScene(const QRectF& pSceneRect); - void controlViewHidden(); void controlViewShown(); - UBGraphicsVideoItem* addVideo(const QUrl& pUrl, bool startPlay, const QPointF& pos); UBGraphicsAudioItem* addAudio(const QUrl& pUrl, bool startPlay, const QPointF& pos); - void cut(); void copy(); void paste(); - void processMimeData(const QMimeData* pMimeData, const QPointF& pPos); - void moveGraphicsWidgetToControlView(UBGraphicsWidgetItem* graphicWidget); void moveToolWidgetToScene(UBToolWidget* toolWidget); - void addItem(); signals: - void activeSceneWillBePersisted(); void activeSceneWillChange(); void activeSceneChanged(); - void activeDocumentChanged(); - void zoomChanged(qreal pZoomFactor); void systemScaleFactorChanged(qreal pSystemScaleFactor); void penColorChanged(); void controlViewportChanged(); - void backgroundChanged(); void cacheEnabled(); void cacheDisabled(); @@ -265,82 +220,56 @@ class UBBoardController : public QObject void documentReorganized(int index); protected: - void setupViews(); void setupToolbar(); void connectToolbar(); void initToolbarTexts(); - void updateActionStates(); void updateSystemScaleFactor(); - QString truncate(QString text, int maxWidth); protected slots: - void selectionChanged(); - void undoRedoStateChange(bool canUndo); - void documentSceneChanged(UBDocumentProxy* proxy, int pIndex); private: + void updatePageSizeState(); + void saveViewState(); + void adjustDisplayViews(); UBMainWindow *mMainWindow; - UBDocumentProxy* mActiveDocument; UBGraphicsScene* mActiveScene; int mActiveSceneIndex; - UBBoardPaletteManager *mPaletteManager; - UBSoftwareUpdateDialog *mSoftwareUpdateDialog; - UBMessageWindow *mMessageWindow; - UBBoardView *mControlView; UBBoardView *mDisplayView; - QWidget *mControlContainer; QHBoxLayout *mControlLayout; - qreal mZoomFactor; - bool mIsClosing; - QColor mPenColorOnDarkBackground; QColor mPenColorOnLightBackground; QColor mMarkerColorOnDarkBackground; QColor mMarkerColorOnLightBackground; - QList mTools; - qreal mSystemScaleFactor; - - void updatePageSizeState(); - void saveViewState(); - void adjustDisplayViews(); - bool mCleanupDone; - QMap > mActionTexts; private slots: - void stylusToolDoubleClicked(int tool); - void boardViewResized(QResizeEvent* event); - void documentWillBeDeleted(UBDocumentProxy* pProxy); - void updateBackgroundActionsState(bool isDark, bool isCrossed); void updateBackgroundState(); - void colorPaletteChanged(); - void libraryDialogClosed(int ret); - void lastWindowClosed(); + void onDownloadModalFinished(); }; diff --git a/src/board/UBBoardView.cpp b/src/board/UBBoardView.cpp index 2cb28ef9..64511330 100644 --- a/src/board/UBBoardView.cpp +++ b/src/board/UBBoardView.cpp @@ -726,11 +726,12 @@ UBBoardView::dragMoveEvent (QDragMoveEvent *event) void UBBoardView::dropEvent (QDropEvent *event) { - if(!event->source() || dynamic_cast(event->source())) - { + qDebug() << event->source(); + if(!event->source() || dynamic_cast(event->source()) || dynamic_cast(event->source())) + { mController->processMimeData (event->mimeData (), mapToScene (event->pos ())); event->acceptProposedAction (); - } + } } void diff --git a/src/core/UBDownloadManager.cpp b/src/core/UBDownloadManager.cpp new file mode 100644 index 00000000..9c581eed --- /dev/null +++ b/src/core/UBDownloadManager.cpp @@ -0,0 +1,366 @@ +/* + * 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, either version 3 of the License, or + * (at your option) any later version. + * + * 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. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include "UBDownloadManager.h" +#include "core/UBApplication.h" +#include "gui/UBMainWindow.h" + +/** The unique instance of the download manager */ +static UBDownloadManager* pInstance = NULL; + +/** + * \brief Constructor + * @param parent as the parent widget + * @param name as the object name + */ +UBDownloadManager::UBDownloadManager(QObject *parent, const char *name):QObject(parent) +{ + setObjectName(name); + init(); + + connect(this, SIGNAL(fileAddedToDownload()), this, SLOT(onUpdateDownloadLists())); +} + +/** + * \brief Destructor + */ +UBDownloadManager::~UBDownloadManager() +{ + +} + +/** + * \brief Get the download manager + * @return a pointer on the download manager + */ +UBDownloadManager* UBDownloadManager::downloadManager() +{ + if(NULL == pInstance) + { + pInstance = new UBDownloadManager(); + } + return pInstance; +} + +/** + * \brief Add a file to the download list + * @param desc as the given file description + */ +void UBDownloadManager::addFileToDownload(sDownloadFileDesc desc) +{ + // Set the ID for this download + desc.id = mLastID; + mLastID++; + + // Add the file to the pending download list + mPendingDL.append(desc); + + // If the download is modal, show the download dialog + if(desc.modal) + { + // Update the download order (priority to modal files) + updateDownloadOrder(); + UBApplication::mainWindow->showDownloadWidget(); + } + + emit fileAddedToDownload(); +} + +/** + * \brief Initialize the download manager + */ +void UBDownloadManager::init() +{ + mCrntDL.clear(); + mPendingDL.clear(); + mLastID = 1; + for(int i=0; i modalFiles; + QVector nonModalfiles; + + for(int i=0; i UBDownloadManager::currentDownloads() +{ + return mCrntDL; +} + +/** + * \brief Get the list of the pending downloads + * @return a QVector of pending downloads + */ +QVector UBDownloadManager::pendingDownloads() +{ + return mPendingDL; +} + +/** + * \brief Update the file transfer information + * @param desc as the current downloaded file description + */ +void UBDownloadManager::onDownloadProgress(int id, qint64 received, qint64 total) +{ + updateFileCurrentSize(id, received, total); +} + +/** + * \brief Called when the download of the given file is finished + * @param desc as the current downloaded file description + */ +void UBDownloadManager::onDownloadFinished(int id, bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground) +{ + for(int i=0; i= 0 && total >= 0) + { + // ------------------------------------- + // [=============== x% ==== ] + // ------------------------------------- + desc.currentSize = received; + desc.totalSize = total; + + emit downloadUpdated(id, received, total); + } + else + { + // ------------------------------------- + // [=============== 100% ==============] + // ------------------------------------- + // received and total are negative. That means that the download is finished + desc.currentSize = mCrntDL.at(i).totalSize; + + // Remove the finished file from the current download list + mCrntDL.remove(i); + + // Free the download slot used by the finished file + for(int j=0; jget(QUrl(desc.url)); +} + +/** + * \brief Verify if modal downloads remains and notify everyone if it is not the case. + */ +void UBDownloadManager::checkIfModalRemains() +{ + bool bModal = false; + for(int i=0; ihideDownloadWidget(); + + // Notify that no modal downloads are pending + emit downloadModalFinished(); + } +} + +/** + * \brief Cancel all downloads + */ +void UBDownloadManager::cancelDownloads() +{ + // Stop the current downloads + + + // Notify everyone that the downloads have been canceled. + emit cancelAllDownloads(); +} + +// ------------------------------------------------------------------------------ +/** + * \brief Constructor + * @param parent as the parent widget + * @param name as the object name + */ +UBDownloadHttpFile::UBDownloadHttpFile(int fileId, QObject *parent):UBHttpGet(parent) +{ + mId = fileId; + + connect(this, SIGNAL(downloadFinished(bool,QUrl,QString,QByteArray,QPointF,QSize,bool)), this, SLOT(onDownloadFinished(bool,QUrl,QString,QByteArray,QPointF,QSize,bool))); + connect(this, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(onDownloadProgress(qint64,qint64))); +} + +/** + * \brief Destructor + */ +UBDownloadHttpFile::~UBDownloadHttpFile() +{ + +} + +/** + * \brief Handles the download progress notification + * @param bytesReceived as the number of received bytes + * @param bytesTotal as the total number of bytes + */ +void UBDownloadHttpFile::onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) +{ + emit downloadProgress(mId, bytesReceived, bytesTotal); +} + +/** + * \brief Handles the download finished notification + * @param pSuccess as the success indicator + * @param sourceUrl as the source URL + * @param pContentTypeHeader as the response content type header + * @param pData as the packet data + * @param pPos as the item position in the board + * @param psize as the item size (GUI) + * @param isBackground as the background mdoe indicator + */ +void UBDownloadHttpFile::onDownloadFinished(bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground) +{ + emit downloadFinished(mId, pSuccess, sourceUrl, pContentTypeHeader, pData, pPos, pSize, isBackground); +} + diff --git a/src/core/UBDownloadManager.h b/src/core/UBDownloadManager.h new file mode 100644 index 00000000..bc273100 --- /dev/null +++ b/src/core/UBDownloadManager.h @@ -0,0 +1,106 @@ +/* + * 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, either version 3 of the License, or + * (at your option) any later version. + * + * 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. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef UBDOWNLOADMANAGER_H +#define UBDOWNLOADMANAGER_H + +#include +#include +#include +#include + +#include "UBDownloadThread.h" + +#include "network/UBHttpGet.h" + +#define SIMULTANEOUS_DOWNLOAD 2 // Maximum 5 because of QNetworkAccessManager limitation!!! + +typedef struct +{ + QString name; + int id; + int totalSize; + int currentSize; + QString url; + bool modal; + QPointF pos; // For board drop only + QSize size; // For board drop only + bool isBackground; // For board drop only +}sDownloadFileDesc; + +class UBDownloadHttpFile : public UBHttpGet +{ + Q_OBJECT +public: + UBDownloadHttpFile(int fileId, QObject* parent=0); + ~UBDownloadHttpFile(); + +signals: + void downloadProgress(int id, qint64 current,qint64 total); + void downloadFinished(int id, bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground); + +private slots: + void onDownloadFinished(bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground); + void onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal); + +private: + int mId; +}; + +class UBDownloadManager : public QObject +{ + Q_OBJECT +public: + UBDownloadManager(QObject* parent=0, const char* name="UBDownloadManager"); + ~UBDownloadManager(); + + static UBDownloadManager* downloadManager(); + void addFileToDownload(sDownloadFileDesc desc); + QVector currentDownloads(); + QVector pendingDownloads(); + void cancelDownloads(); + +signals: + void fileAddedToDownload(); + void downloadUpdated(int id, qint64 crnt, qint64 total); + void downloadFinished(int id); + void downloadModalFinished(); + void addDownloadedFileToBoard(bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground); + void cancelAllDownloads(); + +private slots: + void onUpdateDownloadLists(); + void onDownloadProgress(int id, qint64 received, qint64 total); + void onDownloadFinished(int id, bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground); + +private: + void init(); + void updateDownloadOrder(); + void updateFileCurrentSize(int id, qint64 received=-1, qint64 total=-1); + void startFileDownload(sDownloadFileDesc desc); + void checkIfModalRemains(); + + /** The current downloads */ + QVector mCrntDL; + /** The pending downloads */ + QVector mPendingDL; + /** Pending download mutex */ + QMutex mMutex; + /** The last file ID */ + int mLastID; + /** The current download availability (-1 = free, otherwise the file ID is recorded)*/ + QVector mDLAvailability; +}; + +#endif // UBDOWNLOADMANAGER_H diff --git a/src/core/UBDownloadThread.cpp b/src/core/UBDownloadThread.cpp new file mode 100644 index 00000000..08a57906 --- /dev/null +++ b/src/core/UBDownloadThread.cpp @@ -0,0 +1,115 @@ +/* + * 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, either version 3 of the License, or + * (at your option) any later version. + * + * 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. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include +#include +#include + +#include "core/UBSettings.h" + +#include "UBDownloadThread.h" + +/** + * \brief Constructor + * @param parent as the parent object + * @param name as the object name + */ +UBDownloadThread::UBDownloadThread(QObject *parent, const char *name):QThread(parent) + , mbRun(false) + ,mpReply(NULL) +{ + setObjectName(name); +} + +/** + * \brief Destructor + */ +UBDownloadThread::~UBDownloadThread() +{ + if(NULL != mpReply) + { + delete mpReply; + mpReply = NULL; + } +} + +/** + * \brief Run the thread + */ +void UBDownloadThread::run() +{ + qDebug() << mUrl; + // We start the download + QNetworkAccessManager* pNam = new QNetworkAccessManager(); + + mpReply = pNam->get(QNetworkRequest(QUrl(mUrl))); + qDebug() << " -- Http GET reply ---------------------- "; + qDebug() << mpReply->readAll(); + qDebug() << " ---------------------------------------- "; + + connect(mpReply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(onDownloadProgress(qint64,qint64))); + connect(mpReply, SIGNAL(finished()), this, SLOT(onDownloadFinished())); + + while(mbRun) + { + // Wait here until the end of the download + sleep(100); + } + + disconnect(mpReply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(onDownloadProgress(qint64,qint64))); + disconnect(mpReply, SIGNAL(finished()), this, SLOT(onDownloadFinished())); + if(NULL != mpReply) + { + delete mpReply; + mpReply = NULL; + } +} + +/** + * \brief Stop the current download + */ +void UBDownloadThread::stopDownload() +{ + mbRun = false; +} + +/** + * \brief Start the download + */ +void UBDownloadThread::startDownload(int id, QString url) +{ + mID = id; + mUrl = url; + mbRun = true; + start(); +} + +/** + * \brief Notify the download progression + * @param received as the number of bytes received + * @param total as the total number of bytes of the file + */ +void UBDownloadThread::onDownloadProgress(qint64 received, qint64 total) +{ + qDebug() << received << " on " << total; + emit downloadProgress(mID, received, total); +} + +/** + * \brief Notify the end of the download + */ +void UBDownloadThread::onDownloadFinished() +{ + emit downloadFinised(mID); +} diff --git a/src/core/UBDownloadThread.h b/src/core/UBDownloadThread.h new file mode 100644 index 00000000..455bceca --- /dev/null +++ b/src/core/UBDownloadThread.h @@ -0,0 +1,56 @@ +/* + * 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, either version 3 of the License, or + * (at your option) any later version. + * + * 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. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef UBDOWNLOADTHREAD_H +#define UBDOWNLOADTHREAD_H + +#include +#include +#include +#include + +class UBDownloadThread : public QThread +{ + Q_OBJECT +public: + UBDownloadThread(QObject* parent=0, const char* name="UBDownloadThread"); + ~UBDownloadThread(); + void stopDownload(); + void startDownload(int id, QString url); + +signals: + void downloadFinised(int id); + void downloadProgress(int id, qint64 current, qint64 total); + +protected: + virtual void run(); + +private slots: + void onDownloadProgress(qint64 received, qint64 total); + void onDownloadFinished(); + +private: + /** Flag used to stop the thread */ + bool mbRun; + /** The downloaded file id */ + int mID; + /** The downloaded file url */ + QString mUrl; + /** The network access manager */ + QNetworkAccessManager* mpNam; + /** The network reply */ + QNetworkReply* mpReply; +}; + +#endif // UBDOWNLOADTHREAD_H diff --git a/src/core/UBPersistenceManager.cpp b/src/core/UBPersistenceManager.cpp index 8c2f3f95..46f0cb67 100644 --- a/src/core/UBPersistenceManager.cpp +++ b/src/core/UBPersistenceManager.cpp @@ -306,8 +306,6 @@ void UBPersistenceManager::deleteDocument(UBDocumentProxy* pDocumentProxy) emit documentWillBeDeleted(pDocumentProxy); - qDebug() << "Deleting document" << pDocumentProxy->persistencePath(); - UBFileSystemUtils::deleteDir(pDocumentProxy->persistencePath()); documentProxies.removeAll(QPointer(pDocumentProxy)); @@ -619,8 +617,6 @@ void UBPersistenceManager::persistDocumentScene(UBDocumentProxy* pDocumentProxy, QDir dir(pDocumentProxy->persistencePath()); dir.mkpath(pDocumentProxy->persistencePath()); - qDebug() << "saving page" << pSceneIndex + 1 << pDocumentProxy->persistencePath(); - if (pDocumentProxy->isModified()) UBMetadataDcSubsetAdaptor::persist(pDocumentProxy); diff --git a/src/core/core.pri b/src/core/core.pri index b514fb8d..c3462c83 100644 --- a/src/core/core.pri +++ b/src/core/core.pri @@ -10,7 +10,9 @@ HEADERS += src/core/UB.h \ src/core/UBIdleTimer.h \ src/core/UBDisplayManager.h \ src/core/UBDocumentManager.h \ - src/core/UBApplicationController.h + src/core/UBApplicationController.h \ + src/core/UBDownloadManager.h \ + src/core/UBDownloadThread.h SOURCES += src/core/main.cpp \ src/core/UBApplication.cpp \ @@ -23,6 +25,8 @@ SOURCES += src/core/main.cpp \ src/core/UBIdleTimer.cpp \ src/core/UBDisplayManager.cpp \ src/core/UBDocumentManager.cpp \ - src/core/UBApplicationController.cpp + src/core/UBApplicationController.cpp \ + src/core/UBDownloadManager.cpp \ + src/core/UBDownloadThread.cpp diff --git a/src/gui/UBDownloadWidget.cpp b/src/gui/UBDownloadWidget.cpp new file mode 100644 index 00000000..83b8b7bd --- /dev/null +++ b/src/gui/UBDownloadWidget.cpp @@ -0,0 +1,203 @@ +/* + * 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, either version 3 of the License, or + * (at your option) any later version. + * + * 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. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include +#include +#include +#include + +#include "UBDownloadWidget.h" +#include "core/UBApplication.h" + +/** + * \brief Constructor + * @param parent as the parent widget + * @param name as the widget object name + */ +UBDownloadWidget::UBDownloadWidget(QWidget *parent, const char *name):QWidget(parent) + , mpLayout(NULL) + , mpBttnLayout(NULL) + , mpTree(NULL) + , mpCancelBttn(NULL) + , mpItem(NULL) +{ + setObjectName(name); + setWindowTitle(tr("Downloading files")); + setAttribute(Qt::WA_StyledBackground, true); + setStyleSheet(UBApplication::globalStyleSheet()); + resize(400, 300); + + mpLayout = new QVBoxLayout(this); + setLayout(mpLayout); + + mpTree = new QTreeWidget(this); + mpTree->setRootIsDecorated(false); + mpTree->header()->close(); + mpLayout->addWidget(mpTree, 1); + + mpBttnLayout = new QHBoxLayout(); + mpBttnLayout->addStretch(1); + mpCancelBttn = new QPushButton(tr("Cancel"), this); + mpBttnLayout->addWidget(mpCancelBttn, 0); + mpLayout->addLayout(mpBttnLayout); + + connect(UBDownloadManager::downloadManager(), SIGNAL(fileAddedToDownload()), this, SLOT(onFileAddedToDownload())); + connect(UBDownloadManager::downloadManager(), SIGNAL(downloadUpdated(int,qint64,qint64)), this, SLOT(onDownloadUpdated(int,qint64,qint64))); + connect(UBDownloadManager::downloadManager(), SIGNAL(downloadFinished(int)), this, SLOT(onDownloadFinished(int))); + connect(mpCancelBttn, SIGNAL(clicked()), this, SLOT(onCancelClicked())); +} + +/** + * \brief Destructor + */ +UBDownloadWidget::~UBDownloadWidget() +{ + if(NULL != mpItem) + { + delete mpItem; + mpItem = NULL; + } + if(NULL != mpCancelBttn) + { + delete mpCancelBttn; + mpCancelBttn = NULL; + } + if(NULL != mpTree) + { + delete mpTree; + mpTree = NULL; + } + if(NULL != mpBttnLayout) + { + delete mpBttnLayout; + mpBttnLayout = NULL; + } + if(NULL != mpLayout) + { + delete mpLayout; + mpLayout = NULL; + } +} + +/** + * \brief Refresh the tree of downloaded files + */ +void UBDownloadWidget::onFileAddedToDownload() +{ + if(NULL != mpTree) + { + mpTree->clear(); + addCurrentDownloads(); + addPendingDownloads(); + } +} + +/** + * \brief Add the current downloads + */ +void UBDownloadWidget::addCurrentDownloads() +{ + QVector actualDL = UBDownloadManager::downloadManager()->currentDownloads(); + for(int i=0; isetText(0, actualDL.at(i).name); + mpItem->setData(0, Qt::UserRole, QVariant(actualDL.at(i).id)); + mpTree->addTopLevelItem(mpItem); + mpItem = new QTreeWidgetItem(mpTree); + mpItem->setData(0, Qt::UserRole, actualDL.at(i).currentSize); + mpItem->setData(0, Qt::UserRole + 1, actualDL.at(i).totalSize); + mpItem->setData(0, Qt::UserRole + 2, actualDL.at(i).id); + mpTree->addTopLevelItem(mpItem); + mpTree->setItemDelegateForRow(((i+1)*2)-1, &mProgressBarDelegate); + } +} + +/** + * \brief Add the pending downloads + */ +void UBDownloadWidget::addPendingDownloads() +{ + QVector pendingDL = UBDownloadManager::downloadManager()->pendingDownloads(); + for(int i=0; isetText(0, pendingDL.at(i).name); + mpItem->setData(0, Qt::UserRole, QVariant(pendingDL.at(i).id)); + mpTree->addTopLevelItem(mpItem); + } +} + +/** + * \brief Update the progress bar + * @param id as the downloaded file id + * @param crnt as the current transfered size + * @param total as the total size of the file + */ +void UBDownloadWidget::onDownloadUpdated(int id, qint64 crnt, qint64 total) +{ + if(NULL != mpTree) + { + QAbstractItemModel* model = mpTree->model(); + if(NULL != model) + { + for(int i=0; i< model->rowCount(); i++) + { + QModelIndex currentIndex = model->index(i, 0); + if(id == currentIndex.data(Qt::UserRole + 2)) + { + // We found the right item, now we update the progress bar + model->setData(currentIndex, crnt, Qt::UserRole); + model->setData(currentIndex, total, Qt::UserRole + 1); + break; + } + } + } + } +} + +/** + * \brief Handles the download finish notification + * @param id as the downloaded file id + */ +void UBDownloadWidget::onDownloadFinished(int id) +{ + // Refresh the file's list + onFileAddedToDownload(); +} + +/** + * \brief Handles the Cancel button action + */ +void UBDownloadWidget::onCancelClicked() +{ + +} + +// --------------------------------------------------------------------------------------------- +UBDownloadProgressDelegate::UBDownloadProgressDelegate(QObject *parent):QItemDelegate(parent) +{ + +} + +void UBDownloadProgressDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const +{ + QStyleOptionProgressBarV2 opt; + opt.rect = option.rect; + opt.minimum = 0; + opt.maximum = index.data(Qt::UserRole + 1).toInt(); + opt.progress = index.data(Qt::UserRole).toInt(); + + QApplication::style()->drawControl(QStyle::CE_ProgressBar, &opt, painter, 0); +} diff --git a/src/gui/UBDownloadWidget.h b/src/gui/UBDownloadWidget.h new file mode 100644 index 00000000..0819da7c --- /dev/null +++ b/src/gui/UBDownloadWidget.h @@ -0,0 +1,69 @@ +/* + * 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, either version 3 of the License, or + * (at your option) any later version. + * + * 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. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef UBDOWNLOADWIDGET_H +#define UBDOWNLOADWIDGET_H + +#include +#include +#include +#include +#include +#include +#include + +#include "core/UBDownloadManager.h" + +class UBDownloadProgressDelegate : public QItemDelegate +{ + Q_OBJECT +public: + UBDownloadProgressDelegate(QObject* parent=0); + +protected: + void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; +}; + +class UBDownloadWidget : public QWidget +{ + Q_OBJECT +public: + UBDownloadWidget(QWidget* parent=0, const char* name="UBDownloadWidget"); + ~UBDownloadWidget(); + +private slots: + void onFileAddedToDownload(); + void onDownloadUpdated(int id, qint64 crnt, qint64 total); + void onDownloadFinished(int id); + void onCancelClicked(); + +private: + void addCurrentDownloads(); + void addPendingDownloads(); + + /** The general layout of this widget */ + QVBoxLayout* mpLayout; + /** The button layout */ + QHBoxLayout* mpBttnLayout; + /** The treeview that will display the files list */ + QTreeWidget* mpTree; + /** The 'Cancel' button */ + QPushButton* mpCancelBttn; + /** A temporary tree widget item */ + QTreeWidgetItem* mpItem; + /** The delegate that will draw the progressbars */ + UBDownloadProgressDelegate mProgressBarDelegate; +}; + +#endif // UBDOWNLOADWIDGET_H diff --git a/src/gui/UBLibWebView.cpp b/src/gui/UBLibWebView.cpp index d939654d..63ca4139 100644 --- a/src/gui/UBLibWebView.cpp +++ b/src/gui/UBLibWebView.cpp @@ -19,7 +19,6 @@ UBLibWebView::UBLibWebView(QWidget* parent, const char* name):QWidget(parent) mpView = new QWebView(this); mpView->setObjectName("SearchEngineView"); - //mpView->setStyleSheet(QString("background-color:white;")); mpWebSettings = QWebSettings::globalSettings(); mpWebSettings->setAttribute(QWebSettings::JavaEnabled, true); diff --git a/src/gui/UBMainWindow.cpp b/src/gui/UBMainWindow.cpp index 1bc2cab5..e3b69236 100644 --- a/src/gui/UBMainWindow.cpp +++ b/src/gui/UBMainWindow.cpp @@ -27,9 +27,12 @@ UBMainWindow::UBMainWindow(QWidget *parent, Qt::WindowFlags flags) , mBoardWidget(0) , mWebWidget(0) , mDocumentsWidget(0) + , mpDownloadWidget(NULL) { Ui::MainWindow::setupUi(this); + mpDownloadWidget = new UBDownloadWidget(); + mpDownloadWidget->setWindowModality(Qt::ApplicationModal); //Setting tooltip colors staticly, since they look not quite well on different color themes QPalette toolTipPalette; @@ -55,7 +58,11 @@ UBMainWindow::UBMainWindow(QWidget *parent, Qt::WindowFlags flags) UBMainWindow::~UBMainWindow() { - // NOOP + if(NULL != mpDownloadWidget) + { + delete mpDownloadWidget; + mpDownloadWidget = NULL; + } } void UBMainWindow::addBoardWidget(QWidget *pWidget) @@ -77,7 +84,6 @@ void UBMainWindow::switchToBoardWidget() void UBMainWindow::addWebWidget(QWidget *pWidget) { - qDebug() << "add to StackedLayout size height: " << pWidget->height() << " width: " << pWidget->width(); if (!mWebWidget) { mWebWidget = pWidget; @@ -182,3 +188,18 @@ void UBMainWindow::information(QString windowTitle, QString text) oneButtonMessageBox(windowTitle, text, QMessageBox::Information); } +void UBMainWindow::showDownloadWidget() +{ + if(NULL != mpDownloadWidget) + { + mpDownloadWidget->show(); + } +} + +void UBMainWindow::hideDownloadWidget() +{ + if(NULL != mpDownloadWidget) + { + mpDownloadWidget->hide(); + } +} diff --git a/src/gui/UBMainWindow.h b/src/gui/UBMainWindow.h index 4150bbec..a09d66a1 100644 --- a/src/gui/UBMainWindow.h +++ b/src/gui/UBMainWindow.h @@ -21,6 +21,7 @@ #include #include #include +#include "UBDownloadWidget.h" class QStackedLayout; @@ -47,6 +48,9 @@ class UBMainWindow : public QMainWindow, public Ui::MainWindow void warning(QString windowTitle, QString text); void information(QString windowTitle, QString text); + void showDownloadWidget(); + void hideDownloadWidget(); + signals: void closeEvent_Signal( QCloseEvent *event ); @@ -70,6 +74,9 @@ class UBMainWindow : public QMainWindow, public Ui::MainWindow QWidget *mBoardWidget; QWidget *mWebWidget; QWidget *mDocumentsWidget; + +private: + UBDownloadWidget* mpDownloadWidget; }; #endif /* UBMAINWINDOW_H_ */ diff --git a/src/gui/gui.pri b/src/gui/gui.pri index d5f9eb62..1ea89a32 100644 --- a/src/gui/gui.pri +++ b/src/gui/gui.pri @@ -44,7 +44,8 @@ HEADERS += src/gui/UBThumbnailView.h \ src/gui/UBMagnifer.h \ src/gui/UBCachePropertiesWidget.h \ src/gui/UBTeacherBarWidget.h \ - src/gui/UBLibWebView.h + src/gui/UBLibWebView.h \ + src/gui/UBDownloadWidget.h SOURCES += src/gui/UBThumbnailView.cpp \ src/gui/UBFloatingPalette.cpp \ @@ -91,7 +92,8 @@ SOURCES += src/gui/UBThumbnailView.cpp \ src/gui/UBMagnifer.cpp \ src/gui/UBCachePropertiesWidget.cpp \ src/gui/UBTeacherBarWidget.cpp \ - src/gui/UBLibWebView.cpp + src/gui/UBLibWebView.cpp \ + src/gui/UBDownloadWidget.cpp win32 {