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

0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 // SPDX-FileCopyrightText: 2009 Dominik Seichter <domseichter@web.de>
0003 
0004 // Own includes
0005 #include "dirsortplugin.h"
0006 
0007 #include "batchrenamer.h"
0008 
0009 // KDE includes
0010 #include <kio/job.h>
0011 #include <kmessagebox.h>
0012 #include <KJobWidgets>
0013 #include <kio_version.h>
0014 
0015 DirSortPlugin::DirSortPlugin(PluginLoader *loader)
0016     : Plugin(loader)
0017 {
0018     m_widget = new Ui::DirSortPluginWidget();
0019 }
0020 
0021 DirSortPlugin::~DirSortPlugin()
0022 {
0023     delete m_widget;
0024 }
0025 
0026 const QString DirSortPlugin::name() const
0027 {
0028     return i18n("Subfolder-Sort Plugin");
0029 }
0030 
0031 int DirSortPlugin::type() const
0032 {
0033     return ePluginType_File;
0034 }
0035 
0036 bool DirSortPlugin::enabledByDefault() const
0037 {
0038     return false;
0039 }
0040 
0041 const QIcon DirSortPlugin::icon() const
0042 {
0043     return QIcon::fromTheme("folder");
0044 }
0045 
0046 void DirSortPlugin::createUI(QWidget *parent) const
0047 {
0048     m_widget->setupUi(parent);
0049 
0050     m_widget->outputUrl->setMode(KFile::Directory | KFile::ExistingOnly);
0051 }
0052 
0053 /*
0054 void DirSortPlugin::fillStructure()
0055 {
0056     fpd = spinFiles->value();
0057     fpd--;
0058     dir = outputdir->text();
0059 
0060     filecounter = 0;
0061     dircounter = spinStart->value();
0062     curdir = dir +  QString("/%1/").arg( dircounter );
0063     d = new QDir( dir );
0064     d->mkdir( curdir );
0065 }
0066 */
0067 
0068 QString DirSortPlugin::processFile(BatchRenamer *b, int index, const QString &, EPluginType)
0069 {
0070     QString errorMsg;
0071 
0072     if (index == 0) {
0073         // Initialize plugin
0074         m_dirCounter = m_widget->spinStart->value();
0075         m_fileCounter = 0;
0076         m_filesPerDir = m_widget->spinFiles->value();
0077         m_digits = m_widget->spinDigits->value();
0078         m_baseDirectory = m_widget->outputUrl->url();
0079 
0080 #if KIO_VERSION >= QT_VERSION_CHECK(5, 69, 0)
0081         KIO::StatJob *statJob = KIO::statDetails(m_baseDirectory, KIO::StatJob::DestinationSide, KIO::StatNoDetails);
0082 #else
0083         KIO::StatJob *statJob = KIO::stat(m_baseDirectory, KIO::StatJob::DestinationSide, 0);
0084 #endif
0085         KJobWidgets::setWindow(statJob, m_widget->spinStart);
0086         statJob->exec();
0087         if (statJob->error()) {
0088             m_valid = false;
0089             return
0090                 i18n("%1: The output folder %2 does not exist.",
0091                      this->name(),
0092                      m_baseDirectory.toDisplayString(QUrl::PreferLocalFile));
0093         } else {
0094             m_valid = true;
0095 
0096             m_currentDirectory = createNewSubdirectory();
0097         }
0098     }
0099 
0100     if (!m_valid) {
0101         return errorMsg;
0102     }
0103 
0104     if (m_fileCounter == m_filesPerDir) {
0105         m_fileCounter = 0;
0106         m_dirCounter++;
0107 
0108         m_currentDirectory = createNewSubdirectory();
0109     }
0110 
0111     QUrl srcUrl = b->buildDestinationUrl((*b->files())[index]);
0112     QUrl dstUrl = m_currentDirectory;
0113     dstUrl = dstUrl.adjusted(QUrl::StripTrailingSlash);
0114     dstUrl.setPath(dstUrl.path() + '/' + (srcUrl.fileName()));
0115     KIO::JobFlags flags = KIO::DefaultFlags | KIO::HideProgressInfo;
0116     KIO::Job *job = KIO::file_move(srcUrl, dstUrl, -1, flags);
0117     m_fileCounter++;
0118     KJobWidgets::setWindow(job, m_widget->spinStart);
0119     job->exec();
0120     if (!job->exec()) {
0121         errorMsg = i18n("Error renaming %2 (to %1)",
0122                         dstUrl.toDisplayString(QUrl::PreferLocalFile),
0123                         srcUrl.toDisplayString(QUrl::PreferLocalFile));
0124     }
0125 
0126     return errorMsg;
0127 }
0128 
0129 const QStringList &DirSortPlugin::supportedTokens() const
0130 {
0131     return m_emptyList;
0132 }
0133 
0134 const QStringList &DirSortPlugin::help() const
0135 {
0136     return m_emptyList;
0137 }
0138 
0139 QUrl DirSortPlugin::createNewSubdirectory() const
0140 {
0141     QUrl url = m_baseDirectory;
0142 
0143     QString dir;
0144     dir.sprintf("%0*i", m_digits, m_dirCounter);
0145     url = url.adjusted(QUrl::StripTrailingSlash);
0146     url.setPath(url.path() + '/' + (dir));
0147 
0148     KIO::MkdirJob *job = KIO::mkdir(url);
0149     KJobWidgets::setWindow(job, m_widget->groupBox); // we just need a random widget, FIXME use the proper parent
0150 
0151     if (!job->exec()) {
0152         KMessageBox::error(m_widget->groupBox,
0153                            i18n("Cannot create folder %1", url.toDisplayString(QUrl::PreferLocalFile)));
0154     }
0155 
0156     return url;
0157 }