TTW_Bot/mediafilemanager.cpp

141 lines
4.0 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "MediaFileManager.h"
#include <QDebug>
MediaFileManager::MediaFileManager()
{
// Конструктор может быть использован для инициализации
}
bool MediaFileManager::addFile(const QString& name, const QString& filePath)
{
qDebug() << "Добавляем ебаный файл";
// Проверка на пустые значения
if (name.isEmpty() || filePath.isEmpty()) {
qWarning() << "Имя файла или путь не могут быть пустыми";
return false;
}
// Проверка на уникальность имени
if (contains(name)) {
qWarning() << "Файл с именем" << name << "уже существует";
return false;
}
// Добавление нового файла
mediaFiles.append(MediaFile(name, filePath));
qDebug() << "Файл" << name << "добавлен с путем:" << filePath;
return true;
}
bool MediaFileManager::removeFile(const QString& name)
{
int index = findFileIndex(name);
if (index == -1) {
qWarning() << "Файл с именем" << name << "не найден";
return false;
}
mediaFiles.remove(index);
qDebug() << "Файл" << name << "удален";
return true;
}
bool MediaFileManager::updateFile(const QString& oldName, const QString& newName, const QString& newFilePath)
{
int index = findFileIndex(oldName);
if (index == -1) {
qWarning() << "Файл с именем" << oldName << "не найден";
return false;
}
// Проверяем, не используется ли новое имя другим файлом
if (oldName != newName && contains(newName)) {
qWarning() << "Файл с именем" << newName << "уже существует";
return false;
}
// Обновляем информацию о файле
mediaFiles[index].name = newName;
mediaFiles[index].filePath = newFilePath;
qDebug() << "Файл обновлен:" << oldName << "->" << newName << "путь:" << newFilePath;
return true;
}
QString MediaFileManager::getFilePathByName(const QString& name) const
{
int index = findFileIndex(name);
if (index == -1) {
qWarning() << "Файл с именем" << name << "не найден";
return QString();
}
return mediaFiles[index].filePath;
}
int MediaFileManager::getFileCount() const
{
return mediaFiles.size();
}
QVector<MediaFile> MediaFileManager::getAllFiles() const
{
return mediaFiles;
}
bool MediaFileManager::contains(const QString& name) const
{
return findFileIndex(name) != -1;
}
int MediaFileManager::findFileIndex(const QString& name) const
{
for (int i = 0; i < mediaFiles.size(); ++i) {
if (mediaFiles[i].name == name) {
return i;
}
}
return -1;
}
bool MediaFileManager::renameFile(const QString& oldName, const QString& newName)
{
int index = findFileIndex(oldName);
if (index == -1) {
qWarning() << "Файл с именем" << oldName << "не найден";
return false;
}
// Проверяем, не используется ли новое имя другим файлом
if (oldName != newName && contains(newName)) {
qWarning() << "Файл с именем" << newName << "уже существует";
return false;
}
QString oldPath = mediaFiles[index].filePath;
mediaFiles[index].name = newName;
qDebug() << "Файл переименован:" << oldName << "->" << newName;
return true;
}
bool MediaFileManager::updateFilePath(const QString& name, const QString& newFilePath)
{
int index = findFileIndex(name);
if (index == -1) {
qWarning() << "Файл с именем" << name << "не найден";
return false;
}
QString oldPath = mediaFiles[index].filePath;
mediaFiles[index].filePath = newFilePath;
qDebug() << "Путь файла обновлен:" << name << "новый путь:" << newFilePath;
return true;
}