Commit fd5578cf authored by Frerich Raabe's avatar Frerich Raabe

- Decomposed rename(), so that I can reuse some stuff in the configuratio

  dialog

svn path=/trunk/kdemultimedia/juk/; revision=248733
parent 95f6dfd2
......@@ -25,62 +25,78 @@ FileRenamer::FileRenamer(const PlaylistItem *item)
rename(item);
}
void FileRenamer::rename(const PlaylistItem *item)
QString FileRenamer::filenameScheme() const
{
if(item == 0 || item->tag() == 0)
return;
return KConfigGroup(kapp->config(), "FileRenamer").readEntry("FilenameScheme");
}
QString newFilename;
QString titleToken, artistToken, albumToken, trackToken, commentToken;
KConfig *cfg = kapp->config();
{
KConfigGroupSaver saver(cfg, "FileRenamer");
newFilename = cfg->readPathEntry("FilenameScheme", item->absFilePath());
QString FileRenamer::getToken(const QString &name, const QString &value) const
{
const KConfigGroup grp(kapp->config(), "FileRenamer");
if(!item->tag()->track().isNull()) {
titleToken = cfg->readEntry("Title token");
titleToken.replace("%s", item->tag()->track());
}
const bool needContent = grp.readBoolEntry("Need" + name + "Content", true);
if(value.isEmpty() && needContent)
return QString();
if(!item->tag()->artist().isNull()) {
artistToken = cfg->readEntry("Artist token");
artistToken.replace("%s", item->tag()->artist());
}
QString token = grp.readEntry(name + "Token");
token.replace("%s", value);
return token;
}
if(!item->tag()->album().isNull()) {
albumToken = cfg->readEntry("Album token");
albumToken.replace("%s", item->tag()->album());
}
QString FileRenamer::titleToken(const QString &value) const
{
return getToken("Title", value);
}
if(item->tag()->trackNumber() > 0) {
trackToken = cfg->readEntry("Track token");
trackToken.replace("%s", item->tag()->trackNumberString());
}
QString FileRenamer::artistToken(const QString &value) const
{
return getToken("Artist", value);
}
if(!item->tag()->comment().isNull()) {
commentToken = cfg->readEntry("Comment token");
trackToken.replace("%s", item->tag()->comment());
}
}
QString FileRenamer::albumToken(const QString &value) const
{
return getToken("Album", value);
}
QString FileRenamer::trackToken(const QString &value) const
{
return getToken("Track", value);
}
QString FileRenamer::commentToken(const QString &value) const
{
return getToken("Comment", value);
}
void FileRenamer::rename(const PlaylistItem *item)
{
if(item == 0 || item->tag() == 0)
return;
QString newFilename = rename(item->absFilePath(), *item->tag());
moveFile(item->absFilePath(), newFilename);
}
QString FileRenamer::rename(const QString &filename, const Tag &tag) const
{
QString newFilename = filenameScheme();
QMap<QChar, QString> substitutions;
substitutions[ 't' ] = titleToken;
substitutions[ 'a' ] = artistToken;
substitutions[ 'A' ] = albumToken;
substitutions[ 'T' ] = trackToken;
substitutions[ 'c' ] = commentToken;
substitutions[ 't' ] = titleToken(tag.track());
substitutions[ 'a' ] = artistToken(tag.artist());
substitutions[ 'A' ] = albumToken(tag.album());
substitutions[ 'T' ] = trackToken(tag.trackNumberString());
substitutions[ 'c' ] = commentToken(tag.comment());
newFilename = KMacroExpander::expandMacros(newFilename, substitutions);
newFilename = newFilename.stripWhiteSpace();
if(QFileInfo(newFilename).isRelative())
newFilename = item->absFilePath().left( item->absFilePath().findRev( "/" ) )
newFilename = filename.left( filename.findRev( "/" ) )
+ "/" + newFilename;
newFilename += "." + QFileInfo(filename).extension();
newFilename += "." + QFileInfo(item->absFilePath()).extension();
moveFile(item->absFilePath(), newFilename);
return newFilename;
}
void FileRenamer::moveFile(const QString &src, const QString &dest)
......
......@@ -9,8 +9,10 @@
#ifndef FILERENAMER_H
#define FILERENAMER_H
#include <qstring.h>
class PlaylistItem;
class QString;
class Tag;
class FileRenamer
{
......@@ -18,9 +20,18 @@ class FileRenamer
FileRenamer();
FileRenamer(const PlaylistItem *item);
QString filenameScheme() const;
QString titleToken( const QString &value = QString::null ) const;
QString artistToken( const QString &value = QString::null ) const;
QString albumToken( const QString &value = QString::null ) const;
QString trackToken( const QString &value = QString::null) const;
QString commentToken( const QString &value = QString::null ) const;
void rename(const PlaylistItem *item);
QString rename(const QString &filename, const Tag &tag) const;
private:
QString getToken(const QString &name, const QString &value) const;
void moveFile(const QString &src, const QString &dest);
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment