示例#1
0
bool MailFolder::compact(unsigned level)
{
    // sync first so that you are sure the database is ok
    sync();

    if ( level ) {
        qDebug("Folder compressing isn't yet available.");
    }

    QCString mboxInfo;
    mboxInfo = "From - " + QDateTime::currentDateTime().toString() + "\r\n";

    QFile descriptorFile( getDescriptorFileName() );
    if ( !descriptorFile.open(IO_ReadOnly) )
        return false;

    QFile newDescriptorFile( getDescriptorFileName()+"new" );
    if ( !newDescriptorFile.open(IO_WriteOnly) )
        return false;

    QFile messageFile( getMessagesFileName() );
    if ( !messageFile.open(IO_ReadOnly) )
        return false;

    QFile newMessageFile( getMessagesFileName()+"new" );
    if ( !newMessageFile.open(IO_WriteOnly) )
        return false;

    IndexClass * index = 0L;
    QByteArray buffer;

    for (QDictIterator<IndexClass> it(indexCollection); it.current(); ++it) {
        index = it.current();

        if ( !index->getDescriptorLength() ||
                !index->getUniblockLength() ) {
            qDebug("The index file seems to be broken :(");
            indexCollection.remove( index->getID() );
            continue;
        }

        // read the descriptor
        buffer.resize( index->getDescriptorLength() );
        descriptorFile.at( index->getDescriptorOffset() );
        descriptorFile.readBlock(buffer.data(), buffer.size());

        // write the descriptor
        index->setDescriptorOffset( newDescriptorFile.at() );
        newDescriptorFile.writeBlock( buffer );

        // read the message
        buffer.resize( index->getUniblockLength() );
        messageFile.at( index->getUniblockOffset() );
        messageFile.readBlock(buffer.data(), buffer.size());

        // write the message
        // The MBOX line isn't right but the line isn't used too.
        // So, I decided to just store only the current date.
        newMessageFile.writeBlock((const char *)mboxInfo, mboxInfo.length());
        index->setUniblockOffset(newMessageFile.at(), true);

        newMessageFile.writeBlock( buffer );
        newMessageFile.writeBlock("\r\n", 2);
    }

    descriptorFile.close();
    newDescriptorFile.close();
    messageFile.close();
    newMessageFile.close();

    buffer.truncate(0);

    // replace the old files
    descriptorFile.remove();
    messageFile.remove();

    QDir folder( getStorageDevice() );
    folder.rename( newDescriptorFile.name(), getDescriptorFileName(), true );
    folder.rename( newMessageFile.name(), getMessagesFileName(), true );

    saveIndex();

    return true;
}
示例#2
0
文件: DkUpnp.cpp 项目: a17r/nomacs
bool DkUpnpDeviceHost::startDevicehost(QString pathToConfig) {
	qDebug() << "pathToConfig:" << pathToConfig;
	qDebug() << "starting DeviceHost";
	QFile f(pathToConfig);
	if (!f.exists()) {
		qDebug() << "DkUpnpDeviceHost: config file not found";
		return false;
	}

	QUuid uuid = QUuid::createUuid();
	QString uuidString = uuid.toString();
	uuidString.replace("{","");
	uuidString.replace("}","");
	QString newXMLpath = QDir::tempPath() + QDir::separator() + uuidString + ".xml";
	

	QByteArray fileData;
	f.open(QIODevice::ReadOnly);
	//f.seek(0);
	fileData = f.readAll();
	f.close();
	QString fileText(fileData);
	fileText.replace("insert-new-uuid-here", uuidString);
#ifdef WIN32
	fileText.replace("nomacs-service.xml", QDir::temp().dirName()+"/nomacs-service.xml");
#else
	fileText.replace("nomacs-service.xml", "/nomacs-service.xml");
#endif // WIN32
	
	QFile newDescriptorFile(newXMLpath);
	newDescriptorFile.open(QIODevice::WriteOnly);
	newDescriptorFile.write(fileText.toUtf8());
	qDebug() << "writing file:" << newXMLpath;
	newDescriptorFile.close();

	QFileInfo fileInfo = QFileInfo(f);
	QFile serviceXML(fileInfo.absolutePath() + QDir::separator() + "nomacs-service.xml");
	if (!serviceXML.open(QIODevice::ReadOnly))
		qDebug() << "nomacs-service.xml file does not exist";
	
	// copy the resource file to the user tmp folder
	QString newServiceXMLPath = QDir::tempPath()+ QDir::separator() + "nomacs-service.xml";
	QFile tmpServiceXmlFile(newServiceXMLPath);
	tmpServiceXmlFile.open(QIODevice::WriteOnly);
	tmpServiceXmlFile.write(serviceXML.readAll());
	tmpServiceXmlFile.close();
	serviceXML.close();
	
	//if(!QFile::exists(newServiceXMLPath)) {
	//	if (!serviceXML.copy(newServiceXMLPath))
	//		qDebug() << "unable to copy nomacs-service.xml to " << newServiceXMLPath << ", perhaps files already exists";
	//}
	QFile newServiceXMLFile(QDir::tempPath()+ QDir::separator() + "nomacs-service.xml");
	serviceXML.close();
	DkUpnpDeviceModelCreator creator;

	Herqq::Upnp::HDeviceHostConfiguration hostConfig;
	hostConfig.setDeviceModelCreator(creator);
	
	Herqq::Upnp::HDeviceConfiguration config;
	//config.setPathToDeviceDescription(pathToConfig);
	config.setPathToDeviceDescription(newXMLpath);
	
	hostConfig.add(config);
	
	bool retVal = init(hostConfig);
	if (!retVal) {
		qDebug() << "error while initializing device host:\n" << errorDescription();
	}

	if(!newDescriptorFile.remove()) 
		qDebug() << "unable to remove upnp device.xml file";
	// this->serviceXMLPath = newServiceXMLPath;

	if (!tmpServiceXmlFile.remove())
		qDebug() << tmpServiceXmlFile.errorString();

	//qDebug() << "newServiceXMLPath" << newServiceXMLPath;
	//if(!QFile::remove(newServiceXMLPath))
		//qDebug() << "unable to remove upnp service.xml file";
	return retVal;
}