Autoset terminal options if not setted before.

This commit is contained in:
Roy Qu 2023-09-21 12:19:17 +08:00
parent 1e645d7770
commit f145e87962
2 changed files with 61 additions and 75 deletions

View File

@ -3773,28 +3773,8 @@ void Settings::Environment::setIconZoomFactor(double newIconZoomFactor)
QString Settings::Environment::queryPredefinedTerminalArgumentsPattern(const QString &executable) const QString Settings::Environment::queryPredefinedTerminalArgumentsPattern(const QString &executable) const
{ {
#ifdef Q_OS_WINDOWS for (const TerminalItem& item: loadTerminalList()) {
QString terminalListFilename(":/config/terminal-windows.json"); if (item.terminal.compare(executable,PATH_SENSITIVITY)==0) return item.param;
#else // UNIX
QString terminalListFilename(":/config/terminal-unix.json");
#endif
QFile terminalListFile(terminalListFilename);
if (!terminalListFile.open(QFile::ReadOnly))
throw FileError(QObject::tr("Can't open file '%1' for read.")
.arg(terminalListFilename));
QByteArray terminalListContent = terminalListFile.readAll();
QJsonDocument terminalListDocument(QJsonDocument::fromJson(terminalListContent));
// determing terminal (if not set yet) and build predefined arguments pattern map from our list
for (const auto &terminalGroup: terminalListDocument.array()) {
const QJsonArray &terminals = terminalGroup.toObject()["terminals"].toArray();
for (const auto &terminal_: terminals) {
const QJsonObject &terminal = terminal_.toObject();
const QString &path = terminal["path"].toString();
const QString &termExecutable = QFileInfo(path).fileName();
const QString &pattern = terminal["argsPattern"].toString();
if (QString::compare( executable , termExecutable, PATH_SENSITIVITY)==0) return pattern;
}
} }
return QString(); return QString();
} }
@ -3809,59 +3789,60 @@ void Settings::Environment::setUseCustomTerminal(bool newUseCustomTerminal)
mUseCustomTerminal = newUseCustomTerminal; mUseCustomTerminal = newUseCustomTerminal;
} }
//bool Settings::Environment::checkAndSetTerminal(QString terminalPath, QString argsPattern) void Settings::Environment::checkAndSetTerminal()
//{ {
// QStringList patternItems = splitProcessCommand(argsPattern); if (!mUseCustomTerminal || !mTerminalPath.isEmpty()) return;
QStringList pathList = getExecutableSearchPaths();
QList<TerminalItem> terminalList = loadTerminalList();
for (const QString &dirPath: pathList) {
QDir dir{dirPath};
for (const TerminalItem& termItem:terminalList) {
QString absoluteTerminalPath = dir.absoluteFilePath(termItem.terminal);
if(fileExists(absoluteTerminalPath)) {
mTerminalPath = absoluteTerminalPath;
mTerminalArgumentsPattern = termItem.param;
return;
}
}
}
//Can't Find a term
QMessageBox::critical(
nullptr,
QCoreApplication::tr("Settings","Error"),
QCoreApplication::tr("Settings","Can't find terminal program!"));
}
// if (patternItems.empty() || QList<Settings::Environment::TerminalItem> Settings::Environment::loadTerminalList() const
// !(patternItems.contains("$argv") || patternItems.contains("$command") || patternItems.contains("$tmpfile")) // program not referenced {
// ) #ifdef Q_OS_WINDOWS
// return false; QString terminalListFilename(":/config/terminal-windows.json");
#else // UNIX
QString terminalListFilename(":/config/terminal-unix.json");
#endif
QFile terminalListFile(terminalListFilename);
if (!terminalListFile.open(QFile::ReadOnly))
throw FileError(QObject::tr("Can't open file '%1' for read.")
.arg(terminalListFilename));
QByteArray terminalListContent = terminalListFile.readAll();
QJsonDocument terminalListDocument(QJsonDocument::fromJson(terminalListContent));
// // `term` is not referenced ("$argv"), QList<Settings::Environment::TerminalItem> result;
// // or is not directly called ("open -app $term -args $tmpfile"), // determing terminal (if not set yet) and build predefined arguments pattern map from our list
// // do not check terminal path for (const auto &terminalGroup: terminalListDocument.array()) {
// if (patternItems[0] != "$term") { const QJsonArray &terminals = terminalGroup.toObject()["terminals"].toArray();
// setTerminalPath(terminalPath); for (const auto &terminal_: terminals) {
// setTerminalArgumentsPattern(argsPattern); const QJsonObject &terminal = terminal_.toObject();
// return true; const QString &path = terminal["path"].toString();
// } const QString &termExecutable = QFileInfo(path).fileName();
const QString &pattern = terminal["argsPattern"].toString();
//#define DO_CHECK_AND_SET do { \ Settings::Environment::TerminalItem terminalItem;
// if (termPathInfo.isFile() && termPathInfo.isReadable() && termPathInfo.isExecutable()) { \ terminalItem.terminal = path;
// mTerminalPath = terminalPath; \ terminalItem.param = pattern;
// mTerminalArgumentsPattern = argsPattern; \ result.append(terminalItem);
// return true; \ }
// } \ }
// } while (0) return result;
}
// switch (getPathUnixExecSemantics(terminalPath)) {
// case UnixExecSemantics::Absolute: {
// QFileInfo termPathInfo(terminalPath);
// DO_CHECK_AND_SET;
// break;
// }
// case UnixExecSemantics::RelativeToCwd: {
// QDir appDir(pSettings->dirs().appDir());
// QString absoluteTerminalPath = appDir.absoluteFilePath(terminalPath);
// QFileInfo termPathInfo(absoluteTerminalPath);
// DO_CHECK_AND_SET;
// break;
// }
// case UnixExecSemantics::SearchInPath: {
// QStringList pathList = getExecutableSearchPaths();
// for (const QString &dir: pathList) {
// QString absoluteTerminalPath = QDir(dir).absoluteFilePath(terminalPath);
// QFileInfo termPathInfo(absoluteTerminalPath);
// DO_CHECK_AND_SET;
// }
// break;
// }
// }
//#undef DO_CHECK_AND_SET
// return false;
//}
void Settings::Environment::doSave() void Settings::Environment::doSave()
{ {

View File

@ -602,8 +602,13 @@ public:
void setUseCustomTerminal(bool newUseCustomTerminal); void setUseCustomTerminal(bool newUseCustomTerminal);
private: private:
// bool checkAndSetTerminal(QString terminalPath, QString argsPattern); struct TerminalItem {
bool updateTerminalList(); QString terminal;
QString param;
};
void checkAndSetTerminal();
QList<TerminalItem> loadTerminalList() const;
//Appearance //Appearance
QString mTheme; QString mTheme;