From c46270b3a39252259ecadae28a37041716ef29e2 Mon Sep 17 00:00:00 2001 From: Michael Reeves Date: Sat, 13 Jan 2018 23:05:03 -0500 Subject: [PATCH] FIx local file handling with Qt5 --- src/CMakeLists.txt | 3 ++- src/QUrlFix.cpp | 19 +++++++++++++++++++ src/QUrlFix.h | 15 +++++++++++++++ src/fileaccess.cpp | 45 ++++++++++++++++++++++++++------------------- src/fileaccess.h | 4 ++-- 5 files changed, 64 insertions(+), 22 deletions(-) create mode 100644 src/QUrlFix.cpp create mode 100644 src/QUrlFix.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 900641d..54f7db4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -19,7 +19,8 @@ set(kdiff3part_PART_SRCS gnudiff_xmalloc.cpp common.cpp smalldialogs.cpp - progress.cpp ) + progress.cpp + QUrlFix.cpp ) add_library(kdiff3part MODULE ${kdiff3part_PART_SRCS}) diff --git a/src/QUrlFix.cpp b/src/QUrlFix.cpp new file mode 100644 index 0000000..dc58f73 --- /dev/null +++ b/src/QUrlFix.cpp @@ -0,0 +1,19 @@ +#include +#include "QUrlFix.h" + + QUrlFix::QUrlFix(const QString &url):QUrl(url, TolerantMode) + { + if(this->scheme().isEmpty()) this->setScheme("file");//so QUrl:isLocalFile: works as expected on QT5. + } + +#ifndef QT_NO_URL_CAST_FROM_STRING +QUrlFix& QUrlFix::operator =(const QString &url){ + setUrl(url); + return *this; +} +#endif +void QUrlFix::setUrl(const QString &url){ + QUrl::setUrl(url); + if(this->scheme().isEmpty()) this->setScheme("file");//so QUrl:isLocalFile: works as expected on QT5. +} + diff --git a/src/QUrlFix.h b/src/QUrlFix.h new file mode 100644 index 0000000..e1596d9 --- /dev/null +++ b/src/QUrlFix.h @@ -0,0 +1,15 @@ +#ifndef QURLFIX_H +#define QURLFIX_H + +#include + +class QUrlFix:public QUrl{ +public: + QUrlFix():QUrl(){};//make compiler shutup + QUrlFix(const QUrl ©):QUrl(copy){};//make compiler shutup + QUrlFix(const QString &url); + void setUrl(const QString &url); + QUrlFix& operator =(const QString &url); +}; + +#endif \ No newline at end of file diff --git a/src/fileaccess.cpp b/src/fileaccess.cpp index 95b71ab..df40a16 100644 --- a/src/fileaccess.cpp +++ b/src/fileaccess.cpp @@ -52,7 +52,7 @@ public: } void reset() { - m_url = QUrl(); + m_url = QUrlFix(); m_bValidData = false; m_name = QString(); //m_creationTime = QDateTime(); @@ -65,7 +65,7 @@ public: m_pParent = 0; } - QUrl m_url; + QUrlFix m_url; bool m_bLocal; bool m_bValidData; @@ -250,11 +250,11 @@ void FileAccess::setFile( const QFileInfo& fi, FileAccess* pParent ) } d()->m_bLocal = true; d()->m_bValidData = true; - d()->m_url = QUrl( fi.filePath() ); + d()->m_url = QUrlFix( fi.filePath() ); if ( d()->m_url.isRelative() ) { - d()->m_url = QUrl::fromUserInput(absoluteFilePath()); + d()->m_url = QUrlFix::fromUserInput(absoluteFilePath()); } if ( !m_bExists && absoluteFilePath().contains("@@") ) @@ -307,8 +307,9 @@ void FileAccess::setFile( const QString& name, bool bWantToWrite ) // (This is a Win95-bug which has been corrected only in WinNT/2000/XP.) if ( !name.isEmpty() ) { - QUrl url( name ); - + QUrlFix url( name ); + if(url.scheme().isEmpty()) url.setScheme("file");//so QUrlFix:isLocalFile: works as expected on QT5. + // FileAccess tries to detect if the given name is an URL or a local file. // This is a problem if the filename looks like an URL (i.e. contains a colon ':'). // e.g. "file:f.txt" is a valid filename. @@ -366,7 +367,7 @@ void FileAccess::addPath( const QString& txt ) { if ( d()!=0 && d()->m_url.isValid() ) { - QUrl url = d()->m_url.adjusted(QUrl::StripTrailingSlash); + QUrlFix url = d()->m_url.adjusted(QUrlFix::StripTrailingSlash); d()->m_url.setPath(url.path()+'/'+txt ); setFile( d()->m_url.url() ); // reinitialise } @@ -445,7 +446,7 @@ void FileAccess::setUdsEntry( const KIO::UDSEntry& e ) break; } - case KIO::UDSEntry::UDS_URL : // m_url = QUrl( e.stringValue(f) ); + case KIO::UDSEntry::UDS_URL : // m_url = QUrlFix( e.stringValue(f) ); break; case KIO::UDSEntry::UDS_MIME_TYPE : break; case KIO::UDSEntry::UDS_GUESSED_MIME_TYPE : break; @@ -511,16 +512,16 @@ qint64 FileAccess::size() const return QFileInfo( absoluteFilePath() ).size(); } -QUrl FileAccess::url() const +QUrlFix FileAccess::url() const { if ( d()!=0 ) return d()->m_url; else { - QUrl url( m_filePath ); + QUrlFix url( m_filePath ); if ( url.isRelative() ) { - url = QUrl::fromLocalFile( absoluteFilePath() ); + url = QUrlFix::fromLocalFile( absoluteFilePath() ); } return url; } @@ -913,7 +914,7 @@ void FileAccess::setStatusText( const QString& s ) QString FileAccess::cleanPath( const QString& path ) // static { - QUrl url(path); + QUrlFix url(path); if ( url.isLocalFile() || ! url.isValid() ) { return QDir().cleanPath( path ); @@ -1100,7 +1101,8 @@ void FileAccessJobHandler::slotPutJobResult(KJob* pJob) bool FileAccessJobHandler::mkDir( const QString& dirName ) { - QUrl dirURL = QUrl( dirName ); + QUrlFix dirURL = QUrlFix( dirName ); + if ( dirName.isEmpty() ) return false; else if ( dirURL.isLocalFile() || dirURL.isRelative() ) @@ -1120,7 +1122,8 @@ bool FileAccessJobHandler::mkDir( const QString& dirName ) bool FileAccessJobHandler::rmDir( const QString& dirName ) { - QUrl dirURL = QUrl( dirName ); + QUrlFix dirURL = QUrlFix( dirName ); + if ( dirName.isEmpty() ) return false; else if ( dirURL.isLocalFile() ) @@ -1174,9 +1177,11 @@ bool FileAccessJobHandler::rename( const QString& dest ) if ( dest.isEmpty() ) return false; - QUrl kurl( dest ); + QUrlFix kurl( dest ); + if(kurl.scheme().isEmpty()) kurl.setScheme("file");//so QUrlFix:isLocalFile: works as expected on QT5. + if ( kurl.isRelative() ) - kurl = QUrl( QDir().absoluteFilePath(dest) ); // assuming that invalid means relative + kurl = QUrlFix( QDir().absoluteFilePath(dest) ); // assuming that invalid means relative if ( m_pFileAccess->isLocal() && kurl.isLocalFile() ) { @@ -1215,9 +1220,11 @@ void FileAccessJobHandler::slotSimpleJobResult(KJob* pJob) bool FileAccessJobHandler::copyFile( const QString& dest ) { ProgressProxyExtender pp; - QUrl destUrl( dest ); + QUrlFix destUrl( dest ); + if(destUrl.scheme().isEmpty()) destUrl.setScheme("file");//so QUrlFix:isLocalFile: works as expected on QT5. + m_pFileAccess->setStatusText( QString() ); - if ( ! m_pFileAccess->isLocal() || ! destUrl.isLocalFile() ) // if either url is nonlocal + if ( ! m_pFileAccess->isLocal() || !destUrl.isLocalFile() ) // if either url is nonlocal { int permissions = (m_pFileAccess->isExecutable()?0111:0)+(m_pFileAccess->isWritable()?0222:0)+(m_pFileAccess->isReadable()?0444:0); m_bSuccess = false; @@ -1755,7 +1762,7 @@ bool FileAccessJobHandler::listDir( t_DirectoryList* pDirList, bool bRecursive, void FileAccessJobHandler::slotListDirProcessNewEntries( KIO::Job*, const KIO::UDSEntryList& l ) { - QUrl parentUrl( QUrl::fromUserInput(m_pFileAccess->absoluteFilePath()) ); + QUrlFix parentUrl( QUrlFix::fromUserInput(m_pFileAccess->absoluteFilePath()) ); KIO::UDSEntryList::ConstIterator i; for ( i=l.begin(); i!=l.end(); ++i ) diff --git a/src/fileaccess.h b/src/fileaccess.h index 2a6aaa5..17f8e45 100644 --- a/src/fileaccess.h +++ b/src/fileaccess.h @@ -17,7 +17,7 @@ #include #include -#include +#include "QUrlFix.h" #include @@ -66,7 +66,7 @@ public: QString fileName() const; // Just the name-part of the path, without parent directories QString filePath() const; // The path-string that was used during construction QString prettyAbsPath() const; - QUrl url() const; + QUrlFix url() const; QString absoluteFilePath() const; bool isLocal() const; -- GitLab