bool ClearManager::addTask(int task) { QMutexLocker lock(&data->mutex); if (!data->working) return false; if (!data->tasks.isEmpty() && (data->tasks.at(0) == ClearManagerAll)) return true; if (task == ClearManagerAll) { data->tasks.clear(); } else { if (task & ClearManagerImages) { if (data->images.isEmpty()) { data->images = _storageMap; } else { for (StorageMap::const_iterator i = _storageMap.cbegin(), e = _storageMap.cend(); i != e; ++i) { StorageKey k = i.key(); while (data->images.constFind(k) != data->images.cend()) { ++k.second; } data->images.insert(k, i.value()); } } _storageMap.clear(); _storageFilesSize = 0; _mapChanged = true; _writeMap(); } for (int32 i = 0, l = data->tasks.size(); i < l; ++i) { if (data->tasks.at(i) == task) return true; } } data->tasks.push_back(task); return true; }
void ClearManager::onStart() { while (true) { int task = 0; bool result = false; StorageMap images; { QMutexLocker lock(&data->mutex); if (data->tasks.isEmpty()) { data->working = false; break; } task = data->tasks.at(0); images = data->images; } switch (task) { case ClearManagerAll: result = (QDir(cTempDir()).removeRecursively() && QDir(_basePath).removeRecursively()); break; case ClearManagerDownloads: result = QDir(cTempDir()).removeRecursively(); break; case ClearManagerImages: for (StorageMap::const_iterator i = images.cbegin(), e = images.cend(); i != e; ++i) { clearKey(i.value().first, false); } result = true; break; } { QMutexLocker lock(&data->mutex); if (data->tasks.at(0) == task) { data->tasks.pop_front(); if (data->tasks.isEmpty()) { data->working = false; } } if (result) { emit succeed(task, data->working ? 0 : this); } else { emit failed(task, data->working ? 0 : this); } if (!data->working) break; } } }
void writeImage(const StorageKey &location, const StorageImageSaved &image, bool overwrite) { if (!_working()) return; qint32 size = _storageImageSize(image.data.size()); StorageMap::const_iterator i = _storageMap.constFind(location); if (i == _storageMap.cend()) { i = _storageMap.insert(location, FileDesc(genKey(), size)); _storageFilesSize += size; _mapChanged = true; _writeMap(); } else if (!overwrite) { return; } EncryptedDescriptor data(sizeof(quint64) * 2 + sizeof(quint32) + sizeof(quint32) + image.data.size()); data.stream << quint64(location.first) << quint64(location.second) << quint32(image.type) << image.data; FileWriteDescriptor file(i.value().first, false); file.writeEncrypted(data); if (i.value().second != size) { _storageFilesSize += size; _storageFilesSize -= i.value().second; _storageMap[location].second = size; } }