Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

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 @@
#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);
if(value.isEmpty() && needContent)
return QString();
void FileRenamer::Config::setToken(TokenType type, const QString &value)
{
m_grp.writeEntry(tokenToString(type) + "Token", value);
}
QString token = grp.readEntry(name + "Token");
token.replace("%s", value);
return token;
bool FileRenamer::Config::tokenNeedsValue(TokenType type) const
{
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)
{
if(item == 0 || item->tag() == 0)
......@@ -79,21 +97,21 @@ void FileRenamer::rename(const PlaylistItem *item)
QString FileRenamer::rename(const QString &filename, const Tag &tag) const
{
QString newFilename = filenameScheme();
QString newFilename = m_cfg.filenameScheme();
QMap<QChar, QString> substitutions;
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());
substitutions[ 't' ] = expandToken(Title, tag.track());
substitutions[ 'a' ] = expandToken(Artist, tag.artist());
substitutions[ 'A' ] = expandToken(Album, tag.album());
substitutions[ 'T' ] = expandToken(Track, tag.trackNumberString());
substitutions[ 'c' ] = expandToken(Comment, tag.comment());
newFilename = KMacroExpander::expandMacros(newFilename, substitutions);
newFilename = newFilename.stripWhiteSpace();
if(QFileInfo(newFilename).isRelative())
newFilename = filename.left( filename.findRev( "/" ) )
+ "/" + newFilename;
+ "/" + newFilename;
newFilename += "." + QFileInfo(filename).extension();
return newFilename;
......@@ -121,7 +139,7 @@ void FileRenamer::moveFile(const QString &src, const QString &dest)
}
}
return;
return;
QFile srcFile(src);
if(!srcFile.open(IO_ReadOnly)) {
......
......@@ -9,7 +9,7 @@
#ifndef FILERENAMER_H
#define FILERENAMER_H
#include <qstring.h>
#include <kconfigbase.h>
class PlaylistItem;
class Tag;
......@@ -17,22 +17,41 @@ class Tag;
class FileRenamer
{
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(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;
QString expandToken(TokenType type, const QString &value) const;
void moveFile(const QString &src, const QString &dest);
Config m_cfg;
};
#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