/
LostDead
/
G.E.E.Z
Обзор
Документация
Войти
/
LostDead
/
G.E.E.Z
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
core/gifmanager.cpp
97 строк
3 KB
Максим Антонов
upload files
10 май 2025, 21:29
10 май 2025, 21:29
d8b4715
Код
Авторство
О чём код?
#include "core/gifmanager.h" #include <QFileDialog> #include <QMessageBox> GifManager::GifManager(const QString &storagePath, QObject *parent) : QObject(parent), m_storagePath(storagePath) { QDir().mkpath(m_storagePath); } void GifManager::loadGifs() { QDir dir(m_storagePath); for(const QString &file : dir.entryList({"*.gif"})) { QMovie *movie = new QMovie(dir.filePath(file), QByteArray(), this); if(movie->isValid()) { m_gifs[file] = movie; m_gifPaths.insert(file,dir.filePath(file)); } } } bool GifManager::addGif(const QString &path) { // Проверка существования исходного файла if (!QFile::exists(path)) { emit error("Файл не найден: " + path); return false; } // Получение имени файла до копирования const QString fileName = QFileInfo(path).fileName(); // Проверка на дубликат if (m_gifs.contains(fileName)) { emit error("Гифка уже добавлена: " + fileName); return false; } // Копирование в хранилище QString destPath; if (!copyToStorage(path, destPath)) { emit error("Ошибка копирования файла: " + path); return false; } // Создание и проверка QMovie QMovie* movie = new QMovie(destPath, QByteArray(), this); if (!movie->isValid()) { delete movie; emit error("Некорректный формат гифки: " + destPath); return false; } // Атомарное обновление структур данных m_gifs.insert(fileName, movie); m_gifPaths.insert(fileName, destPath); emit gifAdded(fileName); return true; } QString GifManager::getGifPath(const QString &name) const { return m_gifPaths.value(name, ""); } bool GifManager::removeGif(const QString &name) { if (!m_gifs.contains(name)) return false; QMovie* movie = m_gifs.take(name); delete movie; QFile file(m_storagePath + "/" + name); if(!file.remove()) { emit error("Ошибка удаления файла"); return false; } m_gifPaths.remove(name); return true; } QMovie* GifManager::getGif(const QString &name) const { return m_gifs.value(name, nullptr); } QStringList GifManager::gifNames() const { return m_gifs.keys(); } bool GifManager::copyToStorage(const QString &source, QString &dest) { QFileInfo srcInfo(source); dest = m_storagePath + "/" + srcInfo.fileName(); if(QFile::exists(dest)) { QMessageBox::StandardButton reply = QMessageBox::question( nullptr, "File exists", "Overwrite?", QMessageBox::Yes | QMessageBox::No ); if(reply == QMessageBox::No) return false; } return QFile::copy(source, dest); }