File indexing completed on 2024-04-21 16:32:30

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