Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional adding of two/three-letter language codes to names of downloaded subtitle files (#72, #108) #170

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
26 changes: 11 additions & 15 deletions cli/src/clisubtitlesdownloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ void printSubtitlesList(const Console& c, QNapi& napi) {
}
}

Maybe<int> selectSubtitles(const Console& c, const QNapiConfig& config,
QNapi& napi) {
bool selectSubtitles(const Console& c, const QNapiConfig& config, QNapi& napi) {
bool showList = false;
bool napiShowList = napi.needToShowList();

Expand All @@ -129,30 +128,29 @@ Maybe<int> selectSubtitles(const Console& c, const QNapiConfig& config,
}

if (!showList) {
return just(napi.bestIdx());
napi.selectSubtitlesByIdx(napi.bestIdx());
} else {
QList<SubtitleInfo> subtitlesList = napi.listSubtitles();

printSubtitlesList(c, napi);
int selIdx = c.inputNumber(tr("Select subtitles to download: "), 0,
subtitlesList.size());
if (selIdx == 0) {
return nothing();
} else {
return just(selIdx - 1);
}
if (selIdx == 0) return false;
napi.selectSubtitlesByIdx(selIdx - 1);
}

return true;
}

int finishSubtitles(int selIdx, const Console& c, QNapi& napi) {
int finishSubtitles(const Console& c, QNapi& napi) {
c.printLineOrdinary(tr("Downloading subtitles..."));
if (!napi.download(selIdx)) {
if (!napi.download()) {
c.printLineError(tr("Unable to download subtitles!"));
return EC_COULD_NOT_DOWNLOAD;
}

c.printLineOrdinary(tr("Unpacking subtitles..."));
if (!napi.unpack(selIdx)) {
if (!napi.unpack()) {
c.printLineError(tr("Failed to unpack subtitles!"));
return EC_COULD_NOT_UNARCHIVE;
}
Expand Down Expand Up @@ -197,10 +195,8 @@ int downloadForMovie(const Console& c, const QString& movieFilePath, int i,
return EC_SUBTITLES_NOT_FOUND;
}

Maybe<int> selIdx = selectSubtitles(c, config, napi);

if (selIdx) {
return finishSubtitles(selIdx.value(), c, napi);
if (selectSubtitles(c, config, napi)) {
return finishSubtitles(c, napi);
}

napi.cleanup();
Expand Down
41 changes: 28 additions & 13 deletions gui/src/forms/frmconvert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "libqnapi.h"
#include "movieinfo/movieinfoprovider.h"
#include "qnapiopendialog.h"
#include "utils/pathutils.h"

#include <QDesktopServices>
#include <QDesktopWidget>
Expand Down Expand Up @@ -148,21 +149,35 @@ void frmConvert::checkFPSNeeded() {
ui.pbMovieFPSSelect->setEnabled(fpsNeeded);

if (fpsNeeded) {
QFileInfo srcSubFI(ui.leSrcSubFile->text());

QString movieFilePathBase =
srcSubFI.absoluteDir().filePath(srcSubFI.completeBaseName());

foreach (QString movieExt, staticConfig->movieExtensions()) {
QString movieFilePath = movieFilePathBase + "." + movieExt;
if (QFileInfo(movieFilePath).exists()) {
Maybe<QString> maybeFPS = determineMovieFPS(movieFilePath);
if (maybeFPS) {
ui.cbMovieFPS->setCurrentText(maybeFPS.value());
ui.cbFPSTo->setCurrentText(maybeFPS.value());
QString srcSubAbsoluteFilePath =
QFileInfo(ui.leSrcSubFile->text()).absoluteFilePath();

QString movieFilePathBase = ChangeFilePath()
.removeExtension().apply(srcSubAbsoluteFilePath);

auto determineFPSFromPathBase = [this, &movieFilePathBase] () {
foreach (QString movieExt, staticConfig->movieExtensions()) {
QString movieFilePath = movieFilePathBase + "." + movieExt;
if (QFileInfo(movieFilePath).exists()) {
Maybe<QString> maybeFPS = determineMovieFPS(movieFilePath);
if (maybeFPS) {
ui.cbMovieFPS->setCurrentText(maybeFPS.value());
ui.cbFPSTo->setCurrentText(maybeFPS.value());
}
return true;
}
break;
}
return false;
};

if (determineFPSFromPathBase()) return;

// try removing language code from file name
QString movieFilePathBaseWithoutLang = ChangeFilePath()
.removeExtension().removeLanguageCode().apply(srcSubAbsoluteFilePath);
if (movieFilePathBase != movieFilePathBaseWithoutLang) {
movieFilePathBase = movieFilePathBaseWithoutLang;
determineFPSFromPathBase();
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions gui/src/forms/frmoptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,9 @@ void frmOptions::writeConfig() {
ui.cbLangBackup->itemData(ui.cbLangBackup->currentIndex())
.toString())
.setNoBackup(ui.cbNoBackup->isChecked())
.setLangCodeInFileName(
static_cast<LangCodeType>(
ui.cbLangCodeInFileName->currentIndex()))
.setQuietBatch(ui.cbQuietBatch->isChecked())
.setSearchPolicy(
static_cast<SearchPolicy>(ui.cbSearchPolicy->currentIndex()))
Expand Down Expand Up @@ -419,6 +422,8 @@ void frmOptions::readConfig(const QNapiConfig &config) {
ui.cbLangBackup->setCurrentIndex(ui.cbLangBackup->findData(
SubtitleLanguage(config.generalConfig().backupLanguage()).toTwoLetter()));
ui.cbNoBackup->setChecked(config.generalConfig().noBackup());
ui.cbLangCodeInFileName->setCurrentIndex(
config.generalConfig().langCodeInFileName());
ui.cbQuietBatch->setChecked(config.generalConfig().quietBatch());

ui.twEngines->clear();
Expand Down
7 changes: 4 additions & 3 deletions gui/src/forms/frmprogress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,12 @@ void GetThread::run() {
subStatusList << SubtitleInfo::fromFailed(queue[i]);
continue;
}
napi.selectSubtitlesByIdx(selIdx);

emit progressChange(i, queue.size(), 0.5);
emit actionChange(tr("Downloading subtitles file..."));

if (!napi.download(selIdx)) {
if (!napi.download()) {
ABORT_POINT

++napiFail;
Expand All @@ -372,7 +373,7 @@ void GetThread::run() {
emit progressChange(i, queue.size(), 0.65f);
emit actionChange(tr("Unpacking subtitles file..."));

if (!napi.unpack(selIdx)) {
if (!napi.unpack()) {
++napiFail;
subStatusList << SubtitleInfo::fromFailed(queue[i]);
continue;
Expand All @@ -398,7 +399,7 @@ void GetThread::run() {
}

++napiSuccess;
subStatusList << napi.listSubtitles().at(selIdx);
subStatusList << napi.getSelectedSubtitles();

napi.cleanup();

Expand Down
27 changes: 23 additions & 4 deletions gui/src/forms/frmscan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "frmscan.h"
#include "libqnapi.h"
#include "utils/pathutils.h"

frmScan::frmScan(QWidget *parent, Qt::WindowFlags f)
: QDialog(parent, f), scanConfig(LibQNapi::loadConfig().scanConfig()) {
Expand Down Expand Up @@ -236,7 +237,9 @@ void frmScan::accept() {
QDialog::accept();
}

ScanFilesThread::ScanFilesThread() : staticConfig(LibQNapi::staticConfig()) {}
ScanFilesThread::ScanFilesThread()
: subExtensionFilters(LibQNapi::staticConfig()->subtitleExtensionsFilter()
.split(" ")) {}

void ScanFilesThread::run() {
abort = false;
Expand Down Expand Up @@ -264,6 +267,22 @@ bool ScanFilesThread::doScan(const QString &path, QDir::Filters filters) {

emit folderChange(myPath);

QStringList subtitlesBaseNames;
if (skipIfSubtitlesExists) {
for (const auto &s : QDir(myPath).entryInfoList(subExtensionFilters,
QDir::Files | QDir::Hidden)) {

QString completeBaseName = s.completeBaseName();
subtitlesBaseNames << completeBaseName;

QString completeBaseNameWithoutLang = ChangeFilePath()
.removeLanguageCode().removeExtension().apply(s.fileName());
if (completeBaseName != completeBaseNameWithoutLang) {
subtitlesBaseNames << completeBaseNameWithoutLang;
}
}
}

QFileInfoList list = QDir(myPath).entryInfoList(scanFilters, filters);

for (QFileInfoList::iterator p = list.begin(); p != list.end(); p++) {
Expand All @@ -276,9 +295,9 @@ bool ScanFilesThread::doScan(const QString &path, QDir::Filters filters) {

bool subtitleFileFound = false;
if (skipIfSubtitlesExists) {
foreach (QString subExt, staticConfig->subtitleExtensions()) {
if (QFile::exists((*p).absolutePath() + "/" +
(*p).completeBaseName() + "." + subExt)) {
QString baseName = (*p).completeBaseName();
for (const auto &subBaseName : subtitlesBaseNames) {
if (subBaseName.compare(baseName, Qt::CaseInsensitive) == 0) {
subtitleFileFound = true;
break;
}
Expand Down
3 changes: 1 addition & 2 deletions gui/src/forms/frmscan.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ class ScanFilesThread : public QNapiThread {
private:
bool doScan(const QString &path, QDir::Filters filters);

QSharedPointer<const StaticConfig> staticConfig;
QString searchPath;
QStringList scanFilters, skipFilters;
QStringList scanFilters, skipFilters, subExtensionFilters;
bool skipIfSubtitlesExists, followSymLinks;
QSet<QString> visited;
};
Expand Down
36 changes: 31 additions & 5 deletions gui/ui/frmoptions.ui
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<string>General</string>
</attribute>
<layout class="QGridLayout" name="gridLayout">
<item row="10" column="0" colspan="3">
<item row="11" column="0" colspan="3">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
Expand All @@ -41,7 +41,7 @@
</property>
</spacer>
</item>
<item row="7" column="0" colspan="3">
<item row="8" column="0" colspan="3">
<widget class="QCheckBox" name="cbQuietBatch">
<property name="toolTip">
<string>Do not show any windows while retrieving subtitles in command-line</string>
Expand Down Expand Up @@ -88,14 +88,14 @@
</property>
</widget>
</item>
<item row="8" column="0" colspan="2">
<item row="9" column="0" colspan="2">
<widget class="QCheckBox" name="cbChangePermissions">
<property name="text">
<string>Subtitles file permissions: </string>
</property>
</widget>
</item>
<item row="8" column="2">
<item row="9" column="2">
<layout class="QHBoxLayout" name="_2">
<property name="spacing">
<number>6</number>
Expand Down Expand Up @@ -157,7 +157,7 @@
</item>
</layout>
</item>
<item row="9" column="0" colspan="3">
<item row="10" column="0" colspan="3">
<widget class="QCheckBox" name="cbShowDockIcon">
<property name="text">
<string>Show icon in the system tray (requires application restart)</string>
Expand Down Expand Up @@ -274,6 +274,32 @@
</property>
</widget>
</item>
<item row="7" column="0" colspan="2">
<widget class="QLabel" name="lbLangCodeInFileName">
<property name="text">
<string>Add language code to subtitle file name:</string>
</property>
</widget>
</item>
<item row="7" column="2">
<widget class="QComboBox" name="cbLangCodeInFileName">
<item>
<property name="text">
<string>No</string>
</property>
</item>
<item>
<property name="text">
<string>Two-letter</string>
</property>
</item>
<item>
<property name="text">
<string>Three-letter</string>
</property>
</item>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="tabDownloadEngines">
Expand Down
2 changes: 2 additions & 0 deletions libqnapi/libqnapi.pro
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ SOURCES += src/config/configreader.cpp \
src/utils/console.cpp \
src/utils/encodingutils.cpp \
src/utils/p7zipdecoder.cpp \
src/utils/pathutils.cpp \
src/utils/synchttp.cpp \
src/utils/syncxmlrpc.cpp \
src/qnapi.cpp \
Expand Down Expand Up @@ -103,6 +104,7 @@ HEADERS += src/config/configreader.h \
src/subtitlepostprocessor.h \
src/utils/console.h \
src/utils/encodingutils.h \
src/utils/pathutils.h \
src/utils/p7zipdecoder.h \
src/utils/synchttp.h \
src/utils/syncxmlrpc.h \
Expand Down
2 changes: 2 additions & 0 deletions libqnapi/src/config/configreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ const GeneralConfig ConfigReader::readGeneralConfig(
settings.value("qnapi/language", preferredLangCode).toString(),
settings.value("qnapi/language_backup", backupLangCode).toString(),
settings.value("qnapi/no_backup", false).toBool(),
static_cast<LangCodeType>(
settings.value("qnapi/lang_code_in_file_name", LCT_NONE).toInt()),
settings.value("qnapi/quiet_batch", false).toBool(),
static_cast<SearchPolicy>(
settings.value("qnapi/search_policy", SP_BREAK_IF_FOUND).toInt()),
Expand Down
2 changes: 2 additions & 0 deletions libqnapi/src/config/configwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ void ConfigWriter::writeGeneralConfig(const GeneralConfig& generalConfig,
settings.setValue("qnapi/language", generalConfig.language());
settings.setValue("qnapi/language_backup", generalConfig.backupLanguage());
settings.setValue("qnapi/no_backup", generalConfig.noBackup());
settings.setValue("qnapi/lang_code_in_file_name",
generalConfig.langCodeInFileName());
settings.setValue("qnapi/quiet_batch", generalConfig.quietBatch());
settings.setValue("qnapi/search_policy", generalConfig.searchPolicy());
settings.setValue("qnapi/download_policy", generalConfig.downloadPolicy());
Expand Down
1 change: 1 addition & 0 deletions libqnapi/src/config/generalconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ QString GeneralConfig::toString() const {
<< "language: " << language() << endl
<< "backupLanguage: " << backupLanguage() << endl
<< "noBackup: " << noBackup() << endl
<< "langCodeInFileName: " << langCodeInFileName() << endl
<< "quietBatch: " << quietBatch() << endl
<< "searchPolicy: " << searchPolicy() << endl
<< "downloadPolicy: " << downloadPolicy() << endl
Expand Down
Loading