Files
lms/src/database/DatabaseUpdater.hpp
T

137 lines
4.0 KiB
C++

/*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <Wt/WIOService>
#include <Wt/WSignal>
#include <mutex>
#include <boost/asio/deadline_timer.hpp>
#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<std::string>& extensions);
void setVideoExtensions(const std::vector<std::string>& 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<Stats>& scanComplete() { return _sigScanComplete; }
// Emitted when a track changed
// true -> added or modified, false -> to be deleted
Wt::Signal<bool, Track::id_type>& 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<boost::filesystem::path>& rootDirectories,
const std::vector<boost::filesystem::path>& 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<Database::Cluster::pointer> getGenreClusters( const std::list<std::string>& 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<Stats> _sigScanComplete;
Wt::Signal<bool, Artist::id_type> _sigArtistChanged;
Wt::Signal<bool, Release::id_type> _sigReleaseChanged;
Wt::Signal<bool, Track::id_type> _sigTrackChanged;
std::mutex _mutex;
boost::asio::deadline_timer _scheduleTimer;
Database::Handler* _db = nullptr;
std::vector<boost::filesystem::path> _audioFileExtensions;
std::vector<boost::filesystem::path> _videoFileExtensions;
MetaData::AvFormat _metadataParser;
}; // class Updater
} // Database