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
|
/*
SPDX-FileCopyrightText: 2001 Shie Erlich <krusader@users.sourceforge.net>
SPDX-FileCopyrightText: 2001 Rafi Yanai <krusader@users.sourceforge.net>
SPDX-FileCopyrightText: 2004-2022 Krusader Krew <https://krusader.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "krquery.h"
// QtCore
#include <QFile>
#include <QMetaMethod>
#include <QRegExp>
#include <QTextCodec>
#include <KFileItem>
#include <KFormat>
#include <KIO/Job>
#include <KIO/TransferJob>
#include <KLocalizedString>
#include <KUrlCompletion>
#include <utility>
#include "../Archive/krarchandler.h"
#include "fileitem.h"
#include "filesystem.h"
#include "krpermhandler.h"
#define STATUS_SEND_DELAY 250
#define MAX_LINE_LEN 1000
// set the defaults
KrQuery::KrQuery()
: matchesCaseSensitive(true)
, bNull(true)
, contain(QString())
, containCaseSensetive(true)
, containWholeWord(false)
, containRegExp(false)
, minSize(0)
, maxSize(0)
, newerThen(0)
, olderThen(0)
, owner(QString())
, group(QString())
, perm(QString())
, type(QString())
, inArchive(false)
, recurse(true)
, followLinksP(true)
, receivedBuffer(nullptr)
, receivedBufferLen(0)
, processEventsConnected(0)
, codec(QTextCodec::codecForLocale())
{
QChar ch = '\n';
QTextCodec::ConverterState state(QTextCodec::IgnoreHeader);
encodedEnterArray = codec->fromUnicode(&ch, 1, &state);
encodedEnter = encodedEnterArray.data();
encodedEnterLen = encodedEnterArray.size();
}
// set the defaults
KrQuery::KrQuery(const QString &name, bool matchCase)
: bNull(true)
, contain(QString())
, containCaseSensetive(true)
, containWholeWord(false)
, containRegExp(false)
, minSize(0)
, maxSize(0)
, newerThen(0)
, olderThen(0)
, owner(QString())
, group(QString())
, perm(QString())
, type(QString())
, inArchive(false)
, recurse(true)
, followLinksP(true)
, receivedBuffer(nullptr)
, receivedBufferLen(0)
, processEventsConnected(0)
, codec(QTextCodec::codecForLocale())
{
QChar ch = '\n';
QTextCodec::ConverterState state(QTextCodec::IgnoreHeader);
encodedEnterArray = codec->fromUnicode(&ch, 1, &state);
encodedEnter = encodedEnterArray.data();
encodedEnterLen = encodedEnterArray.size();
setNameFilter(name, matchCase);
}
KrQuery::KrQuery(const KrQuery &that)
: QObject()
, receivedBuffer(nullptr)
, receivedBufferLen(0)
, processEventsConnected(0)
{
*this = that;
}
KrQuery::~KrQuery()
{
if (receivedBuffer)
delete[] receivedBuffer;
receivedBuffer = nullptr;
}
KrQuery &KrQuery::operator=(const KrQuery &old)
{
matches = old.matches;
excludes = old.excludes;
includedDirs = old.includedDirs;
excludedDirs = old.excludedDirs;
matchesCaseSensitive = old.matchesCaseSensitive;
bNull = old.bNull;
contain = old.contain;
containCaseSensetive = old.containCaseSensetive;
containWholeWord = old.containWholeWord;
containRegExp = old.containRegExp;
minSize = old.minSize;
maxSize = old.maxSize;
newerThen = old.newerThen;
olderThen = old.olderThen;
owner = old.owner;
group = old.group;
perm = old.perm;
type = old.type;
customType = old.customType;
inArchive = old.inArchive;
recurse = old.recurse;
followLinksP = old.followLinksP;
whereToSearch = old.whereToSearch;
excludedFolderNames = old.excludedFolderNames;
whereNotToSearch = old.whereNotToSearch;
origFilter = old.origFilter;
codec = old.codec;
encodedEnterArray = old.encodedEnterArray;
encodedEnter = encodedEnterArray.data();
encodedEnterLen = encodedEnterArray.size();
return *this;
}
void KrQuery::load(const KConfigGroup &cfg)
{
*this = KrQuery(); // reset parameters first
if (cfg.readEntry("IsNull", true))
return;
#define LOAD(key, var) (var = cfg.readEntry(key, var))
LOAD("Matches", matches);
LOAD("Excludes", excludes);
LOAD("IncludedDirs", includedDirs);
LOAD("ExcludedDirs", excludedDirs);
LOAD("MatchesCaseSensitive", matchesCaseSensitive);
LOAD("Contain", contain);
LOAD("ContainCaseSensetive", containCaseSensetive);
LOAD("ContainWholeWord", containWholeWord);
LOAD("ContainRegExp", containRegExp);
LOAD("MinSize", minSize);
LOAD("MaxSize", maxSize);
newerThen = QDateTime::fromString(cfg.readEntry("NewerThan", QDateTime::fromSecsSinceEpoch(static_cast<uint>(newerThen)).toString())).toSecsSinceEpoch();
olderThen = QDateTime::fromString(cfg.readEntry("OlderThan", QDateTime::fromSecsSinceEpoch(static_cast<uint>(olderThen)).toString())).toSecsSinceEpoch();
LOAD("Owner", owner);
LOAD("Group", group);
LOAD("Perm", perm);
LOAD("Type", type);
LOAD("CustomType", customType);
LOAD("InArchive", inArchive);
LOAD("Recurse", recurse);
LOAD("FollowLinks", followLinksP);
// KF5 TODO?
// LOAD("WhereToSearch", whereToSearch);
// LOAD("WhereNotToSearch", whereNotToSearch);
LOAD("OrigFilter", origFilter);
codec = QTextCodec::codecForName(cfg.readEntry("Codec", codec->name()));
if (!codec)
codec = QTextCodec::codecForLocale();
LOAD("EncodedEnterArray", encodedEnterArray);
encodedEnter = encodedEnterArray.data();
encodedEnterLen = encodedEnterArray.size();
#undef LOAD
bNull = false;
}
void KrQuery::save(KConfigGroup cfg)
{
cfg.writeEntry("IsNull", bNull);
if (bNull)
return;
cfg.writeEntry("Matches", matches);
cfg.writeEntry("Excludes", excludes);
cfg.writeEntry("IncludedDirs", includedDirs);
cfg.writeEntry("ExcludedDirs", excludedDirs);
cfg.writeEntry("MatchesCaseSensitive", matchesCaseSensitive);
cfg.writeEntry("Contain", contain);
cfg.writeEntry("ContainCaseSensetive", containCaseSensetive);
cfg.writeEntry("ContainWholeWord", containWholeWord);
cfg.writeEntry("ContainRegExp", containRegExp);
cfg.writeEntry("MinSize", minSize);
cfg.writeEntry("MaxSize", maxSize);
cfg.writeEntry("NewerThan", QDateTime::fromSecsSinceEpoch(static_cast<uint>(newerThen)).toString());
cfg.writeEntry("OlderThan", QDateTime::fromSecsSinceEpoch(static_cast<uint>(olderThen)).toString());
cfg.writeEntry("Owner", owner);
cfg.writeEntry("Group", group);
cfg.writeEntry("Perm", perm);
cfg.writeEntry("Type", type);
cfg.writeEntry("CustomType", customType);
cfg.writeEntry("InArchive", inArchive);
cfg.writeEntry("Recurse", recurse);
cfg.writeEntry("FollowLinks", followLinksP);
// KF5 TODO?
// cfg.writeEntry("WhereToSearch", whereToSearch);
// cfg.writeEntry("WhereNotToSearch", whereNotToSearch);
cfg.writeEntry("OrigFilter", origFilter);
cfg.writeEntry("Codec", codec->name());
cfg.writeEntry("EncodedEnterArray", encodedEnterArray);
cfg.writeEntry("EncodedEnter", encodedEnter);
cfg.writeEntry("EncodedEnterLen", encodedEnterLen);
}
void KrQuery::connectNotify(const QMetaMethod &signal)
{
if (signal == QMetaMethod::fromSignal(&KrQuery::processEvents))
processEventsConnected++;
}
void KrQuery::disconnectNotify(const QMetaMethod &signal)
{
if (signal == QMetaMethod::fromSignal(&KrQuery::processEvents))
processEventsConnected--;
}
bool KrQuery::checkPerm(QString filePerm) const
{
for (int i = 0; i < 9; ++i)
if (perm[i] != '?' && perm[i] != filePerm[i + 1])
return false;
return true;
}
bool KrQuery::checkType(const QString &mime) const
{
if (type == mime)
return true;
if (type == i18n("Archives"))
return KrArcHandler::arcSupported(mime);
if (type == i18n("Folders"))
return mime.contains("directory");
if (type == i18n("Image Files"))
return mime.contains("image/");
if (type == i18n("Text Files"))
return mime.contains("text/");
if (type == i18n("Video Files"))
return mime.contains("video/");
if (type == i18n("Audio Files"))
return mime.contains("audio/");
if (type == i18n("Custom"))
return customType.contains(mime);
return false;
}
bool KrQuery::match(const QString &name) const
{
return matchCommon(name, matches, excludes);
}
bool KrQuery::matchDirName(const QString &name) const
{
return matchCommon(name, includedDirs, excludedDirs);
}
bool KrQuery::matchCommon(const QString &nameIn, const QStringList &matchList, const QStringList &excludeList) const
{
if (excludeList.count() == 0 && matchList.count() == 0) /* true if there's no match condition */
return true;
QString name(nameIn);
qsizetype ndx = nameIn.lastIndexOf('/'); // virtual filenames may contain '/'
if (ndx != -1) // but the end of the filename is OK
name = nameIn.mid(ndx + 1);
for (int i = 0; i < excludeList.count(); ++i) {
if (QRegExp(excludeList[i], matchesCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive, QRegExp::Wildcard).exactMatch(name))
return false;
}
if (matchList.count() == 0)
return true;
for (int i = 0; i < matchList.count(); ++i) {
if (QRegExp(matchList[i], matchesCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive, QRegExp::Wildcard).exactMatch(name))
return true;
}
return false;
}
bool KrQuery::match(FileItem *item) const
{
if (item->isDir() && !matchDirName(item->getName()))
return false;
// see if the name matches
if (!match(item->getName()))
return false;
// checking the mime
if (!type.isEmpty() && !checkType(item->getMime()))
return false;
// check that the size fit
KIO::filesize_t size = item->getSize();
if (minSize && size < minSize)
return false;
if (maxSize && size > maxSize)
return false;
// check the time frame
time_t mtime = item->getModificationTime();
if (olderThen && mtime > olderThen)
return false;
if (newerThen && mtime < newerThen)
return false;
// check owner name
if (!owner.isEmpty() && item->getOwner() != owner)
return false;
// check group name
if (!group.isEmpty() && item->getGroup() != group)
return false;
// check permission
if (!perm.isEmpty() && !checkPerm(item->getPerm()))
return false;
if (!contain.isEmpty()) {
totalBytes = item->getSize();
receivedBytes = 0;
if (receivedBuffer)
delete receivedBuffer;
receivedBuffer = nullptr;
receivedBufferLen = 0;
fileName = item->getName();
timer.start();
// search locally
if (item->getUrl().isLocalFile()) {
return containsContent(item->getUrl().path());
}
// search remotely
if (processEventsConnected == 0) {
return false;
}
return containsContent(item->getUrl());
}
return true;
}
// takes the string and adds BOLD to it, so that when it is displayed,
// the grepped text will be bold
void fixFoundTextForDisplay(QString &haystack, qsizetype start, qsizetype length)
{
QString before = haystack.left(start);
QString text = haystack.mid(start, length);
QString after = haystack.mid(start + length);
before.replace('&', "&");
before.replace('<', "<");
before.replace('>', ">");
text.replace('&', "&");
text.replace('<', "<");
text.replace('>', ">");
after.replace('&', "&");
after.replace('<', "<");
after.replace('>', ">");
haystack = ("<qt>" + before + "<b>" + text + "</b>" + after + "</qt>");
}
bool KrQuery::checkBuffer(const char *data, qsizetype len) const
{
bool result = false;
auto *mergedBuffer = new char[len + receivedBufferLen];
if (receivedBufferLen)
memcpy(mergedBuffer, receivedBuffer, receivedBufferLen);
if (len)
memcpy(mergedBuffer + receivedBufferLen, data, len);
qsizetype maxLen = len + receivedBufferLen;
qsizetype maxBuffer = maxLen - encodedEnterLen;
qsizetype lastLinePosition = 0;
for (qsizetype enterIndex = 0; enterIndex < maxBuffer; enterIndex++) {
if (memcmp(mergedBuffer + enterIndex, encodedEnter, encodedEnterLen) == 0) {
QString str = codec->toUnicode(mergedBuffer + lastLinePosition, static_cast<int>(enterIndex + encodedEnterLen - lastLinePosition));
if (str.endsWith('\n')) {
str.chop(1);
result = result || checkLine(str);
lastLinePosition = enterIndex + encodedEnterLen;
enterIndex = lastLinePosition;
continue;
}
}
}
if (maxLen - lastLinePosition > MAX_LINE_LEN || len == 0) {
QString str = codec->toUnicode(mergedBuffer + lastLinePosition, static_cast<int>(maxLen - lastLinePosition));
result = result || checkLine(str);
lastLinePosition = maxLen;
}
delete[] receivedBuffer;
receivedBuffer = nullptr;
receivedBufferLen = maxLen - lastLinePosition;
if (receivedBufferLen) {
receivedBuffer = new char[receivedBufferLen];
memcpy(receivedBuffer, mergedBuffer + lastLinePosition, receivedBufferLen);
}
delete[] mergedBuffer;
return result;
}
bool KrQuery::checkLine(const QString &line, bool backwards) const
{
if (containRegExp) {
QRegExp rexp(contain, containCaseSensetive ? Qt::CaseSensitive : Qt::CaseInsensitive, QRegExp::RegExp);
int ndx = backwards ? rexp.lastIndexIn(line) : rexp.indexIn(line);
bool result = ndx >= 0;
if (result)
fixFoundTextForDisplay(lastSuccessfulGrep = line, lastSuccessfulGrepMatchIndex = ndx, lastSuccessfulGrepMatchLength = rexp.matchedLength());
return result;
}
qsizetype ndx = backwards ? -1 : 0;
if (line.isNull())
return false;
if (containWholeWord) {
while ((ndx = (backwards) ? line.lastIndexOf(contain, ndx, containCaseSensetive ? Qt::CaseSensitive : Qt::CaseInsensitive)
: line.indexOf(contain, ndx, containCaseSensetive ? Qt::CaseSensitive : Qt::CaseInsensitive))
!= -1) {
QChar before = '\n';
QChar after = '\n';
if (ndx > 0)
before = line.at(ndx - 1);
if (ndx + contain.length() < line.length())
after = line.at(ndx + contain.length());
if (!before.isLetterOrNumber() && !after.isLetterOrNumber() && after != '_' && before != '_') {
lastSuccessfulGrep = line;
fixFoundTextForDisplay(lastSuccessfulGrep, lastSuccessfulGrepMatchIndex = ndx, lastSuccessfulGrepMatchLength = contain.length());
return true;
}
if (backwards)
ndx -= line.length() + 1;
else
ndx++;
}
} else if ((ndx = (backwards) ? line.lastIndexOf(contain, -1, containCaseSensetive ? Qt::CaseSensitive : Qt::CaseInsensitive)
: line.indexOf(contain, 0, containCaseSensetive ? Qt::CaseSensitive : Qt::CaseInsensitive))
!= -1) {
lastSuccessfulGrep = line;
fixFoundTextForDisplay(lastSuccessfulGrep, lastSuccessfulGrepMatchIndex = ndx, lastSuccessfulGrepMatchLength = contain.length());
return true;
}
return false;
}
bool KrQuery::containsContent(const QString &file) const
{
QFile qf(file);
if (!qf.open(QIODevice::ReadOnly))
return false;
char buffer[1440]; // 2k buffer
while (!qf.atEnd()) {
// Note: As it's stated in the documentation, "`qint64 QIODevice::read(char *data,
// qint64 maxSize)` Reads at most `maxSize` bytes"
int bytes = static_cast<int>(qf.read(buffer, sizeof(buffer)));
if (bytes <= 0)
break;
receivedBytes += bytes;
if (checkBuffer(buffer, bytes))
return true;
if (checkTimer()) {
bool stopped = false;
emit(const_cast<KrQuery *>(this))->processEvents(stopped);
if (stopped)
return false;
}
}
if (checkBuffer(buffer, 0))
return true;
lastSuccessfulGrep.clear(); // nothing was found
return false;
}
bool KrQuery::containsContent(const QUrl &url) const
{
KIO::TransferJob *contentReader = KIO::get(url, KIO::NoReload, KIO::HideProgressInfo);
connect(contentReader, &KIO::TransferJob::data, this, &KrQuery::containsContentData);
connect(contentReader, &KIO::Job::result, this, &KrQuery::containsContentFinished);
busy = true;
containsContentResult = false;
bool stopped = false;
while (busy && !stopped) {
checkTimer();
emit(const_cast<KrQuery *>(this))->processEvents(stopped);
}
if (busy) {
contentReader->kill(KJob::EmitResult);
busy = false;
}
return containsContentResult;
}
void KrQuery::containsContentData(KIO::Job *job, const QByteArray &array)
{
receivedBytes += array.size();
if (checkBuffer(array.data(), array.size())) {
containsContentResult = true;
containsContentFinished(job);
job->kill(KJob::EmitResult);
return;
}
checkTimer();
}
void KrQuery::containsContentFinished(KJob *)
{
busy = false;
}
bool KrQuery::checkTimer() const
{
if (timer.elapsed() >= STATUS_SEND_DELAY) {
static KFormat format;
// clang-format off
QString message =
i18nc("%1=filename, %2=percentage", "Searching content of '%1' (%2)", fileName,
(totalBytes > 0 && totalBytes >= receivedBytes) ?
// The normal case: A percentage is shown.
// Note: Instead of rounding, a truncation is performed because each percentage is seen
// only briefly —therefore, here speed is more important than precision
i18nc("%1=percentage", "%1%", (receivedBytes*100)/totalBytes) :
// An unusual case: There are problems when calculating that percentage. Sometimes it's
// because the contents of a big symlinked file are read, then the variable `totalBytes`
// is very small (it's the size of a symlink) but much more bytes are read
format.formatByteSize(static_cast<double>(receivedBytes), 0, KFormat::IECBinaryDialect, KFormat::UnitMegaByte)
);
// clang-format on
timer.start();
emit(const_cast<KrQuery *>(this))->status(message);
return true;
}
return false;
}
QStringList KrQuery::split(QString str)
{
QStringList list;
int splitNdx = 0;
int startNdx = 0;
bool quotation = false;
while (splitNdx < str.length()) {
if (str[splitNdx] == '"')
quotation = !quotation;
if (!quotation && str[splitNdx] == ' ') {
QString section = str.mid(startNdx, splitNdx - startNdx);
startNdx = splitNdx + 1;
if (section.startsWith('\"') && section.endsWith('\"') && section.length() >= 2)
section = section.mid(1, section.length() - 2);
if (!section.isEmpty())
list.append(section);
}
splitNdx++;
}
if (startNdx < splitNdx) {
QString section = str.mid(startNdx, splitNdx - startNdx);
if (section.startsWith('\"') && section.endsWith('\"') && section.length() >= 2)
section = section.mid(1, section.length() - 2);
if (!section.isEmpty())
list.append(section);
}
return list;
}
void KrQuery::setNameFilter(const QString &text, bool cs)
{
bNull = false;
matchesCaseSensitive = cs;
origFilter = text;
QString matchText = text;
QString excludeText;
int excludeNdx = 0;
bool quotationMark = 0;
while (excludeNdx < matchText.length()) {
if (matchText[excludeNdx] == '"')
quotationMark = !quotationMark;
if (!quotationMark) {
if (matchText[excludeNdx] == '|')
break;
}
excludeNdx++;
}
if (excludeNdx < matchText.length()) {
excludeText = matchText.mid(excludeNdx + 1).trimmed();
matchText.truncate(excludeNdx);
matchText = matchText.trimmed();
if (matchText.isEmpty())
matchText = '*';
}
int i;
matches = split(matchText);
includedDirs.clear();
for (i = 0; i < matches.count();) {
if (matches[i].endsWith('/')) {
includedDirs.push_back(matches[i].left(matches[i].length() - 1));
matches.removeAll(matches.at(i));
continue;
}
if (!matches[i].contains("*") && !matches[i].contains("?"))
matches[i] = '*' + matches[i] + '*';
i++;
}
excludes = split(excludeText);
excludedDirs.clear();
for (i = 0; i < excludes.count();) {
if (excludes[i].endsWith('/')) {
excludedDirs.push_back(excludes[i].left(excludes[i].length() - 1));
excludes.removeAll(excludes.at(i));
continue;
}
if (!excludes[i].contains("*") && !excludes[i].contains("?"))
excludes[i] = '*' + excludes[i] + '*';
i++;
}
}
void KrQuery::setContent(const QString &content, bool cs, bool wholeWord, const QString &encoding, bool regExp)
{
bNull = false;
contain = content;
containCaseSensetive = cs;
containWholeWord = wholeWord;
containRegExp = regExp;
if (encoding.isEmpty())
codec = QTextCodec::codecForLocale();
else {
codec = QTextCodec::codecForName(encoding.toLatin1());
if (codec == nullptr)
codec = QTextCodec::codecForLocale();
}
QChar ch = '\n';
QTextCodec::ConverterState state(QTextCodec::IgnoreHeader);
encodedEnterArray = codec->fromUnicode(&ch, 1, &state);
encodedEnter = encodedEnterArray.data();
encodedEnterLen = encodedEnterArray.size();
}
void KrQuery::setMinimumFileSize(KIO::filesize_t minimumSize)
{
bNull = false;
minSize = minimumSize;
}
void KrQuery::setMaximumFileSize(KIO::filesize_t maximumSize)
{
bNull = false;
maxSize = maximumSize;
}
void KrQuery::setNewerThan(time_t time)
{
bNull = false;
newerThen = time;
}
void KrQuery::setOlderThan(time_t time)
{
bNull = false;
olderThen = time;
}
void KrQuery::setOwner(const QString &ownerIn)
{
bNull = false;
owner = ownerIn;
}
void KrQuery::setGroup(const QString &groupIn)
{
bNull = false;
group = groupIn;
}
void KrQuery::setPermissions(const QString &permIn)
{
bNull = false;
perm = permIn;
}
void KrQuery::setMimeType(const QString &typeIn, QStringList customList)
{
bNull = false;
type = typeIn;
customType = std::move(customList);
}
bool KrQuery::isExcluded(const QUrl &url)
{
for (QUrl &item : whereNotToSearch)
if (item.isParentOf(url) || url.matches(item, QUrl::StripTrailingSlash))
return true;
// Exclude folder names that are configured in settings
QString filename = url.fileName();
for (QString &item : excludedFolderNames)
if (filename == item)
return true;
if (!matchDirName(filename))
return true;
return false;
}
void KrQuery::setSearchInDirs(const QList<QUrl> &urls)
{
whereToSearch.clear();
for (int i = 0; i < urls.count(); ++i) {
QString url = urls[i].url();
QUrl completed = QUrl::fromUserInput(KUrlCompletion::replacedPath(url, true, true), QString(), QUrl::AssumeLocalFile);
whereToSearch.append(completed);
}
}
void KrQuery::setDontSearchInDirs(const QList<QUrl> &urls)
{
whereNotToSearch.clear();
for (int i = 0; i < urls.count(); ++i) {
QString url = urls[i].url();
QUrl completed = QUrl::fromUserInput(KUrlCompletion::replacedPath(url, true, true), QString(), QUrl::AssumeLocalFile);
whereNotToSearch.append(completed);
}
}
void KrQuery::setExcludeFolderNames(const QStringList &paths)
{
excludedFolderNames.clear();
excludedFolderNames.append(paths);
}
|