RetroShare/retroshare-gui/src/util/RsGxsUpdateBroadcast.cpp
2014-07-04 21:51:17 +00:00

70 lines
1.8 KiB
C++

//#include <QTimer>
#include <QMap>
#include "RsGxsUpdateBroadcast.h"
#include "RsProtectedTimer.h"
#include <retroshare/rsgxsifacehelper.h>
QMap<RsGxsIfaceHelper*, RsGxsUpdateBroadcast*> updateBroadcastMap;
RsGxsUpdateBroadcast::RsGxsUpdateBroadcast(RsGxsIfaceHelper *ifaceImpl) :
QObject(NULL), mIfaceImpl(ifaceImpl)
{
mTimer = new RsProtectedTimer(this);
mTimer->setInterval(1000);
mTimer->setSingleShot(true);
connect(mTimer, SIGNAL(timeout()), this, SLOT(poll()));
}
void RsGxsUpdateBroadcast::cleanup()
{
QMap<RsGxsIfaceHelper*, RsGxsUpdateBroadcast*>::iterator it;
for (it = updateBroadcastMap.begin(); it != updateBroadcastMap.end(); ++it) {
delete(it.value());
}
updateBroadcastMap.clear();
}
RsGxsUpdateBroadcast *RsGxsUpdateBroadcast::get(RsGxsIfaceHelper *ifaceImpl)
{
QMap<RsGxsIfaceHelper*, RsGxsUpdateBroadcast*>::iterator it = updateBroadcastMap.find(ifaceImpl);
if (it != updateBroadcastMap.end()) {
return it.value();
}
RsGxsUpdateBroadcast *updateBroadcast = new RsGxsUpdateBroadcast(ifaceImpl);
updateBroadcastMap.insert(ifaceImpl, updateBroadcast);
updateBroadcast->poll();
return updateBroadcast;
}
void RsGxsUpdateBroadcast::poll()
{
std::map<RsGxsGroupId, std::vector<RsGxsMessageId> > msgs;
std::map<RsGxsGroupId, std::vector<RsGxsMessageId> > msgsMeta;
std::list<RsGxsGroupId> grps;
std::list<RsGxsGroupId> grpsMeta;
if (mIfaceImpl->updated(true, true))
{
mIfaceImpl->msgsChanged(msgs, msgsMeta);
if (!msgs.empty() || !msgsMeta.empty())
{
emit msgsChanged(msgs, msgsMeta);
}
mIfaceImpl->groupsChanged(grps, grpsMeta);
if (!grps.empty() || !grpsMeta.empty())
{
emit grpsChanged(grps, grpsMeta);
}
emit changed();
}
mTimer->start();
}