Newer
Older
Kevin Whitaker
committed
/*
* Copyright (C) 2017 Kevin Whitaker <eyecreate@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "WebInterface.h"
Kevin Whitaker
committed
#include "ui/LoginInterface.h"
Kevin Whitaker
committed
#include "ui/PlayerInterface.h"
Kevin Whitaker
committed
#include <Wt/WText>
Kevin Whitaker
committed
#include <Wt/WAnimation>
#include <Wt/WBootstrapTheme>
Kevin Whitaker
committed
#include <regex>
Kevin Whitaker
committed
struct WebInterface::internal
{
LoginInterface* loginUI;
Kevin Whitaker
committed
PlayerInterface* playerUI;
Kevin Whitaker
committed
};
WebInterface::WebInterface(const Wt::WEnvironment& env) : Wt::WApplication(env)
{
priv_int = new internal;
this->useStyleSheet(Wt::WLink("/resources/font-awesome/css/font-awesome.min.css"));
setTheme(new Wt::WBootstrapTheme());
((Wt::WBootstrapTheme*)theme())->setVersion(Wt::WBootstrapTheme::Version3);
Kevin Whitaker
committed
setTitle("Arbitrateor - Audio Jukebox");
enableUpdates(true);
Kevin Whitaker
committed
priv_int->playerUI = new PlayerInterface(this);
priv_int->playerUI->hide();
Kevin Whitaker
committed
priv_int->loginUI = new LoginInterface(this);
Kevin Whitaker
committed
root()->addWidget(priv_int->playerUI);
Kevin Whitaker
committed
root()->addWidget(priv_int->loginUI);
priv_int->loginUI->animateShow(Wt::WAnimation(Wt::WAnimation::AnimationEffect::SlideInFromTop));
priv_int->loginUI->checkSessionValidity();
notificationSlot.setJavaScript(getNotificationJs(""));
Kevin Whitaker
committed
}
Kevin Whitaker
committed
WebInterface::~WebInterface()
{
delete priv_int->playerUI;
delete priv_int->loginUI;
delete priv_int;
}
void WebInterface::createUser(Wt::Dbo::Session* session, std::string username, std::string rawPassword, bool isAdmin)
{
priv_int->loginUI->createUser(session, username, rawPassword, isAdmin);
}
Kevin Whitaker
committed
void WebInterface::loginCompleted()
{
//Login completed, now bring up main inteface.
Kevin Whitaker
committed
priv_int->loginUI->animateHide(Wt::WAnimation(Wt::WAnimation::AnimationEffect::Fade));
priv_int->playerUI->animateShow(Wt::WAnimation(Wt::WAnimation::AnimationEffect::Fade, Wt::WAnimation::TimingFunction::EaseIn, 500));
priv_int->playerUI->updateDetailsFromServer(GroovePlayerMgr::getInstance()->currentTrack);
Kevin Whitaker
committed
priv_int->playerUI->updateVoteableTracksFromServer(GroovePlayerMgr::getInstance()->currentVoteBatch);
priv_int->playerUI->updateVoteTrackState();
Kevin Whitaker
committed
if(currentUser.canUpload) priv_int->playerUI->mainDisplay->addTab(priv_int->playerUI->uploadPane, "Upload Track", Wt::WTabWidget::LazyLoading);
if(currentUser.isAdmin) priv_int->playerUI->mainDisplay->addTab(priv_int->playerUI->adminPane, "Admin", Wt::WTabWidget::LazyLoading);
}
Kevin Whitaker
committed
void WebInterface::playPauseActionFromServer(User userPausing, bool pause)
{
Kevin Whitaker
committed
priv_int->playerUI->isPaused = pause;
priv_int->playerUI->playpauseUpdated();
triggerUpdate();
}
Kevin Whitaker
committed
void WebInterface::songChangedFromServer(AudioTrack nextTrack)
{
priv_int->playerUI->updateDetailsFromServer(nextTrack);
Kevin Whitaker
committed
priv_int->playerUI->requestPane->updateRequestButtonState();
triggerUpdate();
}
Kevin Whitaker
committed
void WebInterface::skipVotedFromServer(User userRequestingToSkipCurrentTrack)
{
priv_int->playerUI->updateSkipRequestedFromServer();
//show notification
notificationSlot.setJavaScript(getNotificationJs(userRequestingToSkipCurrentTrack.username+" has requested to skip "+GroovePlayerMgr::getInstance()->currentTrack.trackName+"."));
notificationSlot.exec();
triggerUpdate();
}
Kevin Whitaker
committed
void WebInterface::skipVoteUpdateFromServer(User userVoting, bool forSkip)
{
if(!forSkip) priv_int->playerUI->updateSkipDeniedFromServer();
triggerUpdate();
Kevin Whitaker
committed
}
Kevin Whitaker
committed
void WebInterface::voteNextSongFromServer(User userVoting, AudioTrack trackVoted)
priv_int->playerUI->updateVoteCount(trackVoted);
triggerUpdate();
}
Kevin Whitaker
committed
void WebInterface::voteNextPollClosedFromServer(AudioTrack winner)
Kevin Whitaker
committed
priv_int->playerUI->updateVotingEnded(winner);
Kevin Whitaker
committed
Kevin Whitaker
committed
void WebInterface::voteTracksUpdatedFromServer(std::list<AudioTrack> voteableTracks)
Kevin Whitaker
committed
{
Kevin Whitaker
committed
priv_int->playerUI->updateVoteableTracksFromServer(voteableTracks);
priv_int->playerUI->updateVoteTrackState();
Kevin Whitaker
committed
triggerUpdate();
}
std::string WebInterface::getNotificationJs(std::string notificationText)
{
Kevin Whitaker
committed
std::string escapedText = std::regex_replace(notificationText,std::regex("\""),"'");
return std::string("function notifyMe(a,b){var c=\"")+escapedText+std::string("\";if('Notification'in window)if('granted'===Notification.permission){new Notification(c)}else'denied'!==Notification.permission&&Notification.requestPermission(function(a){if('granted'===a){new Notification(c)}});else;}");