|
5 | 5 | #include <QJsonArray>
|
6 | 6 | #include <QJsonDocument>
|
7 | 7 |
|
| 8 | +#include <core/vnotex.h> |
| 9 | +#include <core/notebookmgr.h> |
| 10 | + |
8 | 11 | #include <utils/fileutils.h>
|
| 12 | +#include <utils/pathutils.h> |
| 13 | +#include <utils/vxurlutils.h> |
9 | 14 |
|
10 | 15 | #include "configmgr.h"
|
11 | 16 | #include "mainconfig.h"
|
@@ -462,15 +467,93 @@ void SessionConfig::setQuickAccessFiles(const QStringList &p_files)
|
462 | 467 | }
|
463 | 468 | }
|
464 | 469 | updateConfig(m_quickAccessFiles, files, this);
|
| 470 | + if(tryCorrectQuickAccessFiles()) |
| 471 | + { |
| 472 | + update(); |
| 473 | + } |
465 | 474 | }
|
466 | 475 |
|
467 | 476 | void SessionConfig::removeQuickAccessFile(const QString &p_file)
|
468 | 477 | {
|
469 | 478 | if (m_quickAccessFiles.removeOne(p_file)) {
|
| 479 | + tryCorrectQuickAccessFiles(); |
470 | 480 | update();
|
471 | 481 | }
|
472 | 482 | }
|
473 | 483 |
|
| 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 | + |
474 | 557 | void SessionConfig::loadExternalPrograms(const QJsonObject &p_session)
|
475 | 558 | {
|
476 | 559 | const auto arr = p_session.value(QStringLiteral("external_programs")).toArray();
|
|
0 commit comments