Skip to content
WebInterface.cpp 4.77 KiB
Newer Older
/*
 * 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"

struct WebInterface::internal
{
    LoginInterface* loginUI;
};

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);
    setTitle("Arbitrateor - Audio Jukebox");
    priv_int->playerUI = new PlayerInterface(this);
    priv_int->playerUI->hide();
    priv_int->loginUI = new LoginInterface(this);
    root()->addWidget(priv_int->playerUI);
    priv_int->loginUI->animateShow(Wt::WAnimation(Wt::WAnimation::AnimationEffect::SlideInFromTop));
    priv_int->loginUI->checkSessionValidity();
Kevin Whitaker's avatar
Kevin Whitaker committed
    if(currentUser.isAdmin) priv_int->playerUI->mainDisplay->addTab(priv_int->playerUI->adminPane, "Admin", Wt::WTabWidget::LazyLoading);
    notificationSlot.setJavaScript(getNotificationJs(""));
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);
}

void WebInterface::loginCompleted()
{
    //Login completed, now bring up main inteface.
    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);
    priv_int->playerUI->updateVoteableTracksFromServer(GroovePlayerMgr::getInstance()->currentVoteBatch);
    priv_int->playerUI->updateVoteTrackState();
void WebInterface::playPauseActionFromServer(User userPausing, bool pause)
    priv_int->playerUI->isPaused = pause;
    priv_int->playerUI->playpauseUpdated();
void WebInterface::songChangedFromServer(AudioTrack nextTrack)
    priv_int->playerUI->updateDetailsFromServer(nextTrack);
    priv_int->playerUI->requestPane->updateRequestButtonState();
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();
void WebInterface::skipVoteUpdateFromServer(User userVoting, bool forSkip)
    if(!forSkip) priv_int->playerUI->updateSkipDeniedFromServer();
void WebInterface::voteNextSongFromServer(User userVoting, AudioTrack trackVoted)
    priv_int->playerUI->updateVoteCount(trackVoted);
void WebInterface::voteNextPollClosedFromServer(AudioTrack winner)
    priv_int->playerUI->updateVotingEnded(winner);
void WebInterface::voteTracksUpdatedFromServer(std::list<AudioTrack> voteableTracks)
    priv_int->playerUI->updateVoteableTracksFromServer(voteableTracks);
    priv_int->playerUI->updateVoteTrackState();

std::string WebInterface::getNotificationJs(std::string notificationText)
{
    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;}");