1
/* This file is part of Clementine.
2
Copyright 2011, David Sansome <me@davidsansome.com>
4
Clementine is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, either version 3 of the License, or
7
(at your option) any later version.
9
Clementine is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
18
#include "globalsearch.h"
19
#include "globalsearchservice.h"
20
#include "globalsearch/globalsearchadaptor.h"
21
#include "core/logging.h"
22
#include "core/mpris_common.h"
25
GlobalSearchService::GlobalSearchService(GlobalSearch* engine, QObject* parent)
29
qDBusRegisterMetaType<GlobalSearchServiceResult>();
30
qDBusRegisterMetaType<GlobalSearchServiceResultList>();
32
new GlobalSearchAdaptor(this);
33
QDBusConnection::sessionBus().registerObject("/GlobalSearch", this);
35
connect(engine_, SIGNAL(ResultsAvailable(int,SearchProvider::ResultList)),
36
this, SLOT(ResultsAvailableSlot(int,SearchProvider::ResultList)),
37
Qt::QueuedConnection);
38
connect(engine_, SIGNAL(SearchFinished(int)),
39
this, SLOT(SearchFinishedSlot(int)),
40
Qt::QueuedConnection);
41
connect(engine_, SIGNAL(ArtLoaded(int,QPixmap)),
42
this, SLOT(ArtLoadedSlot(int,QPixmap)),
43
Qt::QueuedConnection);
46
int GlobalSearchService::StartSearch(const QString& query, bool prefetch_art) {
47
PendingSearch pending_search;
48
pending_search.prefetch_art_ = prefetch_art;
50
const int id = engine_->SearchAsync(query);
51
pending_searches_[id] = pending_search;
55
void GlobalSearchService::CancelSearch(int id) {
56
if (!pending_searches_.contains(id))
59
engine_->CancelSearch(id);
60
pending_searches_.remove(id);
63
void GlobalSearchService::ResultsAvailableSlot(int id, const SearchProvider::ResultList& results) {
64
if (!pending_searches_.contains(id))
67
const PendingSearch& pending = pending_searches_[id];
69
GlobalSearchServiceResultList ret;
70
foreach (const SearchProvider::Result& result, results) {
71
const int result_id = next_result_id_ ++;
73
RecentResult* recent = &recent_results_[result_id];
74
recent->result_.art_on_the_way_ = false;
76
// Prefetch art if it was requested
77
if (pending.prefetch_art_ && !result.provider_->art_is_probably_remote()) {
78
const int art_id = engine_->LoadArtAsync(result);
79
prefetching_art_[art_id] = result_id;
80
recent->result_.art_on_the_way_ = true;
83
// Build the result to send back
84
recent->result_.result_id_ = result_id;
85
recent->result_.provider_name_ = result.provider_->name();
86
recent->result_.type_ = result.type_;
87
recent->result_.match_quality_ = result.match_quality_;
89
recent->result_.album_size_ = result.album_size_;
91
recent->result_.title_ = result.metadata_.title();
92
recent->result_.artist_ = result.metadata_.artist();
93
recent->result_.album_ = result.metadata_.album();
94
recent->result_.album_artist_ = result.metadata_.albumartist();
95
recent->result_.is_compilation_ = result.metadata_.is_compilation();
96
recent->result_.track_ = result.metadata_.track();
98
ret << recent->result_;
101
emit ResultsAvailable(id, ret);
104
void GlobalSearchService::SearchFinishedSlot(int id) {
105
if (!pending_searches_.contains(id))
108
emit SearchFinished(id);
109
pending_searches_.remove(id);
112
void GlobalSearchService::ArtLoadedSlot(int id, const QPixmap& pixmap) {
113
QMap<int, int>::iterator it = prefetching_art_.find(id);
114
if (it == prefetching_art_.end())
117
const int result_id = prefetching_art_.take(id);
118
QMap<int, RecentResult>::iterator it2 = recent_results_.find(result_id);
119
if (it2 == recent_results_.end())
122
// Encode the pixmap as a png
124
buf.open(QIODevice::WriteOnly);
125
pixmap.toImage().save(&buf, "PNG");
128
emit ArtLoaded(result_id, buf.data());