Commit af7c6a00 authored by Frerich Raabe's avatar Frerich Raabe

- Almost going live with the file renamer

svn path=/trunk/kdemultimedia/juk/; revision=248853
parent d75180d3
...@@ -12,7 +12,9 @@ ...@@ -12,7 +12,9 @@
#include <kapplication.h> #include <kapplication.h>
#include <kconfig.h> #include <kconfig.h>
#include <kdebug.h> #include <kdebug.h>
#include <klocale.h>
#include <kmacroexpander.h> #include <kmacroexpander.h>
#include <kmessagebox.h>
#include <qdir.h> #include <qdir.h>
...@@ -92,7 +94,42 @@ void FileRenamer::rename(const PlaylistItem *item) ...@@ -92,7 +94,42 @@ void FileRenamer::rename(const PlaylistItem *item)
return; return;
QString newFilename = rename(item->absFilePath(), *item->tag()); QString newFilename = rename(item->absFilePath(), *item->tag());
moveFile(item->absFilePath(), newFilename); if(KMessageBox::warningContinueCancel(0,
i18n("You're about to rename the file '%1' to '%2'. Are you sure you "
"want to continue?").arg(item->absFilePath()).arg(newFilename),
i18n("Warning"), KStdGuiItem::cont(), "ShowFileRenamerWarning")
== KMessageBox::Continue) {
moveFile(item->absFilePath(), newFilename);
}
}
void FileRenamer::rename(const PlaylistItemList &items)
{
QMap<QString, QString> map;
QStringList filenames;
PlaylistItemList::ConstIterator it = items.begin();
for(; it != items.end(); ++it) {
const QString oldName = (*it)->absFilePath();
const QString newName = rename(oldName, *(*it)->tag());
filenames += oldName + "=>" + newName;
map[oldName] = newName;
}
if(KMessageBox::warningContinueCancelList(0, i18n("You're about to "
"rename the following files. Are you sure you want to continue?"),
filenames, i18n("Warning"), KStdGuiItem::cont(), "ShowFileRenamerWarning")
== KMessageBox::Continue) {
KApplication::setOverrideCursor(Qt::waitCursor);
int j = 1;
QMap<QString, QString>::ConstIterator it = map.begin();
for(; it != map.end(); ++it, ++j) {
moveFile(it.key(), it.data());
if(j % 5 == 0)
kapp->processEvents();
}
KApplication::restoreOverrideCursor();
}
} }
QString FileRenamer::rename(const QString &filename, const Tag &tag) const QString FileRenamer::rename(const QString &filename, const Tag &tag) const
...@@ -139,23 +176,24 @@ void FileRenamer::moveFile(const QString &src, const QString &dest) ...@@ -139,23 +176,24 @@ void FileRenamer::moveFile(const QString &src, const QString &dest)
} }
} }
return;
QFile srcFile(src); QFile srcFile(src);
if(!srcFile.open(IO_ReadOnly)) { if(!srcFile.open(IO_ReadOnly)) {
kdWarning() << "Could not open" << src << " for reading." << endl; KMessageBox::error(0, i18n("Could not open %1 for reading.").arg(src));
return; return;
} }
QFile destFile(dest); QFile destFile(dest);
if(!destFile.open(IO_WriteOnly)) { if(!destFile.open(IO_WriteOnly)) {
kdWarning() << "Could not open " << dest << " for writing." << endl; KMessageBox::error(0, i18n("Could not open %1 for writing.").arg(dest));
return; return;
} }
destFile.writeBlock(srcFile.readAll()); destFile.writeBlock(srcFile.readAll());
if(!srcFile.remove()) if(!srcFile.remove())
kdWarning() << "Could not delete source file " << src << endl; KMessageBox::sorry(0, i18n("Renamed the file, but failed the source "
"file %1. You might want to do so by "
"hand.").arg(src));
} }
// vim:ts=4:sw=4:noet
// vim:ts=4:sw=4:et
...@@ -10,8 +10,8 @@ ...@@ -10,8 +10,8 @@
#define FILERENAMER_H #define FILERENAMER_H
#include <kconfigbase.h> #include <kconfigbase.h>
#include "playlistitem.h"
class PlaylistItem;
class Tag; class Tag;
class FileRenamer class FileRenamer
...@@ -45,6 +45,7 @@ class FileRenamer ...@@ -45,6 +45,7 @@ class FileRenamer
FileRenamer(const PlaylistItem *item); FileRenamer(const PlaylistItem *item);
void rename(const PlaylistItem *item); void rename(const PlaylistItem *item);
void rename(const PlaylistItemList &items);
QString rename(const QString &filename, const Tag &tag) const; QString rename(const QString &filename, const Tag &tag) const;
private: private:
......
...@@ -405,8 +405,8 @@ void JuK::setupActions() ...@@ -405,8 +405,8 @@ void JuK::setupActions()
connect(m_guessMenu->popupMenu(), SIGNAL(activated(int)), this, SLOT(slotGuessTagInfo(int))); connect(m_guessMenu->popupMenu(), SIGNAL(activated(int)), this, SLOT(slotGuessTagInfo(int)));
// new KAction(i18n("&Rename File"), 0, "CTRL+r", m_splitter, SLOT(slotRenameFile()), new KAction(i18n("&Rename File"), 0, "CTRL+r", m_splitter, SLOT(slotRenameFile()),
// actionCollection(), "renameFile"); // 4 actionCollection(), "renameFile"); // 4
////////////////////////////////////////////////// //////////////////////////////////////////////////
// settings menu // settings menu
......
...@@ -42,6 +42,7 @@ ...@@ -42,6 +42,7 @@
#include "genrelistlist.h" #include "genrelistlist.h"
#include "mediafiles.h" #include "mediafiles.h"
#include "collectionlist.h" #include "collectionlist.h"
#include "filerenamer.h"
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Playlist::SharedSettings definition // Playlist::SharedSettings definition
...@@ -534,11 +535,15 @@ void Playlist::slotRefresh() ...@@ -534,11 +535,15 @@ void Playlist::slotRefresh()
void Playlist::slotRenameFile() void Playlist::slotRenameFile()
{ {
KApplication::setOverrideCursor(Qt::waitCursor);
PlaylistItemList items = selectedItems(); PlaylistItemList items = selectedItems();
for(PlaylistItemList::Iterator it = items.begin(); it != items.end(); ++it) if(items.count() == 1) {
(*it)->renameFile(); KApplication::setOverrideCursor(Qt::waitCursor);
KApplication::restoreOverrideCursor(); items[ 0 ]->renameFile();
KApplication::restoreOverrideCursor();
return;
}
FileRenamer renamer;
renamer.rename(items);
} }
void Playlist::slotGuessTagInfo(TagGuesser::Type type) void Playlist::slotGuessTagInfo(TagGuesser::Type type)
...@@ -1207,6 +1212,8 @@ void Playlist::slotShowRMBMenu(QListViewItem *item, const QPoint &point, int col ...@@ -1207,6 +1212,8 @@ void Playlist::slotShowRMBMenu(QListViewItem *item, const QPoint &point, int col
m_rmbMenu->insertSeparator(); m_rmbMenu->insertSeparator();
actionCollection->action("guessTag")->plug(m_rmbMenu); actionCollection->action("guessTag")->plug(m_rmbMenu);
if(!readOnly())
actionCollection->action("renameFile")->plug(m_rmbMenu);
m_rmbMenu->insertSeparator(); m_rmbMenu->insertSeparator();
m_rmbMenu->insertItem(SmallIcon("new"), i18n("Create Playlist From Selected Items"), this, SLOT(slotCreateGroup())); m_rmbMenu->insertItem(SmallIcon("new"), i18n("Create Playlist From Selected Items"), this, SLOT(slotCreateGroup()));
......
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