krenamefile.cpp (krename-4.0.9) | : | krenamefile.cpp (krename-4.90.90) | ||
---|---|---|---|---|
skipping to change at line 20 | skipping to change at line 20 | |||
* * | * * | |||
* This program is free software; you can redistribute it and/or modify * | * This program is free software; you can redistribute it and/or modify * | |||
* it under the terms of the GNU General Public License as published by * | * it under the terms of the GNU General Public License as published by * | |||
* the Free Software Foundation; either version 2 of the License, or * | * the Free Software Foundation; either version 2 of the License, or * | |||
* (at your option) any later version. * | * (at your option) any later version. * | |||
* * | * * | |||
***************************************************************************/ | ***************************************************************************/ | |||
#include "krenamefile.h" | #include "krenamefile.h" | |||
#include <kio/netaccess.h> | ||||
#include <kio/previewjob.h> | #include <kio/previewjob.h> | |||
#include <KJobWidgets> | ||||
/** A singleton class that loads icons for urls in a synchronous way | /** A singleton class that loads icons for urls in a synchronous way | |||
*/ | */ | |||
class KRenamePreviewProvider { | class KRenamePreviewProvider | |||
{ | ||||
public: | public: | |||
/** Get the KRenamePreviewProvider instance. This is a singleton | /** Get the KRenamePreviewProvider instance. This is a singleton | |||
* as only one KRenamePreviewProvider may exist for one application. | * as only one KRenamePreviewProvider may exist for one application. | |||
*/ | */ | |||
static KRenamePreviewProvider* Instance() | static KRenamePreviewProvider *Instance() | |||
{ | { | |||
if( !s_instance ) | if (!s_instance) { | |||
s_instance = new KRenamePreviewProvider(); | s_instance = new KRenamePreviewProvider(); | |||
} | ||||
return s_instance; | return s_instance; | |||
} | } | |||
QPixmap loadIcon( const KUrl & url ) | QPixmap loadIcon(const QUrl &url) | |||
{ | { | |||
return KIO::pixmapForUrl( url ); | return KIO::pixmapForUrl(url); | |||
} | } | |||
private: | private: | |||
/** Create a KRenamePreviewProvider | /** Create a KRenamePreviewProvider | |||
*/ | */ | |||
KRenamePreviewProvider() | KRenamePreviewProvider() | |||
{ | { | |||
} | } | |||
~KRenamePreviewProvider() | ~KRenamePreviewProvider() | |||
{ | { | |||
} | } | |||
private: | private: | |||
static KRenamePreviewProvider* s_instance; | static KRenamePreviewProvider *s_instance; | |||
}; | }; | |||
KRenamePreviewProvider* KRenamePreviewProvider::s_instance = NULL; | KRenamePreviewProvider *KRenamePreviewProvider::s_instance = nullptr; | |||
const int KRenameFile::DEFAULT_ICON_SIZE = 64; | const int KRenameFile::DEFAULT_ICON_SIZE = 64; | |||
const char* KRenameFile::EXTRA_DATA_KEY = "KRenameFile::EXTRA_DATA_KEY"; | int KRenameFile::m_iconSize = 0; // read from config file | |||
const char *KRenameFile::EXTRA_DATA_KEY = "KRenameFile::EXTRA_DATA_KEY"; | ||||
KRenameFile::KRenameFile( const KUrl & src, ESplitMode eSplitMode, unsigned int | KRenameFile::KRenameFile(const QUrl &src, ESplitMode eSplitMode, unsigned int do | |||
dot ) | t) | |||
: m_bValid( false ), m_error( 0 ), m_manualMode( eManualChangeMode_None ) | : m_bValid(false), m_error(0), m_manualMode(eManualChangeMode_None) | |||
{ | { | |||
KIO::UDSEntry entry; | KIO::StatJob *statJob = KIO::stat(src, KIO::StatJob::DestinationSide, 2); | |||
KIO::NetAccess::stat( src, entry, NULL ); | statJob->exec(); | |||
KFileItem file( entry, src ); | if (statJob->error()) { | |||
return; | ||||
} | ||||
KFileItem file(statJob->statResult(), src); | ||||
m_bValid = file.isReadable(); | m_bValid = file.isReadable(); | |||
m_bDirectory = file.isDir(); | m_bDirectory = file.isDir(); | |||
m_fileItem = file; | m_fileItem = file; | |||
initFileDescription( m_src, src, eSplitMode, dot ); | initFileDescription(m_src, src, eSplitMode, dot); | |||
} | } | |||
KRenameFile::KRenameFile( const KUrl & src, bool directory, ESplitMode eSplitMod | KRenameFile::KRenameFile(const QUrl &src, bool directory, ESplitMode eSplitMode, | |||
e, unsigned int dot ) | unsigned int dot) | |||
: m_bDirectory( directory ), m_bValid( true ), m_error( 0 ), m_manualMode( e | : m_bDirectory(directory), m_bValid(true), m_error(0), m_manualMode(eManualC | |||
ManualChangeMode_None ) | hangeMode_None) | |||
{ | { | |||
initFileDescription( m_src, src, eSplitMode, dot ); | initFileDescription(m_src, src, eSplitMode, dot); | |||
} | } | |||
KRenameFile::KRenameFile( const KFileItem& item, ESplitMode eSplitMode, unsigned | KRenameFile::KRenameFile(const KFileItem &item, ESplitMode eSplitMode, unsigned | |||
int dot ) | int dot) | |||
: m_bDirectory( item.isDir() ), m_bValid( item.isReadable() ), m_error( 0 ), | : m_bDirectory(item.isDir()), m_bValid(item.isReadable()), m_error(0), m_man | |||
m_manualMode( eManualChangeMode_None ) | ualMode(eManualChangeMode_None) | |||
{ | { | |||
m_fileItem = item; | m_fileItem = item; | |||
initFileDescription( m_src, item.url(), eSplitMode, dot ); | initFileDescription(m_src, item.url(), eSplitMode, dot); | |||
} | } | |||
KRenameFile::KRenameFile( const KRenameFile & rhs ) | KRenameFile::KRenameFile(const KRenameFile &rhs) | |||
{ | { | |||
this->operator=( rhs ); | this->operator=(rhs); | |||
} | } | |||
const KRenameFile & KRenameFile::operator=( const KRenameFile & rhs ) | const KRenameFile &KRenameFile::operator=(const KRenameFile &rhs) | |||
{ | { | |||
m_bDirectory = rhs.m_bDirectory; | m_bDirectory = rhs.m_bDirectory; | |||
m_src = rhs.m_src; | m_src = rhs.m_src; | |||
m_dst = rhs.m_dst; | m_dst = rhs.m_dst; | |||
m_bValid = rhs.m_bValid; | m_bValid = rhs.m_bValid; | |||
m_icon = rhs.m_icon; | m_icon = rhs.m_icon; | |||
m_error = rhs.m_error; | m_error = rhs.m_error; | |||
m_manual = rhs.m_manual; | m_manual = rhs.m_manual; | |||
m_manualMode = rhs.m_manualMode; | m_manualMode = rhs.m_manualMode; | |||
return *this; | return *this; | |||
} | } | |||
bool KRenameFile::operator==( const KFileItem & item ) const | bool KRenameFile::operator==(const KFileItem &item) const | |||
{ | { | |||
return this->fileItem() == item; | return this->fileItem() == item; | |||
} | } | |||
void KRenameFile::setIconSize(int size) | ||||
{ | ||||
m_iconSize = size; | ||||
} | ||||
int KRenameFile::iconSize() | ||||
{ | ||||
return m_iconSize; | ||||
} | ||||
int KRenameFile::getDefaultIconSize() | int KRenameFile::getDefaultIconSize() | |||
{ | { | |||
return DEFAULT_ICON_SIZE; | return DEFAULT_ICON_SIZE; | |||
} | } | |||
void KRenameFile::setCurrentSplitMode( ESplitMode eSplitMode, unsigned int dot ) | void KRenameFile::setCurrentSplitMode(ESplitMode eSplitMode, unsigned int dot) | |||
{ | { | |||
KUrl url = m_src.url; | QUrl url = m_src.url; | |||
QString filename = m_src.filename; | QString filename = m_src.filename; | |||
if( !m_src.extension.isEmpty() ) | if (!m_src.extension.isEmpty()) { | |||
{ | filename = filename + '.' + m_src.extension; | |||
filename = filename + "." + m_src.extension; | ||||
} | } | |||
url.setDirectory( m_src.directory ); | url.setPath(m_src.directory + '/' + filename); | |||
url.addPath( filename ); | ||||
this->initFileDescription( m_src, url, eSplitMode, dot ); | this->initFileDescription(m_src, url, eSplitMode, dot); | |||
} | } | |||
void KRenameFile::initFileDescription( TFileDescription & rDescription, const KU | void KRenameFile::initFileDescription(TFileDescription &rDescription, const QUrl | |||
rl & url, | &url, | |||
ESplitMode eSplitMode, unsigned int dot ) | ESplitMode eSplitMode, unsigned int dot) c | |||
const | onst | |||
{ | { | |||
int splitPos = -1; | int splitPos = -1; | |||
QString path = url.path(); | QString path = url.path(); | |||
QString file; | QString file; | |||
if( !m_bValid ) | if (!m_bValid) { | |||
return; | return; | |||
} | ||||
rDescription.url = url; | rDescription.url = url; | |||
rDescription.directory = path; | rDescription.directory = path; | |||
if( !m_bDirectory ) | if (!m_bDirectory) { | |||
{ | ||||
// split directory/filename | // split directory/filename | |||
splitPos = path.lastIndexOf( '/' ); | splitPos = path.lastIndexOf('/'); | |||
if( splitPos == -1 ) | if (splitPos == -1) { | |||
{ | ||||
// only a filename?? can this happen? | // only a filename?? can this happen? | |||
file = path; | file = path; | |||
path = QString::null; | path.clear(); | |||
} | } else { | |||
else | file = path.right(path.length() - splitPos - 1); | |||
{ | path = path.left(splitPos); | |||
file = path.right( path.length() - splitPos - 1 ); | ||||
path = path.left( splitPos ); | ||||
} | } | |||
// split filename.extension | // split filename.extension | |||
splitPos = -1; | splitPos = -1; | |||
if( eSplitMode == eSplitMode_FirstDot ) | if (eSplitMode == eSplitMode_FirstDot) { | |||
splitPos = file.indexOf( '.' ); | splitPos = file.indexOf('.'); | |||
else if( eSplitMode == eSplitMode_LastDot ) | } else if (eSplitMode == eSplitMode_LastDot) { | |||
splitPos = file.lastIndexOf( '.' ); | splitPos = file.lastIndexOf('.'); | |||
else if( eSplitMode == eSplitMode_NoExtension ) | } else if (eSplitMode == eSplitMode_NoExtension) { | |||
splitPos = file.length(); | splitPos = file.length(); | |||
else | } else { | |||
{ | if (dot) { | |||
if( dot ) | ||||
{ | ||||
int i = 0; | int i = 0; | |||
splitPos = 0; | splitPos = 0; | |||
do { | do { | |||
splitPos = file.indexOf( '.', splitPos + 1 ); | splitPos = file.indexOf('.', splitPos + 1); | |||
++i; | ++i; | |||
} while( i < static_cast<int>(dot) && splitPos != -1 ); | } while (i < static_cast<int>(dot) && splitPos != -1); | |||
} | } else | |||
else | ||||
// if dot == 0, do not take an extension | // if dot == 0, do not take an extension | |||
{ | ||||
splitPos = file.length(); | splitPos = file.length(); | |||
} | ||||
} | } | |||
if( splitPos == -1 ) | if (splitPos == -1) { | |||
splitPos = file.length(); | splitPos = file.length(); | |||
} | ||||
rDescription.filename = file.left( splitPos ); | rDescription.filename = file.left(splitPos); | |||
rDescription.extension = ""; | rDescription.extension = ""; | |||
if( splitPos != file.length() ) | if (splitPos != file.length()) { | |||
rDescription.extension = file.right( file.length() - splitPos - 1 ); | rDescription.extension = file.right(file.length() - splitPos - 1); | |||
} | ||||
rDescription.directory = path; | rDescription.directory = path; | |||
} | } else { | |||
else | if (rDescription.directory.endsWith('/')) { | |||
{ | rDescription.directory = rDescription.directory.left(rDescription.di | |||
if( rDescription.directory.endsWith( '/' ) ) | rectory.length() - 1); | |||
rDescription.directory = rDescription.directory.left( rDescription.d | } | |||
irectory.length() - 1 ); | ||||
int lastSlash = rDescription.directory.lastIndexOf('/'); | int lastSlash = rDescription.directory.lastIndexOf('/'); | |||
rDescription.filename = rDescription.directory.right( rDescription.dire | rDescription.filename = rDescription.directory.right(rDescription.direc | |||
ctory.length() - lastSlash - 1 ); | tory.length() - lastSlash - 1); | |||
rDescription.directory = rDescription.directory.left( lastSlash ); | rDescription.directory = rDescription.directory.left(lastSlash); | |||
rDescription.extension = ""; | rDescription.extension = ""; | |||
} | } | |||
/* | /* | |||
qDebug("URL : %s", url.prettyUrl().toLatin1().data() ); | qDebug("URL : %s", url.toDisplayString().toLatin1().data() ); | |||
qDebug("Path: %s", rDescription.directory.toLatin1().data()); | qDebug("Path: %s", rDescription.directory.toLatin1().data()); | |||
qDebug("File: %s", rDescription.filename.toLatin1().data()); | qDebug("File: %s", rDescription.filename.toLatin1().data()); | |||
qDebug("Ext : %s", rDescription.extension.toLatin1().data()); | qDebug("Ext : %s", rDescription.extension.toLatin1().data()); | |||
qDebug("Split %i", splitPos ); | qDebug("Split %i", splitPos ); | |||
qDebug("Dot %i", dot ); | qDebug("Dot %i", dot ); | |||
qDebug("====="); | qDebug("====="); | |||
*/ | */ | |||
} | } | |||
void KRenameFile::loadPreviewIcon() | void KRenameFile::loadPreviewIcon() | |||
{ | { | |||
m_icon = KRenamePreviewProvider::Instance()->loadIcon( m_src.url ); | m_icon = KRenamePreviewProvider::Instance()->loadIcon(m_src.url); | |||
} | } | |||
int KRenameFile::dots() const | int KRenameFile::dots() const | |||
{ | { | |||
int dots = 0; | int dots = 0; | |||
dots += m_src.filename.count( '.' ); | dots += m_src.filename.count('.'); | |||
dots += m_src.extension.count( '.' ); | dots += m_src.extension.count('.'); | |||
if( !m_src.extension.isEmpty() ) | if (!m_src.extension.isEmpty()) { | |||
++dots; | ++dots; | |||
} | ||||
return dots; | return dots; | |||
} | } | |||
const KUrl KRenameFile::srcUrl() const | const QUrl KRenameFile::srcUrl() const | |||
{ | { | |||
if( m_overrideDir.isNull() ) | if (m_overrideDir.isNull()) { | |||
return m_src.url; | return m_src.url; | |||
else | } else { | |||
{ | QUrl changed = m_src.url; | |||
KUrl changed = m_src.url; | QString filename = m_src.filename; | |||
changed.setDirectory( m_overrideDir ); | if (!m_src.extension.isEmpty()) { | |||
QString filename = m_src.filename; | filename += '.'; | |||
if( !m_src.extension.isEmpty() ) | filename += m_src.extension; | |||
{ | } | |||
filename += '.'; | ||||
filename += m_src.extension; | ||||
} | ||||
changed.setFileName( filename ); | changed.setPath(m_overrideDir + '/' + filename); | |||
return changed; | return changed; | |||
} | } | |||
} | } | |||
const KFileItem & KRenameFile::fileItem() const | const KFileItem &KRenameFile::fileItem() const | |||
{ | { | |||
if( m_fileItem.isNull() ) | if (m_fileItem.isNull()) { | |||
{ | ||||
// No file item has been constructed | // No file item has been constructed | |||
// create one first. | // create one first. | |||
KIO::UDSEntry entry; | KIO::StatJob *statJob = KIO::stat(m_src.url, KIO::StatJob::DestinationSi | |||
KIO::NetAccess::stat( m_src.url, entry, NULL ); | de, 2); | |||
KFileItem file( entry, m_src.url ); | statJob->exec(); | |||
if (!statJob->error()) { | ||||
const_cast<KRenameFile*>(this)->m_fileItem = file; | KFileItem file(statJob->statResult(), m_src.url); | |||
const_cast<KRenameFile *>(this)->m_fileItem = file; | ||||
} | ||||
} | } | |||
/* | /* | |||
// Update extra as often as possible | // Update extra as often as possible | |||
// As the address is changed through sorting and moving files | // As the address is changed through sorting and moving files | |||
// It is only valid if no moving of data has happened! | // It is only valid if no moving of data has happened! | |||
const_cast<KRenameFile*>(this)->m_fileItem.setExtraData( KRenameFile::EXTRA_ DATA_KEY, | const_cast<KRenameFile*>(this)->m_fileItem.setExtraData( KRenameFile::EXTRA_ DATA_KEY, | |||
const_cast<KRenameF ile*>(this) ); | const_cast<KRenameF ile*>(this) ); | |||
*/ | */ | |||
return m_fileItem; | return m_fileItem; | |||
End of changes. 57 change blocks. | ||||
108 lines changed or deleted | 119 lines changed or added |