Implement the controller callback

This commit is contained in:
Micke Prag 2012-02-27 15:35:59 +01:00
parent 6196793ff6
commit c0fe38e0eb
2 changed files with 61 additions and 1 deletions

View file

@ -1,12 +1,12 @@
#include "controllerlist.h"
#include "controller.h"
#include <telldus-core.h>
#include <QDebug>
class ControllerList::PrivateData {
public:
QList<Controller *> list;
int callbackId;
};
ControllerList::ControllerList(QObject *parent) :
@ -18,6 +18,9 @@ ControllerList::ControllerList(QObject *parent) :
roles[Qt::UserRole+1] = "controller";
setRoleNames(roles);
connect(this, SIGNAL(controllerEventSignal(int,int,int,QString)), this, SLOT(controllerEventSlot(int,int,int,QString)), Qt::QueuedConnection);
d->callbackId = tdRegisterControllerEvent(&ControllerList::controllerEvent, this);
const int DATA_LENGTH = 255;
char name[DATA_LENGTH];
int available, controllerId, type;
@ -30,6 +33,7 @@ ControllerList::ControllerList(QObject *parent) :
}
ControllerList::~ControllerList() {
tdUnregisterCallback(d->callbackId);
delete d;
}
@ -46,3 +50,53 @@ void ControllerList::save() {
d->list.at(i)->save();
}
}
void ControllerList::controllerEventSlot(int controllerId, int changeEvent, int changeType, const QString &newValue) {
if (changeEvent == TELLSTICK_DEVICE_STATE_CHANGED) {
for(int i = 0; i < d->list.size(); ++i) {
if (d->list.at(i)->id() != controllerId) {
continue;
}
if (changeType == TELLSTICK_CHANGE_AVAILABLE) {
if (newValue == "1") {
d->list.at(i)->setAvailable(true);
} else if (newValue == "0") {
d->list.at(i)->setAvailable(false);
}
} else if (changeType == TELLSTICK_CHANGE_FIRMWARE) {
d->list.at(i)->setFirmware(newValue);
}
}
return;
}
if (changeEvent == TELLSTICK_DEVICE_ADDED) {
beginInsertRows( QModelIndex(), d->list.size(), d->list.size() );
Controller *controller = new Controller(controllerId, changeType, "", this);
controller->setAvailable(true);
connect(controller, SIGNAL(nameChanged()), this, SIGNAL(changed()));
d->list.append(controller);
endInsertRows();
return;
}
if (changeEvent == TELLSTICK_DEVICE_REMOVED) {
for(int i = 0; i < d->list.size(); ++i) {
if (d->list.at(i)->id() != controllerId) {
continue;
}
beginRemoveRows( QModelIndex(), i, i );
d->list.takeAt(i);
endRemoveRows();
}
return;
}
}
void WINAPI ControllerList::controllerEvent( int controllerId, int changeEvent, int changeType, const char *newValue, int callbackId, void *context) {
ControllerList *controllerList = reinterpret_cast<ControllerList *>(context);
if (!controllerList) {
return;
}
emit controllerList->controllerEventSignal(controllerId, changeEvent, changeType, QString::fromUtf8(newValue));
}

View file

@ -3,6 +3,7 @@
#include <QAbstractListModel>
#include <QScriptValue>
#include <telldus-core.h>
class ControllerList : public QAbstractListModel
{
@ -17,11 +18,16 @@ public:
signals:
void changed();
void controllerEventSignal(int controllerId, int changeEvent, int changeType, const QString &newValue);
public slots:
void save();
private slots:
void controllerEventSlot(int controllerId, int changeEvent, int changeType, const QString &newValue);
private:
static void WINAPI controllerEvent( int controllerId, int changeEvent, int changeType, const char *newValue, int callbackId, void *context);
class PrivateData;
PrivateData *d;