1 /* 2 * Copyright (C) 2013 Emeric Poupon 3 * 4 * This file is part of LMS. 5 * 6 * LMS is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * LMS is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with LMS. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #pragma once 21 22 #include <optional> 23 24 #include <Wt/WApplication.h> 25 26 #include "scanner/ScannerEvents.hpp" 27 28 namespace Database 29 { 30 class Artist; 31 class Cluster; 32 class Db; 33 class Release; 34 class Session; 35 class User; 36 } 37 namespace Wt 38 { 39 class WPopupMenu; 40 } 41 42 namespace UserInterface { 43 44 class CoverResource; 45 class LmsApplicationException; 46 class MediaPlayer; 47 class PlayQueue; 48 class LmsApplicationManager; 49 50 class LmsApplication : public Wt::WApplication 51 { 52 public: 53 54 LmsApplication(const Wt::WEnvironment& env, Database::Db& db, LmsApplicationManager& appManager, std::optional<Database::IdType> userId = std::nullopt); 55 ~LmsApplication(); 56 57 static std::unique_ptr<Wt::WApplication> create(const Wt::WEnvironment& env, Database::Db& db, LmsApplicationManager& appManager); 58 static LmsApplication* instance(); 59 60 61 // Session application data getCoverResource()62 std::shared_ptr<CoverResource> getCoverResource() { return _coverResource; } 63 Database::Session& getDbSession(); // always thread safe 64 65 Wt::Dbo::ptr<Database::User> getUser(); 66 Database::IdType getUserId(); 67 bool isUserAuthStrong() const; // user must be logged in prior this call 68 bool isUserAdmin(); // user must be logged in prior this call 69 bool isUserDemo(); // user must be logged in prior this call 70 std::string getUserLoginName(); // user must be logged in prior this call 71 72 // Proxified scanner events getScannerEvents()73 Scanner::Events& getScannerEvents() { return _scannerEvents; } 74 75 // Utils 76 void post(std::function<void()> func); 77 78 // Used to classify the message sent to the user 79 enum class MsgType 80 { 81 Success, 82 Info, 83 Warning, 84 Danger, 85 }; 86 void notifyMsg(MsgType type, const Wt::WString& message, std::chrono::milliseconds duration = std::chrono::milliseconds {4000}); 87 88 static Wt::WLink createArtistLink(Wt::Dbo::ptr<Database::Artist> artist); 89 static std::unique_ptr<Wt::WAnchor> createArtistAnchor(Wt::Dbo::ptr<Database::Artist> artist, bool addText = true); 90 static Wt::WLink createReleaseLink(Wt::Dbo::ptr<Database::Release> release); 91 static std::unique_ptr<Wt::WAnchor> createReleaseAnchor(Wt::Dbo::ptr<Database::Release> release, bool addText = true); 92 static std::unique_ptr<Wt::WText> createCluster(Wt::Dbo::ptr<Database::Cluster> cluster, bool canDelete = false); 93 Wt::WPopupMenu* createPopupMenu(); 94 getMediaPlayer() const95 MediaPlayer& getMediaPlayer() const { return *_mediaPlayer; } getPlayQueue() const96 PlayQueue& getPlayQueue() const { return *_playQueue; } 97 98 // Signal emitted just before the session ends (user may already be logged out) preQuit()99 Wt::Signal<>& preQuit() { return _preQuit; } 100 101 private: 102 void init(); 103 void setTheme(); 104 void processPasswordAuth(); 105 void handleException(LmsApplicationException& e); 106 void goHomeAndQuit(); 107 108 // Signal slots 109 void logoutUser(); 110 void onUserLoggedIn(); 111 112 void notify(const Wt::WEvent& event) override; 113 void finalize() override; 114 115 void createHome(); 116 117 Database::Db& _db; 118 Wt::Signal<> _preQuit; 119 LmsApplicationManager& _appManager; 120 Scanner::Events _scannerEvents; 121 struct UserAuthInfo 122 { 123 Database::IdType userId; 124 bool strongAuth {}; 125 }; 126 std::optional<UserAuthInfo> _authenticatedUser; 127 std::shared_ptr<CoverResource> _coverResource; 128 MediaPlayer* _mediaPlayer {}; 129 PlayQueue* _playQueue {}; 130 std::unique_ptr<Wt::WPopupMenu> _popupMenu; 131 }; 132 133 134 // Helper to get session instance 135 #define LmsApp ::UserInterface::LmsApplication::instance() 136 137 } // namespace UserInterface 138 139