| |
| |
|
|
| #include <vector> |
| #include <algorithm> |
| #include <cstdio> |
| #include "dictionary.hh" |
|
|
| |
| #include <QFileInfo> |
| #include <QDateTime> |
|
|
| #include "config.hh" |
| #include "common/globalbroadcaster.hh" |
| #include <QDir> |
| #include <QCryptographicHash> |
| #include <QImage> |
| #include <QPixmap> |
| #include <QPainter> |
| #include <QRegularExpression> |
| #include "utils.hh" |
| #include "zipfile.hh" |
| #include <array> |
|
|
| namespace Dictionary { |
|
|
| bool Request::isFinished() |
| { |
| return Utils::AtomicInt::loadAcquire( isFinishedFlag ); |
| } |
|
|
| void Request::update() |
| { |
| if ( !Utils::AtomicInt::loadAcquire( isFinishedFlag ) ) { |
| emit updated(); |
| } |
| } |
|
|
| void Request::finish() |
| { |
| if ( !Utils::AtomicInt::loadAcquire( isFinishedFlag ) ) { |
| { |
| QMutexLocker _( &dataMutex ); |
| isFinishedFlag.ref(); |
|
|
| cond.wakeAll(); |
| } |
| emit finished(); |
| } |
| } |
|
|
| void Request::setErrorString( const QString & str ) |
| { |
| QMutexLocker _( &errorStringMutex ); |
|
|
| errorString = str; |
| } |
|
|
| QString Request::getErrorString() |
| { |
| QMutexLocker _( &errorStringMutex ); |
|
|
| return errorString; |
| } |
|
|
|
|
| |
|
|
| size_t WordSearchRequest::matchesCount() |
| { |
| QMutexLocker _( &dataMutex ); |
|
|
| return matches.size(); |
| } |
|
|
| WordMatch WordSearchRequest::operator[]( size_t index ) |
| { |
| QMutexLocker _( &dataMutex ); |
|
|
| if ( index >= matches.size() ) { |
| throw exIndexOutOfRange(); |
| } |
|
|
| return matches[ index ]; |
| } |
|
|
| vector< WordMatch > & WordSearchRequest::getAllMatches() |
| { |
| if ( !isFinished() ) { |
| throw exRequestUnfinished(); |
| } |
|
|
| return matches; |
| } |
|
|
| void WordSearchRequest::addMatch( const WordMatch & match ) |
| { |
| QMutexLocker _( &dataMutex ); |
|
|
| |
| if ( std::find( matches.begin(), matches.end(), match ) == matches.end() ) { |
| matches.push_back( match ); |
| } |
| } |
|
|
| |
|
|
| long DataRequest::dataSize() |
| { |
| QMutexLocker _( &dataMutex ); |
| long size = hasAnyData ? (long)data.size() : -1; |
|
|
| if ( size == 0 && !isFinished() ) { |
| cond.wait( &dataMutex ); |
| size = hasAnyData ? (long)data.size() : -1; |
| } |
| return size; |
| } |
|
|
| void DataRequest::appendDataSlice( const void * buffer, size_t size ) |
| { |
| QMutexLocker _( &dataMutex ); |
|
|
| size_t offset = data.size(); |
|
|
| data.resize( data.size() + size ); |
|
|
| memcpy( &data.front() + offset, buffer, size ); |
| cond.wakeAll(); |
| } |
|
|
| void DataRequest::appendString( std::string_view str ) |
| { |
| QMutexLocker _( &dataMutex ); |
| data.reserve( data.size() + str.size() ); |
| data.insert( data.end(), str.begin(), str.end() ); |
| cond.wakeAll(); |
| } |
|
|
| void DataRequest::getDataSlice( size_t offset, size_t size, void * buffer ) |
| { |
| if ( size == 0 ) { |
| return; |
| } |
| QMutexLocker _( &dataMutex ); |
|
|
| if ( !hasAnyData ) { |
| throw exSliceOutOfRange(); |
| } |
|
|
| memcpy( buffer, &data[ offset ], size ); |
| } |
|
|
| vector< char > & DataRequest::getFullData() |
| { |
| if ( !isFinished() ) { |
| throw exRequestUnfinished(); |
| } |
|
|
| return data; |
| } |
|
|
| Class::Class( const string & id_, const vector< string > & dictionaryFiles_ ): |
| id( id_ ), |
| dictionaryFiles( dictionaryFiles_ ), |
| indexedFtsDoc( 0 ), |
| dictionaryIconLoaded( false ), |
| can_FTS( false ), |
| FTS_index_completed( false ) |
| { |
| } |
|
|
| void Class::deferredInit() |
| { |
| |
| } |
|
|
| sptr< WordSearchRequest > Class::stemmedMatch( const std::u32string & , |
| unsigned , |
| unsigned , |
| unsigned long ) |
| { |
| return std::make_shared< WordSearchRequestInstant >(); |
| } |
|
|
| sptr< WordSearchRequest > Class::findHeadwordsForSynonym( const std::u32string & ) |
| { |
| return std::make_shared< WordSearchRequestInstant >(); |
| } |
|
|
| vector< std::u32string > Class::getAlternateWritings( const std::u32string & ) noexcept |
| { |
| return {}; |
| } |
|
|
| QString Class::getContainingFolder() const |
| { |
| if ( !dictionaryFiles.empty() ) { |
| auto fileInfo = QFileInfo( QString::fromStdString( dictionaryFiles[ 0 ] ) ); |
| if ( fileInfo.isDir() ) { |
| return fileInfo.absoluteFilePath(); |
| } |
| return fileInfo.absolutePath(); |
| } |
|
|
| return {}; |
| } |
|
|
| sptr< DataRequest > Class::getResource( const string & ) |
| |
| { |
| return std::make_shared< DataRequestInstant >( false ); |
| } |
|
|
| sptr< DataRequest > Class::getSearchResults( const QString &, int, bool, bool ) |
| { |
| return std::make_shared< DataRequestInstant >( false ); |
| } |
|
|
| const QString & Class::getDescription() |
| { |
| return dictionaryDescription; |
| } |
|
|
| void Class::setIndexedFtsDoc( long _indexedFtsDoc ) |
| { |
| indexedFtsDoc = _indexedFtsDoc; |
|
|
| auto newProgress = getIndexingFtsProgress(); |
| if ( newProgress != lastProgress ) { |
| lastProgress = newProgress; |
| emit GlobalBroadcaster::instance() |
| -> indexingDictionary( QString( "%1......%%2" ).arg( QString::fromStdString( getName() ) ).arg( newProgress ) ); |
| } |
| } |
|
|
|
|
| QString Class::getMainFilename() |
| { |
| return {}; |
| } |
|
|
| const QIcon & Class::getIcon() noexcept |
| { |
| if ( !dictionaryIconLoaded ) { |
| loadIcon(); |
| } |
| return dictionaryIcon; |
| } |
|
|
| void Class::loadIcon() noexcept |
| { |
| dictionaryIconLoaded = true; |
| } |
|
|
| int Class::getOptimalIconSize() |
| { |
| return 64 * qGuiApp->devicePixelRatio(); |
| } |
|
|
| bool Class::loadIconFromFileName( const QString & mainDictFileName ) |
| { |
| const QFileInfo info( mainDictFileName ); |
| QDir dir = info.absoluteDir(); |
|
|
| dir.setFilter( QDir::Files ); |
| dir.setNameFilters( QStringList() << "*.bmp" |
| << "*.png" |
| << "*.jpg" |
| << "*.ico" |
| << "*.jpeg" |
| << "*.gif" |
| << "*.webp" |
| << "*.svg" |
| << "*.svgz" ); |
|
|
| const QString basename = info.baseName(); |
| for ( const auto & f : dir.entryInfoList() ) { |
| if ( f.baseName() == basename && loadIconFromFilePath( f.absoluteFilePath() ) ) { |
| return true; |
| } |
| } |
| return false; |
| } |
|
|
| bool Class::loadIconFromFilePath( const QString & filename ) |
| { |
| auto iconSize = getOptimalIconSize(); |
| QImage img( filename ); |
|
|
| if ( img.isNull() ) { |
| return false; |
| } |
| else { |
| auto result = img.scaled( { iconSize, iconSize }, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation ); |
| dictionaryIcon = QIcon( QPixmap::fromImage( result ) ); |
|
|
| return !dictionaryIcon.isNull(); |
| } |
| } |
|
|
| bool Class::loadIconFromText( const QString & iconUrl, const QString & text ) |
| { |
| |
| auto abbrName = getAbbrName( text ); |
| if ( abbrName.isEmpty() ) { |
| return false; |
| } |
| QImage img( iconUrl ); |
|
|
| if ( !img.isNull() ) { |
| auto iconSize = getOptimalIconSize(); |
|
|
| QImage result = img.scaled( { iconSize, iconSize }, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation ); |
|
|
| QPainter painter( &result ); |
| painter.setRenderHints( QPainter::Antialiasing | QPainter::TextAntialiasing ); |
| painter.setCompositionMode( QPainter::CompositionMode_SourceAtop ); |
|
|
| QFont font = painter.font(); |
| |
| font.setPixelSize( iconSize * 0.8 ); |
| font.setWeight( QFont::Bold ); |
| painter.setFont( font ); |
|
|
| const QRect rectangle = QRect( 0, 0, iconSize, iconSize ); |
|
|
| painter.setPen( intToFixedColor( qHash( abbrName ) ) ); |
|
|
| |
| painter.drawText( rectangle, Qt::AlignCenter, abbrName.at( 0 ) ); |
|
|
| |
| font.setPixelSize( iconSize * 0.4 ); |
| QFontMetrics fm1( font ); |
| const QString & orderNum = abbrName.mid( 1 ); |
|
|
| painter.setFont( font ); |
| painter.drawText( rectangle, Qt::AlignRight | Qt::AlignBottom, orderNum ); |
|
|
| painter.end(); |
|
|
| dictionaryIcon = QIcon( QPixmap::fromImage( result ) ); |
|
|
| return !dictionaryIcon.isNull(); |
| } |
| return false; |
| } |
|
|
| QColor Class::intToFixedColor( int index ) |
| { |
| |
| static const std::array colors = { |
| QColor( 255, 0, 0, 200 ), |
| QColor( 4, 57, 108, 200 ), |
| QColor( 0, 255, 0, 200 ), |
| QColor( 0, 0, 255, 200 ), |
| QColor( 255, 255, 0, 200 ), |
| QColor( 0, 255, 255, 200 ), |
| QColor( 255, 0, 255, 200 ), |
| QColor( 192, 192, 192, 200 ), |
| QColor( 255, 165, 0, 200 ), |
| QColor( 128, 0, 128, 200 ), |
| QColor( 128, 128, 0, 200 ) |
| }; |
|
|
| |
| return colors[ index % colors.size() ]; |
| } |
|
|
| QString Class::getAbbrName( const QString & text ) |
| { |
| return GlobalBroadcaster::instance()->getAbbrName( text ); |
| } |
|
|
| |
| int findMatchingBracket( const QString & css, int startPos ); |
|
|
| void Class::isolateCSS( QString & css, const QString & wrapperSelector ) |
| { |
| |
| if ( css.isEmpty() ) { |
| return; |
| } |
|
|
| if ( css.indexOf( "{" ) == -1 ) { |
| return; |
| } |
|
|
| QString newCSS; |
| int currentPos = 0; |
|
|
| |
| QString prefix( "#gdarticlefrom-" ); |
| prefix += QString::fromLatin1( getId().c_str() ); |
| if ( !wrapperSelector.isEmpty() ) { |
| prefix += " " + wrapperSelector; |
| } |
|
|
| |
| QRegularExpression commentRegex( R"(\/\*(?:.(?!\*\/))*.?\*\/)", QRegularExpression::DotMatchesEverythingOption ); |
| QRegularExpression selectorSeparatorRegex( R"([ \*\>\+,;:\[\{\]])" ); |
| QRegularExpression selectorEndRegex( "[,\\{]" ); |
|
|
| |
| css.replace( commentRegex, QString() ); |
|
|
| |
| css.replace( QRegularExpression( R"(:root\s*{)" ), "html{" ); |
|
|
| |
| while ( currentPos < css.length() ) { |
| QChar ch = css.at( currentPos ); |
|
|
| if ( ch == '@' ) { |
| |
| int ruleNameEnd = css.indexOf( QRegularExpression( "[^\\w-]" ), currentPos + 1 ); |
| if ( ruleNameEnd == -1 ) { |
| |
| newCSS.append( css.mid( currentPos ) ); |
| break; |
| } |
|
|
| QString ruleName = css.mid( currentPos, ruleNameEnd - currentPos ).trimmed(); |
|
|
| |
| if ( ruleName.compare( "@import", Qt::CaseInsensitive ) == 0 |
| || ruleName.compare( "@charset", Qt::CaseInsensitive ) == 0 ) { |
| |
| int semicolonPos = css.indexOf( ';', currentPos ); |
| if ( semicolonPos != -1 ) { |
| newCSS.append( css.mid( currentPos, semicolonPos - currentPos + 1 ) ); |
| currentPos = semicolonPos + 1; |
| continue; |
| } |
| } |
|
|
| |
| if ( ruleName.compare( "@page", Qt::CaseInsensitive ) == 0 ) { |
| int closeBracePos = findMatchingBracket( css, currentPos ); |
| if ( closeBracePos != -1 ) { |
| currentPos = closeBracePos + 1; |
| continue; |
| } |
| } |
|
|
| |
| int openBracePos = css.indexOf( '{', currentPos ); |
| int semicolonPos = css.indexOf( ';', currentPos ); |
|
|
| if ( openBracePos != -1 && ( semicolonPos == -1 || openBracePos < semicolonPos ) ) { |
| |
| |
| newCSS.append( css.mid( currentPos, openBracePos - currentPos + 1 ) ); |
|
|
| |
| int closeBracePos = findMatchingBracket( css, openBracePos ); |
| if ( closeBracePos != -1 ) { |
| |
| QString innerCSS = css.mid( openBracePos + 1, closeBracePos - openBracePos - 1 ); |
| isolateCSS( innerCSS, wrapperSelector ); |
| newCSS.append( innerCSS ); |
| newCSS.append( '}' ); |
| currentPos = closeBracePos + 1; |
| } |
| else { |
| |
| currentPos = openBracePos + 1; |
| } |
| } |
| else if ( semicolonPos != -1 ) { |
| |
| newCSS.append( css.mid( currentPos, semicolonPos - currentPos + 1 ) ); |
| currentPos = semicolonPos + 1; |
| continue; |
| } |
| else { |
| |
| newCSS.append( css.mid( currentPos ) ); |
| break; |
| } |
| } |
| else if ( ch == '{' ) { |
| |
| int closeBracePos = findMatchingBracket( css, currentPos ); |
| if ( closeBracePos != -1 ) { |
| newCSS.append( css.mid( currentPos, closeBracePos - currentPos + 1 ) ); |
| currentPos = closeBracePos + 1; |
| continue; |
| } |
| else { |
| newCSS.append( css.mid( currentPos ) ); |
| break; |
| } |
| } |
| else if ( ch.isLetter() || ch == '.' || ch == '#' || ch == '*' || ch == '\\' || ch == ':' || ch == '[' ) { |
| if ( ch.isLetter() || ch == '*' ) { |
| |
| QChar chr; |
| for ( int i = currentPos; i < css.length(); i++ ) { |
| chr = css.at( i ); |
| if ( chr.isLetterOrNumber() || chr.isMark() || chr == '_' || chr == '-' |
| || ( chr == '*' && i == currentPos ) ) { |
| continue; |
| } |
|
|
| if ( chr == '|' ) { |
| |
| newCSS.append( css.mid( currentPos, i - currentPos + 1 ) ); |
| currentPos = i + 1; |
| } |
| break; |
| } |
| if ( chr == '|' ) { |
| continue; |
| } |
| } |
|
|
| |
| if ( ch == '[' ) { |
| |
| int bracketDepth = 1; |
| int closingBracketPos = -1; |
| for ( int i = currentPos + 1; i < css.length(); i++ ) { |
| QChar currentChar = css.at( i ); |
| |
| if ( currentChar == '\\' && i + 1 < css.length() ) { |
| i++; |
| continue; |
| } |
| if ( currentChar == '[' ) { |
| bracketDepth++; |
| } |
| else if ( currentChar == ']' ) { |
| bracketDepth--; |
| if ( bracketDepth == 0 ) { |
| closingBracketPos = i; |
| break; |
| } |
| } |
| } |
|
|
| if ( closingBracketPos != -1 ) { |
| |
| newCSS.append( prefix + " " ); |
| newCSS.append( css.mid( currentPos, closingBracketPos - currentPos + 1 ) ); |
| currentPos = closingBracketPos + 1; |
| continue; |
| } |
| } |
|
|
| |
| |
| int selectorEndPos = css.indexOf( selectorSeparatorRegex, currentPos + 1 ); |
| |
| if ( selectorEndPos < 0 ) { |
| selectorEndPos = css.indexOf( selectorEndRegex, currentPos ); |
| } |
| QString selectorPart = css.mid( currentPos, selectorEndPos < 0 ? selectorEndPos : selectorEndPos - currentPos ); |
|
|
| if ( selectorEndPos < 0 ) { |
| newCSS.append( prefix + " " + selectorPart ); |
| break; |
| } |
|
|
| QString trimmedSelector = selectorPart.trimmed(); |
| if ( trimmedSelector.compare( "body", Qt::CaseInsensitive ) == 0 |
| || trimmedSelector.compare( "html", Qt::CaseInsensitive ) == 0 ) { |
| |
| newCSS.append( selectorPart + " " + prefix + " " ); |
| currentPos += trimmedSelector.length(); |
| } |
| else { |
| |
| newCSS.append( prefix + " " ); |
| } |
|
|
| int ruleStartPos = css.indexOf( selectorEndRegex, currentPos ); |
| QString remainingPart = css.mid( currentPos, ruleStartPos < 0 ? ruleStartPos : ruleStartPos - currentPos ); |
| newCSS.append( remainingPart ); |
|
|
| if ( ruleStartPos < 0 ) { |
| break; |
| } |
|
|
| currentPos = ruleStartPos; |
| continue; |
| } |
| else { |
| newCSS.append( ch ); |
| currentPos++; |
| } |
| } |
|
|
| css = newCSS; |
| } |
|
|
| |
| int findMatchingBracket( const QString & css, int startPos ) |
| { |
| int depth = 1; |
| for ( int i = startPos + 1; i < css.length(); i++ ) { |
| QChar ch = css.at( i ); |
|
|
| |
| if ( ch == '\\' && i + 1 < css.length() ) { |
| i++; |
| continue; |
| } |
|
|
| if ( ch == '{' ) { |
| depth++; |
| } |
| else if ( ch == '}' ) { |
| depth--; |
| if ( depth == 0 ) { |
| return i; |
| } |
| } |
| } |
| return -1; |
| } |
|
|
| string makeDictionaryId( const vector< string > & dictionaryFiles ) noexcept |
| { |
| std::vector< string > sortedList; |
|
|
| if ( Config::isPortableVersion() ) { |
| |
| sortedList.reserve( dictionaryFiles.size() ); |
|
|
| const QDir dictionariesDir( Config::getPortableVersionDictionaryDir() ); |
|
|
| for ( const auto & full : dictionaryFiles ) { |
| QFileInfo fileInfo( QString::fromStdString( full ) ); |
|
|
| if ( fileInfo.isAbsolute() ) { |
| sortedList.push_back( dictionariesDir.relativeFilePath( fileInfo.filePath() ).toStdString() ); |
| } |
| else { |
| |
| |
| sortedList.push_back( full ); |
| } |
| } |
| } |
| else { |
| sortedList = dictionaryFiles; |
| } |
|
|
| std::sort( sortedList.begin(), sortedList.end() ); |
|
|
| QCryptographicHash hash( QCryptographicHash::Md5 ); |
|
|
| for ( const auto & i : sortedList ) { |
| |
| hash.addData( { i.c_str(), static_cast< qsizetype >( i.size() + 1 ) } ); |
| } |
|
|
| return hash.result().toHex().data(); |
| } |
|
|
| |
| |
| |
| |
| bool needToRebuildIndex( const vector< string > & dictionaryFiles, const string & indexFile ) noexcept |
| { |
| |
| Config::Class * cfg = GlobalBroadcaster::instance()->getConfig(); |
|
|
| |
| if ( cfg && !cfg->dictionariesToReindex.isEmpty() ) { |
| std::string dictId = makeDictionaryId( dictionaryFiles ); |
|
|
| |
| if ( cfg->dictionariesToReindex.contains( QString::fromStdString( dictId ) ) ) { |
| |
| cfg->dictionariesToReindex.remove( QString::fromStdString( dictId ) ); |
| |
| cfg->dirty = true; |
| return true; |
| } |
| } |
|
|
| |
| qint64 lastModified = 0; |
|
|
| for ( const auto & dictionaryFile : dictionaryFiles ) { |
| QString name = QString::fromUtf8( dictionaryFile.c_str() ); |
| QFileInfo fileInfo( name ); |
| qint64 ts; |
|
|
| if ( fileInfo.isDir() ) { |
| continue; |
| } |
|
|
| if ( name.toLower().endsWith( ".zip" ) ) { |
| ZipFile::SplitZipFile zf( name ); |
| if ( !zf.exists() ) { |
| return true; |
| } |
| ts = zf.lastModified().toSecsSinceEpoch(); |
| } |
| else { |
| if ( !fileInfo.exists() ) { |
| continue; |
| } |
| ts = fileInfo.lastModified().toSecsSinceEpoch(); |
| } |
|
|
| if ( ts > lastModified ) { |
| lastModified = ts; |
| } |
| } |
|
|
|
|
| QFileInfo fileInfo( indexFile.c_str() ); |
|
|
| if ( !fileInfo.exists() ) { |
| return true; |
| } |
|
|
| return fileInfo.lastModified().toSecsSinceEpoch() < lastModified; |
| } |
|
|
| string getFtsSuffix() |
| { |
| return "_FTS_x"; |
| } |
|
|
| QString generateRandomDictionaryId() |
| { |
| return QCryptographicHash::hash( QUuid::createUuid().toString().toUtf8(), QCryptographicHash::Md5 ).toHex(); |
| } |
|
|
| QMap< std::string, sptr< Dictionary::Class > > dictToMap( const std::vector< sptr< Dictionary::Class > > & dicts ) |
| { |
| QMap< std::string, sptr< Dictionary::Class > > dictMap; |
| for ( const auto & dict : dicts ) { |
| if ( !dict ) { |
| continue; |
| } |
| dictMap.insert( dict.get()->getId(), dict ); |
| } |
| return dictMap; |
| } |
| } |
|
|