Esempio n. 1
0
void
Scrobbler::trackStarted( const Tomahawk::result_ptr& track )
{
    Q_ASSERT( QThread::currentThread() == thread() );
//    qDebug() << Q_FUNC_INFO;

    if( m_reachedScrobblePoint )
    {
        m_reachedScrobblePoint = false;
        scrobble();
    }

    Tomahawk::InfoSystem::InfoCriteriaHash trackInfo;

    trackInfo["title"] = track->track();
    trackInfo["artist"] = track->artist()->name();
    trackInfo["album"] = track->album()->name();
    trackInfo["duration"] = QString::number( track->duration() );

    Tomahawk::InfoSystem::InfoSystem::instance()->pushInfo(
        s_scInfoIdentifier, Tomahawk::InfoSystem::InfoSubmitNowPlaying,
        QVariant::fromValue< Tomahawk::InfoSystem::InfoCriteriaHash >( trackInfo ) );

    m_scrobblePoint = ScrobblePoint( track->duration() / 2 );
}
Esempio n. 2
0
void
Scrobbler::trackStarted( const Tomahawk::result_ptr& track )
{
    Q_ASSERT( QThread::currentThread() == thread() );

    if ( m_reachedScrobblePoint )
    {
        m_reachedScrobblePoint = false;
        scrobble();
    }

    Tomahawk::InfoSystem::InfoStringHash trackInfo;
    trackInfo["title"] = track->track();
    trackInfo["artist"] = track->artist()->name();
    trackInfo["album"] = track->album()->name();
    trackInfo["duration"] = QString::number( track->duration() );
    trackInfo["albumpos"] = QString::number( track->albumpos() );

    QVariantMap playInfo;
    playInfo["trackinfo"] = QVariant::fromValue< Tomahawk::InfoSystem::InfoStringHash >( trackInfo );
    playInfo["private"] = TomahawkSettings::instance()->privateListeningMode();

    Tomahawk::InfoSystem::InfoPushData pushData (
        s_scInfoIdentifier, Tomahawk::InfoSystem::InfoSubmitNowPlaying,
        playInfo,
        Tomahawk::InfoSystem::PushNoFlag );

    Tomahawk::InfoSystem::InfoSystem::instance()->pushInfo( pushData );

    // liblastfm forces 0-length tracks to scrobble after 4 minutes, stupid.
    if ( track->duration() == 0 )
        m_scrobblePoint = ScrobblePoint( 30 );
    else
        m_scrobblePoint = ScrobblePoint( track->duration() / 2 );
}
Esempio n. 3
0
QVariantMap
MprisPlugin::metadata() const
{
    QVariantMap metadataMap;
    Tomahawk::result_ptr track = AudioEngine::instance()->currentTrack();
    if ( track )
    {
        metadataMap.insert( "mpris:trackid", QVariant::fromValue(QDBusObjectPath(QString( "/track/" ) + track->id().replace( "-", "" ))) );
        metadataMap.insert( "mpris:length", static_cast<qlonglong>(track->track()->duration()) * 1000000 );
        metadataMap.insert( "xesam:album", track->track()->album() );
        metadataMap.insert( "xesam:artist", QStringList( track->track()->artist() ) );
        metadataMap.insert( "xesam:title", track->track()->track() );

        // Only return art if tempfile exists, and if its name contains the same "artist_album_tomahawk_cover.png"
        if ( !m_coverTempFile.isEmpty() )
        {
            QFile coverFile( m_coverTempFile );
            if ( coverFile.exists() && coverFile.fileName().contains( track->track()->artist() + "_" + track->track()->album() + "_tomahawk_cover.png" ) )
                metadataMap.insert( "mpris:artUrl", QString( QUrl::fromLocalFile( m_coverTempFile ).toEncoded() ) );
        }
    }

    return metadataMap;
}
Esempio n. 4
0
QVariantMap
MprisPlugin::metadata() const
{
    QVariantMap metadataMap;
    Tomahawk::result_ptr track = AudioEngine::instance()->currentTrack();
    if( track )
    {
        metadataMap.insert( "mpris:trackid", QString( "/track/" ) + track->id().replace( "-", "" ) );
        metadataMap.insert( "mpris:length", track->duration() );
        metadataMap.insert( "xesam:album", track->album()->name() );
        metadataMap.insert( "xesam:artist", track->artist()->name() );
        metadataMap.insert( "xesam:title", track->track() );
    }

    return metadataMap;
}
void
QueueProxyModel::onPlaybackStarted( const Tomahawk::result_ptr& result )
{
    for ( int i = 0; i < rowCount(); i++ )
    {
        QModelIndex idx = index( i, 0 );
        PlayableItem* item = itemFromIndex( mapToSource( idx ) );
        if ( item && item->query() && ( item->query()->results().contains( result ) ||
                                        item->query()->track()->equals( result->track() ) ) )
        {
            removeIndex( idx );
            if ( !rowCount() )
                ViewManager::instance()->hideQueue();
        }
    }
}
Esempio n. 6
0
DownloadJob::DownloadJob( const Tomahawk::result_ptr& result, DownloadFormat format, bool tryResuming, DownloadJob::TrackState state )
    : m_state( state )
    , m_retries( 0 )
    , m_tryResuming( tryResuming )
    , m_reply( 0 )
    , m_file( 0 )
    , m_rcvdStamp( 0 )
    , m_rcvdEmit( 0 )
    , m_rcvdSize( 0 )
    , m_fileSize( 0 )
    , m_format( format )
    , m_track( result->track() )
    , m_result( result )
{
    m_finished = ( state == Finished );
}
void
TomahawkTrayIcon::setResult( const Tomahawk::result_ptr result )
{
    if ( m_currentTrack )
    {
        disconnect( m_currentTrack->track().data(), SIGNAL( socialActionsLoaded() ), this, SLOT( onSocialActionsLoaded() ) );
    }

    m_currentTrack = result;
    refreshToolTip();

    if ( result )
        connect( result->track().data(), SIGNAL( socialActionsLoaded() ), SLOT( onSocialActionsLoaded() ), Qt::UniqueConnection );

    onSocialActionsLoaded();
    onStopContinueAfterTrackChanged();
}
Esempio n. 8
0
bool
TrackProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const
{
    TrackModelItem* pi = itemFromIndex( sourceModel()->index( sourceRow, 0, sourceParent ) );
    if ( !pi )
        return false;

    const Tomahawk::query_ptr& q = pi->query();
    if( q.isNull() ) // uh oh? filter out invalid queries i guess
        return false;

    Tomahawk::result_ptr r;
    if ( q->numResults() )
        r = q->results().first();

    if ( !m_showOfflineResults && !r.isNull() && !r->collection()->source()->isOnline() )
        return false;

    if ( filterRegExp().isEmpty() )
        return true;

    QStringList sl = filterRegExp().pattern().split( " ", QString::SkipEmptyParts );
    foreach( QString s, sl )
    {
        s = s.toLower();
        if ( !r.isNull() )
        {
            if ( !r->artist()->name().toLower().contains( s ) &&
                 !r->album()->name().toLower().contains( s ) &&
                 !r->track().toLower().contains( s ) )
            {
                return false;
            }
        }
        else
        {
            if ( !q->artist().toLower().contains( s ) &&
                 !q->album().toLower().contains( s ) &&
                 !q->track().toLower().contains( s ) )
            {
                return false;
            }
        }
    }
// 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;
}
Esempio n. 10
0
void
MetadataEditor::loadResult( const Tomahawk::result_ptr& result )
{
    if ( result.isNull() )
        return;

    m_result = result;
    setEditable( result->collection() && result->collection()->source()->isLocal() );

    setTitle( result->track()->track() );
    setArtist( result->track()->artist() );
    setAlbum( result->track()->album() );
    setAlbumPos( result->track()->albumpos() );
    setDuration( result->track()->duration() );
    setYear( result->track()->year() );
    setBitrate( result->bitrate() );

    if ( result->collection() && result->collection()->source()->isLocal() )
    {
        QString furl = m_result->url();
        if ( furl.startsWith( "file://" ) )
            furl = furl.right( furl.length() - 7 );

        QFileInfo fi( furl );
        setFileName( fi.absoluteFilePath() );
        setFileSize( TomahawkUtils::filesizeToString( fi.size() ) );
    }

    setWindowTitle( result->track()->track() );

    if ( m_interface )
    {
        m_index = m_interface->indexOfResult( result );

        if ( m_index >= 0 )
            enablePushButtons();
    }
}
Esempio n. 11
0
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;
}
Esempio n. 12
0
void
Api_v1_5::playback( QxtWebRequestEvent* event, const QString& command )
{
    if ( command == "next")
    {
        JSON_REPLY( QMetaObject::invokeMethod( AudioEngine::instance(), "next", Qt::QueuedConnection ) , "Skipping to the next track failed." );
    }
    else if ( command == "previous" )
    {
        JSON_REPLY( QMetaObject::invokeMethod( AudioEngine::instance(), "previous", Qt::QueuedConnection ), "Rewinding to the previous track failed." );
    }
    else if ( command == "playpause" )
    {
        JSON_REPLY( QMetaObject::invokeMethod( AudioEngine::instance(), "playPause", Qt::QueuedConnection ), "Play/Pause failed." );
    }
    else if ( command == "play" )
    {
        JSON_REPLY( QMetaObject::invokeMethod( AudioEngine::instance(), "play", Qt::QueuedConnection ), "Starting the playback failed." );
    }
    else if ( command == "pause" )
    {
        JSON_REPLY( QMetaObject::invokeMethod( AudioEngine::instance(), "pause", Qt::QueuedConnection ), "Pausing the current track failed." );
    }
    else if ( command == "stop" )
    {
        JSON_REPLY( QMetaObject::invokeMethod( AudioEngine::instance(), "stop", Qt::QueuedConnection ), "Stopping the current track failed." );
    }
    else if ( command == "lowervolume" )
    {
        JSON_REPLY( QMetaObject::invokeMethod( AudioEngine::instance(), "lowerVolume", Qt::QueuedConnection ), "Lowering volume failed." );
    }
    else if ( command == "raisevolume" )
    {
        JSON_REPLY( QMetaObject::invokeMethod( AudioEngine::instance(), "raiseVolume", Qt::QueuedConnection ), "Raising volume failed." );
    }
    else if ( command == "currenttrack" )
    {
        QByteArray json;
        Tomahawk::result_ptr currentTrack =  AudioEngine::instance()->currentTrack();

        if ( currentTrack.isNull() )
        {
            json = "{ \"playing\": false }";
        }
        else
        {
            QVariantMap trackInfo;
            trackInfo.insert( "playing", true );
            trackInfo.insert( "paused", AudioEngine::instance()->isPaused() );
            trackInfo.insert( "position", AudioEngine::instance()->currentTime() / 1000 );
            trackInfo.insert( "bitrate", currentTrack->bitrate() );
            if ( currentTrack->resolvedBy() ) {
                QString resolverName = currentTrack->resolvedBy()->name();
                trackInfo.insert( "resolvedBy", resolverName );
            } else {
                trackInfo.insert( "resolvedBy", "<unknown resolver>" );
            }
            trackInfo.insert( "score", currentTrack->score() );
            trackInfo.insert( "album", currentTrack->track()->album() );
            trackInfo.insert( "albumpos", currentTrack->track()->albumpos() );
            trackInfo.insert( "artist", currentTrack->track()->artist() );
            trackInfo.insert( "duration", currentTrack->track()->duration() );
            trackInfo.insert( "track", currentTrack->track()->track() );

            bool ok;
            json = TomahawkUtils::toJson( trackInfo, &ok );
            Q_ASSERT( ok );
        }

        QxtWebPageEvent * e = new QxtWebPageEvent( event->sessionID, event->requestID, json );
        e->headers.insert( "Access-Control-Allow-Origin", "*" );
        e->contentType = "application/json";
        m_service->postEvent( e );
    }
    else if ( command == "volume" )
    {
        QByteArray json = QString( "{ \"result\": \"ok\", \"volume\": %1}" ).arg( AudioEngine::instance()->volume() ).toUtf8();
        QxtWebPageEvent * e = new QxtWebPageEvent( event->sessionID, event->requestID, json );
        e->headers.insert( "Access-Control-Allow-Origin", "*" );
        e->contentType = "application/json";
        m_service->postEvent( e );
    }
    else
    {
        m_service->sendJsonError( event, "No such playback command." );
    }
}