/*
* This file is part of Catalog Builder - A movie catalog building software.
* Copyright (C) (2009) (Siva Chandran P) <siva.chandran.p@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <QFile>
#include <QStringList>
#include <QDebug>
#include "util.h"
Util *Util::m_Instance = NULL;
Util::Util()
:m_YearRegExp("\\d{4}"),
m_BracketsRegExp("(\\(.*\\))|(\\[.*\\])|(\\{.*\\})"),
m_StartTrimRegExp("^[ \\-_]*"),
m_MiddleTrimRegExp("\\.( )*\\."),
m_EndTrimRegExp("[ \\-_]*$"),
m_ImdbIdRegExp("tt\\d+")
{
QFile file("words-to-ignore.txt");
QByteArray contents;
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{
contents = file.readAll();
file.close();
}
else
{
qWarning("Can't open words-to-ignore.txt file");
return;
}
QStringList wordsToIgnore;
wordsToIgnore = QString(contents).split("\n", QString::SkipEmptyParts);
for (int j = 0; j < wordsToIgnore.size(); j++)
{
QString pattern;
pattern.append("((^|[ \\-\\._])" + wordsToIgnore[j] + "([ \\-\\._]|$))");
m_WordsRegExp.append(new QRegExp(pattern));
}
}
Util *Util::instance()
{
if (Util::m_Instance == NULL)
Util::m_Instance = new Util();
return m_Instance;
}
QString Util::stripFileName(QString fileName, int *year /*= NULL*/,
QString *imdbId /*= NULL*/)
{
int index;
// qDebug() << "Input file name: " << fileName;
// removes imdb id if exist
index = m_ImdbIdRegExp.indexIn(fileName);
if (index >= 0)
{
if (imdbId)
*imdbId = m_ImdbIdRegExp.cap(0);
fileName = fileName.replace(m_ImdbIdRegExp, " ");
}
// removes year if exist
index = m_YearRegExp.indexIn(fileName);
if (index >= 0)
{
int parsedYear = m_YearRegExp.cap(0).toInt();
if (parsedYear >= 1900)
{
if (year)
*year = parsedYear;
fileName = fileName.replace(m_YearRegExp, " ");
}
}
// qDebug() << "File name after removing year: " << fileName;
// removes anything inside brackets
index = m_BracketsRegExp.indexIn(fileName);
if (index >= 0)
fileName = fileName.replace(m_BracketsRegExp, " ");
// qDebug() << "File name after brackets year: " << fileName;
// removes all the ignored words if exist
for (int j = 0; j < m_WordsRegExp.size(); j++)
{
index = m_WordsRegExp[j]->indexIn(fileName);
if (index >= 0)
fileName = fileName.replace(*m_WordsRegExp[j], " ");
}
// qDebug() << "File name after ignored words: " << fileName;
// lets trim(remove blank spaces) the string
index = m_StartTrimRegExp.indexIn(fileName);
if (index >= 0)
fileName = fileName.remove(m_StartTrimRegExp);
index = m_MiddleTrimRegExp.indexIn(fileName);
if (index >= 0)
fileName = fileName.remove(m_MiddleTrimRegExp);
index = m_EndTrimRegExp.indexIn(fileName);
if (index >= 0)
fileName = fileName.remove(m_EndTrimRegExp);
// qDebug() << "File name after trimming: " << fileName;
// replace dot(.) or hyphen(-) with blank space( )
if (fileName.count(' ') == 0)
{
if (fileName.count('.') > 0)
fileName = fileName.replace('.', ' ');
else if (fileName.count('-') > 0)
fileName = fileName.replace('-', ' ');
fileName = fileName.trimmed();
}
return fileName;
}