File indexing completed on 2024-04-21 04:18:47

0001 // vim: set tabstop=4 shiftwidth=4 expandtab:
0002 /*
0003 Gwenview: an image viewer
0004 Copyright 2009 Aurélien Gâteau <agateau@kde.org>
0005 
0006 This program is free software; you can redistribute it and/or
0007 modify it under the terms of the GNU General Public License
0008 as published by the Free Software Foundation; either version 2
0009 of the License, or (at your option) any later version.
0010 
0011 This program is distributed in the hope that it will be useful,
0012 but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 GNU General Public License for more details.
0015 
0016 You should have received a copy of the GNU General Public License
0017 along with this program; if not, write to the Free Software
0018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
0019 
0020 */
0021 // Self
0022 #include "documentdirfinder.h"
0023 
0024 // Qt
0025 
0026 // KF
0027 #include <KDirLister>
0028 #include <KIO/Job>
0029 #include <KJobUiDelegate>
0030 
0031 // Local
0032 #include <lib/mimetypeutils.h>
0033 
0034 namespace Gwenview
0035 {
0036 struct DocumentDirFinderPrivate {
0037     QUrl mRootUrl;
0038     KDirLister *mDirLister = nullptr;
0039 
0040     QUrl mFoundDirUrl;
0041 };
0042 
0043 DocumentDirFinder::DocumentDirFinder(const QUrl &rootUrl)
0044     : d(new DocumentDirFinderPrivate)
0045 {
0046     d->mRootUrl = rootUrl;
0047     d->mDirLister = new KDirLister(this);
0048     connect(d->mDirLister, &KCoreDirLister::itemsAdded, this, &DocumentDirFinder::slotItemsAdded);
0049     connect(d->mDirLister, SIGNAL(completed()), SLOT(slotCompleted()));
0050     connect(d->mDirLister, &KCoreDirLister::jobError, this, [this](KIO::Job *job) {
0051         if (job->error() == KIO::Error::ERR_CANNOT_CREATE_WORKER) {
0052             Q_EMIT protocollNotSupportedError(job->errorText());
0053         } else {
0054             job->uiDelegate()->showErrorMessage();
0055         }
0056     });
0057     d->mDirLister->setAutoErrorHandlingEnabled(false);
0058     d->mDirLister->openUrl(rootUrl);
0059 }
0060 
0061 DocumentDirFinder::~DocumentDirFinder()
0062 {
0063     delete d;
0064 }
0065 
0066 void DocumentDirFinder::start()
0067 {
0068     d->mDirLister->openUrl(d->mRootUrl);
0069 }
0070 
0071 void DocumentDirFinder::slotItemsAdded(const QUrl &dir, const KFileItemList &list)
0072 {
0073     for (const KFileItem &item : list) {
0074         MimeTypeUtils::Kind kind = MimeTypeUtils::fileItemKind(item);
0075         switch (kind) {
0076         case MimeTypeUtils::KIND_DIR:
0077         case MimeTypeUtils::KIND_ARCHIVE:
0078             if (d->mFoundDirUrl.isValid()) {
0079                 // This is the second dir we find, stop now
0080                 finish(dir, MultipleDirsFound);
0081                 return;
0082             } else {
0083                 // First dir
0084                 d->mFoundDirUrl = item.url();
0085             }
0086             break;
0087 
0088         case MimeTypeUtils::KIND_RASTER_IMAGE:
0089         case MimeTypeUtils::KIND_SVG_IMAGE:
0090         case MimeTypeUtils::KIND_VIDEO:
0091             finish(dir, DocumentDirFound);
0092             return;
0093 
0094         case MimeTypeUtils::KIND_UNKNOWN:
0095         case MimeTypeUtils::KIND_FILE:
0096             break;
0097         }
0098     }
0099 }
0100 
0101 void DocumentDirFinder::slotCompleted()
0102 {
0103     if (d->mFoundDirUrl.isValid()) {
0104         const QUrl url = d->mFoundDirUrl;
0105         d->mFoundDirUrl.clear();
0106         d->mDirLister->openUrl(url);
0107     } else {
0108         finish(d->mRootUrl, NoDocumentFound);
0109     }
0110 }
0111 
0112 void DocumentDirFinder::finish(const QUrl &url, DocumentDirFinder::Status status)
0113 {
0114     disconnect(d->mDirLister, nullptr, this, nullptr);
0115     Q_EMIT done(url, status);
0116     deleteLater();
0117 }
0118 
0119 } // namespace
0120 
0121 #include "moc_documentdirfinder.cpp"