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

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