From fc34a244c6a32dc5a96dbb986457b00b5e73d8a4 Mon Sep 17 00:00:00 2001 From: John Maguire Date: Wed, 5 Oct 2011 11:36:08 +0200 Subject: [PATCH] Tidy up closure implementation. --- src/core/closure.cpp | 43 ++++++++++++++++++++++------- src/core/closure.h | 34 +++++++++++++++++------ src/internet/groovesharkservice.cpp | 16 +++++++++-- 3 files changed, 72 insertions(+), 21 deletions(-) diff --git a/src/core/closure.cpp b/src/core/closure.cpp index 9693a7d37..6a8920956 100644 --- a/src/core/closure.cpp +++ b/src/core/closure.cpp @@ -1,3 +1,20 @@ +/* This file is part of Clementine. + Copyright 2011, David Sansome + + 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 . +*/ + #include "closure.h" #include "core/logging.h" @@ -6,24 +23,30 @@ Closure::Closure(QObject* sender, const char* signal, QObject* receiver, const char* slot, - const ClosureArgumentWrapper* val1, - const ClosureArgumentWrapper* val2) + const ClosureArgumentWrapper* val0, + const ClosureArgumentWrapper* val1) : QObject(receiver), - val1_(val1), - val2_(val2) { + val0_(val0), + val1_(val1) { const QMetaObject* meta_receiver = receiver->metaObject(); QByteArray normalised_slot = QMetaObject::normalizedSignature(slot + 1); slot_ = meta_receiver->method( meta_receiver->indexOfSlot(normalised_slot.constData())); - connect(sender, signal, this, SLOT(Invoked()), Qt::AutoConnection); + connect(sender, signal, SLOT(Invoked())); + connect(sender, SIGNAL(destroyed()), SLOT(Cleanup())); } void Closure::Invoked() { - qLog(Debug) << "Invoking:" << slot_.signature() << val1_->arg().name(); - slot_.invoke(parent(), val1_->arg(), val2_->arg()); - - delete val1_; - delete val2_; + slot_.invoke( + parent(), + val0_ ? val0_->arg() : QGenericArgument(), + val1_ ? val1_->arg() : QGenericArgument()); + deleteLater(); +} + +void Closure::Cleanup() { + disconnect(); + deleteLater(); } diff --git a/src/core/closure.h b/src/core/closure.h index 8780590be..a7f4ec982 100644 --- a/src/core/closure.h +++ b/src/core/closure.h @@ -1,19 +1,35 @@ +/* This file is part of Clementine. + Copyright 2011, David Sansome + + 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 . +*/ + #ifndef CLOSURE_H #define CLOSURE_H #include #include +#include + #include "core/logging.h" class ClosureArgumentWrapper { public: virtual ~ClosureArgumentWrapper() {} - virtual QGenericArgument arg() const { - qLog(Debug) << Q_FUNC_INFO; - return QGenericArgument(); - } + virtual QGenericArgument arg() const = 0; }; template @@ -22,7 +38,6 @@ class ClosureArgument : public ClosureArgumentWrapper { ClosureArgument(const T& data) : data_(data) {} virtual QGenericArgument arg() const { - qLog(Debug) << Q_FUNC_INFO; return Q_ARG(T, data_); } @@ -36,17 +51,18 @@ class Closure : public QObject { public: Closure(QObject* sender, const char* signal, QObject* receiver, const char* slot, - const ClosureArgumentWrapper* val1, - const ClosureArgumentWrapper* val2); + const ClosureArgumentWrapper* val0 = 0, + const ClosureArgumentWrapper* val1 = 0); private slots: void Invoked(); + void Cleanup(); private: QMetaMethod slot_; - const ClosureArgumentWrapper* val1_; - const ClosureArgumentWrapper* val2_; + boost::scoped_ptr val0_; + boost::scoped_ptr val1_; }; #define C_ARG(type, data) new ClosureArgument(data) diff --git a/src/internet/groovesharkservice.cpp b/src/internet/groovesharkservice.cpp index e001262c7..64858c8cd 100644 --- a/src/internet/groovesharkservice.cpp +++ b/src/internet/groovesharkservice.cpp @@ -171,7 +171,7 @@ int GrooveSharkService::SearchAlbums(const QString& query) { QNetworkReply* reply = CreateRequest("getAlbumSearchResults", parameters, false); - int id = next_pending_search_id_++; + const int id = next_pending_search_id_++; new Closure(reply, SIGNAL(finished()), this, SLOT(SearchAlbumsFinished(QNetworkReply*,int)), @@ -182,7 +182,19 @@ int GrooveSharkService::SearchAlbums(const QString& query) { } void GrooveSharkService::SearchAlbumsFinished(QNetworkReply* reply, int id) { - qLog(Debug) << reply << id; + reply->deleteLater(); + + QVariantMap result = ExtractResult(reply); + QVariantList albums = result["albums"].toList(); + foreach (const QVariant& v, albums) { + QVariantMap album = v.toMap(); + //quint64 album_id = album["AlbumID"].toULongLong(); + QString album_name = album["AlbumName"].toString(); + QString artist_name = album["ArtistName"].toString(); + //QString cover_art = album["CoverArtFilename"].toString(); + + qLog(Debug) << "Found:" << album_name << artist_name; + } } void GrooveSharkService::DoSearch() {