void KviSharedFilesManager::save(const QString & szFilename)
{
	KviConfigurationFile cfg(szFilename, KviConfigurationFile::Write);
	cfg.clear();
	cfg.setGroup("PermanentFileOffers");

	KviPointerHashTableIterator<QString, KviSharedFileList> it(*m_pSharedListDict);
	int i = 0;
	while(KviSharedFileList * pList = it.current())
	{
		for(KviSharedFile * pFile = pList->first(); pFile; pFile = pList->next())
		{
			if(((int)(pFile->expireTime())) == 0)
			{
				QString szTmp;
				szTmp = QString("%1FName").arg(i);
				cfg.writeEntry(szTmp, it.currentKey());
				szTmp = QString("%1FilePath").arg(i);
				cfg.writeEntry(szTmp, pFile->absFilePath());
				szTmp = QString("%1UserMask").arg(i);
				cfg.writeEntry(szTmp, pFile->userMask());
				++i;
			}
		}
		++it;
	}
	cfg.writeEntry("NEntries", i);
}
KviSharedFile * KviSharedFilesManager::addSharedFile(const QString & szName, const QString & szAbsPath, const QString & szMask, int timeoutInSecs)
{
	QFileInfo inf(szAbsPath);
	if(inf.exists() && inf.isFile() && inf.isReadable() && (inf.size() > 0))
	{
		// First find the list
		KviSharedFileList * l = m_pSharedListDict->find(szName);
		if(!l)
		{
			l = new KviSharedFileList;
			l->setAutoDelete(true);
			m_pSharedListDict->replace(szName, l);
		}

		// Now insert
		KviSharedFile * o = new KviSharedFile(szName, szAbsPath, szMask, timeoutInSecs > 0 ? (((int)(time(nullptr))) + timeoutInSecs) : 0, inf.size());

		doInsert(l, o);

		if(((int)o->expireTime()) > 0)
		{
			if(!m_pCleanupTimer->isActive())
				m_pCleanupTimer->start(60000);
		}

		emit sharedFileAdded(o);

		return o;
	}
	else
	{
		qDebug("File %s unreadable: can't add offer", szAbsPath.toUtf8().data());
		return nullptr;
	}
}
void KviSharedFilesManager::cleanup()
{
	KviPointerHashTableIterator<QString, KviSharedFileList> it(*m_pSharedListDict);
	time_t curTime = time(nullptr);

	bool bOtherStuffToCleanup = false;

	KviPointerList<QString> lDying;
	lDying.setAutoDelete(true);

	while(KviSharedFileList * l = it.current())
	{
		KviPointerList<KviSharedFile> tmp;
		tmp.setAutoDelete(false);
		for(KviSharedFile * o = l->first(); o; o = l->next())
		{
			if(o->expireTime() > 0)
			{
				if(((int)o->expireTime()) <= ((int)curTime))
				{
					tmp.append(o);
				}
				else
				{
					bOtherStuffToCleanup = true;
				}
			}
		}
		for(KviSharedFile * fo = tmp.first(); fo; fo = tmp.next())
		{
			l->removeRef(fo);
			emit sharedFileRemoved(fo);
		}
		if(l->count() == 0)
			lDying.append(new QString(it.currentKey()));

		++it;
	}

	for(QString * pDyingKey = lDying.first(); pDyingKey; pDyingKey = lDying.next())
		m_pSharedListDict->remove(*pDyingKey);

	if(!bOtherStuffToCleanup)
		m_pCleanupTimer->stop();
}