refactor
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
#include "MediaFileManager.h"
|
||||
#include <QDebug>
|
||||
|
||||
MediaFileManager::MediaFileManager()
|
||||
{
|
||||
// Конструктор может быть использован для инициализации
|
||||
}
|
||||
|
||||
bool MediaFileManager::addFile(const QString& name, const QString& filePath)
|
||||
{
|
||||
// Проверка на пустые значения
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user