| 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 <signal.h> |
| 16 | #include <QApplication> |
| 17 | #include <QFileInfo> |
| 18 | #include <QLibraryInfo> |
| 19 | #include <QMessageBox> |
| 20 | #include <QStringList> |
| 21 | #include <QSystemTrayIcon> |
| 22 | #include <QTextCodec> |
| 23 | #include <QTranslator> |
| 24 | #include "libqnapi.h" |
| 25 | #include "qnapiapp.h" |
| 26 | #include "qnapicli.h" |
| 27 | |
| 28 | QStringList parseArgs(int argc, char **argv); |
| 29 | void regSignal(); |
| 30 | void sigHandler(int); |
| 31 | |
| 32 | int main(int argc, char **argv) { |
| 33 | LibQNapi::init(argv[0]); |
| 34 | |
| 35 | const QNapiConfig config = LibQNapi::loadConfig(); |
| 36 | |
| 37 | QString uiLanguage = LibQNapi::uiLanguage(config.generalConfig()); |
| 38 | |
| 39 | bool isCliCall = QNapiCli::isCliCall(argc, argv); |
| 40 | |
| 41 | QStringList parsedFilePaths = parseArgs(argc, argv); |
| 42 | |
| 43 | regSignal(); |
| 44 | |
| 45 | bool quietBatch = config.generalConfig().quietBatch(); |
| 46 | #ifdef Q_OS_MAC |
| 47 | quietBatch = false; |
| 48 | #endif |
| 49 | |
| 50 | bool useGui = !isCliCall && !(quietBatch && !parsedFilePaths.isEmpty()); |
| 51 | |
| 52 | if (useGui) { |
| 53 | QNapiApp app(argc, argv, true, "QNapi"); |
| 54 | |
| 55 | QString resourceDir = |
| 56 | QLibraryInfo::location(QLibraryInfo::TranslationsPath); |
| 57 | |
| 58 | QTranslator qtTranslator, qnapiTranslator; |
| 59 | qtTranslator.load("qt_" + uiLanguage, resourceDir); |
| 60 | app.installTranslator(&qtTranslator); |
| 61 | |
| 62 | qnapiTranslator.load("qnapi_" + uiLanguage, ":/translations"); |
| 63 | app.installTranslator(&qnapiTranslator); |
| 64 | |
| 65 | app.setQuitOnLastWindowClosed(false); |
| 66 | |
| 67 | if (app.arguments().contains("-o") || |
| 68 | app.arguments().contains("--options")) { |
| 69 | app.setQuitOnLastWindowClosed(true); |
| 70 | app.showSettings(); |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | if (!app.isInstanceAllowed()) { |
| 75 | for (int i = 0; i < parsedFilePaths.size(); i++) { |
| 76 | QString &fileName = parsedFilePaths[i]; |
| 77 | QFileInfo fi(fileName); |
| 78 | app.sendRequest(fi.absoluteFilePath()); |
| 79 | } |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | if (config.firstrun()) { |
| 84 | if (QMessageBox::question(0, QObject::tr("First Run"), |
| 85 | QObject::tr("This is the first launch of the " |
| 86 | "program QNapi. Do you want to " |
| 87 | "configure it now?"), |
| 88 | QMessageBox::Yes | QMessageBox::No) == |
| 89 | QMessageBox::Yes) { |
| 90 | app.showSettings(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // Jesli podano parametry, ustawiamy tzw. batch mode |
| 95 | if (parsedFilePaths.size() > 0) { |
| 96 | app.progress()->setBatchMode(true); |
| 97 | |
| 98 | QString p, batchLang, batchLangBackup; |
| 99 | bool invalidLang = false, batchLangBackupPassed = false; |
| 100 | |
| 101 | for (int i = 1; i < argc; i++) { |
| 102 | p = argv[i]; |
| 103 | |
| 104 | if ((p == "-l") || (p == "--lang")) { |
| 105 | ++i; |
| 106 | if (i < argc) { |
| 107 | batchLang = SubtitleLanguage(argv[i]).toTwoLetter(); |
| 108 | if (batchLang.isEmpty()) { |
| 109 | invalidLang = true; |
| 110 | } |
| 111 | } else { |
| 112 | invalidLang = true; |
| 113 | } |
| 114 | } else if ((p == "-lb") || (p == "--lang-backup")) { |
| 115 | ++i; |
| 116 | if (i < argc) { |
| 117 | batchLangBackup = SubtitleLanguage(argv[i]).toTwoLetter(); |
| 118 | batchLangBackupPassed = true; |
| 119 | } |
| 120 | break; |
| 121 | } else if ((p == "--format") || (p == "-f")) { |
| 122 | ++i; |
| 123 | if (i < argc) { |
| 124 | if (!LibQNapi::subtitleFormatsRegistry() |
| 125 | ->select(argv[i]) |
| 126 | .isNull()) { |
| 127 | app.progress()->setTargetFormatOverride(argv[i]); |
| 128 | } |
| 129 | } |
| 130 | } else if ((p == "--extension") || (p == "-e")) { |
| 131 | ++i; |
| 132 | if (i < argc) { |
| 133 | app.progress()->setTargetExtOverride(argv[i]); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | if (invalidLang) { |
| 139 | if (QMessageBox::question(0, "QNapi", |
| 140 | "Invalid language code!\n" |
| 141 | "Do you want to download the subtitles in " |
| 142 | "the default language?", |
| 143 | QMessageBox::Yes | QMessageBox::No) != |
| 144 | QMessageBox::Yes) { |
| 145 | return 0; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | app.progress()->setBatchLanguages(batchLang, batchLangBackup, |
| 150 | batchLangBackupPassed); |
| 151 | |
| 152 | if (QFileInfo(parsedFilePaths.at(0)).isDir()) { |
| 153 | if (!app.showScanDialog(parsedFilePaths.at(0))) return 1; |
| 154 | } else { |
| 155 | app.progress()->enqueueFiles(parsedFilePaths); |
| 156 | if (!app.progress()->download()) return 1; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | // Jesli nie dzialamy w trybie pobierania, mozemy ew. utworzyc ikone w |
| 161 | // tray-u badz pokazac okno wyboru plikow z filmami |
| 162 | if (!app.progress()->isBatchMode()) { |
| 163 | // Jesli nie ma traya, od razu wyswietlamy okienko z wyborem pliku |
| 164 | if (!QSystemTrayIcon::isSystemTrayAvailable()) { |
| 165 | if (!app.progress()->isBatchMode()) { |
| 166 | app.progress()->setBatchMode(true); |
| 167 | if (!app.showOpenDialog()) return 1; |
| 168 | } |
| 169 | } else // Jesli ikona w tray-u jest obsligiwana, tworzymy ja |
| 170 | { |
| 171 | app.createTrayIcon(); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return app.exec(); |
| 176 | } else { |
| 177 | QNapiCli app(argc, argv, config, uiLanguage); |
| 178 | return app.exec(); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | QStringList parseArgs(int argc, char **argv) { |
| 183 | QStringList parsedFilePaths; |
| 184 | |
| 185 | for (int i = 1; i < argc; i++) { |
| 186 | #ifdef Q_OS_WIN |
| 187 | QString p = QString::fromLocal8Bit(argv[i]); |
| 188 | #else |
| 189 | QString p = argv[i]; |
| 190 | #endif |
| 191 | if (p.startsWith("file://")) p = p.remove(0, 7); |
| 192 | |
| 193 | if ((parsedFilePaths.size() == 0) && QFileInfo(p).isDir()) { |
| 194 | parsedFilePaths << p; |
| 195 | break; |
| 196 | } |
| 197 | |
| 198 | if (QFileInfo(p).isFile()) parsedFilePaths << p; |
| 199 | } |
| 200 | return parsedFilePaths; |
| 201 | } |
| 202 | |
| 203 | void regSignal() { |
| 204 | #ifdef Q_OS_WIN |
| 205 | signal(SIGTERM, sigHandler); |
| 206 | signal(SIGINT, sigHandler); |
| 207 | #else |
| 208 | struct sigaction sa; |
| 209 | memset(&sa, 0, sizeof(struct sigaction)); |
| 210 | sigemptyset(&sa.sa_mask); |
| 211 | sa.sa_handler = sigHandler; |
| 212 | sigaction(SIGTERM, &sa, 0); |
| 213 | sigaction(SIGINT, &sa, 0); |
| 214 | #endif |
| 215 | } |
| 216 | |
| 217 | void sigHandler(int sig) { |
| 218 | Q_UNUSED(sig); |
| 219 | |
| 220 | std::cout << std::endl |
| 221 | << QObject::tr("QNapi: deleting temporary files...").toStdString() |
| 222 | << std::endl; |
| 223 | |
| 224 | const QNapiConfig config = LibQNapi::loadConfig(); |
| 225 | QString tmpPath = config.generalConfig().tmpPath(); |
| 226 | QDir tmpDir(tmpPath); |
| 227 | |
| 228 | QStringList filters; |
| 229 | filters << "QNapi-*-rc"; |
| 230 | filters << "QNapi.*.tmp"; |
| 231 | |
| 232 | QFileInfoList files = tmpDir.entryInfoList(filters); |
| 233 | |
| 234 | foreach (QFileInfo file, files) { QFile::remove(file.filePath()); } |
| 235 | |
| 236 | std::cout << QObject::tr("QNapi: finished.").toStdString() << std::endl; |
| 237 | |
| 238 | exit(666); |
| 239 | } |