//TODO: add support of URL void Translator::translateHtml(QString filePath, QDir &docDir) { QFileInfo fileInfo(filePath); auto cmd = new QProcess(this); QTemporaryFile tmpDoc("document.html"); cmd->setWorkingDirectory(QDir(DATALOCATION).absoluteFilePath("apertium-all-dev/bin")); QStringList args; qApp->processEvents(); //deshtml #ifdef Q_OS_WIN //windows bug with backslash //use replace to fix windows bug args << "/u" << "/c" << "type" << "\"" + filePath.replace('/', QDir::separator()) + "\"" << "|" << "apertium-deshtml"; cmd->setNativeArguments(args.join(' ')); cmd->start("cmd.exe"); #endif cmd->waitForFinished(); cmd->waitForReadyRead(); if (!tmpDoc.open()) { emit fileTranslateRejected(); cmd->deleteLater(); return; } tmpDoc.setTextModeEnabled(true); tmpDoc.write(notLinuxTranslate(cmd->readAllStandardOutput()).toUtf8()); tmpDoc.close(); //rehtml //FIXME: rehtml or rehtml-noent??? args.clear(); #ifdef Q_OS_WIN args << "/u" << "/c" << "type" << "\"" + tmpDoc.fileName().replace('/', QDir::separator()) + "\"" << "|" << "apertium-rehtml"; cmd->setNativeArguments(args.join(' ')); cmd->start("cmd.exe"); #endif cmd->waitForFinished(); cmd->waitForReadyRead(); QString trFilePath = docDir.absoluteFilePath(fileInfo.baseName() + "_" + parent->getCurrentSourceLang() + "-" + parent->getCurrentTargetLang() + "." + fileInfo.suffix()); QFile newDoc(trFilePath); if (!newDoc.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) { emit fileTranslateRejected(); cmd->deleteLater(); return; } newDoc.write(cmd->readAllStandardOutput()); newDoc.close(); emit fileTranslated(trFilePath); cmd->deleteLater(); //#else // // cmd->start("ar", QStringList() << "x" << "data.tmp"); //#endif }
/*! * \brief Translates text on not Linux paltforms. */ QString Translator::notLinuxTranslate(QString text) { QString name = parent->getCurrentSourceLang() + "-" + parent->getCurrentTargetLang(); if (name.isEmpty()) return ""; QDir dir(QDir(DATALOCATION).absoluteFilePath("usr/share/apertium/modes")); if (!dir.exists() || !dir.exists(name + ".mode")) return ""; QFile file(dir.absoluteFilePath(name + ".mode")); if (file.open(QIODevice::ReadOnly) == false) { return ""; } QString mode = file.readAll(); file.close(); mode = mode.trimmed(); if (mode.isEmpty()) { return ""; } mode.replace("$1", "-g"); mode.remove("$2"); if (mode.indexOf("'/usr/share") == -1) { mode.replace(QRegularExpression("(\\s*)(/usr/share/\\S+)(\\s*)"), "\\1\"\\2\"\\3"); } mode.replace("/usr/share", QDir(DATALOCATION).absolutePath() + "/usr/share"); #ifdef Q_OS_WIN // Windows can't handle C:/ paths in ' quotes mode.replace("'", "\""); #define OS_SEP ";" #else #define OS_SEP ":" #endif auto env = QProcessEnvironment::systemEnvironment(); env.insert("PATH", QDir(DATALOCATION).absoluteFilePath("apertium-all-dev/bin") + OS_SEP + env.value("PATH")); env.insert("LC_ALL", "en_US.UTF-8"); auto run = new QProcess(this); run->setProcessEnvironment(env); run->setProcessChannelMode(QProcess::MergedChannels); #ifdef Q_OS_WIN run->setNativeArguments(mode); run->start("cmd", QStringList() << "/D" << "/Q" << "/S" << "/C"); #else run->start("/bin/sh", QStringList() << "-c" << mode); #endif run->waitForStarted(); run->write(text.toUtf8() + " "); run->closeWriteChannel(); run->waitForFinished(); run->deleteLater(); return QString::fromUtf8(run->readAll()); }
void KProcess::clearProgram() { Q_D(KProcess); d->prog.clear(); d->args.clear(); #ifdef Q_OS_WIN setNativeArguments(QString()); #endif }
void KProcess::setProgram(const QString &exe, const QStringList &args) { Q_D(KProcess); d->prog = exe; d->args = args; #ifdef Q_OS_WIN setNativeArguments(QString()); #endif }
void KProcess::setProgram(const QStringList &argv) { Q_D(KProcess); Q_ASSERT( !argv.isEmpty() ); d->args = argv; d->prog = d->args.takeFirst(); #ifdef Q_OS_WIN setNativeArguments(QString()); #endif }
void KProcess::setShellCommand(const QString &cmd) { Q_D(KProcess); KShell::Errors err; d->args = KShell::splitArgs( cmd, KShell::AbortOnMeta | KShell::TildeExpand, &err); if (err == KShell::NoError && !d->args.isEmpty()) { d->prog = KStandardDirs::findExe(d->args[0]); if (!d->prog.isEmpty()) { d->args.removeFirst(); #ifdef Q_OS_WIN setNativeArguments(QString()); #endif return; } } d->args.clear(); #ifdef Q_OS_UNIX // #ifdef NON_FREE // ... as they ship non-POSIX /bin/sh # if !defined(__linux__) && !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__) && !defined(__DragonFly__) && !defined(__GNU__) // If /bin/sh is a symlink, we can be pretty sure that it points to a // POSIX shell - the original bourne shell is about the only non-POSIX // shell still in use and it is always installed natively as /bin/sh. d->prog = QFile::symLinkTarget(QString::fromLatin1("/bin/sh")); if (d->prog.isEmpty()) { // Try some known POSIX shells. d->prog = KStandardDirs::findExe(QString::fromLatin1("ksh")); if (d->prog.isEmpty()) { d->prog = KStandardDirs::findExe(QString::fromLatin1("ash")); if (d->prog.isEmpty()) { d->prog = KStandardDirs::findExe(QString::fromLatin1("bash")); if (d->prog.isEmpty()) { d->prog = KStandardDirs::findExe(QString::fromLatin1("zsh")); if (d->prog.isEmpty()) // We're pretty much screwed, to be honest ... d->prog = QString::fromLatin1("/bin/sh"); } } } } # else d->prog = QString::fromLatin1("/bin/sh"); # endif d->args << QString::fromLatin1("-c") << cmd; #else // Q_OS_UNIX // KMacroExpander::expandMacrosShellQuote(), KShell::quoteArg() and // KShell::joinArgs() may generate these for security reasons. setEnv(PERCENT_VARIABLE, QLatin1String("%")); #ifndef _WIN32_WCE WCHAR sysdir[MAX_PATH + 1]; UINT size = GetSystemDirectoryW(sysdir, MAX_PATH + 1); d->prog = QString::fromUtf16((const ushort *) sysdir, size); d->prog += QLatin1String("\\cmd.exe"); setNativeArguments(QLatin1String("/V:OFF /S /C \"") + cmd + QLatin1Char('"')); #else d->prog = QLatin1String("\\windows\\cmd.exe"); setNativeArguments(QLatin1String("/S /C \"") + cmd + QLatin1Char('"')); #endif #endif }
void Translator::translateXlsx(QString filePath, QDir &docDir) { QFileInfo fileInfo(filePath); QTemporaryDir dir(docDir.absoluteFilePath(fileInfo.baseName() + ".XXXXXX")); auto cmd = new QProcess(this); QDir exep (qApp->applicationDirPath()); #ifndef Q_OS_LINUX cmd->start(exep.absoluteFilePath("7z"), QStringList() << "t" << fileInfo.absoluteFilePath()); #endif cmd->waitForReadyRead(); QString res = cmd->readAllStandardOutput(); if (res.contains("ERROR")) { emit fileTranslateRejected(); cmd->deleteLater(); return; } cmd->waitForFinished(); cmd->start(exep.absoluteFilePath("7z"), QStringList() << "x" << "-y" << fileInfo.absoluteFilePath() << "-o" + dir.path()); cmd->waitForReadyRead(); cmd->waitForFinished(); QStringList args; //for (QString sheetName : sheetDir.entryList(QStringList() << "sheet*.xml")) { QFile sheet (QDir(dir.path() + "/xl").absoluteFilePath("sharedStrings.xml")); QTemporaryFile tmpSheet(sheet.fileName()); cmd->setWorkingDirectory(QDir(DATALOCATION).absoluteFilePath("apertium-all-dev/bin")); qApp->processEvents(); //desxlsx args << "/u" << "/c" << "type" << "\"" + sheet.fileName().replace('/', QDir::separator()) + "\"" << "|" << "apertium-desxlsx"; cmd->setNativeArguments(args.join(' ')); args.clear(); cmd->start("cmd.exe"); cmd->waitForReadyRead(); cmd->waitForFinished(); if (!tmpSheet.open()) { emit fileTranslateRejected(); cmd->deleteLater(); return; } tmpSheet.setTextModeEnabled(true); tmpSheet.write(notLinuxTranslate(cmd->readAllStandardOutput()).toUtf8()); tmpSheet.close(); //rexlsx args << "/u" << "/c" << "type" << "\"" + tmpSheet.fileName().replace('/', QDir::separator()) + "\"" << "|" << "apertium-rexlsx"; cmd->setNativeArguments(args.join(" ")); args.clear(); cmd->start("cmd.exe"); cmd->waitForReadyRead(); if (!sheet.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) { emit fileTranslateRejected(); cmd->deleteLater(); return; } sheet.write(cmd->readAllStandardOutput()); cmd->waitForFinished(); sheet.close(); cmd->setWorkingDirectory(dir.path()); QString trFilePath = docDir.absoluteFilePath(fileInfo.baseName() + "_" + parent->getCurrentSourceLang() + "-" + parent->getCurrentTargetLang() + "." + fileInfo.suffix()); QFile(trFilePath).remove(); args << "a" << "-tzip" << "-y" << "\"" + trFilePath + "\"" << "\"" + dir.path() + "/*\""; cmd->setNativeArguments(args.join(' ')); cmd->start(exep.absoluteFilePath("7z")); cmd->waitForFinished(); emit fileTranslated(trFilePath); cmd->deleteLater(); }
void Translator::translateDocx(QString filePath, QDir &docDir) { QFileInfo fileInfo(filePath); QTemporaryDir dir(docDir.absoluteFilePath(fileInfo.baseName() + ".XXXXXX")); auto cmd = new QProcess(this); QDir exep (qApp->applicationDirPath()); cmd->start(exep.absoluteFilePath("7z"), QStringList() << "t" << fileInfo.absoluteFilePath()); cmd->waitForReadyRead(); if (cmd->readAllStandardOutput().contains("ERROR")) { emit fileTranslateRejected(); cmd->deleteLater(); return; } cmd->waitForFinished(); cmd->start(exep.absoluteFilePath("7z"), QStringList() << "x" << "-y" << fileInfo.absoluteFilePath() << "-o" + dir.path()); cmd->waitForReadyRead(); cmd->waitForFinished(); QTemporaryFile tmpDoc("document.xml"); QFile doc(dir.path() + "/word/document.xml"); cmd->setWorkingDirectory(QDir(DATALOCATION).absoluteFilePath("apertium-all-dev/bin")); QStringList args; qApp->processEvents(); //deswxml #ifdef Q_OS_WIN //windows bug with backslash //use replace to fix windows bug args << "/u" << "/c" << "type" << "\"" + doc.fileName().replace('/', QDir::separator()) + "\"" << "|" << "apertium-deswxml"; cmd->setNativeArguments(args.join(' ')); cmd->start("cmd.exe"); #endif cmd->waitForFinished(); cmd->waitForReadyRead(); if (!tmpDoc.open()) { emit fileTranslateRejected(); cmd->deleteLater(); return; } tmpDoc.setTextModeEnabled(true); tmpDoc.write(notLinuxTranslate(cmd->readAllStandardOutput()).toUtf8()); tmpDoc.close(); //rewxml args.clear(); #ifdef Q_OS_WIN args << "/u" << "/c" << "type" << "\"" + tmpDoc.fileName().replace('/', QDir::separator()) + "\"" << "|" << "apertium-rewxml"; cmd->setNativeArguments(args.join(' ')); cmd->start("cmd.exe"); #endif cmd->waitForFinished(); cmd->waitForReadyRead(); if (!doc.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) { emit fileTranslateRejected(); cmd->deleteLater(); return; } doc.write(cmd->readAllStandardOutput()); doc.close(); cmd->setWorkingDirectory(dir.path()); QString trFilePath = docDir.absoluteFilePath(fileInfo.baseName() + "_" + parent->getCurrentSourceLang() + "-" + parent->getCurrentTargetLang() + "." + fileInfo.suffix()); cmd->start(exep.absoluteFilePath("7z"), QStringList() << "a" << "-tzip" << "-y" << trFilePath << dir.path() + "/*"); cmd->waitForFinished(); emit fileTranslated(trFilePath); cmd->deleteLater(); }