2010-03-24 00:11:46 +01:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
|
|
|
|
Clementine 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.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-12-24 20:16:07 +01:00
|
|
|
#include "library.h"
|
2010-05-09 02:10:26 +02:00
|
|
|
#include "librarymodel.h"
|
2009-12-24 20:16:07 +01:00
|
|
|
#include "librarybackend.h"
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
const char* Library::kSongsTable = "songs";
|
|
|
|
const char* Library::kDirsTable = "directories";
|
|
|
|
const char* Library::kSubdirsTable = "subdirectories";
|
2009-12-24 20:16:07 +01:00
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
Library::Library(Database *db, QObject *parent)
|
|
|
|
: backend_(new LibraryBackend(db, kSongsTable, kDirsTable, kSubdirsTable, this)),
|
|
|
|
model_(new LibraryModel(backend_, parent)),
|
2010-03-23 18:26:54 +01:00
|
|
|
watcher_factory_(new BackgroundThreadFactoryImplementation<LibraryWatcher, LibraryWatcher>),
|
2010-05-09 02:10:26 +02:00
|
|
|
watcher_(NULL)
|
2009-12-24 20:16:07 +01:00
|
|
|
{
|
2010-03-23 18:26:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Library::set_watcher_factory(BackgroundThreadFactory<LibraryWatcher>* factory) {
|
|
|
|
watcher_factory_.reset(factory);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Library::Init() {
|
|
|
|
watcher_ = watcher_factory_->GetThread(this);
|
|
|
|
connect(watcher_, SIGNAL(Initialised()), SLOT(WatcherInitialised()));
|
|
|
|
}
|
|
|
|
|
2009-12-24 20:16:07 +01:00
|
|
|
void Library::StartThreads() {
|
2010-03-23 18:26:54 +01:00
|
|
|
Q_ASSERT(watcher_);
|
2009-12-24 20:16:07 +01:00
|
|
|
|
2010-03-21 16:55:58 +01:00
|
|
|
watcher_->set_io_priority(BackgroundThreadBase::IOPRIO_CLASS_IDLE);
|
2010-03-23 18:26:54 +01:00
|
|
|
watcher_->set_cpu_priority(QThread::IdlePriority);
|
|
|
|
watcher_->Start();
|
2009-12-24 20:16:07 +01:00
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
model_->Init();
|
2009-12-24 20:16:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Library::WatcherInitialised() {
|
2010-05-09 02:10:26 +02:00
|
|
|
LibraryWatcher* watcher = watcher_->Worker().get();
|
|
|
|
connect(watcher, SIGNAL(ScanStarted()), SIGNAL(ScanStarted()));
|
|
|
|
connect(watcher, SIGNAL(ScanFinished()), SIGNAL(ScanFinished()));
|
|
|
|
|
|
|
|
watcher->SetBackend(backend_);
|
|
|
|
|
|
|
|
connect(backend_, SIGNAL(DirectoryDiscovered(Directory,SubdirectoryList)),
|
|
|
|
watcher, SLOT(AddDirectory(Directory,SubdirectoryList)));
|
|
|
|
connect(backend_, SIGNAL(DirectoryDeleted(Directory)),
|
|
|
|
watcher, SLOT(RemoveDirectory(Directory)));
|
|
|
|
connect(watcher, SIGNAL(NewOrUpdatedSongs(SongList)),
|
|
|
|
backend_, SLOT(AddOrUpdateSongs(SongList)));
|
|
|
|
connect(watcher, SIGNAL(SongsMTimeUpdated(SongList)),
|
|
|
|
backend_, SLOT(UpdateMTimesOnly(SongList)));
|
|
|
|
connect(watcher, SIGNAL(SongsDeleted(SongList)),
|
|
|
|
backend_, SLOT(DeleteSongs(SongList)));
|
|
|
|
connect(watcher, SIGNAL(SubdirsDiscovered(SubdirectoryList)),
|
|
|
|
backend_, SLOT(AddOrUpdateSubdirs(SubdirectoryList)));
|
|
|
|
connect(watcher, SIGNAL(SubdirsMTimeUpdated(SubdirectoryList)),
|
|
|
|
backend_, SLOT(AddOrUpdateSubdirs(SubdirectoryList)));
|
2009-12-24 20:16:07 +01:00
|
|
|
|
|
|
|
// This will start the watcher checking for updates
|
2010-05-09 02:10:26 +02:00
|
|
|
backend_->LoadDirectoriesAsync();
|
2009-12-24 20:16:07 +01:00
|
|
|
}
|
|
|
|
|