File indexing completed on 2024-06-16 13:00:18

0001 /*
0002     This file is part of the KDE desktop environment
0003     SPDX-FileCopyrightText: 2001, 2002 Michael Brade <brade@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "kdirlistertest_gui.h"
0009 
0010 #include <QApplication>
0011 #include <QCommandLineParser>
0012 #include <QDebug>
0013 #include <QDir>
0014 #include <QPushButton>
0015 #include <QVBoxLayout>
0016 
0017 #include <cstdlib>
0018 
0019 KDirListerTest::KDirListerTest(QWidget *parent, const QUrl &initialUrl)
0020     : QWidget(parent)
0021 {
0022     lister = new KDirLister(this);
0023     debug = new PrintSignals;
0024 
0025     QVBoxLayout *layout = new QVBoxLayout(this);
0026 
0027     QPushButton *startH = new QPushButton(QStringLiteral("Start listing Home"), this);
0028     QPushButton *startR = new QPushButton(QStringLiteral("Start listing Root"), this);
0029     QPushButton *test = new QPushButton(QStringLiteral("Many"), this);
0030     QPushButton *startT = new QPushButton(QStringLiteral("tarfile"), this);
0031 
0032     layout->addWidget(startH);
0033     layout->addWidget(startR);
0034     layout->addWidget(startT);
0035     layout->addWidget(test);
0036     resize(layout->sizeHint());
0037 
0038     connect(startR, &QAbstractButton::clicked, this, &KDirListerTest::startRoot);
0039     connect(startH, &QAbstractButton::clicked, this, &KDirListerTest::startHome);
0040     connect(startT, &QAbstractButton::clicked, this, &KDirListerTest::startTar);
0041     connect(test, &QAbstractButton::clicked, this, &KDirListerTest::test);
0042 
0043     connect(lister, &KCoreDirLister::started, debug, &PrintSignals::started);
0044     connect(lister, qOverload<>(&KDirLister::completed), debug, &PrintSignals::completed);
0045     connect(lister, &KCoreDirLister::listingDirCompleted, debug, &PrintSignals::listingDirCompleted);
0046     connect(lister, qOverload<>(&KDirLister::canceled), debug, &PrintSignals::canceled);
0047     connect(lister, &KCoreDirLister::listingDirCanceled, debug, &PrintSignals::listingDirCanceled);
0048     connect(lister, qOverload<const QUrl &, const QUrl &>(&KDirLister::redirection), debug, &PrintSignals::redirection);
0049     connect(lister, qOverload<>(&KDirLister::clear), debug, &PrintSignals::clear);
0050     connect(lister, &KCoreDirLister::newItems, debug, &PrintSignals::newItems);
0051 #if KIOCORE_BUILD_DEPRECATED_SINCE(5, 100)
0052     connect(lister, &KCoreDirLister::itemsFilteredByMime, debug, &PrintSignals::itemsFilteredByMime);
0053 #endif
0054     connect(lister, &KCoreDirLister::itemsDeleted, debug, &PrintSignals::itemsDeleted);
0055     connect(lister, &KCoreDirLister::refreshItems, debug, &PrintSignals::refreshItems);
0056     connect(lister, &KCoreDirLister::infoMessage, debug, &PrintSignals::infoMessage);
0057     connect(lister, &KCoreDirLister::percent, debug, &PrintSignals::percent);
0058     connect(lister, &KCoreDirLister::totalSize, debug, &PrintSignals::totalSize);
0059     connect(lister, &KCoreDirLister::processedSize, debug, &PrintSignals::processedSize);
0060     connect(lister, &KCoreDirLister::speed, debug, &PrintSignals::speed);
0061 
0062     connect(lister, qOverload<>(&KDirLister::completed), this, &KDirListerTest::completed);
0063 
0064     if (initialUrl.isValid()) {
0065         lister->openUrl(initialUrl, KDirLister::NoFlags);
0066     }
0067 }
0068 
0069 KDirListerTest::~KDirListerTest()
0070 {
0071 }
0072 
0073 void KDirListerTest::startHome()
0074 {
0075     QUrl home = QUrl::fromLocalFile(QDir::homePath());
0076     lister->openUrl(home, KDirLister::NoFlags);
0077     //  lister->stop();
0078 }
0079 
0080 void KDirListerTest::startRoot()
0081 {
0082     QUrl root = QUrl::fromLocalFile(QDir::rootPath());
0083     lister->openUrl(root, KDirLister::Keep | KDirLister::Reload);
0084     // lister->stop( root );
0085 }
0086 
0087 void KDirListerTest::startTar()
0088 {
0089     QUrl root = QUrl::fromLocalFile(QDir::homePath() + "/aclocal_1.tgz");
0090     lister->openUrl(root, KDirLister::Keep | KDirLister::Reload);
0091     // lister->stop( root );
0092 }
0093 
0094 void KDirListerTest::test()
0095 {
0096     QUrl home = QUrl::fromLocalFile(QDir::homePath());
0097     QUrl root = QUrl::fromLocalFile(QDir::rootPath());
0098 #ifdef Q_OS_WIN
0099     lister->openUrl(home, KDirLister::Keep);
0100     lister->openUrl(root, KDirLister::Keep | KDirLister::Reload);
0101 #else
0102     /*  lister->openUrl( home, KDirLister::Keep );
0103       lister->openUrl( root, KDirLister::Keep | KDirLister::Reload );
0104       lister->openUrl( QUrl::fromLocalFile("file:/etc"), KDirLister::Keep | KDirLister::Reload );
0105       lister->openUrl( root, KDirLister::Keep | KDirLister::Reload );
0106       lister->openUrl( QUrl::fromLocalFile("file:/dev"), KDirLister::Keep | KDirLister::Reload );
0107       lister->openUrl( QUrl::fromLocalFile("file:/tmp"), KDirLister::Keep | KDirLister::Reload );
0108       lister->openUrl( QUrl::fromLocalFile("file:/usr/include"), KDirLister::Keep | KDirLister::Reload );
0109       lister->updateDirectory( QUrl::fromLocalFile("file:/usr/include") );
0110       lister->updateDirectory( QUrl::fromLocalFile("file:/usr/include") );
0111       lister->openUrl( QUrl::fromLocalFile("file:/usr/"), KDirLister::Keep | KDirLister::Reload );
0112     */
0113     lister->openUrl(QUrl::fromLocalFile(QStringLiteral("/dev")), KDirLister::Keep | KDirLister::Reload);
0114 #endif
0115 }
0116 
0117 void KDirListerTest::completed()
0118 {
0119     if (lister->url().toLocalFile() == QDir::rootPath()) {
0120         const KFileItem item = lister->findByUrl(QUrl::fromLocalFile(QDir::tempPath()));
0121         if (!item.isNull()) {
0122             qDebug() << "Found " << QDir::tempPath() << ": " << item.name();
0123         } else {
0124             qWarning() << QDir::tempPath() << " not found! Bug in findByURL?";
0125         }
0126     }
0127 }
0128 
0129 int main(int argc, char *argv[])
0130 {
0131     QApplication::setApplicationName(QStringLiteral("kdirlistertest"));
0132     QApplication app(argc, argv);
0133 
0134     QCommandLineParser parser;
0135     parser.addHelpOption();
0136     parser.addPositionalArgument(QStringLiteral("URL"), QStringLiteral("URL to a directory to list."), QStringLiteral("[URL...]"));
0137     parser.process(app);
0138     QUrl url;
0139     if (!parser.positionalArguments().isEmpty()) {
0140         url = QUrl::fromUserInput(parser.positionalArguments().at(0));
0141     }
0142 
0143     KDirListerTest *test = new KDirListerTest(nullptr, url);
0144     test->show();
0145     return app.exec();
0146 }
0147 
0148 #include "moc_kdirlistertest_gui.cpp"