2010-03-24 00:11:46 +01:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-03-24 00:11:46 +01:00
|
|
|
|
|
|
|
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-11-27 18:52:08 +01:00
|
|
|
#include "core/database.h"
|
|
|
|
#include "smartplaylists/generator.h"
|
|
|
|
#include "smartplaylists/querygenerator.h"
|
|
|
|
#include "smartplaylists/search.h"
|
2009-12-24 20:16:07 +01:00
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
const char* Library::kSongsTable = "songs";
|
|
|
|
const char* Library::kDirsTable = "directories";
|
|
|
|
const char* Library::kSubdirsTable = "subdirectories";
|
2010-06-20 18:30:10 +02:00
|
|
|
const char* Library::kFtsTable = "songs_fts";
|
2009-12-24 20:16:07 +01:00
|
|
|
|
2010-06-23 15:21:30 +02:00
|
|
|
Library::Library(BackgroundThread<Database>* db_thread, TaskManager* task_manager,
|
|
|
|
QObject *parent)
|
2010-05-28 21:51:51 +02:00
|
|
|
: QObject(parent),
|
2010-06-23 15:21:30 +02:00
|
|
|
task_manager_(task_manager),
|
2010-06-02 17:58:07 +02:00
|
|
|
backend_(NULL),
|
|
|
|
model_(NULL),
|
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-08-27 20:36:57 +02:00
|
|
|
backend_ = new LibraryBackend;
|
|
|
|
backend()->moveToThread(db_thread);
|
|
|
|
|
2010-06-20 18:30:10 +02:00
|
|
|
backend_->Init(db_thread->Worker(), kSongsTable, kDirsTable, kSubdirsTable, kFtsTable);
|
2010-06-02 17:58:07 +02:00
|
|
|
|
2010-11-27 20:18:56 +01:00
|
|
|
using smart_playlists::Generator;
|
|
|
|
using smart_playlists::GeneratorPtr;
|
|
|
|
using smart_playlists::QueryGenerator;
|
|
|
|
using smart_playlists::Search;
|
|
|
|
using smart_playlists::SearchTerm;
|
|
|
|
|
2010-06-02 17:58:07 +02:00
|
|
|
model_ = new LibraryModel(backend_, this);
|
2010-11-17 21:21:04 +01:00
|
|
|
model_->set_show_smart_playlists(true);
|
2010-11-27 18:52:08 +01:00
|
|
|
model_->set_default_smart_playlists(LibraryModel::DefaultGenerators()
|
|
|
|
<< (LibraryModel::GeneratorList()
|
2010-11-27 20:18:56 +01:00
|
|
|
<< GeneratorPtr(new QueryGenerator(tr("50 random tracks"), Search(
|
2010-11-27 18:52:08 +01:00
|
|
|
Search::Type_All, Search::TermList(),
|
2010-11-27 20:18:56 +01:00
|
|
|
Search::Sort_Random, SearchTerm::Field_Title, 50)))
|
|
|
|
<< GeneratorPtr(new QueryGenerator(tr("Ever played"), Search(
|
2010-11-27 18:52:08 +01:00
|
|
|
Search::Type_And, Search::TermList()
|
|
|
|
<< SearchTerm(SearchTerm::Field_PlayCount, SearchTerm::Op_GreaterThan, 0),
|
2010-11-27 20:18:56 +01:00
|
|
|
Search::Sort_Random, SearchTerm::Field_Title)))
|
|
|
|
<< GeneratorPtr(new QueryGenerator(tr("Never played"), Search(
|
2010-11-27 18:52:08 +01:00
|
|
|
Search::Type_And, Search::TermList()
|
|
|
|
<< SearchTerm(SearchTerm::Field_PlayCount, SearchTerm::Op_Equals, 0),
|
2010-11-27 20:18:56 +01:00
|
|
|
Search::Sort_Random, SearchTerm::Field_Title)))
|
|
|
|
<< GeneratorPtr(new QueryGenerator(tr("Last played"), Search(
|
2010-11-27 18:52:08 +01:00
|
|
|
Search::Type_All, Search::TermList(),
|
2010-11-27 20:18:56 +01:00
|
|
|
Search::Sort_FieldDesc, SearchTerm::Field_LastPlayed)))
|
|
|
|
<< GeneratorPtr(new QueryGenerator(tr("Most played"), Search(
|
2010-11-27 18:52:08 +01:00
|
|
|
Search::Type_All, Search::TermList(),
|
2010-11-27 20:18:56 +01:00
|
|
|
Search::Sort_FieldDesc, SearchTerm::Field_PlayCount)))
|
|
|
|
<< GeneratorPtr(new QueryGenerator(tr("Favourite tracks"), Search(
|
2010-11-27 18:52:08 +01:00
|
|
|
Search::Type_All, Search::TermList(),
|
2010-11-27 20:18:56 +01:00
|
|
|
Search::Sort_FieldDesc, SearchTerm::Field_Score)))
|
|
|
|
<< GeneratorPtr(new QueryGenerator(tr("Newest tracks"), Search(
|
2010-11-27 18:52:08 +01:00
|
|
|
Search::Type_All, Search::TermList(),
|
2010-11-27 20:18:56 +01:00
|
|
|
Search::Sort_FieldDesc, SearchTerm::Field_DateCreated)))
|
2010-11-27 18:52:08 +01:00
|
|
|
) << (LibraryModel::GeneratorList()
|
2010-11-27 20:18:56 +01:00
|
|
|
<< GeneratorPtr(new QueryGenerator(tr("All tracks"), Search(
|
2010-11-27 18:52:08 +01:00
|
|
|
Search::Type_All, Search::TermList(),
|
2010-11-27 20:18:56 +01:00
|
|
|
Search::Sort_FieldAsc, SearchTerm::Field_Artist, -1)))
|
|
|
|
<< GeneratorPtr(new QueryGenerator(tr("Least favourite tracks"), Search(
|
2010-11-27 18:52:08 +01:00
|
|
|
Search::Type_Or, Search::TermList()
|
|
|
|
<< SearchTerm(SearchTerm::Field_Rating, SearchTerm::Op_LessThan, 0.6)
|
|
|
|
<< SearchTerm(SearchTerm::Field_SkipCount, SearchTerm::Op_GreaterThan, 4),
|
2010-11-27 20:18:56 +01:00
|
|
|
Search::Sort_FieldDesc, SearchTerm::Field_SkipCount)))
|
2010-11-27 18:52:08 +01:00
|
|
|
) << (LibraryModel::GeneratorList()
|
2010-11-27 20:18:56 +01:00
|
|
|
<< GeneratorPtr(new QueryGenerator(tr("Dynamic random mix"), Search(
|
2010-11-27 18:52:08 +01:00
|
|
|
Search::Type_All, Search::TermList(),
|
2010-11-27 20:18:56 +01:00
|
|
|
Search::Sort_Random, SearchTerm::Field_Title), true))
|
2010-11-27 18:52:08 +01:00
|
|
|
)
|
|
|
|
);
|
2011-02-25 21:10:41 +01:00
|
|
|
|
|
|
|
// full rescan revisions
|
|
|
|
full_rescan_revisions_[26] = tr("CUE sheet support");
|
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();
|
|
|
|
|
2010-06-26 14:41:18 +02:00
|
|
|
watcher->set_backend(backend_);
|
|
|
|
watcher->set_task_manager(task_manager_);
|
2010-05-09 02:10:26 +02:00
|
|
|
|
|
|
|
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)));
|
2010-06-02 18:22:20 +02:00
|
|
|
connect(watcher, SIGNAL(CompilationsNeedUpdating()),
|
|
|
|
backend_, SLOT(UpdateCompilations()));
|
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
|
|
|
}
|
|
|
|
|
2010-05-25 22:40:45 +02:00
|
|
|
void Library::IncrementalScan() {
|
|
|
|
if (!watcher_->Worker())
|
|
|
|
return;
|
|
|
|
|
|
|
|
watcher_->Worker()->IncrementalScanAsync();
|
|
|
|
}
|
2010-06-24 23:46:18 +02:00
|
|
|
|
2011-02-17 20:57:14 +01:00
|
|
|
void Library::FullScan() {
|
|
|
|
if (!watcher_->Worker())
|
|
|
|
return;
|
|
|
|
|
|
|
|
watcher_->Worker()->FullScanAsync();
|
|
|
|
}
|
|
|
|
|
2010-06-24 23:46:18 +02:00
|
|
|
void Library::PauseWatcher() {
|
|
|
|
if (!watcher_->Worker())
|
|
|
|
return;
|
|
|
|
|
|
|
|
watcher_->Worker()->SetRescanPausedAsync(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Library::ResumeWatcher() {
|
|
|
|
if (!watcher_->Worker())
|
|
|
|
return;
|
|
|
|
|
|
|
|
watcher_->Worker()->SetRescanPausedAsync(false);
|
|
|
|
}
|
2010-07-10 19:03:28 +02:00
|
|
|
|
|
|
|
void Library::ReloadSettings() {
|
|
|
|
if (!watcher_->Worker())
|
|
|
|
return;
|
|
|
|
|
|
|
|
watcher_->Worker()->ReloadSettingsAsync();
|
|
|
|
}
|