Skip to content

Commit

Permalink
[NameFormatter] Remove singleton for formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
bugwelle committed May 13, 2020
1 parent 1f43253 commit c7ccd57
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 52 deletions.
16 changes: 8 additions & 8 deletions src/concerts/ConcertController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ bool ConcertController::loadData(MediaCenterInterface* mediaCenterInterface, boo
}

m_concert->blockSignals(true);
NameFormatter* nameFormat = NameFormatter::instance();
NameFormatter nameFormatter;

bool infoLoaded;
if (reloadFromNfo) {
Expand All @@ -79,7 +79,7 @@ bool ConcertController::loadData(MediaCenterInterface* mediaCenterInterface, boo
pathElements.removeLast();
}
if (!pathElements.isEmpty()) {
m_concert->setName(nameFormat->formatName(pathElements.last()));
m_concert->setName(nameFormatter.formatName(pathElements.last()));
}
} else if (QString::compare(fi.fileName(), "index.bdmv", Qt::CaseInsensitive) == 0) {
QStringList pathElements = QDir::toNativeSeparators(fi.path()).split(QDir::separator());
Expand All @@ -88,24 +88,24 @@ bool ConcertController::loadData(MediaCenterInterface* mediaCenterInterface, boo
pathElements.removeLast();
}
if (!pathElements.isEmpty()) {
m_concert->setName(nameFormat->formatName(pathElements.last()));
m_concert->setName(nameFormatter.formatName(pathElements.last()));
}
} else if (m_concert->inSeparateFolder()) {
QStringList splitted = QDir::toNativeSeparators(fi.path()).split(QDir::separator());
if (!splitted.isEmpty()) {
m_concert->setName(nameFormat->formatName(splitted.last()));
m_concert->setName(nameFormatter.formatName(splitted.last()));
} else {
if (m_concert->files().size() > 1) {
m_concert->setName(nameFormat->formatName(nameFormat->formatParts(fi.completeBaseName())));
m_concert->setName(nameFormatter.formatName(nameFormatter.formatParts(fi.completeBaseName())));
} else {
m_concert->setName(nameFormat->formatName(fi.completeBaseName()));
m_concert->setName(nameFormatter.formatName(fi.completeBaseName()));
}
}
} else {
if (m_concert->files().size() > 1) {
m_concert->setName(nameFormat->formatName(nameFormat->formatParts(fi.completeBaseName())));
m_concert->setName(nameFormatter.formatName(nameFormatter.formatParts(fi.completeBaseName())));
} else {
m_concert->setName(nameFormat->formatName(fi.completeBaseName()));
m_concert->setName(nameFormatter.formatName(fi.completeBaseName()));
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/globals/ImageDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,8 @@ QString ImageDialog::formatSearchText(const QString& text)
QString fText = text;
fText.replace(" - ", " ");
fText.replace("-", " ");
fText = NameFormatter::instance()->formatName(fText);
NameFormatter format;
fText = format.formatName(fText);
return fText;
}

Expand Down
21 changes: 3 additions & 18 deletions src/globals/NameFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,7 @@

NameFormatter::NameFormatter(QObject* parent) : QObject(parent)
{
onUpdateExcludeWords();
connect(Settings::instance(), &Settings::sigSettingsSaved, this, &NameFormatter::onUpdateExcludeWords);
}

/**
* @brief Returns an instance of the name formatter
* @param parent Parent widget
* @return Instance of name formatter
*/
NameFormatter* NameFormatter::instance(QObject* parent)
{
static auto* s_formatterInstance = new NameFormatter(parent);
return s_formatterInstance;
updateExcludeWords();
}

/**
Expand All @@ -39,10 +27,7 @@ QString NameFormatter::excludeWords(QString name)
pos = rx.indexIn(name);
}

QStringList braces = QStringList() << "("
<< ")"
<< "["
<< "]";
const QStringList braces = {"(", ")", "[", "]"};
if (braces.contains(word)) {
name.replace(word, "");
}
Expand Down Expand Up @@ -109,7 +94,7 @@ QString NameFormatter::formatParts(QString name)
return name;
}

void NameFormatter::onUpdateExcludeWords()
void NameFormatter::updateExcludeWords()
{
m_exWords = Settings::instance()->excludeWords().remove(" ").split(",", QString::SkipEmptyParts);
std::sort(m_exWords.begin(), m_exWords.end(), NameFormatter::lengthLessThan);
Expand Down
10 changes: 3 additions & 7 deletions src/globals/NameFormatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,20 @@
#include <QObject>
#include <QStringList>

/**
* @brief The NameFormatter class
*/
class NameFormatter : public QObject
{
Q_OBJECT
public:
explicit NameFormatter(QObject* parent = nullptr);
static NameFormatter* instance(QObject* parent = nullptr);

QString excludeWords(QString name);
QString formatName(QString name, bool replaceDots = true, bool replaceUnderscores = true);
QString formatParts(QString name);

private slots:
void onUpdateExcludeWords();
private:
void updateExcludeWords();
static bool lengthLessThan(const QString& s1, const QString& s2);

private:
QStringList m_exWords;
static bool lengthLessThan(const QString& s1, const QString& s2);
};
16 changes: 8 additions & 8 deletions src/movies/MovieController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ bool MovieController::loadData(MediaCenterInterface* mediaCenterInterface, bool
}

m_movie->blockSignals(true);
NameFormatter* nameFormat = NameFormatter::instance();
NameFormatter nameFormatter;

bool infoLoaded;
if (reloadFromNfo) {
Expand All @@ -87,7 +87,7 @@ bool MovieController::loadData(MediaCenterInterface* mediaCenterInterface, bool
pathElements.removeLast();
}
if (!pathElements.isEmpty()) {
m_movie->setName(nameFormat->formatName(pathElements.last(), false));
m_movie->setName(nameFormatter.formatName(pathElements.last(), false));
}
} else if (QString::compare(fi.fileName(), "index.bdmv", Qt::CaseInsensitive) == 0) {
QStringList pathElements = QDir::toNativeSeparators(fi.path()).split(QDir::separator());
Expand All @@ -96,24 +96,24 @@ bool MovieController::loadData(MediaCenterInterface* mediaCenterInterface, bool
pathElements.removeLast();
}
if (!pathElements.isEmpty()) {
m_movie->setName(nameFormat->formatName(pathElements.last(), false));
m_movie->setName(nameFormatter.formatName(pathElements.last(), false));
}
} else if (m_movie->inSeparateFolder()) {
QStringList splitted = QDir::toNativeSeparators(fi.path()).split(QDir::separator());
if (!splitted.isEmpty()) {
m_movie->setName(nameFormat->formatName(splitted.last(), false));
m_movie->setName(nameFormatter.formatName(splitted.last(), false));
} else {
if (m_movie->files().size() > 1) {
m_movie->setName(nameFormat->formatName(nameFormat->formatParts(fi.completeBaseName())));
m_movie->setName(nameFormatter.formatName(nameFormatter.formatParts(fi.completeBaseName())));
} else {
m_movie->setName(nameFormat->formatName(fi.completeBaseName()));
m_movie->setName(nameFormatter.formatName(fi.completeBaseName()));
}
}
} else {
if (m_movie->files().size() > 1) {
m_movie->setName(nameFormat->formatName(nameFormat->formatParts(fi.completeBaseName())));
m_movie->setName(nameFormatter.formatName(nameFormatter.formatParts(fi.completeBaseName())));
} else {
m_movie->setName(nameFormat->formatName(fi.completeBaseName()));
m_movie->setName(nameFormatter.formatName(fi.completeBaseName()));
}
}
QRegExp rx("(tt[0-9]+)");
Expand Down
12 changes: 6 additions & 6 deletions src/movies/MovieFilesOrganizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ void MovieFilesOrganizer::moveToDirs(mediaelch::DirectoryPath dir)
}

QVector<QStringList> contents;
auto fileSearcher = new mediaelch::MovieFileSearcher(this);
auto* fileSearcher = new mediaelch::MovieFileSearcher(this);
fileSearcher->scanDir(path, path, contents, false, true);
fileSearcher->deleteLater();

const int pos = path.lastIndexOf(QDir::separator());
QString dirName = path.right(path.length() - pos - 1);
QString fileName;
NameFormatter* nameFormat = NameFormatter::instance(this);

NameFormatter nameFormatter;

for (const QStringList& movie : contents) {
const int movieIndex = movie.at(0).lastIndexOf(QDir::separator());
if (!(movie.at(0).left(movieIndex).endsWith(dirName))) {
qDebug() << "skipping " << movie.at(0);
qDebug() << "[MovieFilesOrganizer] skipping " << movie.at(0);
continue;
}

Expand All @@ -48,9 +48,9 @@ void MovieFilesOrganizer::moveToDirs(mediaelch::DirectoryPath dir)

QString newFolder;
if (movie.length() == 1) {
newFolder = path + QDir::separator() + nameFormat->formatName(fileName);
newFolder = path + QDir::separator() + nameFormatter.formatName(fileName);
} else if (movie.length() > 1) {
newFolder = path + QDir::separator() + nameFormat->formatName(nameFormat->formatParts(fileName));
newFolder = path + QDir::separator() + nameFormatter.formatName(nameFormatter.formatParts(fileName));
} else {
continue;
}
Expand All @@ -63,7 +63,7 @@ void MovieFilesOrganizer::moveToDirs(mediaelch::DirectoryPath dir)
if (!dir.rename(file,
newFolder + QDir::separator()
+ file.right(file.length() - file.lastIndexOf(QDir::separator()) - 1))) {
qDebug() << "Moving " << file << "to " << newFolder << " failed.";
qWarning() << "Moving " << file << "to " << newFolder << " failed.";
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/tv_shows/TvShow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ bool TvShow::loadData(MediaCenterInterface* mediaCenterInterface, bool reloadFro
}();

if (!infoLoaded) {
setName(NameFormatter::instance()->formatName(dir().dirName()));
NameFormatter format;
setName(format.formatName(dir().dirName()));
}
m_infoLoaded = infoLoaded;
m_infoFromNfoLoaded = infoLoaded && reloadFromNfo;
Expand Down
8 changes: 6 additions & 2 deletions src/ui/imports/ImportDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ int ImportDialog::execMovie(QString searchString)
m_type = "movie";
m_filesToMove.clear();
ui->stackedWidget->setCurrentIndex(0);
ui->movieSearchWidget->search(NameFormatter::instance()->formatName(searchString), id, TmdbId::NoId);

NameFormatter nameFormatter;
ui->movieSearchWidget->search(nameFormatter.formatName(searchString), id, TmdbId::NoId);

ui->placeholders->setType(Renamer::RenameType::Movies);
ui->chkSeasonDirectories->setVisible(false);
Expand Down Expand Up @@ -179,7 +181,9 @@ int ImportDialog::execConcert(QString searchString)
m_type = "concert";
m_filesToMove.clear();
ui->stackedWidget->setCurrentIndex(2);
ui->concertSearchWidget->search(NameFormatter::instance()->formatName(searchString));

NameFormatter nameFormatter;
ui->concertSearchWidget->search(nameFormatter.formatName(searchString));

ui->placeholders->setType(Renamer::RenameType::Concerts);
ui->chkSeasonDirectories->setVisible(false);
Expand Down
1 change: 0 additions & 1 deletion src/ui/main/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWi

TvShowSearch::instance(this);
ImageDialog::instance(this);
NameFormatter::instance(this);
Notificator::instance(nullptr, ui->centralWidget);

#ifdef Q_OS_WIN
Expand Down

0 comments on commit c7ccd57

Please sign in to comment.