1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "filewatcher.h"
#include "common/contenttype.h"
#include "item/serialize.h"
#include <QAbstractItemModel>
#include <QCryptographicHash>
#include <QDataStream>
#include <QDateTime>
#include <QDebug>
#include <QDir>
#include <QElapsedTimer>
#include <QLoggingCategory>
#include <QMimeData>
#include <QRegularExpression>
#include <QUrl>
#include <array>
#include <vector>
constexpr int staleLockTimeMs = 10'000;
const QLatin1String mimeExtensionMap(COPYQ_MIME_PREFIX_ITEMSYNC "mime-to-extension-map");
const QLatin1String mimeBaseName(COPYQ_MIME_PREFIX_ITEMSYNC "basename");
const QLatin1String mimeNoSave(COPYQ_MIME_PREFIX_ITEMSYNC "no-save");
const QLatin1String mimeSyncPath(COPYQ_MIME_PREFIX_ITEMSYNC "sync-path");
const QLatin1String mimeNoFormat(COPYQ_MIME_PREFIX_ITEMSYNC "no-format");
const QLatin1String mimeUnknownFormats(COPYQ_MIME_PREFIX_ITEMSYNC "unknown-formats");
const QLatin1String mimePrivateSyncPrefix(COPYQ_MIME_PREFIX_ITEMSYNC_PRIVATE);
const QLatin1String mimeOldBaseName(COPYQ_MIME_PREFIX_ITEMSYNC_PRIVATE "old-basename");
const QLatin1String mimeHashPrefix(COPYQ_MIME_PREFIX_ITEMSYNC_PRIVATE "hash");
namespace {
Q_DECLARE_LOGGING_CATEGORY(fileWatcher)
Q_LOGGING_CATEGORY(fileWatcher, "copyq.plugin.itemsync.filewatcher")
} // namespace
class SyncDataFile {
public:
SyncDataFile() = default;
explicit SyncDataFile(const QString &path, const QString &format = QString())
: m_path(path)
, m_format(format)
{}
const QString &path() const { return m_path; }
void setPath(const QString &path) { m_path = path; }
const QString &format() const { return m_format; }
void setFormat(const QString &format) { m_format = format; }
qint64 size() const {
QFileInfo f(m_path);
return f.size();
}
QString toString() const {
if ( m_format.isEmpty() )
return m_path;
return QStringLiteral("%1\n%2").arg(m_path, m_format);
}
QByteArray readAll() const
{
QFile f(m_path);
if ( !f.open(QIODevice::ReadOnly) )
return QByteArray();
if ( m_format.isEmpty() )
return f.readAll();
QDataStream stream(&f);
QVariantMap dataMap;
if ( !deserializeData(&stream, &dataMap) ) {
qCCritical(fileWatcher)
<< "Failed to read file" << m_path << f.errorString();
return QByteArray();
}
return dataMap.value(m_format).toByteArray();
}
bool operator==(const SyncDataFile &other) const {
return m_path == other.m_path
&& m_format == other.m_format;
}
private:
QString m_path;
QString m_format;
};
Q_DECLARE_METATYPE(SyncDataFile)
QDataStream &operator<<(QDataStream &out, SyncDataFile value)
{
return out << value.path() << value.format();
}
QDataStream &operator>>(QDataStream &in, SyncDataFile &value)
{
QString path;
QString format;
in >> path >> format;
value.setPath(path);
value.setFormat(format);
return in;
}
void registerSyncDataFileConverter()
{
QMetaType::registerConverter(&SyncDataFile::readAll);
QMetaType::registerConverter(&SyncDataFile::toString);
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
qRegisterMetaTypeStreamOperators<SyncDataFile>("SyncDataFile");
#else
qRegisterMetaType<SyncDataFile>("SyncDataFile");
#endif
}
struct Ext {
Ext() : extension(), format() {}
Ext(const QString &extension, const QString &format)
: extension(extension)
, format(format)
{}
QString extension;
QString format;
};
using Exts = std::vector<Ext>;
struct BaseNameExtensions {
explicit BaseNameExtensions(const QString &baseName = QString(),
const Exts &exts = Exts())
: baseName(baseName)
, exts(exts)
{}
QString baseName;
Exts exts;
};
namespace {
const QLatin1String dataFileSuffix("_copyq.dat");
const QLatin1String noteFileSuffix("_note.txt");
const int defaultUpdateFocusItemsIntervalMs = 10000;
const int batchItemUpdateIntervalMs = 100;
const qint64 sizeLimit = 50'000'000;
FileFormat getFormatSettingsFromFileName(const QString &fileName,
const QList<FileFormat> &formatSettings,
QString *foundExt = nullptr)
{
for (const auto &format : formatSettings) {
for ( const auto &ext : format.extensions ) {
if ( fileName.endsWith(ext) ) {
if (foundExt)
*foundExt = ext;
return format;
}
}
}
return FileFormat();
}
void getBaseNameAndExtension(const QString &fileName, QString *baseName, QString *ext,
const QList<FileFormat> &formatSettings)
{
ext->clear();
const FileFormat fileFormat = getFormatSettingsFromFileName(fileName, formatSettings, ext);
if ( !fileFormat.isValid() ) {
const int i = fileName.lastIndexOf('.');
if (i != -1)
*ext = fileName.mid(i);
}
*baseName = fileName.left( fileName.size() - ext->size() );
if ( baseName->endsWith('.') ) {
baseName->chop(1);
ext->prepend('.');
}
}
using ExtensionsAndFormats = std::array<Ext, 14>;
const ExtensionsAndFormats &fileExtensionsAndFormats()
{
static const ExtensionsAndFormats exts = {
Ext(noteFileSuffix, mimeItemNotes),
Ext(".txt", mimeText),
Ext(".html", mimeHtml),
Ext(".uri", mimeUriList),
Ext(".png", "image/png"),
Ext("_inkscape.svg", "image/x-inkscape-svg-compressed"),
Ext(".svg", "image/svg+xml"),
Ext(".bmp", "image/bmp"),
Ext(".gif", "image/gif"),
Ext(".jpg", "image/jpeg"),
Ext(".ico", "image/x-icon"),
Ext(".webp", "image/webp"),
Ext(".xml", "application/xml"),
Ext(".xml", "text/xml"),
};
return exts;
}
QString findByFormat(const QString &format, const QList<FileFormat> &formatSettings)
{
// Find in default extensions.
for (const auto &ext : fileExtensionsAndFormats()) {
if (ext.format == format)
return ext.extension;
}
// Find in user defined extensions.
for (const auto &fileFormat : formatSettings) {
if ( !fileFormat.extensions.isEmpty() && fileFormat.itemMime != "-"
&& format == fileFormat.itemMime )
{
return fileFormat.extensions.first();
}
}
return QString();
}
Ext findByExtension(const QString &fileName, const QList<FileFormat> &formatSettings)
{
// Is internal data format?
if ( fileName.endsWith(dataFileSuffix) )
return Ext(dataFileSuffix, mimeUnknownFormats);
// Avoid conflicting notes with text.
if ( fileName.endsWith(noteFileSuffix) )
return Ext(noteFileSuffix, mimeItemNotes);
// Find in user defined formats.
bool hasUserFormat = false;
for (const auto &format : formatSettings) {
for (const auto &ext : format.extensions) {
if ( fileName.endsWith(ext) ) {
if ( format.itemMime.isEmpty() )
hasUserFormat = true;
else
return Ext(ext, format.itemMime);
}
}
}
// Find in default formats.
for (const auto &ext : fileExtensionsAndFormats()) {
if ( fileName.endsWith(ext.extension) )
return ext;
}
return hasUserFormat ? Ext(QString(), mimeNoFormat) : Ext();
}
bool saveItemFile(const QString &filePath, const QByteArray &bytes,
QStringList *existingFiles, bool hashChanged = true)
{
if ( !existingFiles->removeOne(filePath) || hashChanged ) {
QFile f(filePath);
if ( !f.open(QIODevice::WriteOnly) || f.write(bytes) == -1 ) {
qCCritical(fileWatcher)
<< "Failed to save item file:" << f.errorString();
return false;
}
}
return true;
}
bool canUseFile(const QFileInfo &info)
{
return !info.fileName().startsWith(QLatin1Char('.'));
}
bool getBaseNameExtension(const QString &filePath, const QList<FileFormat> &formatSettings,
QString *baseName, Ext *ext, const QDir &parentDir)
{
QFileInfo info(filePath);
if ( !canUseFile(info) )
return false;
*ext = findByExtension(filePath, formatSettings);
if ( ext->format.isEmpty() || ext->format == QLatin1String("-") )
return false;
const QString fileName = parentDir.relativeFilePath(info.absoluteFilePath());
*baseName = fileName.left( fileName.size() - ext->extension.size() );
return true;
}
BaseNameExtensionsList listFiles(
const QStringList &files,
const QList<FileFormat> &formatSettings,
const int maxItemCount,
const QDir &parentDir)
{
BaseNameExtensionsList fileList;
QMap<QString, int> fileMap;
for (const auto &filePath : files) {
QString baseName;
Ext ext;
if ( getBaseNameExtension(filePath, formatSettings, &baseName, &ext, parentDir) ) {
int i = fileMap.value(baseName, -1);
if (i == -1) {
i = fileList.size();
if (i >= maxItemCount)
break;
fileList.append( BaseNameExtensions(baseName, {ext}) );
fileMap.insert(baseName, i);
} else {
fileList[i].exts.push_back(ext);
}
}
}
return fileList;
}
/// Sorts own files (copyq_*) first by creation date newer first and other
/// files by name alphabetically. More nested files are later.
bool isBaseNameLessThan(const QString &lhsBaseName, const QString &rhsBaseName)
{
const int lhsNesting = lhsBaseName.count('/');
const int rhsNesting = rhsBaseName.count('/');
if (lhsNesting != rhsNesting)
return lhsNesting < rhsNesting;
const bool isLhsOwn = FileWatcher::isOwnBaseName(lhsBaseName);
const bool isRhsOwn = FileWatcher::isOwnBaseName(rhsBaseName);
if (isLhsOwn && isRhsOwn)
return lhsBaseName > rhsBaseName;
if (isLhsOwn)
return true;
if (isRhsOwn)
return false;
return lhsBaseName < rhsBaseName;
}
QStringList listFiles(const QDir &dir)
{
QList<QDir> dirs{dir};
QSet<QString> visitedDirs;
QStringList result;
while (!dirs.isEmpty()) {
const QDir subDir = dirs.takeFirst();
const QFileInfoList infos = subDir.entryInfoList(
QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot
| QDir::Readable | QDir::Writable);
for (const QFileInfo &info : infos) {
if ( !canUseFile(info) )
continue;
if ( info.isDir() ) {
// Avoid visiting same directory, in case of symlinks.
const QString canonicalPath = info.canonicalFilePath();
if ( visitedDirs.contains(canonicalPath) )
continue;
visitedDirs.insert(canonicalPath);
dirs.append( QDir(info.absoluteFilePath()) );
} else {
result.append(info.absoluteFilePath());
}
}
}
std::sort(std::begin(result), std::end(result), [&](const QString &lhs, const QString &rhs){
const QString lhsBaseName = dir.relativeFilePath(lhs);
const QString rhsBaseName = dir.relativeFilePath(rhs);
return isBaseNameLessThan(lhsBaseName, rhsBaseName);
});
return result;
}
/// Return true only if no file name in @a fileNames starts with @a baseName.
bool isUniqueBaseName(const QString &baseName, const QDir &dir,
const QSet<QString> &baseNames = {})
{
if ( baseNames.contains(baseName) )
return false;
const QFileInfo info(dir.absoluteFilePath(baseName));
const QString pattern = info.fileName() + QLatin1Char('*');
QDir parentDir = info.dir();
parentDir.setNameFilters({pattern});
return parentDir.isEmpty();
}
void moveFormatFiles(const QString &oldPath, const QString &newPath,
const QVariantMap &mimeToExtension)
{
for (const auto &extValue : mimeToExtension) {
const QString ext = extValue.toString();
QFile::rename(oldPath + ext, newPath + ext);
}
}
void copyFormatFiles(const QString &oldPath, const QString &newPath,
const QVariantMap &mimeToExtension)
{
for (const auto &extValue : mimeToExtension) {
const QString ext = extValue.toString();
QFile::copy(oldPath + ext, newPath + ext);
}
}
void removeFormatFiles(const QString &path, const QVariantMap &mimeToExtension)
{
for (const auto &extValue : mimeToExtension)
QFile::remove(path + extValue.toString());
}
bool renameToUnique(
const QDir &dir, const QSet<QString> &baseNames, QString *name,
const QList<FileFormat> &formatSettings)
{
if ( name->isEmpty() ) {
const auto dateFormat = QStringLiteral("yyyyMMddhhmmsszzz");
const auto dateTime = QDateTime::currentDateTimeUtc();
const auto now = dateTime.toString(dateFormat);
*name = QStringLiteral("copyq_%1").arg(now);
} else {
// Replace/remove unsafe characters.
name->replace( QRegularExpression(QLatin1String(R"(/|\\|^\.)")),
QLatin1String("_") );
name->remove( QRegularExpression(QLatin1String(R"(\n|\r)")) );
}
if ( isUniqueBaseName(*name, dir, baseNames) )
return true;
QString ext;
QString baseName;
getBaseNameAndExtension(*name, &baseName, &ext, formatSettings);
int i = 0;
int fieldWidth = 4;
QRegularExpression re(QStringLiteral(R"(\d{1,4}$)"));
const auto m = re.match(baseName);
if (m.hasMatch()) {
const QString num = m.captured();
i = num.toInt();
fieldWidth = num.size();
baseName = baseName.mid( 0, baseName.size() - fieldWidth );
} else {
baseName.append('-');
}
for (int counter = 0; counter < 99999; ++counter) {
*name = baseName + QStringLiteral("%1").arg(++i, fieldWidth, 10, QLatin1Char('0')) + ext;
if ( isUniqueBaseName(*name, dir, baseNames) )
return true;
}
qCCritical(fileWatcher)
<< "Failed to find unique base name with prefix:" << baseName;
return false;
}
QString findLastOwnBaseName(QAbstractItemModel *model, int fromRow) {
for (int row = fromRow; row < model->rowCount(); ++row) {
const QModelIndex index = model->index(row, 0);
const QString baseName = FileWatcher::getBaseName(index);
if ( FileWatcher::isOwnBaseName(baseName) )
return baseName;
}
return {};
}
} // namespace
QString FileWatcher::getBaseName(const QModelIndex &index)
{
return getBaseName( index.data(contentType::data).toMap() );
}
QString FileWatcher::getBaseName(const QVariantMap &data)
{
return data.value(mimeBaseName).toString();
}
bool FileWatcher::isOwnBaseName(const QString &baseName)
{
return baseName.startsWith(QLatin1String("copyq_"));
}
void FileWatcher::removeFilesForRemovedIndex(const QString &tabPath, const QModelIndex &index)
{
const QAbstractItemModel *model = index.model();
if (!model)
return;
const QString baseName = FileWatcher::getBaseName(index);
if ( baseName.isEmpty() )
return;
// Check if item is still present in list (drag'n'drop).
bool remove = true;
for (int i = 0; i < model->rowCount(); ++i) {
const QModelIndex index2 = model->index(i, 0);
if ( index2 != index && baseName == FileWatcher::getBaseName(index2) ) {
remove = false;
break;
}
}
if (!remove)
return;
const QVariantMap itemData = index.data(contentType::data).toMap();
const QVariantMap mimeToExtension = itemData.value(mimeExtensionMap).toMap();
if ( mimeToExtension.isEmpty() )
QFile::remove(tabPath + '/' + baseName);
else
removeFormatFiles(tabPath + '/' + baseName, mimeToExtension);
}
Hash FileWatcher::calculateHash(const QByteArray &bytes)
{
return QCryptographicHash::hash(bytes, QCryptographicHash::Sha1);
}
FileWatcher::FileWatcher(
const QString &path,
const QStringList &paths,
QAbstractItemModel *model,
int maxItems,
const QList<FileFormat> &formatSettings,
int itemDataThreshold,
QObject *parent)
: QObject(parent)
, m_model(model)
, m_formatSettings(formatSettings)
, m_dir(path)
, m_valid(true)
, m_maxItems(maxItems)
, m_itemDataThreshold(itemDataThreshold)
, m_lock(path + QLatin1String("/.copyq_lock"))
{
m_updateTimer.setSingleShot(true);
m_moveTimer.setSingleShot(true);
m_lock.setStaleLockTime(staleLockTimeMs);
bool ok;
const int interval = qEnvironmentVariableIntValue("COPYQ_SYNC_UPDATE_INTERVAL_MS", &ok);
m_interval = ok && interval > 0 ? interval : defaultUpdateFocusItemsIntervalMs;
connect( &m_updateTimer, &QTimer::timeout,
this, &FileWatcher::updateItems );
connect( &m_moveTimer, &QTimer::timeout,
this, &FileWatcher::updateMovedRows );
connect( m_model, &QAbstractItemModel::rowsInserted,
this, &FileWatcher::onRowsInserted );
connect( m_model, &QAbstractItemModel::rowsAboutToBeRemoved,
this, &FileWatcher::onRowsRemoved );
connect( model, &QAbstractItemModel::rowsMoved,
this, &FileWatcher::onRowsMoved );
connect( m_model, &QAbstractItemModel::dataChanged,
this, &FileWatcher::onDataChanged );
if (model->rowCount() > 0)
saveItems(0, model->rowCount() - 1, UpdateType::Inserted);
prependItemsFromFiles( QDir(path), listFiles(paths, m_formatSettings, m_maxItems, m_dir) );
}
bool FileWatcher::lock()
{
if ( !m_valid )
return false;
m_dir.refresh();
// Create path if doesn't exist.
if ( !m_dir.mkpath(QStringLiteral(".")) ) {
qCCritical(fileWatcher)
<< tr("Failed to create synchronization directory \"%1\"!").arg(path());
return false;
}
if ( !m_lock.lock() ) {
qCCritical(fileWatcher) << "Failed to create lock file in"
<< path() << "error code:" << m_lock.error();
return false;
}
m_valid = false;
return true;
}
void FileWatcher::unlock()
{
m_lock.unlock();
m_valid = true;
}
QVariantMap FileWatcher::itemDataFromFiles(const QDir &dir, const BaseNameExtensions &baseNameWithExts)
{
QVariantMap dataMap;
QVariantMap mimeToExtension;
updateDataAndWatchFile(dir, baseNameWithExts, &dataMap, &mimeToExtension);
if ( !mimeToExtension.isEmpty() ) {
const QString baseName = dir.relativeFilePath(baseNameWithExts.baseName);
dataMap.insert(mimeBaseName, baseName);
dataMap.insert(mimeOldBaseName, baseName);
dataMap.insert(mimeExtensionMap, mimeToExtension);
}
return dataMap;
}
void FileWatcher::prependItemsFromFiles(const QDir &dir, const BaseNameExtensionsList &fileList)
{
QVector<QVariantMap> items;
items.reserve(fileList.size());
for (auto it = fileList.rbegin(); it != fileList.rend(); ++it) {
const QVariantMap item = itemDataFromFiles(dir, *it);
if ( !item.isEmpty() )
items.append(item);
}
createItems(items, 0);
}
void FileWatcher::insertItemsFromFiles(const QDir &dir, const BaseNameExtensionsList &fileList)
{
if ( fileList.isEmpty() )
return;
QVector<QVariantMap> items;
items.reserve(fileList.size());
for (const auto &baseNameWithExts : fileList) {
const QVariantMap item = itemDataFromFiles(dir, baseNameWithExts);
// Skip if the associated file was just removed.
if ( !item.isEmpty() )
items.append(item);
}
int row = 0;
int i = 0;
for (; i < items.size(); ++i) {
const QVariantMap &item = items[i];
const QString &newBaseName = getBaseName(item);
for ( ; row < m_model->rowCount(); ++row ) {
const QString rowBaseName = getBaseName( m_model->index(row, 0) );
if ( isBaseNameLessThan(newBaseName, rowBaseName) )
break;
}
if ( row >= m_model->rowCount() )
break;
createItems({item}, row);
++row;
}
// Append rest of the items.
if ( i < items.size() ) {
const int space = m_maxItems - m_model->rowCount();
if (space <= 0)
return;
items.erase(items.begin(), items.begin() + i);
if ( space < items.size() )
items.erase(items.begin(), items.begin() + space);
createItems( items, m_model->rowCount() );
}
// TODO: Remove excessive items, but not pinned.
// This must be done elsewhere using QTimer 0.
}
void FileWatcher::updateItems()
{
if ( !lock() ) {
m_updateTimer.start(m_interval);
return;
}
QElapsedTimer t;
t.start();
m_lastUpdateTimeMs = QDateTime::currentMSecsSinceEpoch();
if ( m_batchIndexData.isEmpty() ) {
const QStringList files = listFiles(m_dir);
m_fileList = listFiles(files, m_formatSettings, m_maxItems, m_dir);
m_batchIndexData.reserve(m_model->rowCount());
for (int row = 0; row < m_model->rowCount(); ++row) {
const QModelIndex index = m_model->index(row, 0);
if ( !oldBaseName(index).isEmpty() )
m_batchIndexData.append(index);
}
m_lastBatchIndex = -1;
if ( t.elapsed() > 100 )
qCInfo(fileWatcher) << "Files listed in" << t.elapsed() << "ms";
}
for ( int i = m_lastBatchIndex + 1; i < m_batchIndexData.size(); ++i ) {
const QPersistentModelIndex &index = m_batchIndexData[i];
if ( !index.isValid() )
continue;
const QString baseName = oldBaseName(index);
if ( baseName.isEmpty() )
continue;
const auto it = std::find_if(
std::begin(m_fileList), std::end(m_fileList),
[&](const BaseNameExtensions &baseNameExtensions) {
return baseNameExtensions.baseName == baseName;
});
QVariantMap dataMap;
QVariantMap mimeToExtension;
if ( it != m_fileList.cend() ) {
updateDataAndWatchFile(m_dir, *it, &dataMap, &mimeToExtension);
m_fileList.erase(it);
}
if ( mimeToExtension.isEmpty() ) {
m_model->removeRow(index.row());
} else {
dataMap.insert(mimeBaseName, baseName);
dataMap.insert(mimeExtensionMap, mimeToExtension);
updateIndexData(index, &dataMap);
}
if ( t.elapsed() > 20 ) {
m_lastBatchIndex = i;
unlock();
m_updateTimer.start(batchItemUpdateIntervalMs);
return;
}
}
t.restart();
insertItemsFromFiles(m_dir, m_fileList);
if ( t.elapsed() > 100 )
qCInfo(fileWatcher) << "Items created in" << t.elapsed() << "ms";
m_fileList.clear();
m_batchIndexData.clear();
unlock();
if (m_updatesEnabled)
m_updateTimer.start(m_interval);
}
void FileWatcher::updateItemsIfNeeded()
{
const auto time = QDateTime::currentMSecsSinceEpoch();
if (time < m_lastUpdateTimeMs + m_interval)
return;
updateItems();
}
void FileWatcher::setUpdatesEnabled(bool enabled)
{
m_updatesEnabled = enabled;
if (enabled)
updateItems();
else if ( m_batchIndexData.isEmpty() )
m_updateTimer.stop();
}
void FileWatcher::onRowsInserted(const QModelIndex &, int first, int last)
{
if (first < m_moveEnd)
m_moveEnd += last - first + 1;
saveItems(first, last, UpdateType::Inserted);
}
void FileWatcher::onDataChanged(const QModelIndex &a, const QModelIndex &b)
{
saveItems(a.row(), b.row(), UpdateType::Changed);
}
void FileWatcher::onRowsRemoved(const QModelIndex &, int first, int last)
{
if (first < m_moveEnd)
m_moveEnd -= std::min(m_moveEnd, last) - first + 1;
const bool wasFull = m_maxItems >= m_model->rowCount();
for ( const auto &index : indexList(first, last) ) {
if ( !index.isValid() )
continue;
const QString baseName = oldBaseName(index);
if ( isOwnBaseName(baseName) )
removeFilesForRemovedIndex(path(), index);
}
// If the tab is no longer full, try to add new files.
if (wasFull)
m_updateTimer.start(0);
}
void FileWatcher::onRowsMoved(const QModelIndex &, int start, int end, const QModelIndex &, int destinationRow)
{
// Update copyq_* base names for moved rows and all rows above so that file
// are ordered the same way as the rows.
//
// The update is postponed and batched since it may need to go through a
// lot of items.
const int count = end - start + 1;
if (destinationRow < start) {
m_moveEnd = std::max(m_moveEnd, destinationRow + count - 1);
} else if (destinationRow > end) {
m_moveEnd = std::max(m_moveEnd, destinationRow - 1);
} else {
m_moveEnd = std::max(m_moveEnd, end);
}
m_moveTimer.start(0);
}
void FileWatcher::updateMovedRows()
{
if ( !lock() ) {
m_moveTimer.start();
return;
}
QString baseName = findLastOwnBaseName(m_model, m_moveEnd + 1);
QSet<QString> baseNames;
const int batchStart = std::max(0, m_moveEnd - 100);
const auto indexList = this->indexList(batchStart, m_moveEnd);
for (const auto &index : indexList) {
const QString currentBaseName = FileWatcher::getBaseName(index);
// Skip renaming non-owned items
if ( !currentBaseName.isEmpty() && !isOwnBaseName(currentBaseName) )
continue;
// Skip already sorted
if ( isBaseNameLessThan(currentBaseName, baseName) ) {
baseName = currentBaseName;
continue;
}
if ( !renameToUnique(m_dir, baseNames, &baseName, m_formatSettings) )
return;
baseNames.insert(baseName);
const QVariantMap data = {{mimeBaseName, baseName}};
m_model->setData(index, data, contentType::updateData);
}
if ( !renameMoveCopy(m_dir, indexList, UpdateType::Changed) )
return;
unlock();
m_moveEnd = batchStart - 1;
if (0 <= m_moveEnd)
m_moveTimer.start(batchItemUpdateIntervalMs);
else
m_moveEnd = -1;
}
QString FileWatcher::oldBaseName(const QModelIndex &index) const
{
return index.data(contentType::data).toMap().value(mimeOldBaseName).toString();
}
void FileWatcher::createItems(const QVector<QVariantMap> &items, int targetRow)
{
if ( items.isEmpty() )
return;
const int row = qMax( 0, qMin(targetRow, m_model->rowCount()) );
if ( !m_model->insertRows(row, items.size()) )
return;
// Find index if it was moved after inserted.
const int rows = m_model->rowCount();
auto it = std::begin(items);
for ( int i = 0; i < rows; ++i ) {
const int row2 = (row + i) % rows;
auto index = m_model->index(row2, 0);
if ( getBaseName(index).isEmpty() ) {
QVariantMap data = *it;
updateIndexData(index, &data);
++it;
if (it == std::end(items))
break;
}
}
}
void FileWatcher::updateIndexData(const QModelIndex &index, QVariantMap *itemData)
{
const QString baseName = getBaseName(*itemData);
if ( baseName.isEmpty() ) {
m_model->setData(index, *itemData, contentType::data);
return;
}
itemData->insert(mimeOldBaseName, baseName);
const QVariantMap mimeToExtension = itemData->value(mimeExtensionMap).toMap();
for ( auto it = mimeToExtension.begin(); it != mimeToExtension.end(); ++it ) {
if ( !it.key().startsWith(COPYQ_MIME_PREFIX_ITEMSYNC) && !it.key().startsWith(COPYQ_MIME_PRIVATE_PREFIX) ) {
const QString ext = it.value().toString();
const Hash hash = calculateHash(itemData->value(it.key()).toByteArray());
const QString mime = mimeHashPrefix + ext;
itemData->insert(mime, hash);
}
}
m_model->setData(index, *itemData, contentType::data);
}
QList<QPersistentModelIndex> FileWatcher::indexList(int first, int last)
{
QList<QPersistentModelIndex> indexList;
indexList.reserve(last - first + 1);
// List items in reverse order so the most recent file is created for the
// top item.
for (int i = last; i >= first; --i)
indexList.append( m_model->index(i, 0) );
return indexList;
}
void FileWatcher::saveItems(int first, int last, UpdateType updateType)
{
if ( !lock() )
return;
if ( !m_batchIndexData.isEmpty() )
m_batchIndexData.clear();
const auto indexList = this->indexList(first, last);
if ( !renameMoveCopy(m_dir, indexList, updateType) )
return;
QStringList existingFiles = listFiles(m_dir);
for (const auto &index : indexList) {
if ( !index.isValid() )
continue;
const QString baseName = getBaseName(index);
const QString filePath = m_dir.absoluteFilePath(baseName);
QVariantMap itemData = index.data(contentType::data).toMap();
QVariantMap oldMimeToExtension = itemData.value(mimeExtensionMap).toMap();
QVariantMap mimeToExtension;
QVariantMap dataMapUnknown;
const QVariantMap noSaveData = itemData.value(mimeNoSave).toMap();
QMutableMapIterator<QString, QVariant> it(itemData);
while (it.hasNext()) {
const auto item = it.next();
const QString &format = item.key();
if ( format.startsWith(COPYQ_MIME_PREFIX_ITEMSYNC) || format.startsWith(COPYQ_MIME_PRIVATE_PREFIX) )
continue; // skip internal data
const QByteArray bytes = it.value().toByteArray();
const Hash hash = calculateHash(bytes);
if ( noSaveData.contains(format) && noSaveData[format].toByteArray() == hash ) {
it.remove();
continue;
}
bool hasFile = oldMimeToExtension.contains(format);
const QString ext = hasFile ? oldMimeToExtension[format].toString()
: findByFormat(format, m_formatSettings);
if ( !hasFile && ext.isEmpty() ) {
dataMapUnknown.insert(format, bytes);
} else {
mimeToExtension.insert(format, ext);
const Hash oldHash = index.data(contentType::data).toMap().value(mimeHashPrefix + ext).toByteArray();
if ( !saveItemFile(filePath + ext, bytes, &existingFiles, hash != oldHash) )
return;
}
}
for ( auto it = oldMimeToExtension.constBegin();
it != oldMimeToExtension.constEnd(); ++it )
{
if ( it.key().startsWith(mimeNoFormat) )
mimeToExtension.insert( it.key(), it.value() );
}
if ( mimeToExtension.isEmpty() || !dataMapUnknown.isEmpty() ) {
mimeToExtension.insert(mimeUnknownFormats, dataFileSuffix);
QByteArray data = serializeData(dataMapUnknown);
if ( !saveItemFile(filePath + dataFileSuffix, data, &existingFiles) )
return;
}
if ( !noSaveData.isEmpty() || mimeToExtension != oldMimeToExtension ) {
itemData.remove(mimeNoSave);
for ( auto format = mimeToExtension.keyBegin(); format != mimeToExtension.keyEnd(); ++format )
oldMimeToExtension.remove(*format);
itemData.insert(mimeExtensionMap, mimeToExtension);
updateIndexData(index, &itemData);
// Remove files of removed formats.
removeFormatFiles(filePath, oldMimeToExtension);
}
}
unlock();
}
bool FileWatcher::renameMoveCopy(
const QDir &dir, const QList<QPersistentModelIndex> &indexList, UpdateType updateType)
{
if ( indexList.isEmpty() )
return false;
QSet<QString> baseNames;
QString baseName = findLastOwnBaseName(m_model, indexList.first().row() + 1);
for (const auto &index : indexList) {
if ( !index.isValid() )
continue;
QString olderBaseName = oldBaseName(index);
const QString oldBaseName = getBaseName(index);
if (updateType == UpdateType::Changed && olderBaseName.isEmpty())
olderBaseName = oldBaseName;
if ( !oldBaseName.isEmpty() )
baseName = oldBaseName;
const bool newItem = updateType != UpdateType::Changed && olderBaseName.isEmpty();
bool itemRenamed = olderBaseName != baseName;
if ( newItem || itemRenamed ) {
if ( !renameToUnique(dir, baseNames, &baseName, m_formatSettings) )
return false;
itemRenamed = olderBaseName != baseName;
baseNames.insert(baseName);
}
QVariantMap itemData = index.data(contentType::data).toMap();
const QString syncPath = itemData.value(mimeSyncPath).toString();
const bool copyFilesFromOtherTab = !syncPath.isEmpty() && syncPath != path();
if (copyFilesFromOtherTab || itemRenamed) {
const QVariantMap mimeToExtension = itemData.value(mimeExtensionMap).toMap();
const QString newBasePath = m_dir.absoluteFilePath(baseName);
if ( !syncPath.isEmpty() ) {
copyFormatFiles(syncPath + '/' + oldBaseName, newBasePath, mimeToExtension);
} else if ( !olderBaseName.isEmpty() ) {
moveFormatFiles(m_dir.absoluteFilePath(olderBaseName), newBasePath, mimeToExtension);
}
itemData.remove(mimeSyncPath);
itemData.insert(mimeBaseName, baseName);
updateIndexData(index, &itemData);
if ( oldBaseName.isEmpty() && itemData.contains(mimeUriList) ) {
if ( copyFilesFromUriList(itemData[mimeUriList].toByteArray(), index.row(), baseNames) )
m_model->removeRow(index.row());
}
}
}
return true;
}
void FileWatcher::updateDataAndWatchFile(const QDir &dir, const BaseNameExtensions &baseNameWithExts,
QVariantMap *dataMap, QVariantMap *mimeToExtension)
{
const QString basePath = dir.absoluteFilePath(baseNameWithExts.baseName);
for (const auto &ext : baseNameWithExts.exts) {
if ( ext.format.isEmpty() )
continue;
const QString fileName = basePath + ext.extension;
const QString path = dir.absoluteFilePath(fileName);
QFile f(path);
if ( !f.open(QIODevice::ReadOnly) )
continue;
if ( ext.extension == dataFileSuffix ) {
QDataStream stream(&f);
QVariantMap dataMap2;
if ( deserializeData(&stream, &dataMap2) ) {
for (auto it = dataMap2.constBegin(); it != dataMap2.constEnd(); ++it) {
const QVariant &value = it.value();
const qint64 size = value.type() == QVariant::ByteArray
? value.toByteArray().size()
: value.value<SyncDataFile>().size();
if (m_itemDataThreshold >= 0 && size > m_itemDataThreshold) {
const QVariant syncDataFile = QVariant::fromValue(SyncDataFile(path, it.key()));
dataMap->insert(it.key(), syncDataFile);
} else {
dataMap->insert(it.key(), value);
}
}
mimeToExtension->insert(mimeUnknownFormats, dataFileSuffix);
}
} else if ( f.size() > sizeLimit || ext.format.startsWith(mimeNoFormat)
|| dataMap->contains(ext.format) )
{
mimeToExtension->insert(mimeNoFormat + ext.extension, ext.extension);
} else if ( m_itemDataThreshold >= 0 && f.size() > m_itemDataThreshold ) {
const QVariant value = QVariant::fromValue(SyncDataFile(path));
dataMap->insert(ext.format, value);
mimeToExtension->insert(ext.format, ext.extension);
} else {
dataMap->insert(ext.format, f.readAll());
mimeToExtension->insert(ext.format, ext.extension);
}
}
}
bool FileWatcher::copyFilesFromUriList(const QByteArray &uriData, int targetRow, const QSet<QString> &baseNames)
{
QMimeData tmpData;
tmpData.setData(mimeUriList, uriData);
QVector<QVariantMap> items;
for ( const auto &url : tmpData.urls() ) {
if ( !url.isLocalFile() )
continue;
QFile f(url.toLocalFile());
if ( !f.exists() )
continue;
QString extName;
QString baseName;
getBaseNameAndExtension( QFileInfo(f).fileName(), &baseName, &extName,
m_formatSettings );
if ( !renameToUnique(m_dir, baseNames, &baseName, m_formatSettings) )
continue;
const QString targetFilePath = m_dir.absoluteFilePath(baseName + extName);
f.copy(targetFilePath);
Ext ext;
if ( getBaseNameExtension(targetFilePath, m_formatSettings, &baseName, &ext, m_dir) ) {
BaseNameExtensions baseNameExts(baseName, {ext});
const QVariantMap item = itemDataFromFiles(m_dir, baseNameExts);
items.append(item);
if (items.size() >= m_maxItems)
break;
}
}
createItems(items, targetRow);
return !items.isEmpty();
}
|