From 5071656209bd0dad327303d12413e274650e8bdf Mon Sep 17 00:00:00 2001 From: csoler Date: Sat, 24 Feb 2024 17:44:13 +0100 Subject: [PATCH] started adapting RsCollectionDialog to the new API of RsCollection --- .../src/gui/FileTransfer/SearchDialog.cpp | 18 +-- .../gui/FileTransfer/SharedFilesDialog.cpp | 18 +-- .../src/gui/FileTransfer/TransfersDialog.cpp | 30 ++--- retroshare-gui/src/gui/MainWindow.cpp | 10 +- .../src/gui/common/RsCollection.cpp | 82 ------------ retroshare-gui/src/gui/common/RsCollection.h | 6 +- .../src/gui/common/RsCollectionDialog.cpp | 126 +++++++++++++++--- .../src/gui/common/RsCollectionDialog.h | 15 ++- .../src/gui/common/RsCollectionModel.cpp | 0 .../src/gui/common/RsCollectionModel.h | 0 .../src/gui/common/RsUrlHandler.cpp | 9 +- 11 files changed, 148 insertions(+), 166 deletions(-) create mode 100644 retroshare-gui/src/gui/common/RsCollectionModel.cpp create mode 100644 retroshare-gui/src/gui/common/RsCollectionModel.h diff --git a/retroshare-gui/src/gui/FileTransfer/SearchDialog.cpp b/retroshare-gui/src/gui/FileTransfer/SearchDialog.cpp index ed2498545..a2be7bc07 100644 --- a/retroshare-gui/src/gui/FileTransfer/SearchDialog.cpp +++ b/retroshare-gui/src/gui/FileTransfer/SearchDialog.cpp @@ -30,7 +30,7 @@ #include "gui/RetroShareLink.h" #include "retroshare-gui/RsAutoUpdatePage.h" #include "gui/msgs/MessageComposer.h" -#include "gui/common/RsCollection.h" +#include "gui/common/RsCollectionDialog.h" #include "gui/common/FilesDefs.h" #include "gui/common/RsUrlHandler.h" #include "gui/settings/rsharesettings.h" @@ -542,12 +542,8 @@ void SearchDialog::collModif() /* open file with a suitable application */ QFileInfo qinfo; qinfo.setFile(QString::fromUtf8(path.c_str())); - if (qinfo.exists()) { - if (qinfo.absoluteFilePath().endsWith(RsCollection::ExtensionString)) { - RsCollection collection; - collection.openColl(qinfo.absoluteFilePath()); - }//if (qinfo.absoluteFilePath().endsWith(RsCollectionFile::ExtensionString)) - }//if (qinfo.exists()) + if (qinfo.exists() && qinfo.absoluteFilePath().endsWith(RsCollection::ExtensionString)) + RsCollectionDialog::openExistingCollection(qinfo.absoluteFilePath()); } void SearchDialog::collView() @@ -574,12 +570,8 @@ void SearchDialog::collView() /* open file with a suitable application */ QFileInfo qinfo; qinfo.setFile(QString::fromUtf8(path.c_str())); - if (qinfo.exists()) { - if (qinfo.absoluteFilePath().endsWith(RsCollection::ExtensionString)) { - RsCollection collection; - collection.openColl(qinfo.absoluteFilePath(), true); - }//if (qinfo.absoluteFilePath().endsWith(RsCollectionFile::ExtensionString)) - }//if (qinfo.exists()) + if (qinfo.exists() && qinfo.absoluteFilePath().endsWith(RsCollection::ExtensionString)) + RsCollectionDialog::openExistingCollection(qinfo.absoluteFilePath(), true); } void SearchDialog::collOpen() diff --git a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp index 4645841a9..2cd2437be 100644 --- a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp +++ b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp @@ -27,7 +27,7 @@ #include "gui/RetroShareLink.h" #include "gui/ShareManager.h" #include "gui/common/PeerDefs.h" -#include "gui/common/RsCollection.h" +#include "gui/common/RsCollectionDialog.h" #include "gui/msgs/MessageComposer.h" #include "gui/gxschannels/GxsChannelDialog.h" #include "gui/gxsforums/GxsForumsDialog.h" @@ -759,12 +759,8 @@ void SharedFilesDialog::collModif() /* open file with a suitable application */ QFileInfo qinfo; qinfo.setFile(QString::fromUtf8(path.c_str())); - if (qinfo.exists()) { - if (qinfo.absoluteFilePath().endsWith(RsCollection::ExtensionString)) { - RsCollection collection; - collection.openColl(qinfo.absoluteFilePath()); - } - } + if (qinfo.exists() && qinfo.absoluteFilePath().endsWith(RsCollection::ExtensionString)) + RsCollectionDialog::openExistingCollection(qinfo.absoluteFilePath()); } void SharedFilesDialog::collView() @@ -789,12 +785,8 @@ void SharedFilesDialog::collView() /* open file with a suitable application */ QFileInfo qinfo; qinfo.setFile(QString::fromUtf8(path.c_str())); - if (qinfo.exists()) { - if (qinfo.absoluteFilePath().endsWith(RsCollection::ExtensionString)) { - RsCollection collection; - collection.openColl(qinfo.absoluteFilePath(), true); - } - } + if (qinfo.exists() && qinfo.absoluteFilePath().endsWith(RsCollection::ExtensionString)) + RsCollectionDialog::openExistingCollection(qinfo.absoluteFilePath(), true); } void SharedFilesDialog::collOpen() diff --git a/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp b/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp index 8dca82e28..44c0260bd 100644 --- a/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp +++ b/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp @@ -24,7 +24,7 @@ #include "gui/SoundManager.h" #include "gui/RetroShareLink.h" #include "gui/common/FilesDefs.h" -#include "gui/common/RsCollection.h" +#include "gui/common/RsCollectionDialog.h" #include "gui/common/RSTreeView.h" #include "gui/common/RsUrlHandler.h" #include "gui/FileTransfer/DetailsDialog.h" @@ -2466,21 +2466,17 @@ void TransfersDialog::collCreate() std::set::iterator it ; getDLSelectedItems(&items, NULL); + RsFileTree tree; + for (it = items.begin(); it != items.end(); ++it) { FileInfo info; if (!rsFiles->FileDetails(*it, RS_FILE_HINTS_DOWNLOAD, info)) continue; - DirDetails details; - details.name = info.fname; - details.hash = info.hash; - details.size = info.size; - details.type = DIR_TYPE_FILE; - - dirVec.push_back(details); + tree.addFile(tree.root(),info.fname,info.hash,info.size); } - RsCollection(dirVec,RS_FILE_HINTS_LOCAL).openNewColl(this); + RsCollectionDialog::openNewCollection(tree); } void TransfersDialog::collModif() @@ -2504,12 +2500,8 @@ void TransfersDialog::collModif() /* open collection */ QFileInfo qinfo; qinfo.setFile(QString::fromUtf8(path.c_str())); - if (qinfo.exists()) { - if (qinfo.absoluteFilePath().endsWith(RsCollection::ExtensionString)) { - RsCollection collection; - collection.openColl(qinfo.absoluteFilePath()); - } - } + if (qinfo.exists() && qinfo.absoluteFilePath().endsWith(RsCollection::ExtensionString)) + RsCollectionDialog::openExistingCollection(qinfo.absoluteFilePath()); } } @@ -2534,12 +2526,8 @@ void TransfersDialog::collView() /* open collection */ QFileInfo qinfo; qinfo.setFile(QString::fromUtf8(path.c_str())); - if (qinfo.exists()) { - if (qinfo.absoluteFilePath().endsWith(RsCollection::ExtensionString)) { - RsCollection collection; - collection.openColl(qinfo.absoluteFilePath(), true); - } - } + if (qinfo.exists() && qinfo.absoluteFilePath().endsWith(RsCollection::ExtensionString)) + RsCollectionDialog::openExistingCollection(qinfo.absoluteFilePath(), true); } } diff --git a/retroshare-gui/src/gui/MainWindow.cpp b/retroshare-gui/src/gui/MainWindow.cpp index d69e2bb42..a35870bd1 100644 --- a/retroshare-gui/src/gui/MainWindow.cpp +++ b/retroshare-gui/src/gui/MainWindow.cpp @@ -119,7 +119,7 @@ #include "gui/statistics/StatisticsWindow.h" #include "gui/connect/ConnectFriendWizard.h" -#include "gui/common/RsCollection.h" +#include "gui/common/RsCollectionDialog.h" #include "settings/rsettingswin.h" #include "settings/rsharesettings.h" #include "common/StatusDefs.h" @@ -1623,12 +1623,8 @@ void MainWindow::retroshareLinkActivated(const QUrl &url) void MainWindow::openRsCollection(const QString &filename) { QFileInfo qinfo(filename); - if (qinfo.exists()) { - if (qinfo.absoluteFilePath().endsWith(RsCollection::ExtensionString)) { - RsCollection collection; - collection.openColl(qinfo.absoluteFilePath()); - } - } + if (qinfo.exists() && qinfo.absoluteFilePath().endsWith(RsCollection::ExtensionString)) + RsCollectionDialog::openExistingCollection(qinfo.absoluteFilePath()); } void MainWindow::processLastArgs() diff --git a/retroshare-gui/src/gui/common/RsCollection.cpp b/retroshare-gui/src/gui/common/RsCollection.cpp index 08474fb19..c1a21f9a1 100644 --- a/retroshare-gui/src/gui/common/RsCollection.cpp +++ b/retroshare-gui/src/gui/common/RsCollection.cpp @@ -615,89 +615,7 @@ bool RsCollection::save(QWidget *parent) const } -bool RsCollection::openNewColl(QWidget *parent, QString fileName) -{ - if(!misc::getSaveFileName(parent, RshareSettings::LASTDIR_EXTRAFILE - , QApplication::translate("RsCollectionFile", "Create collection file") - , QApplication::translate("RsCollectionFile", "Collection files") + " (*." + RsCollection::ExtensionString + ")" - , fileName,0, QFileDialog::DontConfirmOverwrite)) - return false; - if (!fileName.endsWith("." + RsCollection::ExtensionString)) - fileName += "." + RsCollection::ExtensionString ; - - std::cerr << "Got file name: " << fileName.toStdString() << std::endl; - - QFile file(fileName) ; - - if(file.exists()) - { - if (!checkFile(fileName,true)) return false; - - QMessageBox mb; - mb.setText(tr("Save Collection File.")); - mb.setInformativeText(tr("File already exists.")+"\n"+tr("What do you want to do?")); - QAbstractButton *btnOwerWrite = mb.addButton(tr("Overwrite"), QMessageBox::YesRole); - QAbstractButton *btnMerge = mb.addButton(tr("Merge"), QMessageBox::NoRole); - QAbstractButton *btnCancel = mb.addButton(tr("Cancel"), QMessageBox::ResetRole); - mb.setIcon(QMessageBox::Question); - mb.exec(); - - if (mb.clickedButton()==btnOwerWrite) { - //Nothing to do _xml_doc already up to date - } else if (mb.clickedButton()==btnMerge) { - //Open old file to merge it with _xml_doc - QDomDocument qddOldFile("RsCollection"); - if (qddOldFile.setContent(&file)) { - QDomElement docOldElem = qddOldFile.elementsByTagName("RsCollection").at(0).toElement(); - std::vector colOldFileInfos; - recursCollectColFileInfos(docOldElem,colOldFileInfos,QString(),false); - - QDomElement root = _xml_doc.elementsByTagName("RsCollection").at(0).toElement(); - for(uint32_t i = 0;i colFileInfos ; - - recursCollectColFileInfos(_xml_doc.documentElement(),colFileInfos,QString(),false) ; - - RsCollectionDialog* rcd = new RsCollectionDialog(fileName, colFileInfos,true); - connect(rcd,SIGNAL(saveColl(std::vector, QString)),this,SLOT(saveColl(std::vector, QString))) ; - _saved=false; - rcd->exec() ; - delete rcd; - - return _saved; -} - -bool RsCollection::openColl(const QString& fileName, bool readOnly /* = false */, bool showError /* = true*/) -{ - if (load(fileName, showError)) { - std::vector colFileInfos ; - - recursCollectColFileInfos(_xml_doc.documentElement(),colFileInfos,QString(),false) ; - - RsCollectionDialog* rcd = new RsCollectionDialog(fileName, colFileInfos, true, readOnly); - connect(rcd,SIGNAL(saveColl(std::vector, QString)),this,SLOT(saveColl(std::vector, QString))) ; - _saved=false; - rcd->exec() ; - delete rcd; - - return _saved; - } - return false; -} #endif qulonglong RsCollection::size() diff --git a/retroshare-gui/src/gui/common/RsCollection.h b/retroshare-gui/src/gui/common/RsCollection.h index 31fa777a3..32863be80 100644 --- a/retroshare-gui/src/gui/common/RsCollection.h +++ b/retroshare-gui/src/gui/common/RsCollection.h @@ -89,10 +89,8 @@ public: // Save to disk bool save(const QString& fileName) const ; - // Open new collection - bool openNewColl(QWidget *parent, QString fileName = ""); - // Open existing collection - bool openColl(const QString& fileName, bool readOnly = false, bool showError = true); + // returns the file tree + const RsFileTree& fileTree() const { return mFileTree; } // Download the content. void downloadFiles() const ; diff --git a/retroshare-gui/src/gui/common/RsCollectionDialog.cpp b/retroshare-gui/src/gui/common/RsCollectionDialog.cpp index 417af69bc..240bae85c 100644 --- a/retroshare-gui/src/gui/common/RsCollectionDialog.cpp +++ b/retroshare-gui/src/gui/common/RsCollectionDialog.cpp @@ -693,36 +693,35 @@ void RsCollectionDialog::changeFileName() std::cerr << "Got file name: " << fileName.toStdString() << std::endl; QFile file(fileName) ; + RsCollection::RsCollectionErrorCode err; - if(file.exists()) - { - RsCollection collFile; - if (!collFile.checkFile(fileName,true)) return; - + if(file.exists() && RsCollection::checkFile(fileName,err)) + { QMessageBox mb; mb.setText(tr("Save Collection File.")); mb.setInformativeText(tr("File already exists.")+"\n"+tr("What do you want to do?")); QAbstractButton *btnOwerWrite = mb.addButton(tr("Overwrite"), QMessageBox::YesRole); - QAbstractButton *btnMerge = mb.addButton(tr("Merge"), QMessageBox::NoRole); - QAbstractButton *btnCancel = mb.addButton(tr("Cancel"), QMessageBox::ResetRole); + QAbstractButton *btnMerge = mb.addButton(tr("Merge"), QMessageBox::NoRole); + QAbstractButton *btnCancel = mb.addButton(tr("Cancel"), QMessageBox::ResetRole); mb.setIcon(QMessageBox::Question); mb.exec(); if (mb.clickedButton()==btnOwerWrite) { //Nothing to do - } else if (mb.clickedButton()==btnMerge) { + } + else if(mb.clickedButton()==btnMerge) + { //Open old file to merge it with RsCollection - QDomDocument qddOldFile("RsCollection"); - if (qddOldFile.setContent(&file)) { - QDomElement docOldElem = qddOldFile.elementsByTagName("RsCollection").at(0).toElement(); - collFile.recursCollectColFileInfos(docOldElem,_newColFileInfos,QString(),false); - } - } else if (mb.clickedButton()==btnCancel) { + RsCollection qddOldFileCollection(fileName,err); + + if(err != RsCollection::RsCollectionErrorCode::NO_ERROR) + _collection.merge_in(qddOldFileCollection.fileTree()); + } + else if(mb.clickedButton()==btnCancel) return; - } else { + else return; - } } else {//if(file.exists()) //create a new empty file to check if name if good. @@ -1444,3 +1443,98 @@ void RsCollectionDialog::saveChild(QTreeWidgetItem *parentItem, ColFileInfo *par } } } + +bool RsCollectionDialog::openExistingCollection(const QString& fileName, bool readOnly /* = false */, bool showError /* = true*/) +{ +#ifdef TODO + RsCollection::RsCollectionErrorCode err; + RsCollection col(fileName,err); + + if(err != RsCollection::RsCollectionErrorCode::NO_ERROR) + { + RsCollectionDialog rcd = new RsCollectionDialog(col, true, readOnly); + return rcd.exec() ; + } + + if(showError) + QMessageBox::information(nullptr,tr("Error openning RsCollection"),RsCollection::errorString(err)); + +#endif + return false; +} + +bool RsCollectionDialog::openNewCollection(const RsFileTree& tree,const QString& proposed_file_name) +{ +#ifdef TODO + QString fileName = proposed_file_name; + + if(!misc::getSaveFileName(nullptr, RshareSettings::LASTDIR_EXTRAFILE + , QApplication::translate("RsCollectionFile", "Create collection file") + , QApplication::translate("RsCollectionFile", "Collection files") + " (*." + RsCollection::ExtensionString + ")" + , fileName,0, QFileDialog::DontConfirmOverwrite)) + return false; + + if (!fileName.endsWith("." + RsCollection::ExtensionString)) + fileName += "." + RsCollection::ExtensionString ; + + std::cerr << "Got file name: " << fileName.toStdString() << std::endl; + + QFile file(fileName) ; + + if(file.exists()) + { + RsCollection::RsCollectionErrorCode err; + if (!RsCollection::checkFile(fileName,err)) + { + QMessageBox::information(nullptr,tr("Error openning collection"),RsCollection::errorString(err)); + return false; + } + + QMessageBox mb; + mb.setText(tr("Save Collection File.")); + mb.setInformativeText(tr("File already exists.")+"\n"+tr("What do you want to do?")); + QAbstractButton *btnOwerWrite = mb.addButton(tr("Overwrite"), QMessageBox::YesRole); + QAbstractButton *btnMerge = mb.addButton(tr("Merge"), QMessageBox::NoRole); + QAbstractButton *btnCancel = mb.addButton(tr("Cancel"), QMessageBox::ResetRole); + mb.setIcon(QMessageBox::Question); + mb.exec(); + + if (mb.clickedButton()==btnOwerWrite) { + //Nothing to do _xml_doc already up to date + } else if (mb.clickedButton()==btnMerge) { + //Open old file to merge it with _xml_doc + QDomDocument qddOldFile("RsCollection"); + if (qddOldFile.setContent(&file)) { + QDomElement docOldElem = qddOldFile.elementsByTagName("RsCollection").at(0).toElement(); + std::vector colOldFileInfos; + recursCollectColFileInfos(docOldElem,colOldFileInfos,QString(),false); + + QDomElement root = _xml_doc.elementsByTagName("RsCollection").at(0).toElement(); + for(uint32_t i = 0;i colFileInfos ; + + recursCollectColFileInfos(_xml_doc.documentElement(),colFileInfos,QString(),false) ; + + RsCollectionDialog* rcd = new RsCollectionDialog(fileName, colFileInfos,true); + connect(rcd,SIGNAL(saveColl(std::vector, QString)),this,SLOT(saveColl(std::vector, QString))) ; + _saved=false; + rcd->exec() ; + delete rcd; + +#endif + return true; +} + diff --git a/retroshare-gui/src/gui/common/RsCollectionDialog.h b/retroshare-gui/src/gui/common/RsCollectionDialog.h index 43c7dbdd7..f4fd5749a 100644 --- a/retroshare-gui/src/gui/common/RsCollectionDialog.h +++ b/retroshare-gui/src/gui/common/RsCollectionDialog.h @@ -30,15 +30,20 @@ class RsCollectionDialog: public QDialog Q_OBJECT public: - RsCollectionDialog(const QString& filename - , const std::vector &colFileInfos - , const bool& creation - , const bool& readOnly = false) ; virtual ~RsCollectionDialog(); + // Open new collection + static bool openNewCollection(const RsFileTree &tree, const QString &proposed_file_name = QString()); + + // Open existing collection + static bool openExistingCollection(const QString& fileName, bool readOnly = false, bool showError = true); + protected: bool eventFilter(QObject *obj, QEvent *ev); + RsCollectionDialog(const QString& filename, const std::vector &colFileInfos, const bool& creation, + const bool& readOnly = false) ; + private slots: void directoryLoaded(QString dirLoaded); void updateSizes() ; @@ -88,4 +93,6 @@ private: QItemSelectionModel *_selectionProxy; bool _dirLoaded; QHash _listOfFilesAddedInDir; + + RsCollection _collection; }; diff --git a/retroshare-gui/src/gui/common/RsCollectionModel.cpp b/retroshare-gui/src/gui/common/RsCollectionModel.cpp new file mode 100644 index 000000000..e69de29bb diff --git a/retroshare-gui/src/gui/common/RsCollectionModel.h b/retroshare-gui/src/gui/common/RsCollectionModel.h new file mode 100644 index 000000000..e69de29bb diff --git a/retroshare-gui/src/gui/common/RsUrlHandler.cpp b/retroshare-gui/src/gui/common/RsUrlHandler.cpp index 6ac6310ac..3d005aed6 100644 --- a/retroshare-gui/src/gui/common/RsUrlHandler.cpp +++ b/retroshare-gui/src/gui/common/RsUrlHandler.cpp @@ -28,12 +28,9 @@ bool RsUrlHandler::openUrl(const QUrl& url) { if(url.scheme() == QString("file") && url.toLocalFile().endsWith("."+RsCollection::ExtensionString)) { - RsCollection collection ; - if(collection.load(url.toLocalFile())) - { - collection.downloadFiles() ; - return true; - } + RsCollection::RsCollectionErrorCode err; + RsCollection(url.toLocalFile(),err).downloadFiles() ; + return true; } return QDesktopServices::openUrl(url) ; }