File indexing completed on 2024-12-01 05:19:26

0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 // SPDX-FileCopyrightText: 2010 Matteo Azzali <matte.azzali@NOSPAMalice.it>
0003 
0004 #include "snumplugin.h"
0005 
0006 #include "batchrenamer.h"
0007 
0008 #include <KLocalizedString>
0009 
0010 #include <QRegExp>
0011 
0012 SnumPlugin::SnumPlugin(PluginLoader *loader)
0013     : FilePlugin(loader)
0014 {
0015     this->addSupportedToken("snum");
0016     this->addSupportedToken("season");
0017     this->addSupportedToken("episode");
0018     //this->addSupportedToken("season;.*");
0019     //this->addSupportedToken("episode;.*");
0020 
0021     m_help.append("[snum];;" + i18n("Inserts the series number of original filename"));
0022     m_help.append("[season];;" + i18n("Inserts the season number in two digits"));
0023     m_help.append("[episode];;" + i18n("Inserts the episode number in two or three digits"));
0024     //m_help.append( "[season;pad];;" + i18n("Inserts the season number in at least pad digits") );
0025     //m_help.append( "[episode;pad];;" + i18n("Inserts the episode number in at least pad digits") );
0026 
0027     m_name = i18n("SeriesNumber");
0028     m_icon = "video-television";
0029     m_comment = i18n("<qt>This plugin can extract information from the filename "
0030                      "of a TV series.</qt>");
0031 
0032 }
0033 
0034 SnumPlugin::~SnumPlugin()
0035 {
0036 
0037 }
0038 
0039 QString SnumPlugin::processFile(BatchRenamer *b, int index, const QString &filenameOrToken, EPluginType eCurrentType)
0040 {
0041     QString src;
0042     QString token;
0043 
0044     // This plugin supports to types
0045     if (eCurrentType == ePluginType_Token) {
0046 
0047         token = filenameOrToken.toLower();
0048 
0049         if (token == "snum") {
0050             src = b->files()->at(index).srcFilename();
0051 
0052             return this->extractnum(src , 0);
0053         } else if (token == "season") {
0054             src = b->files()->at(index).srcFilename();
0055 
0056             return this->extractnum(src , 1);
0057         } else if (token == "episode") {
0058             src = b->files()->at(index).srcFilename();
0059 
0060             return this->extractnum(src , 2);
0061         }
0062     }
0063 
0064     return QString();
0065 }
0066 
0067 QString SnumPlugin::extractnum(const QString &unicoded, int a)
0068 {
0069     int pos;
0070     QString tmp = "";
0071     QString seriesnum = "";
0072     QString season = "";
0073     QString episode = "";
0074 
0075     QRegExp rx("(\\d{1,2})[^\\d]{1}(\\d{1,3})");
0076     pos = 0;
0077     pos = rx.indexIn(unicoded, pos);
0078     if (pos > -1) {
0079         season += rx.cap(1);
0080         episode += rx.cap(2);
0081         pos  += rx.matchedLength();
0082     } else {
0083         QRegExp px("(\\d{1,2})[^\\d]{2}(\\d{1,3})");
0084         pos = 0;
0085         pos = px.indexIn(unicoded, pos);
0086         if (pos > -1) {
0087             season += px.cap(1);
0088             episode += px.cap(2);
0089             pos  += px.matchedLength();
0090         } else {
0091             QRegExp gx("(\\d{1,2})[^\\d]{0}(\\d{2})");
0092             pos = 0;
0093             pos = gx.indexIn(unicoded, pos);
0094             if (pos > -1) {
0095                 season += gx.cap(1);
0096                 episode += gx.cap(2);
0097                 pos  += gx.matchedLength();
0098             }
0099         }
0100     }
0101 
0102     if (season.length() == 1) {
0103         tmp = '0' + season;
0104     } else {
0105         tmp = season;
0106     }
0107     season = tmp;
0108 
0109     if (episode.length() == 1) {
0110         tmp = '0' + episode;
0111     } else {
0112         tmp = episode;
0113     }
0114     episode = tmp;
0115 
0116     seriesnum += season + 'e' + episode;
0117 
0118     if (a == 0) {
0119         return seriesnum;
0120     } else if (a == 1) {
0121         return season;
0122     } else {
0123         return episode;
0124     }
0125 }
0126