fixed a few compilation errors in RsCollection handling

This commit is contained in:
csoler 2024-02-24 15:55:21 +01:00
parent 258fe58547
commit 206da93d99
6 changed files with 181 additions and 121 deletions

View file

@ -597,32 +597,35 @@ void SearchDialog::collOpen()
if (rsFiles->FileDetails(hash, RS_FILE_HINTS_EXTRA | RS_FILE_HINTS_LOCAL if (rsFiles->FileDetails(hash, RS_FILE_HINTS_EXTRA | RS_FILE_HINTS_LOCAL
| RS_FILE_HINTS_BROWSABLE | RS_FILE_HINTS_NETWORK_WIDE | RS_FILE_HINTS_BROWSABLE | RS_FILE_HINTS_NETWORK_WIDE
| RS_FILE_HINTS_SPEC_ONLY, info)) { | RS_FILE_HINTS_SPEC_ONLY, info))
{
/* make path for downloaded files */ /* make path for downloaded files */
std::string path; std::string path;
path = info.path; path = info.path;
/* open file with a suitable application */ /* open file with a suitable application */
QFileInfo qinfo; QFileInfo qinfo;
RsCollection::RsCollectionErrorCode err;
qinfo.setFile(QString::fromUtf8(path.c_str())); qinfo.setFile(QString::fromUtf8(path.c_str()));
if (qinfo.exists()) { if (qinfo.exists() && qinfo.absoluteFilePath().endsWith(RsCollection::ExtensionString))
if (qinfo.absoluteFilePath().endsWith(RsCollection::ExtensionString)) { RsCollection(qinfo.absoluteFilePath(),err).downloadFiles();
RsCollection collection;
if (collection.load(qinfo.absoluteFilePath())) {
collection.downloadFiles();
return;
}
}
}
} }
} }
} }
RsCollection collection; QString fileName;
if (collection.load(this)) { if (!misc::getOpenFileName(nullptr, RshareSettings::LASTDIR_EXTRAFILE, QApplication::translate("RsCollectionFile", "Open collection file"), QApplication::translate("RsCollectionFile", "Collection files") + " (*." + RsCollection::ExtensionString + ")", fileName))
return ;
std::cerr << "Got file name: " << fileName.toStdString() << std::endl;
RsCollection::RsCollectionErrorCode err;
RsCollection collection(fileName, err);
if(err == RsCollection::RsCollectionErrorCode::NO_ERROR)
collection.downloadFiles(); collection.downloadFiles();
}//if (collection.load(this)) else
QMessageBox::information(nullptr,tr("Error open RsCollection file"),RsCollection::errorString(err));
} }
void SearchDialog::downloadDirectory(const QTreeWidgetItem *item, const QString &base) void SearchDialog::downloadDirectory(const QTreeWidgetItem *item, const QString &base)

View file

@ -652,7 +652,7 @@ void SharedFilesDialog::copyLinks(const QModelIndexList& lst, bool remote,QList<
QString dir_name = QDir(QString::fromUtf8(details.name.c_str())).dirName(); QString dir_name = QDir(QString::fromUtf8(details.name.c_str())).dirName();
RetroShareLink link = RetroShareLink::createFileTree(dir_name,ft->mTotalSize,ft->mTotalFiles,QString::fromStdString(ft->toRadix64())) ; RetroShareLink link = RetroShareLink::createFileTree(dir_name,ft->totalFileSize(),ft->numFiles(),QString::fromStdString(ft->toRadix64())) ;
if(link.valid()) if(link.valid())
urls.push_back(link) ; urls.push_back(link) ;
@ -821,8 +821,12 @@ void SharedFilesDialog::collOpen()
qinfo.setFile(QString::fromUtf8(path.c_str())); qinfo.setFile(QString::fromUtf8(path.c_str()));
if (qinfo.exists()) { if (qinfo.exists()) {
if (qinfo.absoluteFilePath().endsWith(RsCollection::ExtensionString)) { if (qinfo.absoluteFilePath().endsWith(RsCollection::ExtensionString)) {
RsCollection collection;
if (collection.load(qinfo.absoluteFilePath())) { RsCollection::RsCollectionErrorCode err;
RsCollection collection(qinfo.absoluteFilePath(),err);
if(err == RsCollection::RsCollectionErrorCode::NO_ERROR)
{
collection.downloadFiles(); collection.downloadFiles();
return; return;
} }
@ -831,10 +835,17 @@ void SharedFilesDialog::collOpen()
} }
} }
RsCollection collection; QString fileName;
if (collection.load(this)) { if (!misc::getOpenFileName(nullptr, RshareSettings::LASTDIR_EXTRAFILE, QApplication::translate("RsCollectionFile", "Open collection file"), QApplication::translate("RsCollectionFile", "Collection files") + " (*." + RsCollection::ExtensionString + ")", fileName))
return ;
std::cerr << "Got file name: " << fileName.toStdString() << std::endl;
RsCollection::RsCollectionErrorCode err;
RsCollection collection(fileName,err);
if(err == RsCollection::RsCollectionErrorCode::NO_ERROR)
collection.downloadFiles(); collection.downloadFiles();
}
} }
void LocalSharedFilesDialog::playselectedfiles() void LocalSharedFilesDialog::playselectedfiles()

View file

@ -2564,23 +2564,29 @@ void TransfersDialog::collOpen()
/* open file with a suitable application */ /* open file with a suitable application */
QFileInfo qinfo; QFileInfo qinfo;
qinfo.setFile(QString::fromUtf8(path.c_str())); qinfo.setFile(QString::fromUtf8(path.c_str()));
if (qinfo.exists()) { if (qinfo.exists() && qinfo.absoluteFilePath().endsWith(RsCollection::ExtensionString))
if (qinfo.absoluteFilePath().endsWith(RsCollection::ExtensionString)) { {
RsCollection collection; RsCollection::RsCollectionErrorCode code;
if (collection.load(qinfo.absoluteFilePath())) { RsCollection(qinfo.absoluteFilePath(),code).downloadFiles();
collection.downloadFiles();
return; return;
} }
} }
} }
} }
}
}
RsCollection collection; QString fileName;
if (collection.load(this)) { if (!misc::getOpenFileName(nullptr, RshareSettings::LASTDIR_EXTRAFILE, QApplication::translate("RsCollectionFile", "Open collection file"), QApplication::translate("RsCollectionFile", "Collection files") + " (*." + RsCollection::ExtensionString + ")", fileName))
return ;
std::cerr << "Got file name: " << fileName.toStdString() << std::endl;
RsCollection::RsCollectionErrorCode code;
RsCollection collection(fileName,code);
if(code == RsCollection::RsCollectionErrorCode::NO_ERROR)
collection.downloadFiles(); collection.downloadFiles();
} else
QMessageBox::information(nullptr,tr("Error openning collection file"),RsCollection::errorString(code));
} }
void TransfersDialog::collAutoOpen(const QString &fileHash) void TransfersDialog::collAutoOpen(const QString &fileHash)
@ -2592,21 +2598,18 @@ void TransfersDialog::collAutoOpen(const QString &fileHash)
if (rsFiles->FileDetails(hash, RS_FILE_HINTS_DOWNLOAD, info)) { if (rsFiles->FileDetails(hash, RS_FILE_HINTS_DOWNLOAD, info)) {
/* make path for downloaded files */ /* make path for downloaded files */
if (info.downloadStatus == FT_STATE_COMPLETE) { if (info.downloadStatus == FT_STATE_COMPLETE)
{
std::string path; std::string path;
path = info.path + "/" + info.fname; path = info.path + "/" + info.fname;
/* open file with a suitable application */ /* open file with a suitable application */
QFileInfo qinfo; QFileInfo qinfo;
qinfo.setFile(QString::fromUtf8(path.c_str())); qinfo.setFile(QString::fromUtf8(path.c_str()));
if (qinfo.exists()) { RsCollection::RsCollectionErrorCode err;
if (qinfo.absoluteFilePath().endsWith(RsCollection::ExtensionString)) {
RsCollection collection; if (qinfo.exists() && qinfo.absoluteFilePath().endsWith(RsCollection::ExtensionString))
if (collection.load(qinfo.absoluteFilePath(), false)) { RsCollection(qinfo.absoluteFilePath(),err).autoDownloadFiles();
collection.autoDownloadFiles();
}
}
}
} }
} }
} }

View file

@ -1143,13 +1143,15 @@ QString RetroShareLink::toHtmlSize() const
if (type() == TYPE_FILE && RsCollection::isCollectionFile(name())) { if (type() == TYPE_FILE && RsCollection::isCollectionFile(name())) {
FileInfo finfo; FileInfo finfo;
if (rsFiles->FileDetails(RsFileHash(hash().toStdString()), RS_FILE_HINTS_EXTRA | RS_FILE_HINTS_LOCAL, finfo)) { if (rsFiles->FileDetails(RsFileHash(hash().toStdString()), RS_FILE_HINTS_EXTRA | RS_FILE_HINTS_LOCAL, finfo))
RsCollection collection; {
if (collection.load(QString::fromUtf8(finfo.path.c_str()), false)) { RsCollection::RsCollectionErrorCode code;
RsCollection collection(QString::fromUtf8(finfo.path.c_str()), code) ;
if(code == RsCollection::RsCollectionErrorCode::NO_ERROR)
size += QString(" [%1]").arg(misc::friendlyUnit(collection.size())); size += QString(" [%1]").arg(misc::friendlyUnit(collection.size()));
} }
} }
}
QString link = QString("<a href=\"%1\">%2</a> <font color=\"blue\">%3</font>").arg(toString()).arg(name()).arg(size); QString link = QString("<a href=\"%1\">%2</a> <font color=\"blue\">%3</font>").arg(toString()).arg(name()).arg(size);
return link; return link;
} }

View file

@ -330,41 +330,70 @@ static void showErrorBox(const QString& fileName, const QString& error)
mb.exec(); mb.exec();
} }
bool RsCollection::load(const QString& fileName, bool showError /* = true*/) QString RsCollection::errorString(RsCollectionErrorCode code)
{ {
switch(code)
{
default: [[fallthrough]] ;
case RsCollectionErrorCode::UNKNOWN_ERROR: return tr("Unknown error");
case RsCollectionErrorCode::NO_ERROR: return tr("No error");
case RsCollectionErrorCode::FILE_READ_ERROR: return tr("Error while openning file");
case RsCollectionErrorCode::FILE_CONTAINS_HARMFUL_STRINGS: return tr("Collection file contains potentially harmful code");
case RsCollectionErrorCode::INVALID_ROOT_NODE: return tr("Invalid root node. RsCollection node was expected.");
case RsCollectionErrorCode::XML_PARSING_ERROR: return tr("XML parsing error in collection file");
}
}
RsCollection::RsCollection(const QString& fileName, RsCollectionErrorCode& error)
{
if (!checkFile(fileName,error))
return ;
if (!checkFile(fileName,showError)) return false;
QFile file(fileName); QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) if (!file.open(QIODevice::ReadOnly))
{ {
std::cerr << "Cannot open file " << fileName.toStdString() << " !!" << std::endl; std::cerr << "Cannot open file " << fileName.toStdString() << " !!" << std::endl;
if (showError) { error = RsCollectionErrorCode::FILE_READ_ERROR;
showErrorBox(fileName, QApplication::translate("RsCollectionFile", "Cannot open file %1").arg(fileName)); //showErrorBox(fileName, QApplication::translate("RsCollectionFile", "Cannot open file %1").arg(fileName));
} return ;
return false;
} }
QDomDocument xml_doc; QDomDocument xml_doc;
bool ok = xml_doc.setContent(&file) ; bool ok = xml_doc.setContent(&file) ;
if(!ok)
{
error = RsCollectionErrorCode::XML_PARSING_ERROR;
return;
}
file.close(); file.close();
QDomNode root = xml_doc.elementsByTagName("RsCollection").at(0).toElement();
if(root.isNull())
{
error = RsCollectionErrorCode::INVALID_ROOT_NODE;
return;
}
return ok; recursParseXml(xml_doc,root,0);
error = RsCollectionErrorCode::NO_ERROR;
} }
// check that the file is a valid rscollection file, and not a lol bomb or some shit like this // check that the file is a valid rscollection file, and not a lol bomb or some shit like this
bool RsCollection::checkFile(const QString& fileName, bool showError)
bool RsCollection::checkFile(const QString& fileName, RsCollectionErrorCode& error)
{ {
QFile file(fileName); QFile file(fileName);
error = RsCollectionErrorCode::NO_ERROR;
if (!file.open(QIODevice::ReadOnly)) if (!file.open(QIODevice::ReadOnly))
{ {
std::cerr << "Cannot open file " << fileName.toStdString() << " !!" << std::endl; std::cerr << "Cannot open file " << fileName.toStdString() << " !!" << std::endl;
if (showError) { error = RsCollectionErrorCode::FILE_READ_ERROR;
showErrorBox(fileName, QApplication::translate("RsCollectionFile", "Cannot open file %1").arg(fileName));
} //showErrorBox(fileName, QApplication::translate("RsCollectionFile", "Cannot open file %1").arg(fileName));
return false; return false;
} }
if (file.reset()){ if (file.reset()){
@ -406,7 +435,8 @@ bool RsCollection::checkFile(const QString& fileName, bool showError)
for(uint i=0;i<bad_strings.size();++i) for(uint i=0;i<bad_strings.size();++i)
if(std::string(current,bad_strings[i].length()) == bad_strings[i]) if(std::string(current,bad_strings[i].length()) == bad_strings[i])
{ {
showErrorBox(file.fileName(), QApplication::translate("RsCollectionFile", "This file contains the string \"%1\" and is therefore an invalid collection file. \n\nIf you believe it is correct, remove the corresponding line from the file and re-open it with Retroshare.").arg(bad_strings[i].c_str())); //showErrorBox(file.fileName(), QApplication::translate("RsCollectionFile", "This file contains the string \"%1\" and is therefore an invalid collection file. \n\nIf you believe it is correct, remove the corresponding line from the file and re-open it with Retroshare.").arg(bad_strings[i].c_str()));
error = RsCollectionErrorCode::FILE_CONTAINS_HARMFUL_STRINGS;
file.close(); file.close();
return false ; return false ;
//std::cerr << "Bad string detected" << std::endl; //std::cerr << "Bad string detected" << std::endl;
@ -420,6 +450,7 @@ bool RsCollection::checkFile(const QString& fileName, bool showError)
file.close(); file.close();
return true; return true;
} }
error = RsCollectionErrorCode::UNKNOWN_ERROR;
return false; return false;
} }
@ -467,7 +498,7 @@ bool RsCollection::save(const QString& fileName) const
return true; return true;
} }
bool RsCollection::recursParseXml(QDomDocument& doc,const QDomElement& e,const RsFileTree::DirIndex parent) bool RsCollection::recursParseXml(QDomDocument& doc,const QDomNode& e,const RsFileTree::DirIndex parent)
{ {
QDomNode n = e.firstChild() ; QDomNode n = e.firstChild() ;
#ifdef COLLECTION_DEBUG #ifdef COLLECTION_DEBUG

View file

@ -58,11 +58,23 @@ class RsCollection : public QObject
Q_OBJECT Q_OBJECT
public: public:
enum class RsCollectionErrorCode:uint8_t {
NO_ERROR = 0x00,
UNKNOWN_ERROR = 0x01,
FILE_READ_ERROR = 0x02,
FILE_CONTAINS_HARMFUL_STRINGS = 0x03,
INVALID_ROOT_NODE = 0x04,
XML_PARSING_ERROR = 0x05,
};
RsCollection(QObject *parent = 0) ; RsCollection(QObject *parent = 0) ;
// create from list of files and directories // create from list of files and directories
RsCollection(const std::vector<DirDetails>& file_entries, FileSearchFlags flags, QObject *parent = 0) ; RsCollection(const std::vector<DirDetails>& file_entries, FileSearchFlags flags, QObject *parent = 0) ;
RsCollection(const RsFileTree& ft); RsCollection(const RsFileTree& ft);
RsCollection(const QString& filename,RsCollectionErrorCode& error_code);
static QString errorString(RsCollectionErrorCode code);
virtual ~RsCollection() ; virtual ~RsCollection() ;
void merge_in(const QString& fname,uint64_t size,const RsFileHash& hash) ; void merge_in(const QString& fname,uint64_t size,const RsFileHash& hash) ;
@ -74,9 +86,6 @@ public:
bool load(QWidget *parent); bool load(QWidget *parent);
bool save(QWidget *parent) const ; bool save(QWidget *parent) const ;
#endif #endif
// Loads file from disk.
bool load(const QString& fileName, bool showError = true);
// Save to disk // Save to disk
bool save(const QString& fileName) const ; bool save(const QString& fileName) const ;
@ -100,7 +109,7 @@ private slots:
private: private:
bool recursExportToXml(QDomDocument& doc,QDomElement& e,const RsFileTree::DirData& dd) const; bool recursExportToXml(QDomDocument& doc,QDomElement& e,const RsFileTree::DirData& dd) const;
bool recursParseXml(QDomDocument& doc,const QDomElement& e,RsFileTree::DirIndex dd) ; bool recursParseXml(QDomDocument& doc, const QDomNode &e, RsFileTree::DirIndex dd) ;
// This function is used to populate a RsCollection from locally or remotly shared files. // This function is used to populate a RsCollection from locally or remotly shared files.
void recursAddElements(RsFileTree::DirIndex parent, const DirDetails& dd, FileSearchFlags flags) ; void recursAddElements(RsFileTree::DirIndex parent, const DirDetails& dd, FileSearchFlags flags) ;
@ -118,7 +127,8 @@ private:
#endif #endif
// check that the file is a valid rscollection file, and not a lol bomb or some shit like this // check that the file is a valid rscollection file, and not a lol bomb or some shit like this
static bool checkFile(const QString &fileName, bool showError); static bool checkFile(const QString &fileName, RsCollectionErrorCode &error);
// Auto Download recursively. // Auto Download recursively.
void autoDownloadFiles(ColFileInfo colFileInfo, QString dlDir) const ; void autoDownloadFiles(ColFileInfo colFileInfo, QString dlDir) const ;