From dd9b0818f13b23ef53ab544b41ddc1f5075c1b7f Mon Sep 17 00:00:00 2001 From: casey Date: Fri, 27 May 2016 22:59:19 -0700 Subject: [PATCH] - Fixed a bug that could let partial results get displayed in CategoryListView - Allow queries to be marked as cancelable so they won't be processed. --- src/core/library/IQuery.h | 1 + src/core/library/query/QueryBase.cpp | 9 +++++++-- src/core/library/query/QueryBase.h | 3 +++ src/musikbox/app/layout/LibraryLayout.cpp | 10 ++++++++-- src/musikbox/app/query/CategoryListViewQuery.cpp | 6 +++--- src/musikbox/app/window/CategoryListView.cpp | 15 ++++++++++----- 6 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/core/library/IQuery.h b/src/core/library/IQuery.h index d9b2cdd04..5f5b8b09c 100755 --- a/src/core/library/IQuery.h +++ b/src/core/library/IQuery.h @@ -22,6 +22,7 @@ namespace musik { namespace core { Running = 2, Failed = 3, Finished = 4, + Canceled = 5 } Status; virtual ~IQuery() { } diff --git a/src/core/library/query/QueryBase.cpp b/src/core/library/query/QueryBase.cpp index 52069194e..2ea0e3a9a 100644 --- a/src/core/library/query/QueryBase.cpp +++ b/src/core/library/query/QueryBase.cpp @@ -48,7 +48,8 @@ static boost::atomic nextId(0); QueryBase::QueryBase() : status(0) , options(0) -, queryId(0) { +, queryId(0) +, cancel(false) { this->queryId = nextId++; } @@ -62,7 +63,11 @@ std::string QueryBase::Name() { bool QueryBase::Run(db::Connection &db) { this->SetStatus(Running); try { - if (OnRun(db)) { + if (this->IsCanceled()) { + this->SetStatus(Canceled); + return true; + } + else if (OnRun(db)) { this->SetStatus(Finished); return true; } diff --git a/src/core/library/query/QueryBase.h b/src/core/library/query/QueryBase.h index 0771cd776..450850694 100644 --- a/src/core/library/query/QueryBase.h +++ b/src/core/library/query/QueryBase.h @@ -57,6 +57,8 @@ namespace musik { namespace core { namespace query { virtual int GetStatus(); virtual int GetId(); virtual int GetOptions(); + virtual void Cancel() { this->cancel = true; } + virtual bool IsCanceled() { return cancel; } protected: void SetStatus(int status); @@ -69,6 +71,7 @@ namespace musik { namespace core { namespace query { unsigned int status; unsigned int queryId; unsigned int options; + volatile bool cancel; boost::mutex stateMutex; }; diff --git a/src/musikbox/app/layout/LibraryLayout.cpp b/src/musikbox/app/layout/LibraryLayout.cpp index 32e2b0801..ed9410a35 100755 --- a/src/musikbox/app/layout/LibraryLayout.cpp +++ b/src/musikbox/app/layout/LibraryLayout.cpp @@ -10,7 +10,13 @@ using namespace musik::core::library::constants; #define CATEGORY_WIDTH 25 -#define TRANSPORT_HEIGHT 2 + +#ifdef WIN32 + #define TRANSPORT_HEIGHT 3 +#else + #define TRANSPORT_HEIGHT 2 +#endif + #define DEFAULT_CATEGORY constants::Track::ALBUM_ID using namespace musik::core; @@ -120,7 +126,7 @@ bool LibraryLayout::KeyPress(const std::string& key) { return true; } else if (key == "KEY_F(5)") { - this->categoryList->Requery(); + this->categoryList->Requery(); return true; } else if (key == "CTL_DOWN") { diff --git a/src/musikbox/app/query/CategoryListViewQuery.cpp b/src/musikbox/app/query/CategoryListViewQuery.cpp index 3ac87336a..a48d9b0e5 100755 --- a/src/musikbox/app/query/CategoryListViewQuery.cpp +++ b/src/musikbox/app/query/CategoryListViewQuery.cpp @@ -15,7 +15,7 @@ using namespace musik::core::db; using namespace musik::core::library::constants; using namespace musik::box; -#define reset(x) x.reset(new std::vector >); +#define RESET_RESULT(x) x.reset(new std::vector >); static const std::string ALBUM_QUERY = "SELECT DISTINCT albums.id, albums.name " @@ -47,7 +47,7 @@ static void initFieldToQueryMap() { CategoryListViewQuery::CategoryListViewQuery(const std::string& trackField) { this->trackField = trackField; - reset(result); + RESET_RESULT(result); { boost::mutex::scoped_lock lock(QUERY_MAP_MUTEX); @@ -71,7 +71,7 @@ CategoryListViewQuery::ResultList CategoryListViewQuery::GetResult() { } bool CategoryListViewQuery::OnRun(Connection& db) { - reset(result); + RESET_RESULT(result); std::string query = FIELD_TO_QUERY_MAP[this->trackField]; Statement stmt(query.c_str(), db); diff --git a/src/musikbox/app/window/CategoryListView.cpp b/src/musikbox/app/window/CategoryListView.cpp index 20a4d99f9..557fa2179 100755 --- a/src/musikbox/app/window/CategoryListView.cpp +++ b/src/musikbox/app/window/CategoryListView.cpp @@ -34,6 +34,10 @@ CategoryListView::~CategoryListView() { } void CategoryListView::Requery() { + if (this->activeQuery) { + this->activeQuery->Cancel(); + } + this->activeQuery.reset(new CategoryListViewQuery(this->fieldName)); this->library->Enqueue(activeQuery); } @@ -56,7 +60,6 @@ void CategoryListView::SetFieldName(const std::string& fieldName) { if (this->metadata) { this->metadata.reset(); - //this->OnAdapterChanged(); } this->Requery(); @@ -71,10 +74,12 @@ void CategoryListView::OnQueryCompleted(QueryPtr query) { void CategoryListView::ProcessMessage(IMessage &message) { if (message.Type() == WINDOW_MESSAGE_QUERY_COMPLETED) { - this->metadata = activeQuery->GetResult(); - activeQuery.reset(); - this->OnAdapterChanged(); - this->OnInvalidated(); + if (this->activeQuery && this->activeQuery->GetStatus() == IQuery::Finished) { + this->metadata = activeQuery->GetResult(); + activeQuery.reset(); + this->OnAdapterChanged(); + this->OnInvalidated(); + } } }