/* * 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 . */ #pragma once #include #include #include #include #include "metadata/AvFormat.hpp" #include "database/DatabaseHandler.hpp" namespace Database { class Updater { public: static Updater& instance(); void setConnectionPool(Wt::Dbo::SqlConnectionPool& connectionPool); void setAudioExtensions(const std::vector& extensions); void setVideoExtensions(const std::vector& extensions); void start(); void stop(); void restart(); struct Stats { std::size_t nbSkipped = 0; // no change since last scan std::size_t nbScanned = 0; std::size_t nbScanErrors = 0; // cannot scan file std::size_t nbNotImported = 0; // Not imported (criteria not filled) std::size_t nbAdded = 0; std::size_t nbRemoved = 0; std::size_t nbModified = 0; std::size_t nbChanges() const { return nbAdded + nbRemoved + nbModified;} }; // Emitted when the whole database has been scanned Wt::Signal& scanComplete() { return _sigScanComplete; } // Emitted when a track changed // true -> added or modified, false -> to be deleted Wt::Signal& trackChanged() { return _sigTrackChanged; } std::mutex& getMutex(void) { return _mutex; } private: Updater(); struct RootDirectory { Database::MediaDirectory::Type type; boost::filesystem::path path; RootDirectory(Database::MediaDirectory::Type t, boost::filesystem::path p) : type(t), path(p) {} }; // Job handling void processNextJob(); void scheduleScan(boost::posix_time::time_duration duration); void scheduleScan(boost::posix_time::ptime time); // Update database (scheduled callback) void process(boost::system::error_code ec); // Check if a file exists and is still in a root directory static bool checkFile(const boost::filesystem::path& p, const std::vector& rootDirectories, const std::vector& extensions); void processRootDirectory( RootDirectory rootDirectory, Stats& stats); // Helpers Database::Artist::pointer getArtist( const boost::filesystem::path& file, const std::string& name, const std::string& MBID); Database::Release::pointer getRelease( const boost::filesystem::path& file, const std::string& name, const std::string& MBID); std::vector getGenreClusters( const std::list& names); void updateFileExtensions(); // Audio void checkAudioFiles( Stats& stats ); void checkDuplicatedAudioFiles( Stats& stats ); void processAudioFile( const boost::filesystem::path& file, Stats& stats); // Video void checkVideoFiles( Stats& stats ); void processVideoFile( const boost::filesystem::path& file, Stats& stats); bool _running; Wt::WIOService _ioService; Wt::Signal _sigScanComplete; Wt::Signal _sigArtistChanged; Wt::Signal _sigReleaseChanged; Wt::Signal _sigTrackChanged; std::mutex _mutex; boost::asio::deadline_timer _scheduleTimer; Database::Handler* _db = nullptr; std::vector _audioFileExtensions; std::vector _videoFileExtensions; MetaData::AvFormat _metadataParser; }; // class Updater } // Database