refactor
This commit is contained in:
+183
@@ -0,0 +1,183 @@
|
||||
#include "filemanager.h"
|
||||
#include <QDebug>
|
||||
|
||||
FileManager::FileManager()
|
||||
{
|
||||
m_systemPath = QCoreApplication::applicationDirPath();
|
||||
m_userDataPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
||||
}
|
||||
|
||||
FileManager& FileManager::instance()
|
||||
{
|
||||
static FileManager instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
QString FileManager::systemPath() const
|
||||
{
|
||||
return m_systemPath;
|
||||
}
|
||||
|
||||
QString FileManager::userDataPath() const
|
||||
{
|
||||
return m_userDataPath;
|
||||
}
|
||||
|
||||
QString FileManager::getPath(FileType type, const QString& subPath) const
|
||||
{
|
||||
QString basePath;
|
||||
|
||||
// Определяем базовый путь (системный или пользовательский)
|
||||
switch (type) {
|
||||
case Icons:
|
||||
case SystemStyles:
|
||||
basePath = m_systemPath;
|
||||
break;
|
||||
default:
|
||||
basePath = m_userDataPath;
|
||||
break;
|
||||
}
|
||||
|
||||
// Добавляем подпапку
|
||||
QString folder;
|
||||
switch (type) {
|
||||
case Sounds: folder = "sounds"; break;
|
||||
case Images: folder = "images"; break;
|
||||
case Styles: folder = "styles"; break;
|
||||
case Voices: folder = "voices"; break;
|
||||
case Fonts: folder = "fonts"; break;
|
||||
case Temp: folder = "temp"; break;
|
||||
case Backups: folder = "backups"; break;
|
||||
case Exports: folder = "exports"; break;
|
||||
case Logs: folder = "logs"; break;
|
||||
case Cache: folder = "cache"; break;
|
||||
case WebServerImages: folder = "webserver/images"; break;
|
||||
case WebServerSounds: folder = "webserver/sounds"; break;
|
||||
case Icons: folder = "ico"; break;
|
||||
case SystemStyles: folder = "styles"; break;
|
||||
default: folder = "";
|
||||
}
|
||||
|
||||
QString path = QString("%1/%2").arg(basePath).arg(folder);
|
||||
if (!subPath.isEmpty()) {
|
||||
path += "/" + subPath;
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
QString FileManager::getFullPath(FileType type, const QString& fileName) const
|
||||
{
|
||||
QString path = getPath(type);
|
||||
return QString("%1/%2").arg(path).arg(fileName);
|
||||
}
|
||||
|
||||
bool FileManager::copyToUserData(const QString& sourceFile, FileType type,
|
||||
CopyMode mode, QString* newFileName)
|
||||
{
|
||||
QFileInfo sourceInfo(sourceFile);
|
||||
if (!sourceInfo.exists()) {
|
||||
qWarning() << "FileManager: Source file doesn't exist:" << sourceFile;
|
||||
return false;
|
||||
}
|
||||
|
||||
QString destFolder = getPath(type);
|
||||
QDir dir(destFolder);
|
||||
if (!dir.exists()) {
|
||||
if (!dir.mkpath(".")) {
|
||||
qWarning() << "FileManager: Cannot create folder:" << destFolder;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
QString destFileName = sourceInfo.fileName();
|
||||
QString destPath = getFullPath(type, destFileName);
|
||||
|
||||
// Обрабатываем конфликты имен
|
||||
if (QFile::exists(destPath)) {
|
||||
switch (mode) {
|
||||
case CopyIfNotExists:
|
||||
// Файл уже существует, не копируем
|
||||
if (newFileName) *newFileName = destFileName;
|
||||
return true;
|
||||
|
||||
case CopyAlways:
|
||||
// Перезаписываем существующий файл
|
||||
QFile::remove(destPath);
|
||||
break;
|
||||
|
||||
case CopyWithNewName:
|
||||
// Генерируем уникальное имя
|
||||
int counter = 1;
|
||||
QString baseName = sourceInfo.baseName();
|
||||
QString suffix = sourceInfo.suffix();
|
||||
|
||||
while (QFile::exists(destPath)) {
|
||||
destFileName = QString("%1_%2.%3").arg(baseName).arg(counter).arg(suffix);
|
||||
destPath = getFullPath(type, destFileName);
|
||||
counter++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Копируем файл
|
||||
if (QFile::copy(sourceFile, destPath)) {
|
||||
if (newFileName) *newFileName = destFileName;
|
||||
return true;
|
||||
}
|
||||
|
||||
qWarning() << "FileManager: Failed to copy file from" << sourceFile << "to" << destPath;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool FileManager::existsInUserData(FileType type, const QString& fileName) const
|
||||
{
|
||||
QString path = getFullPath(type, fileName);
|
||||
return QFile::exists(path);
|
||||
}
|
||||
|
||||
void FileManager::initializeFolderStructure()
|
||||
{
|
||||
// Создаем все пользовательские папки
|
||||
for (int i = Sounds; i <= WebServerSounds; ++i) {
|
||||
FileType type = static_cast<FileType>(i);
|
||||
QString path = getPath(type);
|
||||
QDir dir(path);
|
||||
if (!dir.exists()) {
|
||||
dir.mkpath(".");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QString FileManager::getWebPath(FileType type, const QString& fileName) const
|
||||
{
|
||||
QString folder;
|
||||
switch (type) {
|
||||
case WebServerImages: folder = "imgs"; break;
|
||||
case WebServerSounds: folder = "sounds"; break;
|
||||
default: return QString();
|
||||
}
|
||||
|
||||
return QString("/%1/%2").arg(folder).arg(fileName);
|
||||
}
|
||||
|
||||
void FileManager::copyDefaultFiles()
|
||||
{
|
||||
// Копируем системные стили в пользовательскую папку при первом запуске
|
||||
QString systemStylesPath = getPath(SystemStyles);
|
||||
QDir systemStylesDir(systemStylesPath);
|
||||
|
||||
if (systemStylesDir.exists()) {
|
||||
QStringList qssFiles = systemStylesDir.entryList(QStringList() << "*.qss" << "*.QSS", QDir::Files);
|
||||
|
||||
for (const QString& file : qssFiles) {
|
||||
QString sourcePath = systemStylesDir.absoluteFilePath(file);
|
||||
QString destPath = getFullPath(Styles, file);
|
||||
|
||||
if (!QFile::exists(destPath)) {
|
||||
QFile::copy(sourcePath, destPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user