2026-08-17 01:02:05 +03:00
|
|
|
#include "core/FileStore.h"
|
|
|
|
|
|
|
|
|
|
#include <QDir>
|
|
|
|
|
#include <QFile>
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
|
|
|
|
|
FileStore::FileStore(QObject *parent)
|
|
|
|
|
: QAbstractListModel(parent)
|
|
|
|
|
{
|
|
|
|
|
m_debounce.setSingleShot(true);
|
|
|
|
|
m_debounce.setInterval(300);
|
|
|
|
|
connect(&m_debounce, &QTimer::timeout, this, &FileStore::rebuild);
|
|
|
|
|
connect(&m_watcher, &QFileSystemWatcher::directoryChanged,
|
|
|
|
|
this, &FileStore::scheduleRebuild);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QModelIndex FileStore::index(int row, int column, const QModelIndex &parent) const
|
|
|
|
|
{
|
|
|
|
|
if (parent.isValid() || column != 0 || row < 0 || row >= m_rows.size())
|
|
|
|
|
return QModelIndex();
|
|
|
|
|
return createIndex(row, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QModelIndex FileStore::parent(const QModelIndex &child) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(child)
|
|
|
|
|
return QModelIndex();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int FileStore::rowCount(const QModelIndex &parent) const
|
|
|
|
|
{
|
|
|
|
|
return parent.isValid() ? 0 : m_rows.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant FileStore::data(const QModelIndex &index, int role) const
|
|
|
|
|
{
|
|
|
|
|
if (!index.isValid() || index.row() < 0 || index.row() >= m_rows.size())
|
|
|
|
|
return QVariant();
|
|
|
|
|
const Row &r = m_rows.at(index.row());
|
|
|
|
|
switch (role) {
|
|
|
|
|
case NameRole: return r.name;
|
|
|
|
|
case PathRole: return r.path;
|
|
|
|
|
case DepthRole: return r.depth;
|
|
|
|
|
case IsDirRole: return r.isDir;
|
|
|
|
|
case ExpandedRole: return r.expanded;
|
|
|
|
|
case HasChildrenRole: return r.hasChildren;
|
|
|
|
|
case IconRole: return iconFor(r);
|
|
|
|
|
default: return QVariant();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QHash<int, QByteArray> FileStore::roleNames() const
|
|
|
|
|
{
|
|
|
|
|
QHash<int, QByteArray> roles;
|
|
|
|
|
roles[NameRole] = "display";
|
|
|
|
|
roles[PathRole] = "path";
|
|
|
|
|
roles[DepthRole] = "depth";
|
|
|
|
|
roles[IsDirRole] = "isDir";
|
|
|
|
|
roles[ExpandedRole] = "expanded";
|
|
|
|
|
roles[HasChildrenRole] = "hasChildren";
|
|
|
|
|
roles[IconRole] = "icon";
|
|
|
|
|
return roles;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString FileStore::iconFor(const Row &r) const
|
|
|
|
|
{
|
|
|
|
|
if (r.isDir)
|
|
|
|
|
return r.hasChildren ? QStringLiteral("folder") : QStringLiteral("folder-empty");
|
|
|
|
|
const QString ext = QFileInfo(r.name).suffix().toLower();
|
|
|
|
|
if (ext == "md" || ext == "markdown") return QStringLiteral("file-md");
|
|
|
|
|
if (ext == "html" || ext == "htm") return QStringLiteral("file-html");
|
|
|
|
|
if (ext == "pdf") return QStringLiteral("file-pdf");
|
|
|
|
|
if (ext == "png" || ext == "jpg" || ext == "jpeg" || ext == "gif"
|
|
|
|
|
|| ext == "svg" || ext == "webp" || ext == "bmp")
|
|
|
|
|
return QStringLiteral("file-img");
|
|
|
|
|
if (ext == "txt" || ext == "log") return QStringLiteral("file-text");
|
|
|
|
|
if (ext == "c" || ext == "cpp" || ext == "h" || ext == "hpp" || ext == "py"
|
|
|
|
|
|| ext == "go" || ext == "rs" || ext == "js" || ext == "ts" || ext == "json"
|
|
|
|
|
|| ext == "yaml" || ext == "yml" || ext == "sh" || ext == "bash" || ext == "css")
|
|
|
|
|
return QStringLiteral("file-code");
|
|
|
|
|
return QStringLiteral("file");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString FileStore::openVault(const QString &path)
|
|
|
|
|
{
|
|
|
|
|
const QString clean = QDir::cleanPath(path);
|
|
|
|
|
if (!QDir(clean).exists())
|
|
|
|
|
return QStringLiteral("Папка «%1» не существует.").arg(clean);
|
|
|
|
|
|
|
|
|
|
m_vault = clean;
|
|
|
|
|
m_expanded.clear();
|
|
|
|
|
rebuild();
|
|
|
|
|
registerWatcher();
|
|
|
|
|
|
|
|
|
|
emit vaultChanged();
|
|
|
|
|
emit readyChanged();
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FileStore::rebuild()
|
|
|
|
|
{
|
|
|
|
|
if (!m_watcher.directories().isEmpty())
|
|
|
|
|
m_watcher.removePaths(m_watcher.directories());
|
|
|
|
|
QVector<Row> rows;
|
|
|
|
|
collectDir(m_vault, 0, rows);
|
|
|
|
|
|
|
|
|
|
beginResetModel();
|
|
|
|
|
m_rows = std::move(rows);
|
|
|
|
|
endResetModel();
|
|
|
|
|
|
|
|
|
|
// Регистрируем все каталоги для слежения
|
|
|
|
|
for (const Row &r : m_rows) {
|
|
|
|
|
if (r.isDir && !m_watcher.directories().contains(r.path))
|
|
|
|
|
m_watcher.addPath(r.path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FileStore::collectDir(const QString &dir, int depth, QVector<Row> &out) const
|
|
|
|
|
{
|
|
|
|
|
QDir d(dir);
|
|
|
|
|
d.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::NoSymLinks
|
|
|
|
|
| QDir::System);
|
|
|
|
|
const QFileInfoList entries = d.entryInfoList();
|
|
|
|
|
|
|
|
|
|
QVector<Row> dirs, files;
|
|
|
|
|
for (const QFileInfo &fi : entries) {
|
|
|
|
|
const QString path = QDir::cleanPath(fi.absoluteFilePath());
|
|
|
|
|
if (isIgnored(path))
|
|
|
|
|
continue;
|
|
|
|
|
Row row;
|
|
|
|
|
row.path = path;
|
|
|
|
|
row.name = fi.fileName();
|
|
|
|
|
row.depth = depth;
|
|
|
|
|
row.isDir = fi.isDir();
|
|
|
|
|
if (row.isDir) {
|
|
|
|
|
const int childCount = QDir(path)
|
|
|
|
|
.entryList(QDir::AllEntries | QDir::NoDotAndDotDot
|
|
|
|
|
| QDir::System)
|
|
|
|
|
.count();
|
|
|
|
|
row.hasChildren = childCount > 0;
|
|
|
|
|
}
|
|
|
|
|
row.expanded = row.isDir && m_expanded.contains(path);
|
|
|
|
|
(row.isDir ? dirs : files).append(row);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::sort(dirs.begin(), dirs.end(), [](const Row &a, const Row &b) {
|
|
|
|
|
return a.name.toLower() < b.name.toLower();
|
|
|
|
|
});
|
|
|
|
|
std::sort(files.begin(), files.end(), [](const Row &a, const Row &b) {
|
|
|
|
|
return a.name.toLower() < b.name.toLower();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for (const Row &r : dirs) {
|
|
|
|
|
out.append(r);
|
|
|
|
|
if (r.expanded)
|
|
|
|
|
collectDir(r.path, depth + 1, out);
|
|
|
|
|
}
|
|
|
|
|
for (const Row &r : files)
|
|
|
|
|
out.append(r);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FileStore::isIgnored(const QString &path) const
|
|
|
|
|
{
|
|
|
|
|
const QString name = QFileInfo(path).fileName();
|
|
|
|
|
return name == QLatin1String(".git") || name == QLatin1String(".flora")
|
|
|
|
|
|| name == QLatin1String(".stfolder");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FileStore::registerWatcher()
|
|
|
|
|
{
|
|
|
|
|
const auto dirs = m_watcher.directories();
|
|
|
|
|
for (const QString &d : dirs)
|
|
|
|
|
m_watcher.removePath(d);
|
|
|
|
|
QVector<QString> dirsToWatch;
|
|
|
|
|
for (const Row &r : m_rows)
|
|
|
|
|
if (r.isDir)
|
|
|
|
|
dirsToWatch.append(r.path);
|
|
|
|
|
if (!dirsToWatch.isEmpty())
|
|
|
|
|
m_watcher.addPaths(dirsToWatch);
|
|
|
|
|
if (!m_vault.isEmpty() && QDir(m_vault).exists())
|
|
|
|
|
m_watcher.addPath(m_vault);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FileStore::scheduleRebuild()
|
|
|
|
|
{
|
|
|
|
|
if (!m_vault.isEmpty())
|
|
|
|
|
m_debounce.start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FileStore::toggleExpand(int row)
|
|
|
|
|
{
|
|
|
|
|
if (row < 0 || row >= m_rows.size() || !m_rows[row].isDir)
|
|
|
|
|
return;
|
|
|
|
|
const QString path = m_rows[row].path;
|
|
|
|
|
if (m_expanded.contains(path))
|
|
|
|
|
m_expanded.remove(path);
|
|
|
|
|
else
|
|
|
|
|
m_expanded.insert(path);
|
|
|
|
|
rebuild();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FileStore::expandAll()
|
|
|
|
|
{
|
|
|
|
|
for (const Row &r : m_rows)
|
|
|
|
|
if (r.isDir)
|
|
|
|
|
m_expanded.insert(r.path);
|
|
|
|
|
rebuild();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FileStore::collapseAll()
|
|
|
|
|
{
|
|
|
|
|
m_expanded.clear();
|
|
|
|
|
rebuild();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int FileStore::rowOf(const QString &path) const
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < m_rows.size(); ++i)
|
|
|
|
|
if (m_rows[i].path == path)
|
|
|
|
|
return i;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString FileStore::validateName(const QString &name) const
|
|
|
|
|
{
|
|
|
|
|
const QString n = name.trimmed();
|
|
|
|
|
if (n.isEmpty())
|
|
|
|
|
return QStringLiteral("Имя не может быть пустым.");
|
|
|
|
|
if (n.contains('/') || n.contains('\\') || n == "." || n == "..")
|
|
|
|
|
return QStringLiteral("Имя содержит недопустимые символы (/, \\).");
|
|
|
|
|
if (n.length() > 200)
|
|
|
|
|
return QStringLiteral("Имя слишком длинное.");
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString FileStore::parentDirOfRow(int row) const
|
|
|
|
|
{
|
|
|
|
|
if (row <= 0)
|
|
|
|
|
return m_vault;
|
|
|
|
|
const Row &cur = m_rows[row];
|
|
|
|
|
for (int i = row - 1; i >= 0; --i)
|
|
|
|
|
if (m_rows[i].isDir && m_rows[i].depth < cur.depth)
|
|
|
|
|
return m_rows[i].path;
|
|
|
|
|
return m_vault;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString FileStore::createFile(const QString &parentPath, const QString &name)
|
|
|
|
|
{
|
|
|
|
|
const QString dir = parentPath.isEmpty() ? m_vault : parentPath;
|
|
|
|
|
const QString err = validateName(name);
|
|
|
|
|
if (!err.isEmpty())
|
|
|
|
|
return err;
|
|
|
|
|
const QString path = QDir(dir).filePath(name);
|
|
|
|
|
if (QFileInfo::exists(path))
|
|
|
|
|
return QStringLiteral("Файл с таким именем уже существует.");
|
|
|
|
|
QFile f(path);
|
|
|
|
|
if (!f.open(QIODevice::WriteOnly))
|
|
|
|
|
return QStringLiteral("Не удалось создать файл: %1").arg(path);
|
|
|
|
|
f.close();
|
|
|
|
|
rebuild();
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-17 14:18:43 +03:00
|
|
|
QString FileStore::dailyNotePath(const QString &dateStr)
|
|
|
|
|
{
|
|
|
|
|
if (m_vault.isEmpty())
|
|
|
|
|
return QString();
|
|
|
|
|
if (!QDir().mkpath(m_vault + "/Журнал"))
|
|
|
|
|
return QString();
|
|
|
|
|
const QString path = m_vault + "/Журнал/" + dateStr + ".md";
|
|
|
|
|
if (!QFileInfo::exists(path)) {
|
|
|
|
|
QFile f(path);
|
|
|
|
|
if (f.open(QIODevice::WriteOnly)) {
|
|
|
|
|
f.write(QStringLiteral("# %1\n\n").arg(dateStr).toUtf8());
|
|
|
|
|
f.close();
|
|
|
|
|
} else {
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
m_expanded.insert(m_vault + "/Журнал");
|
|
|
|
|
scheduleRebuild();
|
|
|
|
|
}
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-17 01:02:05 +03:00
|
|
|
QString FileStore::createFolder(const QString &parentPath, const QString &name)
|
|
|
|
|
{
|
|
|
|
|
const QString dir = parentPath.isEmpty() ? m_vault : parentPath;
|
|
|
|
|
const QString err = validateName(name);
|
|
|
|
|
if (!err.isEmpty())
|
|
|
|
|
return err;
|
|
|
|
|
const QString path = QDir(dir).filePath(name);
|
|
|
|
|
if (QFileInfo::exists(path))
|
|
|
|
|
return QStringLiteral("Папка с таким именем уже существует.");
|
|
|
|
|
if (!QDir().mkpath(path))
|
|
|
|
|
return QStringLiteral("Не удалось создать папку: %1").arg(path);
|
|
|
|
|
m_expanded.insert(path);
|
|
|
|
|
rebuild();
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString FileStore::renameItem(const QString &path, const QString &newName)
|
|
|
|
|
{
|
|
|
|
|
const QString n = newName.trimmed();
|
|
|
|
|
const QString err = validateName(n);
|
|
|
|
|
if (!err.isEmpty())
|
|
|
|
|
return err;
|
|
|
|
|
QFileInfo fi(path);
|
|
|
|
|
const QString newPath = QDir(fi.absolutePath()).filePath(n);
|
|
|
|
|
if (path == newPath)
|
|
|
|
|
return QString();
|
|
|
|
|
if (QFileInfo::exists(newPath))
|
|
|
|
|
return QStringLiteral("Имя уже занято.");
|
|
|
|
|
if (!QFile::rename(path, newPath) && !QDir().rename(path, newPath))
|
|
|
|
|
return QStringLiteral("Не удалось переименовать: %1").arg(path);
|
|
|
|
|
if (fi.isDir()) {
|
|
|
|
|
const QString oldAbs = QDir::cleanPath(path);
|
|
|
|
|
const QString newAbs = QDir::cleanPath(newPath);
|
|
|
|
|
if (m_expanded.remove(oldAbs))
|
|
|
|
|
m_expanded.insert(newAbs);
|
|
|
|
|
}
|
|
|
|
|
rebuild();
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString FileStore::removeItem(const QString &path)
|
|
|
|
|
{
|
|
|
|
|
QFileInfo fi(path);
|
|
|
|
|
if (fi.isDir()) {
|
|
|
|
|
QDir d(path);
|
|
|
|
|
if (!d.removeRecursively())
|
|
|
|
|
return QStringLiteral("Не удалось удалить папку «%1».").arg(path);
|
|
|
|
|
m_expanded.remove(QDir::cleanPath(path));
|
|
|
|
|
} else {
|
|
|
|
|
if (!QFile::remove(path))
|
|
|
|
|
return QStringLiteral("Не удалось удалить файл «%1».").arg(path);
|
|
|
|
|
}
|
|
|
|
|
rebuild();
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString FileStore::moveItem(const QString &srcPath, const QString &destDir)
|
|
|
|
|
{
|
|
|
|
|
if (srcPath.isEmpty() || destDir.isEmpty())
|
|
|
|
|
return QStringLiteral("Не указаны пути.");
|
|
|
|
|
const QString src = QDir::cleanPath(srcPath);
|
|
|
|
|
const QString dstDir = QDir::cleanPath(destDir);
|
|
|
|
|
if (src == dstDir || QFileInfo(dstDir).filePath() == QFileInfo(src).absolutePath())
|
|
|
|
|
return QString();
|
|
|
|
|
|
|
|
|
|
// Запрещаем перемещение папки в саму себя
|
|
|
|
|
if (src.startsWith(dstDir + '/'))
|
|
|
|
|
return QStringLiteral("Нельзя переместить папку в саму себя.");
|
|
|
|
|
|
|
|
|
|
const QFileInfo fi(src);
|
|
|
|
|
const QString newPath = QDir(dstDir).filePath(fi.fileName());
|
|
|
|
|
if (QFileInfo::exists(newPath))
|
|
|
|
|
return QStringLiteral("Элемент с таким именем уже есть в папке назначения.");
|
|
|
|
|
|
|
|
|
|
bool ok = false;
|
|
|
|
|
if (fi.isDir())
|
|
|
|
|
ok = QDir().rename(src, newPath);
|
|
|
|
|
else
|
|
|
|
|
ok = QFile::rename(src, newPath);
|
|
|
|
|
if (!ok)
|
|
|
|
|
return QStringLiteral("Не удалось переместить «%1».").arg(src);
|
|
|
|
|
|
|
|
|
|
rebuild();
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString FileStore::itemName(int row) const
|
|
|
|
|
{
|
|
|
|
|
if (row < 0 || row >= m_rows.size())
|
|
|
|
|
return QString();
|
|
|
|
|
return m_rows[row].name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString FileStore::itemPath(int row) const
|
|
|
|
|
{
|
|
|
|
|
if (row < 0 || row >= m_rows.size())
|
|
|
|
|
return QString();
|
|
|
|
|
return m_rows[row].path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FileStore::itemIsDir(int row) const
|
|
|
|
|
{
|
|
|
|
|
return row >= 0 && row < m_rows.size() && m_rows[row].isDir;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FileStore::itemHasChildren(int row) const
|
|
|
|
|
{
|
|
|
|
|
return row >= 0 && row < m_rows.size() && m_rows[row].hasChildren;
|
|
|
|
|
}
|