Implemented new helper class for downloading local files into document in separate thread.

Have a problem - should take care about saving original src path.
preferencesAboutTextFull
Aleksei Kanash 12 years ago
parent 95d5c724e3
commit b86614edde
  1. 2
      src/api/UBWidgetUniboardAPI.cpp
  2. 107
      src/board/UBBoardController.cpp
  3. 4
      src/board/UBBoardController.h
  4. 30
      src/core/UBDownloadManager.cpp
  5. 3
      src/core/UBDownloadManager.h
  6. 8
      src/domain/UBGraphicsMediaItem.cpp
  7. 5
      src/domain/UBGraphicsScene.cpp
  8. 90
      src/frameworks/UBFileSystemUtils.cpp
  9. 45
      src/frameworks/UBFileSystemUtils.h
  10. 4
      src/gui/UBFeaturesWidget.cpp

@ -506,7 +506,7 @@ void UBWidgetUniboardAPI::ProcessDropEvent(QGraphicsSceneDragDropEvent *event)
sDownloadFileDesc desc; sDownloadFileDesc desc;
desc.dest = sDownloadFileDesc::graphicsWidget; desc.dest = sDownloadFileDesc::graphicsWidget;
desc.modal = true; desc.modal = true;
desc.url = url; desc.srcUrl = url;
desc.currentSize = 0; desc.currentSize = 0;
desc.name = QFileInfo(url).fileName(); desc.name = QFileInfo(url).fileName();
desc.totalSize = 0; // The total size will be retrieved during the download desc.totalSize = 0; // The total size will be retrieved during the download

@ -984,22 +984,36 @@ void UBBoardController::downloadURL(const QUrl& url, const QPointF& pPos, const
|| contentType.startsWith("application/widget") || contentType.startsWith("application/widget")
|| contentType.startsWith("application/vnd.apple-widget"); || contentType.startsWith("application/vnd.apple-widget");
QFile file(fileName);
if (shouldLoadFileData) if (shouldLoadFileData)
{
QFile file(fileName);
file.open(QIODevice::ReadOnly); file.open(QIODevice::ReadOnly);
downloadFinished(true, formedUrl, contentType, file.readAll(), pPos, pSize, isBackground, internalData);
downloadFinished(true, formedUrl, contentType, file.readAll(), pPos, pSize, isBackground, internalData);
if (shouldLoadFileData)
file.close(); file.close();
}
else
{
// media items should be copyed in separate thread
sDownloadFileDesc desc;
desc.modal = false;
desc.srcUrl = sUrl;
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;
UBDownloadManager::downloadManager()->addFileToDownload(desc);
}
} }
else else
{ {
// When we fall there, it means that we are dropping something from the web to the board // When we fall there, it means that we are dropping something from the web to the board
sDownloadFileDesc desc; sDownloadFileDesc desc;
desc.modal = true; desc.modal = true;
desc.url = url.toString(); desc.srcUrl = url.toString();
desc.currentSize = 0; desc.currentSize = 0;
desc.name = QFileInfo(url.toString()).fileName(); desc.name = QFileInfo(url.toString()).fileName();
desc.totalSize = 0; // The total size will be retrieved during the download desc.totalSize = 0; // The total size will be retrieved during the download
@ -1157,11 +1171,9 @@ UBItem *UBBoardController::downloadFinished(bool pSuccess, QUrl sourceUrl, QStri
qDebug() << "accepting mime type" << mimeType << "as video"; qDebug() << "accepting mime type" << mimeType << "as video";
UBGraphicsMediaItem *mediaVideoItem = 0; UBGraphicsMediaItem *mediaVideoItem = 0;
QUuid uuid = QUuid::createUuid();
if (pData.length() > 0) if (pData.length() > 0)
{ {
QUuid uuid = QUuid::createUuid();
QString destFile; QString destFile;
bool b = UBPersistenceManager::persistenceManager()->addFileToDocument(selectedDocument(), bool b = UBPersistenceManager::persistenceManager()->addFileToDocument(selectedDocument(),
sourceUrl.toString(), sourceUrl.toString(),
@ -1178,16 +1190,16 @@ UBItem *UBBoardController::downloadFinished(bool pSuccess, QUrl sourceUrl, QStri
QUrl url = QUrl::fromLocalFile(destFile); QUrl url = QUrl::fromLocalFile(destFile);
mediaVideoItem = mActiveScene->addMedia(url, false, pPos); mediaVideoItem = mActiveScene->addMedia(url, false, pPos);
mediaVideoItem->setSourceUrl(sourceUrl);
mediaVideoItem->setUuid(uuid);
} }
else else
{ {
mediaVideoItem = addVideo(sourceUrl, false, pPos); qDebug() << sourceUrl.toString();
mediaVideoItem = addVideo(sourceUrl, false, pPos, true);
} }
if(mediaVideoItem){ if(mediaVideoItem){
mediaVideoItem->setSourceUrl(sourceUrl);
mediaVideoItem->setUuid(uuid);
connect(this, SIGNAL(activeSceneChanged()), mediaVideoItem, SLOT(activeSceneChanged())); connect(this, SIGNAL(activeSceneChanged()), mediaVideoItem, SLOT(activeSceneChanged()));
} }
@ -1201,10 +1213,9 @@ UBItem *UBBoardController::downloadFinished(bool pSuccess, QUrl sourceUrl, QStri
UBGraphicsMediaItem *audioMediaItem = 0; UBGraphicsMediaItem *audioMediaItem = 0;
QUuid uuid = QUuid::createUuid();
if (pData.length() > 0) if (pData.length() > 0)
{ {
QUuid uuid = QUuid::createUuid();
QString destFile; QString destFile;
bool b = UBPersistenceManager::persistenceManager()->addFileToDocument(selectedDocument(), bool b = UBPersistenceManager::persistenceManager()->addFileToDocument(selectedDocument(),
sourceUrl.toString(), sourceUrl.toString(),
@ -1221,16 +1232,15 @@ UBItem *UBBoardController::downloadFinished(bool pSuccess, QUrl sourceUrl, QStri
QUrl url = QUrl::fromLocalFile(destFile); QUrl url = QUrl::fromLocalFile(destFile);
audioMediaItem = mActiveScene->addMedia(url, false, pPos); audioMediaItem = mActiveScene->addMedia(url, false, pPos);
audioMediaItem->setSourceUrl(sourceUrl);
audioMediaItem->setUuid(uuid);
} }
else else
{ {
audioMediaItem = addAudio(sourceUrl, false, pPos); audioMediaItem = addAudio(sourceUrl, false, pPos, true);
} }
if(audioMediaItem){ if(audioMediaItem){
audioMediaItem->setSourceUrl(sourceUrl);
audioMediaItem->setUuid(uuid);
connect(this, SIGNAL(activeSceneChanged()), audioMediaItem, SLOT(activeSceneChanged())); connect(this, SIGNAL(activeSceneChanged()), audioMediaItem, SLOT(activeSceneChanged()));
} }
@ -2023,23 +2033,28 @@ void UBBoardController::grabScene(const QRectF& pSceneRect)
} }
} }
UBGraphicsMediaItem* UBBoardController::addVideo(const QUrl& pSourceUrl, bool startPlay, const QPointF& pos) UBGraphicsMediaItem* UBBoardController::addVideo(const QUrl& pSourceUrl, bool startPlay, const QPointF& pos, bool bUseSource)
{ {
QUuid uuid = QUuid::createUuid(); QUuid uuid = QUuid::createUuid();
QUrl concreteUrl = pSourceUrl; QUrl concreteUrl = pSourceUrl;
QString destFile; // media file is not in document folder yet
bool b = UBPersistenceManager::persistenceManager()->addFileToDocument(selectedDocument(), if (!bUseSource)
pSourceUrl.toLocalFile(),
UBPersistenceManager::videoDirectory,
uuid,
destFile);
if (!b)
{ {
showMessage(tr("Add file operation failed: file copying error")); QString destFile;
return NULL; bool b = UBPersistenceManager::persistenceManager()->addFileToDocument(selectedDocument(),
} pSourceUrl.toLocalFile(),
concreteUrl = QUrl::fromLocalFile(destFile); UBPersistenceManager::videoDirectory,
uuid,
destFile);
if (!b)
{
showMessage(tr("Add file operation failed: file copying error"));
return NULL;
}
concreteUrl = QUrl::fromLocalFile(destFile);
}// else we just use source Url.
UBGraphicsMediaItem* vi = mActiveScene->addMedia(concreteUrl, startPlay, pos); UBGraphicsMediaItem* vi = mActiveScene->addMedia(concreteUrl, startPlay, pos);
selectedDocument()->setMetaData(UBSettings::documentUpdatedAt, UBStringUtils::toUtcIsoDateTime(QDateTime::currentDateTime())); selectedDocument()->setMetaData(UBSettings::documentUpdatedAt, UBStringUtils::toUtcIsoDateTime(QDateTime::currentDateTime()));
@ -2053,23 +2068,27 @@ UBGraphicsMediaItem* UBBoardController::addVideo(const QUrl& pSourceUrl, bool st
} }
UBGraphicsMediaItem* UBBoardController::addAudio(const QUrl& pSourceUrl, bool startPlay, const QPointF& pos) UBGraphicsMediaItem* UBBoardController::addAudio(const QUrl& pSourceUrl, bool startPlay, const QPointF& pos, bool bUseSource)
{ {
QUuid uuid = QUuid::createUuid(); QUuid uuid = QUuid::createUuid();
QUrl concreteUrl = pSourceUrl; QUrl concreteUrl = pSourceUrl;
QString destFile; // media file is not in document folder yet
bool b = UBPersistenceManager::persistenceManager()->addFileToDocument(selectedDocument(), if (!bUseSource)
pSourceUrl.toLocalFile(),
UBPersistenceManager::audioDirectory,
uuid,
destFile);
if (!b)
{ {
showMessage(tr("Add file operation failed: file copying error")); QString destFile;
return NULL; bool b = UBPersistenceManager::persistenceManager()->addFileToDocument(selectedDocument(),
} pSourceUrl.toLocalFile(),
concreteUrl = QUrl::fromLocalFile(destFile); UBPersistenceManager::audioDirectory,
uuid,
destFile);
if (!b)
{
showMessage(tr("Add file operation failed: file copying error"));
return NULL;
}
concreteUrl = QUrl::fromLocalFile(destFile);
}// else we just use source Url.
UBGraphicsMediaItem* ai = mActiveScene->addMedia(concreteUrl, startPlay, pos); UBGraphicsMediaItem* ai = mActiveScene->addMedia(concreteUrl, startPlay, pos);
selectedDocument()->setMetaData(UBSettings::documentUpdatedAt, UBStringUtils::toUtcIsoDateTime(QDateTime::currentDateTime())); selectedDocument()->setMetaData(UBSettings::documentUpdatedAt, UBStringUtils::toUtcIsoDateTime(QDateTime::currentDateTime()));

@ -210,8 +210,8 @@ class UBBoardController : public UBDocumentContainer
void setRegularPageSize(bool checked); void setRegularPageSize(bool checked);
void stylusToolChanged(int tool); void stylusToolChanged(int tool);
void grabScene(const QRectF& pSceneRect); void grabScene(const QRectF& pSceneRect);
UBGraphicsMediaItem* addVideo(const QUrl& pUrl, bool startPlay, const QPointF& pos); UBGraphicsMediaItem* addVideo(const QUrl& pUrl, bool startPlay, const QPointF& pos, bool bUseSource = false);
UBGraphicsMediaItem* addAudio(const QUrl& pUrl, bool startPlay, const QPointF& pos); UBGraphicsMediaItem* addAudio(const QUrl& pUrl, bool startPlay, const QPointF& pos, bool bUseSource = false);
UBGraphicsWidgetItem *addW3cWidget(const QUrl& pUrl, const QPointF& pos); UBGraphicsWidgetItem *addW3cWidget(const QUrl& pUrl, const QPointF& pos);
void cut(); void cut();

@ -17,6 +17,7 @@
#include "gui/UBMainWindow.h" #include "gui/UBMainWindow.h"
#include "board/UBBoardController.h" #include "board/UBBoardController.h"
#include "board/UBBoardPaletteManager.h" #include "board/UBBoardPaletteManager.h"
#include "frameworks/UBFileSystemUtils.h"
#include "core/memcheck.h" #include "core/memcheck.h"
@ -209,7 +210,7 @@ void UBDownloadManager::onDownloadFinished(int id, bool pSuccess, QUrl sourceUrl
desc.contentTypeHeader = pContentTypeHeader; desc.contentTypeHeader = pContentTypeHeader;
emit downloadFinished(pSuccess, desc, pData); emit downloadFinished(pSuccess, desc, pData);
} else if(desc.modal) { } else if(desc.dest == sDownloadFileDesc::board) {
// The downloaded file is modal so we must put it on the board // The downloaded file is modal so we must put it on the board
emit addDownloadedFileToBoard(pSuccess, sourceUrl, pContentTypeHeader, pData, pPos, pSize, isBackground); emit addDownloadedFileToBoard(pSuccess, sourceUrl, pContentTypeHeader, pData, pPos, pSize, isBackground);
} }
@ -302,15 +303,24 @@ void UBDownloadManager::updateFileCurrentSize(int id, qint64 received, qint64 to
*/ */
void UBDownloadManager::startFileDownload(sDownloadFileDesc desc) void UBDownloadManager::startFileDownload(sDownloadFileDesc desc)
{ {
UBDownloadHttpFile* http = new UBDownloadHttpFile(desc.id, this); if (desc.srcUrl.startsWith("file://") || desc.srcUrl.startsWith("/"))
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))); UBAsyncLocalFileDownloader * cpHelper = new UBAsyncLocalFileDownloader(desc, this);
connect(cpHelper, SIGNAL(signal_asyncCopyFinished(int, bool, QUrl, QString, QByteArray, QPointF, QSize, bool)), this, SLOT(onDownloadFinished(int, bool, QUrl, QString, QByteArray, QPointF, QSize, bool)));
//the desc.url is encoded. So we have to decode it before. cpHelper->copyFile(QUrl(desc.srcUrl).toLocalFile(), QUrl(desc.dstUrl).toLocalFile(), true);
QUrl url; }
url.setEncodedUrl(desc.url.toUtf8()); else
// We send here the request and store its reply in order to be able to cancel it if needed {
mReplies[desc.id] = http->get(url, desc.pos, desc.size, desc.isBackground); 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)));
//the desc.srcUrl is encoded. So we have to decode it before.
QUrl url;
url.setEncodedUrl(desc.srcUrl.toUtf8());
// We send here the request and store its reply in order to be able to cancel it if needed
mReplies[desc.id] = http->get(url, desc.pos, desc.size, desc.isBackground);
}
} }
/** /**

@ -52,7 +52,8 @@ struct sDownloadFileDesc
int id; int id;
int totalSize; int totalSize;
int currentSize; int currentSize;
QString url; QString srcUrl;
QString dstUrl;
QString contentTypeHeader; QString contentTypeHeader;
bool modal; bool modal;
QPointF pos; // For board drop only QPointF pos; // For board drop only

@ -73,7 +73,11 @@ UBGraphicsMediaItem::UBGraphicsMediaItem(const QUrl& pMediaFileUrl, QGraphicsIte
mMediaObject = new Phonon::MediaObject(this); mMediaObject = new Phonon::MediaObject(this);
if (pMediaFileUrl.toLocalFile().contains("videos")) QString mediaPath = pMediaFileUrl.toString();
if ("" == mediaPath)
mediaPath = pMediaFileUrl.toLocalFile();
if (mediaPath.toLower().contains("videos"))
{ {
mMediaType = mediaType_Video; mMediaType = mediaType_Video;
@ -91,7 +95,7 @@ UBGraphicsMediaItem::UBGraphicsMediaItem(const QUrl& pMediaFileUrl, QGraphicsIte
haveLinkedImage = true; haveLinkedImage = true;
} }
else else
if (pMediaFileUrl.toLocalFile().contains("audios")) if (mediaPath.toLower().contains("audios"))
{ {
mMediaType = mediaType_Audio; mMediaType = mediaType_Audio;
mAudioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this); mAudioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);

@ -1216,6 +1216,11 @@ void UBGraphicsScene::textUndoCommandAdded(UBGraphicsTextItem *textItem)
} }
UBGraphicsMediaItem* UBGraphicsScene::addMedia(const QUrl& pMediaFileUrl, bool shouldPlayAsap, const QPointF& pPos) UBGraphicsMediaItem* UBGraphicsScene::addMedia(const QUrl& pMediaFileUrl, bool shouldPlayAsap, const QPointF& pPos)
{ {
qDebug() << pMediaFileUrl.toLocalFile();
if (!QFile::exists(pMediaFileUrl.toLocalFile()))
if (!QFile::exists(pMediaFileUrl.toString()))
return NULL;
UBGraphicsMediaItem* mediaItem = new UBGraphicsMediaItem(pMediaFileUrl); UBGraphicsMediaItem* mediaItem = new UBGraphicsMediaItem(pMediaFileUrl);
if(mediaItem){ if(mediaItem){
connect(UBApplication::boardController, SIGNAL(activeSceneChanged()), mediaItem, SLOT(activeSceneChanged())); connect(UBApplication::boardController, SIGNAL(activeSceneChanged()), mediaItem, SLOT(activeSceneChanged()));

@ -22,6 +22,8 @@
#include "board/UBBoardController.h" #include "board/UBBoardController.h"
#include "document/UBDocumentContainer.h" #include "document/UBDocumentContainer.h"
#include "core/UBPersistenceManager.h"
#include "globals/UBGlobals.h" #include "globals/UBGlobals.h"
THIRD_PARTY_WARNINGS_DISABLE THIRD_PARTY_WARNINGS_DISABLE
@ -856,3 +858,91 @@ QString UBFileSystemUtils::readTextFile(QString path)
return ""; return "";
} }
UBCopyThread::UBCopyThread(QObject *parent)
: QThread(parent)
{
}
void UBCopyThread::copyFile(const QString &source, const QString &destination, bool overwrite)
{
if (!QFile::exists(source)) {
qDebug() << "file" << source << "does not present in fs";
return;
}
QString normalizedDestination = destination;
if (QFile::exists(normalizedDestination)) {
if (QFileInfo(normalizedDestination).isFile() && overwrite) {
QFile::remove(normalizedDestination);
}
} else {
normalizedDestination = normalizedDestination.replace(QString("\\"), QString("/"));
int pos = normalizedDestination.lastIndexOf("/");
if (pos != -1) {
QString newpath = normalizedDestination.left(pos);
if (!QDir().mkpath(newpath)) {
qDebug() << "can't create a new path at " << newpath;
}
}
}
mFrom = source;
mTo = normalizedDestination;
start();
}
void UBCopyThread::run()
{
QString mimeType = UBFileSystemUtils::mimeTypeFromFileName(mFrom);
int position=mimeType.indexOf(";");
if(position != -1)
mimeType=mimeType.left(position);
UBMimeType::Enum itemMimeType = UBFileSystemUtils::mimeTypeFromString(mimeType);
QString destDirectory;
if (UBMimeType::Video == itemMimeType)
destDirectory = UBPersistenceManager::videoDirectory;
else
if (UBMimeType::Audio == itemMimeType)
destDirectory = UBPersistenceManager::audioDirectory;
QString uuid = QUuid::createUuid();
UBPersistenceManager::persistenceManager()->addFileToDocument(UBApplication::boardController->selectedDocument(),
mFrom,
destDirectory,
uuid,
mTo,
NULL);
emit finished(mTo);
}
UBAsyncLocalFileDownloader::UBAsyncLocalFileDownloader(sDownloadFileDesc desc, QObject *parent)
: QObject(parent)
, mDesc(desc)
{
}
void UBAsyncLocalFileDownloader::copyFile(QString &source, QString &destination, bool bOverwrite)
{
mFrom = source;
mTo = destination;
UBCopyThread *cpThread = new UBCopyThread(this); // possible memory leak. Delete helper at signal_asyncCopyFinished() handler
connect(cpThread, SIGNAL(finished(QString)), this, SLOT(slot_asyncCopyFinished(QString)));
cpThread->copyFile(source, destination, bOverwrite);
}
void UBAsyncLocalFileDownloader::slot_asyncCopyFinished(QString resUrl)
{
emit signal_asyncCopyFinished(mDesc.id, !resUrl.isEmpty(), QUrl(resUrl), "", NULL, mDesc.pos, mDesc.size, mDesc.isBackground);
}

@ -17,14 +17,57 @@
#define UBFILESYSTEMUTILS_H_ #define UBFILESYSTEMUTILS_H_
#include <QtCore> #include <QtCore>
#include <QThread>
#include "core/UB.h" #include "core/UB.h"
#include "core/UBDownloadManager.h"
class UBCopyThread : public QThread
{
Q_OBJECT
public:
explicit UBCopyThread(QObject *parent = 0);
void copyFile(const QString &source, const QString &destination, bool overwrite);
void run();
signals:
void finished(QString resUrl);
private:
QString mFrom;
QString mTo;
};
class UBAsyncLocalFileDownloader : public QObject
{
Q_OBJECT
public:
UBAsyncLocalFileDownloader(sDownloadFileDesc desc, QObject *parent = 0);
void copyFile(QString &source, QString &destination, bool bOverwrite);
public slots:
void slot_asyncCopyFinished(QString resUrl);
signals:
void signal_asyncCopyFinished(int id, bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground);
private:
QString mFrom;
QString mTo;
sDownloadFileDesc mDesc;
};
class QuaZipFile; class QuaZipFile;
class UBProcessingProgressListener; class UBProcessingProgressListener;
class UBFileSystemUtils class UBFileSystemUtils : public QObject
{ {
Q_OBJECT
public: public:
UBFileSystemUtils(); UBFileSystemUtils();

@ -1048,8 +1048,8 @@ void UBFeatureProperties::onAddToLib()
desc.modal = false; desc.modal = false;
desc.name = QFileInfo( mpElement->getFullPath().toString()).fileName(); desc.name = QFileInfo( mpElement->getFullPath().toString()).fileName();
qDebug() << desc.name; qDebug() << desc.name;
desc.url = mpElement->getFullPath().toString(); desc.srcUrl = mpElement->getFullPath().toString();
qDebug() << desc.url; qDebug() << desc.srcUrl;
UBDownloadManager::downloadManager()->addFileToDownload(desc); UBDownloadManager::downloadManager()->addFileToDownload(desc);
} }
} }

Loading…
Cancel
Save