|
| 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 "climain.h" |
| 16 | +#include "clisubtitlesdownloader.h" |
| 17 | +#include "libqnapi.h" |
| 18 | +#include "qnapicommand.h" |
| 19 | +#include "subtitlelanguage.h" |
| 20 | +#include "utils/helphelper.h" |
| 21 | + |
| 22 | +#include <signal.h> |
| 23 | +#include <QDir> |
| 24 | +#include <QTranslator> |
| 25 | +#include <iostream> |
| 26 | + |
| 27 | +namespace CliMain { |
| 28 | + |
| 29 | +void installTranslation(QCoreApplication &app, QTranslator *translator, |
| 30 | + const QNapiConfig &config) { |
| 31 | + QString uiLanguage = LibQNapi::uiLanguage(config.generalConfig()); |
| 32 | + translator->load("qnapi_" + uiLanguage, ":/translations"); |
| 33 | + app.installTranslator(translator); |
| 34 | +} |
| 35 | + |
| 36 | +void printHeader(const Console &c) { |
| 37 | + c.printLine(tr("QNapi %1 (Qt version %2), %3") |
| 38 | + .arg(LibQNapi::displayableVersion()) |
| 39 | + .arg(qVersion()) |
| 40 | + .arg(LibQNapi::webpageUrl())); |
| 41 | + c.printLine(); |
| 42 | +} |
| 43 | + |
| 44 | +void printHelp(const Console &c, |
| 45 | + const QList<QSharedPointer<CliArgParser>> &cliArgParsers) { |
| 46 | + auto helpLines = HelpHelper::formatHelpLinesText(cliArgParsers); |
| 47 | + foreach (auto helpLine, helpLines) { c.printLine(helpLine); } |
| 48 | +} |
| 49 | + |
| 50 | +void printHelpLanguages(const Console &c, const QNapiConfig &config) { |
| 51 | + c.printLine( |
| 52 | + tr("List of languages recognized by QNapi, including corresponding")); |
| 53 | + c.printLine(tr("two-letter language codes:")); |
| 54 | + c.printLine(); |
| 55 | + |
| 56 | + SubtitleLanguage L, LB; |
| 57 | + QStringList langs = L.listLanguages(); |
| 58 | + |
| 59 | + foreach (QString lang, langs) { |
| 60 | + L.setLanguage(lang); |
| 61 | + c.printLine(QString(" %1 - %2").arg(L.toTwoLetter()).arg(lang)); |
| 62 | + } |
| 63 | + |
| 64 | + L.setLanguage(config.generalConfig().language()); |
| 65 | + LB.setLanguage(config.generalConfig().backupLanguage()); |
| 66 | + |
| 67 | + c.printLine(); |
| 68 | + c.printLine(tr("Current default subtitles language: %1 (%2)") |
| 69 | + .arg(L.toFullName()) |
| 70 | + .arg(L.toTwoLetter())); |
| 71 | + |
| 72 | + if (LB.toFullName().isEmpty()) { |
| 73 | + c.printLine(tr("No alternative subtitles language has been set")); |
| 74 | + } else { |
| 75 | + c.printLine(tr("Current alternative subtitles language: %1 (%2)") |
| 76 | + .arg(LB.toFullName()) |
| 77 | + .arg(LB.toTwoLetter())); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +int processCommand(QVariant cliCommand, const QNapiConfig &config, |
| 82 | + const QList<QSharedPointer<CliArgParser>> &cliArgParsers) { |
| 83 | + const Console c(config.generalConfig().quietBatch()); |
| 84 | + |
| 85 | + printHeader(c); |
| 86 | + |
| 87 | + using namespace QNapiCommand; |
| 88 | + if (cliCommand.canConvert<DownloadSubtitles>()) { |
| 89 | + QStringList movieFilePaths = |
| 90 | + cliCommand.value<DownloadSubtitles>().movieFilePaths; |
| 91 | + return CliSubtitlesDownloader::downloadSubtitlesFor(c, movieFilePaths, |
| 92 | + config); |
| 93 | + } else if (cliCommand.canConvert<ShowHelpLanguages>()) { |
| 94 | + printHelpLanguages(c, config); |
| 95 | + return 0; |
| 96 | + } else { |
| 97 | + printHelp(c, cliArgParsers); |
| 98 | + return 0; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +void sigHandler(int sig) { |
| 103 | + Q_UNUSED(sig); |
| 104 | + |
| 105 | + std::cout << std::endl |
| 106 | + << tr("QNapi: deleting temporary files...").toStdString() |
| 107 | + << std::endl; |
| 108 | + |
| 109 | + const QNapiConfig config = LibQNapi::loadConfig(); |
| 110 | + QString tmpPath = config.generalConfig().tmpPath(); |
| 111 | + QDir tmpDir(tmpPath); |
| 112 | + |
| 113 | + QStringList filters; |
| 114 | + filters << "QNapi-*-rc"; |
| 115 | + filters << "QNapi.*.tmp"; |
| 116 | + |
| 117 | + QFileInfoList files = tmpDir.entryInfoList(filters); |
| 118 | + |
| 119 | + foreach (QFileInfo file, files) { QFile::remove(file.filePath()); } |
| 120 | + |
| 121 | + std::cout << tr("QNapi: finished.").toStdString() << std::endl; |
| 122 | + |
| 123 | + exit(666); |
| 124 | +} |
| 125 | + |
| 126 | +void regSignal() { |
| 127 | +#ifdef Q_OS_WIN |
| 128 | + signal(SIGTERM, sigHandler); |
| 129 | + signal(SIGINT, sigHandler); |
| 130 | +#else |
| 131 | + struct sigaction sa; |
| 132 | + memset(&sa, 0, sizeof(struct sigaction)); |
| 133 | + sigemptyset(&sa.sa_mask); |
| 134 | + sa.sa_handler = sigHandler; |
| 135 | + sigaction(SIGTERM, &sa, 0); |
| 136 | + sigaction(SIGINT, &sa, 0); |
| 137 | +#endif |
| 138 | +} |
| 139 | + |
| 140 | +} // namespace CliMain |
0 commit comments