Пример #1
0
QSpotifyAlbum::QSpotifyAlbum(sp_album *album)
    : QSpotifyObject(true)
    , m_isAvailable(false)
    , m_year(0)
    , m_type(Unknown)
{
    connect(this, SIGNAL(dataChanged()), this, SIGNAL(albumDataChanged()));
    sp_album_add_ref(album);
    m_sp_album = album;
    metadataUpdated();
}
QSpotifyPlaylistContainer::QSpotifyPlaylistContainer(sp_playlistcontainer *container)
    : QSpotifyObject(true)
    , m_updateEventPosted(false)
{
    m_container = container;
    g_containerObjects.insert(container, this);
    m_callbacks = new sp_playlistcontainer_callbacks;
    m_callbacks->container_loaded = callback_container_loaded;
    m_callbacks->playlist_added = callback_playlist_added;
    m_callbacks->playlist_moved = callback_playlist_moved;
    m_callbacks->playlist_removed = callback_playlist_removed;
    sp_playlistcontainer_add_callbacks(m_container, m_callbacks, 0);
    connect(QSpotifySession::instance(), SIGNAL(offlineModeChanged()), this, SLOT(updatePlaylists()));

    metadataUpdated();
}
bool QSpotifyPlaylistContainer::event(QEvent *e)
{
    if (e->type() == QEvent::User) {
        metadataUpdated();
        e->accept();
        return true;
    } else if (e->type() == QEvent::User + 1) {
        // PlaylistAdded event
        QSpotifyPlaylistAddedEvent *ev = static_cast<QSpotifyPlaylistAddedEvent *>(e);
        addPlaylist(ev->playlist(), ev->position());
        emit dataChanged();
        postUpdateEvent();
        e->accept();
        return true;
    } else if (e->type() == QEvent::User + 2) {
        // PlaylistRemoved event
        QSpotifyPlaylistRemovedEvent *ev = static_cast<QSpotifyPlaylistRemovedEvent *>(e);
        int i = ev->position();
        if (i >= 0 && i < m_playlists.count()) {
            QSpotifyPlaylist *pl = m_playlists.takeAt(i);
            delete pl;
            emit dataChanged();
        }
        postUpdateEvent();
        e->accept();
        return true;
    } else if (e->type() == QEvent::User + 3) {
        // PlaylistMoved event
        QSpotifyPlaylistMovedEvent *ev = static_cast<QSpotifyPlaylistMovedEvent *>(e);
        int i = ev->position();
        int newpos = ev->newPosition();
        if (i >= 0 && i < m_playlists.count()) {
            QSpotifyPlaylist *pl = m_playlists.takeAt(i);
            m_playlists.insert(newpos > i ? newpos - 1 : newpos, pl);
            emit dataChanged();
        }
        postUpdateEvent();
        e->accept();
        return true;
    } else if (e->type() == QEvent::User + 4) {
        updatePlaylists();
        m_updateEventPosted = false;
        e->accept();
        return true;
    }
    return QSpotifyObject::event(e);
}
Пример #4
0
Spotify::Error Spotify::Session::init( const Spotify::SessionConfig& config )
{
    // Set callback functions:
    sp_session_callbacks g_callbacks = {
        &Spotify::Session::loggedInCallback,
        &Spotify::Session::loggedOutCallback,
        &Spotify::Session::metadataUpdatedCallback,
        &Spotify::Session::connectionErrorCallback,
        &Spotify::Session::messageToUserCallback,
        &Spotify::Session::notifyMainThreadCallback,
        &Spotify::Session::musicDeliveryCallback,
        &Spotify::Session::playTokenLostCallback,
        &Spotify::Session::logMessageCallback,
        &Spotify::Session::endOfTrackCallback };

    // Set config:
    const QByteArray cacheLocation = config.cacheLocation().toUtf8();
    const QByteArray settingsLocation = config.settingsLocation().toUtf8();
    const QByteArray userAgent = config.userAgent().toUtf8();
    m_config.api_version = SPOTIFY_API_VERSION;
    m_config.cache_location = cacheLocation.constData();
    m_config.settings_location = settingsLocation.constData();
    m_config.application_key = g_appkey;
    m_config.application_key_size = g_appkey_size;
    m_config.user_agent = userAgent.constData();
    m_config.callbacks = &g_callbacks;

    // Initiate session:
    m_error = sp_session_init( &m_config, &m_session );
    if( m_error.isError() )
        qDebug() << "FATAL ERROR: Could not initiate session";

    // Open audio device
    audio_init(&m_audiofifo);

    // Set up connections:
    connect( this, SIGNAL(metadataUpdated()),
             this, SLOT(updatePlaylistsMetadataSlot()) );
    qDebug() << "Session created";
    return m_error;
}
Пример #5
0
bool QSpotifySession::event(QEvent *e)
{
    if (e->type() == NotifyMainThreadEventType) {
        qDebug() << "Process spotify event";
        processSpotifyEvents();
        e->accept();
        return true;
    } else if (e->type() == QEvent::Timer) {
        qDebug() << "Timer, start spotify events";
        QTimerEvent *te = static_cast<QTimerEvent *>(e);
        if (te->timerId() == m_timerID) {
            processSpotifyEvents();
            e->accept();
            return true;
        }
    } else if (e->type() == ConnectionErrorEventType) {
        qDebug() << "Connection error";
        QSpotifyConnectionErrorEvent *ev = static_cast<QSpotifyConnectionErrorEvent *>(e);
        setConnectionError(ConnectionError(ev->error()), QString::fromUtf8(sp_error_message(ev->error())));
        e->accept();
        return true;
    } else if (e->type() == MetaDataEventType) {
        qDebug() << "Meta data";
        emit metadataUpdated();
        e->accept();
        return true;
    } else if (e->type() == EndOfTrackEventType) {
        qDebug() << "End track";
        m_trackChangedAutomatically = true;
        playNext();
        e->accept();
        return true;
    } else if (e->type() == StopEventType) {
        qDebug() << "Stop";
        stop();
        e->accept();
        return true;
    } else if (e->type() == TrackProgressEventType) {
        qDebug() << "Track progress";
        if(!m_isPlaying) {
            e->accept();
            return true;
        }
        // Track progressed
        QSpotifyTrackProgressEvent *ev = static_cast<QSpotifyTrackProgressEvent *>(e);
        int currentTrackPositionDelta = ev->delta();
        if (m_previousTrackRemaining > 0) {
            // We're still playing the previous back from our buffer
            int fromPreviousTrack = qMin(currentTrackPositionDelta, m_previousTrackRemaining);
            currentTrackPositionDelta -= fromPreviousTrack;
            m_previousTrackRemaining -= fromPreviousTrack;
        }

        m_currentTrackPosition += currentTrackPositionDelta;
        m_currentTrackPlayedDuration += currentTrackPositionDelta;
        emit currentTrackPositionChanged();
        e->accept();
        return true;
    } else if (e->type() == SendImageRequestEventType) {
        qDebug() << "Send image request";
        QSpotifyRequestImageEvent *ev = static_cast<QSpotifyRequestImageEvent *>(e);
        sendImageRequest(ev->imageId());
        e->accept();
        return true;
    } else if (e->type() == ReceiveImageRequestEventType) {
        qDebug() << "Receive image request";
        QSpotifyReceiveImageEvent *ev = static_cast<QSpotifyReceiveImageEvent *>(e);
        receiveImageResponse(ev->image());
        e->accept();
        return true;
    } else if (e->type() == PlayTokenLostEventType) {
        qDebug() << "Play token lost";
        emit playTokenLost();
        pause();
        e->accept();
        return true;
    } else if (e->type() == LoggedInEventType) {
        qDebug() << "Logged in 1";
        onLoggedIn();
        e->accept();
        return true;
    } else if (e->type() == LoggedOutEventType) {
        qDebug() << "Logged out";
        onLoggedOut();
        e->accept();
        return true;
    } else if (e->type() == OfflineErrorEventType) {
        qDebug() << "Offline error";
        QSpotifyOfflineErrorEvent *ev = static_cast<QSpotifyOfflineErrorEvent *>(e);
        m_offlineErrorMessage = QString::fromUtf8(sp_error_message(ev->error()));
        emit offlineErrorMessageChanged();
        e->accept();
        return true;
    } else if (e->type() == ScrobbleLoginErrorEventType) {
        qDebug() << "Scrobble login error";
        m_lfmLoggedIn = false;
        emit lfmLoggedInChanged();
        emit lfmLoginError();
        e->accept();
        return true;
    } else if (e->type() == ConnectionStateUpdateEventType) {
        qDebug() << "Connectionstate update event";
        setConnectionStatus(ConnectionStatus(sp_session_connectionstate(m_sp_session)));
        if (m_offlineMode && m_connectionStatus == LoggedIn) {
            setConnectionRules(m_connectionRules | AllowNetwork);
            setConnectionRules(m_connectionRules & ~AllowNetwork);
        }
        e->accept();
        return true;
    }
    return QObject::event(e);
}
Пример #6
0
void SpotWorker::emitMetadataUpdated(sp_session * session)
{
    emit metadataUpdated(session);
}