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;
}
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 <qtemporaryfile.h>
class KTemporaryFilePrivate;
#include <kurl.h>
/**
* \class KTemporaryFile ktemporaryfile.h <KTemporaryFile>
@ -139,6 +138,11 @@ public:
* @param pathtemplate The template to use when generating filepath.
*/
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

View file

@ -82,8 +82,8 @@ static QStringList tmpfiles;
static QString lastErrorMsg;
static int lastErrorCode = 0;
NetAccess::NetAccess() :
d( new NetAccessPrivate )
NetAccess::NetAccess()
: d(new NetAccessPrivate())
{
}
@ -92,7 +92,7 @@ NetAccess::~NetAccess()
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()) {
// file protocol, do not need the network
@ -106,15 +106,7 @@ bool NetAccess::download(const KUrl& u, QString & target, QWidget* window)
}
if (target.isEmpty()) {
// same bits as in kparts/part.cpp
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));
target = KTemporaryFile::urlPath(u);
tmpfiles.append(target);
}

View file

@ -183,7 +183,7 @@ public:
*
* @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.
@ -280,7 +280,7 @@ public:
* again be prompted for passwords as needed.
* @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.

View file

@ -471,16 +471,7 @@ void ReadOnlyPartPrivate::openRemoteFile()
{
Q_Q(ReadOnlyPart);
m_bTemp = true;
// Use same extension as remote file. This is important for mimetype-determination
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));
m_file = KTemporaryFile::urlPath(m_url);
KUrl destURL;
destURL.setPath(m_file);