| 1 | /***************************************************************************** |
| 2 | ** QNapi |
| 3 | ** Copyright (C) 2008-2017 Piotr KrzemiĆski <[email protected]> |
| 4 | ** |
| 5 | ** This program is free software; you can redistribute it and/or modify |
| 6 | ** it under the terms of the GNU General Public License as published by |
| 7 | ** the Free Software Foundation; either version 2 of the License, or |
| 8 | ** (at your option) any later version. |
| 9 | ** |
| 10 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
| 11 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | ** |
| 13 | *****************************************************************************/ |
| 14 | |
| 15 | #include "subtitledownloadenginesregistry.h" |
| 16 | |
| 17 | #include "engines/napiprojektdownloadengine.h" |
| 18 | #include "engines/napisy24downloadengine.h" |
| 19 | #include "engines/opensubtitlesdownloadengine.h" |
| 20 | |
| 21 | #include "utils/p7zipdecoder.h" |
| 22 | |
| 23 | #include <Maybe.h> |
| 24 | |
| 25 | #include <QPair> |
| 26 | |
| 27 | SubtitleDownloadEnginesRegistry::SubtitleDownloadEnginesRegistry( |
| 28 | const QString &qnapiDisplayableVersion, |
| 29 | const QStringList &subtitleExtensions, |
| 30 | std::function<QSharedPointer<const P7ZipDecoder>(const QString &)> |
| 31 | p7zipDecoderFactory) |
| 32 | : qnapiDisplayableVersion(qnapiDisplayableVersion), |
| 33 | subtitleExtensions(subtitleExtensions), |
| 34 | p7zipDecoderFactory(p7zipDecoderFactory) {} |
| 35 | |
| 36 | QStringList SubtitleDownloadEnginesRegistry::listEngineNames() const { |
| 37 | static QStringList engineNames = { |
| 38 | NapiProjektDownloadEngine::metadata.name(), |
| 39 | OpenSubtitlesDownloadEngine::metadata.name(), |
| 40 | Napisy24DownloadEngine::metadata.name()}; |
| 41 | return engineNames; |
| 42 | } |
| 43 | |
| 44 | SubtitleDownloadEngineMetadata SubtitleDownloadEnginesRegistry::engineMetadata( |
| 45 | const QString &engineName) const { |
| 46 | if (engineName == NapiProjektDownloadEngine::metadata.name()) |
| 47 | return NapiProjektDownloadEngine::metadata; |
| 48 | |
| 49 | if (engineName == OpenSubtitlesDownloadEngine::metadata.name()) |
| 50 | return OpenSubtitlesDownloadEngine::metadata; |
| 51 | |
| 52 | if (engineName == Napisy24DownloadEngine::metadata.name()) |
| 53 | return Napisy24DownloadEngine::metadata; |
| 54 | |
| 55 | return SubtitleDownloadEngineMetadata("", "", nothing(), nothing()); |
| 56 | } |
| 57 | |
| 58 | const char *const *SubtitleDownloadEnginesRegistry::enginePixmapData( |
| 59 | const QString &engineName) const { |
| 60 | if (engineName == NapiProjektDownloadEngine::metadata.name()) |
| 61 | return NapiProjektDownloadEngine::pixmapData; |
| 62 | |
| 63 | if (engineName == OpenSubtitlesDownloadEngine::metadata.name()) |
| 64 | return OpenSubtitlesDownloadEngine::pixmapData; |
| 65 | |
| 66 | if (engineName == Napisy24DownloadEngine::metadata.name()) |
| 67 | return Napisy24DownloadEngine::pixmapData; |
| 68 | |
| 69 | return {}; |
| 70 | } |
| 71 | |
| 72 | QSharedPointer<SubtitleDownloadEngine> |
| 73 | SubtitleDownloadEnginesRegistry::createEngine(const QString &engineName, |
| 74 | const QNapiConfig &config) const { |
| 75 | QSharedPointer<const P7ZipDecoder> p7zipDecoder = |
| 76 | p7zipDecoderFactory(config.generalConfig().p7zipPath()); |
| 77 | const QString &tmpPath = config.generalConfig().tmpPath(); |
| 78 | |
| 79 | if (engineName == NapiProjektDownloadEngine::metadata.name()) { |
| 80 | const EngineConfig &engineConfig = config.enginesConfig().value( |
| 81 | NapiProjektDownloadEngine::metadata.name(), EngineConfig::empty); |
| 82 | return QSharedPointer<SubtitleDownloadEngine>( |
| 83 | new NapiProjektDownloadEngine(tmpPath, engineConfig, p7zipDecoder)); |
| 84 | } |
| 85 | |
| 86 | if (engineName == OpenSubtitlesDownloadEngine::metadata.name()) { |
| 87 | const EngineConfig &engineConfig = config.enginesConfig().value( |
| 88 | OpenSubtitlesDownloadEngine::metadata.name(), EngineConfig::empty); |
| 89 | const QString &language = config.generalConfig().language(); |
| 90 | return QSharedPointer<SubtitleDownloadEngine>( |
| 91 | new OpenSubtitlesDownloadEngine(tmpPath, engineConfig, p7zipDecoder, |
| 92 | qnapiDisplayableVersion, language)); |
| 93 | } |
| 94 | |
| 95 | if (engineName == Napisy24DownloadEngine::metadata.name()) { |
| 96 | const EngineConfig &engineConfig = config.enginesConfig().value( |
| 97 | Napisy24DownloadEngine::metadata.name(), EngineConfig::empty); |
| 98 | return QSharedPointer<SubtitleDownloadEngine>(new Napisy24DownloadEngine( |
| 99 | tmpPath, engineConfig, p7zipDecoder, subtitleExtensions)); |
| 100 | } |
| 101 | |
| 102 | return QSharedPointer<SubtitleDownloadEngine>(); |
| 103 | } |
| 104 | |
| 105 | QList<QSharedPointer<SubtitleDownloadEngine>> |
| 106 | SubtitleDownloadEnginesRegistry::createEngines( |
| 107 | const QStringList &engineNames, const QNapiConfig &config) const { |
| 108 | QList<QSharedPointer<SubtitleDownloadEngine>> engines; |
| 109 | foreach (QString engineName, engineNames) { |
| 110 | engines << createEngine(engineName, config); |
| 111 | } |
| 112 | return engines; |
| 113 | } |
| 114 | |
| 115 | QList<QSharedPointer<SubtitleDownloadEngine>> |
| 116 | SubtitleDownloadEnginesRegistry::createEnabledEngines( |
| 117 | const QNapiConfig &config) const { |
| 118 | QStringList engineNames; |
| 119 | typedef QPair<QString, bool> EngineEnableCfg; |
| 120 | |
| 121 | foreach (EngineEnableCfg engineCfg, config.enabledEngines()) { |
| 122 | if (engineCfg.second) engineNames << engineCfg.first; |
| 123 | } |
| 124 | |
| 125 | return createEngines(engineNames, config); |
| 126 | } |