wiRedPanda
Logic Circuit Simulator
Loading...
Searching...
No Matches
RecentFiles.cpp
Go to the documentation of this file.
1// Copyright 2015 - 2026, GIBIS-UNIFESP and the wiRedPanda contributors
2// SPDX-License-Identifier: GPL-3.0-or-later
3
5
6#include <QApplication>
7#include <QFile>
8#include <QFileInfo>
9#include <QThread>
10
11#include "App/Core/Common.h"
12#include "App/Core/Settings.h"
13
15 : QObject(parent)
16{
17 m_files = Settings::recentFiles();
18
19 // Watch every already-known file so the menu updates live if a file is
20 // deleted or renamed outside the application (e.g. by the OS or another app).
21 connect(&m_fileWatcher, &QFileSystemWatcher::fileChanged, this, [this](const QString &filePath) {
22 if (!QFile::exists(filePath)) {
23 m_files.removeAll(filePath);
24 emit recentFilesUpdated();
25 }
26 }, Qt::QueuedConnection);
27
28 // Entries restored from settings were only watched once re-opened (F36);
29 // register them now so the comment above is actually true.
30 for (const QString &filePath : std::as_const(m_files)) {
31 if (QFile::exists(filePath)) {
32 m_fileWatcher.addPath(filePath);
33 }
34 }
35}
36
37void RecentFiles::addRecentFile(const QString &filePath)
38{
39 Q_ASSERT(QCoreApplication::instance()->thread() == QThread::currentThread());
40
41 qCDebug(three) << "Setting recent file to: " << filePath;
42
43 if (!QFile(filePath).exists()) {
44 return;
45 }
46
47 m_fileWatcher.addPath(filePath);
48
49 // Move to front: remove any existing occurrence first so there are no
50 // duplicates, then prepend so the latest file is always at index 0.
51 m_files.removeAll(filePath);
52 m_files.prepend(filePath);
53
54 if (m_files.size() > RecentFiles::maxFiles) {
55 m_files.erase(m_files.begin() + RecentFiles::maxFiles, m_files.end());
56 }
57
58 saveRecentFiles();
59
60 emit recentFilesUpdated();
61}
62
64{
65 Q_ASSERT(QCoreApplication::instance()->thread() == QThread::currentThread());
66
67 // Walk with an index rather than an iterator so we can remove stale entries
68 // in-place without invalidating the loop; the file-system watcher covers
69 // deletions at runtime, but files may have disappeared while the app was
70 // closed (between sessions).
71 int i = 0;
72
73 while (i < m_files.size()) {
74 QFileInfo fileInfo(m_files.at(i));
75
76 if (!fileInfo.exists()) {
77 m_files.removeAt(i);
78 continue;
79 }
80
81 ++i;
82 }
83
84 // Persist the cleaned list so stale paths don't accumulate across restarts.
85 saveRecentFiles();
86
87 return m_files;
88}
89
90void RecentFiles::saveRecentFiles()
91{
93}
Common logging utilities, the Pandaception error type, and helper macros.
#define qCDebug(category)
Definition Common.h:29
Recent-files list management with filesystem watching.
Typed wrappers around QSettings for all application preferences.
static constexpr int maxFiles
Definition RecentFiles.h:31
void recentFilesUpdated()
Emitted whenever the recent-files list changes (add or file deleted).
QStringList recentFiles()
Returns the current list of recent file paths.
RecentFiles(QObject *parent=nullptr)
Constructs the recent-files manager with parent.
void addRecentFile(const QString &filePath)
Prepends filePath to the recent-files list and saves it.
static QStringList recentFiles()
Definition Settings.cpp:174
static void setRecentFiles(const QStringList &files)
Definition Settings.cpp:179