2011-10-05 11:36:08 +02:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2011, David Sansome <me@davidsansome.com>
|
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2011-10-04 18:26:40 +02:00
|
|
|
#include "closure.h"
|
|
|
|
|
2012-04-13 15:53:17 +02:00
|
|
|
#include <QTimer>
|
|
|
|
|
2011-10-04 18:26:40 +02:00
|
|
|
#include "core/logging.h"
|
2012-04-13 15:49:29 +02:00
|
|
|
#include "core/timeconstants.h"
|
2011-10-04 18:26:40 +02:00
|
|
|
|
2012-03-20 15:14:44 +01:00
|
|
|
namespace _detail {
|
|
|
|
|
2011-10-04 18:26:40 +02:00
|
|
|
Closure::Closure(QObject* sender,
|
|
|
|
const char* signal,
|
|
|
|
QObject* receiver,
|
|
|
|
const char* slot,
|
2011-10-05 11:36:08 +02:00
|
|
|
const ClosureArgumentWrapper* val0,
|
2011-11-23 01:07:40 +01:00
|
|
|
const ClosureArgumentWrapper* val1,
|
2012-03-09 13:15:24 +01:00
|
|
|
const ClosureArgumentWrapper* val2,
|
|
|
|
const ClosureArgumentWrapper* val3)
|
2011-10-04 18:26:40 +02:00
|
|
|
: QObject(receiver),
|
2011-10-05 14:55:39 +02:00
|
|
|
callback_(NULL),
|
2011-10-05 11:36:08 +02:00
|
|
|
val0_(val0),
|
2011-11-23 01:07:40 +01:00
|
|
|
val1_(val1),
|
2012-03-09 13:15:24 +01:00
|
|
|
val2_(val2),
|
|
|
|
val3_(val3) {
|
2011-10-04 18:26:40 +02:00
|
|
|
const QMetaObject* meta_receiver = receiver->metaObject();
|
|
|
|
|
|
|
|
QByteArray normalised_slot = QMetaObject::normalizedSignature(slot + 1);
|
2011-10-06 13:19:42 +02:00
|
|
|
const int index = meta_receiver->indexOfSlot(normalised_slot.constData());
|
|
|
|
Q_ASSERT(index != -1);
|
|
|
|
slot_ = meta_receiver->method(index);
|
2011-10-04 18:26:40 +02:00
|
|
|
|
2011-10-06 13:19:42 +02:00
|
|
|
Connect(sender, signal);
|
2011-10-04 18:26:40 +02:00
|
|
|
}
|
|
|
|
|
2011-10-05 14:55:39 +02:00
|
|
|
Closure::Closure(QObject* sender,
|
|
|
|
const char* signal,
|
|
|
|
std::tr1::function<void()> callback)
|
|
|
|
: callback_(callback) {
|
2011-10-06 13:19:42 +02:00
|
|
|
Connect(sender, signal);
|
|
|
|
}
|
|
|
|
|
2012-03-19 19:36:52 +01:00
|
|
|
Closure::~Closure() {
|
|
|
|
}
|
|
|
|
|
2011-10-06 13:19:42 +02:00
|
|
|
void Closure::Connect(QObject* sender, const char* signal) {
|
|
|
|
bool success = connect(sender, signal, SLOT(Invoked()));
|
|
|
|
Q_ASSERT(success);
|
|
|
|
success = connect(sender, SIGNAL(destroyed()), SLOT(Cleanup()));
|
|
|
|
Q_ASSERT(success);
|
2011-10-06 14:24:33 +02:00
|
|
|
Q_UNUSED(success);
|
2011-10-05 14:55:39 +02:00
|
|
|
}
|
|
|
|
|
2011-10-04 18:26:40 +02:00
|
|
|
void Closure::Invoked() {
|
2011-10-05 14:55:39 +02:00
|
|
|
if (callback_) {
|
|
|
|
callback_();
|
|
|
|
} else {
|
|
|
|
slot_.invoke(
|
|
|
|
parent(),
|
|
|
|
val0_ ? val0_->arg() : QGenericArgument(),
|
2011-11-23 01:07:40 +01:00
|
|
|
val1_ ? val1_->arg() : QGenericArgument(),
|
2012-03-09 13:15:24 +01:00
|
|
|
val2_ ? val2_->arg() : QGenericArgument(),
|
|
|
|
val3_ ? val3_->arg() : QGenericArgument());
|
2011-10-05 14:55:39 +02:00
|
|
|
}
|
2011-10-05 11:36:08 +02:00
|
|
|
deleteLater();
|
|
|
|
}
|
2011-10-04 18:26:40 +02:00
|
|
|
|
2011-10-05 11:36:08 +02:00
|
|
|
void Closure::Cleanup() {
|
|
|
|
disconnect();
|
|
|
|
deleteLater();
|
2011-10-04 18:26:40 +02:00
|
|
|
}
|
2011-10-06 13:11:18 +02:00
|
|
|
|
2012-03-20 15:14:44 +01:00
|
|
|
} // namespace _detail
|
|
|
|
|
|
|
|
_detail::Closure* NewClosure(
|
2011-10-06 13:11:18 +02:00
|
|
|
QObject* sender, const char* signal,
|
|
|
|
QObject* receiver, const char* slot) {
|
2012-03-20 15:14:44 +01:00
|
|
|
return new _detail::Closure(sender, signal, receiver, slot);
|
2011-10-06 13:11:18 +02:00
|
|
|
}
|
2012-04-13 15:49:29 +02:00
|
|
|
|
|
|
|
void DoAfter(QObject* receiver, const char* slot, int msec) {
|
|
|
|
QTimer::singleShot(msec, receiver, slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DoInAMinuteOrSo(QObject* receiver, const char* slot) {
|
|
|
|
int msec = (60 + (qrand() % 60)) * kMsecPerSec;
|
|
|
|
DoAfter(receiver, slot, msec);
|
|
|
|
}
|