Debian Codegraph @

gui/src/qnapicli.cpp

raw symbols SCIP
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 "qnapicli.h"
16 #include "libqnapi.h"
17 #include "subtitlelanguage.h"
18
19 #include <QTranslator>
20
21 QNapiCli::QNapiCli(int argc, char **argv, const QNapiConfig &config,
22 const QString &uiLanguage)
23 : QCoreApplication(argc, argv),
24 config(config),
25 uiLanguage(uiLanguage),
26 mode(CM_UNSET),
27 showPolicy(SLP_USE_CONFIG),
28 langBackupPassed(false) {
29 args = arguments();
30 }
31
32 bool QNapiCli::isCliCall(int argc, char **argv) {
33 QString p;
34
35 #ifdef Q_OS_WIN
36 for (int i = 1; i < argc; i++) {
37 p = argv[i];
38 if ((p == "--quiet") || (p == "-q")) return true;
39 }
40 return false;
41 #else
42 for (int i = 1; i < argc; i++) {
43 p = argv[i];
44
45 if (p == "--options") {
46 return false;
47 }
48
49 if ((p == "--console") || (p == "-c") || (p == "--quiet") || (p == "-q")) {
50 return true;
51 }
52
53 if ((p == "--help") || (p == "-h") || (p == "--help-languages") ||
54 (p == "-hl")) {
55 return true;
56 }
57 }
58
59 #if defined(Q_OS_MAC)
60 return false;
61 #elif (defined(Q_OS_UNIX) || defined(Q_OS_LINUX))
62 if (getenv("DISPLAY") == 0) return true;
63 return false;
64 #else
65 return false;
66 #endif
67 #endif
68 }
69
70 bool QNapiCli::analyze(const QStringList &args) {
71 QString p;
72
73 for (int i = 1; i < args.size(); i++) {
74 p = args[i];
75
76 if ((p == "--help") || (p == "-h")) {
77 mode = CM_HELP;
78 return true;
79 } else if ((p == "--help-languages") || (p == "-hl")) {
80 mode = CM_HELP_LANGUAGES;
81 return true;
82 } else if ((p == "--console") || (p == "-c")) {
83 if (mode == CM_UNSET) {
84 #ifdef Q_OS_WIN
85 mode = CM_QUIET;
86 showPolicy = SLP_NEVER_SHOW;
87 #else
88 mode = CM_CONSOLE;
89 #endif
90 }
91 } else if ((p == "--quiet") || (p == "-q")) {
92 mode = CM_QUIET;
93 showPolicy = SLP_NEVER_SHOW;
94 } else if ((p == "--lang") || (p == "-l")) {
95 ++i;
96 if (i >= args.size()) return false;
97 p = args[i];
98
99 lang = SubtitleLanguage(p).toTwoLetter();
100 if (lang.isEmpty()) {
101 printCli(tr("Invalid language code: %1").arg(p));
102 return false;
103 }
104 } else if ((p == "--lang-backup") || (p == "-lb")) {
105 ++i;
106 if (i >= args.size()) return false;
107 p = args[i];
108
109 langBackup = SubtitleLanguage(p).toTwoLetter();
110 langBackupPassed = true;
111 } else if ((p == "--format") || (p == "-f")) {
112 ++i;
113 if (i >= args.size()) return false;
114 p = args[i];
115
116 if (LibQNapi::subtitleFormatsRegistry()->select(p).isNull()) {
117 printCli(tr("Invalid target subtitles format: %1").arg(p));
118 return false;
119 }
120
121 config = config.setPostProcessingConfig(
122 config.postProcessingConfig().setSubFormat(p));
123 } else if ((p == "--extension") || (p == "-e")) {
124 ++i;
125 if (i >= args.size()) return false;
126 p = args[i];
127
128 config = config.setPostProcessingConfig(
129 config.postProcessingConfig().setSubExtension(p));
130 } else if ((p == "--show-list") || (p == "-s")) {
131 if (mode != CM_QUIET) showPolicy = SLP_SHOW;
132 } else if ((p == "--dont-show-list") || (p == "-d")) {
133 showPolicy = SLP_NEVER_SHOW;
134 } else if (QFileInfo(p).isFile()) {
135 movieList << p;
136 }
137 }
138
139 if (config.generalConfig().quietBatch() && !movieList.isEmpty()) {
140 mode = CM_QUIET;
141 return true;
142 }
143
144 #if (defined(Q_OS_UNIX) || defined(Q_OS_LINUX))
145 if ((getenv("DISPLAY") == 0) && (mode == CM_UNSET)) mode = CM_CONSOLE;
146 #endif
147
148 return true;
149 }
150
151 int QNapiCli::exec() {
152 QTranslator qnapiTranslator;
153 qnapiTranslator.load("qnapi_" + uiLanguage, ":/translations");
154 installTranslator(&qnapiTranslator);
155
156 if (!analyze(args)) return EC_CMD_LINE_ARG_PARSING_ERROR;
157
158 if (mode == CM_UNSET) return 1;
159
160 if (mode != CM_QUIET) printHeader();
161
162 if (mode == CM_HELP_LANGUAGES) {
163 printHelpLanguages();
164 return EC_OK;
165 }
166
167 if ((mode == CM_CONSOLE || mode == CM_QUIET) && (movieList.isEmpty())) {
168 printCli(tr("File not found!"));
169 return EC_MOVIE_FILE_NOT_FOUND;
170 }
171
172 if (mode == CM_HELP) {
173 printHelp(QFileInfo(args.first()).fileName());
174 return EC_OK;
175 }
176
177 QNapi napi(config);
178
179 if (!napi.checkP7ZipPath()) {
180 printCli(
181 tr("Path to the program p7zip is incorrect! Check your settings."));
182 return EC_P7ZIP_UNAVAILABLE;
183 }
184
185 if (!napi.checkTmpPath()) {
186 printCli(
187 tr("Can not write to the temporary directory! Check your settings."));
188 return EC_CANNOT_WRITE_TMP_DIR;
189 }
190
191 if (lang.isEmpty()) lang = config.generalConfig().language();
192
193 if (!langBackupPassed) langBackup = config.generalConfig().backupLanguage();
194
195 foreach (QString movie, movieList) {
196 printCli(tr(" * Downloading subtitles for '%1'")
197 .arg(QFileInfo(movie).fileName()));
198
199 napi.setMoviePath(movie);
200
201 if (!napi.checkWritePermissions()) {
202 printCli(tr(" No permission to write to the directory '%1'!")
203 .arg(QFileInfo(movie).path()));
204 return EC_NO_WRITE_PERMISSIONS;
205 }
206
207 napi.clearSubtitlesList();
208
209 printCli(tr(" Calculating checksums..."));
210 napi.checksum();
211
212 bool found = false;
213 SearchPolicy sp = config.generalConfig().searchPolicy();
214
215 if (sp == SP_SEARCH_ALL_WITH_BACKUP_LANG) {
216 foreach (QString e, napi.listLoadedEngines()) {
217 printCli(tr(" Searching for subtitles [%1] (%2)...").arg(lang, e));
218 found = napi.lookForSubtitles(lang, e) || found;
219 printCli(tr(" Searching for subtitles in alternative language [%1] "
220 "(%2)...")
221 .arg(langBackup, e));
222 found = napi.lookForSubtitles(langBackup, e) || found;
223 }
224 } else {
225 foreach (QString e, napi.listLoadedEngines()) {
226 printCli(tr(" Searching for subtitles [%1] (%2)...").arg(lang, e));
227 found = napi.lookForSubtitles(lang, e) || found;
228
229 if (sp == SP_BREAK_IF_FOUND && found) break;
230 }
231
232 if (!found && !langBackup.isEmpty()) {
233 foreach (QString e, napi.listLoadedEngines()) {
234 printCli(tr(" Searching for subtitles in alternative language [%1] "
235 "(%2)...")
236 .arg(langBackup, e));
237 found = napi.lookForSubtitles(langBackup, e) || found;
238
239 if (sp == SP_BREAK_IF_FOUND && found) break;
240 }
241 }
242 }
243
244 if (!found) {
245 printCli(tr(" Subtitles not found!"));
246 return EC_SUBTITLES_NOT_FOUND;
247 }
248
249 int selIdx = 0;
250
251 bool showList = false;
252 bool napiShowList = napi.needToShowList();
253
254 if ((mode != CM_QUIET) && (showPolicy != SLP_NEVER_SHOW))
255 showList = napiShowList;
256 else if (showPolicy == SLP_SHOW)
257 showList = true;
258
259 // jesli mozna i potrzeba, listujemy dostepne napisy
260 if (showList) {
261 bool ok = false;
262
263 printCli(tr(" 0)\tDo not download subtitles for this video"));
264 printCli(tr(" Found subtitles:"));
265
266 int i = 1;
267
268 QList<SubtitleInfo> list = napi.listSubtitles();
269
270 foreach (SubtitleInfo s, list) {
271 QString resolution = "";
272
273 if (s.resolution == SUBTITLE_GOOD)
274 resolution = tr(" (good)");
275 else if (s.resolution == SUBTITLE_BAD)
276 resolution = tr(" (bad)");
277
278 printCli(QString(" %1)\t%2 (%3) (%4) (%5)%6")
279 .arg(i++)
280 .arg(s.name)
281 .arg(s.format)
282 .arg(s.lang)
283 .arg(s.engine)
284 .arg(resolution));
285 }
286
287 while (!ok) {
288 std::cout << tr(" Select subtitles to download: ").toStdString();
289 char line[8];
290 std::cin.getline(line, 8);
291
292 selIdx = QString(line).toInt(&ok);
293
294 if (!ok) {
295 printCli(tr(" You must enter a number!"));
296 std::cin.clear();
297 } else if ((selIdx > list.size()) || (selIdx < 0)) {
298 ok = false;
299 printCli(tr(" Enter a number from the list!"));
300 }
301 }
302
303 --selIdx;
304 } else {
305 selIdx = napi.bestIdx();
306 }
307
308 if (selIdx == -1) continue;
309
310 printCli(tr(" Downloading subtitles..."));
311 if (!napi.download(selIdx)) {
312 printCli(tr(" Unable to download subtitles!"));
313 return EC_COULD_NOT_DOWNLOAD;
314 }
315
316 printCli(tr(" Unpacking subtitles..."));
317 if (!napi.unpack(selIdx)) {
318 printCli(tr(" Failed to unpack subtitles!"));
319 return EC_COULD_NOT_UNARCHIVE;
320 }
321
322 if (napi.ppEnabled()) {
323 printCli(tr(" Post-processing subtitles file..."));
324 napi.postProcessSubtitles();
325 }
326
327 printCli(tr(" Adjusting subtitles..."));
328 if (!napi.matchSubtitles()) {
329 printCli(tr(" Could not adjust subtitles!"));
330 return EC_COULD_NOT_MATCH;
331 }
332
333 napi.cleanup();
334 }
335
336 return EC_OK;
337 }
338
339 void QNapiCli::printHeader() {
340 printCli(tr("QNapi %1, %2")
341 .arg(LibQNapi::displayableVersion())
342 .arg(LibQNapi::webpageUrl()));
343 printCli(tr("Qt version: %1").arg(qVersion()));
344 printCli();
345 }
346
347 void QNapiCli::printHelp(const QString &binaryFileName) {
348 QString formats =
349 LibQNapi::subtitleFormatsRegistry()->listFormatNames().join(",");
350
351 printCli(tr("QNapi is distributed under the GNU General Public License v2."));
352 printCli();
353 printCli(tr("Syntax: %1 [options] [list of files]").arg(binaryFileName));
354 printCli(tr("Available options:"));
355 printCli(tr(" -c, --console Download subtitles with console"));
356 printCli(
357 tr(" -q, --quiet Download subtitles quietly without "
358 "showing"));
359 printCli(
360 tr(" any messages or windows (implies -d)"));
361 printCli();
362 printCli(
363 tr(" -s, --show-list Show a list of subtitles (works only "
364 "with -c)"));
365 printCli(
366 tr(" -d, --dont-show-list Do not show a list of subtitles (works "
367 "only with -c)"));
368 printCli();
369 printCli(tr(" -l, --lang [code] Preferred subtitles language"));
370 printCli(tr(" -lb,--lang-backup [code] Alternative subtitles language"));
371 printCli(tr(" -f, --format [format] Select target subtitles file "
372 "format (%1)")
373 .arg(formats));
374 printCli(tr(
375 " -e, --extension [ext] Select target subtitles file extension"));
376 printCli();
377 printCli(tr(" -o, --options Show program options (only GUI)"));
378 printCli(tr(" -h, --help Show help text"));
379 printCli(tr(
380 " -hl,--help-languages List of available subtitles languages"));
381 printCli();
382 }
383
384 void QNapiCli::printHelpLanguages() {
385 printCli(
386 tr("List of languages recognized by QNapi, including corresponding"));
387 printCli(tr("two-letter language codes:"));
388 printCli();
389
390 SubtitleLanguage L, LB;
391 QStringList langs = L.listLanguages();
392
393 foreach (QString lang, langs) {
394 L.setLanguage(lang);
395 printCli(QString(" %1 - %2").arg(L.toTwoLetter()).arg(lang));
396 }
397
398 L.setLanguage(config.generalConfig().language());
399 LB.setLanguage(config.generalConfig().backupLanguage());
400
401 printCli();
402 printCli(tr("Current default subtitles language: %1 (%2)")
403 .arg(L.toFullName())
404 .arg(L.toTwoLetter()));
405
406 if (LB.toFullName().isEmpty()) {
407 printCli(tr("No alternative subtitles language has been set"));
408 } else {
409 printCli(tr("Current alternative subtitles language: %1 (%2)")
410 .arg(LB.toFullName())
411 .arg(LB.toTwoLetter()));
412 }
413 }
414
415 void QNapiCli::printCli(const QString &string) {
416 if (mode != CM_QUIET) std::cout << string.toStdString() << std::endl;
417 }