New version od the download manager coded

preferencesAboutTextFull
shibakaneki 13 years ago
parent 2f9367c39a
commit af5b99cfb3
  1. 2
      resources/style.qss
  2. 24
      src/board/UBBoardController.cpp
  3. 83
      src/board/UBBoardController.h
  4. 3
      src/board/UBBoardView.cpp
  5. 366
      src/core/UBDownloadManager.cpp
  6. 106
      src/core/UBDownloadManager.h
  7. 115
      src/core/UBDownloadThread.cpp
  8. 56
      src/core/UBDownloadThread.h
  9. 4
      src/core/UBPersistenceManager.cpp
  10. 8
      src/core/core.pri
  11. 203
      src/gui/UBDownloadWidget.cpp
  12. 69
      src/gui/UBDownloadWidget.h
  13. 1
      src/gui/UBLibWebView.cpp
  14. 25
      src/gui/UBMainWindow.cpp
  15. 7
      src/gui/UBMainWindow.h
  16. 6
      src/gui/gui.pri

@ -19,7 +19,7 @@ QWidget#UBLibWebView
QWebView#SearchEngineView
{
background:#EEEEEE;
background:white;
}
QColorDialog

@ -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()
{
}

@ -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<UBToolWidget*> mTools;
qreal mSystemScaleFactor;
void updatePageSizeState();
void saveViewState();
void adjustDisplayViews();
bool mCleanupDone;
QMap<QAction*, QPair<QString, QString> > 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();
};

@ -726,7 +726,8 @@ UBBoardView::dragMoveEvent (QDragMoveEvent *event)
void
UBBoardView::dropEvent (QDropEvent *event)
{
if(!event->source() || dynamic_cast<UBThumbnailWidget *>(event->source()))
qDebug() << event->source();
if(!event->source() || dynamic_cast<UBThumbnailWidget *>(event->source()) || dynamic_cast<QWebView*>(event->source()))
{
mController->processMimeData (event->mimeData (), mapToScene (event->pos ()));
event->acceptProposedAction ();

@ -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 <http://www.gnu.org/licenses/>.
*/
#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<SIMULTANEOUS_DOWNLOAD; i++)
{
mDLAvailability.append(-1);
}
}
/**
* \brief Update the download order. The modal downloads will be put in priority.
*/
void UBDownloadManager::updateDownloadOrder()
{
QVector<sDownloadFileDesc> modalFiles;
QVector<sDownloadFileDesc> nonModalfiles;
for(int i=0; i<mPendingDL.size(); i++)
{
sDownloadFileDesc crnt = mPendingDL.at(i);
if(crnt.modal)
{
modalFiles.append(crnt);
}
else
{
nonModalfiles.append(crnt);
}
}
mPendingDL = modalFiles + nonModalfiles;
}
/**
* \brief Update the download list. If a current download is finished, we take a
* file from the pending download list and add it to the download list.
*/
void UBDownloadManager::onUpdateDownloadLists()
{
for(int i=0; i<SIMULTANEOUS_DOWNLOAD; i++)
{
if(mPendingDL.empty())
{
// If we fall here that means that there is no pending download
break;
}
if(-1 == mDLAvailability.at(i))
{
// Pending downloads exist and a download 'slot' is available
// Let's move the first pending download to the current download
// list and fill the slot
sDownloadFileDesc desc = mPendingDL.at(0);
mCrntDL.append(desc);
mPendingDL.remove(0);
mDLAvailability.remove(i);
mDLAvailability.insert(i, desc.id);
// Start the download of this file
startFileDownload(desc);
}
}
}
/**
* \brief Get the list of the current downloads
* @return a QVector of current downloads
*/
QVector<sDownloadFileDesc> UBDownloadManager::currentDownloads()
{
return mCrntDL;
}
/**
* \brief Get the list of the pending downloads
* @return a QVector of pending downloads
*/
QVector<sDownloadFileDesc> 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<mCrntDL.size(); i++)
{
sDownloadFileDesc desc = mCrntDL.at(i);
if(id == desc.id)
{
if(desc.modal)
{
// The downloaded file is modal so we must put it on the board
emit addDownloadedFileToBoard(pSuccess, sourceUrl, pContentTypeHeader, pData, pPos, pSize, isBackground);
}
break;
}
}
// Then do this
updateFileCurrentSize(id);
}
/**
* \brief Update the description of the given current downloaded file
* @param desc as the current downloaded file description
*/
void UBDownloadManager::updateFileCurrentSize(int id, qint64 received, qint64 total)
{
for(int i=0; i<mCrntDL.size();i++)
{
if(mCrntDL.at(i).id == id)
{
sDownloadFileDesc desc = mCrntDL.at(i);
if(received >= 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; j<mDLAvailability.size();j++)
{
if(id == mDLAvailability.at(j))
{
mDLAvailability.remove(j);
mDLAvailability.insert(j, -1);
break;
}
}
// Here we check if some modal downloads remain
checkIfModalRemains();
// Then we update the list of downloads
onUpdateDownloadLists();
emit downloadFinished(id);
break;
}
mCrntDL.remove(i);
mCrntDL.insert(i,desc);
break;
}
}
}
/**
* \brief Start the download of a file
* @param desc as the given file description
*/
void UBDownloadManager::startFileDownload(sDownloadFileDesc desc)
{
UBDownloadHttpFile* http = new UBDownloadHttpFile(desc.id, this);
connect(http, SIGNAL(downloadProgress(int, qint64,qint64)), this, SLOT(onDownloadProgress(int,qint64,qint64)));
connect(http, SIGNAL(downloadFinished(int, bool, QUrl, QString, QByteArray, QPointF, QSize, bool)), this, SLOT(onDownloadFinished(int, bool, QUrl, QString, QByteArray, QPointF, QSize, bool)));
http->get(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; i<mCrntDL.size();i++)
{
if(mCrntDL.at(i).modal)
{
bModal = true;
break;
}
}
if(!bModal)
{
for(int j=0; j<mPendingDL.size(); j++)
{
if(mPendingDL.at(j).modal)
{
bModal = true;
break;
}
}
}
if(bModal)
{
// Close the modal window
UBApplication::mainWindow->hideDownloadWidget();
// 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);
}

@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef UBDOWNLOADMANAGER_H
#define UBDOWNLOADMANAGER_H
#include <QObject>
#include <QString>
#include <QVector>
#include <QMutex>
#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<sDownloadFileDesc> currentDownloads();
QVector<sDownloadFileDesc> 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<sDownloadFileDesc> mCrntDL;
/** The pending downloads */
QVector<sDownloadFileDesc> 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<int> mDLAvailability;
};
#endif // UBDOWNLOADMANAGER_H

@ -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 <http://www.gnu.org/licenses/>.
*/
#include <QDebug>
#include <QNetworkProxy>
#include <QNetworkDiskCache>
#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);
}

@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef UBDOWNLOADTHREAD_H
#define UBDOWNLOADTHREAD_H
#include <QThread>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
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

@ -306,8 +306,6 @@ void UBPersistenceManager::deleteDocument(UBDocumentProxy* pDocumentProxy)
emit documentWillBeDeleted(pDocumentProxy);
qDebug() << "Deleting document" << pDocumentProxy->persistencePath();
UBFileSystemUtils::deleteDir(pDocumentProxy->persistencePath());
documentProxies.removeAll(QPointer<UBDocumentProxy>(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);

@ -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

@ -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 <http://www.gnu.org/licenses/>.
*/
#include <QDebug>
#include <QHeaderView>
#include <QStyleOptionProgressBarV2>
#include <QApplication>
#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<sDownloadFileDesc> actualDL = UBDownloadManager::downloadManager()->currentDownloads();
for(int i=0; i<actualDL.size();i++)
{
mpItem = new QTreeWidgetItem(mpTree);
mpItem->setText(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<sDownloadFileDesc> pendingDL = UBDownloadManager::downloadManager()->pendingDownloads();
for(int i=0; i<pendingDL.size(); i++)
{
mpItem = new QTreeWidgetItem(mpTree);
mpItem->setText(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);
}

@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef UBDOWNLOADWIDGET_H
#define UBDOWNLOADWIDGET_H
#include <QWidget>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <QItemDelegate>
#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

@ -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);

@ -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();
}
}

@ -21,6 +21,7 @@
#include <QWidget>
#include <QWebView>
#include <QMessageBox>
#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_ */

@ -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 {

Loading…
Cancel
Save