Commit 1121cff1 authored by Jaydeep Solanki's avatar Jaydeep Solanki Committed by Albert Astals Cid
Browse files

Implement Open url in browser for plain text

BUGS: 281027
REVIEW: 108614
FIXED-IN: 4.11.0
parent cbf3bddb
......@@ -8,3 +8,6 @@ target_link_libraries( parttest ${KDE4_KDECORE_LIBS} ${QT_QTGUI_LIBRARY} ${QT_QT
kde4_add_unit_test( searchtest searchtest.cpp )
target_link_libraries( searchtest ${KDE4_KDECORE_LIBS} ${QT_QTGUI_LIBRARY} ${QT_QTTEST_LIBRARY} okularcore )
kde4_add_unit_test( urldetecttest urldetecttest.cpp )
target_link_libraries( urldetecttest ${KDE4_KDECORE_LIBS} ${QT_QTTEST_LIBRARY} )
#include <qtest_kde.h>
#include "../ui/url_utils.h"
namespace Okular
{
class UrlDetectTest : public QObject
{
Q_OBJECT
private slots:
void testURL();
void testURL_data();
};
void UrlDetectTest::testURL()
{
QFETCH( QString, selectedText );
QFETCH( QString, url );
QCOMPARE( UrlUtils::getUrl( selectedText ), url );
}
void UrlDetectTest::testURL_data()
{
QTest::addColumn<QString>( "selectedText" );
QTest::addColumn<QString>( "url" );
QTest::newRow( "1" ) << QString( "asdfhttp://okular.org" ) << QString();
QTest::newRow( "2" ) << QString( "google.com/index.php/" ) << QString();
QTest::newRow( "3" ) << QString( "http://google.com)" ) << QString();
QTest::newRow( "4" ) << QString( "https://okular.org" ) << QString( "https://okular.org" );
QTest::newRow( "5" ) << QString( "www.google.com" ) << QString( "www.google.com" );
QTest::newRow( "6" ) << QString( "asdf http://okular.kde.org/" ) << QString( "http://okular.kde.org/" );
QTest::newRow( "7" ) << QString( "http://www.example.com/wpstyle/?p=364" ) << QString( "http://www.example.com/wpstyle/?p=364" );
QTest::newRow( "8" ) << QString( "asdf http://okular.org fdsa" ) << QString( "http://okular.org" );
QTest::newRow( "9" ) << QString( "http://google.com/ø" ) << QString( "http://google.com/ø" );
QTest::newRow( "10" ) << QString( "http://www.wolframalpha.com/input/?i=Plot[%281%2Be^%28-%282%29v%29%29^%28-2%29+%2B+%282%29+%281%2Be^v%29^%28-2%29%2C+{t%2C-0.5%2C+0.5}]" ) << QString( "http://www.wolframalpha.com/input/?i=Plot[%281%2Be^%28-%282%29v%29%29^%28-2%29+%2B+%282%29+%281%2Be^v%29^%28-2%29%2C+{t%2C-0.5%2C+0.5}]" );
QTest::newRow( "11" ) << QString( "http://uid:pass@example.com:8080" ) << QString( "http://uid:pass@example.com:8080" );
QTest::newRow( "12" ) << QString( "www.cs.princeton.edu/~rs/talks/LLRB/LLRB.pdf" ) << QString( "www.cs.princeton.edu/~rs/talks/LLRB/LLRB.pdf" );
QTest::newRow( "13" ) << QString( "http://IISServer/nwind?template=<ROOTxmlns:sql=\"urn:schemas-microsoft-com:xml-sql\"><sql:query>SELECTTOP2*FROM[OrderDetails]WHEREUnitPrice%26lt;10FORXMLAUTO</sql:query></ROOT>" ) << QString( "http://IISServer/nwind?template=<ROOTxmlns:sql=\"urn:schemas-microsoft-com:xml-sql\"><sql:query>SELECTTOP2*FROM[OrderDetails]WHEREUnitPrice%26lt;10FORXMLAUTO</sql:query></ROOT>" );
QTest::newRow( "14" ) << QString( "https://www.example.com/foo/?bar=baz&inga=42&quux" ) << QString( "https://www.example.com/foo/?bar=baz&inga=42&quux" );
QTest::newRow( "15" ) << QString( "http://foo.bar/#tag" ) << QString( "http://foo.bar/#tag" );
}
}
QTEST_KDEMAIN_CORE( Okular::UrlDetectTest )
#include "urldetecttest.moc"
......@@ -50,6 +50,7 @@
#include <kurifilter.h>
#include <kstringhandler.h>
#include <ktoolinvocation.h>
#include <krun.h>
// system includes
#include <math.h>
......@@ -80,6 +81,7 @@
#include "core/tile.h"
#include "settings.h"
#include "settings_core.h"
#include "url_utils.h"
static int pageflags = PagePainter::Accessibility | PagePainter::EnhanceLinks |
PagePainter::EnhanceImages | PagePainter::Highlights |
......@@ -2742,6 +2744,7 @@ void PageView::mouseReleaseEvent( QMouseEvent * e )
KMenu menu( this );
QAction *textToClipboard = menu.addAction( KIcon( "edit-copy" ), i18n( "Copy Text" ) );
QAction *speakText = 0;
QAction *httpLink = 0;
if ( Okular::Settings::useKTTSD() )
speakText = menu.addAction( KIcon( "text-speak" ), i18n( "Speak Text" ) );
if ( !d->document->isAllowed( Okular::AllowCopy ) )
......@@ -2753,6 +2756,12 @@ void PageView::mouseReleaseEvent( QMouseEvent * e )
{
addWebShortcutsMenu( &menu, d->selectedText() );
}
const QString url = UrlUtils::getUrl( d->selectedText() );
if ( !url.isEmpty() )
{
const QString squeezedText = KStringHandler::rsqueeze( url, 30 );
httpLink = menu.addAction( i18n( "Go to '%1'", squeezedText ) );
}
QAction *choice = menu.exec( e->globalPos() );
// check if the user really selected an action
if ( choice )
......@@ -2764,6 +2773,8 @@ void PageView::mouseReleaseEvent( QMouseEvent * e )
const QString text = d->selectedText();
d->tts()->say( text );
}
else if ( choice == httpLink )
new KRun( KUrl( url ), this );
}
}
}
......
#include <QRegExp>
class UrlUtils {
public:
static QString getUrl( QString txt )
{
// match the url
QRegExp reg( QString( "\\b((https?|ftp)://(www\\d{0,3}[.])?[\\S]+)|((www\\d{0,3}[.])[\\S]+)" ) );
// blocks from detecting invalid urls
QRegExp reg1( QString( "[\\w'\"\\(\\)]+https?://|[\\w'\"\\(\\)]+ftp://|[\\w'\"\\(\\)]+www\\d{0,3}[.]" ) );
txt = txt.remove( "\n" );
if( reg1.indexIn( txt ) < 0 && reg.indexIn( txt ) >= 0 && QUrl( reg.cap() ).isValid() )
{
QString url = reg.cap();
if( url.startsWith( "www" ) )
url.prepend( "http://" );
return url;
}
else
return QString();
}
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment