kdecore: new KTemporaryFile::urlPath() method

based on the bits in kparts/part.cpp

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2024-05-13 22:52:30 +03:00
parent 75eeefb652
commit 7f261aca95
5 changed files with 27 additions and 26 deletions

View file

@ -90,3 +90,17 @@ QString KTemporaryFile::filePath(const QString &pathtemplate)
} }
return result; return result;
} }
QString KTemporaryFile::urlPath(const KUrl &url)
{
// use same extension as remote file, this is important for MIME type determination
QFileInfo fileinfo(url.fileName());
const QString filesuffix = fileinfo.completeSuffix();
QString urltemplate = QLatin1String("XXXXXXXXXX");
// but not if the extension is empty or the URL has a query
if (!filesuffix.isEmpty() && url.query().isEmpty()) {
urltemplate.append(QLatin1Char('.'));
urltemplate.append(filesuffix);
}
return KTemporaryFile::filePath(urltemplate);
}

View file

@ -24,8 +24,7 @@
#include <kglobal.h> #include <kglobal.h>
#include <qtemporaryfile.h> #include <qtemporaryfile.h>
#include <kurl.h>
class KTemporaryFilePrivate;
/** /**
* \class KTemporaryFile ktemporaryfile.h <KTemporaryFile> * \class KTemporaryFile ktemporaryfile.h <KTemporaryFile>
@ -139,6 +138,11 @@ public:
* @param pathtemplate The template to use when generating filepath. * @param pathtemplate The template to use when generating filepath.
*/ */
static QString filePath(const QString &pathtemplate = QString()); static QString filePath(const QString &pathtemplate = QString());
/**
* @brief Generates a filepath to be used as temporary file for the given URL.
*/
static QString urlPath(const KUrl &url);
}; };
#endif #endif

View file

@ -82,8 +82,8 @@ static QStringList tmpfiles;
static QString lastErrorMsg; static QString lastErrorMsg;
static int lastErrorCode = 0; static int lastErrorCode = 0;
NetAccess::NetAccess() : NetAccess::NetAccess()
d( new NetAccessPrivate ) : d(new NetAccessPrivate())
{ {
} }
@ -92,7 +92,7 @@ NetAccess::~NetAccess()
delete d; delete d;
} }
bool NetAccess::download(const KUrl& u, QString & target, QWidget* window) bool NetAccess::download(const KUrl &u, QString &target, QWidget *window)
{ {
if (u.isLocalFile()) { if (u.isLocalFile()) {
// file protocol, do not need the network // file protocol, do not need the network
@ -106,15 +106,7 @@ bool NetAccess::download(const KUrl& u, QString & target, QWidget* window)
} }
if (target.isEmpty()) { if (target.isEmpty()) {
// same bits as in kparts/part.cpp target = KTemporaryFile::urlPath(u);
QFileInfo fileInfo(u.fileName());
QString ext = fileInfo.completeSuffix();
QString extension;
if (!ext.isEmpty() && u.query().isEmpty()) {
// not if the URL has a query
extension = '.' + ext;
}
target = KTemporaryFile::filePath(QString::fromLatin1("XXXXXXXXXX%1").arg(extension));
tmpfiles.append(target); tmpfiles.append(target);
} }

View file

@ -183,7 +183,7 @@ public:
* *
* @return true if successful, false for failure * @return true if successful, false for failure
*/ */
static bool file_copy(const KUrl &src, const KUrl &target, QWidget* window = nullptr); static bool file_copy(const KUrl &src, const KUrl &target, QWidget *window = nullptr);
/** /**
* Alternative method for copying over the network. * Alternative method for copying over the network.
@ -280,7 +280,7 @@ public:
* again be prompted for passwords as needed. * again be prompted for passwords as needed.
* @return true on success, false on failure. * @return true on success, false on failure.
*/ */
static bool del(const KUrl &url, QWidget* window); static bool del(const KUrl &url, QWidget *window);
/** /**
* Creates a directory in a synchronous way. * Creates a directory in a synchronous way.

View file

@ -471,16 +471,7 @@ void ReadOnlyPartPrivate::openRemoteFile()
{ {
Q_Q(ReadOnlyPart); Q_Q(ReadOnlyPart);
m_bTemp = true; m_bTemp = true;
// Use same extension as remote file. This is important for mimetype-determination m_file = KTemporaryFile::urlPath(m_url);
QString fileName = m_url.fileName();
QFileInfo fileInfo(fileName);
QString ext = fileInfo.completeSuffix();
QString extension;
if (!ext.isEmpty() && m_url.query().isNull()) {
// not if the URL has a query, e.g. cgi.pl?something. keep the '.'
extension = '.' + ext;
}
m_file = KTemporaryFile::filePath(QString::fromLatin1("XXXXXXXXXX%1").arg(extension));
KUrl destURL; KUrl destURL;
destURL.setPath(m_file); destURL.setPath(m_file);