Merge pull request #554 from hunbernd/feature/paste-RS-link

Automatically detect retroshare links when pasting text
This commit is contained in:
Cyril Soler 2016-11-01 16:30:46 +01:00 committed by GitHub
commit 46dd5be2f2
3 changed files with 57 additions and 39 deletions

View File

@ -1587,46 +1587,8 @@ void RSLinkClipboard::parseClipboard(QList<RetroShareLink> &links)
{
// parse clipboard for links.
//
links.clear();
QString text = QApplication::clipboard()->text() ;
std::cerr << "Parsing clipboard:" << text.toStdString() << std::endl ;
QRegExp rx(QString("retroshare://(%1)[^\r\n]+").arg(HOST_REGEXP));
int pos = 0;
while((pos = rx.indexIn(text, pos)) != -1)
{
QString url(text.mid(pos, rx.matchedLength()));
RetroShareLink link(url);
if(link.valid())
{
// check that the link is not already in the list:
bool already = false ;
for (int i = 0; i <links.size(); ++i)
if(links[i] == link)
{
already = true ;
break ;
}
if(!already)
{
links.push_back(link) ;
#ifdef DEBUG_RSLINK
std::cerr << "captured link: " << link.toString().toStdString() << std::endl ;
#endif
}
}
#ifdef DEBUG_RSLINK
else
std::cerr << "invalid link" << std::endl ;
#endif
pos += rx.matchedLength();
}
parseText(text, links);
}
QString RSLinkClipboard::toString()
@ -1690,3 +1652,46 @@ bool RSLinkClipboard::empty(RetroShareLink::enumType type /* = RetroShareLink::T
return RetroShareLink::process(linksToProcess, flag);
}
void RSLinkClipboard::parseText(QString text, QList<RetroShareLink> &links)
{
links.clear();
std::cerr << "Parsing text:" << text.toStdString() << std::endl ;
QRegExp rx(QString("retroshare://(%1)[^\r\n]+").arg(HOST_REGEXP));
int pos = 0;
while((pos = rx.indexIn(text, pos)) != -1)
{
QString url(text.mid(pos, rx.matchedLength()));
RetroShareLink link(url);
if(link.valid())
{
// check that the link is not already in the list:
bool already = false ;
for (int i = 0; i <links.size(); ++i)
if(links[i] == link)
{
already = true ;
break ;
}
if(!already)
{
links.push_back(link) ;
#ifdef DEBUG_RSLINK
std::cerr << "captured link: " << link.toString().toStdString() << std::endl ;
#endif
}
}
#ifdef DEBUG_RSLINK
else
std::cerr << "invalid link" << std::endl ;
#endif
pos += rx.matchedLength();
}
}

View File

@ -208,6 +208,8 @@ class RSLinkClipboard
//
static int process(RetroShareLink::enumType type = RetroShareLink::TYPE_UNKNOWN, uint flag = RSLINK_PROCESS_NOTIFY_ALL);
static void parseText(QString text, QList<RetroShareLink> &links) ;
private:
static void parseClipboard(QList<RetroShareLink> &links) ;
};

View File

@ -78,6 +78,17 @@ void MimeTextEdit::insertFromMimeData(const QMimeData* source)
}
#endif
//insert retroshare links
QList<RetroShareLink> links;
RSLinkClipboard::parseText(source->text(), links);
if(links.size() > 0)
{
for(int i = 0; i < links.size(); ++i)
insertHtml(links[i].toHtml() + "<br>");
return;
}
return RSTextEdit::insertFromMimeData(source);
}