// Delete all temporary directories for an application int PathUtils::removeTemporaryApplicationDirs(QString appName) { if (appName.isNull()) { appName = qApp->applicationName(); } auto dirName = TEMP_DIR_FORMAT.arg(appName).arg("*").arg("*"); QDir rootTempDir = QDir::tempPath(); auto dirs = rootTempDir.entryInfoList({ dirName }, QDir::Dirs); int removed = 0; for (auto& dir : dirs) { auto dirName = dir.fileName(); auto absoluteDirPath = QDir(dir.absoluteFilePath()); QRegularExpression re { "^" + QRegularExpression::escape(appName) + "\\-(?<pid>\\d+)\\-(?<timestamp>\\d+)$" }; auto match = re.match(dirName); if (match.hasMatch()) { auto pid = match.capturedRef("pid").toLongLong(); auto timestamp = match.capturedRef("timestamp"); if (!processIsRunning(pid)) { qDebug() << " Removing old temporary directory: " << dir.absoluteFilePath(); absoluteDirPath.removeRecursively(); removed++; } else { qDebug() << " Not removing (process is running): " << dir.absoluteFilePath(); } } } return removed; }
KTextEditor::Cursor KTextEditorHelpers::extractCursor(const QString& input, int* pathLength) { static const QRegularExpression pattern(QStringLiteral(":(\\d+)(?::(\\d+))?$")); const auto match = pattern.match(input); if (!match.hasMatch()) { if (pathLength) *pathLength = input.length(); return KTextEditor::Cursor::invalid(); } int line = match.capturedRef(1).toInt() - 1; // don't use an invalid column when the line is valid int column = qMax(0, match.captured(2).toInt() - 1); if (pathLength) *pathLength = match.capturedStart(0); return {line, column}; }
bool PathUtils::deleteMyTemporaryDir(QString dirName) { QDir rootTempDir = QDir::tempPath(); QString appName = qApp->applicationName(); QRegularExpression re { "^" + QRegularExpression::escape(appName) + "\\-(?<pid>\\d+)\\-(?<timestamp>\\d+)$" }; auto match = re.match(dirName); auto pid = match.capturedRef("pid").toLongLong(); if (match.hasMatch() && rootTempDir.exists(dirName) && pid == qApp->applicationPid()) { auto absoluteDirPath = QDir(rootTempDir.absoluteFilePath(dirName)); bool success = absoluteDirPath.removeRecursively(); if (success) { qDebug() << " Removing temporary directory: " << absoluteDirPath.absolutePath(); } else { qDebug() << " Failed to remove temporary directory: " << absoluteDirPath.absolutePath(); } return success; } return false; }