Skip to content

Commit c9a6cd6

Browse files
authored
Separate command-line binary (#120)
* initial draft of separate cli application * reorganize main.cpp in cli a bit, towards real commands parser impl (still wip) * parsing command line arguments in new CLI * console obj with quiet logic extracted; config checking in CLI implemented * move QNapi class to libqnapi; implementing subtitles downloading in CLI (wip) * more informative error formatting * ported CLI functionality to a separated binary * updating translations for new CLI (wip) * improving translation * err codes as constants, not magic numbers; more rational downloading loop break * qmake option to disable building cli/gui targets * install mediainfo (to be present in pkgconfig) for macx cli build * hide ssl warnings from printing to console * fix asking for input in quiet mode * win32 deploy commands supporting both qnapic.exe/qnapi.exe * updated input for doxygen doc (added cli sources) * install pkgconfig for brew * add no_cli/no_gui note to README.md * not install pkgconfig for brew (seems to be already present) * fix pkgconfig issue on macx * get rid of cat * further refactoring * implementing gui command parser (wip), improving cli command parser * abstractios CliArgParser/CliArgParsersExecutor prototyped * QNapiCommand + ShowHelpArgParser * ShowHelpLanguages arg parser * QuietBatch arg parser * ShowList / DontShowList arg parsers * fixed parsers executor * lang arg parser * backup lang arg parser * format arg parser * extension arg parser * download subtitles arg parser * bug fixes in arg parsers * replace old cli arg parsers with new design in CLI * show options args parser, run cli app args parser * use parsers in GUI; wip * scan directory arg parser * improved error message about CLI binary not found * removed QNapiCli from gui app * replace close button & manual alignment with dialog button box (frmAbout) * Console extracted to libqnapi / utils * formatting arg switches help based on cli arg data (wip, cleanup needed) * renaming * removed obsolete CLI help text * translations updated * fixed tr functions to show valid help text translations * display gui help in dialog * show gui help about -c, small code cleanup * code cleanup * extracting help text producing logic to libqnapi * translations updated * init napisy24SubMenu in constructor * memory release * removed [email protected] on osx * fix .travis.yml formatting * added NsProcess.zip binary * add qt 5.9 and 5.10.0 on appveyor * add help languages msgbox with translation * use Qt 5.6.3 instead of 5.6.2 on Travis * add linux qt 5.10.1 build on travis * 10.8 mac minimal version
1 parent 391880f commit c9a6cd6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+3483
-1512
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ debug
55
release
66
Makefile*
77
qnapi
8+
qnapic
89
*.a
910
win32/out/*
1011
win32/*.exe

.travis.yml

+10-7
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,17 @@ matrix:
5252
compiler: gcc
5353
env:
5454
- QT_BASE=59
55+
- os: linux
56+
dist: trusty
57+
sudo: required
58+
compiler: gcc
59+
env:
60+
- QT_BASE=510
5561
- os: osx
5662
compiler: clang
5763
env:
5864
- QT_BASE=55
5965
- MAKE_DMG=1
60-
- os: osx
61-
compiler: clang
62-
env:
63-
- QT_BASE=57
6466
- os: osx
6567
compiler: clang
6668
env:
@@ -76,8 +78,8 @@ before_install:
7678
- if [[ "$QT_BASE" = "57" && "$TRAVIS_OS_NAME" = "linux" ]]; then sudo add-apt-repository ppa:beineri/opt-qt571-trusty -y; fi
7779
- if [[ "$QT_BASE" = "58" && "$TRAVIS_OS_NAME" = "linux" ]]; then sudo add-apt-repository ppa:beineri/opt-qt58-trusty -y; fi
7880
- if [[ "$QT_BASE" = "59" && "$TRAVIS_OS_NAME" = "linux" ]]; then sudo add-apt-repository ppa:beineri/opt-qt593-trusty -y; fi
79-
- if [[ "$QT_BASE" = "510" && "$TRAVIS_OS_NAME" = "linux" ]]; then sudo add-apt-repository ppa:beineri/opt-qt510-trusty -y; fi
80-
- if [ "$TRAVIS_OS_NAME" = "linux" ]; then
81+
- if [[ "$QT_BASE" = "510" && "$TRAVIS_OS_NAME" = "linux" ]]; then sudo add-apt-repository ppa:beineri/opt-qt-5.10.1-trusty -y; fi
82+
- if [ "$TRAVIS_OS_NAME" = "linux" ]; then
8183
sudo apt-get update -qq;
8284
else
8385
brew update;
@@ -87,6 +89,7 @@ before_install:
8789

8890
install:
8991
- if [ "$TRAVIS_OS_NAME" = "linux" ]; then sudo apt-get install -qq libmediainfo-dev; fi
92+
- if [ "$TRAVIS_OS_NAME" = "osx" ]; then brew install mediainfo; fi
9093
- if [ "$TRAVIS_OS_NAME" = "osx" ]; then npm install -g appdmg; fi
9194
- if [[ "$QT_BASE" = "51" && "$TRAVIS_OS_NAME" = "linux" ]]; then sudo apt-get install -qq qt51base; source /opt/qt51/bin/qt51-env.sh; fi
9295
- if [[ "$QT_BASE" = "52" && "$TRAVIS_OS_NAME" = "linux" ]]; then sudo apt-get install -qq qt52base; source /opt/qt52/bin/qt52-env.sh; fi
@@ -97,8 +100,8 @@ install:
97100
- if [[ "$QT_BASE" = "57" && "$TRAVIS_OS_NAME" = "linux" ]]; then sudo apt-get install -qq qt57base; source /opt/qt57/bin/qt57-env.sh; fi
98101
- if [[ "$QT_BASE" = "58" && "$TRAVIS_OS_NAME" = "linux" ]]; then sudo apt-get install -qq qt58base; source /opt/qt58/bin/qt58-env.sh; fi
99102
- if [[ "$QT_BASE" = "59" && "$TRAVIS_OS_NAME" = "linux" ]]; then sudo apt-get install -qq qt59base; source /opt/qt59/bin/qt59-env.sh; fi
103+
- if [[ "$QT_BASE" = "510" && "$TRAVIS_OS_NAME" = "linux" ]]; then sudo apt-get install -qq qt510base; source /opt/qt510/bin/qt510-env.sh; fi
100104
- if [[ "$QT_BASE" = "55" && "$TRAVIS_OS_NAME" = "osx" ]]; then brew install [email protected]; brew link --force [email protected]; fi
101-
- if [[ "$QT_BASE" = "57" && "$TRAVIS_OS_NAME" = "osx" ]]; then brew install [email protected]; brew link --force [email protected]; fi
102105
- if [[ "$QT_BASE" = "510" && "$TRAVIS_OS_NAME" = "osx" ]]; then brew install qt; brew link --force qt; fi
103106

104107

Doxyfile

+1
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,7 @@ WARN_LOGFILE =
791791
# Note: If this tag is empty the current directory is searched.
792792

793793
INPUT = gui/src \
794+
cli/src \
794795
libqnapi/src
795796

796797
# This tag can be used to specify the character encoding of the source files

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ To compile the application, you have to execute two following commands in `qnapi
4646

4747
This will produce `Makefile`.
4848

49+
> By appending `CONFIG+=no_cli` or `CONFIG+=no_gui` to qmake invocation you can disable building
50+
> command-line or graphical interface binaries.
51+
52+
4953
`$ make` (or `mingw32-make` on Windows)
5054

5155
This will compile the sources and build executable binary (or app bundle on OSX).

appveyor.yml

+8-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ environment:
1313
MINGW: C:\Qt\Tools\mingw530_32
1414
DEPLOY: 0
1515
- QT5: C:\Qt\5.8\mingw53_32
16+
MINGW: C:\Qt\Tools\mingw530_32
17+
DEPLOY: 0
18+
- QT5: C:\Qt\5.9\mingw53_32
19+
MINGW: C:\Qt\Tools\mingw530_32
20+
DEPLOY: 0
21+
- QT5: C:\Qt\5.10.0\mingw53_32
1622
MINGW: C:\Qt\Tools\mingw530_32
1723
DEPLOY: 1
1824

@@ -22,9 +28,8 @@ matrix:
2228
install:
2329
- appveyor-retry cinst nsis -y -version 2.51
2430
- appveyor-retry cinst 7zip.commandline -y -x86
25-
- appveyor DownloadFile "http://nsis.sourceforge.net/mediawiki/images/archive/1/18/20140806212030!NsProcess.zip" -FileName NsProcess.zip
26-
- 7z e -o"C:\Program Files (x86)\NSIS\Include" NsProcess.zip Include\*.nsh
27-
- 7z e -o"C:\Program Files (x86)\NSIS\Plugins" NsProcess.zip Plugin\*.dll
31+
- 7z e -o"C:\Program Files (x86)\NSIS\Include" deps\ns-process\NsProcess.zip Include\*.nsh
32+
- 7z e -o"C:\Program Files (x86)\NSIS\Plugins" deps\ns-process\NsProcess.zip Plugin\*.dll
2833
- git submodule update --init --recursive
2934

3035
before_build:

cli/cli.pro

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
TEMPLATE = app
2+
3+
CONFIG += warn_on qt resources silent c++11 console
4+
5+
QT += core network xml
6+
QT -= gui
7+
8+
SOURCES += src/main.cpp \
9+
src/clisubtitlesdownloader.cpp \
10+
src/climain.cpp
11+
12+
HEADERS += \
13+
src/clisubtitlesdownloader.h \
14+
src/climain.h
15+
16+
RESOURCES += res/resources.qrc
17+
18+
MOC_DIR = tmp
19+
RCC_DIR = tmp
20+
OBJECTS_DIR = tmp
21+
INCLUDEPATH = src
22+
23+
include(../libqnapi/libqnapi.pri)
24+
25+
unix {
26+
TARGET = qnapic
27+
DESTDIR = ../
28+
CONFIG += link_pkgconfig
29+
PKGCONFIG += libmediainfo
30+
}
31+
32+
macx {
33+
QT_CONFIG -= no-pkg-config
34+
CONFIG -= app_bundle
35+
LIBS += -framework CoreFoundation
36+
QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.8
37+
QMAKE_CXXFLAGS_X86_64 = -mmacosx-version-min=10.8
38+
}
39+
40+
win32 {
41+
CONFIG += nostrip
42+
RC_FILE = ../win32/qnapi.rc
43+
TARGET = qnapic
44+
target.path = ../win32/out
45+
INSTALLS += target
46+
}
47+

cli/res/resources.qrc

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<RCC>
2+
<qresource prefix="/translations">
3+
<file alias="qnapi_it.qm">../../translations/qnapi_it.qm</file>
4+
<file alias="qnapi_pl.qm">../../translations/qnapi_pl.qm</file>
5+
</qresource>
6+
</RCC>

cli/src/climain.cpp

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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

cli/src/climain.h

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
#ifndef CLIMAIN_H
16+
#define CLIMAIN_H
17+
18+
#include "config/qnapiconfig.h"
19+
#include "parser/cliargparser.h"
20+
#include "parser/cliargparsersexecutor.h"
21+
#include "tr.h"
22+
#include "utils/console.h"
23+
24+
namespace CliMain {
25+
26+
Q_DECLARE_NAMESPACE_TR(CliMain)
27+
28+
void installTranslation(QCoreApplication &app, QTranslator *translator,
29+
const QNapiConfig &config);
30+
void printHeader(const Console &c);
31+
void printHelp(const Console &c,
32+
const QList<QSharedPointer<CliArgParser>> &cliArgParsers);
33+
void printHelpLanguages(const Console &c, const QNapiConfig &config);
34+
int processCommand(QVariant cliCommand, const QNapiConfig &config,
35+
const QList<QSharedPointer<CliArgParser>> &cliArgParsers);
36+
void sigHandler(int sig);
37+
void regSignal();
38+
39+
} // namespace CliMain
40+
41+
#endif // CLIMAIN_H

0 commit comments

Comments
 (0)