~ubuntu-branches/ubuntu/saucy/clementine/saucy

« back to all changes in this revision

Viewing changes to src/globalsearch/globalsearchservice.cpp

  • Committer: Package Import Robot
  • Author(s): Thomas PIERSON
  • Date: 2012-01-01 20:43:39 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120101204339-lsb6nndwhfy05sde
Tags: 1.0.1+dfsg-1
New upstream release. (Closes: #653926, #651611, #657391)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of Clementine.
 
2
   Copyright 2011, David Sansome <me@davidsansome.com>
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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/>.
 
16
*/
 
17
 
 
18
#include "globalsearch.h"
 
19
#include "globalsearchservice.h"
 
20
#include "globalsearch/globalsearchadaptor.h"
 
21
#include "core/logging.h"
 
22
#include "core/mpris_common.h"
 
23
 
 
24
 
 
25
GlobalSearchService::GlobalSearchService(GlobalSearch* engine, QObject* parent)
 
26
  : QObject(parent),
 
27
    engine_(engine)
 
28
{
 
29
  qDBusRegisterMetaType<GlobalSearchServiceResult>();
 
30
  qDBusRegisterMetaType<GlobalSearchServiceResultList>();
 
31
 
 
32
  new GlobalSearchAdaptor(this);
 
33
  QDBusConnection::sessionBus().registerObject("/GlobalSearch", this);
 
34
 
 
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);
 
44
}
 
45
 
 
46
int GlobalSearchService::StartSearch(const QString& query, bool prefetch_art) {
 
47
  PendingSearch pending_search;
 
48
  pending_search.prefetch_art_ = prefetch_art;
 
49
 
 
50
  const int id = engine_->SearchAsync(query);
 
51
  pending_searches_[id] = pending_search;
 
52
  return id;
 
53
}
 
54
 
 
55
void GlobalSearchService::CancelSearch(int id) {
 
56
  if (!pending_searches_.contains(id))
 
57
    return;
 
58
 
 
59
  engine_->CancelSearch(id);
 
60
  pending_searches_.remove(id);
 
61
}
 
62
 
 
63
void GlobalSearchService::ResultsAvailableSlot(int id, const SearchProvider::ResultList& results) {
 
64
  if (!pending_searches_.contains(id))
 
65
    return;
 
66
 
 
67
  const PendingSearch& pending = pending_searches_[id];
 
68
 
 
69
  GlobalSearchServiceResultList ret;
 
70
  foreach (const SearchProvider::Result& result, results) {
 
71
    const int result_id = next_result_id_ ++;
 
72
 
 
73
    RecentResult* recent = &recent_results_[result_id];
 
74
    recent->result_.art_on_the_way_ = false;
 
75
 
 
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;
 
81
    }
 
82
 
 
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_;
 
88
 
 
89
    recent->result_.album_size_ = result.album_size_;
 
90
 
 
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();
 
97
 
 
98
    ret << recent->result_;
 
99
  }
 
100
 
 
101
  emit ResultsAvailable(id, ret);
 
102
}
 
103
 
 
104
void GlobalSearchService::SearchFinishedSlot(int id) {
 
105
  if (!pending_searches_.contains(id))
 
106
    return;
 
107
 
 
108
  emit SearchFinished(id);
 
109
  pending_searches_.remove(id);
 
110
}
 
111
 
 
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())
 
115
    return;
 
116
 
 
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())
 
120
    return;
 
121
 
 
122
  // Encode the pixmap as a png
 
123
  QBuffer buf;
 
124
  buf.open(QIODevice::WriteOnly);
 
125
  pixmap.toImage().save(&buf, "PNG");
 
126
  buf.close();
 
127
 
 
128
  emit ArtLoaded(result_id, buf.data());
 
129
}