File indexing completed on 2022-10-04 17:30:21

0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 // SPDX-FileCopyrightText: 2007 Dominik Seichter <domseichter@web.de>
0003 
0004 #include "progressdialog.h"
0005 
0006 #include "batchrenamer.h"
0007 #include "krenameimpl.h"
0008 
0009 #include <QMenu>
0010 
0011 #include <krun.h>
0012 
0013 ProgressDialog::ProgressDialog(ESplitMode eSplitMode, unsigned int dot, QWidget *parent)
0014     : QDialog(parent), m_canceled(false), m_renamer(nullptr), m_eSplitMode(eSplitMode), m_dot(dot)
0015 {
0016     m_widget.setupUi(this);
0017 
0018     m_buttonMore  = m_widget.buttonBox->addButton(i18n("&Rename more..."), QDialogButtonBox::ActionRole);
0019     m_buttonUndo  = m_widget.buttonBox->addButton(i18n("&Undo"), QDialogButtonBox::ActionRole);
0020     m_buttonDest  = m_widget.buttonBox->addButton(i18n("&Open Destination"), QDialogButtonBox::ActionRole);
0021     m_buttonClose = m_widget.buttonBox->button(QDialogButtonBox::Close);
0022 
0023     m_buttonUndo->setEnabled(false);
0024     m_buttonMore->setEnabled(false);
0025     m_buttonClose->setEnabled(false);
0026 
0027     connect(m_widget.buttonCancel, &QPushButton::clicked,
0028             this, &ProgressDialog::slotCancelled);
0029     connect(m_buttonDest, &QPushButton::clicked,
0030             this, &ProgressDialog::slotOpenDestination);
0031     connect(m_buttonUndo, &QPushButton::clicked,
0032             this, &ProgressDialog::slotUndo);
0033     connect(m_buttonClose, &QPushButton::clicked,
0034             qApp, &QApplication::quit, Qt::QueuedConnection);
0035 
0036     QMenu *menu = new QMenu(this);
0037     menu->addAction(i18n("Restart &KRename..."), this, &ProgressDialog::slotRestartKRename);
0038     menu->addSeparator();
0039     m_actProcessed = menu->addAction(i18n("Rename Processed Files &Again..."), this, &ProgressDialog::slotRenameProcessedAgain);
0040     m_actUnprocessed = menu->addAction(i18n("Rename &Unprocessed Files Again..."), this, &ProgressDialog::slotRenameUnprocessedAgain);
0041     menu->addAction(i18n("&Rename All Files Again..."), this, &ProgressDialog::slotRenameAllAgain);
0042 
0043     m_buttonMore->setMenu(menu);
0044 }
0045 
0046 void ProgressDialog::slotCancelled()
0047 {
0048     m_canceled = true;
0049 }
0050 
0051 void ProgressDialog::slotOpenDestination()
0052 {
0053     new KRun(m_dest, this);
0054 }
0055 
0056 void ProgressDialog::slotRestartKRename()
0057 {
0058     (void *)KRenameImpl::launch(QRect(0, 0, 0, 0), KRenameFile::List());
0059     QDialog::done(0);
0060 }
0061 
0062 void ProgressDialog::slotRenameProcessedAgain()
0063 {
0064     KRenameFile::List list;
0065     KRenameFile::List::const_iterator it = m_renamer->files()->begin();
0066 
0067     list.reserve(m_renamer->files()->count());
0068     while (it != m_renamer->files()->end()) {
0069         if (!(*it).hasError()) {
0070             KRenameFile file(m_renamer->buildDestinationUrl(*it), (*it).isDirectory(), m_eSplitMode, m_dot);
0071             file.setIcon(file.icon());
0072             list.append(file);
0073         }
0074 
0075         ++it;
0076     }
0077 
0078     (void *)KRenameImpl::launch(QRect(0, 0, 0, 0), list);
0079     QDialog::done(0);
0080 }
0081 
0082 void ProgressDialog::slotRenameUnprocessedAgain()
0083 {
0084     KRenameFile::List list;
0085     KRenameFile::List::const_iterator it = m_renamer->files()->begin();
0086 
0087     list.reserve(m_renamer->files()->count());
0088     while (it != m_renamer->files()->end()) {
0089         if ((*it).hasError()) {
0090             KRenameFile file(*it);
0091             file.setManualChanges(QString(), eManualChangeMode_None);   // reset manual changes!!
0092             list.append(file);
0093         }
0094 
0095         ++it;
0096     }
0097 
0098     (void *)KRenameImpl::launch(QRect(0, 0, 0, 0), list);
0099     QDialog::done(0);
0100 }
0101 
0102 void ProgressDialog::slotRenameAllAgain()
0103 {
0104     KRenameFile::List list;
0105     KRenameFile::List::const_iterator it = m_renamer->files()->begin();
0106 
0107     list.reserve(m_renamer->files()->count());
0108     while (it != m_renamer->files()->end()) {
0109         KRenameFile file(m_renamer->buildDestinationUrl(*it), (*it).isDirectory(), m_eSplitMode, m_dot);
0110         file.setIcon(file.icon());
0111         list.append(file);
0112         ++it;
0113     }
0114 
0115     (void *)KRenameImpl::launch(QRect(0, 0, 0, 0), list);
0116     QDialog::done(0);
0117 }
0118 
0119 void ProgressDialog::slotUndo()
0120 {
0121     if (m_renamer) {
0122         m_widget.buttonCancel->setEnabled(true);
0123 
0124         m_buttonUndo->setEnabled(false);
0125         m_buttonMore->setEnabled(false);
0126 
0127         m_renamer->undoFiles(this);
0128     }
0129 }
0130 
0131 void ProgressDialog::renamingDone(bool enableMore, bool enableUndo, BatchRenamer *renamer, int errors)
0132 {
0133     m_widget.buttonCancel->setEnabled(false);
0134 
0135     m_buttonUndo->setEnabled(enableUndo);
0136     m_buttonMore->setEnabled(enableMore);   // Do allow renaming processed files after undo
0137     m_buttonClose->setEnabled(true);
0138     m_buttonClose->setShortcut(QKeySequence(QKeySequence::Quit));
0139 
0140     m_actProcessed->setEnabled(renamer->files()->count() != errors);
0141     m_actUnprocessed->setEnabled(0 != errors);
0142 
0143     m_renamer = renamer;
0144 }
0145 
0146 void ProgressDialog::closeEvent(QCloseEvent *event)
0147 {
0148     // KRenameWindow is still around but hidden. When we close this dialog we
0149     // want to quit the app completely if there isn't some operation still
0150     // running.
0151     if (m_buttonClose->isEnabled()) {
0152         qApp->quit();
0153         QWidget::closeEvent(event);
0154     }
0155 }
0156 
0157 void ProgressDialog::print(const QString &text, const QString &pixmap)
0158 {
0159     QIcon icon = QIcon::fromTheme(pixmap);
0160 
0161     new QListWidgetItem(icon, text, m_widget.display);
0162 }
0163 
0164 void ProgressDialog::error(const QString &text)
0165 {
0166     this->print(text, "cancel");
0167 }
0168 
0169 void ProgressDialog::warning(const QString &text)
0170 {
0171     this->print(text, "help-hint");
0172 }