File indexing completed on 2024-04-28 04:18:53

0001 // vim: set tabstop=4 shiftwidth=4 expandtab:
0002 /*
0003 Gwenview: an image viewer
0004 Copyright 2008 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 "urlutils.h"
0023 
0024 // Qt
0025 #include <QApplication>
0026 #include <QFile>
0027 #include <QFileInfo>
0028 #include <QUrl>
0029 
0030 // KF
0031 #include <KIO/StatJob>
0032 #include <KJobWidgets>
0033 #include <KMountPoint>
0034 #include <KProtocolManager>
0035 
0036 // Local
0037 #include <archiveutils.h>
0038 #include <mimetypeutils.h>
0039 
0040 namespace Gwenview
0041 {
0042 namespace UrlUtils
0043 {
0044 bool urlIsFastLocalFile(const QUrl &url)
0045 {
0046     if (!url.isLocalFile()) {
0047         return false;
0048     }
0049 
0050     // Only query the mountpoint when we haven't queried it for this folder before
0051     static QString lastFolder;
0052     static KMountPoint::Ptr lastMountPoint;
0053 
0054     const QString fileName = url.toLocalFile();
0055     const QString currentFolder = fileName.left(fileName.lastIndexOf(QLatin1Char('/')));
0056 
0057     if (currentFolder != lastFolder) {
0058         lastFolder = currentFolder;
0059         KMountPoint::List list = KMountPoint::currentMountPoints();
0060         lastMountPoint = list.findByPath(url.toLocalFile());
0061     }
0062 
0063     if (!lastMountPoint) {
0064         // We couldn't find a mount point for the url. We are probably in a
0065         // chroot. Assume everything is fast then.
0066         return true;
0067     }
0068 
0069     return !lastMountPoint->probablySlow();
0070 }
0071 
0072 bool urlIsDirectory(const QUrl &url)
0073 {
0074     if (url.fileName().isEmpty()) {
0075         return true; // file:/somewhere/<nothing here>
0076     }
0077 
0078     // Do direct stat instead of using KIO if the file is local (faster)
0079     if (UrlUtils::urlIsFastLocalFile(url)) {
0080         return QFileInfo(url.toLocalFile()).isDir();
0081     }
0082 
0083     const QWidgetList list = QApplication::topLevelWidgets();
0084     QWidget *parent;
0085     if (!list.isEmpty()) {
0086         parent = list[0];
0087     } else {
0088         parent = nullptr;
0089     }
0090     KIO::StatJob *job = KIO::stat(url);
0091     KJobWidgets::setWindow(job, parent);
0092     if (job->exec()) {
0093         return job->statResult().isDir();
0094     }
0095     return false;
0096 }
0097 
0098 QUrl fixUserEnteredUrl(const QUrl &in)
0099 {
0100     if (!in.isRelative() && !in.isLocalFile()) {
0101         return in;
0102     }
0103 
0104     const QFileInfo info(in.toLocalFile());
0105     const QString path = info.absoluteFilePath();
0106 
0107     QUrl out = QUrl::fromLocalFile(path);
0108     const QString mimeType = MimeTypeUtils::urlMimeType(out);
0109 
0110     const QString protocol = ArchiveUtils::protocolForMimeType(mimeType);
0111 
0112     if (!protocol.isEmpty()) {
0113         QUrl tmp = out;
0114         tmp.setScheme(protocol);
0115         if (KProtocolManager::supportsListing(tmp)) {
0116             out = tmp;
0117         }
0118     }
0119     return out;
0120 }
0121 
0122 } // namespace
0123 
0124 } // namespace