SmoozikSimplestClientWindow::SmoozikSimplestClientWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::SmoozikSimplestClientWindow) { ui->setupUi(this); // Initialize SmoozikManager smoozikManager = new SmoozikManager(APIKEY, SECRET, SmoozikManager::XML, false, this); smoozikPlaylist = new SmoozikPlaylist; connect(smoozikManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(processNetworkReply(QNetworkReply*))); // Initialize music directory #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) _dirName = QDesktopServices::storageLocation(QDesktopServices::MusicLocation); #else _dirName = QStandardPaths::writableLocation(QStandardPaths::MusicLocation); #endif // Initialize playlist filler ui->setupUi(this); smoozikPlaylistFillerThread = new QThread(); smoozikPlaylistFiller = new SmoozikPlaylistFiller(smoozikPlaylist); smoozikPlaylistFiller->moveToThread(smoozikPlaylistFillerThread); connect(smoozikPlaylistFiller, SIGNAL(trackFound(QString,QString,QString,QString,uint)), this, SLOT(addTrackToPlaylist(QString,QString,QString,QString,uint))); connect(smoozikPlaylistFiller, SIGNAL(tracksRetrieved()), this, SIGNAL(tracksRetrieved())); connect(smoozikPlaylistFiller, SIGNAL(noTrackRetrieved()), this, SLOT(noTrackRetrievedMessage())); connect(smoozikPlaylistFiller, SIGNAL(maxPlaylistSizeReached()), this, SLOT(maxPlaylistSizeReachedMessage())); connect(smoozikPlaylistFillerThread, SIGNAL(started()), smoozikPlaylistFiller, SLOT(fillPlaylist())); connect(smoozikPlaylistFiller, SIGNAL(finished()), smoozikPlaylistFillerThread, SLOT(quit()), Qt::DirectConnection); // Initialize player #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) player = new Phonon::MediaObject(this); Phonon::AudioOutput *audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this); Phonon::createPath(player, audioOutput); connect(player, SIGNAL(currentSourceChanged(Phonon::MediaSource)), this, SLOT(updateTrackLabels())); connect(player, SIGNAL(stateChanged(Phonon::State,Phonon::State)), this, SLOT(playerStateChanged())); #else player = new QMediaPlayer(this); player->setPlaylist(new QMediaPlaylist(player)); player->playlist()->setPlaybackMode(QMediaPlaylist::Sequential); connect(player, SIGNAL(currentMediaChanged(QMediaContent)), this, SLOT(updateTrackLabels())); connect(player, SIGNAL(stateChanged(QMediaPlayer::State)), this, SLOT(playerStateChanged())); #endif connect(ui->playButton, SIGNAL(clicked()), player, SLOT(play())); connect(ui->pauseButton, SIGNAL(clicked()), player, SLOT(pause())); connect(this, SIGNAL(currentTrackSet()), this, SLOT(updateTrackLabels())); connect(this, SIGNAL(nextTrackSet()), this, SLOT(updateTrackLabels())); // Initialize main state machine which controls what is displayed QStateMachine *mainStateMachine = new QStateMachine(this); QState *mainState = new QState(mainStateMachine); QState *loginState = new QState(mainState); QState *startPartyState = new QState(mainState); QState *connectedState = new QState(mainState); QState *retrieveTracksState = new QState(connectedState); QState *sendPlaylistState = new QState(connectedState); QState *getTopTracksState = new QState(connectedState); QState *partyState = new QState(connectedState); QState *waitingState = new QState(partyState); QState *sendCurrentTrackState = new QState(partyState); QState *sendNextTrackState = new QState(partyState); QStateMachine *playerStateMachine = new QStateMachine(this); QState *playerState = new QState(playerStateMachine); QState *playingState = new QState(playerState); QState *pausedState = new QState(playerState); // Define state initial states and transitions mainStateMachine->setInitialState(mainState); mainState->setInitialState(loginState); connectedState->setInitialState(retrieveTracksState); partyState->setInitialState(waitingState); playerStateMachine->setInitialState(playerState); playerState->setInitialState(pausedState); mainState->addTransition(this, SIGNAL(disconnected()), loginState); loginState->addTransition(this, SIGNAL(loggedIn()), startPartyState); startPartyState->addTransition(this, SIGNAL(partyStarted()), connectedState); connectedState->addTransition(ui->changePlaylistButton, SIGNAL(clicked()), retrieveTracksState); retrieveTracksState->addTransition(this, SIGNAL(tracksRetrieved()), sendPlaylistState); sendPlaylistState->addTransition(this, SIGNAL(playlistSent()), getTopTracksState); getTopTracksState->addTransition(this, SIGNAL(currentTrackSet()), sendCurrentTrackState); sendCurrentTrackState->addTransition(this, SIGNAL(currentTrackSent()), getTopTracksState); getTopTracksState->addTransition(this, SIGNAL(nextTrackSet()), sendNextTrackState); sendNextTrackState->addTransition(this, SIGNAL(nextTrackSent()), waitingState); #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) waitingState->addTransition(player, SIGNAL(currentSourceChanged(Phonon::MediaSource)), sendCurrentTrackState); #else waitingState->addTransition(player, SIGNAL(currentMediaChanged(QMediaContent)), sendCurrentTrackState); #endif playerState->addTransition(this, SIGNAL(playing()), playingState); playerState->addTransition(this, SIGNAL(paused()), pausedState); // Define state properties loginState->assignProperty(this, "state", Login); loginState->assignProperty(ui->stackedWidget, "currentIndex", ui->stackedWidget->indexOf(ui->loginPage)); loginState->assignProperty(ui->loginButton, "enabled", true); loginState->assignProperty(ui->disconnectButton, "visible", false); loginState->assignProperty(ui->changePlaylistButton, "visible", false); loginState->assignProperty(ui->usernameLineEdit, "enabled", true); loginState->assignProperty(ui->passwordLineEdit, "enabled", true); loginState->assignProperty(ui->loginStateLabel, "text", QString()); startPartyState->assignProperty(this, "state", StartParty); startPartyState->assignProperty(ui->loginStateLabel, "text", tr("Starting party...")); startPartyState->assignProperty(ui->disconnectButton, "visible", false); startPartyState->assignProperty(ui->changePlaylistButton, "visible", false); connectedState->assignProperty(ui->disconnectButton, "visible", true); retrieveTracksState->assignProperty(ui->stackedWidget, "currentIndex", ui->stackedWidget->indexOf(ui->loadingPage)); retrieveTracksState->assignProperty(ui->loginStateLabel, "text", tr("Connected")); retrieveTracksState->assignProperty(ui->loadingLabel, "text", tr("Retrieving tracks...")); retrieveTracksState->assignProperty(ui->changePlaylistButton, "visible", false); sendPlaylistState->assignProperty(this, "state", SendPlaylist); sendPlaylistState->assignProperty(ui->loadingLabel, "text", tr("Sending playlist...")); sendPlaylistState->assignProperty(ui->changePlaylistButton, "visible", true); getTopTracksState->assignProperty(this, "state", GetTopTracks); getTopTracksState->assignProperty(ui->loadingLabel, "text", tr("Get top tracks...")); getTopTracksState->assignProperty(ui->nextButton, "enabled", false); getTopTracksState->assignProperty(ui->changePlaylistButton, "visible", true); partyState->assignProperty(ui->stackedWidget, "currentIndex", ui->stackedWidget->indexOf(ui->playerPage)); partyState->assignProperty(ui->changePlaylistButton, "visible", true); sendCurrentTrackState->assignProperty(this, "state", SendCurrentTrack); sendCurrentTrackState->assignProperty(ui->nextButton, "enabled", false); sendNextTrackState->assignProperty(this, "state", SendNextTrack); sendNextTrackState->assignProperty(ui->nextButton, "enabled", false); waitingState->assignProperty(ui->nextButton, "enabled", true); playingState->assignProperty(ui->playButton, "visible", false); playingState->assignProperty(ui->pauseButton, "visible", true); pausedState->assignProperty(ui->playButton, "visible", true); pausedState->assignProperty(ui->pauseButton, "visible", false); // Connect states and actions connect(startPartyState, SIGNAL(entered()), this, SLOT(startParty())); connect(retrieveTracksState, SIGNAL(entered()), this, SLOT(retrieveTracksDialog())); connect(sendPlaylistState, SIGNAL(entered()), this, SLOT(sendPlaylist())); connect(getTopTracksState, SIGNAL(entered()), this, SLOT(getTopTracks())); connect(sendCurrentTrackState, SIGNAL(entered()), this, SLOT(sendCurrentTrack())); connect(sendNextTrackState, SIGNAL(entered()), this, SLOT(sendNextTrack())); // Connect gui and actions connect(ui->usernameLineEdit, SIGNAL(returnPressed()), this, SLOT(submitLogin())); connect(ui->passwordLineEdit, SIGNAL(returnPressed()), this, SLOT(submitLogin())); connect(ui->loginButton, SIGNAL(clicked()), this, SLOT(submitLogin())); connect(ui->nextButton, SIGNAL(clicked()), this, SLOT(nextTrack())); connect(ui->disconnectButton, SIGNAL(clicked()), this, SLOT(disconnect())); // Start state machine mainStateMachine->start(); playerStateMachine->start(); }
void BirthdayParty::startParty() { QTime time = QTime::currentTime(); emit partyStarted(time); }
void SmoozikSimplestClientWindow::getNextTrackInfo(QString *localId, QString *name, QString *artist, QString *album) { *localId = QString(); *name = QString(); *artist = QString(); *album = QString(); #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) if (!player->queue().isEmpty()) { *localId = player->queue().at(0).url().toLocalFile(); #else if (player->playlist()->currentIndex() >= 0 && player->playlist()->mediaCount() > player->playlist()->currentIndex() + 1) { *localId = player->playlist()->media(player->playlist()->currentIndex() + 1).canonicalUrl().toLocalFile(); #endif TagLib::FileRef mediaFileRef(QFile::encodeName(*localId).constData()); if (!mediaFileRef.isNull()) { *name = TStringToQString(mediaFileRef.tag()->title()); *artist = TStringToQString(mediaFileRef.tag()->artist()); *album = TStringToQString(mediaFileRef.tag()->album()); } } } void SmoozikSimplestClientWindow::processNetworkReply(QNetworkReply *reply) { QString path = reply->url().path(); SmoozikXml xml(reply); if (xml.error() != 0) { error(xml.errorMsg()); } else { // Process different cases of request if (path.endsWith("login", Qt::CaseInsensitive) && state() == Login) { //Retrieve sessionKey smoozikManager->setSessionKey(xml["sessionKey"].toString()); emit loggedIn(); } else if (path.endsWith("startParty", Qt::CaseInsensitive) && state() == StartParty) { emit partyStarted(); } else if (path.endsWith("sendPlaylist", Qt::CaseInsensitive) && state() == SendPlaylist) { emit playlistSent(); } else if (path.endsWith("getTopTracks", Qt::CaseInsensitive) && state() == GetTopTracks) { // Set mediaPlaylist from top tracks. SmoozikPlaylist topTracksPlaylist(xml["tracks"].toList()); #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) if (player->queue().isEmpty()) { if(player->currentSource().type() != Phonon::MediaSource::LocalFile) { player->setCurrentSource(Phonon::MediaSource(topTracksPlaylist.value(0)->localId())); emit currentTrackSet(); } else { player->enqueue(Phonon::MediaSource(topTracksPlaylist.value(0)->localId())); emit nextTrackSet(); } } #else if (player->playlist()->mediaCount() < 2 || player->playlist()->currentIndex() >= player->playlist()->mediaCount() - 2) { player->playlist()->addMedia(QUrl::fromLocalFile(topTracksPlaylist.value(0)->localId())); if(player->playlist()->mediaCount() == 1 || player->playlist()->currentIndex() == player->playlist()->mediaCount() - 1) { emit currentTrackSet(); } else { emit nextTrackSet(); } } #endif } else if (path.endsWith("setTrack", Qt::CaseInsensitive) && state() == SendCurrentTrack) { emit currentTrackSent(); } else if (path.endsWith("setTrack", Qt::CaseInsensitive) && state() == SendNextTrack) { emit nextTrackSent(); } } }