From 3fed7dc8087170060f4f6515319422aa9115a362 Mon Sep 17 00:00:00 2001
From: Ivan Ilin <ilin@scand.com>
Date: Tue, 31 Jan 2012 20:23:02 +0200
Subject: [PATCH] some changes for sDownloadFileDesc

---
 src/api/UBWidgetUniboardAPI.cpp     | 19 +++++++++++++
 src/api/UBWidgetUniboardAPI.h       | 16 ++++++++++-
 src/board/UBBoardView.cpp           | 31 +++++++++++++++++---
 src/board/UBBoardView.h             |  6 ++--
 src/core/UBDownloadManager.cpp      | 44 +++++++++++++++++------------
 src/core/UBDownloadManager.h        | 27 ++++++++++++++++--
 src/domain/UBGraphicsWidgetItem.cpp |  4 +++
 src/domain/UBGraphicsWidgetItem.h   |  3 +-
 src/network/UBHttpGet.cpp           |  4 +--
 src/pdf-merger/pdfMerger.pri        |  1 -
 10 files changed, 122 insertions(+), 33 deletions(-)

diff --git a/src/api/UBWidgetUniboardAPI.cpp b/src/api/UBWidgetUniboardAPI.cpp
index c9df1375..43e99e7c 100644
--- a/src/api/UBWidgetUniboardAPI.cpp
+++ b/src/api/UBWidgetUniboardAPI.cpp
@@ -55,6 +55,7 @@ UBWidgetUniboardAPI::UBWidgetUniboardAPI(UBGraphicsScene *pScene, UBGraphicsWidg
         mMessagesAPI = new UBWidgetMessageAPI(w3CGraphicsWidget->w3cWidget());
         mDatastoreAPI = new UBDatastoreAPI(w3CGraphicsWidget);
     }
+    connect(UBDownloadManager::downloadManager(), SIGNAL(downloadFinished(bool,int,QUrl,QString,QByteArray)), this, SLOT(onDownloadFinished(bool,int,QUrl,QString,QByteArray)));
 }
 
 
@@ -451,7 +452,25 @@ QString UBWidgetUniboardAPI::downloadUrl(const QString &objectUrl, const QString
 
     return result;
 }
+QString UBWidgetUniboardAPI::downloadWeb(const QString &objectUrl)
+{
+    // When we fall there, it means that we are dropping something from the web to the board
+    sDownloadFileDesc desc;
+    desc.modal = true;
+    desc.url = objectUrl;
+    desc.currentSize = 0;
+    desc.name = QFileInfo(objectUrl).fileName();
+    desc.totalSize = 0; // The total size will be retrieved during the download
+
+    registerIDWidget(UBDownloadManager::downloadManager()->addFileToDownload(desc));
+    return QString();
+}
 
+void UBWidgetUniboardAPI::onDownloadFinished(bool pSuccess, int id, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData)
+{
+    Q_UNUSED(pData)
+    qDebug() << "got an ID" << id << pSuccess << sourceUrl << pContentTypeHeader;
+}
 
 UBDocumentDatastoreAPI::UBDocumentDatastoreAPI(UBGraphicsW3CWidgetItem *graphicsWidget)
     : UBW3CWebStorage(graphicsWidget)
diff --git a/src/api/UBWidgetUniboardAPI.h b/src/api/UBWidgetUniboardAPI.h
index 56188d03..405c25ef 100644
--- a/src/api/UBWidgetUniboardAPI.h
+++ b/src/api/UBWidgetUniboardAPI.h
@@ -245,6 +245,20 @@ class UBWidgetUniboardAPI : public QObject
          * this method download the object on the widget directory and return the path of the downloaded object
          */
         QString downloadUrl(const QString &objectUrl, const QString &extention = "");
+        QString downloadWeb(const QString &objectUrl);
+
+private slots:
+        void onDownloadFinished(bool pSuccess, int id, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData);
+
+
+private:
+        inline void registerIDWidget(int id){webDownloadIds.append(id);}
+        inline bool expectedID(int id) const {return webDownloadIds.contains(id);}
+        inline bool removeID(int id) {return webDownloadIds.removeAll(id);}
+
+
+
+//        void unregister
 
     private:
 
@@ -265,7 +279,7 @@ class UBWidgetUniboardAPI : public QObject
         UBWidgetMessageAPI* mMessagesAPI;
 
         UBDatastoreAPI* mDatastoreAPI;
-
+        QList<int> webDownloadIds;
 };
 
 
diff --git a/src/board/UBBoardView.cpp b/src/board/UBBoardView.cpp
index ed816413..b6606f3e 100644
--- a/src/board/UBBoardView.cpp
+++ b/src/board/UBBoardView.cpp
@@ -66,6 +66,7 @@ const QString htmlAlias = "html";
 const QString tMainSection = "mimedata";
 const QString tType = "type";
 const QString tPath = "path";
+const QString tMessage = "message";
 
 UBBoardView::UBBoardView (UBBoardController* pController, QWidget* pParent)
 : QGraphicsView (pParent)
@@ -771,6 +772,24 @@ QString UBBoardView::processMimeData(const QMimeData *pMimeData, UBGraphicsWidge
     writer.writeStartDocument();
     writer.writeStartElement(tMainSection);
 
+    if (pMimeData->hasHtml()) {
+        QList<QUrl> urls = pMimeData->urls();
+
+        int index = 0;
+        foreach(const QUrl url, urls) {
+
+//            QPointF pos(pPos + QPointF(index * 15, index * 15));
+//            downloadURL(url, pos);
+            widget->downloadWeb(url.toString());
+            index++;
+        }
+        writer.writeTextElement(tMessage, "Downloading content process...");
+
+        writer.writeEndElement();
+        writer.writeEndDocument();
+        return mimeXml;
+    }
+
     if (pMimeData->hasUrls()) {
         QList<QUrl> urls = pMimeData->urls();
 
@@ -806,7 +825,7 @@ QString UBBoardView::processMimeData(const QMimeData *pMimeData, UBGraphicsWidge
     return mimeXml;
 }
 
-QString UBBoardView::fileExtention(const QString &filename)
+QString UBBoardView::fileExtention(const QString &filename) const
 {
     int pos = filename.lastIndexOf(".");
     if (pos != -1)
@@ -814,7 +833,7 @@ QString UBBoardView::fileExtention(const QString &filename)
     else
         return QString();
 }
-QString UBBoardView::typeForExtention(const QString &extention)
+QString UBBoardView::typeForExtention(const QString &extention) const
 {
     if (extention.isEmpty())
         return QString();
@@ -833,8 +852,12 @@ QString UBBoardView::typeForExtention(const QString &extention)
 
     return result;
 }
-bool UBBoardView::isDropableData(const QMimeData *pMimeData)
+bool UBBoardView::isDropableData(const QMimeData *pMimeData) const
 {
+    if (pMimeData->hasHtml()) {
+        return true;
+    }
+
     if (pMimeData->hasUrls()) {
         if (!typeForExtention(fileExtention(pMimeData->urls().at(0).toLocalFile())).isNull()) {
             return true;
@@ -850,7 +873,7 @@ void UBBoardView::dropEvent (QDropEvent *event)
     QGraphicsItem* graphicsItemAtPos = itemAt(event->pos().x(),event->pos().y());
     UBGraphicsWidgetItem* graphicsWidget = dynamic_cast<UBGraphicsWidgetItem*>(graphicsItemAtPos);
 
-    if (graphicsWidget && graphicsWidget->acceptDrops()){
+    if (graphicsWidget && graphicsWidget->acceptDrops()) {
         // A new event is build to avoid problem related to different way to pass the mime type
         // A parsing is done to try to provide a mimeType with only urls.
         QMimeData mimeData;
diff --git a/src/board/UBBoardView.h b/src/board/UBBoardView.h
index 92f9cd61..5353c711 100644
--- a/src/board/UBBoardView.h
+++ b/src/board/UBBoardView.h
@@ -89,7 +89,7 @@ class UBBoardView : public QGraphicsView
 
         QList<QUrl> processMimeData(const QMimeData* pMimeData);
         QString processMimeData(const QMimeData *pMimeData, UBGraphicsWidgetItem *widget);
-        bool isDropableData(const QMimeData *pMimeData);
+        bool isDropableData(const QMimeData *pMimeData) const;
 
         UBBoardController* mController;
 
@@ -118,8 +118,8 @@ class UBBoardView : public QGraphicsView
 		bool mVirtualKeyboardActive;
         bool mOkOnWidget;
 
-        QString typeForExtention(const QString &extention);
-        QString fileExtention(const QString &filename);
+        QString typeForExtention(const QString &extention) const;
+        QString fileExtention(const QString &filename) const;
 
     private slots:
 
diff --git a/src/core/UBDownloadManager.cpp b/src/core/UBDownloadManager.cpp
index d4442f21..282d70f7 100644
--- a/src/core/UBDownloadManager.cpp
+++ b/src/core/UBDownloadManager.cpp
@@ -70,7 +70,7 @@ void UBDownloadManager::destroy()
  * \brief Add a file to the download list
  * @param desc as the given file description
  */
-void UBDownloadManager::addFileToDownload(sDownloadFileDesc desc)
+int UBDownloadManager::addFileToDownload(sDownloadFileDesc desc)
 {
     // Set the ID for this download
     desc.id = mLastID;
@@ -89,6 +89,8 @@ void UBDownloadManager::addFileToDownload(sDownloadFileDesc desc)
     UBApplication::boardController->paletteManager()->startDownloads();
 
     emit fileAddedToDownload();
+
+    return desc.id;
 }
 
 /**
@@ -194,23 +196,29 @@ void UBDownloadManager::onDownloadProgress(int id, qint64 received, qint64 total
  */
 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);
-            }
-            else
-            {
-                emit addDownloadedFileToLibrary(pSuccess, sourceUrl, pContentTypeHeader, pData);
-            }
-            break;
-        }
-    }
+//    Temporary data for dnd do not delete it please
+    Q_UNUSED(pPos)
+    Q_UNUSED(pSize)
+    Q_UNUSED(isBackground)
+
+    emit downloadFinished(pSuccess, id, sourceUrl, pContentTypeHeader, pData);
+//    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);
+//            }
+//            else
+//            {
+//                emit addDownloadedFileToLibrary(pSuccess, sourceUrl, pContentTypeHeader, pData);
+//            }
+//            break;
+//        }
+//    }
 
     // Then do this
     updateFileCurrentSize(id);
diff --git a/src/core/UBDownloadManager.h b/src/core/UBDownloadManager.h
index bccb121d..343330b7 100644
--- a/src/core/UBDownloadManager.h
+++ b/src/core/UBDownloadManager.h
@@ -19,6 +19,7 @@
 #include <QString>
 #include <QVector>
 #include <QMutex>
+#include <QDropEvent>
 
 #include "UBDownloadThread.h"
 
@@ -26,8 +27,26 @@
 
 #define     SIMULTANEOUS_DOWNLOAD       2   // Maximum 5 because of QNetworkAccessManager limitation!!!
 
-typedef struct
+enum eDestinations {
+    board //default for sDownloadFileDesc
+    , library
+    , graphicsWidget
+};
+
+struct sDownloadFileDesc
 {
+    //creating constructor to make sure to have default values
+    sDownloadFileDesc() :
+        dest(board)
+      , id(0)
+      , totalSize(0)
+      , currentSize(0)
+      , modal(false)
+      , isBackground(false)
+      , widgetDrop(0)
+    {;}
+
+    eDestinations dest;
     QString name;
     int id;
     int totalSize;
@@ -37,7 +56,8 @@ typedef struct
     QPointF pos;        // For board drop only
     QSize size;         // For board drop only
     bool isBackground;  // For board drop only
-}sDownloadFileDesc;
+    QDropEvent *widgetDrop; //For widget's drops
+};
 
 class UBDownloadHttpFile : public UBHttpGet
 {
@@ -66,7 +86,7 @@ public:
     UBDownloadManager(QObject* parent=0, const char* name="UBDownloadManager");
     ~UBDownloadManager();
     static UBDownloadManager* downloadManager();
-    void addFileToDownload(sDownloadFileDesc desc);
+    int addFileToDownload(sDownloadFileDesc desc);
     QVector<sDownloadFileDesc> currentDownloads();
     QVector<sDownloadFileDesc> pendingDownloads();
     void cancelDownloads();
@@ -78,6 +98,7 @@ signals:
     void fileAddedToDownload();
     void downloadUpdated(int id, qint64 crnt, qint64 total);
     void downloadFinished(int id);
+    void downloadFinished(bool pSuccess, int id, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData);
     void downloadModalFinished();
     void addDownloadedFileToBoard(bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground);
     void addDownloadedFileToLibrary(bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData);
diff --git a/src/domain/UBGraphicsWidgetItem.cpp b/src/domain/UBGraphicsWidgetItem.cpp
index 61d49413..08e12bbd 100644
--- a/src/domain/UBGraphicsWidgetItem.cpp
+++ b/src/domain/UBGraphicsWidgetItem.cpp
@@ -258,6 +258,10 @@ QString UBGraphicsWidgetItem::downloadUrl(const QString &fileUrl, const QString
 {
     return mUniboardAPI->downloadUrl(fileUrl, extention);
 }
+QString UBGraphicsWidgetItem::downloadWeb(const QString &fileUrl)
+{
+    return mUniboardAPI->downloadWeb(fileUrl);
+}
 
 UBGraphicsAppleWidgetItem::UBGraphicsAppleWidgetItem(const QUrl& pWidgetUrl, QGraphicsItem *parent)
     : UBGraphicsWidgetItem(parent)
diff --git a/src/domain/UBGraphicsWidgetItem.h b/src/domain/UBGraphicsWidgetItem.h
index 2be7b8af..4ab90816 100644
--- a/src/domain/UBGraphicsWidgetItem.h
+++ b/src/domain/UBGraphicsWidgetItem.h
@@ -62,11 +62,12 @@ class UBGraphicsWidgetItem : public UBGraphicsProxyWidget
         QMap<QString, QString> datastoreEntries() const;
         void removeDatastoreEntry(const QString& key);
         void removeAllDatastoreEntries();
-        virtual UBGraphicsItemDelegate* Delegate() const {return mDelegate;}
+        virtual UBGraphicsItemDelegate* Delegate() const {return mDelegate; }
 
         virtual void remove();
         void removeScript();
         QString downloadUrl(const QString &fileUrl, const QString &extention);
+        QString downloadWeb(const QString &fileUrl);
 
         virtual void setOwnFolder(const QUrl &newFolder) {ownFolder = newFolder;}
         virtual QUrl getOwnFolder() const {return ownFolder;}
diff --git a/src/network/UBHttpGet.cpp b/src/network/UBHttpGet.cpp
index 1edb712b..c1c21bac 100644
--- a/src/network/UBHttpGet.cpp
+++ b/src/network/UBHttpGet.cpp
@@ -49,8 +49,8 @@ QNetworkReply* UBHttpGet::get(QUrl pUrl, QPointF pPos, QSize pSize, bool isBackg
     mSize = pSize;
     mIsBackground = isBackground;
 
-        if (mReply)
-                delete mReply;
+    if (mReply)
+        delete mReply;
 
     UBNetworkAccessManager * nam = UBNetworkAccessManager::defaultAccessManager();
     mReply = nam->get(QNetworkRequest(pUrl)); //mReply deleted by this destructor
diff --git a/src/pdf-merger/pdfMerger.pri b/src/pdf-merger/pdfMerger.pri
index edc8d1fd..0b0e6e39 100644
--- a/src/pdf-merger/pdfMerger.pri
+++ b/src/pdf-merger/pdfMerger.pri
@@ -58,7 +58,6 @@ SOURCES += \
 	
 
 macx {
-
    LIBS += -lz
 }
 
-- 
2.18.1