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.
\a editorId defaults to the empty ID, which lets \QC figure out
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;
const QString fileKey = filePathKey(fileName, KeepLinks);
const QString fileKey = filePathKey(filePath.toString(), KeepLinks);
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())
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()
{
QStringList recentFiles;
QVariantList recentFiles;
QStringList recentEditorIds;
foreach (const RecentFile &file, d->m_recentFiles) {
recentFiles.append(file.first);
recentFiles.append(file.first.toVariant());
recentEditorIds.append(file.second.toString());
}
@@ -1400,18 +1400,17 @@ void readSettings()
QSettings *s = ICore::settings();
d->m_recentFiles.clear();
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();
s->endGroup();
// clean non-existing files
for (int i = 0, n = recentFiles.size(); i < n; ++i) {
const QString &fileName = recentFiles.at(i);
QString editorId;
if (i < recentEditorIds.size()) // guard against old or weird settings
editorId = recentEditorIds.at(i);
if (QFileInfo(fileName).isFile())
d->m_recentFiles.append(DocumentManager::RecentFile(QDir::fromNativeSeparators(fileName), // from native to guard against old settings
Id::fromString(editorId)));
const Utils::FilePath &filePath = FilePath::fromVariant(recentFiles.at(i));
if (filePath.exists() && !filePath.isDir())
d->m_recentFiles.append({filePath, Id::fromString(editorId)});
}
s->beginGroup(QLatin1String(directoryGroupC));

View File

@@ -53,7 +53,7 @@ public:
KeepLinks
};
using RecentFile = QPair<QString, Utils::Id>;
using RecentFile = QPair<Utils::FilePath, Utils::Id>;
static DocumentManager *instance();
@@ -69,7 +69,7 @@ public:
static void unexpectFileChange(const QString &fileName);
// 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();
static QList<RecentFile> recentFiles();

View File

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

View File

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