/* This file is (c) 2008-2009 Konstantin Isakov * 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" #include "dictlock.hh" #include #include using std::vector; using std::string; using std::wstring; using std::set; ArticleMaker::ArticleMaker( vector< sptr< Dictionary::Class > > const & dictionaries_, vector< Instances::Group > const & groups_ ): dictionaries( dictionaries_ ), groups( groups_ ) { } std::string ArticleMaker::makeHtmlHeader( QString const & word, QString const & icon ) { string result = "" ""; // Add a css stylesheet QFile builtInCssFile( ":/article-style.css" ); builtInCssFile.open( QFile::ReadOnly ); QByteArray css = builtInCssFile.readAll(); QFile cssFile( Config::getUserCssFileName() ); if ( cssFile.open( QFile::ReadOnly ) ) css += cssFile.readAll(); result += "\n"; result += "" + Html::escape( Utf8::encode( word.toStdWString() ) ) + ""; // This doesn't seem to be much of influence right now, but we'll keep // it anyway. if ( icon.size() ) result += "\n"; result += ""; return result; } string ArticleMaker::makeDefinitionFor( QString const & inWord, QString const & group ) const { printf( "group = %ls\n", group.toStdWString().c_str() ); wstring word = inWord.trimmed().toStdWString(); if ( group == "internal:about" ) { // This is a special group containing internal welcome/help pages string result = makeHtmlHeader( inWord, QString() ); if ( inWord == "Welcome!" ) { result += tr( "

Welcome to GoldenDict!

" "

To start working with the program, first add some directory paths where to search " "for the dictionary files at Edit|Sources. Note that the each subdirectory is to be added separately. " "After that, you can organize all the dictionaries found into groups " "in Edit|Groups." "

You can also check out the available program preferences at Edit|Preferences. " "All settings there have tooltips, be sure to read them if you are in doubt about anything." "

And then you're reading to look up your words! You can do that in this window " "by using a pane to the left, or you can look up words from other active applications. " "

Should you need further help, have any questions, " "suggestions or just wonder what the others think, you are welcome at the program's forum." "

You can also contact the author directly by writing an \">e-mail." "

Check program's website for the updates. " "

(c) 2008-2009 Konstantin Isakov. Licensed under GPLv3 or later." ).toUtf8().data(); } else if ( inWord == "Working with popup" ) { result += ( tr( "

Working with the popup

" "To look up words from other active applications, you would need to first activate the \"Scan popup functionality\" in Preferences, " "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 return makeNotFoundTextFor( inWord, group ); } result += ""; return result; } // Find the given group Instances::Group const * activeGroup = 0; for( unsigned x = 0; x < groups.size(); ++x ) if ( groups[ x ].name == group ) { 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; string result = makeHtmlHeader( inWord.trimmed(), activeGroup && activeGroup->icon.size() ? activeGroup->icon : QString() ); DictLock _; // Accumulate main forms vector< wstring > alts; { set< wstring > altsSet; for( unsigned x = 0; x < activeDicts.size(); ++x ) { vector< wstring > found = activeDicts[ x ]->findHeadwordsForSynonym( word ); altsSet.insert( found.begin(), found.end() ); } alts.insert( alts.begin(), altsSet.begin(), altsSet.end() ); } for( unsigned x = 0; x < alts.size(); ++x ) { printf( "Alt: %ls\n", alts[ x ].c_str() ); } for( unsigned x = 0; x < activeDicts.size(); ++x ) { try { string body = activeDicts[ x ]->getArticle( word, alts ); printf( "From %s: %s\n", activeDicts[ x ]->getName().c_str(), body.c_str() ); result += string( "
" ) + tr( "From " ).toUtf8().data() + Html::escape( activeDicts[ x ]->getName() ) + "
" + body; } catch( Dictionary::exNoSuchWord & ) { continue; } } result += ""; return result; } string ArticleMaker::makeNotFoundTextFor( QString const & word, QString const & group ) const { return makeHtmlHeader( word, QString() ) + "

" + tr( "No translation for %1 was found in group %2." ). arg( QString::fromUtf8( Html::escape( word.toUtf8().data() ).c_str() ) ). arg( QString::fromUtf8(Html::escape( group.toUtf8().data() ).c_str() ) ). toUtf8().data() +"

" ""; }