| 188 | } |
| 189 | |
| 190 | QString ChatHistoryStore::generateChatFileName(const QString &shortMessage, const QString &dir) const |
| 191 | { |
| 192 | static const QRegularExpression saitizeSymbols = QRegularExpression("[\\/:*?\"<>|\\s]"); |
| 193 | static const QRegularExpression underSymbols = QRegularExpression("_+"); |
| 194 | |
| 195 | QStringList parts; |
| 196 | QString sanitizedMessage = shortMessage; |
| 197 | sanitizedMessage.replace(saitizeSymbols, "_"); |
| 198 | sanitizedMessage.replace(underSymbols, "_"); |
| 199 | sanitizedMessage = sanitizedMessage.trimmed(); |
| 200 | |
| 201 | if (!sanitizedMessage.isEmpty()) { |
| 202 | if (sanitizedMessage.startsWith('_')) { |
| 203 | sanitizedMessage.remove(0, 1); |
| 204 | } |
| 205 | if (sanitizedMessage.endsWith('_')) { |
| 206 | sanitizedMessage.chop(1); |
| 207 | } |
| 208 | |
| 209 | QString fullPath = QDir(dir).filePath(sanitizedMessage); |
| 210 | QFileInfo fileInfo(fullPath); |
| 211 | if (!fileInfo.exists() && QFileInfo(fileInfo.path()).isWritable()) { |
| 212 | parts << sanitizedMessage; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | parts << QDateTime::currentDateTime().toString("yyyy-MM-dd_HH-mm"); |
| 217 | |
| 218 | QString fileName = parts.join("_"); |
| 219 | QString fullPath = QDir(dir).filePath(fileName); |
| 220 | QFileInfo finalCheck(fullPath); |
| 221 | |
| 222 | if (fileName.isEmpty() || finalCheck.exists() || !QFileInfo(finalCheck.path()).isWritable()) { |
| 223 | fileName = QString("chat_%1").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd_HH-mm")); |
| 224 | } |
| 225 | |
| 226 | return fileName; |
| 227 | } |
| 228 | |
| 229 | } // namespace QodeAssist::Chat |