Changeset View
Changeset View
Standalone View
Standalone View
krusader/FileSystem/krquery.cpp
Show All 25 Lines | |||||
26 | #include <QMetaMethod> | 26 | #include <QMetaMethod> | ||
27 | #include <QRegExp> | 27 | #include <QRegExp> | ||
28 | #include <QTextCodec> | 28 | #include <QTextCodec> | ||
29 | 29 | | |||
30 | #include <KI18n/KLocalizedString> | 30 | #include <KI18n/KLocalizedString> | ||
31 | #include <KIO/Job> | 31 | #include <KIO/Job> | ||
32 | #include <KIOCore/KFileItem> | 32 | #include <KIOCore/KFileItem> | ||
33 | #include <KIOWidgets/KUrlCompletion> | 33 | #include <KIOWidgets/KUrlCompletion> | ||
34 | #include <utility> | ||||
34 | 35 | | |||
35 | #include "../Archive/krarchandler.h" | 36 | #include "../Archive/krarchandler.h" | ||
36 | #include "fileitem.h" | 37 | #include "fileitem.h" | ||
37 | #include "filesystem.h" | 38 | #include "filesystem.h" | ||
38 | #include "krpermhandler.h" | 39 | #include "krpermhandler.h" | ||
39 | 40 | | |||
40 | #define STATUS_SEND_DELAY 250 | 41 | #define STATUS_SEND_DELAY 250 | ||
41 | #define MAX_LINE_LEN 1000 | 42 | #define MAX_LINE_LEN 1000 | ||
42 | 43 | | |||
43 | // set the defaults | 44 | // set the defaults | ||
44 | KRQuery::KRQuery() | 45 | KRQuery::KRQuery() | ||
45 | : QObject(), matchesCaseSensitive(true), bNull(true), contain(QString()), | 46 | : matchesCaseSensitive(true), bNull(true), contain(QString()), | ||
46 | containCaseSensetive(true), containWholeWord(false), containRegExp(false), minSize(0), | 47 | containCaseSensetive(true), containWholeWord(false), containRegExp(false), minSize(0), | ||
47 | maxSize(0), newerThen(0), olderThen(0), owner(QString()), group(QString()), perm(QString()), | 48 | maxSize(0), newerThen(0), olderThen(0), owner(QString()), group(QString()), perm(QString()), | ||
48 | type(QString()), inArchive(false), recurse(true), followLinksP(true), receivedBuffer(0), | 49 | type(QString()), inArchive(false), recurse(true), followLinksP(true), receivedBuffer(nullptr), | ||
49 | receivedBufferLen(0), processEventsConnected(0), codec(QTextCodec::codecForLocale()) | 50 | receivedBufferLen(0), processEventsConnected(0), codec(QTextCodec::codecForLocale()) | ||
50 | { | 51 | { | ||
51 | QChar ch = '\n'; | 52 | QChar ch = '\n'; | ||
52 | QTextCodec::ConverterState state(QTextCodec::IgnoreHeader); | 53 | QTextCodec::ConverterState state(QTextCodec::IgnoreHeader); | ||
53 | encodedEnterArray = codec->fromUnicode(&ch, 1, &state); | 54 | encodedEnterArray = codec->fromUnicode(&ch, 1, &state); | ||
54 | encodedEnter = encodedEnterArray.data(); | 55 | encodedEnter = encodedEnterArray.data(); | ||
55 | encodedEnterLen = encodedEnterArray.size(); | 56 | encodedEnterLen = encodedEnterArray.size(); | ||
56 | } | 57 | } | ||
57 | 58 | | |||
58 | // set the defaults | 59 | // set the defaults | ||
59 | KRQuery::KRQuery(const QString &name, bool matchCase) | 60 | KRQuery::KRQuery(const QString &name, bool matchCase) | ||
60 | : QObject(), bNull(true), contain(QString()), containCaseSensetive(true), | 61 | : bNull(true), contain(QString()), containCaseSensetive(true), | ||
61 | containWholeWord(false), containRegExp(false), minSize(0), maxSize(0), newerThen(0), | 62 | containWholeWord(false), containRegExp(false), minSize(0), maxSize(0), newerThen(0), | ||
62 | olderThen(0), owner(QString()), group(QString()), perm(QString()), type(QString()), | 63 | olderThen(0), owner(QString()), group(QString()), perm(QString()), type(QString()), | ||
63 | inArchive(false), recurse(true), followLinksP(true), receivedBuffer(0), receivedBufferLen(0), | 64 | inArchive(false), recurse(true), followLinksP(true), receivedBuffer(nullptr), receivedBufferLen(0), | ||
64 | processEventsConnected(0), codec(QTextCodec::codecForLocale()) | 65 | processEventsConnected(0), codec(QTextCodec::codecForLocale()) | ||
65 | { | 66 | { | ||
66 | QChar ch = '\n'; | 67 | QChar ch = '\n'; | ||
67 | QTextCodec::ConverterState state(QTextCodec::IgnoreHeader); | 68 | QTextCodec::ConverterState state(QTextCodec::IgnoreHeader); | ||
68 | encodedEnterArray = codec->fromUnicode(&ch, 1, &state); | 69 | encodedEnterArray = codec->fromUnicode(&ch, 1, &state); | ||
69 | encodedEnter = encodedEnterArray.data(); | 70 | encodedEnter = encodedEnterArray.data(); | ||
70 | encodedEnterLen = encodedEnterArray.size(); | 71 | encodedEnterLen = encodedEnterArray.size(); | ||
71 | 72 | | |||
72 | setNameFilter(name, matchCase); | 73 | setNameFilter(name, matchCase); | ||
73 | } | 74 | } | ||
74 | 75 | | |||
75 | KRQuery::KRQuery(const KRQuery &that) | 76 | KRQuery::KRQuery(const KRQuery &that) | ||
76 | : QObject(), receivedBuffer(0), receivedBufferLen(0), processEventsConnected(0) | 77 | : QObject(), receivedBuffer(nullptr), receivedBufferLen(0), processEventsConnected(0) | ||
77 | { | 78 | { | ||
78 | *this = that; | 79 | *this = that; | ||
79 | } | 80 | } | ||
80 | 81 | | |||
81 | KRQuery::~KRQuery() | 82 | KRQuery::~KRQuery() | ||
82 | { | 83 | { | ||
83 | if (receivedBuffer) | 84 | if (receivedBuffer) | ||
84 | delete[] receivedBuffer; | 85 | delete[] receivedBuffer; | ||
85 | receivedBuffer = 0; | 86 | receivedBuffer = nullptr; | ||
86 | } | 87 | } | ||
87 | 88 | | |||
88 | KRQuery &KRQuery::operator=(const KRQuery &old) | 89 | KRQuery &KRQuery::operator=(const KRQuery &old) | ||
89 | { | 90 | { | ||
90 | matches = old.matches; | 91 | matches = old.matches; | ||
91 | excludes = old.excludes; | 92 | excludes = old.excludes; | ||
92 | includedDirs = old.includedDirs; | 93 | includedDirs = old.includedDirs; | ||
93 | excludedDirs = old.excludedDirs; | 94 | excludedDirs = old.excludedDirs; | ||
Show All 24 Lines | |||||
118 | 119 | | |||
119 | encodedEnterArray = old.encodedEnterArray; | 120 | encodedEnterArray = old.encodedEnterArray; | ||
120 | encodedEnter = encodedEnterArray.data(); | 121 | encodedEnter = encodedEnterArray.data(); | ||
121 | encodedEnterLen = encodedEnterArray.size(); | 122 | encodedEnterLen = encodedEnterArray.size(); | ||
122 | 123 | | |||
123 | return *this; | 124 | return *this; | ||
124 | } | 125 | } | ||
125 | 126 | | |||
126 | void KRQuery::load(KConfigGroup cfg) | 127 | void KRQuery::load(const KConfigGroup& cfg) | ||
127 | { | 128 | { | ||
128 | *this = KRQuery(); // reset parameters first | 129 | *this = KRQuery(); // reset parameters first | ||
129 | 130 | | |||
130 | if (cfg.readEntry("IsNull", true)) | 131 | if (cfg.readEntry("IsNull", true)) | ||
131 | return; | 132 | return; | ||
132 | 133 | | |||
133 | #define LOAD(key, var) (var = cfg.readEntry(key, var)) | 134 | #define LOAD(key, var) (var = cfg.readEntry(key, var)) | ||
134 | LOAD("Matches", matches); | 135 | LOAD("Matches", matches); | ||
▲ Show 20 Lines • Show All 92 Lines • ▼ Show 20 Line(s) | |||||
227 | bool KRQuery::checkPerm(QString filePerm) const | 228 | bool KRQuery::checkPerm(QString filePerm) const | ||
228 | { | 229 | { | ||
229 | for (int i = 0; i < 9; ++i) | 230 | for (int i = 0; i < 9; ++i) | ||
230 | if (perm[i] != '?' && perm[i] != filePerm[i + 1]) | 231 | if (perm[i] != '?' && perm[i] != filePerm[i + 1]) | ||
231 | return false; | 232 | return false; | ||
232 | return true; | 233 | return true; | ||
233 | } | 234 | } | ||
234 | 235 | | |||
235 | bool KRQuery::checkType(QString mime) const | 236 | bool KRQuery::checkType(const QString& mime) const | ||
236 | { | 237 | { | ||
237 | if (type == mime) | 238 | if (type == mime) | ||
238 | return true; | 239 | return true; | ||
239 | if (type == i18n("Archives")) | 240 | if (type == i18n("Archives")) | ||
240 | return KRarcHandler::arcSupported(mime); | 241 | return KRarcHandler::arcSupported(mime); | ||
241 | if (type == i18n("Folders")) | 242 | if (type == i18n("Folders")) | ||
242 | return mime.contains("directory"); | 243 | return mime.contains("directory"); | ||
243 | if (type == i18n("Image Files")) | 244 | if (type == i18n("Image Files")) | ||
▲ Show 20 Lines • Show All 79 Lines • ▼ Show 20 Line(s) | 323 | if (!perm.isEmpty() && !checkPerm(item->getPerm())) | |||
323 | return false; | 324 | return false; | ||
324 | 325 | | |||
325 | if (!contain.isEmpty()) { | 326 | if (!contain.isEmpty()) { | ||
326 | if ((totalBytes = item->getSize()) == 0) | 327 | if ((totalBytes = item->getSize()) == 0) | ||
327 | totalBytes++; // sanity | 328 | totalBytes++; // sanity | ||
328 | receivedBytes = 0; | 329 | receivedBytes = 0; | ||
329 | if (receivedBuffer) | 330 | if (receivedBuffer) | ||
330 | delete receivedBuffer; | 331 | delete receivedBuffer; | ||
331 | receivedBuffer = 0; | 332 | receivedBuffer = nullptr; | ||
332 | receivedBufferLen = 0; | 333 | receivedBufferLen = 0; | ||
333 | fileName = item->getName(); | 334 | fileName = item->getName(); | ||
334 | timer.start(); | 335 | timer.start(); | ||
335 | 336 | | |||
336 | // search locally | 337 | // search locally | ||
337 | if (item->getUrl().isLocalFile()) { | 338 | if (item->getUrl().isLocalFile()) { | ||
338 | return containsContent(item->getUrl().path()); | 339 | return containsContent(item->getUrl().path()); | ||
339 | } | 340 | } | ||
Show All 30 Lines | 355 | { | |||
370 | 371 | | |||
371 | haystack = ("<qt>" + before + "<b>" + text + "</b>" + after + "</qt>"); | 372 | haystack = ("<qt>" + before + "<b>" + text + "</b>" + after + "</qt>"); | ||
372 | } | 373 | } | ||
373 | 374 | | |||
374 | bool KRQuery::checkBuffer(const char *data, int len) const | 375 | bool KRQuery::checkBuffer(const char *data, int len) const | ||
375 | { | 376 | { | ||
376 | bool result = false; | 377 | bool result = false; | ||
377 | 378 | | |||
378 | char *mergedBuffer = new char[len + receivedBufferLen]; | 379 | auto *mergedBuffer = new char[len + receivedBufferLen]; | ||
379 | if (receivedBufferLen) | 380 | if (receivedBufferLen) | ||
380 | memcpy(mergedBuffer, receivedBuffer, receivedBufferLen); | 381 | memcpy(mergedBuffer, receivedBuffer, receivedBufferLen); | ||
381 | if (len) | 382 | if (len) | ||
382 | memcpy(mergedBuffer + receivedBufferLen, data, len); | 383 | memcpy(mergedBuffer + receivedBufferLen, data, len); | ||
383 | 384 | | |||
384 | int maxLen = len + receivedBufferLen; | 385 | int maxLen = len + receivedBufferLen; | ||
385 | int maxBuffer = maxLen - encodedEnterLen; | 386 | int maxBuffer = maxLen - encodedEnterLen; | ||
386 | int lastLinePosition = 0; | 387 | int lastLinePosition = 0; | ||
Show All 14 Lines | |||||
401 | 402 | | |||
402 | if (maxLen - lastLinePosition > MAX_LINE_LEN || len == 0) { | 403 | if (maxLen - lastLinePosition > MAX_LINE_LEN || len == 0) { | ||
403 | QString str = codec->toUnicode(mergedBuffer + lastLinePosition, maxLen - lastLinePosition); | 404 | QString str = codec->toUnicode(mergedBuffer + lastLinePosition, maxLen - lastLinePosition); | ||
404 | result = result || checkLine(str); | 405 | result = result || checkLine(str); | ||
405 | lastLinePosition = maxLen; | 406 | lastLinePosition = maxLen; | ||
406 | } | 407 | } | ||
407 | 408 | | |||
408 | delete[] receivedBuffer; | 409 | delete[] receivedBuffer; | ||
409 | receivedBuffer = 0; | 410 | receivedBuffer = nullptr; | ||
410 | receivedBufferLen = maxLen - lastLinePosition; | 411 | receivedBufferLen = maxLen - lastLinePosition; | ||
411 | 412 | | |||
412 | if (receivedBufferLen) { | 413 | if (receivedBufferLen) { | ||
413 | receivedBuffer = new char[receivedBufferLen]; | 414 | receivedBuffer = new char[receivedBufferLen]; | ||
414 | memcpy(receivedBuffer, mergedBuffer + lastLinePosition, receivedBufferLen); | 415 | memcpy(receivedBuffer, mergedBuffer + lastLinePosition, receivedBufferLen); | ||
415 | } | 416 | } | ||
416 | 417 | | |||
417 | delete[] mergedBuffer; | 418 | delete[] mergedBuffer; | ||
▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Line(s) | 468 | } else if ((ndx = (backwards) ? | |||
474 | lastSuccessfulGrep = line; | 475 | lastSuccessfulGrep = line; | ||
475 | fixFoundTextForDisplay(lastSuccessfulGrep, lastSuccessfulGrepMatchIndex = ndx, | 476 | fixFoundTextForDisplay(lastSuccessfulGrep, lastSuccessfulGrepMatchIndex = ndx, | ||
476 | lastSuccessfulGrepMatchLength = contain.length()); | 477 | lastSuccessfulGrepMatchLength = contain.length()); | ||
477 | return true; | 478 | return true; | ||
478 | } | 479 | } | ||
479 | return false; | 480 | return false; | ||
480 | } | 481 | } | ||
481 | 482 | | |||
482 | bool KRQuery::containsContent(QString file) const | 483 | bool KRQuery::containsContent(const QString& file) const | ||
483 | { | 484 | { | ||
484 | QFile qf(file); | 485 | QFile qf(file); | ||
485 | if (!qf.open(QIODevice::ReadOnly)) | 486 | if (!qf.open(QIODevice::ReadOnly)) | ||
486 | return false; | 487 | return false; | ||
487 | 488 | | |||
488 | char buffer[1440]; // 2k buffer | 489 | char buffer[1440]; // 2k buffer | ||
489 | 490 | | |||
490 | while (!qf.atEnd()) { | 491 | while (!qf.atEnd()) { | ||
Show All 15 Lines | |||||
506 | } | 507 | } | ||
507 | if (checkBuffer(buffer, 0)) | 508 | if (checkBuffer(buffer, 0)) | ||
508 | return true; | 509 | return true; | ||
509 | 510 | | |||
510 | lastSuccessfulGrep.clear(); // nothing was found | 511 | lastSuccessfulGrep.clear(); // nothing was found | ||
511 | return false; | 512 | return false; | ||
512 | } | 513 | } | ||
513 | 514 | | |||
514 | bool KRQuery::containsContent(QUrl url) const | 515 | bool KRQuery::containsContent(const QUrl& url) const | ||
515 | { | 516 | { | ||
516 | KIO::TransferJob *contentReader = KIO::get(url, KIO::NoReload, KIO::HideProgressInfo); | 517 | KIO::TransferJob *contentReader = KIO::get(url, KIO::NoReload, KIO::HideProgressInfo); | ||
517 | connect(contentReader, &KIO::TransferJob::data, this, &KRQuery::containsContentData); | 518 | connect(contentReader, &KIO::TransferJob::data, this, &KRQuery::containsContentData); | ||
518 | connect(contentReader, &KIO::Job::result, this, &KRQuery::containsContentFinished); | 519 | connect(contentReader, &KIO::Job::result, this, &KRQuery::containsContentFinished); | ||
519 | 520 | | |||
520 | busy = true; | 521 | busy = true; | ||
521 | containsContentResult = false; | 522 | containsContentResult = false; | ||
522 | bool stopped = false; | 523 | bool stopped = false; | ||
Show All 23 Lines | 539 | { | |||
546 | checkTimer(); | 547 | checkTimer(); | ||
547 | } | 548 | } | ||
548 | 549 | | |||
549 | void KRQuery::containsContentFinished(KJob *) { busy = false; } | 550 | void KRQuery::containsContentFinished(KJob *) { busy = false; } | ||
550 | 551 | | |||
551 | bool KRQuery::checkTimer() const | 552 | bool KRQuery::checkTimer() const | ||
552 | { | 553 | { | ||
553 | if (timer.elapsed() >= STATUS_SEND_DELAY) { | 554 | if (timer.elapsed() >= STATUS_SEND_DELAY) { | ||
554 | int pcnt = (int)(100. * (double)receivedBytes / (double)totalBytes + .5); | 555 | auto pcnt = (int)(100. * (double)receivedBytes / (double)totalBytes + .5); | ||
555 | QString message = | 556 | QString message = | ||
556 | i18nc("%1=filename, %2=percentage", "Searching content of '%1' (%2%)", fileName, pcnt); | 557 | i18nc("%1=filename, %2=percentage", "Searching content of '%1' (%2%)", fileName, pcnt); | ||
557 | timer.start(); | 558 | timer.start(); | ||
558 | emit((KRQuery *)this)->status(message); | 559 | emit((KRQuery *)this)->status(message); | ||
559 | return true; | 560 | return true; | ||
560 | } | 561 | } | ||
561 | return false; | 562 | return false; | ||
562 | } | 563 | } | ||
▲ Show 20 Lines • Show All 90 Lines • ▼ Show 20 Line(s) | 648 | for (i = 0; i < excludes.count();) { | |||
653 | 654 | | |||
654 | if (!excludes[i].contains("*") && !excludes[i].contains("?")) | 655 | if (!excludes[i].contains("*") && !excludes[i].contains("?")) | ||
655 | excludes[i] = '*' + excludes[i] + '*'; | 656 | excludes[i] = '*' + excludes[i] + '*'; | ||
656 | 657 | | |||
657 | i++; | 658 | i++; | ||
658 | } | 659 | } | ||
659 | } | 660 | } | ||
660 | 661 | | |||
661 | void KRQuery::setContent(const QString &content, bool cs, bool wholeWord, QString encoding, | 662 | void KRQuery::setContent(const QString &content, bool cs, bool wholeWord, const QString& encoding, | ||
662 | bool regExp) | 663 | bool regExp) | ||
663 | { | 664 | { | ||
664 | bNull = false; | 665 | bNull = false; | ||
665 | contain = content; | 666 | contain = content; | ||
666 | containCaseSensetive = cs; | 667 | containCaseSensetive = cs; | ||
667 | containWholeWord = wholeWord; | 668 | containWholeWord = wholeWord; | ||
668 | containRegExp = regExp; | 669 | containRegExp = regExp; | ||
669 | 670 | | |||
670 | if (encoding.isEmpty()) | 671 | if (encoding.isEmpty()) | ||
671 | codec = QTextCodec::codecForLocale(); | 672 | codec = QTextCodec::codecForLocale(); | ||
672 | else { | 673 | else { | ||
673 | codec = QTextCodec::codecForName(encoding.toLatin1()); | 674 | codec = QTextCodec::codecForName(encoding.toLatin1()); | ||
674 | if (codec == 0) | 675 | if (codec == nullptr) | ||
675 | codec = QTextCodec::codecForLocale(); | 676 | codec = QTextCodec::codecForLocale(); | ||
676 | } | 677 | } | ||
677 | 678 | | |||
678 | QChar ch = '\n'; | 679 | QChar ch = '\n'; | ||
679 | QTextCodec::ConverterState state(QTextCodec::IgnoreHeader); | 680 | QTextCodec::ConverterState state(QTextCodec::IgnoreHeader); | ||
680 | encodedEnterArray = codec->fromUnicode(&ch, 1, &state); | 681 | encodedEnterArray = codec->fromUnicode(&ch, 1, &state); | ||
681 | encodedEnter = encodedEnterArray.data(); | 682 | encodedEnter = encodedEnterArray.data(); | ||
682 | encodedEnterLen = encodedEnterArray.size(); | 683 | encodedEnterLen = encodedEnterArray.size(); | ||
Show All 40 Lines | 723 | { | |||
723 | bNull = false; | 724 | bNull = false; | ||
724 | perm = permIn; | 725 | perm = permIn; | ||
725 | } | 726 | } | ||
726 | 727 | | |||
727 | void KRQuery::setMimeType(const QString &typeIn, QStringList customList) | 728 | void KRQuery::setMimeType(const QString &typeIn, QStringList customList) | ||
728 | { | 729 | { | ||
729 | bNull = false; | 730 | bNull = false; | ||
730 | type = typeIn; | 731 | type = typeIn; | ||
731 | customType = customList; | 732 | customType = std::move(customList); | ||
732 | } | 733 | } | ||
733 | 734 | | |||
734 | bool KRQuery::isExcluded(const QUrl &url) | 735 | bool KRQuery::isExcluded(const QUrl &url) | ||
735 | { | 736 | { | ||
736 | for (QUrl &item : whereNotToSearch) | 737 | for (QUrl &item : whereNotToSearch) | ||
737 | if (item.isParentOf(url) || url.matches(item, QUrl::StripTrailingSlash)) | 738 | if (item.isParentOf(url) || url.matches(item, QUrl::StripTrailingSlash)) | ||
738 | return true; | 739 | return true; | ||
739 | 740 | | |||
Show All 38 Lines |