2012-02-20 21:47:14 +00:00
|
|
|
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
|
2009-01-28 20:55:45 +00:00
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
|
|
|
#include "articleview.hh"
|
2009-04-12 16:22:42 +00:00
|
|
|
#include <map>
|
2009-01-28 20:55:45 +00:00
|
|
|
#include <QMessageBox>
|
2021-12-11 16:34:37 +00:00
|
|
|
#include "weburlrequestinterceptor.h"
|
2009-01-28 20:55:45 +00:00
|
|
|
#include <QMenu>
|
2009-02-01 00:08:08 +00:00
|
|
|
#include <QDesktopServices>
|
2021-07-06 13:01:50 +00:00
|
|
|
#include <QWebEngineHistory>
|
|
|
|
#include <QWebEngineScript>
|
|
|
|
#include <QWebEngineSettings>
|
|
|
|
#include <QWebEngineScriptCollection>
|
2009-05-12 10:52:11 +00:00
|
|
|
#include <QClipboard>
|
2009-05-12 13:25:18 +00:00
|
|
|
#include <QKeyEvent>
|
2013-02-22 12:44:23 +00:00
|
|
|
#include <QFileDialog>
|
2009-05-12 10:52:11 +00:00
|
|
|
#include "folding.hh"
|
|
|
|
#include "wstring_qt.hh"
|
2010-05-29 11:33:04 +00:00
|
|
|
#include "webmultimediadownload.hh"
|
2011-05-29 05:08:37 +00:00
|
|
|
#include "programs.hh"
|
2013-11-16 18:34:09 +00:00
|
|
|
#include "gddebug.hh"
|
2011-07-02 13:04:49 +00:00
|
|
|
#include <QDebug>
|
2013-04-24 14:52:04 +00:00
|
|
|
#include <QCryptographicHash>
|
2014-02-04 13:35:42 +00:00
|
|
|
#include "gestures.hh"
|
2014-04-22 13:47:02 +00:00
|
|
|
#include "fulltextsearch.hh"
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2018-09-25 14:58:51 +00:00
|
|
|
#include <QRegularExpression>
|
|
|
|
#include "wildcard.hh"
|
2021-11-27 07:17:33 +00:00
|
|
|
#include "utils.hh"
|
2013-05-27 11:18:13 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
2010-11-14 15:38:41 +00:00
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
#include <windows.h>
|
2012-09-18 23:01:31 +00:00
|
|
|
#include <QPainter>
|
2010-11-14 15:38:41 +00:00
|
|
|
#endif
|
2012-09-18 23:01:31 +00:00
|
|
|
|
2010-01-02 18:16:22 +00:00
|
|
|
#include <QBuffer>
|
2010-03-29 13:13:29 +00:00
|
|
|
|
2014-05-12 13:53:13 +00:00
|
|
|
#if defined( Q_OS_WIN32 ) || defined( Q_OS_MAC )
|
2013-05-07 13:39:35 +00:00
|
|
|
#include "speechclient.hh"
|
|
|
|
#endif
|
|
|
|
|
2009-04-12 16:22:42 +00:00
|
|
|
using std::map;
|
2009-03-26 19:00:08 +00:00
|
|
|
using std::list;
|
|
|
|
|
2014-04-24 14:12:00 +00:00
|
|
|
/// AccentMarkHandler class
|
|
|
|
///
|
|
|
|
/// Remove accent marks from text
|
|
|
|
/// and mirror position in normalized text to original text
|
|
|
|
|
|
|
|
class AccentMarkHandler
|
|
|
|
{
|
2018-04-10 14:49:52 +00:00
|
|
|
protected:
|
2014-04-24 14:12:00 +00:00
|
|
|
QString normalizedString;
|
|
|
|
QVector< int > accentMarkPos;
|
|
|
|
public:
|
2018-04-10 14:49:52 +00:00
|
|
|
AccentMarkHandler()
|
|
|
|
{}
|
|
|
|
virtual ~AccentMarkHandler()
|
|
|
|
{}
|
2014-04-24 14:12:00 +00:00
|
|
|
static QChar accentMark()
|
|
|
|
{ return QChar( 0x301 ); }
|
|
|
|
|
|
|
|
/// Create text without accent marks
|
|
|
|
/// and store mark positions
|
2018-04-10 14:49:52 +00:00
|
|
|
virtual void setText( QString const & baseString )
|
2014-04-24 14:12:00 +00:00
|
|
|
{
|
|
|
|
accentMarkPos.clear();
|
|
|
|
normalizedString.clear();
|
|
|
|
int pos = 0;
|
|
|
|
QChar mark = accentMark();
|
|
|
|
|
|
|
|
for( int x = 0; x < baseString.length(); x++ )
|
|
|
|
{
|
|
|
|
if( baseString.at( x ) == mark )
|
|
|
|
{
|
|
|
|
accentMarkPos.append( pos );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
normalizedString.append( baseString.at( x ) );
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return text without accent marks
|
|
|
|
QString const & normalizedText() const
|
|
|
|
{ return normalizedString; }
|
|
|
|
|
|
|
|
/// Convert position into position in original text
|
|
|
|
int mirrorPosition( int const & pos ) const
|
|
|
|
{
|
|
|
|
int newPos = pos;
|
|
|
|
for( int x = 0; x < accentMarkPos.size(); x++ )
|
|
|
|
{
|
|
|
|
if( accentMarkPos.at( x ) < pos )
|
|
|
|
newPos++;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return newPos;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// End of DslAccentMark class
|
2010-01-02 18:16:22 +00:00
|
|
|
|
2018-04-10 14:49:52 +00:00
|
|
|
/// DiacriticsHandler class
|
|
|
|
///
|
|
|
|
/// Remove diacritics from text
|
|
|
|
/// and mirror position in normalized text to original text
|
|
|
|
|
|
|
|
class DiacriticsHandler : public AccentMarkHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DiacriticsHandler()
|
|
|
|
{}
|
|
|
|
~DiacriticsHandler()
|
|
|
|
{}
|
|
|
|
|
|
|
|
/// Create text without diacriticss
|
|
|
|
/// and store diacritic marks positions
|
|
|
|
virtual void setText( QString const & baseString )
|
|
|
|
{
|
|
|
|
accentMarkPos.clear();
|
|
|
|
normalizedString.clear();
|
|
|
|
|
|
|
|
gd::wstring baseText = gd::toWString( baseString );
|
|
|
|
gd::wstring normText;
|
|
|
|
|
|
|
|
int pos = 0;
|
|
|
|
normText.reserve( baseText.size() );
|
|
|
|
|
|
|
|
gd::wchar const * nextChar = baseText.data();
|
|
|
|
size_t consumed;
|
|
|
|
|
|
|
|
for( size_t left = baseText.size(); left; )
|
|
|
|
{
|
|
|
|
if( *nextChar >= 0x10000U )
|
|
|
|
{
|
|
|
|
// Will be translated into surrogate pair
|
|
|
|
normText.push_back( *nextChar );
|
|
|
|
pos += 2;
|
|
|
|
nextChar++; left--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
gd::wchar ch = Folding::foldedDiacritic( nextChar, left, consumed );
|
|
|
|
|
|
|
|
if( Folding::isCombiningMark( ch ) )
|
|
|
|
{
|
|
|
|
accentMarkPos.append( pos );
|
|
|
|
nextChar++; left--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( consumed > 1 )
|
|
|
|
{
|
|
|
|
for( size_t i = 1; i < consumed; i++ )
|
|
|
|
accentMarkPos.append( pos );
|
|
|
|
}
|
|
|
|
|
|
|
|
normText.push_back( ch );
|
|
|
|
pos += 1;
|
|
|
|
nextChar += consumed;
|
|
|
|
left -= consumed;
|
|
|
|
}
|
|
|
|
normalizedString = gd::toQString( normText );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
/// End of DiacriticsHandler class
|
2021-11-24 14:38:37 +00:00
|
|
|
|
|
|
|
void ArticleView::emitJavascriptFinished(){
|
|
|
|
emit notifyJavascriptFinished();
|
|
|
|
}
|
|
|
|
//in webengine,javascript has been executed in async mode ,for simpility,use EventLoop to simulate sync execution.
|
|
|
|
//a better solution would be to replace it with callback etc.
|
|
|
|
QString ArticleView::runJavaScriptSync(QWebEnginePage* frame, const QString& variable)
|
2021-07-06 13:01:50 +00:00
|
|
|
{
|
2021-12-12 05:35:32 +00:00
|
|
|
qDebug("%s", QString("runJavascriptScriptSync:%1").arg(variable).toLatin1().data());
|
2021-09-21 10:22:08 +00:00
|
|
|
|
2021-11-24 14:38:37 +00:00
|
|
|
QString result;
|
2021-12-01 14:26:33 +00:00
|
|
|
QSharedPointer<QEventLoop> loop = QSharedPointer<QEventLoop>(new QEventLoop());
|
|
|
|
QTimer::singleShot(1000, loop.data(), &QEventLoop::quit);
|
|
|
|
frame->runJavaScript(variable, [loop,&result](const QVariant &v)
|
2021-11-24 14:38:37 +00:00
|
|
|
{
|
2021-12-01 14:26:33 +00:00
|
|
|
if(loop->isRunning()){
|
|
|
|
if(v.isValid())
|
|
|
|
result = v.toString();
|
|
|
|
loop->quit();
|
|
|
|
}
|
2021-11-24 14:38:37 +00:00
|
|
|
});
|
2021-09-21 10:22:08 +00:00
|
|
|
|
2021-12-01 14:26:33 +00:00
|
|
|
loop->exec();
|
2021-11-24 14:38:37 +00:00
|
|
|
return result;
|
2013-05-29 07:03:37 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 08:59:16 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
char const * const scrollToPrefix = "gdfrom-";
|
|
|
|
|
|
|
|
bool isScrollTo( QString const & id )
|
|
|
|
{
|
|
|
|
return id.startsWith( scrollToPrefix );
|
|
|
|
}
|
|
|
|
|
|
|
|
QString dictionaryIdFromScrollTo( QString const & scrollTo )
|
|
|
|
{
|
|
|
|
Q_ASSERT( isScrollTo( scrollTo ) );
|
|
|
|
const int scrollToPrefixLength = 7;
|
|
|
|
return scrollTo.mid( scrollToPrefixLength );
|
|
|
|
}
|
|
|
|
|
|
|
|
} // unnamed namespace
|
|
|
|
|
|
|
|
QString ArticleView::scrollToFromDictionaryId( QString const & dictionaryId )
|
|
|
|
{
|
|
|
|
Q_ASSERT( !isScrollTo( dictionaryId ) );
|
|
|
|
return scrollToPrefix + dictionaryId;
|
|
|
|
}
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
ArticleView::ArticleView( QWidget * parent, ArticleNetworkAccessManager & nm,
|
2018-03-21 17:49:34 +00:00
|
|
|
AudioPlayerPtr const & audioPlayer_,
|
2009-04-12 16:22:42 +00:00
|
|
|
std::vector< sptr< Dictionary::Class > > const & allDictionaries_,
|
2009-04-10 21:07:03 +00:00
|
|
|
Instances::Groups const & groups_, bool popupView_,
|
2009-05-14 19:27:19 +00:00
|
|
|
Config::Class const & cfg_,
|
2014-04-16 16:18:28 +00:00
|
|
|
QAction & openSearchAction_,
|
2009-09-23 18:44:38 +00:00
|
|
|
QAction * dictionaryBarToggled_,
|
2009-05-14 19:27:19 +00:00
|
|
|
GroupComboBox const * groupComboBox_ ):
|
2009-01-28 20:55:45 +00:00
|
|
|
QFrame( parent ),
|
|
|
|
articleNetMgr( nm ),
|
2018-03-21 17:49:34 +00:00
|
|
|
audioPlayer( audioPlayer_ ),
|
2009-04-12 16:22:42 +00:00
|
|
|
allDictionaries( allDictionaries_ ),
|
2009-01-28 20:55:45 +00:00
|
|
|
groups( groups_ ),
|
2009-04-10 21:07:03 +00:00
|
|
|
popupView( popupView_ ),
|
2009-05-12 10:52:11 +00:00
|
|
|
cfg( cfg_ ),
|
2009-05-14 19:27:19 +00:00
|
|
|
pasteAction( this ),
|
2009-05-15 14:11:54 +00:00
|
|
|
articleUpAction( this ),
|
|
|
|
articleDownAction( this ),
|
2009-05-29 22:04:43 +00:00
|
|
|
goBackAction( this ),
|
|
|
|
goForwardAction( this ),
|
2013-01-18 14:37:24 +00:00
|
|
|
selectCurrentArticleAction( this ),
|
2013-05-15 13:52:47 +00:00
|
|
|
copyAsTextAction( this ),
|
2013-05-30 02:18:28 +00:00
|
|
|
inspectAction( this ),
|
2014-04-16 16:18:28 +00:00
|
|
|
openSearchAction( openSearchAction_ ),
|
2009-05-16 11:14:43 +00:00
|
|
|
searchIsOpened( false ),
|
2009-09-23 18:44:38 +00:00
|
|
|
dictionaryBarToggled( dictionaryBarToggled_ ),
|
2014-04-22 13:47:02 +00:00
|
|
|
groupComboBox( groupComboBox_ ),
|
|
|
|
ftsSearchIsOpened( false ),
|
|
|
|
ftsSearchMatchCase( false ),
|
|
|
|
ftsPosition( 0 )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
ui.setupUi( this );
|
|
|
|
|
2013-05-30 02:18:28 +00:00
|
|
|
ui.definition->setUp( const_cast< Config::Class * >( &cfg ) );
|
|
|
|
|
2009-05-29 22:04:43 +00:00
|
|
|
goBackAction.setShortcut( QKeySequence( "Alt+Left" ) );
|
|
|
|
ui.definition->addAction( &goBackAction );
|
|
|
|
connect( &goBackAction, SIGNAL( triggered() ),
|
|
|
|
this, SLOT( back() ) );
|
2009-05-15 14:24:37 +00:00
|
|
|
|
2009-05-29 22:04:43 +00:00
|
|
|
goForwardAction.setShortcut( QKeySequence( "Alt+Right" ) );
|
|
|
|
ui.definition->addAction( &goForwardAction );
|
|
|
|
connect( &goForwardAction, SIGNAL( triggered() ),
|
|
|
|
this, SLOT( forward() ) );
|
2009-05-15 14:24:37 +00:00
|
|
|
|
2021-07-06 13:01:50 +00:00
|
|
|
ui.definition->pageAction( QWebEnginePage::Copy )->setShortcut( QKeySequence::Copy );
|
|
|
|
ui.definition->addAction( ui.definition->pageAction( QWebEnginePage::Copy ) );
|
2009-04-03 21:57:23 +00:00
|
|
|
|
2021-08-21 01:41:40 +00:00
|
|
|
QAction * selectAll = ui.definition->pageAction( QWebEnginePage::SelectAll );
|
|
|
|
selectAll->setShortcut( QKeySequence::SelectAll );
|
|
|
|
selectAll->setShortcutContext( Qt::WidgetWithChildrenShortcut );
|
|
|
|
ui.definition->addAction( selectAll );
|
2013-01-18 13:22:24 +00:00
|
|
|
|
2021-08-21 01:41:40 +00:00
|
|
|
ui.definition->setContextMenuPolicy( Qt::CustomContextMenu );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2021-12-11 16:34:37 +00:00
|
|
|
//use acceptNavigationRequest method to simulate the linkclick signal
|
|
|
|
//connect( ui.definition, SIGNAL( linkClicked(QUrl) ),this,SLOT( linkClicked(QUrl ) ) );
|
|
|
|
// WebUrlRequestInterceptor *wuri = new WebUrlRequestInterceptor();
|
|
|
|
// ui.definition->page ()->profile ()->setUrlRequestInterceptor(wuri);
|
|
|
|
// connect( wuri, SIGNAL( linkClicked(QUrl) ),this,SLOT( linkClicked(QUrl ) ) );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2021-10-05 01:51:51 +00:00
|
|
|
connect( ui.definition, SIGNAL( loadFinished(bool) ),
|
|
|
|
this, SLOT( loadFinished(bool) ) );
|
2011-07-03 12:27:08 +00:00
|
|
|
|
2021-07-06 13:01:50 +00:00
|
|
|
connect( ui.definition->page(), SIGNAL( titleChanged( QString const & ) ),
|
2009-01-28 20:55:45 +00:00
|
|
|
this, SLOT( handleTitleChanged( QString const & ) ) );
|
|
|
|
|
2021-10-05 01:51:51 +00:00
|
|
|
connect( ui.definition->page(), SIGNAL( urlChanged(QUrl) ),
|
|
|
|
this, SLOT( handleUrlChanged(QUrl) ) );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
connect( ui.definition, SIGNAL( customContextMenuRequested( QPoint const & ) ),
|
|
|
|
this, SLOT( contextMenuRequested( QPoint const & ) ) );
|
|
|
|
|
2021-07-06 13:01:50 +00:00
|
|
|
connect( ui.definition->page(), SIGNAL( linkHovered ( const QString &) ),
|
|
|
|
this, SLOT( linkHovered ( const QString & ) ) );
|
2011-07-02 13:04:49 +00:00
|
|
|
|
2017-03-13 14:38:27 +00:00
|
|
|
connect( ui.definition, SIGNAL( doubleClicked( QPoint ) ),this,SLOT( doubleClicked( QPoint ) ) );
|
2010-04-08 20:37:59 +00:00
|
|
|
|
2009-05-12 10:52:11 +00:00
|
|
|
pasteAction.setShortcut( QKeySequence::Paste );
|
|
|
|
ui.definition->addAction( &pasteAction );
|
|
|
|
connect( &pasteAction, SIGNAL( triggered() ), this, SLOT( pasteTriggered() ) );
|
|
|
|
|
2009-05-15 14:11:54 +00:00
|
|
|
articleUpAction.setShortcut( QKeySequence( "Alt+Up" ) );
|
|
|
|
ui.definition->addAction( &articleUpAction );
|
|
|
|
connect( &articleUpAction, SIGNAL( triggered() ), this, SLOT( moveOneArticleUp() ) );
|
|
|
|
|
|
|
|
articleDownAction.setShortcut( QKeySequence( "Alt+Down" ) );
|
|
|
|
ui.definition->addAction( &articleDownAction );
|
|
|
|
connect( &articleDownAction, SIGNAL( triggered() ), this, SLOT( moveOneArticleDown() ) );
|
|
|
|
|
2009-05-16 11:14:43 +00:00
|
|
|
ui.definition->addAction( &openSearchAction );
|
|
|
|
connect( &openSearchAction, SIGNAL( triggered() ), this, SLOT( openSearch() ) );
|
|
|
|
|
2013-01-18 14:37:24 +00:00
|
|
|
selectCurrentArticleAction.setShortcut( QKeySequence( "Ctrl+Shift+A" ));
|
|
|
|
selectCurrentArticleAction.setText( tr( "Select Current Article" ) );
|
|
|
|
ui.definition->addAction( &selectCurrentArticleAction );
|
|
|
|
connect( &selectCurrentArticleAction, SIGNAL( triggered() ),
|
|
|
|
this, SLOT( selectCurrentArticle() ) );
|
|
|
|
|
2013-05-15 13:52:47 +00:00
|
|
|
copyAsTextAction.setShortcut( QKeySequence( "Ctrl+Shift+C" ) );
|
|
|
|
copyAsTextAction.setText( tr( "Copy as text" ) );
|
|
|
|
ui.definition->addAction( ©AsTextAction );
|
|
|
|
connect( ©AsTextAction, SIGNAL( triggered() ),
|
|
|
|
this, SLOT( copyAsText() ) );
|
|
|
|
|
2013-05-30 02:18:28 +00:00
|
|
|
inspectAction.setShortcut( QKeySequence( Qt::Key_F12 ) );
|
|
|
|
inspectAction.setText( tr( "Inspect" ) );
|
|
|
|
ui.definition->addAction( &inspectAction );
|
2021-11-30 03:40:57 +00:00
|
|
|
//connect( &inspectAction, SIGNAL( triggered() ), this, SLOT( inspect() ) );
|
|
|
|
|
2021-12-01 14:26:33 +00:00
|
|
|
QWebEnginePage *page = ui.definition->page();
|
2021-12-12 05:35:32 +00:00
|
|
|
connect(&inspectAction, &QAction::triggered, this, [page, this]() {
|
|
|
|
if (inspectView == nullptr || !inspectView->isVisible()) {
|
|
|
|
inspectView = new QWebEngineView();
|
|
|
|
page->setDevToolsPage(inspectView->page());
|
|
|
|
page->triggerAction(QWebEnginePage::InspectElement);
|
|
|
|
inspectView->show();
|
|
|
|
}
|
|
|
|
});
|
2013-05-30 02:18:28 +00:00
|
|
|
|
2009-05-12 13:25:18 +00:00
|
|
|
ui.definition->installEventFilter( this );
|
2013-01-22 21:04:45 +00:00
|
|
|
ui.searchFrame->installEventFilter( this );
|
2014-04-22 13:47:02 +00:00
|
|
|
ui.ftsSearchFrame->installEventFilter( this );
|
2009-05-12 13:25:18 +00:00
|
|
|
|
2021-07-06 13:01:50 +00:00
|
|
|
QWebEngineSettings * settings = ui.definition->page()->settings();
|
|
|
|
settings->globalSettings()->setAttribute( QWebEngineSettings::WebAttribute::LocalContentCanAccessRemoteUrls, true );
|
|
|
|
settings->globalSettings()->setAttribute( QWebEngineSettings::WebAttribute::LocalContentCanAccessFileUrls, true );
|
2009-05-11 15:33:57 +00:00
|
|
|
// Load the default blank page instantly, so there would be no flicker.
|
|
|
|
|
|
|
|
QString contentType;
|
2013-05-31 04:20:25 +00:00
|
|
|
QUrl blankPage( "gdlookup://localhost?blank=1" );
|
2009-05-11 15:33:57 +00:00
|
|
|
|
|
|
|
sptr< Dictionary::DataRequest > r = articleNetMgr.getResource( blankPage,
|
|
|
|
contentType );
|
|
|
|
|
2011-08-02 06:50:15 +00:00
|
|
|
ui.definition->setHtml( QString::fromUtf8( &( r->getFullData().front() ),
|
|
|
|
r->getFullData().size() ),
|
|
|
|
blankPage );
|
2012-09-16 10:19:47 +00:00
|
|
|
|
|
|
|
expandOptionalParts = cfg.preferences.alwaysExpandOptionalParts;
|
2014-02-04 13:35:42 +00:00
|
|
|
|
|
|
|
ui.definition->grabGesture( Gestures::GDPinchGestureType );
|
|
|
|
ui.definition->grabGesture( Gestures::GDSwipeGestureType );
|
2014-04-22 13:47:02 +00:00
|
|
|
|
|
|
|
// Variable name for store current selection range
|
|
|
|
rangeVarName = QString( "sr_%1" ).arg( QString::number( (quint64)this, 16 ) );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2012-12-27 20:32:16 +00:00
|
|
|
// explicitly report the minimum size, to avoid
|
|
|
|
// sidebar widgets' improper resize during restore
|
|
|
|
QSize ArticleView::minimumSizeHint() const
|
|
|
|
{
|
|
|
|
return ui.searchFrame->minimumSizeHint();
|
|
|
|
}
|
|
|
|
|
2009-05-14 19:27:19 +00:00
|
|
|
void ArticleView::setGroupComboBox( GroupComboBox const * g )
|
|
|
|
{
|
|
|
|
groupComboBox = g;
|
|
|
|
}
|
|
|
|
|
2009-02-06 15:37:37 +00:00
|
|
|
ArticleView::~ArticleView()
|
|
|
|
{
|
2009-02-08 21:54:19 +00:00
|
|
|
cleanupTemp();
|
2018-03-22 17:07:10 +00:00
|
|
|
audioPlayer->stop();
|
2014-02-04 13:35:42 +00:00
|
|
|
|
|
|
|
ui.definition->ungrabGesture( Gestures::GDPinchGestureType );
|
|
|
|
ui.definition->ungrabGesture( Gestures::GDSwipeGestureType );
|
2009-02-06 15:37:37 +00:00
|
|
|
}
|
|
|
|
|
2021-06-10 16:13:11 +00:00
|
|
|
void ArticleView::showDefinition( Config::InputPhrase const & phrase, unsigned group,
|
2009-05-29 19:48:50 +00:00
|
|
|
QString const & scrollTo,
|
2015-10-28 19:56:58 +00:00
|
|
|
Contexts const & contexts_ )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2013-06-23 09:53:10 +00:00
|
|
|
// first, let's stop the player
|
2018-03-22 17:07:10 +00:00
|
|
|
audioPlayer->stop();
|
2013-06-23 09:53:10 +00:00
|
|
|
|
2013-05-31 04:20:25 +00:00
|
|
|
QUrl req;
|
2015-10-28 19:56:58 +00:00
|
|
|
Contexts contexts( contexts_ );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
req.setScheme( "gdlookup" );
|
|
|
|
req.setHost( "localhost" );
|
2021-11-27 07:17:33 +00:00
|
|
|
Utils::Url::addQueryItem( req, "word", phrase.phrase );
|
2021-06-10 16:13:11 +00:00
|
|
|
if ( !phrase.punctuationSuffix.isEmpty() )
|
2021-11-27 07:17:33 +00:00
|
|
|
Utils::Url::addQueryItem( req, "punctuation_suffix", phrase.punctuationSuffix );
|
|
|
|
Utils::Url::addQueryItem( req, "group", QString::number( group ) );
|
2018-06-13 16:00:42 +00:00
|
|
|
if( cfg.preferences.ignoreDiacritics )
|
2021-11-27 07:17:33 +00:00
|
|
|
Utils::Url::addQueryItem( req, "ignore_diacritics", "1" );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-05-11 19:14:28 +00:00
|
|
|
if ( scrollTo.size() )
|
2021-11-27 07:17:33 +00:00
|
|
|
Utils::Url::addQueryItem( req, "scrollto", scrollTo );
|
2009-05-29 19:48:50 +00:00
|
|
|
|
2015-10-28 19:56:58 +00:00
|
|
|
Contexts::Iterator pos = contexts.find( "gdanchor" );
|
|
|
|
if( pos != contexts.end() )
|
|
|
|
{
|
2021-11-27 07:17:33 +00:00
|
|
|
Utils::Url::addQueryItem( req, "gdanchor", contexts[ "gdanchor" ] );
|
2015-10-28 19:56:58 +00:00
|
|
|
contexts.erase( pos );
|
|
|
|
}
|
|
|
|
|
2009-05-29 19:48:50 +00:00
|
|
|
if ( contexts.size() )
|
|
|
|
{
|
|
|
|
QBuffer buf;
|
|
|
|
|
|
|
|
buf.open( QIODevice::WriteOnly );
|
|
|
|
|
|
|
|
QDataStream stream( &buf );
|
|
|
|
|
|
|
|
stream << contexts;
|
|
|
|
|
|
|
|
buf.close();
|
|
|
|
|
2021-11-27 07:17:33 +00:00
|
|
|
Utils::Url::addQueryItem( req, "contexts", QString::fromLatin1( buf.buffer().toBase64() ) );
|
2009-05-29 19:48:50 +00:00
|
|
|
}
|
2009-05-11 19:14:28 +00:00
|
|
|
|
2009-09-23 18:44:38 +00:00
|
|
|
QString mutedDicts = getMutedForGroup( group );
|
|
|
|
|
|
|
|
if ( mutedDicts.size() )
|
2021-11-27 07:17:33 +00:00
|
|
|
Utils::Url::addQueryItem( req, "muted", mutedDicts );
|
2009-09-23 18:44:38 +00:00
|
|
|
|
2013-01-18 11:00:38 +00:00
|
|
|
// Update both histories (pages history and headwords history)
|
2009-05-29 22:04:43 +00:00
|
|
|
saveHistoryUserData();
|
2021-06-10 16:13:11 +00:00
|
|
|
emit sendWordToHistory( phrase.phrase );
|
2009-05-11 22:25:22 +00:00
|
|
|
|
2010-04-27 07:49:37 +00:00
|
|
|
// Any search opened is probably irrelevant now
|
|
|
|
closeSearch();
|
|
|
|
|
2011-06-12 23:59:35 +00:00
|
|
|
// Clear highlight all button selection
|
|
|
|
ui.highlightAllButton->setChecked(false);
|
|
|
|
|
2012-09-16 10:19:47 +00:00
|
|
|
emit setExpandMode( expandOptionalParts );
|
|
|
|
|
2009-04-11 20:37:11 +00:00
|
|
|
ui.definition->load( req );
|
2009-05-11 22:25:22 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
//QApplication::setOverrideCursor( Qt::WaitCursor );
|
|
|
|
ui.definition->setCursor( Qt::WaitCursor );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2021-06-10 16:13:11 +00:00
|
|
|
void ArticleView::showDefinition( QString const & word, unsigned group,
|
|
|
|
QString const & scrollTo,
|
|
|
|
Contexts const & contexts_ )
|
|
|
|
{
|
|
|
|
showDefinition( Config::InputPhrase::fromPhrase( word ), group, scrollTo, contexts_ );
|
|
|
|
}
|
|
|
|
|
2014-04-22 13:47:02 +00:00
|
|
|
void ArticleView::showDefinition( QString const & word, QStringList const & dictIDs,
|
2018-04-10 14:49:52 +00:00
|
|
|
QRegExp const & searchRegExp, unsigned group,
|
|
|
|
bool ignoreDiacritics )
|
2014-04-16 16:18:28 +00:00
|
|
|
{
|
|
|
|
if( dictIDs.isEmpty() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// first, let's stop the player
|
2018-03-22 17:07:10 +00:00
|
|
|
audioPlayer->stop();
|
2014-04-16 16:18:28 +00:00
|
|
|
|
|
|
|
QUrl req;
|
|
|
|
|
|
|
|
req.setScheme( "gdlookup" );
|
|
|
|
req.setHost( "localhost" );
|
2021-11-27 07:17:33 +00:00
|
|
|
Utils::Url::addQueryItem( req, "word", word );
|
|
|
|
Utils::Url::addQueryItem( req, "dictionaries", dictIDs.join( ",") );
|
|
|
|
Utils::Url::addQueryItem( req, "regexp", searchRegExp.pattern() );
|
2014-04-22 13:47:02 +00:00
|
|
|
if( searchRegExp.caseSensitivity() == Qt::CaseSensitive )
|
2021-11-27 07:17:33 +00:00
|
|
|
Utils::Url::addQueryItem( req, "matchcase", "1" );
|
2014-04-22 13:47:02 +00:00
|
|
|
if( searchRegExp.patternSyntax() == QRegExp::WildcardUnix )
|
2021-11-27 07:17:33 +00:00
|
|
|
Utils::Url::addQueryItem( req, "wildcards", "1" );
|
|
|
|
Utils::Url::addQueryItem( req, "group", QString::number( group ) );
|
2018-04-10 14:49:52 +00:00
|
|
|
if( ignoreDiacritics )
|
2021-11-27 07:17:33 +00:00
|
|
|
Utils::Url::addQueryItem( req, "ignore_diacritics", "1" );
|
2014-04-16 16:18:28 +00:00
|
|
|
|
|
|
|
// Update both histories (pages history and headwords history)
|
|
|
|
saveHistoryUserData();
|
|
|
|
emit sendWordToHistory( word );
|
|
|
|
|
|
|
|
// Any search opened is probably irrelevant now
|
|
|
|
closeSearch();
|
|
|
|
|
|
|
|
// Clear highlight all button selection
|
|
|
|
ui.highlightAllButton->setChecked(false);
|
|
|
|
|
|
|
|
emit setExpandMode( expandOptionalParts );
|
|
|
|
|
|
|
|
ui.definition->load( req );
|
|
|
|
|
|
|
|
//QApplication::setOverrideCursor( Qt::WaitCursor );
|
|
|
|
ui.definition->setCursor( Qt::WaitCursor );
|
|
|
|
}
|
|
|
|
|
2009-02-08 16:50:18 +00:00
|
|
|
void ArticleView::showAnticipation()
|
|
|
|
{
|
|
|
|
ui.definition->setHtml( "" );
|
2009-03-26 19:00:08 +00:00
|
|
|
ui.definition->setCursor( Qt::WaitCursor );
|
|
|
|
//QApplication::setOverrideCursor( Qt::WaitCursor );
|
2009-02-08 16:50:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleView::loadFinished( bool )
|
|
|
|
{
|
2021-08-14 07:25:10 +00:00
|
|
|
QUrl url = ui.definition->url();
|
|
|
|
QObject* obj=sender();
|
|
|
|
qDebug()<<"article view loaded url is :"<<url<<" sender class is :"<<obj->metaObject()->className();
|
|
|
|
|
2021-09-25 02:17:05 +00:00
|
|
|
// // See if we have any iframes in need of expansion
|
|
|
|
// ui.definition->page()->runJavaScript(QString(""
|
|
|
|
//"var frames = window.frames;"
|
|
|
|
//"for (var i=0; i < frames.length; i++) {"
|
|
|
|
//" var f = frames[i];"
|
|
|
|
//" "
|
|
|
|
//" f.onload = function()"
|
|
|
|
//" {"
|
|
|
|
//" f.style.height = "
|
|
|
|
//" f.contentWindow.document.body.scrollHeight + 'px';"
|
|
|
|
//" f.style.width = "
|
|
|
|
//" f.contentWindow.document.body.scrollWidth+'px'; "
|
|
|
|
//" }"
|
|
|
|
//" f.style.display = 'block';"
|
|
|
|
//" var gdLastUrlText;"
|
|
|
|
//" f.document.addEventListener('click', function () { gdLastUrlText = window.event.srcElement.textContent; });"
|
|
|
|
//" f.document.addEventListener('contextmenu', function () { gdLastUrlText = window.event.srcElement.textContent; });"
|
|
|
|
//"}"
|
|
|
|
//));
|
2009-05-16 18:04:21 +00:00
|
|
|
|
2021-07-06 13:01:50 +00:00
|
|
|
QVariant userDataVariant = ui.definition->property("currentArticle");
|
2009-05-29 19:48:50 +00:00
|
|
|
|
2021-07-06 13:01:50 +00:00
|
|
|
if ( userDataVariant.isValid() )
|
2009-05-29 19:48:50 +00:00
|
|
|
{
|
2009-05-29 22:04:43 +00:00
|
|
|
|
2021-07-06 13:01:50 +00:00
|
|
|
QString currentArticle = userDataVariant.toString();
|
2009-05-29 22:04:43 +00:00
|
|
|
|
2021-07-06 13:01:50 +00:00
|
|
|
if ( !currentArticle.isEmpty() )
|
2009-05-29 22:04:43 +00:00
|
|
|
{
|
|
|
|
// There's an active article saved, so set it to be active.
|
|
|
|
setCurrentArticle( currentArticle );
|
|
|
|
}
|
|
|
|
|
|
|
|
double sx = 0, sy = 0;
|
|
|
|
|
2021-07-06 13:01:50 +00:00
|
|
|
QVariant qsx=ui.definition->property("sx");
|
|
|
|
if ( qsx.type() == QVariant::Double )
|
|
|
|
sx = qsx.toDouble();
|
2009-05-29 22:04:43 +00:00
|
|
|
|
2021-07-06 13:01:50 +00:00
|
|
|
QVariant qsy = ui.definition->property("sx");
|
|
|
|
if ( qsy.type() == QVariant::Double )
|
|
|
|
sy = qsy.toDouble();
|
2009-05-29 22:04:43 +00:00
|
|
|
|
|
|
|
if ( sx != 0 || sy != 0 )
|
|
|
|
{
|
|
|
|
// Restore scroll position
|
2021-07-06 13:01:50 +00:00
|
|
|
ui.definition->page()->runJavaScript(
|
2009-05-29 22:04:43 +00:00
|
|
|
QString( "window.scroll( %1, %2 );" ).arg( sx ).arg( sy ) );
|
|
|
|
}
|
2009-05-29 19:48:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-11-27 07:17:33 +00:00
|
|
|
QString const scrollTo = Utils::Url::queryItemValue( url, "scrollto" );
|
2021-06-29 08:59:16 +00:00
|
|
|
if( isScrollTo( scrollTo ) )
|
|
|
|
{
|
|
|
|
// There is no active article saved in history, but we have it as a parameter.
|
|
|
|
// setCurrentArticle will save it and scroll there.
|
|
|
|
setCurrentArticle( scrollTo, true );
|
|
|
|
}
|
2009-05-29 19:48:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
ui.definition->unsetCursor();
|
|
|
|
//QApplication::restoreOverrideCursor();
|
2012-09-16 10:19:47 +00:00
|
|
|
|
2013-06-03 17:14:05 +00:00
|
|
|
// Expand collapsed article if only one loaded
|
2021-07-06 13:01:50 +00:00
|
|
|
ui.definition->page()->runJavaScript( QString( "gdCheckArticlesNumber();" ) );
|
2013-06-03 17:14:05 +00:00
|
|
|
|
2012-09-16 10:19:47 +00:00
|
|
|
// Jump to current article after page reloading
|
|
|
|
if( !articleToJump.isEmpty() )
|
|
|
|
{
|
|
|
|
setCurrentArticle( articleToJump, true );
|
|
|
|
articleToJump.clear();
|
|
|
|
}
|
|
|
|
|
2021-11-27 07:17:33 +00:00
|
|
|
if( !Utils::Url::queryItemValue( url, "gdanchor" ).isEmpty() )
|
2015-10-28 19:56:58 +00:00
|
|
|
{
|
2021-11-27 07:17:33 +00:00
|
|
|
QString anchor = QUrl::fromPercentEncoding( Utils::Url::encodedQueryItemValue( url, "gdanchor" ) );
|
2015-10-28 19:56:58 +00:00
|
|
|
|
|
|
|
// Find GD anchor on page
|
|
|
|
|
|
|
|
int n = anchor.indexOf( '_' );
|
2016-05-11 14:41:13 +00:00
|
|
|
if( n == 33 )
|
|
|
|
// MDict pattern: ("g" + dictionary ID (33 chars total))_(articleID(quint64, hex))_(original anchor)
|
2015-10-28 19:56:58 +00:00
|
|
|
n = anchor.indexOf( '_', n + 1 );
|
2015-10-30 18:01:38 +00:00
|
|
|
else
|
|
|
|
n = 0;
|
2015-10-28 19:56:58 +00:00
|
|
|
|
|
|
|
if( n > 0 )
|
|
|
|
{
|
|
|
|
QString originalAnchor = anchor.mid( n + 1 );
|
|
|
|
|
2021-07-06 13:01:50 +00:00
|
|
|
int end = originalAnchor.indexOf('_');
|
|
|
|
QString hash=originalAnchor.left(end);
|
|
|
|
url.clear();
|
|
|
|
url.setFragment(hash);
|
|
|
|
ui.definition->page()->runJavaScript(
|
|
|
|
QString("window.location.hash = \"%1\"").arg(QString::fromUtf8(url.toEncoded())));
|
|
|
|
|
2015-10-28 19:56:58 +00:00
|
|
|
}
|
2015-10-30 18:01:38 +00:00
|
|
|
else
|
|
|
|
{
|
2016-04-01 13:38:07 +00:00
|
|
|
url.clear();
|
2015-10-30 18:01:38 +00:00
|
|
|
url.setFragment( anchor );
|
2021-07-06 13:01:50 +00:00
|
|
|
ui.definition->page()->runJavaScript(
|
2016-04-01 13:38:07 +00:00
|
|
|
QString( "window.location.hash = \"%1\"" ).arg( QString::fromUtf8( url.toEncoded() ) ) );
|
2015-10-30 18:01:38 +00:00
|
|
|
}
|
2015-10-28 19:56:58 +00:00
|
|
|
}
|
|
|
|
|
2009-05-14 19:27:19 +00:00
|
|
|
emit pageLoaded( this );
|
2014-04-22 13:47:02 +00:00
|
|
|
|
2021-11-27 07:17:33 +00:00
|
|
|
if( Utils::Url::hasQueryItem( ui.definition->url(), "regexp" ) )
|
2014-04-22 13:47:02 +00:00
|
|
|
highlightFTSResults();
|
2009-02-08 16:50:18 +00:00
|
|
|
}
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
void ArticleView::handleTitleChanged( QString const & title )
|
|
|
|
{
|
2016-04-01 13:38:07 +00:00
|
|
|
if( !title.isEmpty() ) // Qt 5.x WebKit raise signal titleChanges(QString()) while navigation within page
|
|
|
|
emit titleChanged( this, title );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleView::handleUrlChanged( QUrl const & url )
|
|
|
|
{
|
|
|
|
QIcon icon;
|
|
|
|
|
2009-04-10 12:48:40 +00:00
|
|
|
unsigned group = getGroup( url );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-10 12:48:40 +00:00
|
|
|
if ( group )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
// Find the group's instance corresponding to the fragment value
|
|
|
|
for( unsigned x = 0; x < groups.size(); ++x )
|
2009-04-10 12:48:40 +00:00
|
|
|
if ( groups[ x ].id == group )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
// Found it
|
|
|
|
|
2010-07-05 19:47:22 +00:00
|
|
|
icon = groups[ x ].makeIcon();
|
|
|
|
break;
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
emit iconChanged( this, icon );
|
|
|
|
}
|
|
|
|
|
2009-04-10 12:48:40 +00:00
|
|
|
unsigned ArticleView::getGroup( QUrl const & url )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2021-11-27 07:17:33 +00:00
|
|
|
if ( url.scheme() == "gdlookup" && Utils::Url::hasQueryItem( url, "group" ) )
|
|
|
|
return Utils::Url::queryItemValue( url, "group" ).toUInt();
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-10 12:48:40 +00:00
|
|
|
return 0;
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2009-05-11 22:25:22 +00:00
|
|
|
QStringList ArticleView::getArticlesList()
|
2021-12-13 14:45:16 +00:00
|
|
|
{
|
|
|
|
return runJavaScriptSync( ui.definition->page(), "gdArticleContents" )
|
|
|
|
.trimmed().split( ' ', QString::SkipEmptyParts );
|
2009-05-11 22:25:22 +00:00
|
|
|
}
|
|
|
|
|
2011-07-03 12:27:08 +00:00
|
|
|
QString ArticleView::getActiveArticleId()
|
|
|
|
{
|
|
|
|
QString currentArticle = getCurrentArticle();
|
2021-06-29 08:59:16 +00:00
|
|
|
if ( !isScrollTo( currentArticle ) )
|
2011-07-03 12:27:08 +00:00
|
|
|
return QString(); // Incorrect id
|
|
|
|
|
2021-06-29 08:59:16 +00:00
|
|
|
return dictionaryIdFromScrollTo( currentArticle );
|
2011-07-03 12:27:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-11 19:14:28 +00:00
|
|
|
QString ArticleView::getCurrentArticle()
|
|
|
|
{
|
2021-12-13 14:45:16 +00:00
|
|
|
QVariant v=ui.definition->property("currentArticle");
|
2009-05-11 19:14:28 +00:00
|
|
|
|
|
|
|
if ( v.type() == QVariant::String )
|
|
|
|
return v.toString();
|
|
|
|
else
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2013-06-28 16:00:13 +00:00
|
|
|
void ArticleView::jumpToDictionary( QString const & id, bool force )
|
2011-06-05 11:49:50 +00:00
|
|
|
{
|
2021-06-29 08:59:16 +00:00
|
|
|
QString targetArticle = scrollToFromDictionaryId( id );
|
2011-07-03 12:27:08 +00:00
|
|
|
|
2013-06-28 16:00:13 +00:00
|
|
|
// jump only if neceessary, or when forced
|
|
|
|
if ( force || targetArticle != getCurrentArticle() )
|
2011-07-03 12:27:08 +00:00
|
|
|
{
|
|
|
|
setCurrentArticle( targetArticle, true );
|
|
|
|
}
|
2011-06-05 11:49:50 +00:00
|
|
|
}
|
|
|
|
|
2009-05-15 14:11:54 +00:00
|
|
|
void ArticleView::setCurrentArticle( QString const & id, bool moveToIt )
|
2009-05-11 22:25:22 +00:00
|
|
|
{
|
2021-06-29 08:59:16 +00:00
|
|
|
if ( !isScrollTo( id ) )
|
2009-05-11 22:25:22 +00:00
|
|
|
return; // Incorrect id
|
|
|
|
|
2014-02-09 15:00:48 +00:00
|
|
|
if ( !ui.definition->isVisible() )
|
|
|
|
return; // No action on background page, scrollIntoView there don't work
|
|
|
|
|
2021-10-05 01:23:30 +00:00
|
|
|
if(moveToIt){
|
2021-10-20 15:44:24 +00:00
|
|
|
QString script=QString(" var elem=document.getElementById('%1'); if(elem!=undefined){elem.scrollIntoView(true);}").arg(id);
|
2021-10-05 01:23:30 +00:00
|
|
|
|
|
|
|
ui.definition->page()->runJavaScript(script);
|
|
|
|
|
|
|
|
onJsActiveArticleChanged(id);
|
|
|
|
|
|
|
|
ui.definition->setProperty("currentArticle",id);
|
|
|
|
}
|
2009-05-11 22:25:22 +00:00
|
|
|
}
|
|
|
|
|
2013-01-18 14:37:24 +00:00
|
|
|
void ArticleView::selectCurrentArticle()
|
|
|
|
{
|
2021-07-06 13:01:50 +00:00
|
|
|
ui.definition->page()->runJavaScript(
|
2013-01-18 14:37:24 +00:00
|
|
|
QString( "gdSelectArticle( '%1' );" ).arg( getActiveArticleId() ) );
|
|
|
|
}
|
|
|
|
|
2009-05-29 19:48:50 +00:00
|
|
|
bool ArticleView::isFramedArticle( QString const & ca )
|
|
|
|
{
|
|
|
|
if ( ca.isEmpty() )
|
|
|
|
return false;
|
|
|
|
|
2021-11-19 13:47:22 +00:00
|
|
|
runJavaScriptSync( ui.definition->page(), QString( "!!document.getElementById('gdexpandframe-%1');" ).arg( ca.mid( 7 ) ) );
|
|
|
|
return false;
|
2009-05-29 19:48:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ArticleView::isExternalLink( QUrl const & url )
|
|
|
|
{
|
|
|
|
return url.scheme() == "http" || url.scheme() == "https" ||
|
2010-06-30 16:43:08 +00:00
|
|
|
url.scheme() == "ftp" || url.scheme() == "mailto" ||
|
|
|
|
url.scheme() == "file";
|
2009-05-29 19:48:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleView::tryMangleWebsiteClickedUrl( QUrl & url, Contexts & contexts )
|
|
|
|
{
|
2010-05-29 11:33:04 +00:00
|
|
|
// Don't try mangling audio urls, even if they are from the framed websites
|
|
|
|
|
|
|
|
if( ( url.scheme() == "http" || url.scheme() == "https" )
|
|
|
|
&& ! Dictionary::WebMultimediaDownload::isAudioUrl( url ) )
|
2009-05-29 19:48:50 +00:00
|
|
|
{
|
|
|
|
// Maybe a link inside a website was clicked?
|
|
|
|
|
|
|
|
QString ca = getCurrentArticle();
|
|
|
|
|
|
|
|
if ( isFramedArticle( ca ) )
|
|
|
|
{
|
2021-07-06 13:01:50 +00:00
|
|
|
//QVariant result = runJavaScriptSync( ui.definition->page(), "gdLastUrlText" );
|
|
|
|
QVariant result ;
|
2009-05-29 19:48:50 +00:00
|
|
|
|
|
|
|
if ( result.type() == QVariant::String )
|
|
|
|
{
|
|
|
|
// Looks this way
|
2021-06-29 08:59:16 +00:00
|
|
|
contexts[ dictionaryIdFromScrollTo( ca ) ] = QString::fromLatin1( url.toEncoded() );
|
2009-05-29 19:48:50 +00:00
|
|
|
|
2013-05-31 04:20:25 +00:00
|
|
|
QUrl target;
|
2009-05-29 19:48:50 +00:00
|
|
|
|
2009-05-29 20:30:45 +00:00
|
|
|
QString queryWord = result.toString();
|
|
|
|
|
|
|
|
// Empty requests are treated as no request, so we work this around by
|
|
|
|
// adding a space.
|
|
|
|
if ( queryWord.isEmpty() )
|
|
|
|
queryWord = " ";
|
|
|
|
|
2009-05-29 19:48:50 +00:00
|
|
|
target.setScheme( "gdlookup" );
|
|
|
|
target.setHost( "localhost" );
|
2009-05-29 20:30:45 +00:00
|
|
|
target.setPath( "/" + queryWord );
|
2009-05-29 19:48:50 +00:00
|
|
|
|
|
|
|
url = target;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-06 13:01:50 +00:00
|
|
|
void ArticleView::updateCurrentArticleFromCurrentFrame( QWebEnginePage * frame ,QPoint * point)
|
2009-05-29 19:48:50 +00:00
|
|
|
{
|
2021-07-06 13:01:50 +00:00
|
|
|
qDebug("updateCurrentArticleFromCurrentFrame");
|
2009-05-29 19:48:50 +00:00
|
|
|
}
|
|
|
|
|
2009-05-29 22:04:43 +00:00
|
|
|
void ArticleView::saveHistoryUserData()
|
|
|
|
{
|
2021-07-06 13:01:50 +00:00
|
|
|
ui.definition->setProperty("currentArticle", getCurrentArticle());
|
|
|
|
ui.definition->setProperty("sx", ui.definition->page()->scrollPosition().x());
|
|
|
|
ui.definition->setProperty("sy", ui.definition->page()->scrollPosition().y());
|
2009-05-29 22:04:43 +00:00
|
|
|
}
|
|
|
|
|
2009-02-08 21:54:19 +00:00
|
|
|
void ArticleView::cleanupTemp()
|
|
|
|
{
|
2017-03-13 14:38:27 +00:00
|
|
|
QSet< QString >::iterator it = desktopOpenedTempFiles.begin();
|
|
|
|
while( it != desktopOpenedTempFiles.end() )
|
2009-02-08 21:54:19 +00:00
|
|
|
{
|
2017-03-13 14:38:27 +00:00
|
|
|
if( QFile::remove( *it ) )
|
|
|
|
it = desktopOpenedTempFiles.erase( it );
|
|
|
|
else
|
|
|
|
++it;
|
2010-09-16 18:53:39 +00:00
|
|
|
}
|
2009-02-08 21:54:19 +00:00
|
|
|
}
|
|
|
|
|
2013-01-23 18:36:45 +00:00
|
|
|
bool ArticleView::handleF3( QObject * /*obj*/, QEvent * ev )
|
2009-05-12 13:25:18 +00:00
|
|
|
{
|
2014-04-23 13:46:48 +00:00
|
|
|
if ( ev->type() == QEvent::ShortcutOverride
|
|
|
|
|| ev->type() == QEvent::KeyPress )
|
|
|
|
{
|
2013-01-23 18:36:45 +00:00
|
|
|
QKeyEvent * ke = static_cast<QKeyEvent *>( ev );
|
|
|
|
if ( ke->key() == Qt::Key_F3 && isSearchOpened() ) {
|
|
|
|
if ( !ke->modifiers() )
|
|
|
|
{
|
2014-04-23 13:46:48 +00:00
|
|
|
if( ev->type() == QEvent::KeyPress )
|
|
|
|
on_searchNext_clicked();
|
|
|
|
|
2013-01-23 18:36:45 +00:00
|
|
|
ev->accept();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ke->modifiers() == Qt::ShiftModifier )
|
|
|
|
{
|
2014-04-23 13:46:48 +00:00
|
|
|
if( ev->type() == QEvent::KeyPress )
|
|
|
|
on_searchPrevious_clicked();
|
|
|
|
|
2013-01-23 18:36:45 +00:00
|
|
|
ev->accept();
|
|
|
|
return true;
|
|
|
|
}
|
2013-01-22 21:04:45 +00:00
|
|
|
}
|
2014-04-22 13:47:02 +00:00
|
|
|
if ( ke->key() == Qt::Key_F3 && ftsSearchIsOpened )
|
|
|
|
{
|
|
|
|
if ( !ke->modifiers() )
|
|
|
|
{
|
2014-04-23 13:46:48 +00:00
|
|
|
if( ev->type() == QEvent::KeyPress )
|
|
|
|
on_ftsSearchNext_clicked();
|
|
|
|
|
2014-04-22 13:47:02 +00:00
|
|
|
ev->accept();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ke->modifiers() == Qt::ShiftModifier )
|
|
|
|
{
|
2014-04-23 13:46:48 +00:00
|
|
|
if( ev->type() == QEvent::KeyPress )
|
|
|
|
on_ftsSearchPrevious_clicked();
|
|
|
|
|
2013-01-23 18:36:45 +00:00
|
|
|
ev->accept();
|
|
|
|
return true;
|
|
|
|
}
|
2013-01-22 21:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-23 18:36:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ArticleView::eventFilter( QObject * obj, QEvent * ev )
|
|
|
|
{
|
2014-02-04 13:35:42 +00:00
|
|
|
if( ev->type() == QEvent::Gesture )
|
|
|
|
{
|
|
|
|
Gestures::GestureResult result;
|
|
|
|
QPoint pt;
|
|
|
|
|
|
|
|
bool handled = Gestures::handleGestureEvent( obj, ev, result, pt );
|
|
|
|
|
|
|
|
if( handled )
|
|
|
|
{
|
|
|
|
if( result == Gestures::ZOOM_IN )
|
|
|
|
zoomIn();
|
|
|
|
else
|
|
|
|
if( result == Gestures::ZOOM_OUT )
|
|
|
|
zoomOut();
|
|
|
|
else
|
|
|
|
if( result == Gestures::SWIPE_LEFT )
|
|
|
|
back();
|
|
|
|
else
|
|
|
|
if( result == Gestures::SWIPE_RIGHT )
|
|
|
|
forward();
|
|
|
|
else
|
|
|
|
if( result == Gestures::SWIPE_UP || result == Gestures::SWIPE_DOWN )
|
|
|
|
{
|
|
|
|
int delta = result == Gestures::SWIPE_UP ? -120 : 120;
|
|
|
|
QWidget *widget = static_cast< QWidget * >( obj );
|
|
|
|
|
|
|
|
QWidget *child = widget->childAt( widget->mapFromGlobal( pt ) );
|
|
|
|
if( child )
|
|
|
|
{
|
|
|
|
QWheelEvent whev( child->mapFromGlobal( pt ), pt, delta, Qt::NoButton, Qt::NoModifier );
|
|
|
|
qApp->sendEvent( child, &whev );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QWheelEvent whev( widget->mapFromGlobal( pt ), pt, delta, Qt::NoButton, Qt::NoModifier );
|
|
|
|
qApp->sendEvent( widget, &whev );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return handled;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ev->type() == QEvent::MouseMove )
|
|
|
|
{
|
|
|
|
if( Gestures::isFewTouchPointsPresented() )
|
|
|
|
{
|
|
|
|
ev->accept();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2013-01-23 18:36:45 +00:00
|
|
|
|
|
|
|
if ( handleF3( obj, ev ) )
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-05-12 13:25:18 +00:00
|
|
|
if ( obj == ui.definition )
|
|
|
|
{
|
2011-06-07 08:26:49 +00:00
|
|
|
if ( ev->type() == QEvent::MouseButtonPress ) {
|
|
|
|
QMouseEvent * event = static_cast< QMouseEvent * >( ev );
|
|
|
|
if ( event->button() == Qt::XButton1 ) {
|
|
|
|
back();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ( event->button() == Qt::XButton2 ) {
|
|
|
|
forward();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2009-05-12 13:25:18 +00:00
|
|
|
if ( ev->type() == QEvent::KeyPress )
|
|
|
|
{
|
|
|
|
QKeyEvent * keyEvent = static_cast< QKeyEvent * >( ev );
|
|
|
|
|
2009-05-14 20:43:32 +00:00
|
|
|
if ( keyEvent->modifiers() &
|
2011-05-08 21:24:41 +00:00
|
|
|
( Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier ) )
|
2009-05-14 20:43:32 +00:00
|
|
|
return false; // A non-typing modifier is pressed
|
|
|
|
|
2009-05-12 15:54:37 +00:00
|
|
|
if ( keyEvent->key() == Qt::Key_Space ||
|
2009-05-12 17:57:53 +00:00
|
|
|
keyEvent->key() == Qt::Key_Backspace ||
|
2011-07-04 08:35:56 +00:00
|
|
|
keyEvent->key() == Qt::Key_Tab ||
|
2014-05-11 11:52:25 +00:00
|
|
|
keyEvent->key() == Qt::Key_Backtab ||
|
|
|
|
keyEvent->key() == Qt::Key_Return ||
|
|
|
|
keyEvent->key() == Qt::Key_Enter )
|
2009-05-12 15:54:37 +00:00
|
|
|
return false; // Those key have other uses than to start typing
|
|
|
|
|
2009-05-12 13:25:18 +00:00
|
|
|
QString text = keyEvent->text();
|
|
|
|
|
|
|
|
if ( text.size() )
|
|
|
|
{
|
|
|
|
emit typingEvent( text );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return QFrame::eventFilter( obj, ev );
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-09-23 18:44:38 +00:00
|
|
|
QString ArticleView::getMutedForGroup( unsigned group )
|
|
|
|
{
|
2012-09-26 13:13:47 +00:00
|
|
|
if ( dictionaryBarToggled && dictionaryBarToggled->isChecked() )
|
2009-09-23 18:44:38 +00:00
|
|
|
{
|
|
|
|
// Dictionary bar is active -- mute the muted dictionaries
|
|
|
|
Instances::Group const * groupInstance = groups.findGroup( group );
|
|
|
|
|
2012-09-26 13:13:47 +00:00
|
|
|
// Find muted dictionaries for current group
|
|
|
|
Config::Group const * grp = cfg.getGroup( group );
|
|
|
|
Config::MutedDictionaries const * mutedDictionaries;
|
|
|
|
if( group == Instances::Group::AllGroupId )
|
|
|
|
mutedDictionaries = popupView ? &cfg.popupMutedDictionaries : &cfg.mutedDictionaries;
|
|
|
|
else
|
|
|
|
mutedDictionaries = grp ? ( popupView ? &grp->popupMutedDictionaries : &grp->mutedDictionaries ) : 0;
|
|
|
|
if( !mutedDictionaries )
|
|
|
|
return QString();
|
|
|
|
|
2009-09-23 18:44:38 +00:00
|
|
|
QStringList mutedDicts;
|
|
|
|
|
|
|
|
if ( groupInstance )
|
|
|
|
{
|
|
|
|
for( unsigned x = 0; x < groupInstance->dictionaries.size(); ++x )
|
|
|
|
{
|
|
|
|
QString id = QString::fromStdString(
|
|
|
|
groupInstance->dictionaries[ x ]->getId() );
|
|
|
|
|
2010-03-30 20:15:55 +00:00
|
|
|
if ( mutedDictionaries->contains( id ) )
|
2009-09-23 18:44:38 +00:00
|
|
|
mutedDicts.append( id );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( mutedDicts.size() )
|
|
|
|
return mutedDicts.join( "," );
|
|
|
|
}
|
|
|
|
|
|
|
|
return QString();
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2021-10-03 11:28:26 +00:00
|
|
|
QStringList ArticleView::getMutedDictionaries(unsigned group)
|
|
|
|
{
|
|
|
|
if (dictionaryBarToggled && dictionaryBarToggled->isChecked())
|
|
|
|
{
|
|
|
|
// Dictionary bar is active -- mute the muted dictionaries
|
|
|
|
Instances::Group const* groupInstance = groups.findGroup(group);
|
|
|
|
|
|
|
|
// Find muted dictionaries for current group
|
|
|
|
Config::Group const* grp = cfg.getGroup(group);
|
|
|
|
Config::MutedDictionaries const* mutedDictionaries;
|
2021-10-05 01:23:30 +00:00
|
|
|
if (group == Instances::Group::AllGroupId)
|
2021-10-03 11:28:26 +00:00
|
|
|
mutedDictionaries = popupView ? &cfg.popupMutedDictionaries : &cfg.mutedDictionaries;
|
|
|
|
else
|
|
|
|
mutedDictionaries = grp ? (popupView ? &grp->popupMutedDictionaries : &grp->mutedDictionaries) : 0;
|
|
|
|
if (!mutedDictionaries)
|
|
|
|
return QStringList();
|
|
|
|
|
|
|
|
QStringList mutedDicts;
|
|
|
|
|
|
|
|
if (groupInstance)
|
|
|
|
{
|
|
|
|
for (unsigned x = 0; x < groupInstance->dictionaries.size(); ++x)
|
|
|
|
{
|
|
|
|
QString id = QString::fromStdString(
|
|
|
|
groupInstance->dictionaries[x]->getId());
|
|
|
|
|
|
|
|
if (mutedDictionaries->contains(id))
|
|
|
|
mutedDicts.append(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mutedDicts;
|
|
|
|
}
|
|
|
|
|
|
|
|
return QStringList();
|
|
|
|
}
|
|
|
|
|
2021-07-06 13:01:50 +00:00
|
|
|
void ArticleView::linkHovered ( const QString & link )
|
2011-07-02 13:04:49 +00:00
|
|
|
{
|
|
|
|
QString msg;
|
2013-05-31 04:20:25 +00:00
|
|
|
QUrl url(link);
|
2011-07-02 13:04:49 +00:00
|
|
|
|
|
|
|
if ( url.scheme() == "bres" )
|
|
|
|
{
|
|
|
|
msg = tr( "Resource" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if ( url.scheme() == "gdau" || Dictionary::WebMultimediaDownload::isAudioUrl( url ) )
|
|
|
|
{
|
|
|
|
msg = tr( "Audio" );
|
|
|
|
}
|
|
|
|
else
|
2013-04-24 16:01:44 +00:00
|
|
|
if ( url.scheme() == "gdtts" )
|
|
|
|
{
|
|
|
|
msg = tr( "TTS Voice" );
|
|
|
|
}
|
|
|
|
else
|
2012-12-07 11:59:29 +00:00
|
|
|
if ( url.scheme() == "gdpicture" )
|
|
|
|
{
|
|
|
|
msg = tr( "Picture" );
|
|
|
|
}
|
|
|
|
else
|
2013-06-22 16:36:25 +00:00
|
|
|
if ( url.scheme() == "gdvideo" )
|
|
|
|
{
|
|
|
|
if ( url.path().isEmpty() )
|
|
|
|
{
|
|
|
|
msg = tr( "Video" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QString path = url.path();
|
|
|
|
if ( path.startsWith( '/' ) )
|
|
|
|
{
|
|
|
|
path = path.mid( 1 );
|
|
|
|
}
|
|
|
|
msg = tr( "Video: %1" ).arg( path );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2011-07-02 13:04:49 +00:00
|
|
|
if (url.scheme() == "gdlookup" || url.scheme().compare( "bword" ) == 0)
|
|
|
|
{
|
|
|
|
QString def = url.path();
|
|
|
|
if (def.startsWith("/"))
|
|
|
|
{
|
|
|
|
def = def.mid( 1 );
|
|
|
|
}
|
2012-11-26 13:13:56 +00:00
|
|
|
|
2021-11-27 07:17:33 +00:00
|
|
|
if( Utils::Url::hasQueryItem( url, "dict" ) )
|
2012-11-26 13:13:56 +00:00
|
|
|
{
|
|
|
|
// Link to other dictionary
|
2021-11-27 07:17:33 +00:00
|
|
|
QString dictName( Utils::Url::queryItemValue( url, "dict" ) );
|
2012-11-26 13:13:56 +00:00
|
|
|
if( !dictName.isEmpty() )
|
|
|
|
msg = tr( "Definition from dictionary \"%1\": %2" ).arg( dictName ).arg( def );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( msg.isEmpty() )
|
2018-02-21 10:48:11 +00:00
|
|
|
{
|
|
|
|
if( def.isEmpty() && url.hasFragment() )
|
|
|
|
msg = '#' + url.fragment(); // this must be a citation, footnote or backlink
|
|
|
|
else
|
|
|
|
msg = tr( "Definition: %1").arg( def );
|
|
|
|
}
|
2011-07-02 13:04:49 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
msg = link;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit statusBarMessage( msg );
|
|
|
|
}
|
|
|
|
|
2011-07-03 12:27:08 +00:00
|
|
|
void ArticleView::attachToJavaScript()
|
|
|
|
{
|
2021-08-14 07:25:10 +00:00
|
|
|
QWebEngineScript script;
|
|
|
|
script.setInjectionPoint(QWebEngineScript::DocumentReady);
|
|
|
|
script.setRunsOnSubFrames(false);
|
|
|
|
script.setWorldId(QWebEngineScript::MainWorld);
|
|
|
|
script.setSourceCode(QString("articleview"));
|
2021-07-06 13:01:50 +00:00
|
|
|
|
2021-08-14 07:25:10 +00:00
|
|
|
ui.definition->page()->scripts().insert(script);
|
2021-07-06 13:01:50 +00:00
|
|
|
|
2011-07-03 12:27:08 +00:00
|
|
|
}
|
2011-07-02 13:04:49 +00:00
|
|
|
|
2009-05-29 19:48:50 +00:00
|
|
|
void ArticleView::linkClicked( QUrl const & url_ )
|
2009-02-08 15:49:17 +00:00
|
|
|
{
|
2017-03-14 14:47:38 +00:00
|
|
|
Qt::KeyboardModifiers kmod = QApplication::keyboardModifiers();
|
|
|
|
|
|
|
|
// Lock jump on links while Alt key is pressed
|
|
|
|
if( kmod & Qt::AltModifier )
|
|
|
|
return;
|
|
|
|
|
2009-05-29 19:48:50 +00:00
|
|
|
updateCurrentArticleFromCurrentFrame();
|
|
|
|
|
2013-05-31 04:20:25 +00:00
|
|
|
QUrl url( url_ );
|
2009-05-29 19:48:50 +00:00
|
|
|
Contexts contexts;
|
|
|
|
|
|
|
|
tryMangleWebsiteClickedUrl( url, contexts );
|
|
|
|
|
2011-07-06 06:53:42 +00:00
|
|
|
if ( !popupView &&
|
|
|
|
( ui.definition->isMidButtonPressed() ||
|
|
|
|
( kmod & ( Qt::ControlModifier | Qt::ShiftModifier ) ) ) )
|
2009-10-22 11:38:11 +00:00
|
|
|
{
|
2011-07-06 06:53:42 +00:00
|
|
|
// Mid button or Control/Shift is currently pressed - open the link in new tab
|
2009-10-22 11:38:11 +00:00
|
|
|
emit openLinkInNewTab( url, ui.definition->url(), getCurrentArticle(), contexts );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
openLink( url, ui.definition->url(), getCurrentArticle(), contexts );
|
2009-02-08 15:49:17 +00:00
|
|
|
}
|
|
|
|
|
2009-05-11 19:14:28 +00:00
|
|
|
void ArticleView::openLink( QUrl const & url, QUrl const & ref,
|
2009-05-29 19:48:50 +00:00
|
|
|
QString const & scrollTo,
|
2015-10-28 19:56:58 +00:00
|
|
|
Contexts const & contexts_ )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2011-07-02 13:04:49 +00:00
|
|
|
qDebug() << "clicked" << url;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2015-10-28 19:56:58 +00:00
|
|
|
Contexts contexts( contexts_ );
|
|
|
|
|
2012-12-07 11:59:29 +00:00
|
|
|
if( url.scheme().compare( "gdpicture" ) == 0 )
|
|
|
|
ui.definition->load( url );
|
|
|
|
else
|
2010-10-07 05:33:41 +00:00
|
|
|
if ( url.scheme().compare( "bword" ) == 0 )
|
2009-08-31 14:27:19 +00:00
|
|
|
{
|
2021-11-27 07:17:33 +00:00
|
|
|
if( Utils::Url::hasQueryItem( ref, "dictionaries" ) )
|
2014-04-21 16:20:24 +00:00
|
|
|
{
|
2021-11-27 07:17:33 +00:00
|
|
|
QStringList dictsList = Utils::Url::queryItemValue( ref, "dictionaries" )
|
2014-04-23 14:19:46 +00:00
|
|
|
.split( ",", QString::SkipEmptyParts );
|
2014-04-22 13:47:02 +00:00
|
|
|
|
2018-04-10 14:49:52 +00:00
|
|
|
showDefinition( url.path(), dictsList, QRegExp(), getGroup( ref ), false );
|
2014-04-21 16:20:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
showDefinition( url.path(),
|
|
|
|
getGroup( ref ), scrollTo, contexts );
|
2009-08-31 14:27:19 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
else
|
|
|
|
if ( url.scheme() == "gdlookup" ) // Plain html links inherit gdlookup scheme
|
2009-03-26 19:00:08 +00:00
|
|
|
{
|
|
|
|
if ( url.hasFragment() )
|
|
|
|
{
|
2021-07-06 13:01:50 +00:00
|
|
|
ui.definition->page()->runJavaScript(
|
2009-03-26 19:00:08 +00:00
|
|
|
QString( "window.location = \"%1\"" ).arg( QString::fromUtf8( url.toEncoded() ) ) );
|
|
|
|
}
|
|
|
|
else
|
2012-11-12 13:52:54 +00:00
|
|
|
{
|
2021-11-27 07:17:33 +00:00
|
|
|
if( Utils::Url::hasQueryItem( ref, "dictionaries" ) )
|
2014-04-21 16:20:24 +00:00
|
|
|
{
|
|
|
|
// Specific dictionary group from full-text search
|
2021-11-27 07:17:33 +00:00
|
|
|
QStringList dictsList = Utils::Url::queryItemValue( ref, "dictionaries" )
|
2014-04-23 14:19:46 +00:00
|
|
|
.split( ",", QString::SkipEmptyParts );
|
2014-04-22 13:47:02 +00:00
|
|
|
|
2018-04-10 14:49:52 +00:00
|
|
|
showDefinition( url.path().mid( 1 ), dictsList, QRegExp(), getGroup( ref ), false );
|
2014-04-21 16:20:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-24 20:30:32 +00:00
|
|
|
QString newScrollTo( scrollTo );
|
2021-11-27 07:17:33 +00:00
|
|
|
if( Utils::Url::hasQueryItem( url, "dict" ) )
|
2012-11-24 20:30:32 +00:00
|
|
|
{
|
|
|
|
// Link to other dictionary
|
2021-11-27 07:17:33 +00:00
|
|
|
QString dictName( Utils::Url::queryItemValue( url, "dict" ) );
|
2012-11-24 20:30:32 +00:00
|
|
|
for( unsigned i = 0; i < allDictionaries.size(); i++ )
|
|
|
|
{
|
|
|
|
if( dictName.compare( QString::fromUtf8( allDictionaries[ i ]->getName().c_str() ) ) == 0 )
|
|
|
|
{
|
2021-06-29 08:59:16 +00:00
|
|
|
newScrollTo = scrollToFromDictionaryId( QString::fromUtf8( allDictionaries[ i ]->getId().c_str() ) );
|
2012-11-24 20:30:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-28 19:56:58 +00:00
|
|
|
|
2021-11-27 07:17:33 +00:00
|
|
|
if( Utils::Url::hasQueryItem( url, "gdanchor" ) )
|
|
|
|
contexts[ "gdanchor" ] = Utils::Url::queryItemValue( url, "gdanchor" );
|
2015-10-28 19:56:58 +00:00
|
|
|
|
2012-11-12 13:52:54 +00:00
|
|
|
showDefinition( url.path().mid( 1 ),
|
2012-11-24 20:30:32 +00:00
|
|
|
getGroup( ref ), newScrollTo, contexts );
|
2012-11-12 13:52:54 +00:00
|
|
|
}
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
else
|
2013-06-22 16:36:25 +00:00
|
|
|
if ( url.scheme() == "bres" || url.scheme() == "gdau" || url.scheme() == "gdvideo" ||
|
2010-05-29 11:33:04 +00:00
|
|
|
Dictionary::WebMultimediaDownload::isAudioUrl( url ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-02-06 16:19:05 +00:00
|
|
|
// Download it
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
// Clear any pending ones
|
|
|
|
|
|
|
|
resourceDownloadRequests.clear();
|
2009-02-06 16:19:05 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
resourceDownloadUrl = url;
|
2009-02-06 16:19:05 +00:00
|
|
|
|
2010-05-29 11:33:04 +00:00
|
|
|
if ( Dictionary::WebMultimediaDownload::isAudioUrl( url ) )
|
|
|
|
{
|
|
|
|
sptr< Dictionary::DataRequest > req =
|
|
|
|
new Dictionary::WebMultimediaDownload( url, articleNetMgr );
|
|
|
|
|
|
|
|
resourceDownloadRequests.push_back( req );
|
|
|
|
|
|
|
|
connect( req.get(), SIGNAL( finished() ),
|
|
|
|
this, SLOT( resourceDownloadFinished() ) );
|
|
|
|
}
|
|
|
|
else
|
2009-04-25 21:04:49 +00:00
|
|
|
if ( url.scheme() == "gdau" && url.host() == "search" )
|
2009-02-06 16:19:05 +00:00
|
|
|
{
|
|
|
|
// Since searches should be limited to current group, we just do them
|
|
|
|
// here ourselves since otherwise we'd need to pass group id to netmgr
|
|
|
|
// and it should've been having knowledge of the current groups, too.
|
|
|
|
|
2009-04-10 12:48:40 +00:00
|
|
|
unsigned currentGroup = getGroup( ref );
|
2009-02-06 16:19:05 +00:00
|
|
|
|
2009-04-25 21:04:49 +00:00
|
|
|
std::vector< sptr< Dictionary::Class > > const * activeDicts = 0;
|
|
|
|
|
|
|
|
if ( groups.size() )
|
|
|
|
{
|
|
|
|
for( unsigned x = 0; x < groups.size(); ++x )
|
|
|
|
if ( groups[ x ].id == currentGroup )
|
|
|
|
{
|
|
|
|
activeDicts = &( groups[ x ].dictionaries );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
activeDicts = &allDictionaries;
|
|
|
|
|
|
|
|
if ( activeDicts )
|
2017-04-27 20:55:53 +00:00
|
|
|
{
|
2017-07-09 17:15:35 +00:00
|
|
|
unsigned preferred = UINT_MAX;
|
|
|
|
if( url.hasFragment() )
|
|
|
|
{
|
|
|
|
// Find sound in the preferred dictionary
|
2021-11-27 07:17:33 +00:00
|
|
|
QString preferredName = Utils::Url::fragment( url );
|
2017-07-09 17:15:35 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
for( unsigned x = 0; x < activeDicts->size(); ++x )
|
|
|
|
{
|
|
|
|
if( preferredName.compare( QString::fromUtf8( (*activeDicts)[ x ]->getName().c_str() ) ) == 0 )
|
|
|
|
{
|
|
|
|
preferred = x;
|
|
|
|
sptr< Dictionary::DataRequest > req =
|
|
|
|
(*activeDicts)[ x ]->getResource(
|
|
|
|
url.path().mid( 1 ).toUtf8().data() );
|
|
|
|
|
|
|
|
resourceDownloadRequests.push_back( req );
|
|
|
|
|
|
|
|
if ( !req->isFinished() )
|
|
|
|
{
|
|
|
|
// Queued loading
|
|
|
|
connect( req.get(), SIGNAL( finished() ),
|
|
|
|
this, SLOT( resourceDownloadFinished() ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Immediate loading
|
|
|
|
if( req->dataSize() > 0 )
|
|
|
|
{
|
|
|
|
// Resource already found, stop next search
|
|
|
|
resourceDownloadFinished();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch( std::exception & e )
|
|
|
|
{
|
|
|
|
emit statusBarMessage(
|
|
|
|
tr( "ERROR: %1" ).arg( e.what() ),
|
|
|
|
10000, QPixmap( ":/icons/error.png" ) );
|
|
|
|
}
|
|
|
|
}
|
2009-04-25 21:04:49 +00:00
|
|
|
for( unsigned x = 0; x < activeDicts->size(); ++x )
|
2009-02-06 16:19:05 +00:00
|
|
|
{
|
2013-10-18 12:50:29 +00:00
|
|
|
try
|
2009-02-06 16:19:05 +00:00
|
|
|
{
|
2017-07-09 17:15:35 +00:00
|
|
|
if( x == preferred )
|
|
|
|
continue;
|
|
|
|
|
2013-10-18 12:50:29 +00:00
|
|
|
sptr< Dictionary::DataRequest > req =
|
|
|
|
(*activeDicts)[ x ]->getResource(
|
|
|
|
url.path().mid( 1 ).toUtf8().data() );
|
|
|
|
|
2017-04-27 20:55:53 +00:00
|
|
|
resourceDownloadRequests.push_back( req );
|
2013-10-18 12:50:29 +00:00
|
|
|
|
|
|
|
if ( !req->isFinished() )
|
|
|
|
{
|
2017-04-28 12:25:45 +00:00
|
|
|
// Queued loading
|
2013-10-18 12:50:29 +00:00
|
|
|
connect( req.get(), SIGNAL( finished() ),
|
|
|
|
this, SLOT( resourceDownloadFinished() ) );
|
|
|
|
}
|
2017-04-28 12:25:45 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Immediate loading
|
|
|
|
if( req->dataSize() > 0 )
|
|
|
|
{
|
|
|
|
// Resource already found, stop next search
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-04-25 21:04:49 +00:00
|
|
|
}
|
2013-10-18 12:50:29 +00:00
|
|
|
catch( std::exception & e )
|
2009-04-25 21:04:49 +00:00
|
|
|
{
|
2013-10-18 12:50:29 +00:00
|
|
|
emit statusBarMessage(
|
|
|
|
tr( "ERROR: %1" ).arg( e.what() ),
|
|
|
|
10000, QPixmap( ":/icons/error.png" ) );
|
2009-02-06 16:19:05 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-27 20:55:53 +00:00
|
|
|
}
|
2009-02-06 16:19:05 +00:00
|
|
|
}
|
2009-03-26 19:00:08 +00:00
|
|
|
else
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-03-26 19:00:08 +00:00
|
|
|
// Normal resource download
|
|
|
|
QString contentType;
|
2009-02-08 14:40:26 +00:00
|
|
|
|
2010-09-16 18:53:39 +00:00
|
|
|
sptr< Dictionary::DataRequest > req =
|
2009-03-26 19:00:08 +00:00
|
|
|
articleNetMgr.getResource( url, contentType );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-25 21:04:49 +00:00
|
|
|
if ( !req.get() )
|
|
|
|
{
|
|
|
|
// Request failed, fail
|
|
|
|
}
|
|
|
|
else
|
2009-03-26 19:00:08 +00:00
|
|
|
if ( req->isFinished() && req->dataSize() >= 0 )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-03-26 19:00:08 +00:00
|
|
|
// Have data ready, handle it
|
|
|
|
resourceDownloadRequests.push_back( req );
|
|
|
|
resourceDownloadFinished();
|
2009-02-08 14:40:26 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
return;
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
2009-03-26 19:00:08 +00:00
|
|
|
else
|
|
|
|
if ( !req->isFinished() )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-03-26 19:00:08 +00:00
|
|
|
// Queue to be handled when done
|
2009-02-08 14:40:26 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
resourceDownloadRequests.push_back( req );
|
|
|
|
|
|
|
|
connect( req.get(), SIGNAL( finished() ),
|
|
|
|
this, SLOT( resourceDownloadFinished() ) );
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
2009-02-08 14:40:26 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
if ( resourceDownloadRequests.empty() ) // No requests were queued
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2013-02-01 12:36:01 +00:00
|
|
|
QMessageBox::critical( this, "GoldenDict", tr( "The referenced resource doesn't exist." ) );
|
2009-03-26 19:00:08 +00:00
|
|
|
return;
|
2009-02-08 21:54:19 +00:00
|
|
|
}
|
2009-04-25 21:04:49 +00:00
|
|
|
else
|
|
|
|
resourceDownloadFinished(); // Check any requests finished already
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
2009-02-01 00:08:08 +00:00
|
|
|
else
|
2011-05-29 05:08:37 +00:00
|
|
|
if ( url.scheme() == "gdprg" )
|
|
|
|
{
|
|
|
|
// Program. Run it.
|
|
|
|
QString id( url.host() );
|
|
|
|
|
|
|
|
for( Config::Programs::const_iterator i = cfg.programs.begin();
|
|
|
|
i != cfg.programs.end(); ++i )
|
|
|
|
{
|
|
|
|
if ( i->id == id )
|
|
|
|
{
|
|
|
|
// Found the corresponding program.
|
2011-06-04 21:35:09 +00:00
|
|
|
Programs::RunInstance * req = new Programs::RunInstance;
|
2011-05-29 05:08:37 +00:00
|
|
|
|
2011-06-04 21:35:09 +00:00
|
|
|
connect( req, SIGNAL(finished(QByteArray,QString)),
|
|
|
|
req, SLOT( deleteLater() ) );
|
2011-05-29 05:08:37 +00:00
|
|
|
|
2011-06-04 21:35:09 +00:00
|
|
|
QString error;
|
|
|
|
|
|
|
|
// Delete the request if it fails to start
|
|
|
|
if ( !req->start( *i, url.path().mid( 1 ), error ) )
|
|
|
|
{
|
2011-05-29 05:08:37 +00:00
|
|
|
delete req;
|
|
|
|
|
2013-02-01 12:36:01 +00:00
|
|
|
QMessageBox::critical( this, "GoldenDict",
|
2011-06-04 21:35:09 +00:00
|
|
|
error );
|
|
|
|
}
|
|
|
|
|
2011-05-29 05:08:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Still here? No such program exists.
|
2013-02-01 12:36:01 +00:00
|
|
|
QMessageBox::critical( this, "GoldenDict",
|
2011-05-29 05:08:37 +00:00
|
|
|
tr( "The referenced audio program doesn't exist." ) );
|
|
|
|
}
|
|
|
|
else
|
2013-04-24 14:52:04 +00:00
|
|
|
if ( url.scheme() == "gdtts" )
|
|
|
|
{
|
|
|
|
// TODO: Port TTS
|
2014-05-12 13:53:13 +00:00
|
|
|
#if defined( Q_OS_WIN32 ) || defined( Q_OS_MAC )
|
2013-04-24 14:52:04 +00:00
|
|
|
// Text to speech
|
2021-11-27 07:17:33 +00:00
|
|
|
QString md5Id = Utils::Url::queryItemValue( url, "engine" );
|
2013-04-24 14:52:04 +00:00
|
|
|
QString text( url.path().mid( 1 ) );
|
|
|
|
|
|
|
|
for ( Config::VoiceEngines::const_iterator i = cfg.voiceEngines.begin();
|
|
|
|
i != cfg.voiceEngines.end(); ++i )
|
|
|
|
{
|
|
|
|
QString itemMd5Id = QString( QCryptographicHash::hash(
|
|
|
|
i->id.toUtf8(),
|
2013-04-24 16:01:44 +00:00
|
|
|
QCryptographicHash::Md5 ).toHex() );
|
2013-04-24 14:52:04 +00:00
|
|
|
|
2013-04-24 16:01:44 +00:00
|
|
|
if ( itemMd5Id == md5Id )
|
|
|
|
{
|
2013-04-26 13:41:39 +00:00
|
|
|
SpeechClient * speechClient = new SpeechClient( *i, this );
|
2013-04-24 14:52:04 +00:00
|
|
|
connect( speechClient, SIGNAL( finished() ), speechClient, SLOT( deleteLater() ) );
|
2013-04-24 16:01:44 +00:00
|
|
|
speechClient->tell( text );
|
2013-04-24 14:52:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
2010-06-30 16:43:08 +00:00
|
|
|
if ( isExternalLink( url ) )
|
2009-02-01 00:08:08 +00:00
|
|
|
{
|
2010-06-30 16:43:08 +00:00
|
|
|
// Use the system handler for the conventional external links
|
2009-02-01 00:08:08 +00:00
|
|
|
QDesktopServices::openUrl( url );
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-27 20:55:53 +00:00
|
|
|
ResourceToSaveHandler * ArticleView::saveResource( const QUrl & url, const QString & fileName )
|
2013-02-22 12:44:23 +00:00
|
|
|
{
|
2013-05-27 11:18:13 +00:00
|
|
|
return saveResource( url, ui.definition->url(), fileName );
|
|
|
|
}
|
|
|
|
|
2017-04-27 20:55:53 +00:00
|
|
|
ResourceToSaveHandler * ArticleView::saveResource( const QUrl & url, const QUrl & ref, const QString & fileName )
|
2013-05-27 11:18:13 +00:00
|
|
|
{
|
2017-04-27 20:55:53 +00:00
|
|
|
ResourceToSaveHandler * handler = new ResourceToSaveHandler( this, fileName );
|
2013-02-22 12:44:23 +00:00
|
|
|
sptr< Dictionary::DataRequest > req;
|
|
|
|
|
2017-04-27 15:05:28 +00:00
|
|
|
if( url.scheme() == "bres" || url.scheme() == "gico" || url.scheme() == "gdau" || url.scheme() == "gdvideo" )
|
2013-02-22 12:44:23 +00:00
|
|
|
{
|
|
|
|
if ( url.host() == "search" )
|
|
|
|
{
|
|
|
|
// Since searches should be limited to current group, we just do them
|
|
|
|
// here ourselves since otherwise we'd need to pass group id to netmgr
|
|
|
|
// and it should've been having knowledge of the current groups, too.
|
|
|
|
|
|
|
|
unsigned currentGroup = getGroup( ref );
|
|
|
|
|
|
|
|
std::vector< sptr< Dictionary::Class > > const * activeDicts = 0;
|
|
|
|
|
|
|
|
if ( groups.size() )
|
|
|
|
{
|
|
|
|
for( unsigned x = 0; x < groups.size(); ++x )
|
|
|
|
if ( groups[ x ].id == currentGroup )
|
|
|
|
{
|
|
|
|
activeDicts = &( groups[ x ].dictionaries );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
activeDicts = &allDictionaries;
|
|
|
|
|
|
|
|
if ( activeDicts )
|
|
|
|
{
|
2019-06-27 15:09:00 +00:00
|
|
|
unsigned preferred = UINT_MAX;
|
|
|
|
if( url.hasFragment() && url.scheme() == "gdau" )
|
|
|
|
{
|
|
|
|
// Find sound in the preferred dictionary
|
2021-11-27 07:17:33 +00:00
|
|
|
QString preferredName = Utils::Url::fragment( url );
|
2019-06-27 15:09:00 +00:00
|
|
|
for( unsigned x = 0; x < activeDicts->size(); ++x )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if( preferredName.compare( QString::fromUtf8( (*activeDicts)[ x ]->getName().c_str() ) ) == 0 )
|
|
|
|
{
|
|
|
|
preferred = x;
|
|
|
|
sptr< Dictionary::DataRequest > req =
|
|
|
|
(*activeDicts)[ x ]->getResource(
|
|
|
|
url.path().mid( 1 ).toUtf8().data() );
|
|
|
|
|
|
|
|
handler->addRequest( req );
|
|
|
|
|
|
|
|
if( req->isFinished() && req->dataSize() > 0 )
|
|
|
|
{
|
|
|
|
handler->downloadFinished();
|
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch( std::exception & e )
|
|
|
|
{
|
|
|
|
gdWarning( "getResource request error (%s) in \"%s\"\n", e.what(),
|
|
|
|
(*activeDicts)[ x ]->getName().c_str() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-02-22 12:44:23 +00:00
|
|
|
for( unsigned x = 0; x < activeDicts->size(); ++x )
|
|
|
|
{
|
2013-10-18 12:50:29 +00:00
|
|
|
try
|
|
|
|
{
|
2019-06-27 15:09:00 +00:00
|
|
|
if( x == preferred )
|
|
|
|
continue;
|
|
|
|
|
2013-10-18 12:50:29 +00:00
|
|
|
req = (*activeDicts)[ x ]->getResource(
|
2021-11-27 07:17:33 +00:00
|
|
|
Utils::Url::path( url ).mid( 1 ).toUtf8().data() );
|
2013-02-22 12:44:23 +00:00
|
|
|
|
2017-04-27 20:55:53 +00:00
|
|
|
handler->addRequest( req );
|
2017-04-28 12:25:45 +00:00
|
|
|
|
|
|
|
if( req->isFinished() && req->dataSize() > 0 )
|
|
|
|
{
|
|
|
|
// Resource already found, stop next search
|
|
|
|
break;
|
|
|
|
}
|
2013-10-18 12:50:29 +00:00
|
|
|
}
|
|
|
|
catch( std::exception & e )
|
|
|
|
{
|
2013-11-16 18:34:09 +00:00
|
|
|
gdWarning( "getResource request error (%s) in \"%s\"\n", e.what(),
|
2013-10-18 12:50:29 +00:00
|
|
|
(*activeDicts)[ x ]->getName().c_str() );
|
|
|
|
}
|
2013-02-22 12:44:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Normal resource download
|
|
|
|
QString contentType;
|
|
|
|
req = articleNetMgr.getResource( url, contentType );
|
2013-05-27 11:18:13 +00:00
|
|
|
|
2013-10-18 14:32:58 +00:00
|
|
|
if( req.get() )
|
|
|
|
{
|
2017-04-27 20:55:53 +00:00
|
|
|
handler->addRequest( req );
|
2013-10-18 14:32:58 +00:00
|
|
|
}
|
2013-02-22 12:44:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-05-27 11:18:13 +00:00
|
|
|
req = new Dictionary::WebMultimediaDownload( url, articleNetMgr );
|
2013-02-22 12:44:23 +00:00
|
|
|
|
2017-04-27 20:55:53 +00:00
|
|
|
handler->addRequest( req );
|
2013-02-22 12:44:23 +00:00
|
|
|
}
|
|
|
|
|
2017-04-27 20:55:53 +00:00
|
|
|
if ( handler->isEmpty() ) // No requests were queued
|
2013-02-22 12:44:23 +00:00
|
|
|
{
|
|
|
|
emit statusBarMessage(
|
|
|
|
tr( "ERROR: %1" ).arg( tr( "The referenced resource doesn't exist." ) ),
|
|
|
|
10000, QPixmap( ":/icons/error.png" ) );
|
|
|
|
}
|
|
|
|
|
2017-04-27 20:55:53 +00:00
|
|
|
// Check already finished downloads
|
|
|
|
handler->downloadFinished();
|
|
|
|
|
|
|
|
return handler;
|
2013-02-22 12:44:23 +00:00
|
|
|
}
|
|
|
|
|
2009-09-23 18:44:38 +00:00
|
|
|
void ArticleView::updateMutedContents()
|
|
|
|
{
|
2013-05-31 04:20:25 +00:00
|
|
|
QUrl currentUrl = ui.definition->url();
|
2009-09-23 18:44:38 +00:00
|
|
|
|
|
|
|
if ( currentUrl.scheme() != "gdlookup" )
|
|
|
|
return; // Weird url -- do nothing
|
|
|
|
|
|
|
|
unsigned group = getGroup( currentUrl );
|
|
|
|
|
|
|
|
if ( !group )
|
|
|
|
return; // No group in url -- do nothing
|
|
|
|
|
|
|
|
QString mutedDicts = getMutedForGroup( group );
|
|
|
|
|
2021-11-27 07:17:33 +00:00
|
|
|
if ( Utils::Url::queryItemValue( currentUrl, "muted" ) != mutedDicts )
|
2009-09-23 18:44:38 +00:00
|
|
|
{
|
|
|
|
// The list has changed -- update the url
|
|
|
|
|
2021-11-27 07:17:33 +00:00
|
|
|
Utils::Url::removeQueryItem( currentUrl, "muted" );
|
2009-09-23 18:44:38 +00:00
|
|
|
|
|
|
|
if ( mutedDicts.size() )
|
2021-11-27 07:17:33 +00:00
|
|
|
Utils::Url::addQueryItem( currentUrl, "muted", mutedDicts );
|
2009-09-23 18:44:38 +00:00
|
|
|
|
|
|
|
saveHistoryUserData();
|
|
|
|
|
|
|
|
ui.definition->load( currentUrl );
|
|
|
|
|
|
|
|
//QApplication::setOverrideCursor( Qt::WaitCursor );
|
|
|
|
ui.definition->setCursor( Qt::WaitCursor );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-07 11:27:19 +00:00
|
|
|
bool ArticleView::canGoBack()
|
|
|
|
{
|
|
|
|
// First entry in a history is always an empty page,
|
|
|
|
// so we skip it.
|
|
|
|
return ui.definition->history()->currentItemIndex() > 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ArticleView::canGoForward()
|
|
|
|
{
|
|
|
|
return ui.definition->history()->canGoForward();
|
|
|
|
}
|
|
|
|
|
2012-09-26 13:59:48 +00:00
|
|
|
void ArticleView::setSelectionBySingleClick( bool set )
|
|
|
|
{
|
|
|
|
ui.definition->setSelectionBySingleClick( set );
|
|
|
|
}
|
|
|
|
|
2011-06-07 07:57:25 +00:00
|
|
|
void ArticleView::back()
|
|
|
|
{
|
|
|
|
// Don't allow navigating back to page 0, which is usually the initial
|
|
|
|
// empty page
|
2011-06-07 11:27:19 +00:00
|
|
|
if ( canGoBack() )
|
2011-06-07 07:57:25 +00:00
|
|
|
{
|
|
|
|
saveHistoryUserData();
|
|
|
|
ui.definition->back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleView::forward()
|
|
|
|
{
|
|
|
|
saveHistoryUserData();
|
|
|
|
ui.definition->forward();
|
|
|
|
}
|
|
|
|
|
2009-04-10 21:07:03 +00:00
|
|
|
bool ArticleView::hasSound()
|
|
|
|
{
|
2021-12-13 14:45:16 +00:00
|
|
|
QVariant v = runJavaScriptSync( ui.definition->page(),"gdAudioLinks.first" );
|
|
|
|
if ( v.type() == QVariant::String )
|
|
|
|
return !v.toString().isEmpty();
|
|
|
|
return false;
|
2009-04-10 21:07:03 +00:00
|
|
|
}
|
|
|
|
|
2021-12-13 14:45:16 +00:00
|
|
|
//use webengine javascript to playsound
|
2009-04-10 21:07:03 +00:00
|
|
|
void ArticleView::playSound()
|
|
|
|
{
|
2021-11-24 14:38:37 +00:00
|
|
|
QString variable = " var link=gdAudioLinks[gdAudioLinks.current]; "
|
|
|
|
" if(link==undefined){ "
|
|
|
|
" link=gdAudioLinks.first; "
|
|
|
|
" } "
|
|
|
|
" "
|
|
|
|
" var music = new Audio(link); "
|
|
|
|
" music.play(); ";
|
|
|
|
ui.definition->page()->runJavaScript(variable);
|
2009-04-10 21:07:03 +00:00
|
|
|
}
|
|
|
|
|
2021-12-12 05:35:32 +00:00
|
|
|
// use eventloop to turn the async callback to sync execution.
|
2009-05-01 11:17:29 +00:00
|
|
|
QString ArticleView::toHtml()
|
|
|
|
{
|
2021-12-12 05:35:32 +00:00
|
|
|
QString result;
|
|
|
|
QSharedPointer<QEventLoop> loop = QSharedPointer<QEventLoop>(new QEventLoop());
|
|
|
|
QTimer::singleShot(1000, loop.data(), &QEventLoop::quit);
|
2021-08-14 07:25:10 +00:00
|
|
|
|
2021-12-12 05:35:32 +00:00
|
|
|
ui.definition->page()->toHtml([loop, &result](const QString &content) {
|
|
|
|
if (loop->isRunning()) {
|
|
|
|
result = content;
|
|
|
|
loop->quit();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
loop->exec();
|
|
|
|
return result;
|
2009-05-01 11:17:29 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 03:05:38 +00:00
|
|
|
void ArticleView::setHtml(const QString& content,const QUrl& baseUrl){
|
2021-08-14 07:25:10 +00:00
|
|
|
ui.definition->page()->setHtml(content,baseUrl);
|
|
|
|
}
|
|
|
|
|
2021-08-21 01:41:40 +00:00
|
|
|
void ArticleView::setContent(const QByteArray &data, const QString &mimeType, const QUrl &baseUrl ){
|
|
|
|
ui.definition->page()->setContent(data,mimeType,baseUrl);
|
|
|
|
}
|
|
|
|
|
2009-05-01 11:17:29 +00:00
|
|
|
QString ArticleView::getTitle()
|
|
|
|
{
|
2021-07-06 13:01:50 +00:00
|
|
|
return ui.definition->page()->title();
|
2009-05-01 11:17:29 +00:00
|
|
|
}
|
|
|
|
|
2021-06-10 16:13:11 +00:00
|
|
|
Config::InputPhrase ArticleView::getPhrase() const
|
|
|
|
{
|
|
|
|
const QUrl url = ui.definition->url();
|
2021-11-27 07:17:33 +00:00
|
|
|
return Config::InputPhrase( Utils::Url::queryItemValue( url, "word" ),
|
|
|
|
Utils::Url::queryItemValue( url, "punctuation_suffix" ) );
|
2021-06-10 16:13:11 +00:00
|
|
|
}
|
|
|
|
|
2009-05-01 12:20:33 +00:00
|
|
|
void ArticleView::print( QPrinter * printer ) const
|
|
|
|
{
|
2021-07-06 13:01:50 +00:00
|
|
|
ui.definition->page()->print(printer, [](bool result) {});
|
2009-05-01 12:20:33 +00:00
|
|
|
}
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
void ArticleView::contextMenuRequested( QPoint const & pos )
|
|
|
|
{
|
|
|
|
// Is that a link? Is there a selection?
|
|
|
|
|
2021-09-22 04:01:17 +00:00
|
|
|
QWebEnginePage* r=ui.definition->page();
|
2021-07-06 13:01:50 +00:00
|
|
|
updateCurrentArticleFromCurrentFrame(ui.definition->page(), const_cast<QPoint *>(& pos));
|
2009-05-29 19:48:50 +00:00
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
QMenu menu( this );
|
|
|
|
|
|
|
|
QAction * followLink = 0;
|
2009-05-29 19:48:50 +00:00
|
|
|
QAction * followLinkExternal = 0;
|
2009-02-08 15:49:17 +00:00
|
|
|
QAction * followLinkNewTab = 0;
|
2009-01-28 20:55:45 +00:00
|
|
|
QAction * lookupSelection = 0;
|
2009-05-14 19:27:19 +00:00
|
|
|
QAction * lookupSelectionGr = 0;
|
2009-02-08 15:49:17 +00:00
|
|
|
QAction * lookupSelectionNewTab = 0;
|
2009-05-14 19:27:19 +00:00
|
|
|
QAction * lookupSelectionNewTabGr = 0;
|
2011-07-31 00:11:07 +00:00
|
|
|
QAction * maxDictionaryRefsAction = 0;
|
2012-09-12 14:11:30 +00:00
|
|
|
QAction * addWordToHistoryAction = 0;
|
2012-09-15 13:24:04 +00:00
|
|
|
QAction * addHeaderToHistoryAction = 0;
|
2012-11-26 13:13:13 +00:00
|
|
|
QAction * sendWordToInputLineAction = 0;
|
2013-02-22 12:44:23 +00:00
|
|
|
QAction * saveImageAction = 0;
|
|
|
|
QAction * saveSoundAction = 0;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2021-12-13 14:45:16 +00:00
|
|
|
//todo url() or lastclickurl ?
|
2021-07-06 13:01:50 +00:00
|
|
|
QUrl targetUrl( r->url() );
|
2009-05-29 19:48:50 +00:00
|
|
|
Contexts contexts;
|
|
|
|
|
|
|
|
tryMangleWebsiteClickedUrl( targetUrl, contexts );
|
|
|
|
|
2021-07-06 13:01:50 +00:00
|
|
|
if ( !r->url().isEmpty() )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-05-29 19:48:50 +00:00
|
|
|
if ( !isExternalLink( targetUrl ) )
|
2009-02-08 15:49:17 +00:00
|
|
|
{
|
2009-05-29 19:48:50 +00:00
|
|
|
followLink = new QAction( tr( "&Open Link" ), &menu );
|
|
|
|
menu.addAction( followLink );
|
2009-04-03 21:57:23 +00:00
|
|
|
|
2009-05-29 19:48:50 +00:00
|
|
|
if ( !popupView )
|
|
|
|
{
|
|
|
|
followLinkNewTab = new QAction( QIcon( ":/icons/addtab.png" ),
|
|
|
|
tr( "Open Link in New &Tab" ), &menu );
|
|
|
|
menu.addAction( followLinkNewTab );
|
|
|
|
}
|
|
|
|
}
|
2009-04-03 21:57:23 +00:00
|
|
|
|
2021-07-06 13:01:50 +00:00
|
|
|
if ( isExternalLink( r->url() ) )
|
2009-05-29 19:48:50 +00:00
|
|
|
{
|
|
|
|
followLinkExternal = new QAction( tr( "Open Link in &External Browser" ), &menu );
|
|
|
|
menu.addAction( followLinkExternal );
|
2021-07-06 13:01:50 +00:00
|
|
|
menu.addAction( ui.definition->pageAction( QWebEnginePage::CopyLinkToClipboard ) );
|
2009-05-29 19:48:50 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString selectedText = ui.definition->selectedText();
|
2017-04-24 14:41:07 +00:00
|
|
|
QString text = selectedText.trimmed();
|
2009-09-08 20:31:31 +00:00
|
|
|
|
2017-04-24 14:41:07 +00:00
|
|
|
if ( text.size() && text.size() < 60 )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-09-08 20:31:31 +00:00
|
|
|
// We don't prompt for selections larger or equal to 60 chars, since
|
|
|
|
// it ruins the menu and it's hardly a single word anyway.
|
|
|
|
|
2013-07-06 21:45:13 +00:00
|
|
|
if( text.isRightToLeft() )
|
|
|
|
{
|
|
|
|
text.insert( 0, (ushort)0x202E ); // RLE, Right-to-Left Embedding
|
|
|
|
text.append( (ushort)0x202C ); // PDF, POP DIRECTIONAL FORMATTING
|
|
|
|
}
|
|
|
|
|
2009-05-14 19:27:19 +00:00
|
|
|
lookupSelection = new QAction( tr( "&Look up \"%1\"" ).
|
2013-07-06 21:45:13 +00:00
|
|
|
arg( text ),
|
2009-05-14 19:27:19 +00:00
|
|
|
&menu );
|
2009-01-28 20:55:45 +00:00
|
|
|
menu.addAction( lookupSelection );
|
2010-09-16 18:53:39 +00:00
|
|
|
|
2009-02-08 15:49:17 +00:00
|
|
|
if ( !popupView )
|
|
|
|
{
|
2009-05-14 19:27:19 +00:00
|
|
|
lookupSelectionNewTab = new QAction( QIcon( ":/icons/addtab.png" ),
|
|
|
|
tr( "Look up \"%1\" in &New Tab" ).
|
2013-07-06 21:45:13 +00:00
|
|
|
arg( text ),
|
2009-05-14 19:27:19 +00:00
|
|
|
&menu );
|
2009-02-08 15:49:17 +00:00
|
|
|
menu.addAction( lookupSelectionNewTab );
|
2012-11-26 13:13:13 +00:00
|
|
|
|
|
|
|
sendWordToInputLineAction = new QAction( tr( "Send \"%1\" to input line" ).
|
2013-07-06 21:45:13 +00:00
|
|
|
arg( text ),
|
2012-11-26 13:13:13 +00:00
|
|
|
&menu );
|
|
|
|
menu.addAction( sendWordToInputLineAction );
|
2009-02-08 15:49:17 +00:00
|
|
|
}
|
2009-04-03 21:57:23 +00:00
|
|
|
|
2012-09-16 10:30:14 +00:00
|
|
|
addWordToHistoryAction = new QAction( tr( "&Add \"%1\" to history" ).
|
2013-07-06 21:45:13 +00:00
|
|
|
arg( text ),
|
2012-09-16 10:30:14 +00:00
|
|
|
&menu );
|
|
|
|
menu.addAction( addWordToHistoryAction );
|
|
|
|
|
2009-05-14 19:27:19 +00:00
|
|
|
Instances::Group const * altGroup =
|
|
|
|
( groupComboBox && groupComboBox->getCurrentGroup() != getGroup( ui.definition->url() ) ) ?
|
|
|
|
groups.findGroup( groupComboBox->getCurrentGroup() ) : 0;
|
|
|
|
|
|
|
|
if ( altGroup )
|
|
|
|
{
|
|
|
|
QIcon icon = altGroup->icon.size() ? QIcon( ":/flags/" + altGroup->icon ) :
|
|
|
|
QIcon();
|
|
|
|
|
2009-05-24 16:40:53 +00:00
|
|
|
lookupSelectionGr = new QAction( icon, tr( "Look up \"%1\" in %2" ).
|
2017-04-24 14:41:07 +00:00
|
|
|
arg( text ).
|
2009-05-14 19:27:19 +00:00
|
|
|
arg( altGroup->name ), &menu );
|
|
|
|
menu.addAction( lookupSelectionGr );
|
|
|
|
|
|
|
|
if ( !popupView )
|
|
|
|
{
|
|
|
|
lookupSelectionNewTabGr = new QAction( QIcon( ":/icons/addtab.png" ),
|
|
|
|
tr( "Look up \"%1\" in %2 in &New Tab" ).
|
2017-04-24 14:41:07 +00:00
|
|
|
arg( text ).
|
2009-05-14 19:27:19 +00:00
|
|
|
arg( altGroup->name ), &menu );
|
|
|
|
menu.addAction( lookupSelectionNewTabGr );
|
|
|
|
}
|
|
|
|
}
|
2009-09-08 20:31:31 +00:00
|
|
|
}
|
2009-05-14 19:27:19 +00:00
|
|
|
|
2017-04-24 14:41:07 +00:00
|
|
|
if( text.isEmpty() && !cfg.preferences.storeHistory)
|
2013-01-17 18:38:49 +00:00
|
|
|
{
|
2015-10-14 15:18:11 +00:00
|
|
|
QString txt = ui.definition->title();
|
|
|
|
if( txt.size() > 60 )
|
|
|
|
txt = txt.left( 60 ) + "...";
|
|
|
|
|
|
|
|
addHeaderToHistoryAction = new QAction( tr( "&Add \"%1\" to history" ).
|
|
|
|
arg( txt ),
|
2013-01-17 18:38:49 +00:00
|
|
|
&menu );
|
2015-10-14 15:18:11 +00:00
|
|
|
menu.addAction( addHeaderToHistoryAction );
|
2013-01-17 18:38:49 +00:00
|
|
|
}
|
|
|
|
|
2013-01-18 13:22:24 +00:00
|
|
|
if ( selectedText.size() )
|
|
|
|
{
|
2021-07-06 13:01:50 +00:00
|
|
|
menu.addAction( ui.definition->pageAction( QWebEnginePage::Copy ) );
|
2013-05-15 13:52:47 +00:00
|
|
|
menu.addAction( ©AsTextAction );
|
2013-01-18 13:22:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-01-18 14:37:24 +00:00
|
|
|
menu.addAction( &selectCurrentArticleAction );
|
2021-07-06 13:01:50 +00:00
|
|
|
menu.addAction( ui.definition->pageAction( QWebEnginePage::SelectAll ) );
|
2013-01-18 13:22:24 +00:00
|
|
|
}
|
|
|
|
|
2009-04-12 16:22:42 +00:00
|
|
|
map< QAction *, QString > tableOfContents;
|
2010-09-16 18:53:39 +00:00
|
|
|
|
2009-04-12 16:22:42 +00:00
|
|
|
// Add table of contents
|
2009-05-11 22:25:22 +00:00
|
|
|
QStringList ids = getArticlesList();
|
2009-04-12 16:22:42 +00:00
|
|
|
|
|
|
|
if ( !menu.isEmpty() && ids.size() )
|
|
|
|
menu.addSeparator();
|
2009-04-14 18:35:27 +00:00
|
|
|
|
|
|
|
unsigned refsAdded = 0;
|
2011-07-31 00:11:07 +00:00
|
|
|
bool maxDictionaryRefsReached = false;
|
2009-04-14 18:35:27 +00:00
|
|
|
|
2009-04-12 16:22:42 +00:00
|
|
|
for( QStringList::const_iterator i = ids.constBegin(); i != ids.constEnd();
|
2009-04-14 18:35:27 +00:00
|
|
|
++i, ++refsAdded )
|
2009-04-12 16:22:42 +00:00
|
|
|
{
|
|
|
|
// Find this dictionary
|
|
|
|
|
|
|
|
for( unsigned x = allDictionaries.size(); x--; )
|
|
|
|
{
|
|
|
|
if ( allDictionaries[ x ]->getId() == i->toUtf8().data() )
|
|
|
|
{
|
2011-07-31 00:11:07 +00:00
|
|
|
QAction * action = 0;
|
2013-06-11 18:31:01 +00:00
|
|
|
if ( refsAdded == cfg.preferences.maxDictionaryRefsInContextMenu )
|
2011-07-31 00:11:07 +00:00
|
|
|
{
|
|
|
|
// Enough! Or the menu would become too large.
|
|
|
|
maxDictionaryRefsAction = new QAction( ".........", &menu );
|
|
|
|
action = maxDictionaryRefsAction;
|
|
|
|
maxDictionaryRefsReached = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
action = new QAction(
|
|
|
|
allDictionaries[ x ]->getIcon(),
|
|
|
|
QString::fromUtf8( allDictionaries[ x ]->getName().c_str() ),
|
|
|
|
&menu );
|
2018-07-07 09:33:15 +00:00
|
|
|
// Force icons in menu on all platforms,
|
2011-07-31 00:11:07 +00:00
|
|
|
// since without them it will be much harder
|
|
|
|
// to find things.
|
|
|
|
action->setIconVisibleInMenu( true );
|
|
|
|
}
|
2009-04-12 16:22:42 +00:00
|
|
|
menu.addAction( action );
|
|
|
|
|
|
|
|
tableOfContents[ action ] = *i;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-07-31 00:11:07 +00:00
|
|
|
if( maxDictionaryRefsReached )
|
|
|
|
break;
|
2009-04-12 16:22:42 +00:00
|
|
|
}
|
2010-09-16 18:53:39 +00:00
|
|
|
|
2013-05-30 02:18:28 +00:00
|
|
|
menu.addSeparator();
|
|
|
|
menu.addAction( &inspectAction );
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
if ( !menu.isEmpty() )
|
|
|
|
{
|
2012-09-12 14:18:16 +00:00
|
|
|
connect( this, SIGNAL( closePopupMenu() ), &menu, SLOT( close() ) );
|
2009-01-28 20:55:45 +00:00
|
|
|
QAction * result = menu.exec( ui.definition->mapToGlobal( pos ) );
|
|
|
|
|
2009-05-29 19:48:50 +00:00
|
|
|
if ( !result )
|
|
|
|
return;
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
if ( result == followLink )
|
2009-05-29 19:48:50 +00:00
|
|
|
openLink( targetUrl, ui.definition->url(), getCurrentArticle(), contexts );
|
|
|
|
else
|
|
|
|
if ( result == followLinkExternal )
|
2021-07-06 13:01:50 +00:00
|
|
|
QDesktopServices::openUrl( r->url() );
|
2009-01-28 20:55:45 +00:00
|
|
|
else
|
|
|
|
if ( result == lookupSelection )
|
2009-05-11 19:14:28 +00:00
|
|
|
showDefinition( selectedText, getGroup( ui.definition->url() ), getCurrentArticle() );
|
2009-02-08 15:49:17 +00:00
|
|
|
else
|
2009-05-14 19:27:19 +00:00
|
|
|
if ( result == lookupSelectionGr && groupComboBox )
|
|
|
|
showDefinition( selectedText, groupComboBox->getCurrentGroup(), QString() );
|
|
|
|
else
|
2012-09-12 14:11:30 +00:00
|
|
|
if ( result == addWordToHistoryAction )
|
|
|
|
emit forceAddWordToHistory( selectedText );
|
2012-09-15 13:24:04 +00:00
|
|
|
if ( result == addHeaderToHistoryAction )
|
|
|
|
emit forceAddWordToHistory( ui.definition->title() );
|
2012-09-12 14:11:30 +00:00
|
|
|
else
|
2012-11-26 13:13:13 +00:00
|
|
|
if( result == sendWordToInputLineAction )
|
|
|
|
emit sendWordToInputLine( selectedText );
|
|
|
|
else
|
2009-04-12 16:22:42 +00:00
|
|
|
if ( !popupView && result == followLinkNewTab )
|
2009-05-29 19:48:50 +00:00
|
|
|
emit openLinkInNewTab( targetUrl, ui.definition->url(), getCurrentArticle(), contexts );
|
2009-04-12 16:22:42 +00:00
|
|
|
else
|
|
|
|
if ( !popupView && result == lookupSelectionNewTab )
|
2009-05-11 19:14:28 +00:00
|
|
|
emit showDefinitionInNewTab( selectedText, getGroup( ui.definition->url() ),
|
2009-05-29 19:48:50 +00:00
|
|
|
getCurrentArticle(), Contexts() );
|
2009-04-12 16:22:42 +00:00
|
|
|
else
|
2009-05-14 19:27:19 +00:00
|
|
|
if ( !popupView && result == lookupSelectionNewTabGr && groupComboBox )
|
|
|
|
emit showDefinitionInNewTab( selectedText, groupComboBox->getCurrentGroup(),
|
2009-05-29 19:48:50 +00:00
|
|
|
QString(), Contexts() );
|
2009-05-14 19:27:19 +00:00
|
|
|
else
|
2013-05-27 11:18:13 +00:00
|
|
|
if( result == saveImageAction || result == saveSoundAction )
|
|
|
|
{
|
2013-07-18 13:02:39 +00:00
|
|
|
#if QT_VERSION >= 0x040600
|
2021-07-06 13:01:50 +00:00
|
|
|
// QUrl url = ( result == saveImageAction ) ? imageUrl : targetUrl;
|
|
|
|
QUrl url = targetUrl;
|
2013-05-27 11:18:13 +00:00
|
|
|
QString savePath;
|
|
|
|
QString fileName;
|
|
|
|
|
|
|
|
if ( cfg.resourceSavePath.isEmpty() )
|
|
|
|
savePath = QDir::homePath();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
savePath = QDir::fromNativeSeparators( cfg.resourceSavePath );
|
|
|
|
if ( !QDir( savePath ).exists() )
|
|
|
|
savePath = QDir::homePath();
|
|
|
|
}
|
|
|
|
|
2021-11-27 07:17:33 +00:00
|
|
|
QString name = Utils::Url::path( url ).section( '/', -1 );
|
2013-05-27 11:18:13 +00:00
|
|
|
|
|
|
|
if ( result == saveSoundAction )
|
|
|
|
{
|
|
|
|
// Audio data
|
|
|
|
if ( name.indexOf( '.' ) < 0 )
|
|
|
|
name += ".wav";
|
|
|
|
|
|
|
|
fileName = savePath + "/" + name;
|
|
|
|
fileName = QFileDialog::getSaveFileName( parentWidget(), tr( "Save sound" ),
|
|
|
|
fileName,
|
2021-06-11 15:04:42 +00:00
|
|
|
tr( "Sound files (*.wav *.ogg *.oga *.mp3 *.mp4 *.aac *.flac *.mid *.wv *.ape);;All files (*.*)" ) );
|
2013-05-27 11:18:13 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Image data
|
|
|
|
|
|
|
|
// Check for babylon image name
|
|
|
|
if ( name[ 0 ] == '\x1E' )
|
|
|
|
name.remove( 0, 1 );
|
|
|
|
if ( name.length() && name[ name.length() - 1 ] == '\x1F' )
|
|
|
|
name.chop( 1 );
|
|
|
|
|
|
|
|
fileName = savePath + "/" + name;
|
|
|
|
fileName = QFileDialog::getSaveFileName( parentWidget(), tr( "Save image" ),
|
|
|
|
fileName,
|
|
|
|
tr( "Image files (*.bmp *.jpg *.png *.tif);;All files (*.*)" ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !fileName.isEmpty() )
|
|
|
|
{
|
|
|
|
QFileInfo fileInfo( fileName );
|
|
|
|
emit storeResourceSavePath( QDir::toNativeSeparators( fileInfo.absoluteDir().absolutePath() ) );
|
|
|
|
saveResource( url, ui.definition->url(), fileName );
|
|
|
|
}
|
2013-07-18 13:02:39 +00:00
|
|
|
#endif
|
2013-05-27 11:18:13 +00:00
|
|
|
}
|
2013-02-22 12:44:23 +00:00
|
|
|
else
|
2009-02-08 15:49:17 +00:00
|
|
|
{
|
2011-07-31 00:11:07 +00:00
|
|
|
if ( !popupView && result == maxDictionaryRefsAction )
|
|
|
|
emit showDictsPane();
|
|
|
|
|
2009-04-12 16:22:42 +00:00
|
|
|
// Match against table of contents
|
|
|
|
QString id = tableOfContents[ result ];
|
|
|
|
|
|
|
|
if ( id.size() )
|
2021-06-29 08:59:16 +00:00
|
|
|
setCurrentArticle( scrollToFromDictionaryId( id ), true );
|
2009-02-08 15:49:17 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2021-09-22 04:01:17 +00:00
|
|
|
qDebug( "url = %s\n", r->url().toString().toLocal8Bit().data() );
|
|
|
|
qDebug( "title = %s\n", r->title().toLocal8Bit().data() );
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
void ArticleView::resourceDownloadFinished()
|
|
|
|
{
|
|
|
|
if ( resourceDownloadRequests.empty() )
|
|
|
|
return; // Stray signal
|
|
|
|
|
|
|
|
// Find any finished resources
|
|
|
|
for( list< sptr< Dictionary::DataRequest > >::iterator i =
|
|
|
|
resourceDownloadRequests.begin(); i != resourceDownloadRequests.end(); )
|
|
|
|
{
|
|
|
|
if ( (*i)->isFinished() )
|
|
|
|
{
|
|
|
|
if ( (*i)->dataSize() >= 0 )
|
|
|
|
{
|
|
|
|
// Ok, got one finished, all others are irrelevant now
|
|
|
|
|
|
|
|
vector< char > const & data = (*i)->getFullData();
|
|
|
|
|
2010-05-29 11:33:04 +00:00
|
|
|
if ( resourceDownloadUrl.scheme() == "gdau" ||
|
|
|
|
Dictionary::WebMultimediaDownload::isAudioUrl( resourceDownloadUrl ) )
|
2009-03-26 19:00:08 +00:00
|
|
|
{
|
2010-01-02 18:16:22 +00:00
|
|
|
// Audio data
|
2018-03-22 17:07:10 +00:00
|
|
|
connect( audioPlayer.data(), SIGNAL( error( QString ) ), this, SLOT( audioPlayerError( QString ) ), Qt::UniqueConnection );
|
|
|
|
QString errorMessage = audioPlayer->play( data.data(), data.size() );
|
|
|
|
if( !errorMessage.isEmpty() )
|
|
|
|
QMessageBox::critical( this, "GoldenDict", tr( "Failed to play sound file: %1" ).arg( errorMessage ) );
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Create a temporary file
|
2017-03-13 14:38:27 +00:00
|
|
|
// Remove the ones previously used, if any
|
2009-03-26 19:00:08 +00:00
|
|
|
cleanupTemp();
|
2017-03-13 14:38:27 +00:00
|
|
|
QString fileName;
|
2010-09-16 18:53:39 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
{
|
|
|
|
QTemporaryFile tmp(
|
|
|
|
QDir::temp().filePath( "XXXXXX-" + resourceDownloadUrl.path().section( '/', -1 ) ), this );
|
|
|
|
|
2012-10-31 13:58:35 +00:00
|
|
|
if ( !tmp.open() || (size_t) tmp.write( &data.front(), data.size() ) != data.size() )
|
2009-03-26 19:00:08 +00:00
|
|
|
{
|
2013-02-01 12:36:01 +00:00
|
|
|
QMessageBox::critical( this, "GoldenDict", tr( "Failed to create temporary file." ) );
|
2009-03-26 19:00:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-09-16 18:53:39 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
tmp.setAutoRemove( false );
|
2010-09-16 18:53:39 +00:00
|
|
|
|
2017-03-13 14:38:27 +00:00
|
|
|
desktopOpenedTempFiles.insert( fileName = tmp.fileName() );
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|
2010-09-16 18:53:39 +00:00
|
|
|
|
2017-03-13 14:38:27 +00:00
|
|
|
if ( !QDesktopServices::openUrl( QUrl::fromLocalFile( fileName ) ) )
|
2013-02-01 12:36:01 +00:00
|
|
|
QMessageBox::critical( this, "GoldenDict",
|
2017-03-13 14:38:27 +00:00
|
|
|
tr( "Failed to auto-open resource file, try opening manually: %1." ).arg( fileName ) );
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ok, whatever it was, it's finished. Remove this and any other
|
|
|
|
// requests and finish.
|
|
|
|
|
|
|
|
resourceDownloadRequests.clear();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-05-29 05:08:37 +00:00
|
|
|
// This one had no data. Erase it.
|
2009-03-26 19:00:08 +00:00
|
|
|
resourceDownloadRequests.erase( i++ );
|
|
|
|
}
|
|
|
|
}
|
2017-04-27 20:55:53 +00:00
|
|
|
else // Unfinished, wait.
|
|
|
|
break;
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( resourceDownloadRequests.empty() )
|
|
|
|
{
|
2011-07-14 20:11:57 +00:00
|
|
|
emit statusBarMessage(
|
|
|
|
tr( "WARNING: %1" ).arg( tr( "The referenced resource failed to download." ) ),
|
|
|
|
10000, QPixmap( ":/icons/error.png" ) );
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-05 10:22:12 +00:00
|
|
|
void ArticleView::audioPlayerError( QString const & message )
|
|
|
|
{
|
2018-03-22 17:07:10 +00:00
|
|
|
emit statusBarMessage( tr( "WARNING: Audio Player: %1" ).arg( message ),
|
2013-05-05 10:22:12 +00:00
|
|
|
10000, QPixmap( ":/icons/error.png" ) );
|
|
|
|
}
|
|
|
|
|
2009-05-12 10:52:11 +00:00
|
|
|
void ArticleView::pasteTriggered()
|
|
|
|
{
|
2021-06-10 16:13:11 +00:00
|
|
|
Config::InputPhrase phrase = cfg.preferences.sanitizeInputPhrase( QApplication::clipboard()->text() );
|
2009-05-12 10:52:11 +00:00
|
|
|
|
2021-06-10 16:13:11 +00:00
|
|
|
if ( phrase.isValid() )
|
2013-08-22 16:05:39 +00:00
|
|
|
{
|
|
|
|
unsigned groupId = getGroup( ui.definition->url() );
|
|
|
|
if ( groupId == 0 )
|
|
|
|
{
|
|
|
|
// We couldn't figure out the group out of the URL,
|
|
|
|
// so let's try the currently selected group.
|
|
|
|
groupId = groupComboBox->getCurrentGroup();
|
|
|
|
}
|
2021-06-10 16:13:11 +00:00
|
|
|
showDefinition( phrase, groupId, getCurrentArticle() );
|
2013-08-22 16:05:39 +00:00
|
|
|
}
|
2009-05-12 10:52:11 +00:00
|
|
|
}
|
|
|
|
|
2009-05-15 14:11:54 +00:00
|
|
|
void ArticleView::moveOneArticleUp()
|
|
|
|
{
|
|
|
|
QString current = getCurrentArticle();
|
|
|
|
|
|
|
|
if ( current.size() )
|
|
|
|
{
|
|
|
|
QStringList lst = getArticlesList();
|
|
|
|
|
2021-06-29 08:59:16 +00:00
|
|
|
int idx = lst.indexOf( dictionaryIdFromScrollTo( current ) );
|
2009-05-15 14:11:54 +00:00
|
|
|
|
|
|
|
if ( idx != -1 )
|
|
|
|
{
|
|
|
|
--idx;
|
|
|
|
|
|
|
|
if ( idx < 0 )
|
|
|
|
idx = lst.size() - 1;
|
|
|
|
|
2021-06-29 08:59:16 +00:00
|
|
|
setCurrentArticle( scrollToFromDictionaryId( lst[ idx ] ), true );
|
2009-05-15 14:11:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleView::moveOneArticleDown()
|
|
|
|
{
|
|
|
|
QString current = getCurrentArticle();
|
|
|
|
|
|
|
|
if ( current.size() )
|
|
|
|
{
|
|
|
|
QStringList lst = getArticlesList();
|
|
|
|
|
2021-06-29 08:59:16 +00:00
|
|
|
int idx = lst.indexOf( dictionaryIdFromScrollTo( current ) );
|
2009-05-15 14:11:54 +00:00
|
|
|
|
|
|
|
if ( idx != -1 )
|
|
|
|
{
|
|
|
|
idx = ( idx + 1 ) % lst.size();
|
|
|
|
|
2021-06-29 08:59:16 +00:00
|
|
|
setCurrentArticle( scrollToFromDictionaryId( lst[ idx ] ), true );
|
2009-05-15 14:11:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-16 11:14:43 +00:00
|
|
|
void ArticleView::openSearch()
|
|
|
|
{
|
2014-04-16 16:18:28 +00:00
|
|
|
if( !isVisible() )
|
|
|
|
return;
|
|
|
|
|
2014-04-22 13:47:02 +00:00
|
|
|
if( ftsSearchIsOpened )
|
|
|
|
closeSearch();
|
|
|
|
|
2009-05-16 11:14:43 +00:00
|
|
|
if ( !searchIsOpened )
|
|
|
|
{
|
|
|
|
ui.searchFrame->show();
|
|
|
|
ui.searchText->setText( getTitle() );
|
|
|
|
searchIsOpened = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.searchText->setFocus();
|
|
|
|
ui.searchText->selectAll();
|
|
|
|
|
|
|
|
// Clear any current selection
|
|
|
|
if ( ui.definition->selectedText().size() )
|
|
|
|
{
|
2021-07-06 13:01:50 +00:00
|
|
|
ui.definition->page()->
|
|
|
|
runJavaScript( "window.getSelection().removeAllRanges();_=0;" );
|
2009-05-16 11:14:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ui.searchText->property( "noResults" ).toBool() )
|
|
|
|
{
|
|
|
|
ui.searchText->setProperty( "noResults", false );
|
2009-05-16 18:04:21 +00:00
|
|
|
|
|
|
|
// Reload stylesheet
|
2010-11-15 15:22:35 +00:00
|
|
|
reloadStyleSheet();
|
2009-05-16 11:14:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleView::on_searchPrevious_clicked()
|
|
|
|
{
|
2013-01-22 21:04:45 +00:00
|
|
|
if ( searchIsOpened )
|
|
|
|
performFindOperation( false, true );
|
2009-05-16 11:14:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleView::on_searchNext_clicked()
|
|
|
|
{
|
2013-01-22 21:04:45 +00:00
|
|
|
if ( searchIsOpened )
|
|
|
|
performFindOperation( false, false );
|
2009-05-16 11:14:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleView::on_searchText_textEdited()
|
|
|
|
{
|
|
|
|
performFindOperation( true, false );
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleView::on_searchText_returnPressed()
|
|
|
|
{
|
|
|
|
on_searchNext_clicked();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleView::on_searchCloseButton_clicked()
|
|
|
|
{
|
|
|
|
closeSearch();
|
|
|
|
}
|
|
|
|
|
2011-06-12 23:59:35 +00:00
|
|
|
void ArticleView::on_searchCaseSensitive_clicked()
|
|
|
|
{
|
|
|
|
performFindOperation( false, false, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleView::on_highlightAllButton_clicked()
|
|
|
|
{
|
|
|
|
performFindOperation( false, false, true );
|
|
|
|
}
|
|
|
|
|
2011-07-03 12:27:08 +00:00
|
|
|
void ArticleView::onJsActiveArticleChanged(QString const & id)
|
|
|
|
{
|
2021-06-29 08:59:16 +00:00
|
|
|
if ( !isScrollTo( id ) )
|
2011-07-03 12:27:08 +00:00
|
|
|
return; // Incorrect id
|
|
|
|
|
2021-06-29 08:59:16 +00:00
|
|
|
emit activeArticleChanged( this, dictionaryIdFromScrollTo( id ) );
|
2011-07-03 12:27:08 +00:00
|
|
|
}
|
|
|
|
|
2017-03-13 14:38:27 +00:00
|
|
|
void ArticleView::doubleClicked( QPoint pos )
|
2010-04-08 20:37:59 +00:00
|
|
|
{
|
2021-07-06 13:01:50 +00:00
|
|
|
//#if QT_VERSION >= 0x040600
|
|
|
|
// QWebEnginePage* r = ui.definition->page();
|
|
|
|
// QWebElement el = r.element();
|
|
|
|
// QUrl imageUrl;
|
|
|
|
// if( !popupView && el.tagName().compare( "img", Qt::CaseInsensitive ) == 0 )
|
|
|
|
// {
|
|
|
|
// // Double click on image; download it and transfer to external program
|
|
|
|
|
|
|
|
// imageUrl = QUrl::fromPercentEncoding( el.attribute( "src" ).toLatin1() );
|
|
|
|
// if( !imageUrl.isEmpty() )
|
|
|
|
// {
|
|
|
|
// // Download it
|
|
|
|
|
|
|
|
// // Clear any pending ones
|
|
|
|
// resourceDownloadRequests.clear();
|
|
|
|
|
|
|
|
// resourceDownloadUrl = imageUrl;
|
|
|
|
// sptr< Dictionary::DataRequest > req;
|
|
|
|
|
|
|
|
// if ( imageUrl.scheme() == "http" || imageUrl.scheme() == "https" || imageUrl.scheme() == "ftp" )
|
|
|
|
// {
|
|
|
|
// // Web resource
|
|
|
|
// req = new Dictionary::WebMultimediaDownload( imageUrl, articleNetMgr );
|
|
|
|
// }
|
|
|
|
// else
|
|
|
|
// if ( imageUrl.scheme() == "bres" || imageUrl.scheme() == "gdpicture" )
|
|
|
|
// {
|
|
|
|
// // Local resource
|
|
|
|
// QString contentType;
|
|
|
|
// req = articleNetMgr.getResource( imageUrl, contentType );
|
|
|
|
// }
|
|
|
|
// else
|
|
|
|
// {
|
|
|
|
// // Unsupported scheme
|
|
|
|
// gdWarning( "Unsupported url scheme \"%s\" to download image\n", imageUrl.scheme().toUtf8().data() );
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if ( !req.get() )
|
|
|
|
// {
|
|
|
|
// // Request failed, fail
|
|
|
|
// gdWarning( "Can't create request to download image \"%s\"\n", imageUrl.toString().toUtf8().data() );
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if ( req->isFinished() && req->dataSize() >= 0 )
|
|
|
|
// {
|
|
|
|
// // Have data ready, handle it
|
|
|
|
// resourceDownloadRequests.push_back( req );
|
|
|
|
// resourceDownloadFinished();
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// else
|
|
|
|
// if ( !req->isFinished() )
|
|
|
|
// {
|
|
|
|
// // Queue to be handled when done
|
|
|
|
// resourceDownloadRequests.push_back( req );
|
|
|
|
// connect( req.get(), SIGNAL( finished() ), this, SLOT( resourceDownloadFinished() ) );
|
|
|
|
// }
|
|
|
|
// if ( resourceDownloadRequests.empty() ) // No requests were queued
|
|
|
|
// {
|
|
|
|
// gdWarning( "The referenced resource \"%s\" doesn't exist\n", imageUrl.toString().toUtf8().data() ) ;
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// else
|
|
|
|
// resourceDownloadFinished(); // Check any requests finished already
|
|
|
|
// }
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
//#endif
|
2017-03-13 14:38:27 +00:00
|
|
|
|
2010-04-08 20:37:59 +00:00
|
|
|
// We might want to initiate translation of the selected word
|
|
|
|
|
|
|
|
if ( cfg.preferences.doubleClickTranslates )
|
|
|
|
{
|
|
|
|
QString selectedText = ui.definition->selectedText();
|
|
|
|
|
|
|
|
// Do some checks to make sure there's a sensible selection indeed
|
|
|
|
if ( Folding::applyWhitespaceOnly( gd::toWString( selectedText ) ).size() &&
|
2013-01-18 11:00:38 +00:00
|
|
|
selectedText.size() < 60 )
|
2010-04-08 20:37:59 +00:00
|
|
|
{
|
|
|
|
// Initiate translation
|
2010-09-16 18:53:39 +00:00
|
|
|
Qt::KeyboardModifiers kmod = QApplication::keyboardModifiers();
|
2011-05-02 00:37:56 +00:00
|
|
|
if (kmod & (Qt::ControlModifier | Qt::ShiftModifier))
|
2010-09-16 18:53:39 +00:00
|
|
|
{ // open in new tab
|
|
|
|
emit showDefinitionInNewTab( selectedText, getGroup( ui.definition->url() ),
|
|
|
|
getCurrentArticle(), Contexts() );
|
|
|
|
}
|
|
|
|
else
|
2014-04-22 18:45:42 +00:00
|
|
|
{
|
|
|
|
QUrl const & ref = ui.definition->url();
|
|
|
|
|
2021-11-27 07:17:33 +00:00
|
|
|
if( Utils::Url::hasQueryItem( ref, "dictionaries" ) )
|
2014-04-22 18:45:42 +00:00
|
|
|
{
|
2021-11-27 07:17:33 +00:00
|
|
|
QStringList dictsList = Utils::Url::queryItemValue(ref, "dictionaries" )
|
2014-04-23 14:19:46 +00:00
|
|
|
.split( ",", QString::SkipEmptyParts );
|
2018-04-10 14:49:52 +00:00
|
|
|
showDefinition( selectedText, dictsList, QRegExp(), getGroup( ref ), false );
|
2014-04-22 18:45:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
showDefinition( selectedText, getGroup( ref ), getCurrentArticle() );
|
|
|
|
}
|
2010-04-08 20:37:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-12 23:59:35 +00:00
|
|
|
void ArticleView::performFindOperation( bool restart, bool backwards, bool checkHighlight )
|
2009-05-16 11:14:43 +00:00
|
|
|
{
|
|
|
|
QString text = ui.searchText->text();
|
|
|
|
|
2011-06-12 23:59:35 +00:00
|
|
|
if ( restart || checkHighlight )
|
2009-05-16 11:14:43 +00:00
|
|
|
{
|
2011-06-12 23:59:35 +00:00
|
|
|
if( restart ) {
|
|
|
|
// Anyone knows how we reset the search position?
|
|
|
|
// For now we resort to this hack:
|
|
|
|
if ( ui.definition->selectedText().size() )
|
|
|
|
{
|
2021-07-06 13:01:50 +00:00
|
|
|
ui.definition->page()->
|
|
|
|
runJavaScript( "window.getSelection().removeAllRanges();_=0;" );
|
2011-06-12 23:59:35 +00:00
|
|
|
}
|
2009-05-16 11:14:43 +00:00
|
|
|
}
|
2011-06-12 23:59:35 +00:00
|
|
|
|
2021-07-06 13:01:50 +00:00
|
|
|
QWebEnginePage::FindFlags f( 0 );
|
2011-06-12 23:59:35 +00:00
|
|
|
|
|
|
|
if ( ui.searchCaseSensitive->isChecked() )
|
2021-07-06 13:01:50 +00:00
|
|
|
f |= QWebEnginePage::FindCaseSensitively;
|
2011-06-12 23:59:35 +00:00
|
|
|
|
|
|
|
ui.definition->findText( "", f );
|
|
|
|
|
|
|
|
if( ui.highlightAllButton->isChecked() )
|
|
|
|
ui.definition->findText( text, f );
|
|
|
|
|
|
|
|
if( checkHighlight )
|
|
|
|
return;
|
2009-05-16 11:14:43 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 13:01:50 +00:00
|
|
|
QWebEnginePage::FindFlags f( 0 );
|
2009-05-16 11:14:43 +00:00
|
|
|
|
|
|
|
if ( ui.searchCaseSensitive->isChecked() )
|
2021-07-06 13:01:50 +00:00
|
|
|
f |= QWebEnginePage::FindCaseSensitively;
|
2009-05-16 11:14:43 +00:00
|
|
|
|
|
|
|
if ( backwards )
|
2021-07-06 13:01:50 +00:00
|
|
|
f |= QWebEnginePage::FindBackward;
|
2009-05-16 11:14:43 +00:00
|
|
|
|
2021-07-06 13:01:50 +00:00
|
|
|
bool setMark = text.size() && !findText(text, f);
|
2009-05-16 11:14:43 +00:00
|
|
|
|
|
|
|
if ( ui.searchText->property( "noResults" ).toBool() != setMark )
|
|
|
|
{
|
|
|
|
ui.searchText->setProperty( "noResults", setMark );
|
2009-05-16 18:04:21 +00:00
|
|
|
|
|
|
|
// Reload stylesheet
|
2010-11-15 15:22:35 +00:00
|
|
|
reloadStyleSheet();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-06 13:01:50 +00:00
|
|
|
bool ArticleView::findText(QString& text, const QWebEnginePage::FindFlags& f)
|
|
|
|
{
|
2021-11-30 02:53:18 +00:00
|
|
|
bool r;
|
2021-12-02 05:40:28 +00:00
|
|
|
// turn async to sync invoke.
|
|
|
|
QSharedPointer<QEventLoop> loop = QSharedPointer<QEventLoop>(new QEventLoop());
|
|
|
|
QTimer::singleShot(1000, loop.data(), &QEventLoop::quit);
|
2021-11-30 02:53:18 +00:00
|
|
|
ui.definition->findText(text, f, [&](bool result)
|
|
|
|
{
|
2021-12-02 05:40:28 +00:00
|
|
|
if(loop->isRunning()){
|
|
|
|
r = result;
|
|
|
|
loop->quit();
|
|
|
|
} });
|
|
|
|
|
|
|
|
loop->exec();
|
2021-11-30 02:53:18 +00:00
|
|
|
return r;
|
2021-07-06 13:01:50 +00:00
|
|
|
}
|
|
|
|
|
2010-11-15 15:22:35 +00:00
|
|
|
void ArticleView::reloadStyleSheet()
|
|
|
|
{
|
|
|
|
for( QWidget * w = parentWidget(); w; w = w->parentWidget() )
|
|
|
|
{
|
|
|
|
if ( w->styleSheet().size() )
|
2009-05-16 18:04:21 +00:00
|
|
|
{
|
2010-11-15 15:22:35 +00:00
|
|
|
w->setStyleSheet( w->styleSheet() );
|
|
|
|
break;
|
2009-05-16 18:04:21 +00:00
|
|
|
}
|
2009-05-16 11:14:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-15 15:22:35 +00:00
|
|
|
|
2009-05-16 11:14:43 +00:00
|
|
|
bool ArticleView::closeSearch()
|
|
|
|
{
|
|
|
|
if ( searchIsOpened )
|
|
|
|
{
|
|
|
|
ui.searchFrame->hide();
|
|
|
|
ui.definition->setFocus();
|
|
|
|
searchIsOpened = false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2014-04-22 13:47:02 +00:00
|
|
|
else
|
|
|
|
if( ftsSearchIsOpened )
|
|
|
|
{
|
|
|
|
allMatches.clear();
|
|
|
|
uniqueMatches.clear();
|
|
|
|
ftsPosition = 0;
|
|
|
|
ftsSearchIsOpened = false;
|
|
|
|
|
|
|
|
ui.ftsSearchFrame->hide();
|
|
|
|
ui.definition->setFocus();
|
|
|
|
|
2021-07-06 13:01:50 +00:00
|
|
|
QWebEnginePage::FindFlags flags ( 0 );
|
2014-04-22 13:47:02 +00:00
|
|
|
|
|
|
|
ui.definition->findText( "", flags );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2009-05-16 11:14:43 +00:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-06-29 19:12:46 +00:00
|
|
|
bool ArticleView::isSearchOpened()
|
|
|
|
{
|
|
|
|
return searchIsOpened;
|
|
|
|
}
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
void ArticleView::showEvent( QShowEvent * ev )
|
|
|
|
{
|
|
|
|
QFrame::showEvent( ev );
|
|
|
|
|
2009-05-16 11:14:43 +00:00
|
|
|
if ( !searchIsOpened )
|
|
|
|
ui.searchFrame->hide();
|
2014-04-22 13:47:02 +00:00
|
|
|
|
|
|
|
if( !ftsSearchIsOpened )
|
|
|
|
ui.ftsSearchFrame->hide();
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
2012-09-16 10:19:47 +00:00
|
|
|
|
|
|
|
void ArticleView::receiveExpandOptionalParts( bool expand )
|
|
|
|
{
|
|
|
|
if( expandOptionalParts != expand )
|
|
|
|
{
|
|
|
|
int n = getArticlesList().indexOf( getActiveArticleId() );
|
|
|
|
if( n > 0 )
|
|
|
|
articleToJump = getCurrentArticle();
|
|
|
|
|
|
|
|
emit setExpandMode( expand );
|
|
|
|
expandOptionalParts = expand;
|
|
|
|
reload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleView::switchExpandOptionalParts()
|
|
|
|
{
|
|
|
|
expandOptionalParts = !expandOptionalParts;
|
|
|
|
|
|
|
|
int n = getArticlesList().indexOf( getActiveArticleId() );
|
|
|
|
if( n > 0 )
|
|
|
|
articleToJump = getCurrentArticle();
|
|
|
|
|
|
|
|
emit setExpandMode( expandOptionalParts );
|
|
|
|
reload();
|
|
|
|
}
|
2012-09-18 23:01:31 +00:00
|
|
|
|
2013-05-15 13:52:47 +00:00
|
|
|
void ArticleView::copyAsText()
|
|
|
|
{
|
|
|
|
QString text = ui.definition->selectedText();
|
|
|
|
if( !text.isEmpty() )
|
|
|
|
QApplication::clipboard()->setText( text );
|
|
|
|
}
|
|
|
|
|
2013-05-30 02:18:28 +00:00
|
|
|
void ArticleView::inspect()
|
|
|
|
{
|
2021-07-06 13:01:50 +00:00
|
|
|
ui.definition->triggerPageAction( QWebEnginePage::InspectElement );
|
2013-05-30 02:18:28 +00:00
|
|
|
}
|
|
|
|
|
2014-04-22 13:47:02 +00:00
|
|
|
void ArticleView::highlightFTSResults()
|
|
|
|
{
|
|
|
|
closeSearch();
|
|
|
|
|
2014-04-22 18:45:42 +00:00
|
|
|
const QUrl & url = ui.definition->url();
|
2018-04-10 14:49:52 +00:00
|
|
|
|
2021-11-27 07:17:33 +00:00
|
|
|
bool ignoreDiacritics = Utils::Url::hasQueryItem( url, "ignore_diacritics" );
|
2018-04-10 14:49:52 +00:00
|
|
|
|
2021-11-27 07:17:33 +00:00
|
|
|
QString regString = Utils::Url::queryItemValue( url, "regexp" );
|
2018-04-10 14:49:52 +00:00
|
|
|
if( ignoreDiacritics )
|
|
|
|
regString = gd::toQString( Folding::applyDiacriticsOnly( gd::toWString( regString ) ) );
|
|
|
|
else
|
|
|
|
regString = regString.remove( AccentMarkHandler::accentMark() );
|
|
|
|
|
2018-09-25 14:58:51 +00:00
|
|
|
QRegularExpression regexp;
|
2021-11-27 07:17:33 +00:00
|
|
|
if( Utils::Url::hasQueryItem( url, "wildcards" ) )
|
2018-09-25 14:58:51 +00:00
|
|
|
regexp.setPattern( wildcardsToRegexp( regString ) );
|
|
|
|
else
|
|
|
|
regexp.setPattern( regString );
|
|
|
|
|
|
|
|
QRegularExpression::PatternOptions patternOptions = QRegularExpression::DotMatchesEverythingOption
|
|
|
|
| QRegularExpression::UseUnicodePropertiesOption
|
|
|
|
| QRegularExpression::MultilineOption
|
|
|
|
| QRegularExpression::InvertedGreedinessOption;
|
2021-11-27 07:17:33 +00:00
|
|
|
if( !Utils::Url::hasQueryItem( url, "matchcase" ) )
|
2018-09-25 14:58:51 +00:00
|
|
|
patternOptions |= QRegularExpression::CaseInsensitiveOption;
|
|
|
|
regexp.setPatternOptions( patternOptions );
|
|
|
|
|
|
|
|
if( regexp.pattern().isEmpty() || !regexp.isValid() )
|
|
|
|
return;
|
2014-04-22 18:45:42 +00:00
|
|
|
|
2018-04-10 14:49:52 +00:00
|
|
|
|
|
|
|
sptr< AccentMarkHandler > marksHandler = ignoreDiacritics ?
|
|
|
|
new DiacriticsHandler : new AccentMarkHandler;
|
|
|
|
|
2014-04-22 13:47:02 +00:00
|
|
|
// Clear any current selection
|
|
|
|
if ( ui.definition->selectedText().size() )
|
|
|
|
{
|
2021-07-06 13:01:50 +00:00
|
|
|
ui.definition->page()->
|
|
|
|
runJavaScript( "window.getSelection().removeAllRanges();_=0;" );
|
2014-04-22 13:47:02 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 13:01:50 +00:00
|
|
|
QString pageText = getWebPageTextSync(ui.definition->page());
|
2018-04-10 14:49:52 +00:00
|
|
|
marksHandler->setText( pageText );
|
2014-04-24 14:12:00 +00:00
|
|
|
|
2018-09-25 14:58:51 +00:00
|
|
|
QRegularExpressionMatchIterator it = regexp.globalMatch( marksHandler->normalizedText() );
|
|
|
|
while( it.hasNext() )
|
|
|
|
{
|
|
|
|
QRegularExpressionMatch match = it.next();
|
|
|
|
|
|
|
|
// Mirror pos and matched length to original string
|
|
|
|
int pos = match.capturedStart();
|
|
|
|
int spos = marksHandler->mirrorPosition( pos );
|
|
|
|
int matched = marksHandler->mirrorPosition( pos + match.capturedLength() ) - spos;
|
|
|
|
|
|
|
|
// Add mark pos (if presented)
|
|
|
|
while( spos + matched < pageText.length()
|
|
|
|
&& pageText[ spos + matched ].category() == QChar::Mark_NonSpacing )
|
|
|
|
matched++;
|
|
|
|
|
|
|
|
if( matched > FTS::MaxMatchLengthForHighlightResults )
|
|
|
|
{
|
|
|
|
gdWarning( "ArticleView::highlightFTSResults(): Too long match - skipped (matched length %i, allowed %i)",
|
|
|
|
match.capturedLength(), FTS::MaxMatchLengthForHighlightResults );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
allMatches.append( pageText.mid( spos, matched ) );
|
|
|
|
}
|
2014-04-22 13:47:02 +00:00
|
|
|
|
2021-11-27 07:17:33 +00:00
|
|
|
ftsSearchMatchCase = Utils::Url::hasQueryItem( url, "matchcase" );
|
2014-04-22 13:47:02 +00:00
|
|
|
|
2021-07-06 13:01:50 +00:00
|
|
|
QWebEnginePage::FindFlags flags ( 0 );
|
2014-04-22 13:47:02 +00:00
|
|
|
|
|
|
|
if( ftsSearchMatchCase )
|
2021-07-06 13:01:50 +00:00
|
|
|
flags |= QWebEnginePage::FindCaseSensitively;
|
2014-04-22 13:47:02 +00:00
|
|
|
|
|
|
|
for( int x = 0; x < allMatches.size(); x++ )
|
|
|
|
ui.definition->findText( allMatches.at( x ), flags );
|
|
|
|
|
|
|
|
if( !allMatches.isEmpty() )
|
|
|
|
{
|
2021-07-06 13:01:50 +00:00
|
|
|
ui.definition->findText( allMatches.at( 0 ), flags );
|
|
|
|
//if( ui.definition->findText( allMatches.at( 0 ), flags ) )
|
2014-04-22 13:47:02 +00:00
|
|
|
{
|
2021-07-06 13:01:50 +00:00
|
|
|
ui.definition->page()->
|
|
|
|
runJavaScript( QString( "%1=window.getSelection().getRangeAt(0);_=0;" )
|
2014-04-22 13:47:02 +00:00
|
|
|
.arg( rangeVarName ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.ftsSearchFrame->show();
|
|
|
|
ui.ftsSearchPrevious->setEnabled( false );
|
2017-04-26 15:40:13 +00:00
|
|
|
ui.ftsSearchNext->setEnabled( allMatches.size()>1 );
|
2014-04-22 13:47:02 +00:00
|
|
|
|
|
|
|
ftsSearchIsOpened = true;
|
|
|
|
}
|
|
|
|
|
2021-07-06 13:01:50 +00:00
|
|
|
QString ArticleView::getWebPageTextSync(QWebEnginePage * page){
|
|
|
|
QString planText;
|
2021-12-02 05:40:28 +00:00
|
|
|
QSharedPointer<QEventLoop> loop = QSharedPointer<QEventLoop>(new QEventLoop());
|
|
|
|
QTimer::singleShot(1000, loop.data(), &QEventLoop::quit);
|
|
|
|
page->toPlainText([&](const QString &result)
|
|
|
|
{
|
|
|
|
if(loop->isRunning()){
|
|
|
|
planText = result;
|
|
|
|
loop->quit();
|
|
|
|
} });
|
|
|
|
loop->exec();
|
2021-07-06 13:01:50 +00:00
|
|
|
return planText;
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:45:16 +00:00
|
|
|
//todo ,futher refinement?
|
2014-04-22 13:47:02 +00:00
|
|
|
void ArticleView::performFtsFindOperation( bool backwards )
|
|
|
|
{
|
|
|
|
if( !ftsSearchIsOpened )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if( allMatches.isEmpty() )
|
|
|
|
{
|
|
|
|
ui.ftsSearchNext->setEnabled( false );
|
|
|
|
ui.ftsSearchPrevious->setEnabled( false );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-06 13:01:50 +00:00
|
|
|
QWebEnginePage::FindFlags flags( 0 );
|
2014-04-22 13:47:02 +00:00
|
|
|
|
|
|
|
if( ftsSearchMatchCase )
|
2021-07-06 13:01:50 +00:00
|
|
|
flags |= QWebEnginePage::FindCaseSensitively;
|
2014-04-22 13:47:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Restore saved highlighted selection
|
2021-07-06 13:01:50 +00:00
|
|
|
ui.definition->page()->
|
|
|
|
runJavaScript( QString( "var sel=window.getSelection();sel.removeAllRanges();sel.addRange(%1);_=0;" )
|
2014-04-22 13:47:02 +00:00
|
|
|
.arg( rangeVarName ) );
|
|
|
|
|
2021-12-12 05:35:32 +00:00
|
|
|
if (backwards) {
|
|
|
|
if (ftsPosition > 0) {
|
|
|
|
ftsPosition -= 1;
|
|
|
|
}
|
2014-04-22 13:47:02 +00:00
|
|
|
|
2021-12-12 05:35:32 +00:00
|
|
|
ui.definition->findText(allMatches.at(ftsPosition),
|
|
|
|
flags | QWebEnginePage::FindBackward,
|
|
|
|
[this](bool res) {
|
|
|
|
ui.ftsSearchPrevious->setEnabled(res);
|
|
|
|
if (!ui.ftsSearchNext->isEnabled())
|
|
|
|
ui.ftsSearchNext->setEnabled(res);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
if (ftsPosition < allMatches.size() - 1) {
|
|
|
|
ftsPosition += 1;
|
|
|
|
}
|
2014-04-22 13:47:02 +00:00
|
|
|
|
2021-12-12 05:35:32 +00:00
|
|
|
ui.definition->findText(allMatches.at(ftsPosition), flags, [this](bool res) {
|
|
|
|
ui.ftsSearchNext->setEnabled(res);
|
|
|
|
if (!ui.ftsSearchPrevious->isEnabled())
|
|
|
|
ui.ftsSearchPrevious->setEnabled(res);
|
|
|
|
});
|
2014-04-22 13:47:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store new highlighted selection
|
2021-07-06 13:01:50 +00:00
|
|
|
ui.definition->page()->
|
|
|
|
runJavaScript( QString( "%1=window.getSelection().getRangeAt(0);_=0;" )
|
2014-04-22 13:47:02 +00:00
|
|
|
.arg( rangeVarName ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleView::on_ftsSearchPrevious_clicked()
|
|
|
|
{
|
|
|
|
performFtsFindOperation( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleView::on_ftsSearchNext_clicked()
|
|
|
|
{
|
|
|
|
performFtsFindOperation( false );
|
|
|
|
}
|
|
|
|
|
2012-09-18 23:01:31 +00:00
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
|
|
|
|
void ArticleView::readTag( const QString & from, QString & to, int & count )
|
|
|
|
{
|
|
|
|
QChar ch, prev_ch;
|
|
|
|
bool inQuote = false, inDoublequote = false;
|
|
|
|
|
|
|
|
to.append( ch = prev_ch = from[ count++ ] );
|
|
|
|
while( count < from.size() )
|
|
|
|
{
|
|
|
|
ch = from[ count ];
|
|
|
|
if( ch == '>' && !( inQuote || inDoublequote ) )
|
|
|
|
{
|
|
|
|
to.append( ch );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if( ch == '\'' )
|
|
|
|
inQuote = !inQuote;
|
|
|
|
if( ch == '\"' )
|
|
|
|
inDoublequote = !inDoublequote;
|
|
|
|
to.append( prev_ch = ch );
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ArticleView::insertSpans( QString const & html )
|
|
|
|
{
|
|
|
|
QChar ch;
|
|
|
|
QString newContent;
|
|
|
|
bool inSpan = false, escaped = false;
|
|
|
|
|
|
|
|
/// Enclose every word in string (exclude tags) with <span></span>
|
|
|
|
|
|
|
|
for( int i = 0; i < html.size(); i++ )
|
|
|
|
{
|
|
|
|
ch = html[ i ];
|
|
|
|
if( ch == '&' )
|
|
|
|
{
|
|
|
|
escaped = true;
|
|
|
|
if( inSpan )
|
|
|
|
{
|
|
|
|
newContent.append( "</span>" );
|
|
|
|
inSpan = false;
|
|
|
|
}
|
|
|
|
newContent.append( ch );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ch == '<' ) // Skip tag
|
|
|
|
{
|
|
|
|
escaped = false;
|
|
|
|
if( inSpan )
|
|
|
|
{
|
|
|
|
newContent.append( "</span>" );
|
|
|
|
inSpan = false;
|
|
|
|
}
|
|
|
|
readTag( html, newContent, i );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( escaped )
|
|
|
|
{
|
|
|
|
if( ch == ';' )
|
|
|
|
escaped = false;
|
|
|
|
newContent.append( ch );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !inSpan && ( ch.isLetterOrNumber() || ch.isLowSurrogate() ) )
|
|
|
|
{
|
|
|
|
newContent.append( "<span>");
|
|
|
|
inSpan = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( inSpan && !( ch.isLetterOrNumber() || ch.isLowSurrogate() ) )
|
|
|
|
{
|
|
|
|
newContent.append( "</span>");
|
|
|
|
inSpan = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ch.isLowSurrogate() )
|
|
|
|
{
|
|
|
|
newContent.append( ch );
|
|
|
|
ch = html[ ++i ];
|
|
|
|
}
|
|
|
|
|
|
|
|
newContent.append( ch );
|
2012-09-27 13:54:09 +00:00
|
|
|
if( ch == '-' && !( html[ i + 1 ] == ' ' || ( i > 0 && html[ i - 1 ] == ' ' ) ) )
|
|
|
|
newContent.append( "<span style=\"font-size:0pt\"> </span>" );
|
2012-09-18 23:01:31 +00:00
|
|
|
}
|
|
|
|
if( inSpan )
|
|
|
|
newContent.append( "</span>" );
|
|
|
|
return newContent;
|
|
|
|
}
|
|
|
|
|
2021-11-30 02:53:18 +00:00
|
|
|
QString ArticleView::checkElement( QWebEnginePage & page, QPoint const & pt )
|
2012-09-18 23:01:31 +00:00
|
|
|
{
|
2021-12-01 14:26:33 +00:00
|
|
|
return runJavaScriptSync(&page, QString(
|
2021-11-30 02:53:18 +00:00
|
|
|
" var a= document.elementFromPoint(%1,%2);"
|
|
|
|
"var nodename=a.nodeName.toLowerCase();"
|
|
|
|
"if(nodename==\"body\"||nodename==\"html\"||nodename==\"head\")"
|
|
|
|
"{"
|
|
|
|
" return '';"
|
|
|
|
"}"
|
|
|
|
"return a.textContent;")
|
|
|
|
.arg(pt.x())
|
|
|
|
.arg(pt.y()));
|
2012-09-18 23:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString ArticleView::wordAtPoint( int x, int y )
|
|
|
|
{
|
|
|
|
QString word;
|
|
|
|
|
|
|
|
if( popupView )
|
|
|
|
return word;
|
|
|
|
|
|
|
|
QPoint pos = mapFromGlobal( QPoint( x, y ) );
|
2021-07-06 13:01:50 +00:00
|
|
|
//todo
|
|
|
|
QWebEnginePage *frame = ui.definition->page();
|
2012-09-18 23:01:31 +00:00
|
|
|
if( !frame )
|
|
|
|
return word;
|
|
|
|
|
2021-07-06 13:01:50 +00:00
|
|
|
QPointF scrollPoint=frame->scrollPosition();
|
2012-09-18 23:01:31 +00:00
|
|
|
|
2021-07-06 13:01:50 +00:00
|
|
|
QPoint posWithScroll = pos + QPoint((int)scrollPoint.x(),(int)scrollPoint.y());
|
2012-09-18 23:01:31 +00:00
|
|
|
|
2021-07-06 13:01:50 +00:00
|
|
|
/// Find target HTML element
|
2021-12-13 14:45:16 +00:00
|
|
|
QString nodeValue = runJavaScriptSync(frame, QString(
|
|
|
|
"var a= document.elementFromPoint(%1,%2);"
|
|
|
|
"var nodename=a.nodeName.toLowerCase();"
|
|
|
|
"if(nodename==\"body\"||nodename==\"html\"||nodename==\"head\")"
|
|
|
|
"{"
|
|
|
|
" return '';"
|
|
|
|
"}"
|
|
|
|
"return a.textContent;")
|
|
|
|
.arg(posWithScroll.x())
|
|
|
|
.arg(posWithScroll.y()));
|
|
|
|
return nodeValue;
|
2012-09-18 23:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2013-05-27 11:18:13 +00:00
|
|
|
|
2017-04-27 20:55:53 +00:00
|
|
|
ResourceToSaveHandler::ResourceToSaveHandler(ArticleView * view, QString const & fileName ) :
|
2013-05-27 11:18:13 +00:00
|
|
|
QObject( view ),
|
2017-04-27 15:04:26 +00:00
|
|
|
fileName( fileName ),
|
2017-04-27 20:55:53 +00:00
|
|
|
alreadyDone( false )
|
2013-05-27 11:18:13 +00:00
|
|
|
{
|
|
|
|
connect( this, SIGNAL( statusBarMessage( QString, int, QPixmap ) ),
|
|
|
|
view, SIGNAL( statusBarMessage( QString, int, QPixmap ) ) );
|
2017-04-27 20:55:53 +00:00
|
|
|
}
|
2013-05-27 11:18:13 +00:00
|
|
|
|
2017-04-27 20:55:53 +00:00
|
|
|
void ResourceToSaveHandler::addRequest( sptr<Dictionary::DataRequest> req )
|
|
|
|
{
|
|
|
|
if( !alreadyDone )
|
2013-05-27 11:18:13 +00:00
|
|
|
{
|
2017-04-27 20:55:53 +00:00
|
|
|
downloadRequests.push_back( req );
|
|
|
|
|
|
|
|
connect( req.get(), SIGNAL( finished() ),
|
|
|
|
this, SLOT( downloadFinished() ) );
|
2013-05-27 11:18:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceToSaveHandler::downloadFinished()
|
|
|
|
{
|
2017-04-27 20:55:53 +00:00
|
|
|
if ( downloadRequests.empty() )
|
|
|
|
return; // Stray signal
|
2013-05-27 11:18:13 +00:00
|
|
|
|
2017-04-27 20:55:53 +00:00
|
|
|
// Find any finished resources
|
|
|
|
for( list< sptr< Dictionary::DataRequest > >::iterator i =
|
|
|
|
downloadRequests.begin(); i != downloadRequests.end(); )
|
2013-05-27 11:18:13 +00:00
|
|
|
{
|
2017-04-27 20:55:53 +00:00
|
|
|
if ( (*i)->isFinished() )
|
|
|
|
{
|
|
|
|
if ( (*i)->dataSize() >= 0 && !alreadyDone )
|
|
|
|
{
|
|
|
|
QByteArray resourceData;
|
|
|
|
vector< char > const & data = (*i)->getFullData();
|
|
|
|
resourceData = QByteArray( data.data(), data.size() );
|
2013-05-27 11:18:13 +00:00
|
|
|
|
2017-04-27 20:55:53 +00:00
|
|
|
// Write data to file
|
2013-05-27 11:18:13 +00:00
|
|
|
|
2017-04-27 20:55:53 +00:00
|
|
|
if ( !fileName.isEmpty() )
|
|
|
|
{
|
|
|
|
QFileInfo fileInfo( fileName );
|
|
|
|
QDir().mkpath( fileInfo.absoluteDir().absolutePath() );
|
2013-05-27 11:18:13 +00:00
|
|
|
|
2017-04-27 20:55:53 +00:00
|
|
|
QFile file( fileName );
|
|
|
|
if ( file.open( QFile::WriteOnly ) )
|
|
|
|
{
|
|
|
|
file.write( resourceData.data(), resourceData.size() );
|
|
|
|
file.close();
|
|
|
|
}
|
2013-05-27 11:18:13 +00:00
|
|
|
|
2017-04-27 20:55:53 +00:00
|
|
|
if ( file.error() )
|
|
|
|
{
|
|
|
|
emit statusBarMessage(
|
|
|
|
tr( "ERROR: %1" ).arg( tr( "Resource saving error: " ) + file.errorString() ),
|
|
|
|
10000, QPixmap( ":/icons/error.png" ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
alreadyDone = true;
|
|
|
|
|
|
|
|
// Clear other requests
|
|
|
|
|
|
|
|
downloadRequests.clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// This one had no data. Erase it.
|
|
|
|
downloadRequests.erase( i++ );
|
|
|
|
}
|
2013-05-27 11:18:13 +00:00
|
|
|
}
|
2017-04-27 20:55:53 +00:00
|
|
|
else // Unfinished, wait.
|
|
|
|
break;
|
2013-05-27 11:18:13 +00:00
|
|
|
}
|
2017-04-27 20:55:53 +00:00
|
|
|
|
|
|
|
if ( downloadRequests.empty() )
|
2013-05-27 11:18:13 +00:00
|
|
|
{
|
2017-04-27 20:55:53 +00:00
|
|
|
if( !alreadyDone )
|
2017-04-27 15:04:26 +00:00
|
|
|
{
|
|
|
|
emit statusBarMessage(
|
2017-04-27 20:55:53 +00:00
|
|
|
tr( "WARNING: %1" ).arg( tr( "The referenced resource failed to download." ) ),
|
2017-04-27 15:04:26 +00:00
|
|
|
10000, QPixmap( ":/icons/error.png" ) );
|
|
|
|
}
|
2017-04-27 20:55:53 +00:00
|
|
|
emit done();
|
|
|
|
deleteLater();
|
2013-05-27 11:18:13 +00:00
|
|
|
}
|
|
|
|
}
|