void PgnImporter::run() { QFile file(m_fileName); QFileInfo fileInfo(m_fileName); const QTime startTime = QTime::currentTime(); static const int updateInterval = 1024; int numReadGames = 0; if (!fileInfo.exists()) { emit error(PgnImporter::FileDoesNotExist); return; } if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { emit error(PgnImporter::IoError); return; } PgnStream pgnStream(&file); QList<const PgnGameEntry*> games; forever { PgnGameEntry* game = new PgnGameEntry; if (m_abort || !game->read(pgnStream)) { delete game; break; } games << game; numReadGames++; if (numReadGames % updateInterval == 0) emit databaseReadStatus(startTime, numReadGames); } PgnDatabase* db = new PgnDatabase(m_fileName); db->setEntries(games); db->setLastModified(fileInfo.lastModified()); emit databaseRead(db); }
bool GameDatabaseManager::readState(const QString& fileName) { QFile stateFile(fileName); if (!stateFile.open(QIODevice::ReadOnly)) return false; QDataStream in(&stateFile); in.setVersion(QDataStream::Qt_4_6); // don't change // Read and verify the magic value quint32 magic; in >> magic; if (magic != GAME_DATABASE_STATE_MAGIC) { qWarning("GameDatabaseManager: bad magic value in state file"); return false; } // Read and verify the version number quint32 version; in >> version; if (version < GAME_DATABASE_STATE_VERSION || version > GAME_DATABASE_STATE_VERSION) { // TODO: Add backward compatibility qWarning("GameDatabaseManager: state file version mismatch"); return false; } // Read the number of databases qint32 dbCount; in >> dbCount; // Read the contents of the databases QString dbFileName; QDateTime dbLastModified; QString dbDisplayName; QList<PgnDatabase*> readDatabases; for (int i = 0; i < dbCount; i++) { in >> dbFileName; in >> dbLastModified; in >> dbDisplayName; // Check if the database exists QFileInfo fileInfo(dbFileName); if (!fileInfo.exists()) { m_modified = true; continue; } // Check if the database has been modified if (fileInfo.lastModified() > dbLastModified) { m_modified = true; importPgnFile(dbFileName); continue; } qint32 dbEntryCount; in >> dbEntryCount; // Read the entries QList<const PgnGameEntry*> entries; for (int j = 0; j < dbEntryCount; j++) { PgnGameEntry* entry = new PgnGameEntry; entry->read(in); entries << entry; } PgnDatabase* db = new PgnDatabase(dbFileName); db->setEntries(entries); db->setLastModified(dbLastModified); db->setDisplayName(dbDisplayName); readDatabases << db; } m_modified = false; m_databases = readDatabases; emit databasesReset(); return true; }