/*
* Copyright (C) 2013 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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 3 of the License, or
* (at your option) any later version.
*
* LMS 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 LMS. If not, see .
*/
#ifndef LMS_APPLICATION_HPP
#define LMS_APPLICATION_HPP
#include
#include
#include "database/Database.hpp"
#include "scanner/MediaScanner.hpp"
#include "LmsApplicationGroup.hpp"
namespace Database {
class Artist;
class Cluster;
class Release;
class User;
}
namespace UserInterface {
class AudioResource;
class ImageResource;
class Auth;
// Events that can be listen from anywhere in the application
struct Events
{
// Events relative to group
Wt::Signal appOpen;
Wt::Signal appClosed;
// A track is being loaded
Wt::Signal trackLoaded;
boost::optional lastLoadedTrackId;
// Unload current track
Wt::Signal<> trackUnloaded;
// Database events
Wt::Signal dbScanned;
Wt::Signal dbScanInProgress;
Wt::Signal dbScanScheduled;
};
// Used to classify the message sent to the user
enum class MsgType
{
Success,
Info,
Warning,
Danger,
};
class LmsApplication : public Wt::WApplication
{
public:
LmsApplication(const Wt::WEnvironment& env, std::unique_ptr dbSession, LmsApplicationGroupContainer& appGroups);
static std::unique_ptr create(const Wt::WEnvironment& env, Database::Database& db, LmsApplicationGroupContainer& appGroups);
static LmsApplication* instance();
// Session application data
std::shared_ptr getImageResource() { return _imageResource; }
std::shared_ptr getAudioResource() { return _audioResource; }
Database::Session& getDbSession() { return *_dbSession.get();}
Wt::Dbo::ptr getUser() const;
bool isUserAdmin() const; // user must be logged in prior this call
bool isUserDemo() const; // user must be logged in prior this call
std::string getUserLoginName() const; // user must be logged in prior this call
Events& getEvents() { return _events; }
// Utils
void goHome();
void goHomeAndQuit();
void post(std::function func);
void notifyMsg(MsgType type, const Wt::WString& message, std::chrono::milliseconds duration = std::chrono::milliseconds(4000));
static Wt::WLink createArtistLink(Wt::Dbo::ptr artist);
static std::unique_ptr createArtistAnchor(Wt::Dbo::ptr artist, bool addText = true);
static Wt::WLink createReleaseLink(Wt::Dbo::ptr release);
static std::unique_ptr createReleaseAnchor(Wt::Dbo::ptr release, bool addText = true);
static std::unique_ptr createCluster(Wt::Dbo::ptr cluster, bool canDelete = false);
// Signal emitted just before the session ends (user may already be logged out)
Wt::Signal<>& preQuit() { return _preQuit; }
private:
LmsApplicationGroup& getApplicationGroup();
// Signal slots
void handleUserLoggedOut();
void handleUserLoggedIn(Database::IdType userId);
void notify(const Wt::WEvent& event) override;
void finalize() override;
void createHome();
Wt::Signal<> _preQuit;
std::unique_ptr _dbSession;
LmsApplicationGroupContainer& _appGroups;
Events _events;
boost::optional _userId {};
std::shared_ptr _imageResource;
std::shared_ptr _audioResource;
};
// Helper to get session instance
#define LmsApp LmsApplication::instance()
} // namespace UserInterface
#endif