/*************************************************************************** * Copyright (C) 2009 * * * * 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., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include #include #include #include #include #include #include "RetroShareLink.h" #define DEBUG_RSLINK 1 const QString RetroShareLink::HEADER_NAME("retroshare://file"); RetroShareLink::RetroShareLink(const QUrl& url) { // parse #ifdef DEBUG_RSLINK std::cerr << "got new RS link \"" << url.toString().toStdString() << "\"" << std::endl ; #endif QStringList list = url.toString().split ("|"); if(list.size() < 4) goto bad_link ; bool ok ; _name = list[1] ; _size = list[2].toULongLong(&ok) ; _hash = list[3].left(40) ; // normally not necessary, but it's a security. if(!ok) goto bad_link ; #ifdef DEBUG_RSLINK std::cerr << "New RetroShareLink forged:" << std::endl ; std::cerr << " name = \"" << _name.toStdString() << "\"" << std::endl ; std::cerr << " hash = \"" << _hash.toStdString() << "\"" << std::endl ; std::cerr << " size = " << _size << std::endl ; #endif check() ; return ; bad_link: #ifdef DEBUG_RSLINK std::cerr << "Wrongly formed RS link. Can't process." << std::endl ; #endif _hash = "" ; _size = 0 ; _name = "" ; } RetroShareLink::RetroShareLink(const QString & name, uint64_t size, const QString & hash) : _name(name),_size(size),_hash(hash) { check() ; } void RetroShareLink::check() { bool valid = true ; if(_size > (((uint64_t)1)<<40)) // 1TB. Who has such large files? valid = false ; if(!checkName(_name)) valid = false ; if(!checkHash(_hash)) valid = false ; if(!valid) // we should throw an exception instead of this crap, but drbob doesn't like exceptions. Why ??? { _hash = "" ; _name = "" ; _size = 0 ; } } QString RetroShareLink::toString() const { return HEADER_NAME + "|" + _name + "|" + QString::number(_size) + "|" + _hash ; } QString RetroShareLink::toHtml() const { return QString("" + name() + "" ; } QString RetroShareLink::toHtmlFull() const { return QString("" + toString() + "" ; } bool RetroShareLink::checkName(const QString& name) { if(name == "") return false ; for(int i=0;i47 && b<58) || (b>96 && b<103))) return false ; } return true ; } void RSLinkClipboard::copyLinks(const std::vector& links) { QString res ; for(uint32_t i=0;isetText(res) ; } std::vector RSLinkClipboard::pasteLinks() { return parseClipboard() ; } std::vector RSLinkClipboard::parseClipboard() { // parse clipboard for links. // std::vector links ; QString text = QApplication::clipboard()->text() ; std::cerr << "Parsing clipboard:" << text.toStdString() << std::endl ; QRegExp rx("retroshare://file") ; rx.setPatternSyntax(QRegExp::Wildcard) ; int pos = 0; while((pos = rx.indexIn(text, pos)) != -1) { QString txt = text.right(text.length()-pos) ; QStringList lst = txt.split('|') ; if(lst.size() < 4) break ; bool ok = false ; RetroShareLink link(lst[1],lst[2].toULongLong(&ok),lst[3].left(40)) ; if(link.valid()) { // check that the link is not already in the list: bool already = false ; for(uint32_t i=0;i links(parseClipboard()) ; QString res ; for(uint32_t i=0;i links(parseClipboard()) ; QString res ; for(uint32_t i=0;i" ; return res ; } QString RSLinkClipboard::toHtmlFull() { std::vector links(parseClipboard()) ; QString res ; for(uint32_t i=0;i" ; return res ; } bool RSLinkClipboard::empty() { return parseClipboard().empty() ; }