"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "src/batchrenamer.cpp" between
krename-4.0.9.tar.gz and krename-4.90.90.tar.gz

About: KRename is a batch file-renamer (KDE).

batchrenamer.cpp  (krename-4.0.9):batchrenamer.cpp  (krename-4.90.90)
skipping to change at line 18 skipping to change at line 18
/*************************************************************************** /***************************************************************************
* * * *
* 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. *
* * * *
***************************************************************************/ ***************************************************************************/
#ifdef HAVE_CONFIG_H #include "batchrenamer.h"
#include <config.h>
#endif
#ifdef _WIN32 #ifdef Q_OS_WIN
#include <windows.h> #include <windows.h>
#endif // _WIN32 #endif
// OS includes // OS includes
#include <stdio.h> #include <stdio.h>
#ifndef _WIN32 #ifndef Q_OS_WIN
#include <unistd.h> #include <unistd.h>
#endif // _WIN32 #endif
#include "../config-krename.h" #include "../config-krename.h"
// chmod: // chmod:
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
// QT includes // QT includes
#include <QTextStream> #include <QTextStream>
// KDE includes // KDE includes
#include <kapplication.h>
#include <kio/job.h> #include <kio/job.h>
#include <kio/netaccess.h> #include <KIO/MkpathJob>
#include <klocale.h> #include <KJobWidgets>
// Own includes // Own includes
#include "batchrenamer.h"
#include "progressdialog.h" #include "progressdialog.h"
#include "pluginloader.h" #include "pluginloader.h"
#include "plugin.h" #include "plugin.h"
using namespace KIO; using namespace KIO;
static bool isToken( const QChar & token ) static bool isToken(const QChar &token)
{ {
const QChar tokens[] = { const QChar tokens[] = {
QChar('&'), QChar('&'),
QChar('$'), QChar('$'),
QChar('%'), QChar('%'),
QChar('#'), QChar('#'),
QChar('['), QChar('['),
QChar(']'), QChar(']'),
QChar('\\'), QChar('\\'),
QChar('/'), QChar('/'),
QChar('{'), QChar('{'),
QChar('}'), QChar('}'),
QChar('*') QChar('*')
}; };
const int count = 11; const int count = 11;
for( int i=0;i<count;i++ ) for (int i = 0; i < count; i++)
if( token == tokens[i] ) if (token == tokens[i]) {
return true; return true;
}
return false; return false;
} }
static int getNextToken( const QString & text, QString & token, int pos = 0 ) static int getNextToken(const QString &text, QString &token, int pos = 0)
{ {
bool escaped = false; bool escaped = false;
token = QString::null; token.clear();
if( pos < 0 ) if (pos < 0) {
return -1; return -1;
}
while( pos < text.length() ) while (pos < text.length()) {
{ if (!escaped && text[pos] == QChar('\\')) {
if( !escaped && text[pos] == QChar('\\') )
{
escaped = true; escaped = true;
} } else if (!escaped && isToken(text[pos])) {
else if( !escaped && isToken( text[pos] ) )
{
token = text[pos]; token = text[pos];
return ++pos; return ++pos;
} } else {
else
{
escaped = false; escaped = false;
} }
++pos; ++pos;
} }
return -1; return -1;
} }
BatchRenamer::BatchRenamer() BatchRenamer::BatchRenamer()
: m_index( 0 ), m_step( 1 ), m_files( NULL ), m_renameMode( eRenameMode_Rena me ) : m_index(0), m_step(1), m_files(nullptr), m_renameMode(eRenameMode_Rename)
{ {
m_counter_index = 0; m_counter_index = 0;
m_reset = false; m_reset = false;
m_overwrite = false; m_overwrite = false;
} }
BatchRenamer::~BatchRenamer() BatchRenamer::~BatchRenamer()
{ {
} }
void BatchRenamer::processFilenames() void BatchRenamer::processFilenames()
{ {
m_counters.clear(); m_counters.clear();
for( unsigned int i = 0; i < static_cast<unsigned int>(m_files->count()); i+ for (unsigned int i = 0; i < static_cast<unsigned int>(m_files->count()); i+
+) +) {
{
m_counter_index = 0; m_counter_index = 0;
if( m_renameMode == eRenameMode_Rename ) // final Path = source Path ) {
{ if (m_renameMode == eRenameMode_Rename) { // final Path = source Path
(*m_files)[i].setDstDirectory( (*m_files)[i].srcDirectory() ); (*m_files)[i].setDstDirectory((*m_files)[i].srcDirectory());
KUrl url = (*m_files)[i].srcUrl(); QUrl url = (*m_files)[i].srcUrl();
url.setFileName( QString::null ); url = url.adjusted(QUrl::RemoveFilename);
(*m_files)[i].setDstUrl( url ); (*m_files)[i].setDstUrl(url);
} } else {
else (*m_files)[i].setDstUrl(m_destination);
{ (*m_files)[i].setDstDirectory(m_destination.path());
(*m_files)[i].setDstUrl( m_destination );
(*m_files)[i].setDstDirectory( m_destination.path() );
} }
if( i > 0 && m_reset ) if (i > 0 && m_reset) {
findCounterReset( i ); findCounterReset(i);
}
//qDebug("SRCFILENAME : %s", (*m_files)[i].srcFilename().toUtf8(). data() ); //qDebug("SRCFILENAME : %s", (*m_files)[i].srcFilename().toUtf8(). data() );
//qDebug("DSTFILENAME SHOULD: %s", processString( text, (*m_files)[i].sr cFilename(), i ).toUtf8().data() ); //qDebug("DSTFILENAME SHOULD: %s", processString( text, (*m_files)[i].sr cFilename(), i ).toUtf8().data() );
(*m_files)[i].setDstFilename( processString( text, (*m_files)[i].srcFile name(), i ) ); (*m_files)[i].setDstFilename(processString(text, (*m_files)[i].srcFilena me(), i));
//qDebug("DSTFILENAME IS : %s", (*m_files)[i].dstFilename().toUtf8(). data()); //qDebug("DSTFILENAME IS : %s", (*m_files)[i].dstFilename().toUtf8(). data());
(*m_files)[i].setDstExtension( processString( extext, (*m_files)[i].srcE xtension(), i ) ); (*m_files)[i].setDstExtension(processString(extext, (*m_files)[i].srcExt ension(), i));
// Let's run the plugins that change the final filename, // Let's run the plugins that change the final filename,
// i.e the encodingsplugin // i.e the encodingsplugin
int errors = 0; int errors = 0;
QString name = executePlugin( i, (*m_files)[i].dstFilename(), ePluginTyp QString name = executePlugin(i, (*m_files)[i].dstFilename(), ePluginType
e_Filename, errors, NULL ); _Filename, errors, nullptr);
if( !name.isNull() ) if (!name.isNull()) {
(*m_files)[i].setDstFilename( name ); (*m_files)[i].setDstFilename(name);
}
/* /*
* take care of renamed directories and * take care of renamed directories and
* correct the paths of their contents * correct the paths of their contents
*/ */
if( (m_renameMode == eRenameMode_Rename || if ((m_renameMode == eRenameMode_Rename ||
m_renameMode == eRenameMode_Move) && m_renameMode == eRenameMode_Move) &&
(*m_files)[i].isDirectory() ) (*m_files)[i].isDirectory()) {
{
const QString topDir = (*m_files)[i].realSrcDirectory() + '/' + (*m _files)[i].srcFilename(); const QString topDir = (*m_files)[i].realSrcDirectory() + '/' + (*m _files)[i].srcFilename();
const QString replace = (*m_files)[i].dstDirectory() + '/' + (*m_fil es)[i].dstFilename(); const QString replace = (*m_files)[i].dstDirectory() + '/' + (*m_fil es)[i].dstFilename();
for( int z = i + 1; z < m_files->count(); z++ ) for (int z = i + 1; z < m_files->count(); z++) {
{ const QString &dir = (*m_files)[z].realSrcDirectory();
const QString & dir = (*m_files)[z].realSrcDirectory(); if (dir.startsWith(topDir)) {
if( dir.startsWith( topDir ) ) QString newDir = replace + dir.right(dir.length() - topDir.l
{ ength());
QString newDir = replace + dir.right( dir.length() - topDir. if (newDir != dir) {
length() ); (*m_files)[z].setOverrideSrcDirectory(newDir);
if( newDir != dir ) {
(*m_files)[z].setOverrideSrcDirectory( newDir );
} }
} }
} }
} }
#if 0 #if 0
if( m_files[i].dir && (m_mode == RENAME || m_mode == MOVE) ) { if (m_files[i].dir && (m_mode == RENAME || m_mode == MOVE)) {
for( unsigned int c = i; c < m_files.count(); c++ ) { for (unsigned int c = i; c < m_files.count(); c++) {
if( m_files[c].src.directory.left( m_files[i].src.name.length() if (m_files[c].src.directory.left(m_files[i].src.name.length() +
+ 1 ) 1)
== ( m_files[i].src.name + "/" ) ) { == (m_files[i].src.name + "/")) {
m_files[c].src.directory.replace( 0, m_files[i].src.name.len m_files[c].src.directory.replace(0, m_files[i].src.name.leng
gth(), m_files[i].dst.name ); th(), m_files[i].dst.name);
m_files[c].src.url.setPath( BatchRenamer::buildFilename( &m_ m_files[c].src.url.setPath(BatchRenamer::buildFilename(&m_fi
files[c].src, true ) ); les[c].src, true));
} }
} }
} }
#endif // 0 #endif // 0
} }
} }
void BatchRenamer::processFiles( ProgressDialog* p ) void BatchRenamer::processFiles(ProgressDialog *p)
{ {
int errors = 0; int errors = 0;
KUrl dest = (*m_files)[0].dstUrl(); QUrl dest = (*m_files)[0].dstUrl();
// TODO: error handling if dest is empty // TODO: error handling if dest is empty
// Give the user some information... // Give the user some information...
p->setProgressTotalSteps( m_files->count() ); p->setProgressTotalSteps(m_files->count());
p->setProgress( 0 ); p->setProgress(0);
p->setDestination( dest ); p->setDestination(dest);
switch( m_renameMode ) switch (m_renameMode) {
{ default:
default: case eRenameMode_Rename:
case eRenameMode_Rename: p->print(i18n("Input files will be renamed."));
p->print( i18n("Input files will be renamed.") ); break;
break; case eRenameMode_Copy:
case eRenameMode_Copy: p->print(i18n("Files will be copied to: %1", dest.toDisplayString(QUrl::
p->print( i18n("Files will be copied to: %1", dest.prettyUrl() ) ); PreferLocalFile)));
break; break;
case eRenameMode_Move: case eRenameMode_Move:
p->print( i18n("Files will be moved to: %1", dest.prettyUrl() ) ); p->print(i18n("Files will be moved to: %1", dest.toDisplayString(QUrl::P
break; referLocalFile)));
case eRenameMode_Link: break;
p->print( i18n("Symbolic links will be created in: %1", dest.prettyU case eRenameMode_Link:
rl() ) ); p->print(i18n("Symbolic links will be created in: %1", dest.toDisplayStr
break; ing(QUrl::PreferLocalFile)));
break;
} }
for( unsigned int i = 0; i < static_cast<unsigned int>(m_files->count()); i+ for (unsigned int i = 0; i < static_cast<unsigned int>(m_files->count()); i+
+) +) {
{ QUrl dstUrl = this->buildDestinationUrl((*m_files)[i]);
KUrl dstUrl = this->buildDestinationUrl( (*m_files)[i] );
//p->print( QString( "%1 -> %2" ).arg( (*m_files)[i].srcUrl().prettyUrl( //p->print( QString( "%1 -> %2" ).arg( (*m_files)[i].srcUrl().prettyUrl(
) ).arg( dstUrl.prettyUrl() ) ); ) ).arg( dstUrl.toDisplayString() ) );
p->setProgress( i + 1 ); p->setProgress(i + 1);
if( p->wasCancelled() ) if (p->wasCancelled()) {
break; break;
}
// Assemble filenames // Assemble filenames
createMissingSubDirs( (*m_files)[i], p ); createMissingSubDirs((*m_files)[i], p);
KIO::JobFlags flags = (m_overwrite ? KIO::Overwrite : KIO::DefaultFlags ) | KIO::HideProgressInfo; KIO::JobFlags flags = (m_overwrite ? KIO::Overwrite : KIO::DefaultFlags ) | KIO::HideProgressInfo;
KIO::Job* job = NULL; KIO::Job *job = nullptr;
const KUrl & srcUrl = (*m_files)[i].srcUrl(); const QUrl &srcUrl = (*m_files)[i].srcUrl();
if( srcUrl == dstUrl ) if (srcUrl == dstUrl) {
{ p->warning(i18n("Cannot rename: source and target filename are equal
p->warning( i18n("Cannot rename: source and target filename are equa : %1", srcUrl.toDisplayString(QUrl::PreferLocalFile)));
l: %1", srcUrl.prettyUrl()) );
//(*m_files)[i].setError( 1 ); //(*m_files)[i].setError( 1 );
//errors++; //errors++;
continue; continue;
} }
switch( m_renameMode ) switch (m_renameMode) {
{ default:
default: case eRenameMode_Rename:
case eRenameMode_Rename: case eRenameMode_Move:
case eRenameMode_Move: job = KIO::file_move(srcUrl, dstUrl, -1, flags);
job = KIO::file_move( srcUrl, dstUrl, -1, flags ); break;
break; case eRenameMode_Copy:
case eRenameMode_Copy: job = KIO::file_copy(srcUrl, dstUrl, -1, flags);
job = KIO::file_copy( srcUrl, dstUrl, -1, flags ); break;
break; case eRenameMode_Link: {
case eRenameMode_Link: if (!srcUrl.isLocalFile()) {
{ // We can only do symlinks to local urls
if( !srcUrl.isLocalFile() ) p->error(i18n("Cannot create symlink to non-local URL: %1", srcU
{ rl.toDisplayString()));
// We can only do symlinks to local urls (*m_files)[i].setError(1);
p->error( i18n("Cannot create symlink to non-local URL: %1", errors++;
srcUrl.prettyUrl()) ); } else {
(*m_files)[i].setError( 1 ); job = KIO::symlink(srcUrl.path(), dstUrl, flags);
errors++;
}
else
job = KIO::symlink( srcUrl.path(), dstUrl, flags );
break;
} }
break;
}
} }
if( job && !NetAccess::synchronousRun( job, p ) ) if (job) {
{ KJobWidgets::setWindow(job, p);
p->error( i18n("Error renaming %2 (to %1)", dstUrl.prettyUrl(), srcU if (!job->exec()) {
rl.prettyUrl()) ); p->error(i18n("Error renaming %2 (to %1)",
(*m_files)[i].setError( 1 ); dstUrl.toDisplayString(QUrl::PreferLocalFile),
errors++; srcUrl.toDisplayString(QUrl::PreferLocalFile)));
(*m_files)[i].setError(job->error());
errors++;
}
} }
/* /*
* The renamed file should be on its correct location now, * The renamed file should be on its correct location now,
* so that we can call the last plugins (e.g. for changing permissions) * so that we can call the last plugins (e.g. for changing permissions)
* *
* Remember, the token argument is the filename for this type of plugins ! * Remember, the token argument is the filename for this type of plugins !
* *
* If the return value is not empty an error has occured! * If the return value is not empty an error has occurred!
* The plugin should return an error message in this case! * The plugin should return an error message in this case!
*/ */
int errorCount = 0; int errorCount = 0;
this->executePlugin( i, dstUrl.path(), ePluginType_File, errorCount, p ) ; this->executePlugin(i, dstUrl.path(), ePluginType_File, errorCount, p);
errors += errorCount; errors += errorCount;
} }
if( errors > 0 ) if (errors > 0) {
p->warning( i18n("%1 errors occurred!", errors ) ); p->warning(i18np("%1 error occurred.", "%1 errors occurred.", errors));
}
p->print( i18n("KRename finished the renaming process."), "krename" ); p->print(i18n("KRename finished the renaming process."), "krename");
p->print( i18n("Press close to quit!") ); p->print(i18n("Press close to quit."));
bool enableUndo = (m_renameMode != eRenameMode_Copy); bool enableUndo = (m_renameMode != eRenameMode_Copy);
p->renamingDone( true, enableUndo, this, errors ); p->renamingDone(true, enableUndo, this, errors);
#if 0 #if 0
delete object; delete object;
t.start(); t.start();
m_counters.clear(); m_counters.clear();
for( unsigned int i = 0; i < m_files->count(); i++) for (unsigned int i = 0; i < m_files->count(); i++) {
{ m_counter_index = 0;
m_counter_index = 0;
if( m_mode == RENAME ) {// final Path = source Path if (m_mode == RENAME) { // final Path = source Path
m_files[i].dst.directory = m_files[i].src.directory; m_files[i].dst.directory = m_files[i].src.directory;
m_files[i].dst.url = m_files[i].src.url; m_files[i].dst.url = m_files[i].src.url;
m_files[i].dst.url.setFileName( QString::null ); m_files[i].dst.url.setFileName(QString::null);
} else { } else {
m_files[i].dst.directory = m_destination.path(); m_files[i].dst.directory = m_destination.path();
m_files[i].dst.url = m_destination; m_files[i].dst.url = m_destination;
} }
if( i == 0 ) if (i == 0) {
p->setDestination( m_files[i].dst.url ); p->setDestination(m_files[i].dst.url);
else } else {
{ if (m_reset) {
if( m_reset ) findCounterReset(i);
findCounterReset( i ); }
} }
m_files[i].dst.name = processString( text, m_files[i].src.name, i ); m_files[i].dst.name = processString(text, m_files[i].src.name, i);
if( !extext.isEmpty() ) if (!extext.isEmpty()) {
m_files[i].dst.extension = processString( extext, m_files[i].src.ext m_files[i].dst.extension = processString(extext, m_files[i].src.exte
ension, i ); nsion, i);
}
// Assemble filenames // Assemble filenames
parseSubdirs( &m_files[i] ); parseSubdirs(&m_files[i]);
// TODO: DOM // TODO: DOM
// ESCAPE HERE // ESCAPE HERE
m_files[i].src.name = BatchRenamer::buildFilename( &m_files[i].src, true ); m_files[i].src.name = BatchRenamer::buildFilename(&m_files[i].src, true) ;
// Let's run the plugins that change the final filename, // Let's run the plugins that change the final filename,
// i.e the encodingsplugin // i.e the encodingsplugin
m_files[i].dst.name = parsePlugins( i, m_files[i].dst.name, TYPE_FINAL_F ILENAME ); m_files[i].dst.name = parsePlugins(i, m_files[i].dst.name, TYPE_FINAL_FI LENAME);
m_files[i].dst.name = BatchRenamer::buildFilename( &m_files[i].dst, true ); m_files[i].dst.name = BatchRenamer::buildFilename(&m_files[i].dst, true) ;
/* /*
* take care of renamed directories and * take care of renamed directories and
* correct the paths of their contents * correct the paths of their contents
*/ */
if( m_files[i].dir && (m_mode == RENAME || m_mode == MOVE) ) { if (m_files[i].dir && (m_mode == RENAME || m_mode == MOVE)) {
for( unsigned int c = i; c < m_files.count(); c++ ) { for (unsigned int c = i; c < m_files.count(); c++) {
if( m_files[c].src.directory.left( m_files[i].src.name.length() if (m_files[c].src.directory.left(m_files[i].src.name.length() +
+ 1 ) 1)
== ( m_files[i].src.name + "/" ) ) { == (m_files[i].src.name + "/")) {
m_files[c].src.directory.replace( 0, m_files[i].src.name.len m_files[c].src.directory.replace(0, m_files[i].src.name.leng
gth(), m_files[i].dst.name ); th(), m_files[i].dst.name);
m_files[c].src.url.setPath( BatchRenamer::buildFilename( &m_ m_files[c].src.url.setPath(BatchRenamer::buildFilename(&m_fi
files[c].src, true ) ); les[c].src, true));
} }
} }
} }
} }
p->print( QString( i18n("Filenames Processed after %1 seconds.")).arg(t.elap sed()/1000) ); p->print(QString(i18n("Filenames Processed after %1 seconds.", t.elapsed() / 1000)));
work( p ); work(p);
#endif // 0 #endif // 0
} }
void BatchRenamer::undoFiles( ProgressDialog* p ) void BatchRenamer::undoFiles(ProgressDialog *p)
{ {
int errors = 0; int errors = 0;
KUrl dest = (*m_files)[0].dstUrl(); QUrl dest = (*m_files)[0].dstUrl();
// Give the user some information... // Give the user some information...
p->setProgressTotalSteps( m_files->count() ); p->setProgressTotalSteps(m_files->count());
p->setProgress( 0 ); p->setProgress(0);
p->setDestination( dest ); p->setDestination(dest);
p->print( i18n("Undoing all renamed files.") ); p->print(i18n("Undoing all renamed files."));
for( unsigned int i = 0; i < static_cast<unsigned int>(m_files->count()); i+ for (unsigned int i = 0; i < static_cast<unsigned int>(m_files->count()); i+
+) +) {
{ QUrl dstUrl = this->buildDestinationUrl((*m_files)[i]);
KUrl dstUrl = this->buildDestinationUrl( (*m_files)[i] );
//p->print( QString( "%1 -> %2" ).arg( (*m_files)[i].srcUrl().prettyUrl( //p->print( QString( "%1 -> %2" ).arg( (*m_files)[i].srcUrl().prettyUrl(
) ).arg( dstUrl.prettyUrl() ) ); ) ).arg( dstUrl.toDisplayString() ) );
p->setProgress( i + 1 ); p->setProgress(i + 1);
if( p->wasCancelled() ) if (p->wasCancelled()) {
break; break;
}
KIO::JobFlags flags = (m_overwrite ? KIO::Overwrite : KIO::DefaultFlags) | KIO::HideProgressInfo; KIO::JobFlags flags = (m_overwrite ? KIO::Overwrite : KIO::DefaultFlags) | KIO::HideProgressInfo;
KIO::Job* job = NULL; KIO::Job *job = nullptr;
switch( m_renameMode ) switch (m_renameMode) {
{ default:
default: case eRenameMode_Rename:
case eRenameMode_Rename: case eRenameMode_Move:
case eRenameMode_Move: job = KIO::file_move(dstUrl, (*m_files)[i].srcUrl(), -1, flags);
job = KIO::file_move( dstUrl, (*m_files)[i].srcUrl(), -1, flags break;
); case eRenameMode_Link:
break; // In case of link delete created file
case eRenameMode_Link: job = KIO::file_delete(dstUrl, KIO::HideProgressInfo);
// In case of link delete created file break;
job = KIO::file_delete( dstUrl, false ); case eRenameMode_Copy: // no undo possible
break; // TODO: Maybe we should delete the created files
case eRenameMode_Copy: // no undo possible break;
// TODO: Maybe we should delete the created files
break;
} }
if( job && !NetAccess::synchronousRun( job, p ) ) if (job) {
{ KJobWidgets::setWindow(job, p);
p->error( i18n("Error during undoing %1", dstUrl.prettyUrl()) ); if (!job->exec()) {
(*m_files)[i].setError( 1 ); p->error(i18n("Error during undoing %1", dstUrl.toDisplayString(
errors++; QUrl::PreferLocalFile)));
(*m_files)[i].setError(job->error());
errors++;
}
} }
} }
if( errors > 0 ) if (errors > 0) {
p->warning( i18n("%1 errors occurred!", errors ) ); p->warning(i18np("%1 error occurred.", "%1 errors occurred.", errors));
}
p->print( i18n("KRename finished the undo process."), "krename" ); p->print(i18n("KRename finished the undo process."), "krename");
p->print( i18n("Press close to quit!") ); p->print(i18n("Press close to quit."));
p->renamingDone( false, false, this, errors ); // do not allow undo from und p->renamingDone(false, false, this, errors); // do not allow undo from und
o o
} }
QString BatchRenamer::processBrackets( QString text, int* length, const QString & oldname, int index ) QString BatchRenamer::processBrackets(QString text, int *length, const QString & oldname, int index)
{ {
int pos = 0; int pos = 0;
QString token; QString token;
QString result; QString result;
*length = 0; *length = 0;
// MSG: qDebug("processBrackets: %s\n", text.toUtf8().data() ); // MSG: qDebug("processBrackets: %s\n", text.toUtf8().data() );
while( (pos = getNextToken( text, token, pos )) != -1 ) while ((pos = getNextToken(text, token, pos)) != -1) {
{ if (token == "[") {
if( token == "[" )
{
int localLength = 0; int localLength = 0;
QString substitute = processBrackets( text.right( text.length() - po QString substitute = processBrackets(text.right(text.length() - pos)
s ), &localLength, oldname, index ); , &localLength, oldname, index);
text.replace( pos - 1, localLength, substitute ); text.replace(pos - 1, localLength, substitute);
// MSG: qDebug("substituted: %s\n", text.toUtf8().data() ); // MSG: qDebug("substituted: %s\n", text.toUtf8().data() );
// Assure that *length does not become negative, // Assure that *length does not become negative,
// this will cause infinite loops // this will cause infinite loops
if( localLength < substitute.length() ) if (localLength < substitute.length()) {
{
*length += localLength; *length += localLength;
} else {
*length += (localLength - substitute.length());
} }
else } else if (token == "]") {
{
*length += (localLength - substitute.length() );
}
}
else if( token == "]" )
{
// Done with this token // Done with this token
// MSG: qDebug("END: %s\n", text.left( pos - 1 ).toUtf8().data() ); // MSG: qDebug("END: %s\n", text.left( pos - 1 ).toUtf8().data() );
result = findToken( oldname, text.left( pos - 1 ), index ); result = findToken(oldname, text.left(pos - 1), index);
*length += pos + 1; *length += pos + 1;
break; break;
} }
} }
// MSG: qDebug("processedBrackets: %s\n", result.toUtf8().data() ); // MSG: qDebug("processedBrackets: %s\n", result.toUtf8().data() );
/* /*
if( pos != -1 ) if( pos != -1 )
{ {
result = findToken( oldname, text.left( pos - 1 ), index ); result = findToken( oldname, text.left( pos - 1 ), index );
*length = pos+1; // skip any closing bracket *length = pos+1; // skip any closing bracket
} }
*/ */
return result; return result;
} }
QString BatchRenamer::processNumber( int length, const QString & appendix ) QString BatchRenamer::processNumber(int length, const QString &appendix)
{ {
tCounterValues countervalues; tCounterValues countervalues;
countervalues.start = m_index; countervalues.start = m_index;
countervalues.step = m_step; countervalues.step = m_step;
if( !appendix.isEmpty() ) if (!appendix.isEmpty()) {
{
bool ok = false; bool ok = false;
int tmp = appendix.section( ';', 0, 0 ).toInt( &ok ); // first section = int tmp = appendix.section(';', 0, 0).toInt(&ok); // first section =
start index start index
if( ok ) if (ok) {
countervalues.start = tmp; countervalues.start = tmp;
}
tmp= appendix.section( ';', 1, 1 ).toInt( &ok ); // second section = ste tmp = appendix.section(';', 1, 1).toInt(&ok); // second section = ste
pping pping
if( ok ) if (ok) {
countervalues.step = tmp; countervalues.step = tmp;
}
} }
if( (signed int)m_counters.count() <= m_counter_index ) if ((signed int)m_counters.count() <= m_counter_index) {
{ countervalues.value = countervalues.start - countervalues.step;
countervalues.value = countervalues.start - countervalues.step; // other wise the counter would start at:
// other wise the counter would start at: // start + step instead of start
// start + step instead of start m_counters.append(countervalues);
m_counters.append( countervalues );
} }
do { do {
m_counters[m_counter_index].value += m_counters[m_counter_index].step; m_counters[m_counter_index].value += m_counters[m_counter_index].step;
} while( m_skip.contains( m_counters[m_counter_index].value ) ); } while (m_skip.contains(m_counters[m_counter_index].value));
QString number; QString number;
number.sprintf("%0*i", length, m_counters[m_counter_index].value ); number.sprintf("%0*i", length, m_counters[m_counter_index].value);
++m_counter_index; ++m_counter_index;
return number; return number;
} }
QString BatchRenamer::processString( QString text, const QString & originalName, int index, bool doFindReplace ) QString BatchRenamer::processString(QString text, const QString &originalName, i nt index, bool doFindReplace)
{ {
QString oldname = originalName; QString oldname = originalName;
doEscape( oldname ); doEscape(oldname);
// Parse into tokens // Parse into tokens
int pos=0; int pos = 0;
QString token; QString token;
while( (pos = getNextToken( text, token, pos )) != -1 ) while ((pos = getNextToken(text, token, pos)) != -1) {
{
// Handle simple tokens // Handle simple tokens
if( token == "$" ) if (token == "$") {
{ text.replace(pos - 1, token.length(), oldname);
text.replace( pos - 1, token.length(), oldname );
pos += oldname.length() - 1; pos += oldname.length() - 1;
} } else if (token == "%") {
else if( token == "%" ) text.replace(pos - 1, token.length(), oldname.toLower());
{
text.replace( pos - 1, token.length(), oldname.toLower() );
pos += oldname.length() - 1; pos += oldname.length() - 1;
} } else if (token == "&") {
else if( token == "&" ) text.replace(pos - 1, token.length(), oldname.toUpper());
{
text.replace( pos - 1, token.length(), oldname.toUpper() );
pos += oldname.length() - 1; pos += oldname.length() - 1;
} } else if (token == "*") {
else if( token == "*" ) QString tmp = capitalize(oldname);
{
QString tmp = capitalize( oldname );
text.replace( pos - 1, token.length(), tmp ); text.replace(pos - 1, token.length(), tmp);
pos += tmp.length() - 1; pos += tmp.length() - 1;
} } else if (token == "[") {
else if( token == "[" )
{
int length = 0; int length = 0;
QString substitute = processBrackets( text.right( text.length() - po QString substitute = processBrackets(text.right(text.length() - pos)
s ), &length, oldname, index ); , &length, oldname, index);
text.replace( pos - 1, length, substitute ); text.replace(pos - 1, length, substitute);
if( substitute.length() > 0 ) if (substitute.length() > 0) {
pos += substitute.length() - 1; pos += substitute.length() - 1;
} }
else if( token == "]" ) } else if (token == "]") {
{
// Ignore // Ignore
} } else if (token == "#") {
else if( token == "#" )
{
int curPos = pos; int curPos = pos;
int count = 1; int count = 1;
while( text[curPos] == '#' ) while (text[curPos] == '#') {
{
++curPos; ++curPos;
count++; count++;
} }
int length = curPos - pos + 1; int length = curPos - pos + 1;
int appendixLength = 0; int appendixLength = 0;
QString appendix; QString appendix;
if( text[curPos] == '{' ) if (text[curPos] == '{') {
{
int appendixPos = curPos + 1; int appendixPos = curPos + 1;
QString appendixToken; QString appendixToken;
while( (appendixPos = getNextToken( text, appendixToken, appendi while ((appendixPos = getNextToken(text, appendixToken, appendix
xPos )) != -1 ) Pos)) != -1) {
{ if (appendixToken == "}") {
if( appendixToken == "}" )
{
break; break;
} }
} }
if( appendixPos == -1 ) if (appendixPos == -1) {
{
// Do go into endless loop if token is not completed correct ly // Do go into endless loop if token is not completed correct ly
appendixPos = text.length(); appendixPos = text.length();
} }
// -2 because we have to go, before the current found token // -2 because we have to go, before the current found token
appendix = text.mid( curPos + 1, appendixPos - curPos - 2 ); appendix = text.mid(curPos + 1, appendixPos - curPos - 2);
appendixLength = appendixPos - curPos; appendixLength = appendixPos - curPos;
} }
QString number = processNumber( count, appendix ); QString number = processNumber(count, appendix);
text.replace( pos - 1, (length + appendixLength), number ); text.replace(pos - 1, (length + appendixLength), number);
if( number.length() > 0 ) if (number.length() > 0) {
pos += number.length() - 1; pos += number.length() - 1;
}
} }
} }
//text = parsePlugins( i, text, TYPE_TOKEN ); //text = parsePlugins( i, text, TYPE_TOKEN );
/* /*
* Replace after Plugins ! * Replace after Plugins !
* Replace shoud be the last the * Replace should be the last the
* before re-escaping tokens ! * before re-escaping tokens !
*/ */
if( doFindReplace ) { if (doFindReplace) {
text = findReplace( text, originalName, index ); text = findReplace(text, originalName, index);
} }
text = unEscape( text ); text = unEscape(text);
return text; return text;
} }
QString BatchRenamer::capitalize( const QString & text ) const QString BatchRenamer::capitalize(const QString &text) const
{ {
QString tmp = text.toLower(); QString tmp = text.toLower();
if( tmp[0].isLetter() ) if (tmp[0].isLetter()) {
tmp[0] = tmp[0].toUpper(); tmp[0] = tmp[0].toUpper();
}
for( int i = 0; i < tmp.length(); i++ ) for (int i = 0; i < tmp.length(); i++)
if( tmp[i+1].isLetter() && !tmp[i].isLetter() && if (tmp[i + 1].isLetter() && !tmp[i].isLetter() &&
tmp[i] != '\'' && tmp[i] != '?' && tmp[i] != '`' ) tmp[i] != '\'' && tmp[i] != '?' && tmp[i] != '`') {
tmp[i+1] = tmp[i+1].toUpper(); tmp[i + 1] = tmp[i + 1].toUpper();
}
return tmp; return tmp;
} }
QString BatchRenamer::executePlugin( int index, const QString & filenameOrPath, int type, int & errorCount, ProgressDialog* p ) QString BatchRenamer::executePlugin(int index, const QString &filenameOrPath, in t type, int &errorCount, ProgressDialog *p)
{ {
const QList<Plugin*> & plugins = PluginLoader::Instance()->plugins(); const QList<Plugin *> &plugins = PluginLoader::Instance()->plugins();
QList<Plugin*>::const_iterator it = plugins.begin(); QList<Plugin *>::const_iterator it = plugins.begin();
errorCount = 0; errorCount = 0;
QString ret = filenameOrPath; QString ret = filenameOrPath;
while( it != plugins.end() ) while (it != plugins.end()) {
{ if ((*it)->isEnabled() && ((*it)->type() & type)) {
if( (*it)->isEnabled() && ((*it)->type() & type) )
{
// Every plugin should use the return value of the previous as the n ew filename to work on // Every plugin should use the return value of the previous as the n ew filename to work on
ret = (*it)->processFile( this, index, ret, static_cast<EPluginType> ret = (*it)->processFile(this, index, ret, static_cast<EPluginType>(
(type) ); type));
if( type == ePluginType_File ) if (type == ePluginType_File) {
{ if (! ret.isEmpty()) {
if( ret != QString::null )
{
// An error occurred -> report it // An error occurred -> report it
if( p != NULL ) if (p != nullptr) {
p->error( ret ); p->error(ret);
}
++errorCount; ++errorCount;
} }
ret = filenameOrPath; ret = filenameOrPath;
} }
} }
++it; ++it;
} }
return ret; return ret;
} }
void BatchRenamer::work( ProgressDialog* ) void BatchRenamer::work(ProgressDialog *)
{ {
#if 0 #if 0
// TODO: use CopyJob here // TODO: use CopyJob here
FileOperation fop; FileOperation fop;
QFile* fundo ( NULL ); QFile *fundo(NULL);
QTextStream* tundo ( NULL ); QTextStream *tundo(NULL);
if( undo ) { if (undo) {
// Create header for undo script // Create header for undo script
fundo = new QFile( m_undoScript ); fundo = new QFile(m_undoScript);
if( fundo->open( IO_WriteOnly ) ) { if (fundo->open(IO_WriteOnly)) {
tundo = new QTextStream( fundo ); tundo = new QTextStream(fundo);
writeUndoScript( tundo ); writeUndoScript(tundo);
} else { } else {
undo = false; undo = false;
p->error( i18n("Can't create undo script :") + fundo->name() ); p->error(i18n("Cannot create undo script: %1", fundo->name()));
delete fundo; delete fundo;
} }
} }
int error = 0; int error = 0;
RenamedList* renamedFiles = new RenamedList[m_files.count()]; RenamedList *renamedFiles = new RenamedList[m_files.count()];
p->setProgressTotalSteps( m_files.count() + 1 ); p->setProgressTotalSteps(m_files.count() + 1);
/* /*
* Give the user some information... * Give the user some information...
*/ */
if( m_mode == COPY) if (m_mode == COPY) {
p->print( QString( i18n("Files will be copied to: %1") ).arg(m_files[0]. p->print(i18n("Files will be copied to: %1", m_files[0].dst.directory));
dst.directory) ); } else if (m_mode == MOVE) {
else if( m_mode == MOVE ) p->print(i18n("Files will be moved to: %1", m_files[0].dst.directory));
p->print( QString( i18n("Files will be moved to: %1") ).arg(m_files[0].d } else if (m_mode == LINK) {
st.directory) ); p->print(i18n("Symbolic links will be created in: %1", m_files[0].dst.di
else if( m_mode == LINK ) rectory));
p->print( QString( i18n("Symbolic links will be created in: %1") ).arg(m } else if (m_mode == RENAME) {
_files[0].dst.directory) ); p->print(i18n("Input files will be renamed."));
else if( m_mode == RENAME ) }
p->print( i18n("Input files will be renamed.") );
unsigned int i; unsigned int i;
for( i = 0; i < m_files.count(); i++) { for (i = 0; i < m_files.count(); i++) {
p->setProgress( i+1 ); p->setProgress(i + 1);
if( p->wasCancelled() ) if (p->wasCancelled()) {
break; break;
}
KURL src = m_files[i].src.url; KURL src = m_files[i].src.url;
KURL dst = m_files[i].dst.url; KURL dst = m_files[i].dst.url;
dst.setPath( m_files[i].dst.name ); dst.setPath(m_files[i].dst.name);
renamedFiles[i].src = src; renamedFiles[i].src = src;
renamedFiles[i].dst = dst; renamedFiles[i].dst = dst;
renamedFiles[i].dir = m_files[i].dir; renamedFiles[i].dir = m_files[i].dir;
FileOperation fop; FileOperation fop;
if( !fop.start( src, dst, m_mode, overwrite ) ) { if (!fop.start(src, dst, m_mode, overwrite)) {
p->error( fop.error() ); p->error(fop.error());
renamedFiles[i].error = true; renamedFiles[i].error = true;
error++; error++;
continue; continue;
} else { } else {
renamedFiles[i].error = false; renamedFiles[i].error = false;
} }
// TODO: overwriting of files! // TODO: overwriting of files!
/* /*
* The renamed file should be on its correct location now, * The renamed file should be on its correct location now,
* so that we can call the last plugins (e.g. for changing permissions) * so that we can call the last plugins (e.g. for changing permissions)
* *
* Remember, the token argument is the filename for this type of plugins ! * Remember, the token argument is the filename for this type of plugins !
* *
* If the return value is not empty an error has occured! * If the return value is not empty an error has occurred!
* The plugin should return an error message in this case! * The plugin should return an error message in this case!
*/ */
QString eplug = parsePlugins( i, QString::null, TYPE_FINAL_FILE ); QString eplug = parsePlugins(i, QString::null, TYPE_FINAL_FILE);
if( !eplug.isEmpty() ) { if (!eplug.isEmpty()) {
p->error( eplug ); p->error(eplug);
error++; error++;
} }
/* Create the undo script now */ /* Create the undo script now */
if( undo ) if (undo)
if( dst.isLocalFile() && src.isLocalFile() ) { if (dst.isLocalFile() && src.isLocalFile()) {
// Plugins ??? // Plugins ???
(*tundo) << "echo \"" << dst.fileName() (*tundo) << "echo \"" << dst.fileName()
<< " -> " << src.fileName() << "\"" << endl; << " -> " << src.fileName() << "\"" << endl;
(*tundo) << "mv -f \"" << m_files[i].dst.name (*tundo) << "mv -f \"" << m_files[i].dst.name
<< "\" \"" << m_files[i].src.name << "\"" << endl; << "\" \"" << m_files[i].src.name << "\"" << endl;
} else } else {
p->warning( QString( i18n("Undo is not possible for remote file: p->warning(i18n("Undo is not possible for remote file: %1", dst.
%1") ).arg( dst.prettyURL() ) ); prettyURL()));
}
} }
if( !p->wasCancelled() ) { if (!p->wasCancelled()) {
QPtrListIterator<PluginLoader::PluginLibrary> it( plug->libs ); QPtrListIterator<PluginLoader::PluginLibrary> it(plug->libs);
for( ; it.current(); ++it ) { for (; it.current(); ++it) {
if( (*it)->usePlugin ) if ((*it)->usePlugin) {
(*it)->plugin->finished(); (*it)->plugin->finished();
}
} }
} }
const QString m = QString( i18n("Renamed %1 files successfully.") ).arg(i-er const QString m = i18n("Renamed %1 files successfully.", i - error);
ror); (i - error) ? p->print(m) : p->warning(m);
( i - error ) ? p->print( m ) : p->warning( m );
if( error > 0 ) if (error > 0) {
p->warning( QString( i18n("%2 errors occurred!") ).arg(error)); p->warning(i18np("%1 error occurred.", "%1 errors occurred.", error));
}
p->print( QString( i18n("Elapsed time: %1 seconds") ).arg( t.elapsed()/1000 p->print(i18n("Elapsed time: %1 seconds", t.elapsed() / 1000), "kalarm");
), "kalarm" ); p->print(i18n("KRename finished the renaming process."), "krename");
p->print( i18n("KRename finished the renaming process."), "krename" ); p->print(i18n("Press close to quit."));
p->print( i18n("Press close to quit!") ); p->setRenamedFiles(renamedFiles, m_files.count());
p->setRenamedFiles( renamedFiles, m_files.count() );
if( undo ) { if (undo) {
(*tundo) << endl << "echo \"Finished undoing " << m_files.count() << " a ctions.\"" << endl; (*tundo) << endl << "echo \"Finished undoing " << m_files.count() << " a ctions.\"" << endl;
delete tundo; delete tundo;
fundo->close(); fundo->close();
// Make fundo exuteable // Make fundo exuteable
if( chmod( (const char*)m_undoScript, (unsigned int) S_IRUSR | S_IWUSR | if (chmod((const char *)m_undoScript, (unsigned int) S_IRUSR | S_IWUSR |
S_IXUSR ) ) S_IXUSR)) {
p->error( i18n("Can't set executable bit on undo script.") ); p->error(i18n("Cannot set executable bit on undo script."));
}
delete fundo; delete fundo;
} }
p->done( error, i-error, m_mode == MOVE || m_mode == RENAME ); p->done(error, i - error, m_mode == MOVE || m_mode == RENAME);
m_files.clear(); m_files.clear();
delete []renamedFiles; delete []renamedFiles;
delete this; delete this;
#endif // 0 #endif // 0
} }
const KUrl BatchRenamer::buildDestinationUrl( const KRenameFile & file ) const const QUrl BatchRenamer::buildDestinationUrl(const KRenameFile &file) const
{ {
KUrl dstUrl = file.dstUrl(); QUrl dstUrl = file.dstUrl();
QString directory = file.dstDirectory(); QString directory = file.dstDirectory();
QString filename = file.dstFilename(); QString filename = file.dstFilename();
QString extension = file.dstExtension(); QString extension = file.dstExtension();
QString manual = file.manualChanges(); QString manual = file.manualChanges();
if( !extension.isEmpty() ) if (!extension.isEmpty()) {
{ filename += '.';
filename += ".";
filename += extension; filename += extension;
} }
if( !manual.isNull() ) if (!manual.isNull()) {
filename = manual; filename = manual;
}
dstUrl.setDirectory( directory ); dstUrl.setPath(directory + '/' + filename);
dstUrl.setFileName( filename );
return dstUrl; return dstUrl;
} }
void BatchRenamer::escape( QString & text, const QString & token, const QString & sequence ) void BatchRenamer::escape(QString &text, const QString &token, const QString &se quence)
{ {
text.replace( token, sequence ); text.replace(token, sequence);
} }
QString & BatchRenamer::doEscape( QString & text ) QString &BatchRenamer::doEscape(QString &text)
{ {
BatchRenamer::escape( text, "\\", "\\\\" ); BatchRenamer::escape(text, "\\", "\\\\");
BatchRenamer::escape( text, "&", "\\&" ); BatchRenamer::escape(text, "&", "\\&");
BatchRenamer::escape( text, "$", "\\$" ); BatchRenamer::escape(text, "$", "\\$");
BatchRenamer::escape( text, "%", "\\%" ); BatchRenamer::escape(text, "%", "\\%");
BatchRenamer::escape( text, "#", "\\#" ); BatchRenamer::escape(text, "#", "\\#");
BatchRenamer::escape( text, "[", "\\[" ); BatchRenamer::escape(text, "[", "\\[");
BatchRenamer::escape( text, "]", "\\]" ); BatchRenamer::escape(text, "]", "\\]");
BatchRenamer::escape( text, "/", "\\/" ); BatchRenamer::escape(text, "/", "\\/");
BatchRenamer::escape( text, "{", "\\{" ); BatchRenamer::escape(text, "{", "\\{");
BatchRenamer::escape( text, "}", "\\}" ); BatchRenamer::escape(text, "}", "\\}");
BatchRenamer::escape( text, "*", "\\*" ); BatchRenamer::escape(text, "*", "\\*");
return text; return text;
} }
QString & BatchRenamer::unEscape( QString & text ) QString &BatchRenamer::unEscape(QString &text)
{ {
BatchRenamer::escape( text, "\\\\", "\\" ); BatchRenamer::escape(text, "\\\\", "\\");
BatchRenamer::escape( text, "\\&", "&" ); BatchRenamer::escape(text, "\\&", "&");
BatchRenamer::escape( text, "\\$", "$" ); BatchRenamer::escape(text, "\\$", "$");
BatchRenamer::escape( text, "\\%", "%" ); BatchRenamer::escape(text, "\\%", "%");
BatchRenamer::escape( text, "\\#", "#" ); BatchRenamer::escape(text, "\\#", "#");
BatchRenamer::escape( text, "\\[", "[" ); BatchRenamer::escape(text, "\\[", "[");
BatchRenamer::escape( text, "\\]", "]" ); BatchRenamer::escape(text, "\\]", "]");
// %252f == /, it seems that filenames on unix cannot contain // %252f == /, it seems that filenames on unix cannot contain
// a /. So I use %252f, at least konqui displays it correctly // a /. So I use %252f, at least konqui displays it correctly
// this was needed, so that plugins that return a slash do not cause errors // this was needed, so that plugins that return a slash do not cause errors
BatchRenamer::escape( text, "\\/", "%2f" ); BatchRenamer::escape(text, "\\/", "%2f");
BatchRenamer::escape( text, "\\{", "{" ); BatchRenamer::escape(text, "\\{", "{");
BatchRenamer::escape( text, "\\}", "}" ); BatchRenamer::escape(text, "\\}", "}");
BatchRenamer::escape( text, "\\*", "*" ); BatchRenamer::escape(text, "\\*", "*");
return text; return text;
} }
QString BatchRenamer::processToken( QString token, QString oldname, int i ) QString BatchRenamer::processToken(QString token, QString oldname, int i)
{ {
QString tmp; QString tmp;
/* /*
* Call here all functions that handle * Call here all functions that handle
* arguments in brackets. * arguments in brackets.
*/ */
tmp = findPartStrings( oldname, token ); tmp = findPartStrings(oldname, token);
if( !tmp.isEmpty() ) if (!tmp.isEmpty()) {
return tmp; return tmp;
}
tmp = findDirName( token, (*m_files)[i].srcDirectory() ); tmp = findDirName(token, (*m_files)[i].srcDirectory());
if( !tmp.isEmpty() ) if (!tmp.isEmpty()) {
return tmp; return tmp;
}
tmp = findLength( token, (*m_files)[i].srcFilename() ); tmp = findLength(token, (*m_files)[i].srcFilename());
if( !tmp.isEmpty() ) if (!tmp.isEmpty()) {
return tmp; return tmp;
}
tmp = findTrimmed( token, (*m_files)[i].srcFilename(), i ); tmp = findTrimmed(token, (*m_files)[i].srcFilename(), i);
if( !tmp.isEmpty() ) if (!tmp.isEmpty()) {
return tmp; return tmp;
}
tmp = findDirSep( token, (*m_files)[i].srcFilename() ); tmp = findDirSep(token, (*m_files)[i].srcFilename());
if( !tmp.isEmpty() ) if (!tmp.isEmpty()) {
return tmp; return tmp;
}
Plugin* p = PluginLoader::Instance()->findPlugin( token ); Plugin *p = PluginLoader::Instance()->findPlugin(token);
if( p ) if (p) {
{ tmp = p->processFile(this, i, token, ePluginType_Token);
tmp = p->processFile( this, i, token, ePluginType_Token ); if (!tmp.isNull()) {
if( !tmp.isNull() ) doEscape(tmp);
{
doEscape( tmp );
return tmp; return tmp;
} }
} }
/* /*
* Maybe I should remove this! * Maybe I should remove this!
* KRename simply ignores unknown tokens! * KRename simply ignores unknown tokens!
* Usefull for the MP3 Plugin! * Useful for the MP3 Plugin!
*/ */
return QString::null; return QString();
} }
QString BatchRenamer::findToken( const QString & oldname, QString token, int i ) QString BatchRenamer::findToken(const QString &oldname, QString token, int i)
{ {
enum conversion { LOWER, UPPER, MIXED, STAR, NONE, EMPTY, NUMBER }; enum conversion { LOWER, UPPER, MIXED, STAR, NONE, EMPTY, NUMBER };
unsigned int numwidth = 0; unsigned int numwidth = 0;
conversion c = EMPTY; conversion c = EMPTY;
if( !token.left(1).compare("$") ) if (!token.left(1).compare("$")) {
c = NONE; c = NONE;
else if( !token.left(1).compare("%") ) } else if (!token.left(1).compare("%")) {
c = LOWER; c = LOWER;
else if( !token.left(1).compare("&") ) } else if (!token.left(1).compare("&")) {
c = UPPER; c = UPPER;
else if( !token.left(1).compare("") ) } else if (!token.left(1).compare("")) {
c = MIXED; c = MIXED;
else if( !token.left(1).compare("*") ) } else if (!token.left(1).compare("*")) {
c = STAR; c = STAR;
else if( !token.left(1).compare("#") ) { } else if (!token.left(1).compare("#")) {
while( !token.left(1).compare("#") ) { while (!token.left(1).compare("#")) {
token.remove( 0, 1 ); token.remove(0, 1);
++numwidth; ++numwidth;
} }
c = NUMBER; c = NUMBER;
} }
if( c != EMPTY && c != NUMBER ) if (c != EMPTY && c != NUMBER) {
token.remove( 0, 1 ); token.remove(0, 1);
}
QString save = token; QString save = token;
token = processToken( token, oldname, i ); token = processToken(token, oldname, i);
switch( c ) { switch (c) {
case LOWER: case LOWER:
token = token.toLower(); token = token.toLower();
break; break;
case UPPER: case UPPER:
token = token.toUpper(); token = token.toUpper();
break; break;
case MIXED: case MIXED:
token = token.toLower(); token = token.toLower();
token.replace( 0, 1, token[0].toUpper()); token.replace(0, 1, token[0].toUpper());
break; break;
case STAR: case STAR:
token = capitalize( token ); token = capitalize(token);
break; break;
case NUMBER: case NUMBER: {
{ bool b = false;
bool b = false; int n = token.toInt(&b);
int n = token.toInt( &b ); if (b) {
if( b ) token = token.sprintf("%0*i", numwidth, n);
token = token.sprintf("%0*i", numwidth, n ); }
} }
break; break;
default: default:
break; break;
} }
return token; return token;
} }
QString BatchRenamer::findPartStrings( QString oldname, QString token ) QString BatchRenamer::findPartStrings(QString oldname, QString token)
{ {
QString first, second; QString first, second;
int pos = -1; int pos = -1;
// MSG: qDebug("PART: %s", token.toUtf8().data() ); // MSG: qDebug("PART: %s", token.toUtf8().data() );
// parse things like [2;4{[dirname]}] // parse things like [2;4{[dirname]}]
if( token.count( '{' ) >= 1 && token.count( '}' ) >= 1 ) { if (token.count('{') >= 1 && token.count('}') >= 1) {
int pos = token.indexOf( '{' ); int pos = token.indexOf('{');
oldname = token.mid( pos + 1, token.lastIndexOf( '}' ) - pos - 1 ); oldname = token.mid(pos + 1, token.lastIndexOf('}') - pos - 1);
token = token.left( pos ); token = token.left(pos);
} }
if( token.contains('-') ) { if (token.contains('-')) {
pos = token.indexOf( '-', 0 ); pos = token.indexOf('-', 0);
first = token.left( pos ); first = token.left(pos);
// ------- Code OK ^ ! // ------- Code OK ^ !
second = token.mid( pos+1, token.length() ); second = token.mid(pos + 1, token.length());
// version < 1.7 // version < 1.7
// return oldname.mid( first.toInt()-1, second.toInt()-first.toInt() +1 ); // return oldname.mid( first.toInt()-1, second.toInt()-first.toInt() +1 );
// version > 1.7 // version > 1.7
//return oldname.mid( first.toInt()-1, second.toInt()-first.toInt() ); //return oldname.mid( first.toInt()-1, second.toInt()-first.toInt() );
// version > 1.8 // version > 1.8
bool ok; bool ok;
int sec = second.toInt( &ok ); int sec = second.toInt(&ok);
if( !ok || sec == 0 ) if (!ok || sec == 0) {
sec = oldname.length(); sec = oldname.length();
}
/* /*
* x should not be larger than the old name * x should not be larger than the old name
* and not smaller than zero. * and not smaller than zero.
*/ */
int x = sec-first.toInt( &ok ); int x = sec - first.toInt(&ok);
// if first is no number, but for example length, we return here so that findLength can do its job // if first is no number, but for example length, we return here so that findLength can do its job
if( !ok ) if (!ok) {
return QString::null; return QString();
}
if( x > (signed int)oldname.length() || x < 0 ) if (x > (signed int)oldname.length() || x < 0) {
x = oldname.length()-first.toInt(); x = oldname.length() - first.toInt();
}
/* /*
* if I would comment my code I would understand this line :) * if I would comment my code I would understand this line :)
* without this line, there is sometimes the last letter * without this line, there is sometimes the last letter
* of a filename missing. * of a filename missing.
*/ */
if( x != -1 ) if (x != -1) {
x++; x++;
}
return oldname.mid( first.toInt()-1, x ); return oldname.mid(first.toInt() - 1, x);
} else if( token.contains(';') ) { } else if (token.contains(';')) {
pos = token.indexOf( ';', 0 ); pos = token.indexOf(';', 0);
first = token.left( pos ); first = token.left(pos);
second = token.mid( pos+1, token.length() ); second = token.mid(pos + 1, token.length());
return oldname.mid( first.toInt()-1, second.toInt() ); return oldname.mid(first.toInt() - 1, second.toInt());
} else { } else {
bool ok = false; bool ok = false;
int number = token.toInt( &ok ); int number = token.toInt(&ok);
if( ok && (number <= (signed int)oldname.length() && number > 0 ) ) if (ok && (number <= (signed int)oldname.length() && number > 0)) {
return QString(oldname[ number -1 ]); return QString(oldname[ number - 1 ]);
else } else {
return QString::null; return QString();
}
} }
} }
QString BatchRenamer::findDirName( QString token, QString path ) QString BatchRenamer::findDirName(QString token, QString path)
{ {
if( token.toLower().startsWith( "dirname" ) ) { if (token.toLower().startsWith(QLatin1String("dirname"))) {
if( path.right( 1 ) == "/" ) if (path.right(1) == "/") {
path = path.left( path.length() - 1); path = path.left(path.length() - 1);
}
int recursion = 1; int recursion = 1;
if( token.length() > 7 ) { if (token.length() > 7) {
token = token.right( token.length() - 7 ); token = token.right(token.length() - 7);
recursion = token.count( '.' ); recursion = token.count('.');
if( recursion != (signed int)token.length() ) if (recursion != (signed int)token.length()) {
return QString::null; return QString();
}
recursion++; recursion++;
} }
return path.section( "/", recursion * -1, recursion * -1); return path.section("/", recursion * -1, recursion * -1);
} }
return QString::null; return QString();
} }
QString BatchRenamer::findDirSep( const QString & token, const QString & path ) QString BatchRenamer::findDirSep(const QString &token, const QString &path)
{ {
if( token.toLower() == "dirsep" ) { if (token.toLower() == "dirsep") {
return "/"; return "/";
} }
return QString::null; return QString();
} }
QString BatchRenamer::findLength( const QString & token, const QString & name ) QString BatchRenamer::findLength(const QString &token, const QString &name)
{ {
if( token.toLower().startsWith( "length" ) ) { if (token.toLower().startsWith(QLatin1String("length"))) {
int minus = 0; int minus = 0;
if( token.length() > 6 && token[6] == '-' ) { if (token.length() > 6 && token[6] == '-') {
bool n = false; bool n = false;
minus = token.mid( 7, token.length() - 7 ).toInt( &n ); minus = token.mid(7, token.length() - 7).toInt(&n);
if( !n ) if (!n) {
minus = 0; minus = 0;
}
} }
return QString::number( name.length() - minus ); return QString::number(name.length() - minus);
} }
return QString::null; return QString();
} }
QString BatchRenamer::findTrimmed( const QString & token, const QString & name, int index ) QString BatchRenamer::findTrimmed(const QString &token, const QString &name, int index)
{ {
if( token.toLower().startsWith( "trimmed" ) ) { if (token.toLower().startsWith(QLatin1String("trimmed"))) {
if( token.contains( ';' ) ) if (token.contains(';')) {
{ QString processed = processString(
QString processed = processString( token.section(';', 1, 1), name, index).trimm
token.section( ';', 1, 1 ), name, index ).trimmed(); ed();
if( processed.isNull() ) if (processed.isNull()) {
return name.trimmed(); return name.trimmed();
else } else {
return processed.trimmed(); return processed.trimmed();
} }
else } else {
return name.trimmed(); return name.trimmed();
}
} }
return QString::null; return QString();
} }
QString BatchRenamer::findReplace( const QString & text, const QString & origFil ename, int index ) QString BatchRenamer::findReplace(const QString &text, const QString &origFilena me, int index)
{ {
QList<TReplaceItem>::const_iterator it = m_replace.begin(); QList<TReplaceItem>::const_iterator it = m_replace.constBegin();
QString t( text ); QString t(text);
while( it != m_replace.end() ) while (it != m_replace.constEnd()) {
{ QString find((*it).find);
QString find( (*it).find );
// Call for each element in replace strings doReplace with correct value s // Call for each element in replace strings doReplace with correct value s
t = doReplace( t, unEscape( find ), (*it).replace, t = doReplace(t, unEscape(find), (*it).replace,
(*it).reg, (*it).doProcessTokens, (*it).reg, (*it).doProcessTokens,
origFilename, index ); origFilename, index);
++it; ++it;
} }
return t; return t;
} }
QString BatchRenamer::doReplace( const QString & text, const QString & find, con st QString & replace, bool reg, bool doProcessTokens, const QString & origFilena me, int index ) QString BatchRenamer::doReplace(const QString &text, const QString &find, const QString &replace, bool reg, bool doProcessTokens, const QString &origFilename, i nt index)
{ {
QString t( text ); QString t(text);
if( !reg ) if (!reg) {
{
QString escaped = find; QString escaped = find;
escaped = doEscape( escaped ); escaped = doEscape(escaped);
// we use the escaped text here because the user might want // we use the escaped text here because the user might want
// to find a "&" and replace it // to find a "&" and replace it
t.replace( escaped, replace ); t.replace(escaped, replace);
} } else {
else
{
// no doEscape() here for the regexp, because it would destroy our regul ar expression // no doEscape() here for the regexp, because it would destroy our regul ar expression
// other wise we will not find stuff like $, [ in the text // other wise we will not find stuff like $, [ in the text
t = unEscape( t ).replace( QRegExp( find ), replace ); t = unEscape(t).replace(QRegExp(find), replace);
t = doEscape( t ); t = doEscape(t);
} }
if( doProcessTokens ) { if (doProcessTokens) {
t = processString( unEscape( t ), origFilename, index, false ); t = processString(unEscape(t), origFilename, index, false);
} }
return t; return t;
} }
void BatchRenamer::writeUndoScript( QTextStream* t ) void BatchRenamer::writeUndoScript(QTextStream *t)
{ {
// write header comments // write header comments
(*t) << "#!/bin/sh" << endl (*t) << "#!/bin/sh" << endl
<< "# KRename Undo Script" << endl << "#" << endl << "# KRename Undo Script" << endl << "#" << endl
<< "# KRename was written by:" << endl << "# KRename was written by:" << endl
<< "# Dominik Seichter <domseichter@web.de>" << endl << "# Dominik Seichter <domseichter@web.de>" << endl
<< "# http://krename.sourceforge.net" << endl << "#" << endl << "# http://krename.sourceforge.net" << endl << "#" << endl
<< "# Script generated by KRename Version: " << VERSION << endl << endl << "# Script generated by KRename Version: " << VERSION << endl << endl
<< "# This script must be started with the option --krename to work!" < < endl; << "# This script must be started with the option --krename to work!" < < endl;
skipping to change at line 1179 skipping to change at line 1157
<< " echo \"\"" << endl << " echo \"\"" << endl
<< "else" << endl << "else" << endl
<< " echo \"You have to start this script\"" << endl << " echo \"You have to start this script\"" << endl
<< " echo \"with the command line option\"" << endl << " echo \"with the command line option\"" << endl
<< " echo \"--krename\"" << endl << " echo \"--krename\"" << endl
<< " echo \"to undo a rename operation.\"" << endl << " echo \"to undo a rename operation.\"" << endl
<< " exit" << endl << " exit" << endl
<< "fi" << endl; << "fi" << endl;
} }
void BatchRenamer::createMissingSubDirs( const KRenameFile & file, ProgressDialo g* p ) void BatchRenamer::createMissingSubDirs(const KRenameFile &file, ProgressDialog *dialog)
{ {
int pos = 0; QUrl url = file.dstUrl().adjusted(QUrl::RemoveFilename);
QString dstFilename = file.dstFilename(); if (url.isEmpty()) {
if( (pos = dstFilename.lastIndexOf( '/', -1 ) ) > 0 ) { return;
QString directories = dstFilename.left( pos ); }
// create the missing subdir now
int i = 0;
QString d = directories.section( "/", i, i, QString::SectionSkipEmpty );
KUrl url = file.dstUrl();
while( !d.isEmpty() ) {
// it is important to unescape here
// to support dirnames containing "&" or
// similar tokens
url.addPath( unEscape( d ) );
if( !KIO::NetAccess::exists( url, true, p ) && !KIO::NetAccess::mkdi
r( url, p ) ) {
p->error( i18n("Cannot create directory %1", url.prettyUrl()) );
}
i++; KIO::MkpathJob *job = KIO::mkpath(url);
d = directories.section( "/", i, i, QString::SectionSkipEmpty ); KJobWidgets::setWindow(job, dialog);
} if (!job->exec()) {
dialog->error(i18n("Cannot create directory %1: %2",
url.toDisplayString(QUrl::PreferLocalFile),
job->errorString()));
} }
} }
void BatchRenamer::findCounterReset( int i ) void BatchRenamer::findCounterReset(int i)
{ {
int z; if ((*m_files)[i - 1].srcDirectory() != (*m_files)[i].srcDirectory())
if( (*m_files)[i-1].srcDirectory() != (*m_files)[i].srcDirectory() ) for (int z = 0; z < (int)m_counters.count(); z++) {
for( z=0;z<(int)m_counters.count();z++ ) m_counters[z].value = m_counters[z].start - m_counters[z].step;
{ }
m_counters[z].value = m_counters[z].start - m_counters[z].step;
}
} }
#include "batchrenamer.moc"
 End of changes. 235 change blocks. 
590 lines changed or deleted 554 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)