File indexing completed on 2022-11-29 20:25:17

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