From c508ec6adf44fc4bc98ac35059b38f49a54b7611 Mon Sep 17 00:00:00 2001
From: shibakaneki <didier@sente.ch>
Date: Fri, 18 Nov 2011 12:35:09 +0100
Subject: [PATCH] Finished the download manager support in the right palette

---
 src/board/UBBoardPaletteManager.cpp |  7 ++-
 src/board/UBBoardPaletteManager.h   |  2 +
 src/core/UBDownloadManager.cpp      | 68 ++++++++++++++++++++++++++---
 src/core/UBDownloadManager.h        |  7 +--
 src/gui/UBDownloadWidget.cpp        | 43 +++++++++++++-----
 src/gui/UBDownloadWidget.h          |  6 +++
 6 files changed, 111 insertions(+), 22 deletions(-)

diff --git a/src/board/UBBoardPaletteManager.cpp b/src/board/UBBoardPaletteManager.cpp
index 4752491b..cc9fff7c 100644
--- a/src/board/UBBoardPaletteManager.cpp
+++ b/src/board/UBBoardPaletteManager.cpp
@@ -83,6 +83,7 @@ UBBoardPaletteManager::UBBoardPaletteManager(QWidget* container, UBBoardControll
     , mpTeacherBarWidget(NULL)
     , mpDesktopLibWidget(NULL)
     , mpDownloadWidget(NULL)
+    , mDownloadInProgress(false)
 {
     setupPalettes();
     connectPalettes();
@@ -1009,16 +1010,18 @@ void UBBoardPaletteManager::refreshPalettes()
 
 void UBBoardPaletteManager::startDownloads()
 {
-    if(!mpDownloadWidget->isVisible())
+    if(!mDownloadInProgress)
     {
+        mDownloadInProgress = true;
         mRightPalette->addTab(mpDownloadWidget);
     }
 }
 
 void UBBoardPaletteManager::stopDownloads()
 {
-    if(mpDownloadWidget->isVisible())
+    if(mDownloadInProgress)
     {
+        mDownloadInProgress = false;
         mRightPalette->removeTab(mpDownloadWidget->name());
     }
 }
diff --git a/src/board/UBBoardPaletteManager.h b/src/board/UBBoardPaletteManager.h
index bfab2f8b..9cbbb119 100644
--- a/src/board/UBBoardPaletteManager.h
+++ b/src/board/UBBoardPaletteManager.h
@@ -144,6 +144,8 @@ class UBBoardPaletteManager : public QObject
         //       lib widget!
         UBLibWidget* mpDesktopLibWidget;
 
+        bool mDownloadInProgress;
+
     private slots:
 
         void changeBackground();
diff --git a/src/core/UBDownloadManager.cpp b/src/core/UBDownloadManager.cpp
index 4b867a33..b6eb0547 100644
--- a/src/core/UBDownloadManager.cpp
+++ b/src/core/UBDownloadManager.cpp
@@ -86,10 +86,7 @@ void UBDownloadManager::addFileToDownload(sDownloadFileDesc desc)
         updateDownloadOrder();
         UBApplication::mainWindow->showDownloadWidget();
     }
-    else
-    {
-        UBApplication::boardController->paletteManager()->startDownloads();
-    }
+    UBApplication::boardController->paletteManager()->startDownloads();
 
     emit fileAddedToDownload();
 }
@@ -276,7 +273,7 @@ void UBDownloadManager::updateFileCurrentSize(int id, qint64 received, qint64 to
                 // Verify if all downloads are finished
                 if(mCrntDL.empty() && mPendingDL.empty())
                 {
-                    emit allDownloadsFinished();
+                    finishDownloads();
                 }
 
                 break;
@@ -357,8 +354,7 @@ void UBDownloadManager::cancelDownloads()
 
     checkIfModalRemains();
 
-    // Notify everyone that the downloads have been canceled.
-    emit cancelAllDownloads();
+    finishDownloads(true);
 }
 
 void UBDownloadManager::onDownloadError(int id)
@@ -380,6 +376,64 @@ void UBDownloadManager::onDownloadError(int id)
     }
 }
 
+void UBDownloadManager::finishDownloads(bool cancel)
+{
+    UBApplication::boardController->paletteManager()->stopDownloads();
+    if(cancel){
+        emit cancelAllDownloads();
+    }
+    else{
+        emit allDownloadsFinished();
+    }
+}
+
+void UBDownloadManager::cancelDownload(int id)
+{
+    // Stop the download
+    mReplies[id]->abort();
+    mReplies.remove(id);
+
+    // Remove the canceled download from the download lists
+    bool bFound = false;
+    for(int i=0; i<mCrntDL.size(); i++){
+        if(id == mCrntDL.at(i).id){
+            mCrntDL.remove(i);
+            bFound = true;
+            break;
+        }
+    }
+    if(!bFound){
+        for(int j=0; j<mPendingDL.size(); j++){
+            if(id == mPendingDL.at(j).id){
+                mPendingDL.remove(j);
+                bFound = true;
+                break;
+            }
+        }
+    }
+
+    // Free the download slot used by the finished file
+    for(int h=0; h<mDLAvailability.size();h++){
+        if(id == mDLAvailability.at(h)){
+            mDLAvailability.remove(h);
+            mDLAvailability.insert(h, -1);
+            break;
+        }
+    }
+
+    // Here we check if some modal downloads remain
+    checkIfModalRemains();
+
+    // Then we update the list of downloads
+    onUpdateDownloadLists();
+
+    // Verify if all downloads are finished
+    if(mCrntDL.empty() && mPendingDL.empty())
+    {
+        finishDownloads();
+    }
+}
+
 // ------------------------------------------------------------------------------
 /**
  * \brief Constructor
diff --git a/src/core/UBDownloadManager.h b/src/core/UBDownloadManager.h
index 22d94fc7..bccb121d 100644
--- a/src/core/UBDownloadManager.h
+++ b/src/core/UBDownloadManager.h
@@ -63,11 +63,14 @@ 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();
+    void cancelDownload(int id);
 
     static void destroy();
 
@@ -88,14 +91,12 @@ private slots:
     void onDownloadError(int id);
 
 private:
-    UBDownloadManager(QObject* parent=0, const char* name="UBDownloadManager");
-    ~UBDownloadManager();
-
     void init();
     void updateDownloadOrder();
     void updateFileCurrentSize(int id, qint64 received=-1, qint64 total=-1);
     void startFileDownload(sDownloadFileDesc desc);
     void checkIfModalRemains();
+    void finishDownloads(bool cancel=false);
 
     /** The current downloads */
     QVector<sDownloadFileDesc> mCrntDL;
diff --git a/src/gui/UBDownloadWidget.cpp b/src/gui/UBDownloadWidget.cpp
index 566e21a4..286b9f20 100644
--- a/src/gui/UBDownloadWidget.cpp
+++ b/src/gui/UBDownloadWidget.cpp
@@ -45,6 +45,11 @@ UBDownloadWidget::UBDownloadWidget(QWidget *parent, const char *name):QWidget(pa
 
     mpTree = new QTreeWidget(this);
     mpTree->setRootIsDecorated(false);
+    mpTree->setColumnCount(2);
+    mpTree->header()->setStretchLastSection(false);
+    mpTree->header()->setResizeMode(eItemColumn_Desc, QHeaderView::Stretch);
+    mpTree->header()->setResizeMode(eItemColumn_Close, QHeaderView::Custom);
+    mpTree->resizeColumnToContents(eItemColumn_Close);
     mpTree->header()->close();
     mpLayout->addWidget(mpTree, 1);
 
@@ -59,6 +64,7 @@ UBDownloadWidget::UBDownloadWidget(QWidget *parent, const char *name):QWidget(pa
     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()));
+    connect(mpTree, SIGNAL(itemClicked(QTreeWidgetItem*,int)), this, SLOT(onItemClicked(QTreeWidgetItem*,int)));
 }
 
 /**
@@ -111,13 +117,14 @@ void UBDownloadWidget::addCurrentDownloads()
     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));
+        mpItem->setText(eItemColumn_Desc, actualDL.at(i).name);
+        mpItem->setData(eItemColumn_Desc, Qt::UserRole, QVariant(actualDL.at(i).id));
+        mpItem->setIcon(eItemColumn_Close, QIcon(":images/close.svg"));
         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);
+        mpItem->setData(eItemColumn_Desc, Qt::UserRole, actualDL.at(i).currentSize);
+        mpItem->setData(eItemColumn_Desc, Qt::UserRole + 1, actualDL.at(i).totalSize);
+        mpItem->setData(eItemColumn_Desc, Qt::UserRole + 2, actualDL.at(i).id);
         mpTree->addTopLevelItem(mpItem);
         mpTree->setItemDelegateForRow(((i+1)*2)-1, &mProgressBarDelegate);
     }
@@ -129,12 +136,11 @@ void UBDownloadWidget::addCurrentDownloads()
 void UBDownloadWidget::addPendingDownloads()
 {
     QVector<sDownloadFileDesc> pendingDL = UBDownloadManager::downloadManager()->pendingDownloads();
-    qDebug() << "Pending downloads size: " << pendingDL.size();
     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));
+        mpItem->setText(eItemColumn_Desc, pendingDL.at(i).name);
+        mpItem->setData(eItemColumn_Desc, Qt::UserRole, QVariant(pendingDL.at(i).id));
         mpTree->addTopLevelItem(mpItem);
     }
 }
@@ -154,7 +160,7 @@ void UBDownloadWidget::onDownloadUpdated(int id, qint64 crnt, qint64 total)
         {
             for(int i=0; i< model->rowCount(); i++)
             {
-                QModelIndex currentIndex = model->index(i, 0);
+                QModelIndex currentIndex = model->index(i, eItemColumn_Desc);
                 if(id == currentIndex.data(Qt::UserRole + 2))
                 {
                     // We found the right item, now we update the progress bar
@@ -186,6 +192,21 @@ void UBDownloadWidget::onCancelClicked()
     UBDownloadManager::downloadManager()->cancelDownloads();
 }
 
+/**
+ * \brief Handles the item click notification
+ * @param pItem as the item clicked
+ * @param col as the column containing the item clicked
+ */
+void UBDownloadWidget::onItemClicked(QTreeWidgetItem *pItem, int col)
+{
+    if( eItemColumn_Close == col
+            && "" != pItem->text(eItemColumn_Desc)){
+
+        // Stop the download of the clicked item and remove it from the list
+        UBDownloadManager::downloadManager()->cancelDownload(pItem->data(eItemColumn_Desc, Qt::UserRole).toInt());
+    }
+}
+
 // ---------------------------------------------------------------------------------------------
 UBDownloadProgressDelegate::UBDownloadProgressDelegate(QObject *parent):QItemDelegate(parent)
 {
@@ -200,5 +221,7 @@ void UBDownloadProgressDelegate::paint(QPainter *painter, const QStyleOptionView
     opt.maximum = index.data(Qt::UserRole + 1).toInt();
     opt.progress = index.data(Qt::UserRole).toInt();
 
-    QApplication::style()->drawControl(QStyle::CE_ProgressBar, &opt, painter, 0);
+    if(0 == index.column()){
+        QApplication::style()->drawControl(QStyle::CE_ProgressBar, &opt, painter, 0);
+    }
 }
diff --git a/src/gui/UBDownloadWidget.h b/src/gui/UBDownloadWidget.h
index 0819da7c..e53df26d 100644
--- a/src/gui/UBDownloadWidget.h
+++ b/src/gui/UBDownloadWidget.h
@@ -25,6 +25,11 @@
 
 #include "core/UBDownloadManager.h"
 
+typedef enum{
+    eItemColumn_Desc,
+    eItemColumn_Close
+}eItemColumn;
+
 class UBDownloadProgressDelegate : public QItemDelegate
 {
     Q_OBJECT
@@ -47,6 +52,7 @@ private slots:
     void onDownloadUpdated(int id, qint64 crnt, qint64 total);
     void onDownloadFinished(int id);
     void onCancelClicked();
+    void onItemClicked(QTreeWidgetItem* pItem, int col);
 
 private:
     void addCurrentDownloads();