From 65dfcec73da9b5f5f1e0d69c473a3f4f34b1535f Mon Sep 17 00:00:00 2001 From: Ilia Ryabokon Date: Thu, 11 Oct 2012 19:31:30 +0300 Subject: [PATCH] Names of downloaded content --- src/board/UBFeaturesController.cpp | 139 ++++++++++++++++++++++------- src/board/UBFeaturesController.h | 5 +- src/core/UBDownloadManager.cpp | 2 +- src/core/UBDownloadManager.h | 2 +- src/gui/UBFeaturesWidget.cpp | 11 ++- src/gui/UBFeaturesWidget.h | 2 +- 6 files changed, 117 insertions(+), 44 deletions(-) diff --git a/src/board/UBFeaturesController.cpp b/src/board/UBFeaturesController.cpp index 53e72d5a..b05c212b 100644 --- a/src/board/UBFeaturesController.cpp +++ b/src/board/UBFeaturesController.cpp @@ -485,6 +485,71 @@ void UBFeaturesController::saveFavoriteList() file.close(); } +QString UBFeaturesController::uniqNameForFeature(const UBFeature &feature, const QString &pName, const QString &pExtention) const +{ + Q_ASSERT(featuresList); + + QStringList resultList; + QString parentVirtualPath = feature.getFullVirtualPath(); + QString resultName; + + qDebug() << "start"; + foreach (UBFeature curFeature, *featuresList) { + + if (curFeature.getFullVirtualPath().startsWith(feature.getFullVirtualPath())) { + + QString curResultName = curFeature.getFullVirtualPath(); + if (!parentVirtualPath.endsWith("/")) { + parentVirtualPath.append("/"); + } + //Cut virtual path prevfix + int i = curResultName.indexOf(feature.getFullVirtualPath()); + if (i != -1) { + curResultName = curResultName.right(curFeature.getFullVirtualPath().count() - i - parentVirtualPath.count()); + } + //if directory has children, emptying the name; + i = curResultName.indexOf("/"); + if (i != -1) { + curResultName = ""; + } + + if (!curResultName.isEmpty()) { + resultList.append(curResultName); + } + + qDebug() << curResultName; + } + } + + if (!resultList.contains(pName + pExtention, Qt::CaseInsensitive)) { + resultName = pName + pExtention; + + } else { + for (int i = 0; i < 16777215; i++) { + QString probeName = pName + "_" + QString::number(i) + pExtention; + if (!resultList.contains(probeName, Qt::CaseInsensitive)) { + resultName = probeName; + break; + } + } + } + qDebug() << "result name is " << resultName; + + return resultName; +} + +QString UBFeaturesController::adjustName(const QString &str) +{ + if (str.isNull()) { + return QString(); + } + + QString resultStr = str; + QRegExp invalidSymbols("[\\/\\s\\:\\?\\*\\|\\<\\>\\\"]+"); + + return resultStr.replace(invalidSymbols, "_"); +} + void UBFeaturesController::addToFavorite( const QUrl &path ) { QString filePath = fileNameFromUrl( path ); @@ -660,7 +725,6 @@ void UBFeaturesController::importImage( const QImage &image, const UBFeature &de } - if ( !destination.getFullVirtualPath().startsWith( picturesElement.getFullVirtualPath(), Qt::CaseInsensitive ) ) { dest = picturesElement; @@ -730,16 +794,20 @@ void UBFeaturesController::addItemAsBackground(const UBFeature &item) UBFeature UBFeaturesController::getDestinationFeatureForUrl( const QUrl &url ) { QString mimetype = UBFileSystemUtils::mimeTypeFromFileName( url.toString() ); + return getDestinationFeatureForMimeType(mimetype); +} - if ( mimetype.contains("audio") ) +UBFeature UBFeaturesController::getDestinationFeatureForMimeType(const QString &pMmimeType) +{ + if ( pMmimeType.contains("audio") ) return audiosElement; - if ( mimetype.contains("video") ) + if ( pMmimeType.contains("video") ) return moviesElement; - else if ( mimetype.contains("image") || mimetype.isEmpty()) + else if ( pMmimeType.contains("image") || pMmimeType.isEmpty()) return picturesElement; - else if ( mimetype.contains("application") ) - { - if ( mimetype.contains( "x-shockwave-flash") ) + else if ( pMmimeType.contains("application") ) + { + if ( pMmimeType.contains( "x-shockwave-flash") ) return flashElement; else return interactElement; @@ -747,48 +815,51 @@ UBFeature UBFeaturesController::getDestinationFeatureForUrl( const QUrl &url ) return UBFeature(); } -void UBFeaturesController::addDownloadedFile(const QUrl &sourceUrl, const QByteArray &pData) +void UBFeaturesController::addDownloadedFile(const QUrl &sourceUrl, const QByteArray &pData, const QString pContentSource, const QString pTitle) { - UBFeature dest = getDestinationFeatureForUrl(sourceUrl); + UBFeature dest = getDestinationFeatureForMimeType(pContentSource); - //TODO:claudio check this if (dest == UBFeature()) return; - QString fileName(""); - QString filePath(""); + QString fileName; + QString filePath; - if(UBFileSystemUtils::mimeTypeFromFileName( sourceUrl.toString() ).isEmpty()){ - fileName = tr("ImportedImage") + "-" + QDateTime::currentDateTime().toString("dd-MM-yyyy hh-mm-ss")+ ".jpg"; - filePath = dest.getFullPath().toLocalFile() + "/" + fileName; - QImage::fromData(pData).save(filePath); + //Audio item + if(dest == picturesElement) { - UBFeature downloadedFeature = UBFeature(dest.getFullVirtualPath() + "/" + fileName, getIcon( filePath, fileTypeFromUrl(filePath)), - fileName, QUrl::fromLocalFile(filePath), FEATURE_ITEM); - if (downloadedFeature != UBFeature()) { - featuresModel->addItem(downloadedFeature); - } - - } - else{ - fileName = QFileInfo( sourceUrl.toString() ).fileName(); - filePath = dest.getFullPath().toLocalFile() + "/" + fileName; - + QString UniqName = uniqNameForFeature(dest, adjustName(pTitle), ".jpg"); + fileName = !UniqName.isNull() + ? UniqName + : tr("ImportedImage") + "-" + QDateTime::currentDateTime().toString("dd-MM-yyyy hh-mm-ss")+ ".jpg"; - - QFile file( filePath ); - if ( file.open(QIODevice::WriteOnly )) - { - file.write(pData); - file.close(); + filePath = dest.getFullPath().toLocalFile() + "/" + fileName; + + QImage::fromData(pData).save(filePath); UBFeature downloadedFeature = UBFeature(dest.getFullVirtualPath() + "/" + fileName, getIcon( filePath, fileTypeFromUrl(filePath)), fileName, QUrl::fromLocalFile(filePath), FEATURE_ITEM); if (downloadedFeature != UBFeature()) { featuresModel->addItem(downloadedFeature); } + + } else { + fileName = QFileInfo( sourceUrl.toString() ).fileName(); + filePath = dest.getFullPath().toLocalFile() + "/" + fileName; + + QFile file( filePath ); + if ( file.open(QIODevice::WriteOnly )) + { + file.write(pData); + file.close(); + + UBFeature downloadedFeature = UBFeature(dest.getFullVirtualPath() + "/" + fileName, getIcon( filePath, fileTypeFromUrl(filePath)), + fileName, QUrl::fromLocalFile(filePath), FEATURE_ITEM); + if (downloadedFeature != UBFeature()) { + featuresModel->addItem(downloadedFeature); + } + } } - } } diff --git a/src/board/UBFeaturesController.h b/src/board/UBFeaturesController.h index 6c05e90b..472f8460 100644 --- a/src/board/UBFeaturesController.h +++ b/src/board/UBFeaturesController.h @@ -151,7 +151,7 @@ public: void setCurrentElement( const UBFeature &elem ) {currentElement = elem;} const UBFeature & getTrashElement () const { return trashElement; } - void addDownloadedFile( const QUrl &sourceUrl, const QByteArray &pData ); + void addDownloadedFile( const QUrl &sourceUrl, const QByteArray &pData, const QString pContentSource, const QString pTitle ); UBFeature moveItemToFolder( const QUrl &url, const UBFeature &destination ); UBFeature copyItemToFolder( const QUrl &url, const UBFeature &destination ); @@ -234,6 +234,8 @@ private: //void addImageToCurrentPage( const QString &path ); void loadFavoriteList(); void saveFavoriteList(); + QString uniqNameForFeature(const UBFeature &feature, const QString &pName = "Imported", const QString &pExtention = "") const; + QString adjustName(const QString &str); QList *featuresList; @@ -276,6 +278,7 @@ private: public: UBFeature trashElement; UBFeature getDestinationFeatureForUrl( const QUrl &url ); + UBFeature getDestinationFeatureForMimeType(const QString &pMmimeType); }; diff --git a/src/core/UBDownloadManager.cpp b/src/core/UBDownloadManager.cpp index e120038a..f1675443 100644 --- a/src/core/UBDownloadManager.cpp +++ b/src/core/UBDownloadManager.cpp @@ -283,7 +283,7 @@ void UBDownloadManager::onDownloadFinished(int id, bool pSuccess, QUrl sourceUrl } else { - emit addDownloadedFileToLibrary(pSuccess, sourceUrl, pContentTypeHeader, pData); + emit addDownloadedFileToLibrary(pSuccess, sourceUrl, pContentTypeHeader, pData, desc.name); } break; diff --git a/src/core/UBDownloadManager.h b/src/core/UBDownloadManager.h index cefa8f40..8ce7adee 100644 --- a/src/core/UBDownloadManager.h +++ b/src/core/UBDownloadManager.h @@ -132,7 +132,7 @@ signals: void downloadFinished(bool pSuccess, sDownloadFileDesc desc, QByteArray pData); void downloadModalFinished(); void addDownloadedFileToBoard(bool pSuccess, QUrl sourceUrl, QUrl contentUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground); - void addDownloadedFileToLibrary(bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData); + void addDownloadedFileToLibrary(bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData, QString pTitle); void cancelAllDownloads(); void allDownloadsFinished(); diff --git a/src/gui/UBFeaturesWidget.cpp b/src/gui/UBFeaturesWidget.cpp index caf9fb64..226f27c5 100644 --- a/src/gui/UBFeaturesWidget.cpp +++ b/src/gui/UBFeaturesWidget.cpp @@ -67,8 +67,8 @@ UBFeaturesWidget::UBFeaturesWidget(QWidget *parent, const char *name) connect(mActionBar, SIGNAL(rescanModel()), this, SLOT(rescanModel())); connect(pathListView, SIGNAL(clicked(const QModelIndex &)), this, SLOT(currentSelected(const QModelIndex &))); connect(UBApplication::boardController, SIGNAL(displayMetadata(QMap)), this, SLOT(onDisplayMetadata( QMap))); - connect(UBDownloadManager::downloadManager(), SIGNAL( addDownloadedFileToLibrary( bool, QUrl, QString, QByteArray)) - , this, SLOT(onAddDownloadedFileToLibrary(bool, QUrl, QString,QByteArray))); + connect(UBDownloadManager::downloadManager(), SIGNAL( addDownloadedFileToLibrary( bool, QUrl, QString, QByteArray, QString)) + , this, SLOT(onAddDownloadedFileToLibrary(bool, QUrl, QString,QByteArray, QString))); connect(centralWidget, SIGNAL(lockMainWidget(bool)), this, SLOT(lockIt(bool))); connect(centralWidget, SIGNAL(createNewFolderSignal(QString)), controller, SLOT(addNewFolder(QString))); connect(controller, SIGNAL(scanStarted()), centralWidget, SLOT(scanStarted())); @@ -299,12 +299,11 @@ void UBFeaturesWidget::onPreviewLoaded(int id, bool pSuccess, QUrl sourceUrl, QU centralWidget->setPropertiesThumbnail(pix); } -void UBFeaturesWidget::onAddDownloadedFileToLibrary(bool pSuccess, QUrl sourceUrl, QString pContentHeader, QByteArray pData) +void UBFeaturesWidget::onAddDownloadedFileToLibrary(bool pSuccess, QUrl sourceUrl, QString pContentHeader, QByteArray pData, QString pTitle) { - Q_UNUSED(pContentHeader) if (pSuccess) { qDebug() << pData.length(); - controller->addDownloadedFile(sourceUrl, pData); + controller->addDownloadedFile(sourceUrl, pData, pContentHeader, pTitle); controller->refreshModels(); } } @@ -1065,7 +1064,7 @@ void UBFeatureProperties::onAddToLib() desc.isBackground = false; desc.modal = false; desc.dest = sDownloadFileDesc::library; - desc.name = QFileInfo( mpElement->getFullPath().toString()).fileName(); + desc.name = mpElement->getMetadata().value("Title", QString()); qDebug() << desc.name; desc.srcUrl = mpElement->getFullPath().toString(); qDebug() << desc.srcUrl; diff --git a/src/gui/UBFeaturesWidget.h b/src/gui/UBFeaturesWidget.h index b94b3136..7363ffcb 100644 --- a/src/gui/UBFeaturesWidget.h +++ b/src/gui/UBFeaturesWidget.h @@ -87,7 +87,7 @@ private slots: void addToFavorite( const UBFeaturesMimeData *); void removeFromFavorite( const UBFeaturesMimeData * ); void onDisplayMetadata( QMap ); - void onAddDownloadedFileToLibrary(bool, QUrl, QString, QByteArray); + void onAddDownloadedFileToLibrary(bool, QUrl, QString, QByteArray, QString pTitle); void addElementsToFavorite(); void removeElementsFromFavorite(); void deleteSelectedElements();