diff options
| author | trialuser02 <trialuser02@90c681e8-e032-0410-971d-27865f9a5e38> | 2009-05-11 12:31:48 +0000 |
|---|---|---|
| committer | trialuser02 <trialuser02@90c681e8-e032-0410-971d-27865f9a5e38> | 2009-05-11 12:31:48 +0000 |
| commit | 7c6e2dd399925d6c82ec3d55e6dc9e787cb252e6 (patch) | |
| tree | 6a007307cac9dab20af39a9d1c69776379660fe5 /src/plugins/General/fileops/fileops.cpp | |
| parent | f12c647031485d06eb2eb46b74a10f097f141fca (diff) | |
| download | qmmp-7c6e2dd399925d6c82ec3d55e6dc9e787cb252e6.tar.gz qmmp-7c6e2dd399925d6c82ec3d55e6dc9e787cb252e6.tar.bz2 qmmp-7c6e2dd399925d6c82ec3d55e6dc9e787cb252e6.zip | |
enabled file operations plugin
git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@936 90c681e8-e032-0410-971d-27865f9a5e38
Diffstat (limited to 'src/plugins/General/fileops/fileops.cpp')
| -rw-r--r-- | src/plugins/General/fileops/fileops.cpp | 106 |
1 files changed, 100 insertions, 6 deletions
diff --git a/src/plugins/General/fileops/fileops.cpp b/src/plugins/General/fileops/fileops.cpp index 3bb32b1e4..f116f7602 100644 --- a/src/plugins/General/fileops/fileops.cpp +++ b/src/plugins/General/fileops/fileops.cpp @@ -25,6 +25,7 @@ #include <QProgressDialog> #include <QMessageBox> #include <QFile> +#include <QDir> #include <qmmp/soundcore.h> #include <qmmpui/generalhandler.h> @@ -63,6 +64,7 @@ FileOps::FileOps(QObject *parent) m_patterns << settings.value(QString("pattern_%1").arg(i)).toString(); m_destinations << settings.value(QString("destination_%1").arg(i)).toString(); QAction *action = new QAction(name, this); + action->setShortcut(settings.value(QString("hotkey_%1").arg(i)).toString()); connect (action, SIGNAL (triggered (bool)), mapper, SLOT (map())); mapper->setMapping(action, i); GeneralHandler::instance()->addAction(action, GeneralHandler::PLAYLIST_MENU); @@ -89,6 +91,13 @@ void FileOps::execAction(int n) { case COPY: { + qDebug("FileOps: copy"); + if (!QDir(destination).exists ()) + { + QMessageBox::critical (qApp->activeWindow (), tr("Error"), + tr("Destination directory doesn't exist")); + break; + } QProgressDialog progress(qApp->activeWindow ()); progress.setWindowModality(Qt::WindowModal); progress.setWindowTitle(tr("Copying")); @@ -100,11 +109,25 @@ void FileOps::execAction(int n) { if (!QFile::exists(item->url())) continue; - + //generate file name + QString fname = generateFileName(item, pattern); + //append extension + QString ext = QString(".") + item->url().split('.',QString::SkipEmptyParts).takeLast (); + if (!fname.endsWith(ext)) + fname += ext; + //copy file QFile in(item->url()); - QFile out(destination + "/" + pattern); - in.open(QIODevice::ReadOnly); - out.open(QIODevice::WriteOnly); + QFile out(destination + "/" + fname); + if (!in.open(QIODevice::ReadOnly)) + { + qDebug("FileOps: %s", qPrintable(in.errorString ())); + continue; + } + if (!out.open(QIODevice::WriteOnly)) + { + qDebug("FileOps: %s", qPrintable(out.errorString ())); + continue; + } progress.setMaximum(int(in.size()/COPY_BLOCK_SIZE)); progress.setValue(0); @@ -123,10 +146,32 @@ void FileOps::execAction(int n) break; } case RENAME: + qDebug("FileOps: rename"); + foreach(PlayListItem *item, items) + { + if (!QFile::exists(item->url())) + continue; + //generate file name + QString fname = generateFileName(item, pattern); + //append extension + QString ext = QString(".") + item->url().split('.',QString::SkipEmptyParts).takeLast (); + if (!fname.endsWith(ext)) + fname += ext; + //rename file + QFile file(item->url()); + if (file.rename(destination + "/" + fname)) + { + item->setMetaData(Qmmp::URL, destination + "/" + fname); + model->doCurrentVisibleRequest(); + } + else + continue; + } break; - case MOVE: - break; + /*case MOVE: + break;*/ case REMOVE: + qDebug("FileOps: remove"); if (QMessageBox::question (qApp->activeWindow (), tr("Remove files"), QString(tr("Are you sure you want to remove %1 file(s) from disk")) .arg(items.size()), @@ -140,3 +185,52 @@ void FileOps::execAction(int n) } } } +//generate file name from tags using given pattern +QString FileOps::generateFileName(PlayListItem *item, QString pattern) +{ + QString fname = pattern; + fname = printTag(fname, "%p", item->artist(), pattern); + fname = printTag(fname, "%a", item->album(), pattern); + fname = printTag(fname, "%t", item->title(), pattern); + fname = printTag(fname, "%n", QString("%1").arg(item->track()), pattern); + fname = printTag(fname, "%g", item->genre(), pattern); + fname = printTag(fname, "%f", item->url().section('/',-1), pattern); + fname = printTag(fname, "%F", item->url(), pattern); + fname = printTag(fname, "%y", QString("%1").arg(item->year ()), pattern); + fname.replace(" ", "_"); + if (fname.isEmpty()) + { + if (item->url().contains('/')) + fname = item->url().split('/',QString::SkipEmptyParts).takeLast (); + } + return fname; + +} + + +QString FileOps::printTag(QString str, QString regExp, QString tagStr, QString fmt) +{ + QString format = fmt; + if (!tagStr.isEmpty()) + str.replace(regExp, tagStr); + else + { + //remove unused separators + int regExpPos = str.indexOf(regExp); + if (regExpPos < 0) + return str; + int nextPos = str.indexOf("%", regExpPos + 1); + if (nextPos < 0) + { + //last separator + regExpPos = format.lastIndexOf(regExp); + nextPos = format.lastIndexOf("%", regExpPos - 1); + QString lastSep = format.right (format.size() - nextPos - 2); + str.remove(lastSep); + str.remove(regExp); + } + else + str.remove ( regExpPos, nextPos - regExpPos); + } + return str; +} |
