Commit e34eae1b authored by Frerich Raabe's avatar Frerich Raabe

- 80% of the "Rename & Move files based on tag information" thing. Lacks

  a configuration dialog. Currently the KAction in juk.cpp is commented out
  so that the merkin doesn't get anal about stuff which does not work yet.
  Also, there's a return in FileRenamer::moveFile() because I don't trust it
  yet.

svn path=/trunk/kdemultimedia/juk/; revision=219338
parent 2483b434
......@@ -10,7 +10,7 @@ juk_SOURCES = directorylistbase.ui genrelisteditorbase.ui \
genrelistlist.cpp genrelist.cpp genre.cpp player.cpp tag.cpp customaction.cpp \
slideraction.cpp keydialog.cpp juk.cpp main.cpp tagguesser.cpp \
tagguesserconfigdlgwidget.ui tagguesserconfigdlg.cpp jukIface.skel \
playlistsearch.cpp searchwidget.cpp
playlistsearch.cpp searchwidget.cpp filerenamer.cpp
tagguessertest_SOURCES = tagguessertest.cpp tagguesser.cpp
......
/*
* filerenamer.cpp - (c) 2003 Frerich Raabe <raabe@kde.org>
*
* 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
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include "filerenamer.h"
#include "playlistitem.h"
#include <kapplication.h>
#include <kconfig.h>
#include <kdebug.h>
#include <kdeversion.h>
#include <qdir.h>
#include <qfile.h>
FileRenamer::FileRenamer()
{
}
FileRenamer::FileRenamer(const PlaylistItem *item)
{
rename(item);
}
void FileRenamer::rename(const PlaylistItem *item)
{
if(item == 0 || item->tag() == 0)
return;
QString newFilename;
QString titleToken, artistToken, albumToken, trackToken, commentToken;
KConfig *cfg = kapp->config();
{
KConfigGroupSaver saver(cfg, "FileRenamer");
newFilename = cfg->readEntry("FilenameScheme", item->absFilePath());
if(!item->tag()->track().isNull()) {
titleToken = cfg->readEntry("Title token");
titleToken.replace("%s", item->tag()->track());
}
if(!item->tag()->artist().isNull()) {
artistToken = cfg->readEntry("Artist token");
artistToken.replace("%s", item->tag()->artist());
}
if(!item->tag()->album().isNull()) {
albumToken = cfg->readEntry("Album token");
albumToken.replace("%s", item->tag()->album());
}
if(item->tag()->trackNumber() > 0) {
trackToken = cfg->readEntry("Track token");
trackToken.replace("%s", item->tag()->trackNumberString());
}
if(!item->tag()->comment().isNull()) {
commentToken = cfg->readEntry("Comment token");
trackToken.replace("%s", item->tag()->comment());
}
}
QMap<QChar, QString> substitutions;
substitutions[ 't' ] = titleToken;
substitutions[ 'a' ] = artistToken;
substitutions[ 'A' ] = albumToken;
substitutions[ 'T' ] = trackToken;
substitutions[ 'c' ] = commentToken;
#if KDE_IS_VERSION(3,1,90)
newFilename = KMacroExpander::expandMacros(newFilename, substitutions);
#else
QMap<QChar, QString>::ConstIterator it = substitutions.begin();
QMap<QChar, QString>::ConstIterator end = substitutions.end();
for(; it != end; ++it)
newFilename.replace("%" + QString(it.key()), it.data());
#endif
newFilename = newFilename.stripWhiteSpace();
if(QFileInfo(newFilename).isRelative())
newFilename = item->absFilePath().left( item->absFilePath().findRev( "/" ) )
+ "/" + newFilename;
newFilename += "." + QFileInfo(item->absFilePath()).extension();
moveFile(item->absFilePath(), newFilename);
}
void FileRenamer::moveFile(const QString &src, const QString &dest)
{
kdDebug() << "Moving file " << src << " to " << dest << endl;
if(src == dest)
return;
QString dest_ = dest.mid(1); // strip the leading "/"
if(dest_.find("/") > 0) {
const QStringList components = QStringList::split("/", dest_.left( dest.findRev("/")));
QStringList::ConstIterator it = components.begin();
QStringList::ConstIterator end = components.end();
QString processedComponents;
for(; it != end; ++it) {
processedComponents += "/" + *it;
kdDebug() << "Checking path " << processedComponents << endl;
QDir dir(processedComponents);
if (!dir.exists())
kdDebug() << "Need to create " << processedComponents << endl;
}
}
return;
QFile srcFile(src);
if(!srcFile.open(IO_ReadOnly)) {
kdWarning() << "Could not open" << src << " for reading." << endl;
return;
}
QFile destFile(dest);
if(!destFile.open(IO_WriteOnly)) {
kdWarning() << "Could not open " << dest << " for writing." << endl;
return;
}
destFile.writeBlock(srcFile.readAll());
if(!srcFile.remove())
kdWarning() << "Could not delete source file " << src << endl;
}
/*
* filerenamer.h - (c) 2003 Frerich Raabe <raabe@kde.org>
*
* 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
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#ifndef FILERENAMER_H
#define FILERENAMER_H
class PlaylistItem;
class QString;
class FileRenamer
{
public:
FileRenamer();
FileRenamer(const PlaylistItem *item);
void rename(const PlaylistItem *item);
private:
void moveFile(const QString &src, const QString &dest);
};
#endif // FILERENAMER_H
// vim:ts=4:sw=4:noet
......@@ -149,6 +149,7 @@ void JuK::setupActions()
new KAction(i18n("&Save"), "filesave", "CTRL+t", m_splitter, SLOT(slotSaveTag()), actionCollection(), "saveItem");
new KAction(i18n("&Delete"), "editdelete", 0, m_splitter, SLOT(slotDeleteSelectedItems()), actionCollection(), "removeItem");
new KAction(i18n("&Guess Tag Information"), 0, "CTRL+g", m_splitter, SLOT(slotGuessTagInfo()), actionCollection(), "guessTag");
//new KAction(i18n("&Rename File"), 0, "CTRL+r", m_splitter, SLOT(slotRenameFile()), actionCollection(), "renameFile");
// settings menu
new KToggleAction(i18n("Show Menu Bar"), "CTRL+m", this, SLOT(slotToggleMenuBar()), actionCollection(), "toggleMenuBar");
......
......@@ -49,6 +49,7 @@
<Action name="saveItem"/>
<Action name="removeItem"/>
<Action name="guessTag"/>
<Action name="renameFile"/>
</Menu>
<Menu name="settings"><text>&amp;Settings</text>
<!-- <Action name="showGenreListEditor"/> -->
......
......@@ -421,6 +421,15 @@ void Playlist::clear()
clearItems(l);
}
void Playlist::slotRenameFile()
{
KApplication::setOverrideCursor(Qt::waitCursor);
PlaylistItemList items = selectedItems();
for(PlaylistItemList::Iterator it = items.begin(); it != items.end(); ++it)
(*it)->renameFile();
KApplication::restoreOverrideCursor();
}
void Playlist::slotGuessTagInfo()
{
KApplication::setOverrideCursor(Qt::waitCursor);
......
......@@ -156,6 +156,8 @@ public slots:
virtual void selectAll() { KListView::selectAll(true); }
void slotGuessTagInfo();
void slotRenameFile();
/**
* Reload the playlist contents from the m3u file.
*/
......
......@@ -20,6 +20,7 @@
#include "tagguesser.h"
#include "playlistitem.h"
#include "playlist.h"
#include "filerenamer.h"
#include "collectionlist.h"
////////////////////////////////////////////////////////////////////////////////
......@@ -94,6 +95,13 @@ void PlaylistItem::guessTagInfo()
slotRefresh();
}
void PlaylistItem::renameFile()
{
FileRenamer renamer;
renamer.rename( this );
slotRefresh();
}
////////////////////////////////////////////////////////////////////////////////
// PlaylistItem public slots
////////////////////////////////////////////////////////////////////////////////
......
......@@ -76,6 +76,7 @@ public:
void setPlaying(bool playing = true) { m_playing = playing; }
void guessTagInfo();
void renameFile();
public slots:
/**
......@@ -109,6 +110,7 @@ protected:
class Data;
Data *data() { return m_data; }
const Data *data() const { return m_data; }
void setData(Data *d) { m_data = d; }
virtual void paintCell(QPainter *p, const QColorGroup &cg, int column, int width, int align);
......
......@@ -360,6 +360,13 @@ void PlaylistSplitter::slotGuessTagInfo()
m_editor->slotRefresh();
}
void PlaylistSplitter::slotRenameFile()
{
visiblePlaylist()->slotRenameFile();
if(m_editor)
m_editor->slotRefresh();
}
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////
......
......@@ -174,6 +174,7 @@ public slots:
void slotSaveTag() { m_editor->save(); }
void slotGuessTagInfo();
void slotRenameFile();
// Playlist slots
......
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