void DynamicModel::newTrackGenerated( const Tomahawk::query_ptr& query ) { if ( m_onDemandRunning ) { bool isDuplicate = false; for ( int i = 0; i < m_deduper.size(); i++ ) { if ( m_deduper[ i ].first == query->track() && m_deduper[ i ].second == query->artist() ) isDuplicate = true; } if ( isDuplicate ) { m_playlist->generator()->fetchNext(); return; } else { m_deduper.append( QPair< QString, QString >( query->track(), query->artist() ) ); } connect( query.data(), SIGNAL( resolvingFinished( bool ) ), this, SLOT( trackResolveFinished( bool ) ) ); m_waitingFor << query.data(); appendQuery( query ); } }
void ColumnViewPreviewWidget::setQuery( const Tomahawk::query_ptr& query ) { if ( !m_query.isNull() ) { disconnect( m_query->track().data(), SIGNAL( updated() ), this, SLOT( onCoverUpdated() ) ); } m_query = query; connect( m_query->track().data(), SIGNAL( updated() ), SLOT( onCoverUpdated() ) ); onCoverUpdated(); m_cover->setQuery( query ); setVisible( true ); m_trackLabel->setText( query->track()->track() ); m_artistLabel->setArtist( query->track()->artistPtr() ); m_artistLabel->setMinimumWidth( qMin( m_artistLabel->fontMetrics().width( query->track()->artist() ) + m_artistLabel->contentsMargins().left() + m_artistLabel->contentsMargins().right() + 2 * m_artistLabel->lineWidth(), width() ) ); m_artistLabel->setElideMode( Qt::ElideRight ); m_composerValue->setText( query->track()->composer() ); m_composerValue->setVisible( !query->track()->composerPtr().isNull() ); m_composerLabel->setVisible( !query->track()->composerPtr().isNull() ); if ( query->numResults() ) { m_yearValue->setText( QString::number( query->track()->year() ) ); m_bitrateValue->setText( tr( "%1 kbps" ).arg( query->results().first()->bitrate() ) ); m_durationValue->setText( TomahawkUtils::timeToString( query->track()->duration() ) ); m_ageValue->setText( TomahawkUtils::ageToString( QDateTime::fromTime_t( query->results().first()->modificationTime() ) ) ); m_yearValue->setVisible( query->track()->year() > 0 ); m_yearLabel->setVisible( query->track()->year() > 0 ); m_bitrateLabel->setVisible( query->results().first()->bitrate() > 0 ); m_bitrateValue->setVisible( query->results().first()->bitrate() > 0 ); m_durationLabel->setVisible( query->track()->duration() > 0 ); m_durationValue->setVisible( query->track()->duration() > 0 ); m_ageLabel->setVisible( query->results().first()->modificationTime() > 0 ); m_ageValue->setVisible( query->results().first()->modificationTime() > 0 ); } else { m_yearLabel->setVisible( false ); m_yearValue->setVisible( false ); m_bitrateLabel->setVisible( false ); m_bitrateValue->setVisible( false ); m_durationLabel->setVisible( false ); m_durationValue->setVisible( false ); m_ageLabel->setVisible( false ); m_ageValue->setVisible( false ); } setMinimumHeight( sizeHint().height() ); }
PlayableItem::PlayableItem( const Tomahawk::query_ptr& query, PlayableItem* parent, int row ) : QObject( parent ) , m_query( query ) { init( parent, row ); connect( query->track().data(), SIGNAL( socialActionsLoaded() ), SIGNAL( dataChanged() ) ); connect( query->track().data(), SIGNAL( updated() ), SIGNAL( dataChanged() ) ); connect( query.data(), SIGNAL( resultsChanged() ), SLOT( onResultsChanged() ) ); }
void ScriptEngine::resolve( const Tomahawk::query_ptr& query ) { qDebug() << Q_FUNC_INFO << query->toString(); QString eval = QString( "resolve( '%1', '%2', '%3', '%4' );" ) .arg( query->id().replace( "'", "\\'" ) ) .arg( query->artist().replace( "'", "\\'" ) ) .arg( query->album().replace( "'", "\\'" ) ) .arg( query->track().replace( "'", "\\'" ) ); QList< Tomahawk::result_ptr > results; QVariantMap m = mainFrame()->evaluateJavaScript( eval ).toMap(); qDebug() << "JavaScript Result:" << m; const QString qid = query->id(); const QVariantList reslist = m.value( "results" ).toList(); foreach( const QVariant& rv, reslist ) { QVariantMap m = rv.toMap(); qDebug() << "RES" << m; Tomahawk::result_ptr rp( new Tomahawk::Result() ); Tomahawk::artist_ptr ap = Tomahawk::Artist::get( 0, m.value( "artist" ).toString() ); rp->setArtist( ap ); rp->setAlbum( Tomahawk::Album::get( 0, m.value( "album" ).toString(), ap ) ); rp->setTrack( m.value( "track" ).toString() ); rp->setBitrate( m.value( "bitrate" ).toUInt() ); rp->setUrl( m.value( "url" ).toString() ); rp->setSize( m.value( "size" ).toUInt() ); rp->setScore( m.value( "score" ).toFloat() * ( (float)m_parent->weight() / 100.0 ) ); rp->setRID( uuid() ); rp->setFriendlySource( m_parent->name() ); if ( m.contains( "year" ) ) { QVariantMap attr; attr[ "releaseyear" ] = m.value( "year" ); rp->setAttributes( attr ); } rp->setDuration( m.value( "duration", 0 ).toUInt() ); if ( rp->duration() <= 0 && m.contains( "durationString" ) ) { QTime time = QTime::fromString( m.value( "durationString" ).toString(), "hh:mm:ss" ); rp->setDuration( time.secsTo( QTime( 0, 0 ) ) * -1 ); } rp->setMimetype( m.value( "mimetype" ).toString() ); if ( rp->mimetype().isEmpty() ) { rp->setMimetype( TomahawkUtils::extensionToMimetype( m.value( "extension" ).toString() ) ); Q_ASSERT( !rp->mimetype().isEmpty() ); } results << rp; }
void MetadataEditor::loadQuery( const Tomahawk::query_ptr& query ) { if ( query.isNull() ) return; if ( query->numResults() ) { loadResult( query->results().first() ); return; } m_result = Tomahawk::result_ptr(); m_query = query; setEditable( false ); setTitle( query->track()->track() ); setArtist( query->track()->artist() ); setAlbum( query->track()->album() ); setAlbumPos( query->track()->albumpos() ); setDuration( query->track()->duration() ); setYear( 0 ); setBitrate( 0 ); setFileName( QString() ); setFileSize( 0 ); setWindowTitle( query->track()->track() ); if ( m_interface ) { m_index = m_interface->indexOfQuery( query ); if ( m_index >= 0 ) enablePushButtons(); } }
void QtScriptResolver::resolve( const Tomahawk::query_ptr& query ) { if ( QThread::currentThread() != thread() ) { QMetaObject::invokeMethod( this, "resolve", Qt::QueuedConnection, Q_ARG(Tomahawk::query_ptr, query) ); return; } QString eval; if ( !query->isFullTextQuery() ) { eval = QString( RESOLVER_LEGACY_CODE2 "resolver.resolve( '%1', '%2', '%3', '%4' );" ) .arg( query->id().replace( "'", "\\'" ) ) .arg( query->artist().replace( "'", "\\'" ) ) .arg( query->album().replace( "'", "\\'" ) ) .arg( query->track().replace( "'", "\\'" ) ); } else { eval = QString( "if(Tomahawk.resolver.instance !== undefined) {" " resolver.search( '%1', '%2' );" "} else {" " resolve( '%1', '', '', '%2' );" "}" ) .arg( query->id().replace( "'", "\\'" ) ) .arg( query->fullTextQuery().replace( "'", "\\'" ) ); } QVariantMap m = m_engine->mainFrame()->evaluateJavaScript( eval ).toMap(); if( m.isEmpty() ) { // if the resolver doesn't return anything, async api is used return; } qDebug() << "JavaScript Result:" << m; const QString qid = query->id(); const QVariantList reslist = m.value( "results" ).toList(); QList< Tomahawk::result_ptr > results = parseResultVariantList( reslist ); Tomahawk::Pipeline::instance()->reportResults( qid, results ); }
// TODO make clever (ft. featuring live (stuff) etc) float DatabaseCommand_Resolve::how_similar( const Tomahawk::query_ptr& q, const Tomahawk::result_ptr& r ) { // query values const QString qArtistname = DatabaseImpl::sortname( q->artist() ); const QString qAlbumname = DatabaseImpl::sortname( q->album() ); const QString qTrackname = DatabaseImpl::sortname( q->track() ); // result values const QString rArtistname = DatabaseImpl::sortname( r->artist()->name() ); const QString rAlbumname = DatabaseImpl::sortname( r->album()->name() ); const QString rTrackname = DatabaseImpl::sortname( r->track() ); // normal edit distance int artdist = levenshtein( qArtistname, rArtistname ); int albdist = levenshtein( qAlbumname, rAlbumname ); int trkdist = levenshtein( qTrackname, rTrackname ); // max length of name int mlart = qMax( qArtistname.length(), rArtistname.length() ); int mlalb = qMax( qAlbumname.length(), rAlbumname.length() ); int mltrk = qMax( qTrackname.length(), rTrackname.length() ); // distance scores float dcart = (float)( mlart - artdist ) / mlart; float dcalb = (float)( mlalb - albdist ) / mlalb; float dctrk = (float)( mltrk - trkdist ) / mltrk; // don't penalize for missing album name if( qAlbumname.length() == 0 ) dcalb = 1.0; // weighted, so album match is worth less than track title float combined = ( dcart*4 + dcalb + dctrk*5 ) / 10; return combined; }
bool PlayableProxyModel::lessThan( int column, const Tomahawk::query_ptr& q1, const Tomahawk::query_ptr& q2 ) const { // Attention: This function may be called very often! // So be aware of its performance. const Tomahawk::track_ptr& t1 = q1->track(); const Tomahawk::track_ptr& t2 = q2->track(); const QString& artist1 = t1->artistSortname(); const QString& artist2 = t2->artistSortname(); const QString& album1 = t1->albumSortname(); const QString& album2 = t2->albumSortname(); const unsigned int albumpos1 = t1->albumpos(); const unsigned int albumpos2 = t2->albumpos(); const unsigned int discnumber1 = t1->discnumber(); const unsigned int discnumber2 = t2->discnumber(); const qint64 id1 = (qint64)&q1; const qint64 id2 = (qint64)&q2; if ( column == PlayableModel::Artist ) // sort by artist { if ( artist1 == artist2 ) { if ( album1 == album2 ) { if ( discnumber1 == discnumber2 ) { if ( albumpos1 == albumpos2 ) return id1 < id2; return albumpos1 < albumpos2; } return discnumber1 < discnumber2; } return QString::localeAwareCompare( album1, album2 ) < 0; } return QString::localeAwareCompare( artist1, artist2 ) < 0; } // Sort by Composer const QString& composer1 = t1->composerSortname(); const QString& composer2 = t2->composerSortname(); if ( column == PlayableModel::Composer ) { if ( composer1 == composer2 ) { if ( album1 == album2 ) { if ( discnumber1 == discnumber2 ) { if ( albumpos1 == albumpos2 ) return id1 < id2; return albumpos1 < albumpos2; } return discnumber1 < discnumber2; } return QString::localeAwareCompare( album1, album2 ) < 0; } return QString::localeAwareCompare( composer1, composer2 ) < 0; } // Sort by Album if ( column == PlayableModel::Album ) // sort by album { if ( album1 == album2 ) { if ( discnumber1 == discnumber2 ) { if ( albumpos1 == albumpos2 ) return id1 < id2; return albumpos1 < albumpos2; } return discnumber1 < discnumber2; } return QString::localeAwareCompare( album1, album2 ) < 0; } // Lazy load these variables, they are not used before. unsigned int bitrate1 = 0, bitrate2 = 0; unsigned int mtime1 = 0, mtime2 = 0; unsigned int size1 = 0, size2 = 0; unsigned int year1 = 0, year2 = 0; float score1 = 0, score2 = 0; QString origin1; QString origin2; if ( !q1->results().isEmpty() ) { Tomahawk::result_ptr r = q1->results().first(); bitrate1 = r->bitrate(); mtime1 = r->modificationTime(); size1 = r->size(); year1 = r->track()->year(); score1 = q1->score(); origin1 = r->friendlySource().toLower(); } if ( !q2->results().isEmpty() ) { Tomahawk::result_ptr r = q2->results().first(); bitrate2 = r->bitrate(); mtime2 = r->modificationTime(); size2 = r->size(); year2 = r->track()->year(); score2 = q2->score(); origin2 = r->friendlySource().toLower(); } // Sort by bitrate if ( column == PlayableModel::Bitrate ) { if ( bitrate1 == bitrate2 ) return id1 < id2; return bitrate1 < bitrate2; } else if ( column == PlayableModel::Duration ) // sort by duration { unsigned int duration1 = t1->duration(); unsigned int duration2 = t2->duration(); if ( duration1 == duration2 ) return id1 < id2; return duration1 < duration2; } else if ( column == PlayableModel::Age ) // sort by mtime { if ( mtime1 == mtime2 ) return id1 < id2; return mtime1 < mtime2; } else if ( column == PlayableModel::Year ) // sort by release year { if ( year1 == year2 ) return id1 < id2; return year1 < year2; } else if ( column == PlayableModel::Filesize ) // sort by file size { if ( size1 == size2 ) return id1 < id2; return size1 < size2; } else if ( column == PlayableModel::Score ) // sort by file score { if ( score1 == score2 ) return id1 < id2; return score1 < score2; } else if ( column == PlayableModel::Origin ) // sort by file origin { if ( origin1 == origin2 ) return id1 < id2; return origin1 < origin2; } else if ( column == PlayableModel::AlbumPos ) // sort by album pos { if ( discnumber1 != discnumber2 ) { return discnumber1 < discnumber2; } else { if ( albumpos1 != albumpos2 ) return albumpos1 < albumpos2; } } const QString& lefts = t1->track(); const QString& rights = t2->track(); if ( lefts == rights ) return id1 < id2; return QString::localeAwareCompare( lefts, rights ) < 0; }
QMap< int, float > FuzzyIndex::search( const Tomahawk::query_ptr& query ) { QMutexLocker lock( &m_mutex ); QMap< int, float > resultsmap; try { if ( !m_luceneReader ) { if ( !IndexReader::indexExists( TomahawkUtils::appDataDir().absoluteFilePath( "tomahawk.lucene" ).toStdString().c_str() ) ) { qDebug() << Q_FUNC_INFO << "index didn't exist."; return resultsmap; } m_luceneReader = IndexReader::open( m_luceneDir ); m_luceneSearcher = _CLNEW IndexSearcher( m_luceneReader ); } float minScore; const TCHAR** fields = 0; MultiFieldQueryParser parser( fields, m_analyzer ); BooleanQuery* qry = _CLNEW BooleanQuery(); if ( query->isFullTextQuery() ) { QString escapedQuery = QString::fromWCharArray( parser.escape( DatabaseImpl::sortname( query->fullTextQuery() ).toStdWString().c_str() ) ); Term* term = _CLNEW Term( _T( "track" ), escapedQuery.toStdWString().c_str() ); Query* fqry = _CLNEW FuzzyQuery( term ); qry->add( fqry, true, BooleanClause::SHOULD ); term = _CLNEW Term( _T( "artist" ), escapedQuery.toStdWString().c_str() ); fqry = _CLNEW FuzzyQuery( term ); qry->add( fqry, true, BooleanClause::SHOULD ); term = _CLNEW Term( _T( "fulltext" ), escapedQuery.toStdWString().c_str() ); fqry = _CLNEW FuzzyQuery( term ); qry->add( fqry, true, BooleanClause::SHOULD ); minScore = 0.00; } else { QString track = QString::fromWCharArray( parser.escape( DatabaseImpl::sortname( query->track() ).toStdWString().c_str() ) ); QString artist = QString::fromWCharArray( parser.escape( DatabaseImpl::sortname( query->artist() ).toStdWString().c_str() ) ); // QString album = QString::fromWCharArray( parser.escape( query->album().toStdWString().c_str() ) ); Term* term = _CLNEW Term( _T( "track" ), track.toStdWString().c_str() ); Query* fqry = _CLNEW FuzzyQuery( term ); qry->add( fqry, true, BooleanClause::MUST ); term = _CLNEW Term( _T( "artist" ), artist.toStdWString().c_str() ); fqry = _CLNEW FuzzyQuery( term ); qry->add( fqry, true, BooleanClause::MUST ); minScore = 0.00; } Hits* hits = m_luceneSearcher->search( qry ); for ( uint i = 0; i < hits->length(); i++ ) { Document* d = &hits->doc( i ); float score = hits->score( i ); int id = QString::fromWCharArray( d->get( _T( "trackid" ) ) ).toInt(); if ( score > minScore ) { resultsmap.insert( id, score ); // tDebug() << "Index hit:" << id << score << QString::fromWCharArray( ((Query*)qry)->toString() ); } } delete hits; delete qry; } catch( CLuceneError& error ) { tDebug() << "Caught CLucene error:" << error.what(); Q_ASSERT( false ); } return resultsmap; }
void AudioEngine::playItem( Tomahawk::playlistinterface_ptr playlist, const Tomahawk::query_ptr& query ) { if ( query->resolvingFinished() ) { if ( query->numResults() && query->results().first()->isOnline() ) { playItem( playlist, query->results().first() ); return; } JobStatusView::instance()->model()->addJob( new ErrorStatusMessage( tr( "Sorry, Tomahawk couldn't find the track '%1' by %2" ).arg( query->track() ).arg( query->artist() ), 15 ) ); if ( isStopped() ) emit stopped(); // we do this so the original caller knows we couldn't find this track } else { Pipeline::instance()->resolve( query ); NewClosure( query.data(), SIGNAL( resolvingFinished( bool ) ), const_cast<AudioEngine*>(this), SLOT( playItem( Tomahawk::playlistinterface_ptr, Tomahawk::query_ptr ) ), playlist, query ); } }