diff options
| author | trialuser02 <trialuser02@90c681e8-e032-0410-971d-27865f9a5e38> | 2015-06-11 09:21:08 +0000 |
|---|---|---|
| committer | trialuser02 <trialuser02@90c681e8-e032-0410-971d-27865f9a5e38> | 2015-06-11 09:21:08 +0000 |
| commit | 08d61df01bcf33bd7d1f7dcf0a56730e94381faa (patch) | |
| tree | c4f15911f9309ecc7baafc01d6b26bc1b5f2ac84 /src | |
| parent | 302e62bd7d92da84c7f3085b407916e6a7e8a1bb (diff) | |
| download | qmmp-08d61df01bcf33bd7d1f7dcf0a56730e94381faa.tar.gz qmmp-08d61df01bcf33bd7d1f7dcf0a56730e94381faa.tar.bz2 qmmp-08d61df01bcf33bd7d1f7dcf0a56730e94381faa.zip | |
fileops: added feature to move files (#132)
git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@5126 90c681e8-e032-0410-971d-27865f9a5e38
Diffstat (limited to 'src')
| -rw-r--r-- | src/plugins/General/fileops/fileops.cpp | 120 | ||||
| -rw-r--r-- | src/plugins/General/fileops/fileops.h | 3 | ||||
| -rw-r--r-- | src/plugins/General/fileops/settingsdialog.cpp | 9 |
3 files changed, 118 insertions, 14 deletions
diff --git a/src/plugins/General/fileops/fileops.cpp b/src/plugins/General/fileops/fileops.cpp index 3b5e394ca..ffce45a0c 100644 --- a/src/plugins/General/fileops/fileops.cpp +++ b/src/plugins/General/fileops/fileops.cpp @@ -113,9 +113,10 @@ void FileOps::execAction(int n) //generate file name QString fname = formatter.format(item); //append extension - QString ext = QString(".") + item->url().split('.',QString::SkipEmptyParts).takeLast (); - if (!fname.endsWith(ext)) + QString ext = QString(".") + item->url().section(".", -1).toLower(); + if (!ext.isEmpty() && !fname.endsWith(ext, Qt::CaseInsensitive)) fname += ext; + //create destination path QString path = destination + "/" + fname; QDir dir = QFileInfo(path).dir(); if(!dir.exists()) @@ -147,11 +148,12 @@ void FileOps::execAction(int n) while (!in.atEnd ()) { - progress.wasCanceled (); out.write(in.read(COPY_BLOCK_SIZE)); progress.setValue(int(out.size()/COPY_BLOCK_SIZE)); qApp->processEvents(); } + if(progress.wasCanceled ()) + break; } progress.close(); break; @@ -160,13 +162,13 @@ void FileOps::execAction(int n) qDebug("FileOps: rename"); foreach(PlayListTrack *item, tracks) { - if (!QFile::exists(item->url())) + if (!QFile::exists(item->url())) //is it file? continue; //generate file name QString fname = formatter.format(item); //append extension - QString ext = QString(".") + item->url().split('.',QString::SkipEmptyParts).takeLast (); - if (!fname.endsWith(ext)) + QString ext = QString(".") + item->url().section(".", -1).toLower(); + if (!ext.isEmpty() && !fname.endsWith(ext, Qt::CaseInsensitive)) fname += ext; //rename file QFile file(item->url()); @@ -180,8 +182,7 @@ void FileOps::execAction(int n) continue; } break; - /*case MOVE: - break;*/ + case REMOVE: qDebug("FileOps: remove"); if (QMessageBox::question (qApp->activeWindow (), tr("Remove Files"), @@ -195,5 +196,108 @@ void FileOps::execAction(int n) if (QFile::exists(track->url()) && QFile::remove(track->url())) model->removeTrack(track); } + break; + + case MOVE: + { + qDebug("FileOps: move"); + if (!QDir(destination).exists ()) + { + QMessageBox::critical (qApp->activeWindow (), tr("Error"), + tr("Destination directory doesn't exist")); + break; + } + if (QMessageBox::question (qApp->activeWindow (), tr("Move Files"), + tr("Are you sure you want to move %n file(s)?", + "",tracks.size()), + QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes) + { + break; + } + QProgressDialog progress(qApp->activeWindow ()); + progress.setWindowModality(Qt::WindowModal); + progress.setWindowTitle(tr("Renaming")); + progress.setCancelButtonText(tr("Stop")); + progress.show(); + progress.setAutoClose (false); + int i = 0; + foreach(PlayListTrack *item, tracks) + { + if (!QFile::exists(item->url())) + continue; + //generate file name + QString fname = formatter.format(item); + //append extension + QString ext = QString(".") + item->url().section(".", -1).toLower(); + if (!ext.isEmpty() && !fname.endsWith(ext, Qt::CaseInsensitive)) + fname += ext; + //create destination path + QString path = destination + "/" + fname; + //skip moved files + if(path == item->url()) + continue; + + QDir dir = QFileInfo(path).dir(); + if(!dir.exists()) + { + if(!dir.mkpath(dir.absolutePath())) + { + qWarning("FileOps: unable to create directory"); + continue; + } + } + + progress.setRange(0, 100); + progress.setValue(0); + progress.setLabelText (QString(tr("Moving file %1/%2")).arg(++i).arg(tracks.size())); + progress.update(); + //try to rename file first + if(QFile::rename(item->url(), path)) + { + progress.setValue(100); + item->insert(Qmmp::URL, path); + model->doCurrentVisibleRequest(); + continue; + } + //copy file + QFile in(item->url()); + QFile out(path); + if (!in.open(QIODevice::ReadOnly)) + { + qWarning("FileOps: %s", qPrintable(in.errorString ())); + continue; + } + if (!out.open(QIODevice::WriteOnly)) + { + qWarning("FileOps: %s", qPrintable(out.errorString ())); + continue; + } + + progress.setMaximum(int(in.size()/COPY_BLOCK_SIZE)); + progress.setValue(0); + progress.update(); + + while (!in.atEnd ()) + { + progress.wasCanceled (); + out.write(in.read(COPY_BLOCK_SIZE)); + progress.setValue(int(out.size()/COPY_BLOCK_SIZE)); + qApp->processEvents(); + } + + in.close(); + + if(!QFile::remove(item->url())) + qWarning("FileOps: unable to remove file '%s'", qPrintable(item->url())); + + item->insert(Qmmp::URL, path); + model->doCurrentVisibleRequest(); + + if(progress.wasCanceled()) + break; + } + progress.close(); + break; + } } } diff --git a/src/plugins/General/fileops/fileops.h b/src/plugins/General/fileops/fileops.h index fdd87182f..23677a0c2 100644 --- a/src/plugins/General/fileops/fileops.h +++ b/src/plugins/General/fileops/fileops.h @@ -43,7 +43,8 @@ public: { COPY = 0, RENAME, - REMOVE + REMOVE, + MOVE, }; private slots: diff --git a/src/plugins/General/fileops/settingsdialog.cpp b/src/plugins/General/fileops/settingsdialog.cpp index 2b0a53204..008c9997c 100644 --- a/src/plugins/General/fileops/settingsdialog.cpp +++ b/src/plugins/General/fileops/settingsdialog.cpp @@ -17,13 +17,12 @@ * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * ***************************************************************************/ -#include <QTextCodec> #include <QSettings> -#include <QCheckBox> -#include <QComboBox> #include <QMenu> #include <QHeaderView> #include <QApplication> +#include <QCheckBox> +#include <QComboBox> #include <QDesktopServices> #include <qmmp/qmmp.h> #include <qmmpui/filedialog.h> @@ -52,7 +51,7 @@ SettingsDialog::SettingsDialog(QWidget *parent) QComboBox *comboBox = new QComboBox; comboBox->addItem (tr("Copy"), FileOps::COPY); comboBox->addItem (tr("Rename"), FileOps::RENAME); - //comboBox->addItem (tr("Move"), FileOps::MOVE); + comboBox->addItem (tr("Move"), FileOps::MOVE); comboBox->addItem (tr("Remove"), FileOps::REMOVE); comboBox->setFocusPolicy (Qt::NoFocus); @@ -132,7 +131,7 @@ void SettingsDialog::createAction() QComboBox *comboBox = new QComboBox; comboBox->addItem (tr("Copy"), FileOps::COPY); comboBox->addItem (tr("Rename"), FileOps::RENAME); - //comboBox->addItem (tr("Move"), FileOps::MOVE); + comboBox->addItem (tr("Move"), FileOps::MOVE); comboBox->addItem (tr("Remove"), FileOps::REMOVE); comboBox->setFocusPolicy (Qt::NoFocus); |
