/*************************************:*************************** * RetroShare is distributed under the following license: * * Copyright (C) 2011 - 2011 RetroShare Team * * Cyril Soler (csoler@users.sourceforge.net) * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. ****************************************************************/ #include #include #include "RsCollection.h" #include "RsCollectionDialog.h" #include "util/misc.h" #include #include #include #include #include #include #include const QString RsCollection::ExtensionString = QString("rscollection") ; RsCollection::RsCollection(QObject *parent) : QObject(parent), _xml_doc("RsCollection") { } RsCollection::RsCollection(const FileTree& fr) : _xml_doc("RsCollection") { QDomElement root = _xml_doc.createElement("RsCollection"); _xml_doc.appendChild(root); recursAddElements(_xml_doc,fr,0,root) ; } RsCollection::RsCollection(const std::vector& file_infos, QObject *parent) : QObject(parent), _xml_doc("RsCollection") { QDomElement root = _xml_doc.createElement("RsCollection"); _xml_doc.appendChild(root); for(uint32_t i = 0;i colFileInfos ; recursCollectColFileInfos(docElem,colFileInfos,QString(),false) ; RsCollectionDialog(_fileName, colFileInfos, false).exec() ; } static QString purifyFileName(const QString& input,bool& bad) { static const QString bad_chars = "/\\\"*:?<>|" ; bad = false ; QString output = input ; for(int i=0;i& colFileInfos,const QString& current_path, bool bad_chars_in_parent) const { QDomNode n = e.firstChild() ; std::cerr << "Parsing element " << e.tagName().toStdString() << std::endl; while(!n.isNull()) { QDomElement ee = n.toElement(); // try to convert the node to an element. std::cerr << " Seeing child " << ee.tagName().toStdString() << std::endl; if(ee.tagName() == QString("File")) { ColFileInfo newChild ; newChild.hash = ee.attribute(QString("sha1")) ; bool bad_chars_detected = false ; newChild.name = purifyFileName(ee.attribute(QString("name")), bad_chars_detected) ; newChild.filename_has_wrong_characters = bad_chars_detected || bad_chars_in_parent ; newChild.size = ee.attribute(QString("size")).toULongLong() ; newChild.path = current_path ; newChild.type = DIR_TYPE_FILE ; colFileInfos.push_back(newChild) ; } else if(ee.tagName() == QString("Directory")) { ColFileInfo newParent ; bool bad_chars_detected = false ; QString cleanDirName = purifyFileName(ee.attribute(QString("name")),bad_chars_detected) ; newParent.name=cleanDirName; newParent.filename_has_wrong_characters = bad_chars_detected || bad_chars_in_parent ; newParent.size = 0; newParent.path = current_path ; newParent.type = DIR_TYPE_DIR ; recursCollectColFileInfos(ee,newParent.children,current_path + "/" + cleanDirName, bad_chars_in_parent || bad_chars_detected) ; uint32_t size = newParent.children.size(); for(uint32_t i=0;iRequestDirDetails(details.children[i].ref, subDirDetails, flags)) continue; recursAddElements(doc,subDirDetails,d) ; } e.appendChild(d) ; } } void RsCollection::recursAddElements(QDomDocument& doc,const ColFileInfo& colFileInfo,QDomElement& e) const { if (colFileInfo.type == DIR_TYPE_FILE) { QDomElement f = doc.createElement("File") ; f.setAttribute(QString("name"),colFileInfo.name) ; f.setAttribute(QString("sha1"),colFileInfo.hash) ; f.setAttribute(QString("size"),QString::number(colFileInfo.size)) ; e.appendChild(f) ; } else if (colFileInfo.type == DIR_TYPE_DIR) { QDomElement d = doc.createElement("Directory") ; d.setAttribute(QString("name"),colFileInfo.name) ; for (std::vector::const_iterator it = colFileInfo.children.begin(); it != colFileInfo.children.end(); ++it) { recursAddElements(doc,(*it),d) ; } e.appendChild(d) ; } } void RsCollection::recursAddElements(QDomDocument& doc,const FileTree& ft,uint32_t index,QDomElement& e) const { std::vector subdirs ; std::vector subfiles ; std::string name; if(!ft.getDirectoryContent(index,name,subdirs,subfiles)) return ; QDomElement d = doc.createElement("Directory") ; d.setAttribute(QString("name"),QString::fromUtf8(name.c_str())) ; e.appendChild(d) ; for (uint32_t i=0;i bad_strings ; bad_strings.push_back(std::string("= 0) { if (!file.atEnd()) file.getChar(&c); else c=0; if(c == '\t' || c == '\n' || c == '\b' || c == '\r') continue ; if (n == max_size || file.atEnd()) for(int i=0;i= 'A' && c <= 'Z') c += 'a' - 'A' ; if(!file.atEnd()) current[n] = c ; else current[n] = 0 ; //std::cerr << "n==" << n <<" Checking string " << std::string(current,n+1) << " c = " << std::hex << (int)c << std::dec << std::endl; for(uint i=0;i 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; } qulonglong RsCollection::size() { QDomElement docElem = _xml_doc.documentElement(); std::vector colFileInfos; recursCollectColFileInfos(docElem, colFileInfos, QString(),false); uint64_t size = 0; for (uint32_t i = 0; i < colFileInfos.size(); ++i) { size += colFileInfos[i].size; } return size; } bool RsCollection::isCollectionFile(const QString &fileName) { QString ext = QFileInfo(fileName).suffix().toLower(); return (ext == RsCollection::ExtensionString); } void RsCollection::saveColl(std::vector colFileInfos, const QString &fileName) { QDomElement root = _xml_doc.elementsByTagName("RsCollection").at(0).toElement(); while (root.childNodes().count()>0) root.removeChild(root.firstChild()); for(uint32_t i = 0;i