Commit 7f97a935 authored by Frerich Raabe's avatar Frerich Raabe

- Tearing things apart some more

svn path=/trunk/kdemultimedia/juk/; revision=248811
parent fd5578cf
...@@ -16,58 +16,76 @@ ...@@ -16,58 +16,76 @@
#include <qdir.h> #include <qdir.h>
FileRenamer::FileRenamer() FileRenamer::Config::Config(KConfigBase *cfg)
: m_grp(cfg, "FileRenamer")
{ {
} }
FileRenamer::FileRenamer(const PlaylistItem *item) QString FileRenamer::Config::filenameScheme() const
{ {
rename(item); return m_grp.readEntry("FilenameScheme");
} }
QString FileRenamer::filenameScheme() const void FileRenamer::Config::setFilenameScheme(const QString &scheme)
{ {
return KConfigGroup(kapp->config(), "FileRenamer").readEntry("FilenameScheme"); m_grp.writeEntry("FilenameScheme", scheme);
} }
QString FileRenamer::getToken(const QString &name, const QString &value) const QString FileRenamer::Config::getToken(TokenType type) const
{ {
const KConfigGroup grp(kapp->config(), "FileRenamer"); return m_grp.readEntry(tokenToString(type) + "Token");
}
const bool needContent = grp.readBoolEntry("Need" + name + "Content", true); void FileRenamer::Config::setToken(TokenType type, const QString &value)
if(value.isEmpty() && needContent) {
return QString(); m_grp.writeEntry(tokenToString(type) + "Token", value);
}
QString token = grp.readEntry(name + "Token"); bool FileRenamer::Config::tokenNeedsValue(TokenType type) const
token.replace("%s", value); {
return token; return m_grp.readBoolEntry("Need" + tokenToString(type) + "Value");
} }
QString FileRenamer::titleToken(const QString &value) const void FileRenamer::Config::setTokenNeedsValue(TokenType type, bool needsValue)
{ {
return getToken("Title", value); m_grp.writeEntry("Need" + tokenToString(type) + "Value", needsValue);
} }
QString FileRenamer::artistToken(const QString &value) const QString FileRenamer::tokenToString(TokenType type)
{ {
return getToken("Artist", value); switch(type) {
case Title: return "Title";
case Artist: return "Artist";
case Album: return "Album";
case Track: return "Track";
case Comment: return "Comment";
}
return QString::null;
} }
QString FileRenamer::albumToken(const QString &value) const FileRenamer::FileRenamer()
: m_cfg(kapp->config())
{ {
return getToken("Album", value);
} }
QString FileRenamer::trackToken(const QString &value) const FileRenamer::FileRenamer(const PlaylistItem *item)
: m_cfg(kapp->config())
{ {
return getToken("Track", value); rename(item);
} }
QString FileRenamer::commentToken(const QString &value) const QString FileRenamer::expandToken(TokenType type, const QString &value) const
{ {
return getToken("Comment", value); const bool needValue = m_cfg.tokenNeedsValue(type);
if(needValue && value.isEmpty())
return QString();
QString token = m_cfg.getToken(type);
token.replace("%s", value);
return token;
} }
void FileRenamer::rename(const PlaylistItem *item) void FileRenamer::rename(const PlaylistItem *item)
{ {
if(item == 0 || item->tag() == 0) if(item == 0 || item->tag() == 0)
...@@ -79,21 +97,21 @@ void FileRenamer::rename(const PlaylistItem *item) ...@@ -79,21 +97,21 @@ void FileRenamer::rename(const PlaylistItem *item)
QString FileRenamer::rename(const QString &filename, const Tag &tag) const QString FileRenamer::rename(const QString &filename, const Tag &tag) const
{ {
QString newFilename = filenameScheme(); QString newFilename = m_cfg.filenameScheme();
QMap<QChar, QString> substitutions; QMap<QChar, QString> substitutions;
substitutions[ 't' ] = titleToken(tag.track()); substitutions[ 't' ] = expandToken(Title, tag.track());
substitutions[ 'a' ] = artistToken(tag.artist()); substitutions[ 'a' ] = expandToken(Artist, tag.artist());
substitutions[ 'A' ] = albumToken(tag.album()); substitutions[ 'A' ] = expandToken(Album, tag.album());
substitutions[ 'T' ] = trackToken(tag.trackNumberString()); substitutions[ 'T' ] = expandToken(Track, tag.trackNumberString());
substitutions[ 'c' ] = commentToken(tag.comment()); substitutions[ 'c' ] = expandToken(Comment, tag.comment());
newFilename = KMacroExpander::expandMacros(newFilename, substitutions); newFilename = KMacroExpander::expandMacros(newFilename, substitutions);
newFilename = newFilename.stripWhiteSpace(); newFilename = newFilename.stripWhiteSpace();
if(QFileInfo(newFilename).isRelative()) if(QFileInfo(newFilename).isRelative())
newFilename = filename.left( filename.findRev( "/" ) ) newFilename = filename.left( filename.findRev( "/" ) )
+ "/" + newFilename; + "/" + newFilename;
newFilename += "." + QFileInfo(filename).extension(); newFilename += "." + QFileInfo(filename).extension();
return newFilename; return newFilename;
...@@ -121,7 +139,7 @@ void FileRenamer::moveFile(const QString &src, const QString &dest) ...@@ -121,7 +139,7 @@ void FileRenamer::moveFile(const QString &src, const QString &dest)
} }
} }
return; return;
QFile srcFile(src); QFile srcFile(src);
if(!srcFile.open(IO_ReadOnly)) { if(!srcFile.open(IO_ReadOnly)) {
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
#ifndef FILERENAMER_H #ifndef FILERENAMER_H
#define FILERENAMER_H #define FILERENAMER_H
#include <qstring.h> #include <kconfigbase.h>
class PlaylistItem; class PlaylistItem;
class Tag; class Tag;
...@@ -17,22 +17,41 @@ class Tag; ...@@ -17,22 +17,41 @@ class Tag;
class FileRenamer class FileRenamer
{ {
public: public:
enum TokenType {
Title, Artist, Album, Track, Comment
};
class Config
{
public:
Config(KConfigBase *cfg);
QString filenameScheme() const;
void setFilenameScheme(const QString &scheme);
QString getToken(TokenType type) const;
void setToken(TokenType type, const QString &value);
bool tokenNeedsValue(TokenType type) const;
void setTokenNeedsValue(TokenType type, bool needsValue);
private:
KConfigGroup m_grp;
};
static QString tokenToString(TokenType type);
FileRenamer(); FileRenamer();
FileRenamer(const PlaylistItem *item); 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); void rename(const PlaylistItem *item);
QString rename(const QString &filename, const Tag &tag) const; QString rename(const QString &filename, const Tag &tag) const;
private: private:
QString getToken(const QString &name, const QString &value) const; QString expandToken(TokenType type, const QString &value) const;
void moveFile(const QString &src, const QString &dest); void moveFile(const QString &src, const QString &dest);
Config m_cfg;
}; };
#endif // FILERENAMER_H #endif // FILERENAMER_H
......
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