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 "article_maker.hh"
|
|
|
|
#include "config.hh"
|
|
|
|
#include "htmlescape.hh"
|
|
|
|
#include "utf8.hh"
|
2009-04-18 17:20:12 +00:00
|
|
|
#include "wstring_qt.hh"
|
2009-04-10 12:48:40 +00:00
|
|
|
#include <limits.h>
|
2009-01-28 20:55:45 +00:00
|
|
|
#include <QFile>
|
2009-11-11 10:37:23 +00:00
|
|
|
#include <QUrl>
|
2010-03-30 13:41:14 +00:00
|
|
|
#include "folding.hh"
|
2010-04-03 09:43:39 +00:00
|
|
|
#include "langcoder.hh"
|
2011-06-19 18:50:11 +00:00
|
|
|
#include "dprintf.hh"
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
using std::vector;
|
|
|
|
using std::string;
|
2009-04-18 17:20:12 +00:00
|
|
|
using gd::wstring;
|
2009-01-28 20:55:45 +00:00
|
|
|
using std::set;
|
2009-03-26 19:00:08 +00:00
|
|
|
using std::list;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
ArticleMaker::ArticleMaker( vector< sptr< Dictionary::Class > > const & dictionaries_,
|
2009-05-11 11:03:36 +00:00
|
|
|
vector< Instances::Group > const & groups_,
|
|
|
|
QString const & displayStyle_ ):
|
2009-01-28 20:55:45 +00:00
|
|
|
dictionaries( dictionaries_ ),
|
2009-05-11 11:03:36 +00:00
|
|
|
groups( groups_ ),
|
|
|
|
displayStyle( displayStyle_ )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-05-11 11:03:36 +00:00
|
|
|
void ArticleMaker::setDisplayStyle( QString const & st )
|
|
|
|
{
|
|
|
|
displayStyle = st;
|
|
|
|
}
|
|
|
|
|
2009-02-01 00:08:08 +00:00
|
|
|
std::string ArticleMaker::makeHtmlHeader( QString const & word,
|
2009-05-11 11:03:36 +00:00
|
|
|
QString const & icon ) const
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
string result =
|
2009-03-26 19:00:08 +00:00
|
|
|
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"
|
2009-01-28 20:55:45 +00:00
|
|
|
"<html><head>"
|
|
|
|
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">";
|
|
|
|
|
2009-02-08 17:21:46 +00:00
|
|
|
// Add a css stylesheet
|
2009-05-01 12:20:33 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
QFile builtInCssFile( ":/article-style.css" );
|
|
|
|
builtInCssFile.open( QFile::ReadOnly );
|
|
|
|
QByteArray css = builtInCssFile.readAll();
|
|
|
|
|
2009-05-11 11:03:36 +00:00
|
|
|
if ( displayStyle.size() )
|
|
|
|
{
|
|
|
|
// Load an additional stylesheet
|
|
|
|
QFile builtInCssFile( QString( ":/article-style-st-%1.css" ).arg( displayStyle ) );
|
|
|
|
builtInCssFile.open( QFile::ReadOnly );
|
|
|
|
css += builtInCssFile.readAll();
|
|
|
|
}
|
|
|
|
|
2009-05-01 12:20:33 +00:00
|
|
|
QFile cssFile( Config::getUserCssFileName() );
|
2009-02-08 17:21:46 +00:00
|
|
|
|
2009-05-01 12:20:33 +00:00
|
|
|
if ( cssFile.open( QFile::ReadOnly ) )
|
|
|
|
css += cssFile.readAll();
|
|
|
|
|
|
|
|
result += "<style type=\"text/css\" media=\"all\">\n";
|
|
|
|
result += css.data();
|
|
|
|
result += "</style>\n";
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-05-01 12:20:33 +00:00
|
|
|
// Add print-only css
|
2009-02-08 17:21:46 +00:00
|
|
|
|
2009-05-01 12:20:33 +00:00
|
|
|
{
|
|
|
|
QFile builtInCssFile( ":/article-style-print.css" );
|
|
|
|
builtInCssFile.open( QFile::ReadOnly );
|
|
|
|
QByteArray css = builtInCssFile.readAll();
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-05-01 12:20:33 +00:00
|
|
|
QFile cssFile( Config::getUserCssPrintFileName() );
|
|
|
|
|
|
|
|
if ( cssFile.open( QFile::ReadOnly ) )
|
|
|
|
css += cssFile.readAll();
|
|
|
|
|
|
|
|
result += "<style type=\"text/css\" media=\"print\">\n";
|
|
|
|
result += css.data();
|
|
|
|
result += "</style>\n";
|
|
|
|
}
|
|
|
|
|
2009-04-18 17:20:12 +00:00
|
|
|
result += "<title>" + Html::escape( Utf8::encode( gd::toWString( word ) ) ) + "</title>";
|
2009-02-01 00:08:08 +00:00
|
|
|
|
|
|
|
// This doesn't seem to be much of influence right now, but we'll keep
|
|
|
|
// it anyway.
|
|
|
|
if ( icon.size() )
|
|
|
|
result += "<link rel=\"icon\" type=\"image/png\" href=\"qrcx://localhost/flags/" + Html::escape( icon.toUtf8().data() ) + "\" />\n";
|
|
|
|
|
2009-05-11 22:25:22 +00:00
|
|
|
result += "<script language=\"JavaScript\">"
|
|
|
|
"function gdMakeArticleActive( newId ) {"
|
2009-05-14 20:38:17 +00:00
|
|
|
"if ( gdCurrentArticle != 'gdfrom-' + newId ) {"
|
2009-05-11 22:25:22 +00:00
|
|
|
"document.getElementById( gdCurrentArticle ).className = 'gdarticle';"
|
2009-05-14 20:38:17 +00:00
|
|
|
"document.getElementById( 'gdfrom-' + newId ).className = 'gdarticle gdactivearticle';"
|
2011-07-03 12:27:08 +00:00
|
|
|
"gdCurrentArticle = 'gdfrom-' + newId; articleview.onJsActiveArticleChanged(gdCurrentArticle);"
|
|
|
|
"eval( 'gdActivateAudioLink_' + newId + '();' ); } }"
|
2011-07-30 20:28:24 +00:00
|
|
|
"var overIframeId = null;"
|
|
|
|
"function processIframeMouseOut() { overIframeId = null; top.focus(); }"
|
|
|
|
"function processIframeMouseOver( newId ) { overIframeId = newId; }"
|
|
|
|
"function processIframeClick() { if( overIframeId != null ) { overIframeId = overIframeId.replace( 'gdexpandframe-', '' ); gdMakeArticleActive( overIframeId ) } }"
|
2011-09-03 14:45:43 +00:00
|
|
|
"function init() { window.addEventListener('blur', processIframeClick, false); }"
|
2011-07-30 20:28:24 +00:00
|
|
|
"window.addEventListener('load', init, false);"
|
2009-05-11 22:25:22 +00:00
|
|
|
"</script>";
|
|
|
|
|
2009-02-01 00:08:08 +00:00
|
|
|
result += "</head><body>";
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-05-11 11:03:36 +00:00
|
|
|
std::string ArticleMaker::makeNotFoundBody( QString const & word,
|
|
|
|
QString const & group )
|
2009-02-01 00:08:08 +00:00
|
|
|
{
|
2010-03-30 13:41:14 +00:00
|
|
|
string result( "<div class=\"gdnotfound\"><p>" );
|
2009-02-01 00:08:08 +00:00
|
|
|
|
2010-03-30 13:41:14 +00:00
|
|
|
if ( word.size() )
|
|
|
|
result += tr( "No translation for <b>%1</b> was found in group <b>%2</b>." ).
|
|
|
|
arg( QString::fromUtf8( Html::escape( word.toUtf8().data() ).c_str() ) ).
|
|
|
|
arg( QString::fromUtf8( Html::escape( group.toUtf8().data() ).c_str() ) ).
|
|
|
|
toUtf8().data();
|
|
|
|
else
|
|
|
|
result += tr( "No translation was found in group <b>%1</b>." ).
|
|
|
|
arg( QString::fromUtf8( Html::escape( group.toUtf8().data() ).c_str() ) ).
|
|
|
|
toUtf8().data();
|
|
|
|
|
|
|
|
result += "</p></div>";
|
|
|
|
|
|
|
|
return result;
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sptr< Dictionary::DataRequest > ArticleMaker::makeDefinitionFor(
|
2009-05-29 19:48:50 +00:00
|
|
|
QString const & inWord, unsigned groupId,
|
2009-09-23 18:44:38 +00:00
|
|
|
QMap< QString, QString > const & contexts,
|
|
|
|
QSet< QString > const & mutedDicts ) const
|
2009-03-26 19:00:08 +00:00
|
|
|
{
|
2010-05-08 14:01:59 +00:00
|
|
|
if ( groupId == Instances::Group::HelpGroupId )
|
2009-02-08 18:35:29 +00:00
|
|
|
{
|
|
|
|
// This is a special group containing internal welcome/help pages
|
|
|
|
string result = makeHtmlHeader( inWord, QString() );
|
|
|
|
|
2009-04-12 19:41:58 +00:00
|
|
|
if ( inWord == tr( "Welcome!" ) )
|
2009-02-08 18:35:29 +00:00
|
|
|
{
|
|
|
|
result += tr(
|
|
|
|
"<h3 align=\"center\">Welcome to <b>GoldenDict</b>!</h3>"
|
2009-05-24 17:38:38 +00:00
|
|
|
"<p>To start working with the program, first visit <b>Edit|Dictionaries</b> to add some directory paths where to search "
|
|
|
|
"for the dictionary files, set up various Wikipedia sites or other sources, adjust dictionary order or create dictionary groups."
|
2009-02-08 21:32:33 +00:00
|
|
|
"<p>And then you're ready to look up your words! You can do that in this window "
|
2009-02-08 18:35:29 +00:00
|
|
|
"by using a pane to the left, or you can <a href=\"Working with popup\">look up words from other active applications</a>. "
|
2009-05-24 17:38:38 +00:00
|
|
|
"<p>To customize program, check out the available preferences at <b>Edit|Preferences</b>. "
|
|
|
|
"All settings there have tooltips, be sure to read them if you are in doubt about anything."
|
2009-02-08 18:35:29 +00:00
|
|
|
"<p>Should you need further help, have any questions, "
|
2010-11-06 19:40:07 +00:00
|
|
|
"suggestions or just wonder what the others think, you are welcome at the program's <a href=\"http://goldendict.org/forum/\">forum</a>."
|
|
|
|
"<p>Check program's <a href=\"http://goldendict.org/\">website</a> for the updates. "
|
2012-02-20 21:47:14 +00:00
|
|
|
"<p>(c) 2008-2012 Konstantin Isakov. Licensed under GPLv3 or later."
|
2009-02-08 18:35:29 +00:00
|
|
|
|
|
|
|
).toUtf8().data();
|
|
|
|
}
|
|
|
|
else
|
2009-04-12 19:41:58 +00:00
|
|
|
if ( inWord == tr( "Working with popup" ) )
|
2009-02-08 18:35:29 +00:00
|
|
|
{
|
|
|
|
result += ( tr( "<h3 align=\"center\">Working with the popup</h3>"
|
|
|
|
|
|
|
|
"To look up words from other active applications, you would need to first activate the <i>\"Scan popup functionality\"</i> in <b>Preferences</b>, "
|
|
|
|
"and then enable it at any time either by triggering the 'Popup' icon above, or "
|
|
|
|
"by clicking the tray icon down below with your right mouse button and choosing so in the menu you've popped. " ) +
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
tr( "Then just stop the cursor over the word you want to look up in another application, "
|
|
|
|
"and a window would pop up which would describe it to you." )
|
|
|
|
#else
|
|
|
|
tr( "Then just select any word you want to look up in another application by your mouse "
|
|
|
|
"(double-click it or swipe it with mouse with the button pressed), "
|
|
|
|
"and a window would pop up which would describe the word to you." )
|
|
|
|
#endif
|
|
|
|
).toUtf8().data();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Not found
|
2009-04-10 12:48:40 +00:00
|
|
|
return makeNotFoundTextFor( inWord, "help" );
|
2009-02-08 18:35:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result += "</body></html>";
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
sptr< Dictionary::DataRequestInstant > r = new Dictionary::DataRequestInstant( true );
|
|
|
|
|
|
|
|
r->getData().resize( result.size() );
|
|
|
|
memcpy( &( r->getData().front() ), result.data(), result.size() );
|
|
|
|
|
|
|
|
return r;
|
2009-02-08 18:35:29 +00:00
|
|
|
}
|
2009-03-26 19:00:08 +00:00
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
// Find the given group
|
|
|
|
|
|
|
|
Instances::Group const * activeGroup = 0;
|
|
|
|
|
|
|
|
for( unsigned x = 0; x < groups.size(); ++x )
|
2009-04-10 12:48:40 +00:00
|
|
|
if ( groups[ x ].id == groupId )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
activeGroup = &groups[ x ];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we've found a group, use its dictionaries; otherwise, use the global
|
|
|
|
// heap.
|
|
|
|
std::vector< sptr< Dictionary::Class > > const & activeDicts =
|
|
|
|
activeGroup ? activeGroup->dictionaries : dictionaries;
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
string header = makeHtmlHeader( inWord.trimmed(),
|
2009-02-01 00:08:08 +00:00
|
|
|
activeGroup && activeGroup->icon.size() ?
|
|
|
|
activeGroup->icon : QString() );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-09-23 18:44:38 +00:00
|
|
|
if ( mutedDicts.size() )
|
|
|
|
{
|
|
|
|
std::vector< sptr< Dictionary::Class > > unmutedDicts;
|
|
|
|
|
|
|
|
unmutedDicts.reserve( activeDicts.size() );
|
|
|
|
|
|
|
|
for( unsigned x = 0; x < activeDicts.size(); ++x )
|
|
|
|
if ( !mutedDicts.contains(
|
|
|
|
QString::fromStdString( activeDicts[ x ]->getId() ) ) )
|
|
|
|
unmutedDicts.push_back( activeDicts[ x ] );
|
|
|
|
|
|
|
|
return new ArticleRequest( inWord.trimmed(), activeGroup ? activeGroup->name : "",
|
|
|
|
contexts, unmutedDicts, header );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return new ArticleRequest( inWord.trimmed(), activeGroup ? activeGroup->name : "",
|
|
|
|
contexts, activeDicts, header );
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sptr< Dictionary::DataRequest > ArticleMaker::makeNotFoundTextFor(
|
|
|
|
QString const & word, QString const & group ) const
|
|
|
|
{
|
|
|
|
string result = makeHtmlHeader( word, QString() ) + makeNotFoundBody( word, group ) +
|
|
|
|
"</body></html>";
|
|
|
|
|
|
|
|
sptr< Dictionary::DataRequestInstant > r = new Dictionary::DataRequestInstant( true );
|
|
|
|
|
|
|
|
r->getData().resize( result.size() );
|
|
|
|
memcpy( &( r->getData().front() ), result.data(), result.size() );
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2009-05-11 15:33:57 +00:00
|
|
|
sptr< Dictionary::DataRequest > ArticleMaker::makeEmptyPage() const
|
|
|
|
{
|
|
|
|
string result = makeHtmlHeader( tr( "(untitled)" ), QString() ) +
|
|
|
|
"</body></html>";
|
|
|
|
|
|
|
|
sptr< Dictionary::DataRequestInstant > r =
|
|
|
|
new Dictionary::DataRequestInstant( true );
|
|
|
|
|
|
|
|
r->getData().resize( result.size() );
|
|
|
|
memcpy( &( r->getData().front() ), result.data(), result.size() );
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
//////// ArticleRequest
|
|
|
|
|
|
|
|
ArticleRequest::ArticleRequest(
|
|
|
|
QString const & word_, QString const & group_,
|
2009-05-29 19:48:50 +00:00
|
|
|
QMap< QString, QString > const & contexts_,
|
2009-03-26 19:00:08 +00:00
|
|
|
vector< sptr< Dictionary::Class > > const & activeDicts_,
|
|
|
|
string const & header ):
|
2009-05-29 19:48:50 +00:00
|
|
|
word( word_ ), group( group_ ), contexts( contexts_ ),
|
|
|
|
activeDicts( activeDicts_ ),
|
2009-04-12 16:22:42 +00:00
|
|
|
altsDone( false ), bodyDone( false ), foundAnyDefinitions( false ),
|
|
|
|
closePrevSpan( false )
|
2009-03-26 19:00:08 +00:00
|
|
|
{
|
|
|
|
// No need to lock dataMutex on construction
|
|
|
|
|
|
|
|
hasAnyData = true;
|
|
|
|
|
|
|
|
data.resize( header.size() );
|
|
|
|
memcpy( &data.front(), header.data(), header.size() );
|
2009-01-29 19:16:25 +00:00
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
// Accumulate main forms
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
for( unsigned x = 0; x < activeDicts.size(); ++x )
|
|
|
|
{
|
2009-04-18 17:20:12 +00:00
|
|
|
sptr< Dictionary::WordSearchRequest > s = activeDicts[ x ]->findHeadwordsForSynonym( gd::toWString( word ) );
|
2009-03-26 19:00:08 +00:00
|
|
|
|
|
|
|
connect( s.get(), SIGNAL( finished() ),
|
2011-12-12 16:52:07 +00:00
|
|
|
this, SLOT( altSearchFinished() ), Qt::QueuedConnection );
|
2009-03-26 19:00:08 +00:00
|
|
|
|
|
|
|
altSearches.push_back( s );
|
|
|
|
}
|
|
|
|
|
|
|
|
altSearchFinished(); // Handle any ones which have already finished
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
void ArticleRequest::altSearchFinished()
|
|
|
|
{
|
|
|
|
if ( altsDone )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Check every request for finishing
|
|
|
|
for( list< sptr< Dictionary::WordSearchRequest > >::iterator i =
|
|
|
|
altSearches.begin(); i != altSearches.end(); )
|
|
|
|
{
|
|
|
|
if ( (*i)->isFinished() )
|
|
|
|
{
|
|
|
|
// This one's finished
|
|
|
|
for( size_t count = (*i)->matchesCount(), x = 0; x < count; ++x )
|
|
|
|
alts.insert( (**i)[ x ].word );
|
|
|
|
|
|
|
|
altSearches.erase( i++ );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( altSearches.empty() )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2011-06-19 18:50:11 +00:00
|
|
|
DPRINTF( "alts finished\n" );
|
2009-03-26 19:00:08 +00:00
|
|
|
|
|
|
|
// They all've finished! Now we can look up bodies
|
|
|
|
|
|
|
|
altsDone = true; // So any pending signals in queued mode won't mess us up
|
|
|
|
|
|
|
|
vector< wstring > altsVector( alts.begin(), alts.end() );
|
|
|
|
|
|
|
|
for( unsigned x = 0; x < altsVector.size(); ++x )
|
|
|
|
{
|
2011-06-19 18:50:11 +00:00
|
|
|
DPRINTF( "Alt: %ls\n", altsVector[ x ].c_str() );
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|
|
|
|
|
2009-04-18 17:20:12 +00:00
|
|
|
wstring wordStd = gd::toWString( word );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
for( unsigned x = 0; x < activeDicts.size(); ++x )
|
|
|
|
{
|
2009-03-26 19:00:08 +00:00
|
|
|
sptr< Dictionary::DataRequest > r =
|
2009-05-29 19:48:50 +00:00
|
|
|
activeDicts[ x ]->getArticle( wordStd, altsVector,
|
|
|
|
gd::toWString( contexts.value( QString::fromStdString( activeDicts[ x ]->getId() ) ) ) );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
connect( r.get(), SIGNAL( finished() ),
|
2011-12-12 16:52:07 +00:00
|
|
|
this, SLOT( bodyFinished() ), Qt::QueuedConnection );
|
2009-03-26 19:00:08 +00:00
|
|
|
|
|
|
|
bodyRequests.push_back( r );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
bodyFinished(); // Handle any ones which have already finished
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
void ArticleRequest::bodyFinished()
|
|
|
|
{
|
|
|
|
if ( bodyDone )
|
|
|
|
return;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2011-06-19 18:50:11 +00:00
|
|
|
DPRINTF( "some body finished\n" );
|
2009-03-26 19:00:08 +00:00
|
|
|
|
|
|
|
bool wasUpdated = false;
|
|
|
|
|
|
|
|
while ( bodyRequests.size() )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-03-26 19:00:08 +00:00
|
|
|
// Since requests should go in order, check the first one first
|
|
|
|
if ( bodyRequests.front()->isFinished() )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-03-26 19:00:08 +00:00
|
|
|
// Good
|
|
|
|
|
2011-06-19 18:50:11 +00:00
|
|
|
DPRINTF( "one finished.\n" );
|
2009-03-26 19:00:08 +00:00
|
|
|
|
|
|
|
Dictionary::DataRequest & req = *bodyRequests.front();
|
|
|
|
|
|
|
|
QString errorString = req.getErrorString();
|
|
|
|
|
|
|
|
if ( req.dataSize() >= 0 || errorString.size() )
|
|
|
|
{
|
2010-04-03 09:43:39 +00:00
|
|
|
sptr< Dictionary::Class > const & activeDict =
|
|
|
|
activeDicts[ activeDicts.size() - bodyRequests.size() ];
|
|
|
|
|
|
|
|
string dictId = activeDict->getId();
|
2009-04-12 16:22:42 +00:00
|
|
|
|
|
|
|
string head;
|
|
|
|
|
2009-05-11 19:14:28 +00:00
|
|
|
string gdFrom = "gdfrom-" + Html::escape( dictId );
|
|
|
|
|
2009-04-12 16:22:42 +00:00
|
|
|
if ( closePrevSpan )
|
|
|
|
{
|
2010-04-03 09:43:39 +00:00
|
|
|
head += "</span></span><span class=\"gdarticleseparator\"></span>";
|
2009-04-12 16:22:42 +00:00
|
|
|
}
|
2009-05-11 19:14:28 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// This is the first article
|
|
|
|
head += "<script language=\"JavaScript\">"
|
2011-07-03 12:27:08 +00:00
|
|
|
"var gdCurrentArticle=\"" + gdFrom + "\"; "
|
|
|
|
"articleview.onJsActiveArticleChanged(gdCurrentArticle)</script>";
|
2009-05-11 19:14:28 +00:00
|
|
|
}
|
2009-04-12 16:22:42 +00:00
|
|
|
|
|
|
|
string jsVal = Html::escapeForJavaScript( dictId );
|
|
|
|
head += "<script language=\"JavaScript\">var gdArticleContents; "
|
|
|
|
"if ( !gdArticleContents ) gdArticleContents = \"" + jsVal +" \"; "
|
|
|
|
"else gdArticleContents += \"" + jsVal + " \";</script>";
|
2009-05-11 22:25:22 +00:00
|
|
|
|
|
|
|
head += string( "<span class=\"gdarticle" ) +
|
|
|
|
( closePrevSpan ? "" : " gdactivearticle" ) +
|
|
|
|
"\" id=\"" + gdFrom +
|
2009-05-14 20:38:17 +00:00
|
|
|
"\" onClick=\"gdMakeArticleActive( '" + jsVal + "' );\" " +
|
|
|
|
" onContextMenu=\"gdMakeArticleActive( '" + jsVal + "' );\""
|
2009-05-11 19:14:28 +00:00
|
|
|
+ ">";
|
2009-04-12 16:22:42 +00:00
|
|
|
|
|
|
|
closePrevSpan = true;
|
2010-04-03 09:43:39 +00:00
|
|
|
|
2009-05-11 09:32:18 +00:00
|
|
|
head += string( "<div class=\"gddictname\"><span class=\"gdfromprefix\">" ) +
|
|
|
|
Html::escape( tr( "From " ).toUtf8().data() ) + "</span>" +
|
2010-04-03 09:43:39 +00:00
|
|
|
Html::escape( activeDict->getName().c_str() )
|
2009-03-26 19:00:08 +00:00
|
|
|
+ "</div>";
|
|
|
|
|
2010-04-03 09:43:39 +00:00
|
|
|
head += "<span class=\"gdarticlebody gdlangfrom-";
|
|
|
|
head += LangCoder::intToCode2( activeDict->getLangFrom() ).toAscii().data();
|
|
|
|
head += "\" lang=\"";
|
|
|
|
head += LangCoder::intToCode2( activeDict->getLangTo() ).toAscii().data();
|
|
|
|
head += "\">";
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
if ( errorString.size() )
|
|
|
|
{
|
|
|
|
head += "<div class=\"gderrordesc\">" +
|
|
|
|
Html::escape( tr( "Query error: %1" ).arg( errorString ).toUtf8().data() )
|
|
|
|
+ "</div>";
|
|
|
|
}
|
|
|
|
|
|
|
|
Mutex::Lock _( dataMutex );
|
|
|
|
|
|
|
|
size_t offset = data.size();
|
|
|
|
|
|
|
|
data.resize( data.size() + head.size() + ( req.dataSize() > 0 ? req.dataSize() : 0 ) );
|
|
|
|
|
|
|
|
memcpy( &data.front() + offset, head.data(), head.size() );
|
|
|
|
|
|
|
|
if ( req.dataSize() > 0 )
|
|
|
|
bodyRequests.front()->getDataSlice( 0, req.dataSize(),
|
|
|
|
&data.front() + offset + head.size() );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
wasUpdated = true;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
foundAnyDefinitions = true;
|
|
|
|
}
|
2011-06-19 18:50:11 +00:00
|
|
|
DPRINTF( "erasing..\n" );
|
2009-03-26 19:00:08 +00:00
|
|
|
bodyRequests.pop_front();
|
2011-06-19 18:50:11 +00:00
|
|
|
DPRINTF( "erase done..\n" );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
2009-03-26 19:00:08 +00:00
|
|
|
else
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2011-06-19 18:50:11 +00:00
|
|
|
DPRINTF( "one not finished.\n" );
|
2009-03-26 19:00:08 +00:00
|
|
|
break;
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
if ( bodyRequests.empty() )
|
|
|
|
{
|
|
|
|
// No requests left, end the article
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
bodyDone = true;
|
|
|
|
|
|
|
|
{
|
|
|
|
string footer;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-12 16:22:42 +00:00
|
|
|
if ( closePrevSpan )
|
|
|
|
{
|
2010-04-03 09:43:39 +00:00
|
|
|
footer += "</span></span>";
|
2009-04-12 16:22:42 +00:00
|
|
|
closePrevSpan = false;
|
|
|
|
}
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
if ( !foundAnyDefinitions )
|
|
|
|
{
|
|
|
|
// No definitions were ever found, say so to the user.
|
2010-03-30 13:41:14 +00:00
|
|
|
|
|
|
|
// Larger words are usually whole sentences - don't clutter the ouput
|
|
|
|
// with their full bodies.
|
|
|
|
footer += ArticleMaker::makeNotFoundBody( word.size() < 40 ? word : "", group );
|
2009-03-26 19:00:08 +00:00
|
|
|
|
2009-04-17 13:51:50 +00:00
|
|
|
// When there were no definitions, we run stemmed search.
|
|
|
|
stemmedWordFinder = new WordFinder( this );
|
|
|
|
|
|
|
|
connect( stemmedWordFinder.get(), SIGNAL( finished() ),
|
|
|
|
this, SLOT( stemmedSearchFinished() ), Qt::QueuedConnection );
|
|
|
|
|
|
|
|
stemmedWordFinder->stemmedMatch( word, activeDicts );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
footer += "</body></html>";
|
|
|
|
}
|
2009-03-26 19:00:08 +00:00
|
|
|
|
|
|
|
Mutex::Lock _( dataMutex );
|
|
|
|
|
|
|
|
size_t offset = data.size();
|
|
|
|
|
|
|
|
data.resize( data.size() + footer.size() );
|
|
|
|
|
|
|
|
memcpy( &data.front() + offset, footer.data(), footer.size() );
|
|
|
|
}
|
|
|
|
|
2009-04-17 13:51:50 +00:00
|
|
|
if ( stemmedWordFinder.get() )
|
|
|
|
update();
|
|
|
|
else
|
|
|
|
finish();
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if ( wasUpdated )
|
|
|
|
update();
|
2009-02-01 00:08:08 +00:00
|
|
|
}
|
2009-03-26 19:00:08 +00:00
|
|
|
|
2009-04-17 13:51:50 +00:00
|
|
|
void ArticleRequest::stemmedSearchFinished()
|
|
|
|
{
|
|
|
|
// Got stemmed matching results
|
|
|
|
|
|
|
|
WordFinder::SearchResults sr = stemmedWordFinder->getResults();
|
|
|
|
|
|
|
|
string footer;
|
|
|
|
|
2010-03-30 13:41:14 +00:00
|
|
|
bool continueMatching = false;
|
|
|
|
|
2009-04-17 13:51:50 +00:00
|
|
|
if ( sr.size() )
|
|
|
|
{
|
|
|
|
footer += "<div class=\"gdstemmedsuggestion\"><span class=\"gdstemmedsuggestion_head\">" +
|
|
|
|
Html::escape( tr( "Close words: " ).toUtf8().data() ) +
|
|
|
|
"</span><span class=\"gdstemmedsuggestion_body\">";
|
|
|
|
|
|
|
|
for( unsigned x = 0; x < sr.size(); ++x )
|
|
|
|
{
|
2010-03-30 13:41:14 +00:00
|
|
|
footer += linkWord( sr[ x ].first );
|
2009-04-17 13:51:50 +00:00
|
|
|
|
|
|
|
if ( x != sr.size() - 1 )
|
|
|
|
{
|
|
|
|
footer += ", ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
footer += "</span></div>";
|
|
|
|
}
|
|
|
|
|
2010-03-30 13:41:14 +00:00
|
|
|
splittedWords = splitIntoWords( word );
|
|
|
|
|
|
|
|
if ( splittedWords.first.size() > 1 ) // Contains more than one word
|
|
|
|
{
|
|
|
|
disconnect( stemmedWordFinder.get(), SIGNAL( finished() ),
|
|
|
|
this, SLOT( stemmedSearchFinished() ) );
|
|
|
|
|
|
|
|
connect( stemmedWordFinder.get(), SIGNAL( finished() ),
|
|
|
|
this, SLOT( individualWordFinished() ), Qt::QueuedConnection );
|
|
|
|
|
|
|
|
currentSplittedWordStart = -1;
|
|
|
|
currentSplittedWordEnd = currentSplittedWordStart;
|
|
|
|
|
|
|
|
firstCompoundWasFound = false;
|
|
|
|
|
|
|
|
compoundSearchNextStep( false );
|
|
|
|
|
|
|
|
continueMatching = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !continueMatching )
|
|
|
|
footer += "</body></html>";
|
2009-04-17 13:51:50 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
Mutex::Lock _( dataMutex );
|
|
|
|
|
|
|
|
size_t offset = data.size();
|
|
|
|
|
|
|
|
data.resize( data.size() + footer.size() );
|
|
|
|
|
|
|
|
memcpy( &data.front() + offset, footer.data(), footer.size() );
|
|
|
|
}
|
|
|
|
|
2010-03-30 13:41:14 +00:00
|
|
|
if ( continueMatching )
|
|
|
|
update();
|
|
|
|
else
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleRequest::compoundSearchNextStep( bool lastSearchSucceeded )
|
|
|
|
{
|
|
|
|
if ( !lastSearchSucceeded )
|
|
|
|
{
|
|
|
|
// Last search was unsuccessful. First, emit what we had.
|
|
|
|
|
|
|
|
string footer;
|
|
|
|
|
2010-05-29 20:50:16 +00:00
|
|
|
if ( lastGoodCompoundResult.size() ) // We have something to append
|
2010-03-30 13:41:14 +00:00
|
|
|
{
|
2011-06-19 18:50:11 +00:00
|
|
|
// DPRINTF( "Appending\n" );
|
2010-03-30 13:41:14 +00:00
|
|
|
|
|
|
|
if ( !firstCompoundWasFound )
|
|
|
|
{
|
|
|
|
// Append the beginning
|
|
|
|
footer += "<div class=\"gdstemmedsuggestion\"><span class=\"gdstemmedsuggestion_head\">" +
|
|
|
|
Html::escape( tr( "Compound expressions: " ).toUtf8().data() ) +
|
|
|
|
"</span><span class=\"gdstemmedsuggestion_body\">";
|
|
|
|
|
|
|
|
firstCompoundWasFound = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Append the separator
|
|
|
|
footer += " / ";
|
|
|
|
}
|
|
|
|
|
2010-05-29 20:50:16 +00:00
|
|
|
footer += linkWord( lastGoodCompoundResult );
|
|
|
|
|
|
|
|
lastGoodCompoundResult.clear();
|
2010-03-30 13:41:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Then, start a new search for the next word, if possible
|
|
|
|
|
|
|
|
if ( currentSplittedWordStart >= splittedWords.first.size() - 2 )
|
|
|
|
{
|
|
|
|
// The last word was the last possible to start from
|
|
|
|
|
|
|
|
if ( firstCompoundWasFound )
|
|
|
|
footer += "</span>";
|
|
|
|
|
2010-04-01 09:08:51 +00:00
|
|
|
// Now add links to all the individual words. They conclude the result.
|
|
|
|
|
|
|
|
footer += "<div class=\"gdstemmedsuggestion\"><span class=\"gdstemmedsuggestion_head\">" +
|
|
|
|
Html::escape( tr( "Individual words: " ).toUtf8().data() ) +
|
|
|
|
"</span><span class=\"gdstemmedsuggestion_body\">";
|
|
|
|
|
|
|
|
footer += escapeSpacing( splittedWords.second[ 0 ] );
|
|
|
|
|
|
|
|
for( int x = 0; x < splittedWords.first.size(); ++x )
|
|
|
|
{
|
|
|
|
footer += linkWord( splittedWords.first[ x ] );
|
|
|
|
footer += escapeSpacing( splittedWords.second[ x + 1 ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
footer += "</span>";
|
|
|
|
|
2010-03-30 13:41:14 +00:00
|
|
|
footer += "</body></html>";
|
|
|
|
|
|
|
|
appendToData( footer );
|
|
|
|
|
|
|
|
finish();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( footer.size() )
|
|
|
|
{
|
|
|
|
appendToData( footer );
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advance to the next word and start from looking up two words
|
|
|
|
++currentSplittedWordStart;
|
|
|
|
currentSplittedWordEnd = currentSplittedWordStart + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Last lookup succeeded -- see if we can try the larger sequence
|
|
|
|
|
|
|
|
if ( currentSplittedWordEnd < splittedWords.first.size() - 1 )
|
|
|
|
{
|
|
|
|
// We can, indeed.
|
|
|
|
++currentSplittedWordEnd;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We can't. Emit what we have and start over.
|
|
|
|
|
|
|
|
++currentSplittedWordEnd; // So we could use the same code for result
|
|
|
|
// emitting
|
|
|
|
|
|
|
|
// Initiate new lookup
|
|
|
|
compoundSearchNextStep( false );
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the compound sequence
|
|
|
|
|
|
|
|
currentSplittedWordCompound = makeSplittedWordCompound();
|
|
|
|
|
|
|
|
// Look it up
|
|
|
|
|
2011-06-19 18:50:11 +00:00
|
|
|
// DPRINTF( "Looking up %s\n", qPrintable( currentSplittedWordCompound ) );
|
2010-03-30 13:41:14 +00:00
|
|
|
|
2010-05-29 20:50:16 +00:00
|
|
|
stemmedWordFinder->prefixMatch( currentSplittedWordCompound, activeDicts, 40, // Would one be enough? Leave 40 to be safe.
|
|
|
|
Dictionary::SuitableForCompoundSearching );
|
2010-03-30 13:41:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString ArticleRequest::makeSplittedWordCompound()
|
|
|
|
{
|
|
|
|
QString result;
|
|
|
|
|
|
|
|
result.clear();
|
|
|
|
|
|
|
|
for( int x = currentSplittedWordStart; x <= currentSplittedWordEnd; ++x )
|
|
|
|
{
|
|
|
|
result.append( splittedWords.first[ x ] );
|
|
|
|
|
|
|
|
if ( x < currentSplittedWordEnd )
|
|
|
|
{
|
|
|
|
wstring ws( gd::toWString( splittedWords.second[ x + 1 ] ) );
|
|
|
|
|
|
|
|
Folding::normalizeWhitespace( ws );
|
|
|
|
|
|
|
|
result.append( gd::toQString( ws ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2009-04-17 13:51:50 +00:00
|
|
|
}
|
|
|
|
|
2010-03-30 13:41:14 +00:00
|
|
|
void ArticleRequest::individualWordFinished()
|
|
|
|
{
|
|
|
|
WordFinder::SearchResults const & results = stemmedWordFinder->getResults();
|
|
|
|
|
|
|
|
if ( results.size() )
|
|
|
|
{
|
|
|
|
// Check if the aliases are acceptable
|
|
|
|
wstring source = Folding::applySimpleCaseOnly( gd::toWString( currentSplittedWordCompound ) );
|
|
|
|
|
2010-05-29 20:50:16 +00:00
|
|
|
bool hadSomething = false;
|
|
|
|
|
2010-03-30 13:41:14 +00:00
|
|
|
for( unsigned x = 0; x < results.size(); ++x )
|
2010-05-29 20:50:16 +00:00
|
|
|
{
|
|
|
|
if ( results[ x ].second )
|
|
|
|
continue; // We're not interested in suggestions
|
|
|
|
|
|
|
|
wstring result( Folding::applySimpleCaseOnly( gd::toWString( results[ x ].first ) ) );
|
|
|
|
|
|
|
|
if ( source.size() <= result.size() && result.compare( 0, source.size(), source ) == 0 )
|
2010-03-30 13:41:14 +00:00
|
|
|
{
|
2010-05-29 20:50:16 +00:00
|
|
|
// The resulting string begins with the source one
|
|
|
|
|
|
|
|
hadSomething = true;
|
|
|
|
|
|
|
|
if ( source.size() == result.size() )
|
|
|
|
{
|
|
|
|
// Got the match. No need to continue.
|
|
|
|
lastGoodCompoundResult = currentSplittedWordCompound;
|
|
|
|
break;
|
|
|
|
}
|
2010-03-30 13:41:14 +00:00
|
|
|
}
|
2010-05-29 20:50:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( hadSomething )
|
|
|
|
{
|
|
|
|
compoundSearchNextStep( true );
|
|
|
|
return;
|
|
|
|
}
|
2010-03-30 13:41:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
compoundSearchNextStep( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleRequest::appendToData( std::string const & str )
|
|
|
|
{
|
|
|
|
Mutex::Lock _( dataMutex );
|
|
|
|
|
|
|
|
size_t offset = data.size();
|
|
|
|
|
|
|
|
data.resize( data.size() + str.size() );
|
|
|
|
|
|
|
|
memcpy( &data.front() + offset, str.data(), str.size() );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QPair< ArticleRequest::Words, ArticleRequest::Spacings > ArticleRequest::splitIntoWords( QString const & input )
|
|
|
|
{
|
|
|
|
QPair< Words, Spacings > result;
|
|
|
|
|
|
|
|
QChar const * ptr = input.data();
|
|
|
|
|
|
|
|
for( ; ; )
|
|
|
|
{
|
|
|
|
QString spacing;
|
|
|
|
|
|
|
|
for( ; ptr->unicode() && ( Folding::isPunct( ptr->unicode() ) || Folding::isWhitespace( ptr->unicode() ) ); ++ptr )
|
|
|
|
spacing.append( *ptr );
|
|
|
|
|
|
|
|
result.second.append( spacing );
|
|
|
|
|
|
|
|
QString word;
|
|
|
|
|
|
|
|
for( ; ptr->unicode() && !( Folding::isPunct( ptr->unicode() ) || Folding::isWhitespace( ptr->unicode() ) ); ++ptr )
|
|
|
|
word.append( *ptr );
|
|
|
|
|
|
|
|
if ( word.isEmpty() )
|
|
|
|
break;
|
|
|
|
|
|
|
|
result.first.append( word );
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
string ArticleRequest::linkWord( QString const & str )
|
|
|
|
{
|
|
|
|
QUrl url;
|
|
|
|
|
|
|
|
url.setScheme( "gdlookup" );
|
|
|
|
url.setHost( "localhost" );
|
|
|
|
url.setPath( str );
|
|
|
|
|
|
|
|
string escapedResult = Html::escape( str.toUtf8().data() );
|
|
|
|
return string( "<a href=\"" ) + url.toEncoded().data() + "\">" + escapedResult +"</a>";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ArticleRequest::escapeSpacing( QString const & str )
|
|
|
|
{
|
|
|
|
QByteArray spacing = Html::escape( str.toUtf8().data() ).c_str();
|
|
|
|
|
|
|
|
spacing.replace( "\n", "<br>" );
|
|
|
|
|
|
|
|
return spacing.data();
|
|
|
|
}
|
2011-12-12 16:52:07 +00:00
|
|
|
|
|
|
|
void ArticleRequest::cancel()
|
|
|
|
{
|
|
|
|
if( isFinished() )
|
|
|
|
return;
|
|
|
|
if( !altSearches.empty() )
|
|
|
|
{
|
|
|
|
for( list< sptr< Dictionary::WordSearchRequest > >::iterator i =
|
|
|
|
altSearches.begin(); i != altSearches.end(); ++i )
|
|
|
|
{
|
|
|
|
(*i)->cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( !bodyRequests.empty() )
|
|
|
|
{
|
|
|
|
for( list< sptr< Dictionary::DataRequest > >::iterator i =
|
|
|
|
bodyRequests.begin(); i != bodyRequests.end(); ++i )
|
|
|
|
{
|
|
|
|
(*i)->cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( stemmedWordFinder.get() ) stemmedWordFinder->cancel();
|
|
|
|
finish();
|
|
|
|
}
|