fixed RS link clipboard so that links can be imported/exported from both RS and the application clipboard.

git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5.0@2579 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2010-03-19 21:24:10 +00:00
parent 61d733d6d2
commit e06b7e5d27
2 changed files with 146 additions and 23 deletions

View File

@ -19,14 +19,16 @@
#include <iostream>
#include <QStringList>
#include <QRegExp>
#include <QApplication>
#include <QMimeData>
#include <QClipboard>
#include "RetroShareLink.h"
#define DEBUG_RSLINK 1
const QString RetroShareLink::HEADER_NAME("retroshare://file");
std::vector<RetroShareLink> RSLinkClipboard::_links ;
RetroShareLink::RetroShareLink(const QUrl& url)
{
// parse
@ -100,6 +102,10 @@ QString RetroShareLink::toHtml() const
{
return QString("<a href='") + toString() + "'>" + name() + "</a>" ;
}
QString RetroShareLink::toHtmlFull() const
{
return QString("<a href='") + toString() + "'>" + toString() + "</a>" ;
}
bool RetroShareLink::checkName(const QString& name)
{
@ -147,3 +153,105 @@ bool RetroShareLink::checkHash(const QString& hash)
return true ;
}
void RSLinkClipboard::copyLinks(const std::vector<RetroShareLink>& links)
{
QString res ;
for(uint32_t i=0;i<links.size();++i)
res += links[i].toString() + "\n" ;
QApplication::clipboard()->setText(res) ;
}
std::vector<RetroShareLink> RSLinkClipboard::pasteLinks()
{
return parseClipboard() ;
}
std::vector<RetroShareLink> RSLinkClipboard::parseClipboard()
{
// parse clipboard for links.
//
std::vector<RetroShareLink> 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.size();++i)
if(links[i] == link)
{
already = true ;
break ;
}
if(!already)
{
links.push_back(link) ;
std::cerr << "captured link: " << link.toString().toStdString() << std::endl ;
}
}
else
std::cerr << "invalid link" << std::endl ;
pos += rx.matchedLength();
}
return links ;
}
QString RSLinkClipboard::toString()
{
std::vector<RetroShareLink> links(parseClipboard()) ;
QString res ;
for(uint32_t i=0;i<links.size();++i)
res += links[i].toString() + "\n" ;
return res ;
}
QString RSLinkClipboard::toHtml()
{
std::vector<RetroShareLink> links(parseClipboard()) ;
QString res ;
for(uint32_t i=0;i<links.size();++i)
res += links[i].toHtml() + "<br/>" ;
return res ;
}
QString RSLinkClipboard::toHtmlFull()
{
std::vector<RetroShareLink> links(parseClipboard()) ;
QString res ;
for(uint32_t i=0;i<links.size();++i)
res += links[i].toHtmlFull() + "<br/>" ;
return res ;
}
bool RSLinkClipboard::empty()
{
return parseClipboard().empty() ;
}

View File

@ -49,11 +49,16 @@ class RetroShareLink
/// returns the string retroshare://file|name|size|hash
QString toString() const ;
/// returns the string <a href="retroshare://file|name|size|hash">retroshare://file|name|size|hash</a>
/// returns the string <a href="retroshare://file|name|size|hash">name</a>
QString toHtml() const ;
/// returns the string <a href="retroshare://file|name|size|hash">retroshare://file|name|size|hash</a>
QString toHtmlFull() const ;
QUrl toUrl() const ;
bool valid() const { return _size > 0 ; }
bool operator==(const RetroShareLink& l) const { return _hash == l._hash ; }
private:
void check() ;
static bool checkHash(const QString& hash) ;
@ -68,32 +73,42 @@ class RetroShareLink
/// This class handles the copy/paste of links. Every member is static to ensure unicity.
/// I put no mutex, because all calls supposely com from the GUI thread.
///
/// All links are stored in html format into the clipboard. Why? Because this allows to import
/// links from both the clipboard and the RS application, e.g. paste links from an internet forum.
/// This requires many clipboard parsing operations, but this is not a problem because this code is
/// not performances-critical.
//
class RSLinkClipboard
{
public:
static void copyLinks(const std::vector<RetroShareLink>& links)
{
_links = links ;
}
static const std::vector<RetroShareLink>& pasteLinks()
{
return _links ;
}
static QString toHtml()
{
QString res ;
for(uint32_t i=0;i<_links.size();++i)
res += _links[i].toHtml() + "<br/>" ;
// Copy these links to the RS clipboard. Also copy them to the system clipboard
//
static void copyLinks(const std::vector<RetroShareLink>& links) ;
// Get the liste of pasted links, either from the internal RS links, or by default
// from the clipboard.
//
static std::vector<RetroShareLink> pasteLinks() ;
// Produces a list of links with no html structure.
static QString toString() ;
// produces a list of html links that displays with the file names only
//
static QString toHtml();
// produces a list of html links that displays the full links
//
static QString toHtmlFull();
// Returns true is no links are found to paste.
// Useful for menus.
//
static bool empty();
return res ;
}
static bool empty()
{
return _links.empty();
}
private:
static std::vector<RetroShareLink> _links ;
static std::vector<RetroShareLink> parseClipboard() ;
};