Skip to content
This repository has been archived by the owner on Jan 21, 2021. It is now read-only.

Commit

Permalink
* New: 'Find by Name' and 'Find by Info' for find subtitles (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
bylee20 committed Apr 10, 2015
1 parent 53b86ea commit fceef7d
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 77 deletions.
51 changes: 35 additions & 16 deletions src/bomi/dialog/subtitlefinddialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ struct SubtitleFindDialog::Data {
void updateState() {
const bool ok = finder->isAvailable() && !downloader.isRunning();
ui.open->setEnabled(ok);
ui.state->setEnabled(ok);
ui.find_file->setEnabled(ok);
ui.find_info->setEnabled(ok);
ui.find_name->setEnabled(ok);
ui.language->setEnabled(ok);
ui.get->setEnabled(ok);
ui.view->setEnabled(ok);
Expand All @@ -129,18 +131,25 @@ struct SubtitleFindDialog::Data {
}
} else
ui.prog->setRange(0, 0);

switch (finder->state()) {
case OpenSubtitlesFinder::Connecting:
ui.state->setText(tr("Connecting..."));
break;
case OpenSubtitlesFinder::Finding:
ui.state->setText(tr("Finding..."));
break;
default:
ui.state->setText(tr("Find"));
break;
}
updateStateText();
}
auto updateStateText() -> void
{
auto text = [=] () {
switch (finder->state()) {
case OpenSubtitlesFinder::Connecting:
return tr("Connecting...");
case OpenSubtitlesFinder::Finding:
return tr("Finding...");
case OpenSubtitlesFinder::Unavailable:
return tr("Unavailable");
case OpenSubtitlesFinder::Error:
return tr("Error");
default:
return tr("Available");
}
};
ui.state->setText(text());
}
auto getNameToPreserve(const QString &subName) -> QString
{
Expand Down Expand Up @@ -241,6 +250,16 @@ SubtitleFindDialog::SubtitleFindDialog(QWidget *parent)
if (!file.isEmpty())
find(QUrl::fromLocalFile(file));
});
connect(d->ui.find_file, &QPushButton::clicked, this, [=] () {
if (d->mediaFile.exists())
find(QUrl::fromLocalFile(d->mediaFile.absoluteFilePath()));
});
connect(d->ui.find_info, &QPushButton::clicked, this, [=] () {
d->finder->find(d->ui.query->text(),
d->ui.season->value(), d->ui.episode->value());
});
connect(d->ui.find_name, &QPushButton::clicked,
this, [=] () { d->finder->find(d->ui.tag->text()); });
connect(d->ui.language, &CheckListWidget::checkedItemsChanged, [=] () {
auto langs = d->ui.language->checkedData();
d->proxy.langCodes.clear();
Expand All @@ -265,17 +284,17 @@ SubtitleFindDialog::SubtitleFindDialog(QWidget *parent)
if (d->downloader.start(url))
d->downloads[d->downloader.url()] = info;
});
connect(d->finder, &OpenSubtitlesFinder::stateChanged,
connect(d->finder, &OpenSubtitlesFinder::stateChanged, this,
[this] () { d->updateState(); });
connect(d->finder, &OpenSubtitlesFinder::found,
connect(d->finder, &OpenSubtitlesFinder::found, this,
[this] (QVector<SubtitleLink> links) {
// Select first entry of list
d->ui.view->setCurrentIndex(d->ui.view->indexAt(QPoint()));
d->fillLanguage(links);
d->model.setList(links);
d->proxy.invalidate();
});
d->updateState();
d->updateStateText();

_SetWindowTitle(this, tr("Find Subtitle from OpenSubtitles.org"));
d->storage.setObject(this, u"subtitle_find_dialog"_q);
Expand Down
86 changes: 59 additions & 27 deletions src/bomi/subtitle/opensubtitlesfinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct OpenSubtitlesFinder::Data {
OpenSubtitlesFinder *p = nullptr;
XmlRpcClient client;
QString token, error;

void setState(State s) {
if (_Change(state, s)) {
if (state != Error)
Expand Down Expand Up @@ -47,6 +48,35 @@ struct OpenSubtitlesFinder::Data {
this->error = error;
setState(Error);
}
auto call(const QVariantMap &map) -> void
{
const auto args = _Args() << token << QVariant(QVariantList() << map);
client.call(u"SearchSubtitles"_q, args, [this] (const QVariantList &results) {
setState(Available);
if (results.isEmpty() || results.first().type() != QVariant::Map) {
emit p->found(QVector<SubtitleLink>());
} else {
const auto list = results.first().toMap()[u"data"_q].toList();
QVector<SubtitleLink> links;
for (auto &it : list) {
if (it.type() != QVariant::Map)
continue;
auto const map = it.toMap();
SubtitleLink link;
link.fileName = map[u"SubFileName"_q].toString();
link.date = map[u"SubAddDate"_q].toString();
link.url = map[u"SubDownloadLink"_q].toString();
link.langCode = map[u"SubLanguageID"_q].toString();
if (link.langCode.isEmpty())
link.langCode = map[u"ISO639"_q].toString();
if (link.langCode.isEmpty())
link.langCode = map[u"LanguageName"_q].toString();
links.append(link);
}
emit p->found(links);
}
});
}
};

OpenSubtitlesFinder::OpenSubtitlesFinder(QObject *parent)
Expand All @@ -62,6 +92,34 @@ OpenSubtitlesFinder::~OpenSubtitlesFinder() {
delete d;
}

auto OpenSubtitlesFinder::find(const QString &tag) -> bool
{
if (d->state != Available || tag.isEmpty())
return false;
d->setState(Finding);
QVariantMap map;
map[u"sublanguageid"_q] = u"all"_q;
map[u"tag"_q] = tag;
d->call(map);
return true;
}

auto OpenSubtitlesFinder::find(const QString &query, int season, int episode) -> bool
{
if (d->state != Available || query.isEmpty())
return false;
d->setState(Finding);
QVariantMap map;
map[u"sublanguageid"_q] = u"all"_q;
map[u"query"_q] = query;
if (season >= 0)
map[u"season"_q] = season;
if (episode >= 0)
map[u"episode"_q] = episode;
d->call(map);
return true;
}

auto OpenSubtitlesFinder::find(const Mrl &mrl) -> bool
{
if (d->state != Available)
Expand Down Expand Up @@ -93,33 +151,7 @@ auto OpenSubtitlesFinder::find(const Mrl &mrl) -> bool
map[u"sublanguageid"_q] = u"all"_q;
map[u"moviehash"_q] = hash;
map[u"moviebytesize"_q] = bytes;
const auto args = _Args() << d->token << QVariant(QVariantList() << map);
d->client.call(u"SearchSubtitles"_q, args,
[this] (const QVariantList &results) {
d->setState(Available);
if (results.isEmpty() || results.first().type() != QVariant::Map) {
emit found(QVector<SubtitleLink>());
} else {
const auto list = results.first().toMap()[u"data"_q].toList();
QVector<SubtitleLink> links;
for (auto &it : list) {
if (it.type() != QVariant::Map)
continue;
auto const map = it.toMap();
SubtitleLink link;
link.fileName = map[u"SubFileName"_q].toString();
link.date = map[u"SubAddDate"_q].toString();
link.url = map[u"SubDownloadLink"_q].toString();
link.langCode = map[u"SubLanguageID"_q].toString();
if (link.langCode.isEmpty())
link.langCode = map[u"ISO639"_q].toString();
if (link.langCode.isEmpty())
link.langCode = map[u"LanguageName"_q].toString();
links.append(link);
}
emit found(links);
}
});
d->call(map);
return true;
}

Expand Down
2 changes: 2 additions & 0 deletions src/bomi/subtitle/opensubtitlesfinder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class OpenSubtitlesFinder : public QObject {
OpenSubtitlesFinder(QObject *parent = nullptr);
~OpenSubtitlesFinder();
auto find(const Mrl &mrl) -> bool;
auto find(const QString &tag) -> bool;
auto find(const QString &query, int season, int episode) -> bool;
auto state() const -> State;
auto isAvailable() const -> bool { return state() == Available; }
auto error() const -> QString;
Expand Down
Loading

0 comments on commit fceef7d

Please sign in to comment.