summaryrefslogtreecommitdiffstats
path: root/filesystemwidget.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'filesystemwidget.cpp')
-rw-r--r--filesystemwidget.cpp214
1 files changed, 213 insertions, 1 deletions
diff --git a/filesystemwidget.cpp b/filesystemwidget.cpp
index c424eaa..aafcd6d 100644
--- a/filesystemwidget.cpp
+++ b/filesystemwidget.cpp
@@ -17,6 +17,11 @@
#include <QCompleter>
#include <QProcess>
#include <QApplication>
+#include <QMessageBox>
+#include <QFile>
+#include <QAction>
+#include <QRegExp>
+
#include <QDebug>
#include "filesystemwidget.h"
@@ -25,6 +30,7 @@
#include "shemoviconprovider.h"
#include "filesystemfileproxy.h"
#include "helper.h"
+#include "messagedialog.h"
FilesystemWidget::FilesystemWidget(QWidget *parent) : QWidget(parent) {
mModel = new QDirModel;
@@ -53,6 +59,10 @@ FilesystemWidget::FilesystemWidget(QWidget *parent) : QWidget(parent) {
mFileView->setItemsExpandable(false);
mFileView->setSelectionMode(QAbstractItemView::MultiSelection);
+ QString title = QString(tr("%1 - Rename file")).arg(qApp->applicationName());
+ mRenameDialog = new MessageDialog(tr("Rename dummy to:"), title, this);
+ connect(mRenameDialog, SIGNAL(accepted()), this, SLOT(doRenameFile()));
+
QWidget *fileWidget = new QWidget;
QHBoxLayout *directoryEdit = new QHBoxLayout;
QLabel *dirLabel = new QLabel(tr("&Directory"));
@@ -155,7 +165,145 @@ void FilesystemWidget::parentDir(){
if(idx.parent().isValid()){
mDirView->setCurrentIndex(idx.parent());
}
- mFileView->selectionModel()->clearSelection();
+}
+
+void FilesystemWidget::deleteFiles(){
+ QSortFilterProxyModel *proxy = static_cast<QSortFilterProxyModel*>(mFileView->model());
+ QModelIndexList selected = mFileView->selectionModel()->selectedRows();
+ int count(0);
+ if(!selected.isEmpty()){
+ count = selected.count();
+ QAction *refresh = action(mFileView, "RE");
+ if(refresh){
+ // a refresh would invalidate selection
+ refresh->setEnabled(false);
+ }
+ QString message = QString(tr("Really delete %1 files?")).arg(QString::number(selected.count()));
+ int retval = QMessageBox::question(this, tr("Question"), message, QMessageBox::Yes | QMessageBox::No);
+ if(retval == QMessageBox::Yes){
+ foreach(QModelIndex idx, selected){
+ QModelIndex real = proxy->mapToSource(idx);
+ deleteRecursive(mModel->fileInfo(real));
+ }
+ }
+ mFileView->selectionModel()->clearSelection();
+ mModel->refresh(QModelIndex());
+ if(refresh){
+ refresh->setEnabled(true);
+ }
+ }else{
+ count = 1;
+ QModelIndex cur = mFileView->currentIndex();
+ QModelIndex real = proxy->mapToSource(cur);
+ QString message = QString(tr("Really delete %1?")).arg(mModel->fileName(real));
+ int retval = QMessageBox::question(this, tr("Question"), message, QMessageBox::Yes | QMessageBox::No);
+ if(retval == QMessageBox::Yes){
+ deleteRecursive(mModel->fileInfo(real));
+ mModel->refresh(real.parent());
+ }
+ }
+ QString message = QString(tr("Deleted %1 file(s)")).arg(count);
+ emit statusbarMessage(message);
+}
+
+void FilesystemWidget::copyFiles(){
+ QModelIndexList selected = mFileView->selectionModel()->selectedRows();
+ if(selected.isEmpty()){
+ emit statusbarMessage(tr("No files selected!"));
+ return;
+ }
+ QSortFilterProxyModel *proxy = static_cast<QSortFilterProxyModel*>(mFileView->model());
+ QModelIndex rootIndex = proxy->mapToSource(mFileView->rootIndex());
+ QFileInfo root = mModel->fileInfo(rootIndex);
+ QString message = QString(tr("Really copy %1 files to %2?")).arg(selected.count()).arg(root.absoluteFilePath());
+ int retval = QMessageBox::question(this, tr("Question"), message, QMessageBox::Yes | QMessageBox::No);
+ if(retval == QMessageBox::Yes){
+ QAction *refresh = action(mFileView, "RE");
+ if(refresh){
+ refresh->setEnabled(false);
+ }
+ int files(0), dirs(0), failed(0);
+ foreach(QModelIndex idx, selected){
+ QModelIndex real = proxy->mapToSource(idx);
+ QFileInfo info = mModel->fileInfo(real);
+ if(info.isDir()){
+ ++dirs;
+ copyRecursive(info, root.absoluteFilePath());
+ }else{
+ QString newFile = QString("%1/%2").arg(root.absoluteFilePath()).arg(info.fileName());
+ if(QFile::copy(info.absoluteFilePath(), newFile)){
+ ++files;
+ }else{
+ ++failed;
+ }
+ }
+ }
+ QString message = QString(tr("Successfully copied %1 files and %2 directories, %3 errors")).arg(files).arg(dirs).arg(failed);
+ statusbarMessage(message);
+ mFileView->selectionModel()->clearSelection();
+ mModel->refresh(rootIndex);
+ if(refresh){
+ refresh->setEnabled(true);
+ }
+ }
+}
+
+void FilesystemWidget::moveFiles(){
+ QModelIndexList selected = mFileView->selectionModel()->selectedRows();
+ if(selected.isEmpty()){
+ emit statusbarMessage(tr("No files selected!"));
+ return;
+ }
+ QSortFilterProxyModel *proxy = static_cast<QSortFilterProxyModel*>(mFileView->model());
+ QModelIndex rootIndex = proxy->mapToSource(mFileView->rootIndex());
+ QFileInfo root = mModel->fileInfo(rootIndex);
+ QString message = QString(tr("Really move %1 file(s) to %2?")).arg(selected.count()).arg(root.absoluteFilePath());
+ int retval = QMessageBox::question(this, tr("Question"), message, QMessageBox::Yes | QMessageBox::No);
+ if(retval == QMessageBox::Yes){
+ QAction *refresh = action(mFileView, "RE");
+ if(refresh){
+ refresh->setEnabled(false);
+ }
+ int success(0), failed(0);
+ foreach(QModelIndex cur, selected){
+ QModelIndex real = proxy->mapToSource(cur);
+ QFileInfo info = mModel->fileInfo(real);
+ QString dest = QString("%1/%2").arg(root.absoluteFilePath()).arg(info.fileName());
+ if(QFile::rename(info.absoluteFilePath(), dest)){
+ ++success;
+ }else{
+ ++failed;
+ }
+ }
+ QString message = QString(tr("Successfully moved %1 file(s), %2 errors")).arg(success).arg(failed);
+ emit statusbarMessage(message);
+ if(refresh){
+ refresh->setEnabled(true);
+ }
+ mFileView->selectionModel()->clearSelection();
+ mModel->refresh(QModelIndex());
+ }
+}
+
+void FilesystemWidget::renameFile(){
+ QModelIndex cur = mFileView->currentIndex();
+ if(!cur.isValid()){
+ emit statusbarMessage(tr("Nothing to rename!"));
+ return;
+ }
+ QSortFilterProxyModel *proxy = static_cast<QSortFilterProxyModel*>(mFileView->model());
+ QModelIndex source = proxy->mapToSource(cur);
+ QFileInfo info = mModel->fileInfo(source);
+ QString text = QString(tr("Rename %1 to:")).arg(info.fileName());
+ mRenameDialog->setMessage(text);
+ QString newFilename = info.fileName().toLower();
+ QRegExp re2("['#()&%!]+");
+ newFilename.replace(re2, " ");
+ QRegExp re1("\\s{2,}");
+ newFilename.replace(re1, " ");
+ newFilename.replace(' ', '.');
+ mRenameDialog->setText(newFilename);
+ mRenameDialog->exec();
}
void FilesystemWidget::setWindowTitle(const QString &dir){
@@ -163,3 +311,67 @@ void FilesystemWidget::setWindowTitle(const QString &dir){
emit windowTitle(mWindowTitle);
}
+void FilesystemWidget::deleteRecursive(const QFileInfo &start){
+ if(start.isDir()){
+ QDir curDir = QDir(start.absoluteFilePath());;
+ foreach(QFileInfo info, curDir.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot)){
+ if(info.isDir()){
+ deleteRecursive(info);
+ }else{
+ QFile::remove(info.absoluteFilePath());
+ }
+ }
+ QDir dir = start.absoluteDir();
+ dir.rmdir(start.fileName());
+ }else{
+ QFile::remove(start.absoluteFilePath());
+ }
+}
+
+void FilesystemWidget::copyRecursive(const QFileInfo &start, const QString &destdir){
+ if(!start.isDir()){
+ return;
+ }
+ QDir source(start.absoluteFilePath());
+ QDir dest = QDir(destdir);
+ if(!dest.exists(source.dirName())){
+ dest.mkdir(source.dirName());
+ }
+ QString d = QString("%1/%2").arg(destdir).arg(source.dirName());
+ foreach(QFileInfo cur, source.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot)){
+ if(cur.isDir()){
+ copyRecursive(cur, d);
+ }else{
+ QString destPath = QString("%1/%2").arg(d).arg(cur.fileName());
+ QFile::copy(cur.absoluteFilePath(), destPath);
+ }
+ }
+}
+
+void FilesystemWidget::doRenameFile(){
+ QString name = mRenameDialog->text();
+ QSortFilterProxyModel *proxy = static_cast<QSortFilterProxyModel*>(mFileView->model());
+ QModelIndex cur = mFileView->currentIndex();
+ QModelIndex real = proxy->mapToSource(cur);
+ QFileInfo info = mModel->fileInfo(real);
+ QString newName = QString("%1/%2").arg(info.absolutePath()).arg(name);
+ if(QFile::rename(info.absoluteFilePath(), newName)){
+ QString message = QString(tr("Renamed %1 to %2")).arg(info.absoluteFilePath()).arg(newName);
+ emit statusbarMessage(message);
+ mModel->refresh(real.parent());
+ }
+ QFileInfo n(newName);
+ mTemplate = n.completeBaseName();
+ emit newTemplate(mTemplate);
+}
+
+QAction * FilesystemWidget::action(QWidget *widget, const QVariant &data) const{
+ QAction *retval = 0;
+ foreach(QAction *a, widget->actions()){
+ if(a->data() == data){
+ retval = a;
+ break;
+ }
+ }
+ return retval;
+}