Core: Use FilePaths for recent documents

Change-Id: I453e7dd2c5d8d357b651392fa7d7f0baf07173f7
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
David Schulz
2021-06-08 14:34:05 +02:00
parent b29ee5493b
commit 95a7dc4857
4 changed files with 17 additions and 20 deletions

View File

@@ -1336,22 +1336,22 @@ void DocumentManager::checkForReload()
} }
/*! /*!
Adds the \a fileName to the list of recent files. Associates the file to Adds the \a filePath to the list of recent files. Associates the file to
be reopened with the editor that has the specified \a editorId, if possible. be reopened with the editor that has the specified \a editorId, if possible.
\a editorId defaults to the empty ID, which lets \QC figure out \a editorId defaults to the empty ID, which lets \QC figure out
the best editor itself. the best editor itself.
*/ */
void DocumentManager::addToRecentFiles(const QString &fileName, Id editorId) void DocumentManager::addToRecentFiles(const Utils::FilePath &filePath, Id editorId)
{ {
if (fileName.isEmpty()) if (filePath.isEmpty())
return; return;
const QString fileKey = filePathKey(fileName, KeepLinks); const QString fileKey = filePathKey(filePath.toString(), KeepLinks);
Utils::erase(d->m_recentFiles, [fileKey](const RecentFile &file) { Utils::erase(d->m_recentFiles, [fileKey](const RecentFile &file) {
return fileKey == filePathKey(file.first, DocumentManager::KeepLinks); return fileKey == filePathKey(file.first.toString(), DocumentManager::KeepLinks);
}); });
while (d->m_recentFiles.count() >= EditorManagerPrivate::maxRecentFiles()) while (d->m_recentFiles.count() >= EditorManagerPrivate::maxRecentFiles())
d->m_recentFiles.removeLast(); d->m_recentFiles.removeLast();
d->m_recentFiles.prepend(RecentFile(fileName, editorId)); d->m_recentFiles.prepend(RecentFile(filePath, editorId));
} }
/*! /*!
@@ -1373,10 +1373,10 @@ QList<DocumentManager::RecentFile> DocumentManager::recentFiles()
void DocumentManager::saveSettings() void DocumentManager::saveSettings()
{ {
QStringList recentFiles; QVariantList recentFiles;
QStringList recentEditorIds; QStringList recentEditorIds;
foreach (const RecentFile &file, d->m_recentFiles) { foreach (const RecentFile &file, d->m_recentFiles) {
recentFiles.append(file.first); recentFiles.append(file.first.toVariant());
recentEditorIds.append(file.second.toString()); recentEditorIds.append(file.second.toString());
} }
@@ -1400,18 +1400,17 @@ void readSettings()
QSettings *s = ICore::settings(); QSettings *s = ICore::settings();
d->m_recentFiles.clear(); d->m_recentFiles.clear();
s->beginGroup(QLatin1String(settingsGroupC)); s->beginGroup(QLatin1String(settingsGroupC));
const QStringList recentFiles = s->value(QLatin1String(filesKeyC)).toStringList(); const QVariantList recentFiles = s->value(QLatin1String(filesKeyC)).toList();
const QStringList recentEditorIds = s->value(QLatin1String(editorsKeyC)).toStringList(); const QStringList recentEditorIds = s->value(QLatin1String(editorsKeyC)).toStringList();
s->endGroup(); s->endGroup();
// clean non-existing files // clean non-existing files
for (int i = 0, n = recentFiles.size(); i < n; ++i) { for (int i = 0, n = recentFiles.size(); i < n; ++i) {
const QString &fileName = recentFiles.at(i);
QString editorId; QString editorId;
if (i < recentEditorIds.size()) // guard against old or weird settings if (i < recentEditorIds.size()) // guard against old or weird settings
editorId = recentEditorIds.at(i); editorId = recentEditorIds.at(i);
if (QFileInfo(fileName).isFile()) const Utils::FilePath &filePath = FilePath::fromVariant(recentFiles.at(i));
d->m_recentFiles.append(DocumentManager::RecentFile(QDir::fromNativeSeparators(fileName), // from native to guard against old settings if (filePath.exists() && !filePath.isDir())
Id::fromString(editorId))); d->m_recentFiles.append({filePath, Id::fromString(editorId)});
} }
s->beginGroup(QLatin1String(directoryGroupC)); s->beginGroup(QLatin1String(directoryGroupC));

View File

@@ -53,7 +53,7 @@ public:
KeepLinks KeepLinks
}; };
using RecentFile = QPair<QString, Utils::Id>; using RecentFile = QPair<Utils::FilePath, Utils::Id>;
static DocumentManager *instance(); static DocumentManager *instance();
@@ -69,7 +69,7 @@ public:
static void unexpectFileChange(const QString &fileName); static void unexpectFileChange(const QString &fileName);
// recent files // recent files
static void addToRecentFiles(const QString &fileName, Utils::Id editorId = {}); static void addToRecentFiles(const Utils::FilePath &filePath, Utils::Id editorId = {});
Q_SLOT void clearRecentFiles(); Q_SLOT void clearRecentFiles();
static QList<RecentFile> recentFiles(); static QList<RecentFile> recentFiles();

View File

@@ -1445,8 +1445,7 @@ void EditorManagerPrivate::addEditor(IEditor *editor)
const bool addWatcher = !isTemporary; const bool addWatcher = !isTemporary;
DocumentManager::addDocument(document, addWatcher); DocumentManager::addDocument(document, addWatcher);
if (!isTemporary) if (!isTemporary)
DocumentManager::addToRecentFiles(document->filePath().toString(), DocumentManager::addToRecentFiles(document->filePath(), document->id());
document->id());
emit m_instance->documentOpened(document); emit m_instance->documentOpened(document);
} }
emit m_instance->editorOpened(editor); emit m_instance->editorOpened(editor);
@@ -1983,7 +1982,7 @@ void EditorManagerPrivate::addDocumentToRecentFiles(IDocument *document)
DocumentModel::Entry *entry = DocumentModel::entryForDocument(document); DocumentModel::Entry *entry = DocumentModel::entryForDocument(document);
if (!entry) if (!entry)
return; return;
DocumentManager::addToRecentFiles(document->filePath().toString(), entry->id()); DocumentManager::addToRecentFiles(document->filePath(), entry->id());
} }
void EditorManagerPrivate::updateAutoSave() void EditorManagerPrivate::updateAutoSave()

View File

@@ -1200,8 +1200,7 @@ void MainWindow::aboutToShowRecentFiles()
for (int i = 0; i < recentFiles.count(); ++i) { for (int i = 0; i < recentFiles.count(); ++i) {
const DocumentManager::RecentFile file = recentFiles[i]; const DocumentManager::RecentFile file = recentFiles[i];
const QString filePath const QString filePath = Utils::quoteAmpersands(file.first.shortNativePath());
= Utils::quoteAmpersands(QDir::toNativeSeparators(withTildeHomePath(file.first)));
const QString actionText = ActionManager::withNumberAccelerator(filePath, i + 1); const QString actionText = ActionManager::withNumberAccelerator(filePath, i + 1);
QAction *action = menu->addAction(actionText); QAction *action = menu->addAction(actionText);
connect(action, &QAction::triggered, this, [file] { connect(action, &QAction::triggered, this, [file] {