Skip to content

Commit 26a949d

Browse files
committed
QuickAccess: update VxUrl list when accessing or changing entries
Ensure that the path is always displayed correctly when the QuickAccess list is modified or read, to fix display inconsistencies caused by outdated VxUrl paths.
1 parent 924451b commit 26a949d

File tree

4 files changed

+89
-1
lines changed

4 files changed

+89
-1
lines changed

src/core/quickaccesshelper.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ void QuickAccessHelper::pinToQuickAccess(const QStringList &p_files)
1212
}
1313

1414
auto &sessionConfig = ConfigMgr::getInst().getSessionConfig();
15+
sessionConfig.tryCorrectQuickAccessFiles();
1516
auto qaFiles = sessionConfig.getQuickAccessFiles();
1617
qaFiles.append(p_files);
1718
qaFiles.removeDuplicates();

src/core/sessionconfig.cpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
#include <QJsonArray>
66
#include <QJsonDocument>
77

8+
#include <core/vnotex.h>
9+
#include <core/notebookmgr.h>
10+
811
#include <utils/fileutils.h>
12+
#include <utils/pathutils.h>
13+
#include <utils/vxurlutils.h>
914

1015
#include "configmgr.h"
1116
#include "mainconfig.h"
@@ -462,15 +467,93 @@ void SessionConfig::setQuickAccessFiles(const QStringList &p_files)
462467
}
463468
}
464469
updateConfig(m_quickAccessFiles, files, this);
470+
if(tryCorrectQuickAccessFiles())
471+
{
472+
update();
473+
}
465474
}
466475

467476
void SessionConfig::removeQuickAccessFile(const QString &p_file)
468477
{
469478
if (m_quickAccessFiles.removeOne(p_file)) {
479+
tryCorrectQuickAccessFiles();
470480
update();
471481
}
472482
}
473483

484+
bool SessionConfig::tryCorrectQuickAccessFiles(void)
485+
{
486+
auto notebook = VNoteX::getInst().getNotebookMgr().getCurrentNotebook();
487+
if (!notebook) {
488+
return false;
489+
}
490+
491+
QStringList oldResult = m_quickAccessFiles;
492+
QStringList newResult;
493+
for (const auto &file : m_quickAccessFiles) {
494+
auto fi = file.trimmed();
495+
if (fi.isEmpty()) {
496+
continue;
497+
}
498+
// check absolute path
499+
if (!file.startsWith("#")) {
500+
if (QFileInfo(file).exists()) {
501+
newResult << file;
502+
}
503+
continue;
504+
}
505+
506+
// update VxURL if file path is changed
507+
const QString rootPath = notebook->getRootFolderAbsolutePath();
508+
QString signature = VxUrlUtils::getSignatureFromVxURL(file);
509+
QString oldFilePath = VxUrlUtils::getFilePathFromVxURL(file);
510+
511+
// Start searching for the file from oldFilePath until reaching rootPath
512+
QString newFilePath;
513+
QString currentDir = PathUtils::parentDirPath(oldFilePath);
514+
while (true) {
515+
// make sure currentDir is under rootPath
516+
if (!currentDir.startsWith(rootPath)) {
517+
break;
518+
}
519+
520+
newFilePath = VxUrlUtils::getFilePathFromSignature(currentDir, signature);
521+
if (!newFilePath.isEmpty()) {
522+
break;
523+
}
524+
// invalid path
525+
if (currentDir.isEmpty() || QDir(currentDir).isRoot()) {
526+
break;
527+
}
528+
529+
currentDir = PathUtils::parentDirPath(currentDir);
530+
}
531+
532+
// file deleted
533+
if (newFilePath.isEmpty()) {
534+
continue;
535+
}
536+
// file path not changed
537+
if (oldFilePath == newFilePath) {
538+
newResult << file;
539+
continue;
540+
}
541+
// file path changed, but not exists
542+
if (!QFileInfo(newFilePath).exists()) {
543+
continue;
544+
}
545+
QString newVxURL = VxUrlUtils::generateVxURL(signature, newFilePath);
546+
newResult << newVxURL;
547+
}
548+
newResult.removeDuplicates();
549+
m_quickAccessFiles = newResult;
550+
551+
if (oldResult != newResult) {
552+
return true;
553+
}
554+
return false;
555+
}
556+
474557
void SessionConfig::loadExternalPrograms(const QJsonObject &p_session)
475558
{
476559
const auto arr = p_session.value(QStringLiteral("external_programs")).toArray();

src/core/sessionconfig.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ namespace vnotex
159159
void setQuickAccessFiles(const QStringList &p_files);
160160

161161
void removeQuickAccessFile(const QString &p_file);
162+
bool tryCorrectQuickAccessFiles(void);
162163

163164
const QVector<ExternalProgram> &getExternalPrograms() const;
164165
const ExternalProgram *findExternalProgram(const QString &p_name) const;

src/widgets/toolbarhelper.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,10 @@ void ToolBarHelper::addSpacer(QToolBar *p_toolBar)
486486
void ToolBarHelper::updateQuickAccessMenu(QMenu *p_menu)
487487
{
488488
p_menu->clear();
489-
const auto &quickAccess = ConfigMgr::getInst().getSessionConfig().getQuickAccessFiles();
489+
auto &sessionConfig = ConfigMgr::getInst().getSessionConfig();
490+
sessionConfig.tryCorrectQuickAccessFiles();
491+
492+
const auto &quickAccess = sessionConfig.getQuickAccessFiles();
490493
if (quickAccess.isEmpty()) {
491494
auto act = p_menu->addAction(MainWindow::tr("Quick Access Not Set"));
492495
act->setEnabled(false);

0 commit comments

Comments
 (0)