File indexing completed on 2024-12-08 06:40:37

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, &KDirLister::redirection, debug, &PrintSignals::redirection);
0049     connect(lister, qOverload<>(&KDirLister::clear), debug, &PrintSignals::clear);
0050     connect(lister, &KCoreDirLister::newItems, debug, &PrintSignals::newItems);
0051     connect(lister, &KCoreDirLister::itemsDeleted, debug, &PrintSignals::itemsDeleted);
0052     connect(lister, &KCoreDirLister::refreshItems, debug, &PrintSignals::refreshItems);
0053     connect(lister, &KCoreDirLister::infoMessage, debug, &PrintSignals::infoMessage);
0054     connect(lister, &KCoreDirLister::percent, debug, &PrintSignals::percent);
0055     connect(lister, &KCoreDirLister::totalSize, debug, &PrintSignals::totalSize);
0056     connect(lister, &KCoreDirLister::processedSize, debug, &PrintSignals::processedSize);
0057     connect(lister, &KCoreDirLister::speed, debug, &PrintSignals::speed);
0058 
0059     connect(lister, qOverload<>(&KDirLister::completed), this, &KDirListerTest::completed);
0060 
0061     if (initialUrl.isValid()) {
0062         lister->openUrl(initialUrl, KDirLister::NoFlags);
0063     }
0064 }
0065 
0066 KDirListerTest::~KDirListerTest()
0067 {
0068 }
0069 
0070 void KDirListerTest::startHome()
0071 {
0072     QUrl home = QUrl::fromLocalFile(QDir::homePath());
0073     lister->openUrl(home, KDirLister::NoFlags);
0074     //  lister->stop();
0075 }
0076 
0077 void KDirListerTest::startRoot()
0078 {
0079     QUrl root = QUrl::fromLocalFile(QDir::rootPath());
0080     lister->openUrl(root, KDirLister::Keep | KDirLister::Reload);
0081     // lister->stop( root );
0082 }
0083 
0084 void KDirListerTest::startTar()
0085 {
0086     QUrl root = QUrl::fromLocalFile(QDir::homePath() + QStringLiteral("/aclocal_1.tgz"));
0087     lister->openUrl(root, KDirLister::Keep | KDirLister::Reload);
0088     // lister->stop( root );
0089 }
0090 
0091 void KDirListerTest::test()
0092 {
0093     QUrl home = QUrl::fromLocalFile(QDir::homePath());
0094     QUrl root = QUrl::fromLocalFile(QDir::rootPath());
0095 #ifdef Q_OS_WIN
0096     lister->openUrl(home, KDirLister::Keep);
0097     lister->openUrl(root, KDirLister::Keep | KDirLister::Reload);
0098 #else
0099     /*  lister->openUrl( home, KDirLister::Keep );
0100       lister->openUrl( root, KDirLister::Keep | KDirLister::Reload );
0101       lister->openUrl( QUrl::fromLocalFile("file:/etc"), KDirLister::Keep | KDirLister::Reload );
0102       lister->openUrl( root, KDirLister::Keep | KDirLister::Reload );
0103       lister->openUrl( QUrl::fromLocalFile("file:/dev"), KDirLister::Keep | KDirLister::Reload );
0104       lister->openUrl( QUrl::fromLocalFile("file:/tmp"), KDirLister::Keep | KDirLister::Reload );
0105       lister->openUrl( QUrl::fromLocalFile("file:/usr/include"), KDirLister::Keep | KDirLister::Reload );
0106       lister->updateDirectory( QUrl::fromLocalFile("file:/usr/include") );
0107       lister->updateDirectory( QUrl::fromLocalFile("file:/usr/include") );
0108       lister->openUrl( QUrl::fromLocalFile("file:/usr/"), KDirLister::Keep | KDirLister::Reload );
0109     */
0110     lister->openUrl(QUrl::fromLocalFile(QStringLiteral("/dev")), KDirLister::Keep | KDirLister::Reload);
0111 #endif
0112 }
0113 
0114 void KDirListerTest::completed()
0115 {
0116     if (lister->url().toLocalFile() == QDir::rootPath()) {
0117         const KFileItem item = lister->findByUrl(QUrl::fromLocalFile(QDir::tempPath()));
0118         if (!item.isNull()) {
0119             qDebug() << "Found " << QDir::tempPath() << ": " << item.name();
0120         } else {
0121             qWarning() << QDir::tempPath() << " not found! Bug in findByURL?";
0122         }
0123     }
0124 }
0125 
0126 int main(int argc, char *argv[])
0127 {
0128     QApplication::setApplicationName(QStringLiteral("kdirlistertest"));
0129     QApplication app(argc, argv);
0130 
0131     QCommandLineParser parser;
0132     parser.addHelpOption();
0133     parser.addPositionalArgument(QStringLiteral("URL"), QStringLiteral("URL to a directory to list."), QStringLiteral("[URL...]"));
0134     parser.process(app);
0135     QUrl url;
0136     if (!parser.positionalArguments().isEmpty()) {
0137         url = QUrl::fromUserInput(parser.positionalArguments().at(0));
0138     }
0139 
0140     KDirListerTest *test = new KDirListerTest(nullptr, url);
0141     test->show();
0142     return app.exec();
0143 }
0144 
0145 #include "moc_kdirlistertest_gui.cpp"